expand_dims.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/c/common.h"
  13. #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
  14. #include "tensorflow/lite/kernels/kernel_util.h"
  15. #include "tensorflow/lite/micro/kernels/kernel_util.h"
  16. #include "tensorflow/lite/micro/micro_utils.h"
  17. namespace tflite {
  18. namespace {
  19. constexpr int kInputTensor = 0;
  20. constexpr int kAxisTensor = 1;
  21. constexpr int kOutputTensor = 0;
  22. TfLiteStatus GetAxisValueFromTensor(TfLiteContext* context,
  23. const TfLiteTensor* axis,
  24. int32_t* axis_value) {
  25. const int axis_dims = (tflite::GetTensorShape(axis)).DimensionsCount();
  26. if (axis_dims > 1) {
  27. TF_LITE_KERNEL_LOG(context, "Axis has only one element for Expand_Dims.",
  28. axis_dims);
  29. return kTfLiteError;
  30. }
  31. if (kTfLiteInt32 == (axis->type)) {
  32. const int32_t* axis_ptr = tflite::GetTensorData<int32_t>(axis);
  33. *axis_value = axis_ptr[0];
  34. return kTfLiteOk;
  35. } else {
  36. TF_LITE_KERNEL_LOG(context,
  37. "Axis type %s (%d) not supported by Expand_Dims.",
  38. TfLiteTypeGetName(axis->type), axis->type);
  39. return kTfLiteError;
  40. }
  41. }
  42. // Verifies that the output tensor's dimension shape is equivalent to inserting
  43. // a dimension of length 1 at the dimension index axis of input's shape as
  44. // defined in https://www.tensorflow.org/api_docs/python/tf/expand_dims.
  45. TfLiteStatus VerifyTensorDim(TfLiteContext* context, const TfLiteTensor* input,
  46. const TfLiteTensor* axis_tensor,
  47. const TfLiteTensor* output) {
  48. int32_t axis_value = 0;
  49. TF_LITE_ENSURE_OK(context,
  50. GetAxisValueFromTensor(context, axis_tensor, &axis_value));
  51. tflite::RuntimeShape input_shape = tflite::GetTensorShape(input);
  52. if (axis_value < 0) {
  53. axis_value = input_shape.DimensionsCount() + 1 + axis_value;
  54. }
  55. TF_LITE_ENSURE(context, axis_value <= input_shape.DimensionsCount());
  56. // TFLM only supports fixed dimension tensor and assumes that the output shape
  57. // is fully specified in the model. As such, TFLM directly use the pointer to
  58. // the dimension array in the model buffer.
  59. tflite::RuntimeShape output_shape = tflite::GetTensorShape(output);
  60. TF_LITE_ENSURE(context, output_shape.DimensionsCount() ==
  61. input_shape.DimensionsCount() + 1);
  62. for (int i = 0; i < output_shape.DimensionsCount(); ++i) {
  63. if (i < axis_value) {
  64. TF_LITE_ENSURE(context, output_shape.Dims(i) == input_shape.Dims(i));
  65. } else if (i == axis_value) {
  66. TF_LITE_ENSURE(context, output_shape.Dims(i) == 1);
  67. } else {
  68. TF_LITE_ENSURE(context, output_shape.Dims(i) == input_shape.Dims(i - 1));
  69. }
  70. }
  71. return kTfLiteOk;
  72. }
  73. TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
  74. MicroContext* micro_context = GetMicroContext(context);
  75. TF_LITE_ENSURE_EQ(context, NumInputs(node), 2);
  76. TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
  77. TfLiteTensor* input =
  78. micro_context->AllocateTempInputTensor(node, kInputTensor);
  79. TF_LITE_ENSURE(context, input != nullptr);
  80. TfLiteTensor* axis =
  81. micro_context->AllocateTempInputTensor(node, kAxisTensor);
  82. TF_LITE_ENSURE(context, axis != nullptr);
  83. TfLiteTensor* output =
  84. micro_context->AllocateTempOutputTensor(node, kOutputTensor);
  85. TF_LITE_ENSURE(context, output != nullptr);
  86. output->type = input->type;
  87. if (IsDynamicTensor(axis)) {
  88. TF_LITE_KERNEL_LOG(context,
  89. "DynamicTensor is not yet supported by Expand_Dims.");
  90. return kTfLiteError;
  91. }
  92. TF_LITE_ENSURE_OK(context, VerifyTensorDim(context, input, axis, output));
  93. micro_context->DeallocateTempTfLiteTensor(input);
  94. micro_context->DeallocateTempTfLiteTensor(axis);
  95. micro_context->DeallocateTempTfLiteTensor(output);
  96. return kTfLiteOk;
  97. }
  98. template <typename T>
  99. void memCopyN(T* out, const T* in, const int num_elements) {
  100. for (int i = 0; i < num_elements; ++i) {
  101. out[i] = in[i];
  102. }
  103. }
  104. TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
  105. const TfLiteEvalTensor* input =
  106. tflite::micro::GetEvalInput(context, node, kInputTensor);
  107. TfLiteEvalTensor* output =
  108. tflite::micro::GetEvalOutput(context, node, kOutputTensor);
  109. const int flat_size = ElementCount(*input->dims);
  110. switch (input->type) {
  111. case kTfLiteFloat32: {
  112. memCopyN(tflite::micro::GetTensorData<float>(output),
  113. tflite::micro::GetTensorData<float>(input), flat_size);
  114. } break;
  115. case kTfLiteInt8: {
  116. memCopyN(tflite::micro::GetTensorData<int8_t>(output),
  117. tflite::micro::GetTensorData<int8_t>(input), flat_size);
  118. } break;
  119. default:
  120. TF_LITE_KERNEL_LOG(
  121. context,
  122. "Expand_Dims only currently supports int8 and float32, got %d.",
  123. input->type);
  124. return kTfLiteError;
  125. }
  126. return kTfLiteOk;
  127. }
  128. } // namespace
  129. TfLiteRegistration Register_EXPAND_DIMS() {
  130. return tflite::micro::RegisterOp(nullptr, Prepare, Eval);
  131. }
  132. } // namespace tflite