kernel_util.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* Copyright 2020 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/kernels/kernel_util.h"
  13. #include "tensorflow/lite/c/common.h"
  14. #include "tensorflow/lite/micro/memory_helpers.h"
  15. namespace tflite {
  16. namespace micro {
  17. namespace {
  18. int ValidateTensorIndexing(const TfLiteContext* context, int index,
  19. int max_size, const int* tensor_indices) {
  20. if (index >= 0 && index < max_size) {
  21. const int tensor_index = tensor_indices[index];
  22. if (tensor_index != kTfLiteOptionalTensor) {
  23. return tensor_index;
  24. }
  25. }
  26. return -1;
  27. }
  28. } // namespace
  29. // Returns a mutable tensor for a given input index. is_variable must be checked
  30. // during prepare when the full TfLiteTensor is available.
  31. TfLiteEvalTensor* GetMutableEvalInput(const TfLiteContext* context,
  32. const TfLiteNode* node, int index) {
  33. TFLITE_DCHECK(context != nullptr);
  34. TFLITE_DCHECK(node != nullptr);
  35. const int tensor_index = ValidateTensorIndexing(
  36. context, index, node->inputs->size, node->inputs->data);
  37. if (tensor_index < 0) {
  38. return nullptr;
  39. }
  40. return context->GetEvalTensor(context, node->inputs->data[index]);
  41. }
  42. // Returns the TfLiteEvalTensor struct for a given input index in a node.
  43. const TfLiteEvalTensor* GetEvalInput(const TfLiteContext* context,
  44. const TfLiteNode* node, int index) {
  45. return GetMutableEvalInput(context, node, index);
  46. }
  47. // Returns the TfLiteEvalTensor struct for a given output index in a node.
  48. TfLiteEvalTensor* GetEvalOutput(const TfLiteContext* context,
  49. const TfLiteNode* node, int index) {
  50. TFLITE_DCHECK(context != nullptr);
  51. TFLITE_DCHECK(node != nullptr);
  52. return context->GetEvalTensor(context, node->outputs->data[index]);
  53. }
  54. bool HaveSameShapes(const TfLiteEvalTensor* input1,
  55. const TfLiteEvalTensor* input2) {
  56. TFLITE_DCHECK(input1 != nullptr);
  57. TFLITE_DCHECK(input2 != nullptr);
  58. return TfLiteIntArrayEqual(input1->dims, input2->dims);
  59. }
  60. const RuntimeShape GetTensorShape(const TfLiteEvalTensor* tensor) {
  61. if (tensor == nullptr || tensor->dims == nullptr) {
  62. return RuntimeShape();
  63. }
  64. TfLiteIntArray* dims = tensor->dims;
  65. const int dims_size = dims->size;
  66. const int32_t* dims_data = reinterpret_cast<const int32_t*>(dims->data);
  67. return RuntimeShape(dims_size, dims_data);
  68. }
  69. PaddingType RuntimePaddingType(TfLitePadding padding) {
  70. switch (padding) {
  71. case TfLitePadding::kTfLitePaddingSame:
  72. return PaddingType::kSame;
  73. case TfLitePadding::kTfLitePaddingValid:
  74. return PaddingType::kValid;
  75. case TfLitePadding::kTfLitePaddingUnknown:
  76. default:
  77. return PaddingType::kNone;
  78. }
  79. }
  80. // Relocate tensor dims from FlatBuffer to the persistent storage arena.
  81. // The old dims data is copied to the new storage area.
  82. // The tensor and eval_tensor must be the same tensor.
  83. // Only use during Prepare phase.
  84. TfLiteStatus CreateWritableTensorDimsWithCopy(TfLiteContext* context,
  85. TfLiteTensor* tensor,
  86. TfLiteEvalTensor* eval_tensor) {
  87. TF_LITE_ENSURE(context, tensor != nullptr);
  88. TF_LITE_ENSURE(context, eval_tensor != nullptr);
  89. TF_LITE_ENSURE(context, context->AllocatePersistentBuffer != nullptr);
  90. int ranks = tensor->dims->size;
  91. size_t alloc_size = TfLiteIntArrayGetSizeInBytes(ranks);
  92. TfLiteIntArray* new_dims = static_cast<TfLiteIntArray*>(
  93. context->AllocatePersistentBuffer(context, alloc_size));
  94. TfLiteIntArray* old_dims = tensor->dims;
  95. new_dims->size = ranks;
  96. tensor->dims = new_dims;
  97. eval_tensor->dims = new_dims;
  98. for (int i = 0; i < ranks; i++) {
  99. new_dims->data[i] = old_dims->data[i];
  100. }
  101. return kTfLiteOk;
  102. }
  103. // Verify that both tensors have the same type and size, then return the size
  104. // of both tensors in bytes if they are the same, or -1 if they are different.
  105. size_t ValidateAndGetTensorSizes(const TfLiteEvalTensor* tensor1,
  106. const TfLiteEvalTensor* tensor2) {
  107. TFLITE_DCHECK(tensor1->type == tensor2->type);
  108. size_t tensor1_size = 0;
  109. size_t tensor2_size = 0;
  110. TfLiteEvalTensorByteLength(tensor1, &tensor1_size);
  111. TfLiteEvalTensorByteLength(tensor2, &tensor2_size);
  112. return (tensor1_size == tensor2_size) ? tensor1_size : -1;
  113. }
  114. TfLiteStatus CopyOpInputsToOpOutputs(TfLiteContext* context, TfLiteNode* node) {
  115. TF_LITE_ENSURE(context, node->inputs->size == node->outputs->size);
  116. for (int i = 0; i < node->inputs->size; i++) {
  117. const TfLiteEvalTensor* input =
  118. tflite::micro::GetEvalInput(context, node, i);
  119. TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, i);
  120. int bytes = ValidateAndGetTensorSizes(input, output);
  121. TF_LITE_ENSURE(context, bytes >= 0);
  122. memcpy(output->data.raw, input->data.raw, bytes);
  123. }
  124. return kTfLiteOk;
  125. }
  126. TfLiteStatus CopyOpInputsToSubgraphInputs(TfLiteContext* context,
  127. TfLiteNode* node,
  128. MicroGraph* graph_info,
  129. int subgraph_idx,
  130. int first_tensor_idx) {
  131. TF_LITE_ENSURE(context,
  132. static_cast<size_t>(node->inputs->size - first_tensor_idx) ==
  133. graph_info->NumSubgraphInputs(subgraph_idx));
  134. for (int i = 0; i < node->inputs->size - first_tensor_idx; i++) {
  135. const TfLiteEvalTensor* input =
  136. tflite::micro::GetEvalInput(context, node, i + first_tensor_idx);
  137. TfLiteEvalTensor* subgraph_input =
  138. graph_info->GetSubgraphInput(subgraph_idx, i);
  139. int bytes = ValidateAndGetTensorSizes(input, subgraph_input);
  140. TF_LITE_ENSURE(context, bytes >= 0);
  141. memcpy(subgraph_input->data.raw, input->data.raw, bytes);
  142. }
  143. return kTfLiteOk;
  144. }
  145. TfLiteStatus CopyOpOutputsToSubgraphInputs(TfLiteContext* context,
  146. TfLiteNode* node,
  147. MicroGraph* graph_info,
  148. int subgraph_idx) {
  149. TF_LITE_ENSURE(context, static_cast<size_t>(node->outputs->size) ==
  150. graph_info->NumSubgraphInputs(subgraph_idx));
  151. for (int i = 0; i < node->outputs->size; i++) {
  152. TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, i);
  153. TfLiteEvalTensor* subgraph_input =
  154. graph_info->GetSubgraphInput(subgraph_idx, i);
  155. int bytes = ValidateAndGetTensorSizes(output, subgraph_input);
  156. TF_LITE_ENSURE(context, bytes >= 0);
  157. memcpy(subgraph_input->data.raw, output->data.raw, bytes);
  158. }
  159. return kTfLiteOk;
  160. }
  161. TfLiteStatus CopySubgraphOutputsToOpOutputs(TfLiteContext* context,
  162. TfLiteNode* node,
  163. MicroGraph* graph_info,
  164. int subgraph_idx) {
  165. TF_LITE_ENSURE(context, static_cast<size_t>(node->outputs->size) ==
  166. graph_info->NumSubgraphOutputs(subgraph_idx));
  167. for (int i = 0; i < node->outputs->size; i++) {
  168. TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, i);
  169. TfLiteEvalTensor* subgraph_output =
  170. graph_info->GetSubgraphOutput(subgraph_idx, i);
  171. int bytes = ValidateAndGetTensorSizes(output, subgraph_output);
  172. TF_LITE_ENSURE(context, bytes >= 0);
  173. memcpy(output->data.raw, subgraph_output->data.raw, bytes);
  174. }
  175. return kTfLiteOk;
  176. }
  177. } // namespace micro
  178. } // namespace tflite