micro_interpreter.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /* Copyright 2018 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_interpreter.h"
  13. #include <cstdarg>
  14. #include <cstddef>
  15. #include <cstdint>
  16. #include "flatbuffers/flatbuffers.h" // from @flatbuffers
  17. #include "tensorflow/lite/c/common.h"
  18. #include "tensorflow/lite/core/api/error_reporter.h"
  19. #include "tensorflow/lite/core/api/tensor_utils.h"
  20. #include "tensorflow/lite/micro/memory_helpers.h"
  21. #include "tensorflow/lite/micro/micro_allocator.h"
  22. #include "tensorflow/lite/micro/micro_op_resolver.h"
  23. #include "tensorflow/lite/micro/micro_profiler.h"
  24. #include "tensorflow/lite/schema/schema_generated.h"
  25. namespace tflite {
  26. namespace {
  27. #ifndef TF_LITE_STRIP_ERROR_STRINGS
  28. const char* OpNameFromRegistration(const TfLiteRegistration* registration) {
  29. if (registration->builtin_code == BuiltinOperator_CUSTOM) {
  30. return registration->custom_name;
  31. } else {
  32. return EnumNameBuiltinOperator(BuiltinOperator(registration->builtin_code));
  33. }
  34. }
  35. #endif // !defined(TF_LITE_STRIP_ERROR_STRINGS)
  36. } // namespace
  37. namespace internal {
  38. ContextHelper::ContextHelper(ErrorReporter* error_reporter,
  39. MicroAllocator* allocator, const Model* model)
  40. : allocator_(allocator), error_reporter_(error_reporter), model_(model) {}
  41. void* ContextHelper::AllocatePersistentBuffer(TfLiteContext* ctx,
  42. size_t bytes) {
  43. return reinterpret_cast<ContextHelper*>(ctx->impl_)
  44. ->allocator_->AllocatePersistentBuffer(bytes);
  45. }
  46. TfLiteStatus ContextHelper::RequestScratchBufferInArena(TfLiteContext* ctx,
  47. size_t bytes,
  48. int* buffer_idx) {
  49. ContextHelper* helper = reinterpret_cast<ContextHelper*>(ctx->impl_);
  50. return helper->allocator_->RequestScratchBufferInArena(
  51. helper->current_node_idx_, bytes, buffer_idx);
  52. }
  53. void* ContextHelper::GetScratchBuffer(TfLiteContext* ctx, int buffer_idx) {
  54. return reinterpret_cast<ContextHelper*>(ctx->impl_)
  55. ->allocator_->GetScratchBuffer(buffer_idx);
  56. }
  57. void ContextHelper::ReportOpError(struct TfLiteContext* context,
  58. const char* format, ...) {
  59. #ifndef TF_LITE_STRIP_ERROR_STRINGS
  60. ContextHelper* helper = static_cast<ContextHelper*>(context->impl_);
  61. va_list args;
  62. va_start(args, format);
  63. TF_LITE_REPORT_ERROR(helper->error_reporter_, format, args);
  64. va_end(args);
  65. #endif
  66. }
  67. TfLiteTensor* ContextHelper::GetTensor(const struct TfLiteContext* context,
  68. int tensor_idx) {
  69. ContextHelper* helper = static_cast<ContextHelper*>(context->impl_);
  70. return helper->allocator_->AllocateTempTfLiteTensor(
  71. helper->model_, helper->eval_tensors_, tensor_idx);
  72. }
  73. TfLiteEvalTensor* ContextHelper::GetEvalTensor(
  74. const struct TfLiteContext* context, int tensor_idx) {
  75. ContextHelper* helper = reinterpret_cast<ContextHelper*>(context->impl_);
  76. return &helper->eval_tensors_[tensor_idx];
  77. }
  78. void ContextHelper::SetNodeIndex(int idx) { current_node_idx_ = idx; }
  79. void ContextHelper::SetTfLiteEvalTensors(TfLiteEvalTensor* eval_tensors) {
  80. eval_tensors_ = eval_tensors;
  81. }
  82. } // namespace internal
  83. MicroInterpreter::MicroInterpreter(const Model* model,
  84. const MicroOpResolver& op_resolver,
  85. uint8_t* tensor_arena,
  86. size_t tensor_arena_size,
  87. ErrorReporter* error_reporter,
  88. tflite::Profiler* profiler)
  89. : model_(model),
  90. op_resolver_(op_resolver),
  91. error_reporter_(error_reporter),
  92. allocator_(*MicroAllocator::Create(tensor_arena, tensor_arena_size,
  93. error_reporter)),
  94. tensors_allocated_(false),
  95. initialization_status_(kTfLiteError),
  96. eval_tensors_(nullptr),
  97. context_helper_(error_reporter_, &allocator_, model),
  98. input_tensor_(nullptr),
  99. output_tensor_(nullptr) {
  100. Init(profiler);
  101. }
  102. MicroInterpreter::MicroInterpreter(const Model* model,
  103. const MicroOpResolver& op_resolver,
  104. MicroAllocator* allocator,
  105. ErrorReporter* error_reporter,
  106. tflite::Profiler* profiler)
  107. : model_(model),
  108. op_resolver_(op_resolver),
  109. error_reporter_(error_reporter),
  110. allocator_(*allocator),
  111. tensors_allocated_(false),
  112. initialization_status_(kTfLiteError),
  113. eval_tensors_(nullptr),
  114. context_helper_(error_reporter_, &allocator_, model),
  115. input_tensor_(nullptr),
  116. output_tensor_(nullptr) {
  117. Init(profiler);
  118. }
  119. MicroInterpreter::~MicroInterpreter() {
  120. if (node_and_registrations_ != nullptr) {
  121. for (size_t i = 0; i < subgraph_->operators()->size(); ++i) {
  122. TfLiteNode* node = &(node_and_registrations_[i].node);
  123. const TfLiteRegistration* registration =
  124. node_and_registrations_[i].registration;
  125. // registration is allocated outside the interpreter, so double check to
  126. // make sure it's not nullptr;
  127. if (registration != nullptr && registration->free != nullptr) {
  128. registration->free(&context_, node->user_data);
  129. }
  130. }
  131. }
  132. }
  133. void MicroInterpreter::Init(tflite::Profiler* profiler) {
  134. const flatbuffers::Vector<flatbuffers::Offset<SubGraph>>* subgraphs =
  135. model_->subgraphs();
  136. if (subgraphs->size() != 1) {
  137. TF_LITE_REPORT_ERROR(error_reporter_,
  138. "Only 1 subgraph is currently supported.\n");
  139. initialization_status_ = kTfLiteError;
  140. return;
  141. }
  142. subgraph_ = (*subgraphs)[0];
  143. context_.impl_ = static_cast<void*>(&context_helper_);
  144. context_.ReportError = context_helper_.ReportOpError;
  145. context_.GetTensor = context_helper_.GetTensor;
  146. context_.GetEvalTensor = context_helper_.GetEvalTensor;
  147. context_.recommended_num_threads = 1;
  148. context_.profiler = profiler;
  149. initialization_status_ = kTfLiteOk;
  150. }
  151. void MicroInterpreter::CorrectTensorEndianness(TfLiteEvalTensor* tensorCorr) {
  152. int32_t tensorSize = 1;
  153. for (int d = 0; d < tensorCorr->dims->size; ++d)
  154. tensorSize *= reinterpret_cast<const int32_t*>(tensorCorr->dims->data)[d];
  155. switch (tensorCorr->type) {
  156. case TfLiteType::kTfLiteFloat32:
  157. CorrectTensorDataEndianness(tensorCorr->data.f, tensorSize);
  158. break;
  159. case TfLiteType::kTfLiteFloat16:
  160. CorrectTensorDataEndianness(tensorCorr->data.f16, tensorSize);
  161. break;
  162. case TfLiteType::kTfLiteInt64:
  163. CorrectTensorDataEndianness(tensorCorr->data.i64, tensorSize);
  164. break;
  165. case TfLiteType::kTfLiteInt32:
  166. CorrectTensorDataEndianness(tensorCorr->data.i32, tensorSize);
  167. break;
  168. case TfLiteType::kTfLiteInt16:
  169. CorrectTensorDataEndianness(tensorCorr->data.i16, tensorSize);
  170. break;
  171. case TfLiteType::kTfLiteComplex64:
  172. CorrectTensorDataEndianness(tensorCorr->data.c64, tensorSize);
  173. break;
  174. case TfLiteType::kTfLiteComplex128:
  175. CorrectTensorDataEndianness(tensorCorr->data.c128, tensorSize);
  176. break;
  177. default:
  178. // Do nothing for other data types.
  179. break;
  180. }
  181. }
  182. template <class T>
  183. void MicroInterpreter::CorrectTensorDataEndianness(T* data, int32_t size) {
  184. for (int32_t i = 0; i < size; ++i) {
  185. data[i] = flatbuffers::EndianScalar(data[i]);
  186. }
  187. }
  188. TfLiteStatus MicroInterpreter::AllocateTensors() {
  189. if (allocator_.StartModelAllocation(model_, op_resolver_,
  190. &node_and_registrations_,
  191. &eval_tensors_) != kTfLiteOk) {
  192. TF_LITE_REPORT_ERROR(error_reporter_,
  193. "Failed starting model allocation.\n");
  194. initialization_status_ = kTfLiteError;
  195. return kTfLiteError;
  196. }
  197. // Update the pointer now that TfLiteEvalTensor allocation has completed on
  198. // the context helper.
  199. // TODO(b/16157777): This call would not be needed if ContextHelper rolled
  200. // into the interpreter.
  201. context_helper_.SetTfLiteEvalTensors(eval_tensors_);
  202. // If the system is big endian then convert weights from the flatbuffer from
  203. // little to big endian on startup so that it does not need to be done during
  204. // inference.
  205. // NOTE: This requires that the flatbuffer is held in memory which can be
  206. // modified by this process.
  207. if (!FLATBUFFERS_LITTLEENDIAN) {
  208. for (size_t t = 0; t < subgraph_->tensors()->size(); ++t) {
  209. if (auto* buffer =
  210. (*model_->buffers())[subgraph_->tensors()->Get(t)->buffer()]) {
  211. // If we've found a buffer, does it have any data?
  212. if (auto* array = buffer->data()) {
  213. // If it has any data, is the data size larger than zero?
  214. if (array->size()) {
  215. // Update the endianness of the corresponding eval tensor since that
  216. // struct holds the buffer used at inference time.
  217. CorrectTensorEndianness(&eval_tensors_[t]);
  218. }
  219. }
  220. }
  221. }
  222. }
  223. // Only allow AllocatePersistentBuffer in Init stage.
  224. context_.AllocatePersistentBuffer = context_helper_.AllocatePersistentBuffer;
  225. context_.RequestScratchBufferInArena = nullptr;
  226. context_.GetScratchBuffer = nullptr;
  227. for (size_t i = 0; i < subgraph_->operators()->size(); ++i) {
  228. context_helper_.SetNodeIndex(i);
  229. auto* node = &(node_and_registrations_[i].node);
  230. auto* registration = node_and_registrations_[i].registration;
  231. size_t init_data_size;
  232. const char* init_data;
  233. if (registration->builtin_code == BuiltinOperator_CUSTOM) {
  234. init_data = reinterpret_cast<const char*>(node->custom_initial_data);
  235. init_data_size = node->custom_initial_data_size;
  236. } else {
  237. init_data = reinterpret_cast<const char*>(node->builtin_data);
  238. init_data_size = 0;
  239. }
  240. if (registration->init) {
  241. node->user_data =
  242. registration->init(&context_, init_data, init_data_size);
  243. }
  244. }
  245. context_helper_.SetNodeIndex(-1);
  246. // Both AllocatePersistentBuffer and RequestScratchBufferInArena is
  247. // available in Prepare stage.
  248. context_.RequestScratchBufferInArena =
  249. context_helper_.RequestScratchBufferInArena;
  250. for (size_t i = 0; i < subgraph_->operators()->size(); ++i) {
  251. // Set node idx to annotate the lifetime for scratch buffers.
  252. context_helper_.SetNodeIndex(i);
  253. auto* node = &(node_and_registrations_[i].node);
  254. auto* registration = node_and_registrations_[i].registration;
  255. if (registration->prepare) {
  256. TfLiteStatus prepare_status = registration->prepare(&context_, node);
  257. if (prepare_status != kTfLiteOk) {
  258. TF_LITE_REPORT_ERROR(
  259. error_reporter_,
  260. "Node %s (number %df) failed to prepare with status %d",
  261. OpNameFromRegistration(registration), i, prepare_status);
  262. return kTfLiteError;
  263. }
  264. }
  265. allocator_.ResetTempAllocations();
  266. }
  267. context_helper_.SetNodeIndex(-1);
  268. // Prepare is done, we're ready for Invoke. Memory allocation is no longer
  269. // allowed. Kernels can only fetch scratch buffers via GetScratchBuffer.
  270. context_.AllocatePersistentBuffer = nullptr;
  271. context_.RequestScratchBufferInArena = nullptr;
  272. context_.GetScratchBuffer = context_helper_.GetScratchBuffer;
  273. TF_LITE_ENSURE_OK(&context_,
  274. allocator_.FinishModelAllocation(model_, eval_tensors_));
  275. TF_LITE_ENSURE_STATUS(ResetVariableTensors());
  276. tensors_allocated_ = true;
  277. return kTfLiteOk;
  278. }
  279. TfLiteStatus MicroInterpreter::Invoke() {
  280. if (initialization_status_ != kTfLiteOk) {
  281. TF_LITE_REPORT_ERROR(error_reporter_,
  282. "Invoke() called after initialization failed\n");
  283. return kTfLiteError;
  284. }
  285. // Ensure tensors are allocated before the interpreter is invoked to avoid
  286. // difficult to debug segfaults.
  287. if (!tensors_allocated_) {
  288. TF_LITE_ENSURE_OK(&context_, AllocateTensors());
  289. }
  290. for (size_t i = 0; i < subgraph_->operators()->size(); ++i) {
  291. auto* node = &(node_and_registrations_[i].node);
  292. auto* registration = node_and_registrations_[i].registration;
  293. if (registration->invoke) {
  294. TfLiteStatus invoke_status;
  295. #ifndef NDEBUG // Omit profiler overhead from release builds.
  296. // The case where profiler == nullptr is handled by
  297. // ScopedOperatorProfile.
  298. tflite::Profiler* profiler =
  299. reinterpret_cast<tflite::Profiler*>(context_.profiler);
  300. ScopedOperatorProfile scoped_profiler(
  301. profiler, OpNameFromRegistration(registration), i);
  302. #endif
  303. invoke_status = registration->invoke(&context_, node);
  304. // All TfLiteTensor structs used in the kernel are allocated from temp
  305. // memory in the allocator. This creates a chain of allocations in the
  306. // temp section. The call below resets the chain of allocations to
  307. // prepare for the next call.
  308. allocator_.ResetTempAllocations();
  309. if (invoke_status == kTfLiteError) {
  310. TF_LITE_REPORT_ERROR(
  311. error_reporter_,
  312. "Node %s (number %d) failed to invoke with status %d",
  313. OpNameFromRegistration(registration), i, invoke_status);
  314. return kTfLiteError;
  315. } else if (invoke_status != kTfLiteOk) {
  316. return invoke_status;
  317. }
  318. }
  319. }
  320. return kTfLiteOk;
  321. }
  322. TfLiteTensor* MicroInterpreter::input(size_t index) {
  323. const size_t length = inputs_size();
  324. if (index >= length) {
  325. TF_LITE_REPORT_ERROR(error_reporter_,
  326. "Input index %d out of range (length is %d)", index,
  327. length);
  328. return nullptr;
  329. }
  330. if (index != 0) {
  331. TF_LITE_REPORT_ERROR(
  332. error_reporter_,
  333. "Input tensors not at index 0 are allocated from the "
  334. "persistent memory arena. Repeat calls will cause excess "
  335. "allocation!");
  336. return allocator_.AllocatePersistentTfLiteTensor(model_, eval_tensors_,
  337. inputs().Get(index));
  338. }
  339. if (input_tensor_ == nullptr) {
  340. input_tensor_ = allocator_.AllocatePersistentTfLiteTensor(
  341. model_, eval_tensors_, inputs().Get(index));
  342. }
  343. return input_tensor_;
  344. }
  345. TfLiteTensor* MicroInterpreter::output(size_t index) {
  346. const size_t length = outputs_size();
  347. if (index >= length) {
  348. TF_LITE_REPORT_ERROR(error_reporter_,
  349. "Output index %d out of range (length is %d)", index,
  350. length);
  351. return nullptr;
  352. }
  353. if (index != 0) {
  354. TF_LITE_REPORT_ERROR(
  355. error_reporter_,
  356. "Output tensors not at index 0 are allocated from the "
  357. "persistent memory arena. Repeat calls will cause excess "
  358. "allocation!");
  359. return allocator_.AllocatePersistentTfLiteTensor(model_, eval_tensors_,
  360. outputs().Get(index));
  361. }
  362. if (output_tensor_ == nullptr) {
  363. // TODO(b/160894903): This API will allocate TfLiteTensor structs from
  364. // persistent (tail) memory and cache on this pointer.
  365. output_tensor_ = allocator_.AllocatePersistentTfLiteTensor(
  366. model_, eval_tensors_, outputs().Get(index));
  367. }
  368. return output_tensor_;
  369. }
  370. TfLiteTensor* MicroInterpreter::tensor(size_t index) {
  371. const size_t length = tensors_size();
  372. if (index >= length) {
  373. TF_LITE_REPORT_ERROR(error_reporter_,
  374. "Tensor index %d out of range (length is %d)", index,
  375. length);
  376. return nullptr;
  377. }
  378. return allocator_.AllocatePersistentTfLiteTensor(model_, eval_tensors_,
  379. index);
  380. }
  381. TfLiteStatus MicroInterpreter::ResetVariableTensors() {
  382. for (size_t i = 0; i < subgraph_->tensors()->size(); ++i) {
  383. auto* tensor = subgraph_->tensors()->Get(i);
  384. if (tensor->is_variable()) {
  385. size_t buffer_size;
  386. TF_LITE_ENSURE_STATUS(
  387. TfLiteEvalTensorByteLength(&eval_tensors_[i], &buffer_size));
  388. int value = 0;
  389. if (tensor->type() == tflite::TensorType_INT8) {
  390. value = tensor->quantization()->zero_point()->Get(0);
  391. }
  392. memset(eval_tensors_[i].data.raw, value, buffer_size);
  393. }
  394. }
  395. return kTfLiteOk;
  396. }
  397. } // namespace tflite