Skip to content

Commit 9d93a98

Browse files
author
Jacob Hegna
committed
[MLGO] Force persistency in tflite buffers.
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
1 parent 2d8d2be commit 9d93a98

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

llvm/lib/Analysis/TFLiteUtils.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,15 @@ TFModelEvaluatorImpl::TFModelEvaluatorImpl(
121121
tflite::InterpreterBuilder Builder(*Model, Resolver);
122122
Builder(&Interpreter);
123123

124+
// We assume the input buffers are valid for the lifetime of the interpreter.
125+
// By default, tflite allocates memory in an arena and will periodically take
126+
// away memory and reallocate it in a different location after evaluations in
127+
// order to improve utilization of the buffers owned in the arena. So, we
128+
// explicitly mark our input buffers as persistent to avoid this behavior.
129+
for (size_t I = 0; I < Interpreter->inputs().size(); ++I)
130+
Interpreter->tensor(I)->allocation_type =
131+
TfLiteAllocationType::kTfLiteArenaRwPersistent;
132+
124133
if (!Interpreter ||
125134
Interpreter->AllocateTensors() != TfLiteStatus::kTfLiteOk) {
126135
invalidate();

0 commit comments

Comments
 (0)