Skip to content

Commit

Permalink
[MLGO] Force persistency in tflite buffers.
Browse files Browse the repository at this point in the history
When training large models, we encounter use-after-free bugs when
writing to the input tensors for various MLGO models. This patch fixes the
issue by marking the tensors as "persistent".

Differential Revision: https://reviews.llvm.org/D135739
  • Loading branch information
Jacob Hegna committed Oct 12, 2022
1 parent 2d8d2be commit 9d93a98
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions llvm/lib/Analysis/TFLiteUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ TFModelEvaluatorImpl::TFModelEvaluatorImpl(
tflite::InterpreterBuilder Builder(*Model, Resolver);
Builder(&Interpreter);

// We assume the input buffers are valid for the lifetime of the interpreter.
// By default, tflite allocates memory in an arena and will periodically take
// away memory and reallocate it in a different location after evaluations in
// order to improve utilization of the buffers owned in the arena. So, we
// explicitly mark our input buffers as persistent to avoid this behavior.
for (size_t I = 0; I < Interpreter->inputs().size(); ++I)
Interpreter->tensor(I)->allocation_type =
TfLiteAllocationType::kTfLiteArenaRwPersistent;

if (!Interpreter ||
Interpreter->AllocateTensors() != TfLiteStatus::kTfLiteOk) {
invalidate();
Expand Down

0 comments on commit 9d93a98

Please sign in to comment.