unpack.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Copyright 2019 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/builtin_op_data.h"
  13. #include "tensorflow/lite/c/common.h"
  14. #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
  15. #include "tensorflow/lite/kernels/kernel_util.h"
  16. #include "tensorflow/lite/micro/kernels/kernel_util.h"
  17. namespace tflite {
  18. namespace ops {
  19. namespace micro {
  20. namespace unpack {
  21. namespace {
  22. constexpr int kInputTensor = 0;
  23. template <typename T>
  24. TfLiteStatus UnpackImpl(TfLiteContext* context, TfLiteNode* node,
  25. const TfLiteEvalTensor* input, int output_count,
  26. int axis) {
  27. const TfLiteEvalTensor* output0 =
  28. tflite::micro::GetEvalOutput(context, node, 0);
  29. const TfLiteIntArray* input_dims = input->dims;
  30. const TfLiteIntArray* output_dims = output0->dims;
  31. const int dimensions = input_dims->size;
  32. if (axis < 0) {
  33. axis += input->dims->size;
  34. }
  35. TFLITE_DCHECK_LT(axis, dimensions);
  36. int outer_size = 1;
  37. for (int i = 0; i < axis; ++i) {
  38. outer_size *= input_dims->data[i];
  39. }
  40. int copy_size = 1;
  41. for (int i = axis + 1; i < dimensions; ++i) {
  42. copy_size *= input_dims->data[i];
  43. }
  44. int output_size = 1;
  45. for (int i = 0; i < output_dims->size; ++i) {
  46. output_size *= output_dims->data[i];
  47. }
  48. TFLITE_DCHECK_EQ(output_size, copy_size * outer_size);
  49. const T* input_data = tflite::micro::GetTensorData<T>(input);
  50. for (int i = 0; i < output_count; ++i) {
  51. TfLiteEvalTensor* t = tflite::micro::GetEvalOutput(context, node, i);
  52. T* output_data = tflite::micro::GetTensorData<T>(t);
  53. for (int k = 0; k < outer_size; ++k) {
  54. T* output_ptr = output_data + copy_size * k;
  55. int loc = k * output_count * copy_size + i * copy_size;
  56. const T* input_ptr = input_data + loc;
  57. for (int j = 0; j < copy_size; ++j) output_ptr[j] = input_ptr[j];
  58. }
  59. }
  60. return kTfLiteOk;
  61. }
  62. TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
  63. TfLiteUnpackParams* data =
  64. reinterpret_cast<TfLiteUnpackParams*>(node->builtin_data);
  65. const TfLiteEvalTensor* input =
  66. tflite::micro::GetEvalInput(context, node, kInputTensor);
  67. switch (input->type) {
  68. case kTfLiteFloat32: {
  69. return UnpackImpl<float>(context, node, input, data->num, data->axis);
  70. }
  71. case kTfLiteInt32: {
  72. return UnpackImpl<int32_t>(context, node, input, data->num, data->axis);
  73. }
  74. case kTfLiteUInt8: {
  75. return UnpackImpl<uint8_t>(context, node, input, data->num, data->axis);
  76. }
  77. case kTfLiteInt8: {
  78. return UnpackImpl<int8_t>(context, node, input, data->num, data->axis);
  79. }
  80. default: {
  81. TF_LITE_KERNEL_LOG(context, "Type '%s' is not supported by unpack.",
  82. TfLiteTypeGetName(input->type));
  83. return kTfLiteError;
  84. }
  85. }
  86. return kTfLiteOk;
  87. }
  88. } // namespace
  89. } // namespace unpack
  90. TfLiteRegistration Register_UNPACK() {
  91. return {/*init=*/nullptr,
  92. /*free=*/nullptr,
  93. /*prepare=*/nullptr,
  94. /*invoke=*/unpack::Eval,
  95. /*profiling_string=*/nullptr,
  96. /*builtin_code=*/0,
  97. /*custom_name=*/nullptr,
  98. /*version=*/0};
  99. }
  100. } // namespace micro
  101. } // namespace ops
  102. } // namespace tflite