kernel_util.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. namespace tflite {
  15. namespace micro {
  16. namespace {
  17. int ValidateTensorIndexing(const TfLiteContext* context, int index,
  18. int max_size, const int* tensor_indices) {
  19. if (index >= 0 && index < max_size) {
  20. const int tensor_index = tensor_indices[index];
  21. if (tensor_index != kTfLiteOptionalTensor) {
  22. return tensor_index;
  23. }
  24. }
  25. return -1;
  26. }
  27. } // namespace
  28. // Returns a mutable tensor for a given input index. is_variable must be checked
  29. // during prepare when the full TfLiteTensor is available.
  30. TfLiteEvalTensor* GetMutableEvalInput(const TfLiteContext* context,
  31. const TfLiteNode* node, int index) {
  32. TFLITE_DCHECK(context != nullptr);
  33. TFLITE_DCHECK(node != nullptr);
  34. const int tensor_index = ValidateTensorIndexing(
  35. context, index, node->inputs->size, node->inputs->data);
  36. if (tensor_index < 0) {
  37. return nullptr;
  38. }
  39. return context->GetEvalTensor(context, node->inputs->data[index]);
  40. }
  41. // Returns the TfLiteEvalTensor struct for a given input index in a node.
  42. const TfLiteEvalTensor* GetEvalInput(const TfLiteContext* context,
  43. const TfLiteNode* node, int index) {
  44. return GetMutableEvalInput(context, node, index);
  45. }
  46. // Returns the TfLiteEvalTensor struct for a given output index in a node.
  47. TfLiteEvalTensor* GetEvalOutput(const TfLiteContext* context,
  48. const TfLiteNode* node, int index) {
  49. TFLITE_DCHECK(context != nullptr);
  50. TFLITE_DCHECK(node != nullptr);
  51. return context->GetEvalTensor(context, node->outputs->data[index]);
  52. }
  53. bool HaveSameShapes(const TfLiteEvalTensor* input1,
  54. const TfLiteEvalTensor* input2) {
  55. TFLITE_DCHECK(input1 != nullptr);
  56. TFLITE_DCHECK(input2 != nullptr);
  57. return TfLiteIntArrayEqual(input1->dims, input2->dims);
  58. }
  59. const RuntimeShape GetTensorShape(const TfLiteEvalTensor* tensor) {
  60. if (tensor == nullptr || tensor->dims == nullptr) {
  61. return RuntimeShape();
  62. }
  63. TfLiteIntArray* dims = tensor->dims;
  64. const int dims_size = dims->size;
  65. const int32_t* dims_data = reinterpret_cast<const int32_t*>(dims->data);
  66. return RuntimeShape(dims_size, dims_data);
  67. }
  68. PaddingType RuntimePaddingType(TfLitePadding padding) {
  69. switch (padding) {
  70. case TfLitePadding::kTfLitePaddingSame:
  71. return PaddingType::kSame;
  72. case TfLitePadding::kTfLitePaddingValid:
  73. return PaddingType::kValid;
  74. case TfLitePadding::kTfLitePaddingUnknown:
  75. default:
  76. return PaddingType::kNone;
  77. }
  78. }
  79. // Relocate tensor dims from FlatBuffer to the persistent storage arena.
  80. // The old dims data is copied to the new storage area.
  81. // The tensor and eval_tensor must be the same tensor.
  82. // Only use during Prepare phase.
  83. TfLiteStatus CreateWritableTensorDimsWithCopy(TfLiteContext* context,
  84. TfLiteTensor* tensor,
  85. TfLiteEvalTensor* eval_tensor) {
  86. TF_LITE_ENSURE(context, tensor != nullptr);
  87. TF_LITE_ENSURE(context, eval_tensor != nullptr);
  88. TF_LITE_ENSURE(context, context->AllocatePersistentBuffer != nullptr);
  89. int ranks = tensor->dims->size;
  90. size_t alloc_size = TfLiteIntArrayGetSizeInBytes(ranks);
  91. TfLiteIntArray* new_dims = static_cast<TfLiteIntArray*>(
  92. context->AllocatePersistentBuffer(context, alloc_size));
  93. TfLiteIntArray* old_dims = tensor->dims;
  94. new_dims->size = ranks;
  95. tensor->dims = new_dims;
  96. eval_tensor->dims = new_dims;
  97. for (int i = 0; i < ranks; i++) {
  98. new_dims->data[i] = old_dims->data[i];
  99. }
  100. return kTfLiteOk;
  101. }
  102. // Returns a blob of payload data. The payload is subjected to interpretation by
  103. // the OP. This is the recommended API for an OP to get an external context. OP
  104. // should use this instead of directly calling GetExternalContext function in
  105. // context.
  106. void* GetExternalContext(TfLiteContext* context) {
  107. return reinterpret_cast<void*>(
  108. context->GetExternalContext(context, kTfLiteMaxExternalContexts));
  109. }
  110. } // namespace micro
  111. } // namespace tflite