Skip to content

Commit 9a2336d

Browse files
authored
Sync from tflite-micro. (tensorflow#173)
1 parent 3f0bb1a commit 9a2336d

File tree

5 files changed

+53
-33
lines changed

5 files changed

+53
-33
lines changed

src/tensorflow/lite/micro/micro_allocation_info.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
1+
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -204,6 +204,14 @@ TfLiteStatus AllocationInfoBuilder::InitializeAllocationInfo(
204204
(current->bytes != 0);
205205
if (offline_offsets) {
206206
current->offline_offset = offline_offsets[i];
207+
208+
// Mark offline planned variable tensors so they can get an offline
209+
// offset and be handled offline.
210+
if (subgraph->tensors()->Get(i)->is_variable() &&
211+
current->offline_offset != kOnlinePlannedBuffer) {
212+
current->needs_allocating = true;
213+
}
214+
207215
} else {
208216
current->offline_offset = kOnlinePlannedBuffer;
209217
}

src/tensorflow/lite/micro/micro_allocator.cpp

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
1+
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -490,15 +490,6 @@ TfLiteStatus MicroAllocator::FinishModelAllocation(
490490
TF_LITE_ENSURE_STATUS(AllocateScratchBufferHandles(
491491
scratch_buffer_handles, scratch_buffer_request_count_));
492492

493-
// Allocate buffers for variable tensors.
494-
for (size_t subgraph_idx = 0; subgraph_idx < model->subgraphs()->size();
495-
subgraph_idx++) {
496-
const SubGraph* subgraph = model->subgraphs()->Get(subgraph_idx);
497-
TFLITE_DCHECK(subgraph != nullptr);
498-
TF_LITE_ENSURE_STATUS(AllocateVariables(
499-
subgraph, subgraph_allocations[subgraph_idx].tensors));
500-
}
501-
502493
// Plan all subgraphs and scratch buffers together.
503494
TF_LITE_ENSURE_STATUS(CommitStaticMemoryPlan(model, subgraph_allocations,
504495
*scratch_buffer_handles));
@@ -754,23 +745,27 @@ TfLiteStatus MicroAllocator::AllocateTfLiteEvalTensors(
754745
return kTfLiteOk;
755746
}
756747

757-
TfLiteStatus MicroAllocator::AllocateVariables(const SubGraph* subgraph,
758-
TfLiteEvalTensor* eval_tensors) {
748+
TfLiteStatus MicroAllocator::AllocateVariables(
749+
const SubGraph* subgraph, TfLiteEvalTensor* eval_tensors,
750+
const int32_t* offline_planner_offsets) {
759751
for (size_t i = 0; i < subgraph->tensors()->size(); ++i) {
760752
auto* tensor = subgraph->tensors()->Get(i);
761753
if (tensor->is_variable()) {
762-
size_t buffer_size;
763-
TF_LITE_ENSURE_STATUS(
764-
TfLiteEvalTensorByteLength(&eval_tensors[i], &buffer_size));
754+
if (offline_planner_offsets == nullptr ||
755+
offline_planner_offsets[i] == kOnlinePlannedBuffer) {
756+
size_t buffer_size;
757+
TF_LITE_ENSURE_STATUS(
758+
TfLiteEvalTensorByteLength(&eval_tensors[i], &buffer_size));
765759

766-
eval_tensors[i].data.data =
767-
persistent_buffer_allocator_->AllocatePersistentBuffer(
768-
buffer_size, MicroArenaBufferAlignment());
760+
eval_tensors[i].data.data =
761+
persistent_buffer_allocator_->AllocatePersistentBuffer(
762+
buffer_size, MicroArenaBufferAlignment());
769763

770-
if (eval_tensors[i].data.data == nullptr) {
771-
MicroPrintf("Failed to allocate variable tensor of size %d",
772-
buffer_size);
773-
return kTfLiteError;
764+
if (eval_tensors[i].data.data == nullptr) {
765+
MicroPrintf("Failed to allocate variable tensor of size %d",
766+
buffer_size);
767+
return kTfLiteError;
768+
}
774769
}
775770
}
776771
}
@@ -819,6 +814,17 @@ TfLiteStatus MicroAllocator::CommitStaticMemoryPlan(
819814
const int32_t* offline_planner_offsets = nullptr;
820815
TF_LITE_ENSURE_STATUS(
821816
builder.GetOfflinePlannedOffsets(&offline_planner_offsets));
817+
818+
// We allocate buffers for variable tensors here since the offline planner
819+
// offsets are conviently available here.
820+
for (size_t subgraph_idx = 0; subgraph_idx < model->subgraphs()->size();
821+
subgraph_idx++) {
822+
const SubGraph* subgraph = model->subgraphs()->Get(subgraph_idx);
823+
TFLITE_DCHECK(subgraph != nullptr);
824+
TF_LITE_ENSURE_STATUS(AllocateVariables(
825+
subgraph, allocations[subgraph_idx].tensors, offline_planner_offsets));
826+
}
827+
822828
TF_LITE_ENSURE_STATUS(
823829
builder.InitializeAllocationInfo(offline_planner_offsets, allocations));
824830

src/tensorflow/lite/micro/micro_allocator.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
1+
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -247,9 +247,13 @@ class MicroAllocator {
247247
// for all tensor buffers.
248248
virtual TfLiteStatus AllocateTfLiteEvalTensors(
249249
const Model* model, SubgraphAllocations* subgraph_allocations);
250+
250251
// Allocates persistent tensor buffers for variable tensors in the subgraph.
251-
virtual TfLiteStatus AllocateVariables(const SubGraph* subgraph,
252-
TfLiteEvalTensor* eval_tensors);
252+
// Online and offline variable tensors are handled differently hence the
253+
// offline_planner_offsets parameter is needed.
254+
virtual TfLiteStatus AllocateVariables(
255+
const SubGraph* subgraph, TfLiteEvalTensor* eval_tensors,
256+
const int32_t* offline_planner_offsets);
253257

254258
// Allocate and return a persistent TfLiteTensor.
255259
// TODO(b/162311891): Drop this method when the interpreter has an API for

src/tensorflow/lite/micro/recording_micro_allocator.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
1+
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -192,11 +192,12 @@ TfLiteStatus RecordingMicroAllocator::AllocateTfLiteEvalTensors(
192192
}
193193

194194
TfLiteStatus RecordingMicroAllocator::AllocateVariables(
195-
const SubGraph* subgraph, TfLiteEvalTensor* eval_tensors) {
195+
const SubGraph* subgraph, TfLiteEvalTensor* eval_tensors,
196+
const int32_t* offline_planner_offsets) {
196197
RecordedAllocation allocations = SnapshotAllocationUsage();
197198

198-
TfLiteStatus status =
199-
MicroAllocator::AllocateVariables(subgraph, eval_tensors);
199+
TfLiteStatus status = MicroAllocator::AllocateVariables(
200+
subgraph, eval_tensors, offline_planner_offsets);
200201

201202
RecordAllocationUsage(allocations,
202203
recorded_tflite_tensor_variable_buffer_data_);

src/tensorflow/lite/micro/recording_micro_allocator.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
1+
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -77,8 +77,9 @@ class RecordingMicroAllocator : public MicroAllocator {
7777
const Model* model, SubgraphAllocations* subgraph_allocations) override;
7878
TfLiteStatus AllocateTfLiteEvalTensors(
7979
const Model* model, SubgraphAllocations* subgraph_allocations) override;
80-
TfLiteStatus AllocateVariables(const SubGraph* subgraph,
81-
TfLiteEvalTensor* eval_tensors) override;
80+
TfLiteStatus AllocateVariables(
81+
const SubGraph* subgraph, TfLiteEvalTensor* eval_tensors,
82+
const int32_t* offline_planner_offsets) override;
8283
// TODO(b/162311891): Once all kernels have been updated to the new API drop
8384
// this method. It is only used to record TfLiteTensor persistent allocations.
8485
TfLiteTensor* AllocatePersistentTfLiteTensorInternal() override;

0 commit comments

Comments
 (0)