micro_allocator.cc 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #include "tensorflow/lite/micro/micro_allocator.h"
  13. #include <cstddef>
  14. #include <cstdint>
  15. #include "flatbuffers/flatbuffers.h" // from @flatbuffers
  16. #include "tensorflow/lite/c/common.h"
  17. #include "tensorflow/lite/core/api/error_reporter.h"
  18. #include "tensorflow/lite/core/api/flatbuffer_conversions.h"
  19. #include "tensorflow/lite/core/api/op_resolver.h"
  20. #include "tensorflow/lite/core/api/tensor_utils.h"
  21. #include "tensorflow/lite/kernels/internal/compatibility.h"
  22. #include "tensorflow/lite/micro/compatibility.h"
  23. #include "tensorflow/lite/micro/memory_helpers.h"
  24. #include "tensorflow/lite/micro/memory_planner/greedy_memory_planner.h"
  25. #include "tensorflow/lite/micro/memory_planner/memory_planner.h"
  26. #include "tensorflow/lite/micro/micro_op_resolver.h"
  27. #include "tensorflow/lite/micro/simple_memory_allocator.h"
  28. #include "tensorflow/lite/schema/schema_generated.h"
  29. namespace tflite {
  30. namespace {
  31. // Used to hold information used during allocation calculations.
  32. struct AllocationInfo {
  33. size_t bytes;
  34. void** output_ptr;
  35. int first_created;
  36. int last_used;
  37. int32_t offline_offset;
  38. bool needs_allocating;
  39. };
  40. // We align tensor buffers to 16-byte boundaries, since this is a common
  41. // requirement for SIMD extensions.
  42. constexpr int kBufferAlignment = 16;
  43. constexpr char kOfflineMemAllocMetadata[] = "OfflineMemoryAllocation";
  44. const TfLiteIntArray kZeroLengthIntArray = {0, {}};
  45. class MicroBuiltinDataAllocator : public BuiltinDataAllocator {
  46. public:
  47. explicit MicroBuiltinDataAllocator(SimpleMemoryAllocator* memory_allocator)
  48. : memory_allocator_(memory_allocator) {}
  49. void* Allocate(size_t size, size_t alignment_hint) override {
  50. return memory_allocator_->AllocateFromTail(size, alignment_hint);
  51. }
  52. void Deallocate(void* data) override {
  53. // Do not deallocate, builtin data needs to be available for the life time
  54. // of the model.
  55. }
  56. private:
  57. SimpleMemoryAllocator* memory_allocator_;
  58. TF_LITE_REMOVE_VIRTUAL_DELETE
  59. };
  60. #if !defined(__clang__)
  61. // Helper function to check flatbuffer metadata correctness. This function is
  62. // not called by default. Hence it's not linked in to the final binary code.
  63. TfLiteStatus CheckOfflinePlannedOffsets(const Model* model,
  64. ErrorReporter* error_reporter) {
  65. // Suppress compile warning for unused function
  66. (void)CheckOfflinePlannedOffsets;
  67. if (model->metadata()) {
  68. for (size_t i = 0; i < model->metadata()->size(); ++i) {
  69. auto metadata = model->metadata()->Get(i);
  70. if (strncmp(metadata->name()->c_str(), kOfflineMemAllocMetadata,
  71. strlen(kOfflineMemAllocMetadata)) == 0) {
  72. auto* subgraphs = model->subgraphs();
  73. const SubGraph* subgraph = (*subgraphs)[0];
  74. const flatbuffers::Vector<flatbuffers::Offset<Tensor>>* tensors =
  75. subgraph->tensors();
  76. const flatbuffers::Vector<flatbuffers::Offset<Buffer>>* buffers =
  77. model->buffers();
  78. int nbr_tflite_tensors = tensors->size();
  79. auto* buffer = (*buffers)[metadata->buffer()];
  80. auto* array = buffer->data();
  81. const uint32_t* metadata_buffer = (uint32_t*)array->data();
  82. int version = metadata_buffer[0];
  83. int subgraph_idx = metadata_buffer[1];
  84. const int nbr_offline_offsets = metadata_buffer[2];
  85. #ifndef TF_LITE_STRIP_ERROR_STRINGS
  86. int* offline_planner_offsets = (int*)&metadata_buffer[3];
  87. #endif
  88. TF_LITE_REPORT_ERROR(error_reporter, "==== Model metadata info: =====");
  89. TF_LITE_REPORT_ERROR(error_reporter,
  90. "Offline planner metadata found, version %d, "
  91. "subgraph %d, nbr offline offsets %d",
  92. version, subgraph_idx, nbr_offline_offsets);
  93. for (int j = 0; j < nbr_offline_offsets; ++j) {
  94. TF_LITE_REPORT_ERROR(
  95. error_reporter,
  96. "Offline planner tensor index %d, offline offset: %d", j,
  97. offline_planner_offsets[j]);
  98. }
  99. if (version != 1) {
  100. TF_LITE_REPORT_ERROR(error_reporter, "Version not supported! (%d)\n",
  101. version);
  102. return kTfLiteError;
  103. }
  104. if (subgraph_idx != 0) {
  105. TF_LITE_REPORT_ERROR(error_reporter,
  106. "Only 1 subgraph supported! Subgraph idx (%d)\n",
  107. subgraph_idx);
  108. return kTfLiteError;
  109. }
  110. if (nbr_tflite_tensors != nbr_offline_offsets) {
  111. TF_LITE_REPORT_ERROR(error_reporter,
  112. "Nbr of offline buffer offsets (%d) in metadata "
  113. "not equal nbr tensors (%d)\n",
  114. nbr_offline_offsets, nbr_tflite_tensors);
  115. return kTfLiteError;
  116. }
  117. }
  118. }
  119. }
  120. return kTfLiteOk;
  121. }
  122. #endif
  123. // A helper class to construct AllocationInfo array. This array contains the
  124. // lifetime of tensors / scratch_buffer and will be used to calculate the memory
  125. // plan. Methods need to be called in order from `Init`, `Add*`, to `Finish`.
  126. class AllocationInfoBuilder {
  127. public:
  128. AllocationInfoBuilder(ErrorReporter* reporter,
  129. SimpleMemoryAllocator* allocator)
  130. : reporter_(reporter), allocator_(allocator) {}
  131. // Initializes the builder by allocating AllocationInfo array from the
  132. // simple memory allocator.
  133. TfLiteStatus Init(size_t tensor_count, size_t scratch_buffer_count) {
  134. tensor_count_ = tensor_count;
  135. buffer_count_ = scratch_buffer_count;
  136. return Allocate();
  137. }
  138. // Check if model contains offline planned buffer offsets.
  139. // - If there's no metadata available, offline_planner_offsets is not set
  140. // - If there's metadata available, offline_planner_offsets will point to the
  141. // first offset in the metadata buffer list.
  142. TfLiteStatus GetOfflinePlannedOffsets(
  143. const Model* model, const int32_t** offline_planner_offsets);
  144. // Add allocaiton information for the tensors.
  145. TfLiteStatus AddTensors(const SubGraph* subgraph,
  146. const int32_t* offline_offsets,
  147. TfLiteEvalTensor* eval_tensors);
  148. // Add allocation information for the scratch buffers.
  149. TfLiteStatus AddScratchBuffers(internal::ScratchBufferHandle* buffer_handles);
  150. // Returns a pointer to the built AllocationInfo array.
  151. const AllocationInfo* Finish() const { return info_; }
  152. size_t Size() const { return tensor_count_ + buffer_count_; }
  153. private:
  154. // Allocate the output AllocationInfo array from the allocator_;
  155. TfLiteStatus Allocate();
  156. ErrorReporter* reporter_ = nullptr;
  157. SimpleMemoryAllocator* allocator_ = nullptr;
  158. size_t tensor_count_ = 0;
  159. size_t buffer_count_ = 0;
  160. AllocationInfo* info_ = nullptr;
  161. };
  162. TfLiteStatus AllocationInfoBuilder::Allocate() {
  163. size_t bytes = sizeof(AllocationInfo) * Size();
  164. info_ = reinterpret_cast<AllocationInfo*>(
  165. allocator_->AllocateFromTail(bytes, alignof(AllocationInfo)));
  166. if (info_ == nullptr) {
  167. TF_LITE_REPORT_ERROR(
  168. reporter_,
  169. "Failed to allocate memory for allocation_info, %d bytes required",
  170. bytes);
  171. return kTfLiteError;
  172. }
  173. return kTfLiteOk;
  174. }
  175. TfLiteStatus AllocationInfoBuilder::AddTensors(const SubGraph* subgraph,
  176. const int32_t* offline_offsets,
  177. TfLiteEvalTensor* eval_tensors) {
  178. TFLITE_DCHECK(eval_tensors != nullptr);
  179. // Set up allocation info for all tensors.
  180. for (size_t i = 0; i < tensor_count_; ++i) {
  181. AllocationInfo* current = &info_[i];
  182. current->output_ptr = &(eval_tensors[i].data.data);
  183. TF_LITE_ENSURE_STATUS(
  184. TfLiteEvalTensorByteLength(&eval_tensors[i], &current->bytes));
  185. current->first_created = -1;
  186. current->last_used = -1;
  187. current->needs_allocating = (eval_tensors[i].data.data == nullptr) &&
  188. (!subgraph->tensors()->Get(i)->is_variable());
  189. if (offline_offsets) {
  190. current->offline_offset = offline_offsets[i];
  191. } else {
  192. current->offline_offset = kOnlinePlannedBuffer;
  193. }
  194. }
  195. for (size_t i = 0; i < subgraph->inputs()->size(); ++i) {
  196. const int tensor_index = subgraph->inputs()->Get(i);
  197. AllocationInfo* current = &info_[tensor_index];
  198. current->first_created = 0;
  199. }
  200. // Mark all outputs as persistent to the end of the invocation.
  201. for (size_t i = 0; i < subgraph->outputs()->size(); ++i) {
  202. const int tensor_index = subgraph->outputs()->Get(i);
  203. AllocationInfo* current = &info_[tensor_index];
  204. current->last_used = subgraph->operators()->size() - 1;
  205. }
  206. // Figure out when the first and last use of each tensor is.
  207. for (int i = (subgraph->operators()->size() - 1); i >= 0; --i) {
  208. const auto* op = subgraph->operators()->Get(i);
  209. for (size_t n = 0; n < op->inputs()->size(); ++n) {
  210. const int tensor_index = op->inputs()->Get(n);
  211. AllocationInfo* current = &info_[tensor_index];
  212. if (((current->last_used == -1) || (current->last_used < i))) {
  213. current->last_used = i;
  214. }
  215. }
  216. for (size_t n = 0; n < op->outputs()->size(); ++n) {
  217. const int tensor_index = op->outputs()->Get(n);
  218. AllocationInfo* current = &info_[tensor_index];
  219. if ((current->first_created == -1) || (current->first_created > i)) {
  220. current->first_created = i;
  221. }
  222. }
  223. }
  224. // Work out which tensors need to be allocated.
  225. for (size_t i = 0; i < tensor_count_; ++i) {
  226. AllocationInfo* current = &info_[i];
  227. const bool is_read_only =
  228. (current->first_created == -1) && (current->last_used != -1);
  229. if (is_read_only) {
  230. current->needs_allocating = false;
  231. }
  232. const bool has_partial_lifetime =
  233. !is_read_only &&
  234. ((current->first_created == -1) || (current->last_used == -1));
  235. if (has_partial_lifetime && current->needs_allocating) {
  236. TF_LITE_REPORT_ERROR(
  237. reporter_,
  238. "Logic error in memory planner, tensor %d has an invalid lifetime: "
  239. "first_created: %d, last_used: %d",
  240. i, current->first_created, current->last_used);
  241. return kTfLiteError;
  242. }
  243. }
  244. return kTfLiteOk;
  245. }
  246. // The tensor offsets will be encoded in the metadata:[Metadata] field of the
  247. // Model. The following encoding applies:
  248. //
  249. // | Metadata component | Value |
  250. // | name:string | “OfflineMemoryAllocation” |
  251. // | buffer:unit | Index of buffer containing memory allocation data |
  252. //
  253. // The buffer contents for the memory allocation is a list of 32-bit integers.
  254. // The number of tensors, n, must be equal to the number of tensors defined in
  255. // the model. The following encoding applies:
  256. //
  257. // | Offset | Value |
  258. // | 0 | Offline allocation format version – set to 0 |
  259. // | 1 | Subgraph index to which this allocation applies |
  260. // | 2 | Number offsets following: n |
  261. // | 3 | Arena byte offset of tensor #0 or -1 to allocate at runtime |
  262. // | 4 | Arena byte offset of tensor #1 or -1 to allocate at runtime |
  263. // | 3+(n-1) | Arena byte offset of tensor #(n-1) or -1 to allocate at runtime |
  264. TfLiteStatus AllocationInfoBuilder::GetOfflinePlannedOffsets(
  265. const Model* model, const int32_t** offline_planner_offsets) {
  266. if (model->metadata()) {
  267. for (size_t i = 0; i < model->metadata()->size(); ++i) {
  268. auto metadata = model->metadata()->Get(i);
  269. if (strncmp(metadata->name()->c_str(), kOfflineMemAllocMetadata,
  270. strlen(kOfflineMemAllocMetadata)) == 0) {
  271. const flatbuffers::Vector<flatbuffers::Offset<Buffer>>* buffers =
  272. model->buffers();
  273. auto* buffer = (*buffers)[metadata->buffer()];
  274. auto* array = buffer->data();
  275. const uint32_t* metadata_buffer =
  276. reinterpret_cast<const uint32_t*>(array->data());
  277. const size_t nbr_tensors = static_cast<size_t>(metadata_buffer[2]);
  278. *offline_planner_offsets =
  279. reinterpret_cast<const int32_t*>(&metadata_buffer[3]);
  280. if (tensor_count_ != nbr_tensors) {
  281. TF_LITE_REPORT_ERROR(reporter_,
  282. "Nbr of offline buffer offsets (%d) in metadata "
  283. "not equal nbr tensors (%d)\n",
  284. nbr_tensors, tensor_count_);
  285. return kTfLiteError;
  286. }
  287. }
  288. }
  289. }
  290. return kTfLiteOk;
  291. }
  292. TfLiteStatus AllocationInfoBuilder::AddScratchBuffers(
  293. internal::ScratchBufferHandle* buffer_handles) {
  294. // Set up allocation info for buffers.
  295. for (size_t i = tensor_count_; i < tensor_count_ + buffer_count_; ++i) {
  296. AllocationInfo* current = &info_[i];
  297. internal::ScratchBufferHandle* handle =
  298. &(buffer_handles[i - tensor_count_]);
  299. current->output_ptr = reinterpret_cast<void**>(&handle->data);
  300. current->bytes = handle->bytes;
  301. current->first_created = handle->node_idx;
  302. current->last_used = handle->node_idx;
  303. current->needs_allocating = true;
  304. current->offline_offset = kOnlinePlannedBuffer;
  305. }
  306. return kTfLiteOk;
  307. }
  308. TfLiteStatus CreatePlan(ErrorReporter* error_reporter,
  309. GreedyMemoryPlanner* planner,
  310. const AllocationInfo* allocation_info,
  311. size_t allocation_info_size) {
  312. // Add the tensors to our allocation plan.
  313. for (size_t i = 0; i < allocation_info_size; ++i) {
  314. const AllocationInfo* current = &allocation_info[i];
  315. if (current->needs_allocating) {
  316. size_t aligned_bytes_required =
  317. AlignSizeUp(current->bytes, kBufferAlignment);
  318. if (current->offline_offset == kOnlinePlannedBuffer) {
  319. TF_LITE_ENSURE_STATUS(
  320. planner->AddBuffer(error_reporter, aligned_bytes_required,
  321. current->first_created, current->last_used));
  322. } else {
  323. TF_LITE_ENSURE_STATUS(planner->AddBuffer(
  324. error_reporter, aligned_bytes_required, current->first_created,
  325. current->last_used, current->offline_offset));
  326. }
  327. }
  328. }
  329. return kTfLiteOk;
  330. }
  331. TfLiteStatus CommitPlan(ErrorReporter* error_reporter, MemoryPlanner* planner,
  332. uint8_t* starting_point,
  333. const AllocationInfo* allocation_info,
  334. size_t allocation_info_size) {
  335. // Figure out the actual memory addresses for each buffer, based on the plan.
  336. int planner_index = 0;
  337. for (size_t i = 0; i < allocation_info_size; ++i) {
  338. const AllocationInfo* current = &allocation_info[i];
  339. if (current->needs_allocating) {
  340. int offset = -1;
  341. TF_LITE_ENSURE_STATUS(
  342. planner->GetOffsetForBuffer(error_reporter, planner_index, &offset));
  343. *current->output_ptr = reinterpret_cast<void*>(starting_point + offset);
  344. ++planner_index;
  345. }
  346. }
  347. return kTfLiteOk;
  348. }
  349. } // namespace
  350. namespace internal {
  351. // Handles architecture safe mapping of flatbuffer vectors to a TfLite*Array
  352. // struct. Matching types are required (e.g. float and TfLiteFloatArray).
  353. // Big-endian systems will always allocate dimension array data in the tail
  354. // (persistent) section.
  355. template <typename kFlatBufferVectorType, typename kTfLiteArrayType>
  356. TfLiteStatus FlatBufferVectorToTfLiteTypeArray(
  357. SimpleMemoryAllocator* allocator, ErrorReporter* error_reporter,
  358. const flatbuffers::Vector<kFlatBufferVectorType>* flatbuffer_array,
  359. kTfLiteArrayType** result) {
  360. TFLITE_DCHECK(error_reporter != nullptr);
  361. TFLITE_DCHECK(flatbuffer_array != nullptr);
  362. // TODO(b/159668691): Consider adding type assertion or breaking this function
  363. // into multiple functions for each type. std::is_same is c++11 and has a
  364. // special updated constructor in c++17 that requires a string argument.
  365. if (FLATBUFFERS_LITTLEENDIAN) {
  366. // On little-endian machines, TfLite*Array happens to have the same memory
  367. // layout as flatbuffers:Vector<kFlatBufferVectorType>, so we can
  368. // reinterpret_cast the flatbuffer vector and avoid a copy and malloc.
  369. *result = const_cast<kTfLiteArrayType*>(
  370. reinterpret_cast<const kTfLiteArrayType*>(flatbuffer_array));
  371. } else {
  372. // Big-endian architecture can not use the same memory layout as
  373. // flatbuffers::Vector<kFlatBufferVectorType>. Allocate from the tail and
  374. // copy values from the flatbuffer into the newly allocated chunk.
  375. kTfLiteArrayType* array =
  376. reinterpret_cast<kTfLiteArrayType*>(allocator->AllocateFromTail(
  377. TfLiteIntArrayGetSizeInBytes(flatbuffer_array->Length()),
  378. alignof(kTfLiteArrayType)));
  379. if (array == nullptr) {
  380. TF_LITE_REPORT_ERROR(
  381. error_reporter,
  382. "Failed to allocate %d bytes of memory to copy an array.",
  383. TfLiteIntArrayGetSizeInBytes(flatbuffer_array->Length()));
  384. return kTfLiteError;
  385. }
  386. array->size = flatbuffer_array->Length();
  387. for (int i = 0; i < array->size; ++i) {
  388. array->data[i] = flatbuffer_array->Get(i);
  389. }
  390. *result = array;
  391. }
  392. return kTfLiteOk;
  393. }
  394. // Returns a pointer to any buffer associated with the flatbuffer tensor. Can
  395. // return nullptr if no buffer is found.
  396. void* GetFlatbufferTensorBuffer(
  397. const tflite::Tensor& flatbuffer_tensor,
  398. const flatbuffers::Vector<flatbuffers::Offset<Buffer>>* buffers) {
  399. // We need to figure out where the actual contents of this tensor are stored
  400. // in memory. We'll check to see if there's a serialized buffer (pretty much
  401. // the same as a constant op in TensorFlow) associated with this tensor first,
  402. // and if there is update the runtime structure to point to its location in
  403. // memory.
  404. // First see if there's any buffer information in the serialized tensor.
  405. // TODO(b/160894903): Add better unit tests that validate flatbuffer values.
  406. void* out_buffer = nullptr;
  407. if (auto* buffer = (*buffers)[flatbuffer_tensor.buffer()]) {
  408. // If we've found a buffer, does it have any data?
  409. if (auto* array = buffer->data()) {
  410. // If it has any data, is the data size larger than zero?
  411. if (array->size()) {
  412. // We've found a buffer with valid data, so update the runtime tensor
  413. // data structure to point to it.
  414. out_buffer = const_cast<void*>(static_cast<const void*>(array->data()));
  415. }
  416. }
  417. // TODO(petewarden): It's not clear in what circumstances we could have a
  418. // buffer in the serialized tensor, but it doesn't have any data in it. Is
  419. // that a validly-generated file, and if so what does it mean, or is it an
  420. // error condition? It would be good to tighten up the specification to make
  421. // it less ambiguous.
  422. }
  423. return out_buffer;
  424. }
  425. TfLiteStatus InitializeTfLiteTensorFromFlatbuffer(
  426. SimpleMemoryAllocator* allocator, bool allocate_temp,
  427. const tflite::Tensor& flatbuffer_tensor,
  428. const flatbuffers::Vector<flatbuffers::Offset<Buffer>>* buffers,
  429. ErrorReporter* error_reporter, TfLiteTensor* result) {
  430. TFLITE_DCHECK(result != nullptr);
  431. *result = {};
  432. // Make sure the serialized type is one we know how to deal with, and convert
  433. // it from a flatbuffer enum into a constant used by the kernel C API.
  434. TF_LITE_ENSURE_STATUS(ConvertTensorType(flatbuffer_tensor.type(),
  435. &result->type, error_reporter));
  436. // Make sure we remember if the serialized tensor is designated as a variable.
  437. result->is_variable = flatbuffer_tensor.is_variable();
  438. result->data.data = GetFlatbufferTensorBuffer(flatbuffer_tensor, buffers);
  439. // TODO(petewarden): Some of these paths aren't getting enough testing
  440. // coverage, so we should figure out some tests that exercise them.
  441. if (result->data.data == nullptr) {
  442. // The tensor contents haven't been set from a serialized buffer, so
  443. // make a note that they will be allocated from memory. The actual
  444. // allocation won't happen until later.
  445. result->allocation_type = kTfLiteArenaRw;
  446. } else {
  447. // We set the data from a serialized buffer, so record tha.
  448. result->allocation_type = kTfLiteMmapRo;
  449. }
  450. // Figure out what the size in bytes of the buffer is and store it.
  451. size_t type_size;
  452. TF_LITE_ENSURE_STATUS(BytesRequiredForTensor(
  453. flatbuffer_tensor, &result->bytes, &type_size, error_reporter));
  454. if (flatbuffer_tensor.shape() == nullptr) {
  455. // flatbuffer_tensor.shape() can return a nullptr in the case of a scalar
  456. // tensor.
  457. result->dims = const_cast<TfLiteIntArray*>(&kZeroLengthIntArray);
  458. } else {
  459. // TFLM doesn't allow reshaping the tensor which requires dynamic memory
  460. // allocation so it is safe to drop the const qualifier. In the future, if
  461. // we really want to update the tensor shape, we can always pass in a new
  462. // TfLiteIntArray - especially we have to do so if the dimension is
  463. TF_LITE_ENSURE_STATUS(FlatBufferVectorToTfLiteTypeArray(
  464. allocator, error_reporter, flatbuffer_tensor.shape(), &(result->dims)));
  465. }
  466. // Copy the quantization information from the serialized data.
  467. const auto* src_quantization = flatbuffer_tensor.quantization();
  468. if (src_quantization && src_quantization->scale() &&
  469. (src_quantization->scale()->size() > 0) &&
  470. src_quantization->zero_point() &&
  471. (src_quantization->zero_point()->size() > 0)) {
  472. // Always populate the TfLiteTensor.params field, even if there are
  473. // per-channel quantization parameters.
  474. result->params.scale = src_quantization->scale()->Get(0);
  475. // Note that the zero_point field in the FlatBuffers schema is a 64-bit
  476. // integer, but the zero_point field in the TfLiteQuantizationParams struct
  477. // is a 32-bit integer.
  478. result->params.zero_point =
  479. static_cast<int32_t>(src_quantization->zero_point()->Get(0));
  480. // Populate per-channel quantization params.
  481. int channels = src_quantization->scale()->size();
  482. TfLiteAffineQuantization* quantization =
  483. allocate_temp
  484. ? reinterpret_cast<TfLiteAffineQuantization*>(
  485. allocator->AllocateTemp(sizeof(TfLiteAffineQuantization),
  486. alignof(TfLiteAffineQuantization)))
  487. : reinterpret_cast<TfLiteAffineQuantization*>(
  488. allocator->AllocateFromTail(
  489. sizeof(TfLiteAffineQuantization),
  490. alignof(TfLiteAffineQuantization)));
  491. if (quantization == nullptr) {
  492. TF_LITE_REPORT_ERROR(error_reporter,
  493. "Unable to allocate TfLiteAffineQuantization.\n");
  494. return kTfLiteError;
  495. }
  496. // TODO(b/153688719): Reduce tail allocation by using a global zero-point
  497. // buffer. This value can not be reused from the flatbuffer since the
  498. // zero_point is stored as a int64_t.
  499. quantization->zero_point =
  500. allocate_temp
  501. ? reinterpret_cast<TfLiteIntArray*>(allocator->AllocateTemp(
  502. TfLiteIntArrayGetSizeInBytes(channels),
  503. alignof(TfLiteIntArray)))
  504. : reinterpret_cast<TfLiteIntArray*>(allocator->AllocateFromTail(
  505. TfLiteIntArrayGetSizeInBytes(channels),
  506. alignof(TfLiteIntArray)));
  507. if (quantization->zero_point == nullptr) {
  508. TF_LITE_REPORT_ERROR(error_reporter,
  509. "Unable to allocate quantization->zero_point.\n");
  510. return kTfLiteError;
  511. }
  512. TF_LITE_ENSURE_STATUS(FlatBufferVectorToTfLiteTypeArray(
  513. allocator, error_reporter, src_quantization->scale(),
  514. &quantization->scale));
  515. quantization->zero_point->size = channels;
  516. int* zero_point_data = quantization->zero_point->data;
  517. for (int i = 0; i < channels; i++) {
  518. zero_point_data[i] = src_quantization->zero_point()->Get(i);
  519. }
  520. // TODO(rocky): Need to add a micro_allocator test case that fails when
  521. // this is not copied:
  522. quantization->quantized_dimension = src_quantization->quantized_dimension();
  523. result->quantization = {kTfLiteAffineQuantization, quantization};
  524. }
  525. return kTfLiteOk;
  526. }
  527. TfLiteStatus InitializeTfLiteEvalTensorFromFlatbuffer(
  528. SimpleMemoryAllocator* allocator, const tflite::Tensor& flatbuffer_tensor,
  529. const flatbuffers::Vector<flatbuffers::Offset<Buffer>>* buffers,
  530. ErrorReporter* error_reporter, TfLiteEvalTensor* result) {
  531. *result = {};
  532. // Make sure the serialized type is one we know how to deal with, and convert
  533. // it from a flatbuffer enum into a constant used by the kernel C API.
  534. TF_LITE_ENSURE_STATUS(ConvertTensorType(flatbuffer_tensor.type(),
  535. &result->type, error_reporter));
  536. result->data.data = GetFlatbufferTensorBuffer(flatbuffer_tensor, buffers);
  537. if (flatbuffer_tensor.shape() == nullptr) {
  538. // flatbuffer_tensor.shape() can return a nullptr in the case of a scalar
  539. // tensor.
  540. result->dims = const_cast<TfLiteIntArray*>(&kZeroLengthIntArray);
  541. } else {
  542. TF_LITE_ENSURE_STATUS(FlatBufferVectorToTfLiteTypeArray(
  543. allocator, error_reporter, flatbuffer_tensor.shape(), &(result->dims)));
  544. }
  545. return kTfLiteOk;
  546. }
  547. } // namespace internal
  548. MicroAllocator::MicroAllocator(SimpleMemoryAllocator* memory_allocator,
  549. ErrorReporter* error_reporter)
  550. : memory_allocator_(memory_allocator),
  551. error_reporter_(error_reporter),
  552. model_is_allocating_(false) {}
  553. MicroAllocator::~MicroAllocator() {}
  554. MicroAllocator* MicroAllocator::Create(uint8_t* tensor_arena, size_t arena_size,
  555. ErrorReporter* error_reporter) {
  556. uint8_t* aligned_arena = AlignPointerUp(tensor_arena, kBufferAlignment);
  557. if (aligned_arena != tensor_arena) {
  558. TF_LITE_REPORT_ERROR(
  559. error_reporter,
  560. "%d bytes lost due to alignment. To avoid this loss, please make sure "
  561. "the tensor_arena is 16 bytes aligned.",
  562. aligned_arena - tensor_arena);
  563. }
  564. size_t aligned_arena_size = tensor_arena + arena_size - aligned_arena;
  565. return Create(SimpleMemoryAllocator::Create(error_reporter, aligned_arena,
  566. aligned_arena_size),
  567. error_reporter);
  568. }
  569. MicroAllocator* MicroAllocator::Create(SimpleMemoryAllocator* memory_allocator,
  570. ErrorReporter* error_reporter) {
  571. TFLITE_DCHECK(memory_allocator != nullptr);
  572. TFLITE_DCHECK(error_reporter != nullptr);
  573. uint8_t* allocator_buffer = memory_allocator->AllocateFromTail(
  574. sizeof(MicroAllocator), alignof(MicroAllocator));
  575. MicroAllocator* allocator =
  576. new (allocator_buffer) MicroAllocator(memory_allocator, error_reporter);
  577. return allocator;
  578. }
  579. TfLiteStatus MicroAllocator::StartModelAllocation(
  580. const Model* model, const MicroOpResolver& op_resolver,
  581. NodeAndRegistration** node_and_registrations,
  582. TfLiteEvalTensor** eval_tensors) {
  583. TFLITE_DCHECK(model != nullptr);
  584. if (model_is_allocating_) {
  585. TF_LITE_REPORT_ERROR(error_reporter_,
  586. "MicroAllocator: Model allocation started before "
  587. "finishing previously allocated model");
  588. return kTfLiteError;
  589. }
  590. model_is_allocating_ = true;
  591. TF_LITE_ENSURE_STATUS(AllocateTfLiteEvalTensors(model, eval_tensors));
  592. TF_LITE_ENSURE_STATUS(
  593. AllocateNodeAndRegistrations(model, node_and_registrations));
  594. TF_LITE_ENSURE_STATUS(PrepareNodeAndRegistrationDataFromFlatbuffer(
  595. model, op_resolver, *node_and_registrations));
  596. return kTfLiteOk;
  597. }
  598. TfLiteStatus MicroAllocator::FinishModelAllocation(
  599. const Model* model, TfLiteEvalTensor* eval_tensors) {
  600. if (!model_is_allocating_) {
  601. TF_LITE_REPORT_ERROR(error_reporter_,
  602. "MicroAllocator: Model allocation finished before "
  603. "starting allocating model");
  604. return kTfLiteError;
  605. }
  606. const SubGraph* subgraph = GetSubGraphFromModel(model);
  607. TFLITE_DCHECK(subgraph != nullptr);
  608. TF_LITE_ENSURE_STATUS(CommitStaticMemoryPlan(model, subgraph, eval_tensors));
  609. TF_LITE_ENSURE_STATUS(AllocateVariables(subgraph, eval_tensors));
  610. model_is_allocating_ = false;
  611. return kTfLiteOk;
  612. }
  613. void* MicroAllocator::AllocatePersistentBuffer(size_t bytes) {
  614. return memory_allocator_->AllocateFromTail(bytes, kBufferAlignment);
  615. }
  616. TfLiteStatus MicroAllocator::RequestScratchBufferInArena(int node_id,
  617. size_t bytes,
  618. int* buffer_idx) {
  619. // A consistency check to make sure scratch_buffer_handles_ is contiguous i.e.
  620. // scratch_buffer_handles_ is pointing to the last allocation from memory
  621. // allocator.
  622. if (scratch_buffer_handles_ != nullptr &&
  623. reinterpret_cast<uint8_t*>(scratch_buffer_handles_) !=
  624. memory_allocator_->GetTail()) {
  625. TF_LITE_REPORT_ERROR(error_reporter_,
  626. "Internal error: AllocateFromTail can not be called "
  627. "between two RequestScratchBufferInArena calls.");
  628. return kTfLiteError;
  629. }
  630. internal::ScratchBufferHandle* handle =
  631. reinterpret_cast<internal::ScratchBufferHandle*>(
  632. memory_allocator_->AllocateFromTail(
  633. sizeof(internal::ScratchBufferHandle),
  634. alignof(internal::ScratchBufferHandle)));
  635. if (handle == nullptr) {
  636. TF_LITE_REPORT_ERROR(error_reporter_,
  637. "Failed to register scratch buffer handle for node %s",
  638. node_id);
  639. return kTfLiteError;
  640. }
  641. *handle = {};
  642. handle->bytes = bytes;
  643. handle->node_idx = node_id;
  644. *buffer_idx = scratch_buffer_count_;
  645. scratch_buffer_count_ += 1;
  646. // scratch_buffer_handles_ is in reverse order. The following code ensures
  647. // that scratch_buffers[0] is pointing to the newly allocated handle.
  648. scratch_buffer_handles_ = handle;
  649. return kTfLiteOk;
  650. }
  651. void* MicroAllocator::GetScratchBuffer(int buffer_idx) const {
  652. if (static_cast<size_t>(buffer_idx) >= scratch_buffer_count_) {
  653. TF_LITE_REPORT_ERROR(error_reporter_,
  654. "Buffer %d not found. %d buffers available.",
  655. buffer_idx, scratch_buffer_count_);
  656. return nullptr;
  657. }
  658. // scratch_buffer_handles_ is in reverse order.
  659. return scratch_buffer_handles_[scratch_buffer_count_ - buffer_idx - 1].data;
  660. }
  661. size_t MicroAllocator::used_bytes() const {
  662. return memory_allocator_->GetUsedBytes();
  663. }
  664. TfLiteStatus MicroAllocator::AllocateNodeAndRegistrations(
  665. const Model* model, NodeAndRegistration** node_and_registrations) {
  666. TFLITE_DCHECK(node_and_registrations);
  667. const SubGraph* subgraph = GetSubGraphFromModel(model);
  668. TFLITE_DCHECK(subgraph != nullptr);
  669. NodeAndRegistration* output = reinterpret_cast<NodeAndRegistration*>(
  670. memory_allocator_->AllocateFromTail(
  671. sizeof(NodeAndRegistration) * subgraph->operators()->size(),
  672. alignof(NodeAndRegistration)));
  673. if (output == nullptr) {
  674. TF_LITE_REPORT_ERROR(
  675. error_reporter_,
  676. "Failed to allocate memory for node_and_registrations.");
  677. return kTfLiteError;
  678. }
  679. *node_and_registrations = output;
  680. return kTfLiteOk;
  681. }
  682. TfLiteStatus MicroAllocator::PrepareNodeAndRegistrationDataFromFlatbuffer(
  683. const Model* model, const MicroOpResolver& op_resolver,
  684. NodeAndRegistration* node_and_registrations) {
  685. TFLITE_DCHECK(model != nullptr);
  686. TFLITE_DCHECK(node_and_registrations != nullptr);
  687. const SubGraph* subgraph = GetSubGraphFromModel(model);
  688. TFLITE_DCHECK(subgraph != nullptr);
  689. TfLiteStatus status = kTfLiteOk;
  690. auto* opcodes = model->operator_codes();
  691. MicroBuiltinDataAllocator builtin_data_allocator(memory_allocator_);
  692. for (size_t i = 0; i < subgraph->operators()->size(); ++i) {
  693. const auto* op = subgraph->operators()->Get(i);
  694. const size_t index = op->opcode_index();
  695. if (index >= opcodes->size()) {
  696. TF_LITE_REPORT_ERROR(error_reporter_,
  697. "Missing registration for opcode_index %d\n", index);
  698. return kTfLiteError;
  699. }
  700. auto* opcode = (*opcodes)[index];
  701. status =
  702. GetRegistrationFromOpCode(opcode, op_resolver, error_reporter_,
  703. &(node_and_registrations[i].registration));
  704. if (status != kTfLiteOk) {
  705. TF_LITE_REPORT_ERROR(error_reporter_,
  706. "Failed to get registration from op code %s\n ",
  707. EnumNameBuiltinOperator(opcode->builtin_code()));
  708. return status;
  709. }
  710. const auto* registration = node_and_registrations[i].registration;
  711. if (registration == nullptr) {
  712. TF_LITE_REPORT_ERROR(error_reporter_, "Skipping op for opcode_index %d\n",
  713. index);
  714. return kTfLiteError;
  715. }
  716. BuiltinOperator op_type =
  717. static_cast<BuiltinOperator>(registration->builtin_code);
  718. const char* custom_data = nullptr;
  719. size_t custom_data_size = 0;
  720. unsigned char* builtin_data = nullptr;
  721. if (op_type == BuiltinOperator_CUSTOM) {
  722. // Custom Ops may or may not have a non-null custom_options field.
  723. if (op->custom_options() != nullptr) {
  724. custom_data =
  725. reinterpret_cast<const char*>(op->custom_options()->data());
  726. custom_data_size = op->custom_options()->size();
  727. }
  728. } else {
  729. if (op->custom_options() != nullptr) {
  730. TF_LITE_REPORT_ERROR(
  731. error_reporter_,
  732. "Unsupported behavior: found builtin operator %s with custom "
  733. "options.\n",
  734. EnumNameBuiltinOperator(op_type));
  735. return kTfLiteError;
  736. }
  737. MicroOpResolver::BuiltinParseFunction parser =
  738. op_resolver.GetOpDataParser(op_type);
  739. if (parser == nullptr) {
  740. TF_LITE_REPORT_ERROR(error_reporter_, "Did not find a parser for %s",
  741. EnumNameBuiltinOperator(op_type));
  742. return kTfLiteError;
  743. }
  744. TF_LITE_ENSURE_STATUS(parser(op, error_reporter_, &builtin_data_allocator,
  745. (void**)(&builtin_data)));
  746. }
  747. TfLiteIntArray* inputs_array;
  748. TF_LITE_ENSURE_STATUS(internal::FlatBufferVectorToTfLiteTypeArray(
  749. memory_allocator_, error_reporter_, op->inputs(), &inputs_array));
  750. TfLiteIntArray* outputs_array;
  751. TF_LITE_ENSURE_STATUS(internal::FlatBufferVectorToTfLiteTypeArray(
  752. memory_allocator_, error_reporter_, op->outputs(), &outputs_array));
  753. TfLiteNode* node = &(node_and_registrations[i].node);
  754. *node = {};
  755. node->inputs = inputs_array;
  756. node->outputs = outputs_array;
  757. node->builtin_data = reinterpret_cast<void*>(builtin_data);
  758. node->custom_initial_data = custom_data;
  759. node->custom_initial_data_size = custom_data_size;
  760. }
  761. return kTfLiteOk;
  762. }
  763. TfLiteTensor* MicroAllocator::AllocatePersistentTfLiteTensor(
  764. const Model* model, TfLiteEvalTensor* eval_tensors, int tensor_index) {
  765. const SubGraph* subgraph = GetSubGraphFromModel(model);
  766. TFLITE_DCHECK(subgraph != nullptr);
  767. // This value is allocated from persistent arena space. It is guaranteed to be
  768. // around for the lifetime of the application.
  769. TfLiteTensor* tensor =
  770. AllocatePersistentTfLiteTensorInternal(model, eval_tensors, tensor_index);
  771. // Populate any fields from the flatbuffer, since this TfLiteTensor struct is
  772. // allocated in the persistent section of the arena, ensure that additional
  773. // allocations also take place in that section of the arena.
  774. if (PopulateTfLiteTensorFromFlatbuffer(model, subgraph, tensor, tensor_index,
  775. /*allocate_temp=*/false) !=
  776. kTfLiteOk) {
  777. TF_LITE_REPORT_ERROR(error_reporter_,
  778. "Failed to populate a persistent TfLiteTensor struct "
  779. "from flatbuffer data!");
  780. return nullptr;
  781. }
  782. if (eval_tensors != nullptr) {
  783. // Tensor buffers that are allocated at runtime (e.g. non-weight buffers)
  784. // and not located in the flatbuffer are stored on the pre-allocated list of
  785. // TfLiteEvalTensors structs. These structs are the source of truth, simply
  786. // point the corresponding buffer to the new TfLiteTensor data value.
  787. tensor->data.data = eval_tensors[tensor_index].data.data;
  788. }
  789. return tensor;
  790. }
  791. TfLiteTensor* MicroAllocator::AllocateTempTfLiteTensor(
  792. const Model* model, TfLiteEvalTensor* eval_tensors, int tensor_index) {
  793. const SubGraph* subgraph = GetSubGraphFromModel(model);
  794. TFLITE_DCHECK(subgraph != nullptr);
  795. // This value is allocated from temporary arena space. It is guaranteed to be
  796. // around for at least the scope of the calling function. Since this struct
  797. // allocation takes place in temp space, no need to own or cleanup.
  798. TfLiteTensor* tensor =
  799. reinterpret_cast<TfLiteTensor*>(memory_allocator_->AllocateTemp(
  800. sizeof(TfLiteTensor), alignof(TfLiteTensor)));
  801. // Populate any fields from the flatbuffer, since this TfLiteTensor struct is
  802. // allocated in the temp section of the arena, ensure that additional
  803. // allocations also take place in that section of the arena.
  804. if (PopulateTfLiteTensorFromFlatbuffer(model, subgraph, tensor, tensor_index,
  805. /*allocate_temp=*/true) != kTfLiteOk) {
  806. TF_LITE_REPORT_ERROR(
  807. error_reporter_,
  808. "Failed to populate a temp TfLiteTensor struct from flatbuffer data!");
  809. return nullptr;
  810. }
  811. if (eval_tensors != nullptr) {
  812. // Tensor buffers that are allocated at runtime (e.g. non-weight buffers)
  813. // and not located in the flatbuffer are stored on the pre-allocated list of
  814. // TfLiteEvalTensors structs. These structs are the source of truth, simply
  815. // point the corresponding buffer to the new TfLiteTensor data value.
  816. tensor->data.data = eval_tensors[tensor_index].data.data;
  817. }
  818. return tensor;
  819. }
  820. void MicroAllocator::ResetTempAllocations() {
  821. memory_allocator_->ResetTempAllocations();
  822. }
  823. TfLiteStatus MicroAllocator::AllocateTfLiteEvalTensors(
  824. const Model* model, TfLiteEvalTensor** eval_tensors) {
  825. TFLITE_DCHECK(eval_tensors != nullptr);
  826. const SubGraph* subgraph = GetSubGraphFromModel(model);
  827. TFLITE_DCHECK(subgraph != nullptr);
  828. size_t alloc_count = subgraph->tensors()->size();
  829. TfLiteEvalTensor* tensors =
  830. reinterpret_cast<TfLiteEvalTensor*>(memory_allocator_->AllocateFromTail(
  831. sizeof(TfLiteEvalTensor) * alloc_count, alignof(TfLiteEvalTensor)));
  832. if (tensors == nullptr) {
  833. TF_LITE_REPORT_ERROR(error_reporter_,
  834. "Failed to allocate memory for context->eval_tensors, "
  835. "%d bytes required",
  836. sizeof(TfLiteEvalTensor) * alloc_count);
  837. return kTfLiteError;
  838. }
  839. for (size_t i = 0; i < alloc_count; ++i) {
  840. TfLiteStatus status = internal::InitializeTfLiteEvalTensorFromFlatbuffer(
  841. memory_allocator_, *subgraph->tensors()->Get(i), model->buffers(),
  842. error_reporter_, &tensors[i]);
  843. if (status != kTfLiteOk) {
  844. TF_LITE_REPORT_ERROR(error_reporter_, "Failed to initialize tensor %d",
  845. i);
  846. return kTfLiteError;
  847. }
  848. }
  849. *eval_tensors = tensors;
  850. return kTfLiteOk;
  851. }
  852. TfLiteStatus MicroAllocator::AllocateVariables(const SubGraph* subgraph,
  853. TfLiteEvalTensor* eval_tensors) {
  854. for (size_t i = 0; i < subgraph->tensors()->size(); ++i) {
  855. auto* tensor = subgraph->tensors()->Get(i);
  856. if (tensor->is_variable()) {
  857. size_t buffer_size;
  858. TF_LITE_ENSURE_STATUS(
  859. TfLiteEvalTensorByteLength(&eval_tensors[i], &buffer_size));
  860. eval_tensors[i].data.data =
  861. memory_allocator_->AllocateFromTail(buffer_size, kBufferAlignment);
  862. if (eval_tensors[i].data.data == nullptr) {
  863. TF_LITE_REPORT_ERROR(error_reporter_,
  864. "Failed to allocate variable tensor of size %d",
  865. buffer_size);
  866. return kTfLiteError;
  867. }
  868. }
  869. }
  870. return kTfLiteOk;
  871. }
  872. TfLiteTensor* MicroAllocator::AllocatePersistentTfLiteTensorInternal(
  873. const Model* model, TfLiteEvalTensor* eval_tensors, int tensor_index) {
  874. return reinterpret_cast<TfLiteTensor*>(memory_allocator_->AllocateFromTail(
  875. sizeof(TfLiteTensor), alignof(TfLiteTensor)));
  876. }
  877. TfLiteStatus MicroAllocator::PopulateTfLiteTensorFromFlatbuffer(
  878. const Model* model, const SubGraph* subgraph, TfLiteTensor* tensor,
  879. int tensor_index, bool allocate_temp) {
  880. // TODO(b/160894903): This method serves as a stub to ensure quantized
  881. // allocations in the tail can be recorded. Once all kernels have been ported
  882. // to the new API this can be dropped.
  883. return internal::InitializeTfLiteTensorFromFlatbuffer(
  884. memory_allocator_, allocate_temp, *subgraph->tensors()->Get(tensor_index),
  885. model->buffers(), error_reporter_, tensor);
  886. }
  887. ErrorReporter* MicroAllocator::error_reporter() const {
  888. return error_reporter_;
  889. }
  890. const SubGraph* MicroAllocator::GetSubGraphFromModel(const Model* model) {
  891. auto* subgraphs = model->subgraphs();
  892. if (subgraphs->size() != 1) {
  893. TF_LITE_REPORT_ERROR(error_reporter_,
  894. "Only 1 subgraph is currently supported.\n");
  895. return nullptr;
  896. }
  897. return (*subgraphs)[0];
  898. }
  899. TfLiteStatus MicroAllocator::CommitStaticMemoryPlan(
  900. const Model* model, const SubGraph* subgraph,
  901. TfLiteEvalTensor* eval_tensors) {
  902. size_t head_usage = 0;
  903. // Create static memory plan
  904. // 1. Calculate AllocationInfo to know the lifetime of each tensor/buffer.
  905. // 2. Add them into the planner (such as the GreedyMemoryPlanner).
  906. // 3. Static memory planning using the planner.
  907. // 4. Set tensor/buffer pointers based on the offsets from the previous step.
  908. // Note that AllocationInfo is only needed for creating the plan. It will be
  909. // thrown away when the child allocator (tmp_allocator) goes out of scope.
  910. {
  911. // TODO(b/162595810): Use temp allocation buffer instead of a stack
  912. // instance:
  913. SimpleMemoryAllocator tmp_allocator(error_reporter_,
  914. memory_allocator_->GetBufferHead(),
  915. memory_allocator_->GetTail());
  916. AllocationInfoBuilder builder(error_reporter_, &tmp_allocator);
  917. TF_LITE_ENSURE_STATUS(
  918. builder.Init(subgraph->tensors()->size(), scratch_buffer_count_));
  919. const int32_t* offline_planner_offsets = nullptr;
  920. TF_LITE_ENSURE_STATUS(
  921. builder.GetOfflinePlannedOffsets(model, &offline_planner_offsets));
  922. TF_LITE_ENSURE_STATUS(
  923. builder.AddTensors(subgraph, offline_planner_offsets, eval_tensors));
  924. TF_LITE_ENSURE_STATUS(builder.AddScratchBuffers(scratch_buffer_handles_));
  925. const AllocationInfo* allocation_info = builder.Finish();
  926. // Remaining arena size that memory planner can use for calculating offsets.
  927. size_t remaining_arena_size =
  928. tmp_allocator.GetAvailableMemory(kBufferAlignment);
  929. uint8_t* planner_arena =
  930. tmp_allocator.AllocateTemp(remaining_arena_size, kBufferAlignment);
  931. TF_LITE_ENSURE(error_reporter_, planner_arena != nullptr);
  932. GreedyMemoryPlanner planner(planner_arena, remaining_arena_size);
  933. TF_LITE_ENSURE_STATUS(
  934. CreatePlan(error_reporter_, &planner, allocation_info, builder.Size()));
  935. size_t actual_available_arena_size =
  936. memory_allocator_->GetAvailableMemory(kBufferAlignment);
  937. // Make sure we have enough arena size.
  938. if (planner.GetMaximumMemorySize() > actual_available_arena_size) {
  939. TF_LITE_REPORT_ERROR(
  940. error_reporter_,
  941. "Arena size is too small for activation buffers. Needed %d but only "
  942. "%d was available.",
  943. planner.GetMaximumMemorySize(), actual_available_arena_size);
  944. return kTfLiteError;
  945. }
  946. // Commit the plan.
  947. TF_LITE_ENSURE_STATUS(CommitPlan(error_reporter_, &planner,
  948. memory_allocator_->GetBufferHead(),
  949. allocation_info, builder.Size()));
  950. head_usage = planner.GetMaximumMemorySize();
  951. }
  952. TF_LITE_ENSURE_STATUS(
  953. memory_allocator_->EnsureHeadSize(head_usage, kBufferAlignment));
  954. return kTfLiteOk;
  955. }
  956. } // namespace tflite