micro_context.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* Copyright 2021 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_context.h"
  13. #include <cstdarg>
  14. #include <cstddef>
  15. #include <cstdint>
  16. #include "tensorflow/lite/micro/micro_error_reporter.h"
  17. namespace tflite {
  18. MicroContext::MicroContext(MicroAllocator* allocator, const Model* model,
  19. MicroGraph* graph)
  20. : allocator_(*allocator), graph_(*graph), model_(model) {}
  21. MicroContext::~MicroContext() {}
  22. void* MicroContext::AllocatePersistentBuffer(size_t bytes) {
  23. return allocator_.AllocatePersistentBuffer(bytes);
  24. }
  25. TfLiteStatus MicroContext::RequestScratchBufferInArena(size_t bytes,
  26. int* buffer_idx) {
  27. return allocator_.RequestScratchBufferInArena(
  28. bytes, graph_.GetCurrentSubgraphIndex(), buffer_idx);
  29. }
  30. void* MicroContext::GetScratchBuffer(int buffer_idx) {
  31. ScratchBufferHandle* handle = scratch_buffer_handles_ + buffer_idx;
  32. return handle->data;
  33. }
  34. TfLiteTensor* MicroContext::AllocateTempTfLiteTensor(int tensor_idx) {
  35. return allocator_.AllocateTempTfLiteTensor(model_, graph_.GetAllocations(),
  36. tensor_idx,
  37. graph_.GetCurrentSubgraphIndex());
  38. }
  39. int MicroContext::GetTensorIndex(int index, int max_size,
  40. const int* tensor_indices) {
  41. if (index >= 0 && index < max_size) {
  42. const int tensor_index = tensor_indices[index];
  43. if (tensor_index != kTfLiteOptionalTensor) {
  44. return tensor_index;
  45. }
  46. }
  47. return -1;
  48. }
  49. TfLiteTensor* MicroContext::AllocateTempInputTensor(const TfLiteNode* node,
  50. int index) {
  51. const int tensor_index =
  52. GetTensorIndex(index, node->inputs->size, node->inputs->data);
  53. if (tensor_index < 0) {
  54. return nullptr;
  55. }
  56. return AllocateTempTfLiteTensor(tensor_index);
  57. }
  58. TfLiteTensor* MicroContext::AllocateTempOutputTensor(const TfLiteNode* node,
  59. int index) {
  60. const int tensor_index =
  61. GetTensorIndex(index, node->outputs->size, node->outputs->data);
  62. if (tensor_index < 0) {
  63. return nullptr;
  64. }
  65. return AllocateTempTfLiteTensor(tensor_index);
  66. }
  67. TfLiteTensor* MicroContext::AllocateTempIntermediateTensor(
  68. const TfLiteNode* node, int index) {
  69. const int tensor_index = GetTensorIndex(index, node->intermediates->size,
  70. node->intermediates->data);
  71. if (tensor_index < 0) {
  72. return nullptr;
  73. }
  74. return AllocateTempTfLiteTensor(tensor_index);
  75. }
  76. void MicroContext::DeallocateTempTfLiteTensor(TfLiteTensor* tensor) {
  77. return allocator_.DeallocateTempTfLiteTensor(tensor);
  78. }
  79. TfLiteEvalTensor* MicroContext::GetEvalTensor(int tensor_idx) {
  80. return &graph_.GetAllocations()[graph_.GetCurrentSubgraphIndex()]
  81. .tensors[tensor_idx];
  82. }
  83. void MicroContext::SetScratchBufferHandles(
  84. ScratchBufferHandle* scratch_buffer_handles) {
  85. scratch_buffer_handles_ = scratch_buffer_handles;
  86. }
  87. TfLiteStatus MicroContext::set_external_context(
  88. void* external_context_payload) {
  89. if (external_context_payload == nullptr ||
  90. external_context_payload_ != nullptr) {
  91. MicroPrintf(
  92. "Attempting to set external context to %x but it was %x already",
  93. external_context_payload, external_context_payload_);
  94. return kTfLiteError;
  95. }
  96. external_context_payload_ = external_context_payload;
  97. return kTfLiteOk;
  98. }
  99. void MicroContextReportOpError(struct TfLiteContext* context,
  100. const char* format, ...) {
  101. va_list args;
  102. va_start(args, format);
  103. GetMicroErrorReporter()->Report(format, args);
  104. va_end(args);
  105. }
  106. } // namespace tflite