transpose.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/kernels/internal/reference/transpose.h"
  13. #include "tensorflow/lite/c/common.h"
  14. #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
  15. #include "tensorflow/lite/kernels/internal/types.h"
  16. #include "tensorflow/lite/kernels/kernel_util.h"
  17. namespace tflite {
  18. namespace {
  19. struct TransposeContext {
  20. TransposeContext(TfLiteContext* context, TfLiteNode* node) {
  21. input = GetInput(context, node, 0);
  22. perm = GetInput(context, node, 1);
  23. output = GetOutput(context, node, 0);
  24. }
  25. const TfLiteTensor* input;
  26. const TfLiteTensor* perm;
  27. TfLiteTensor* output;
  28. };
  29. TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
  30. TF_LITE_ENSURE_EQ(context, NumInputs(node), 2);
  31. TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
  32. TransposeContext op_context(context, node);
  33. // Ensure validity of input tensor.
  34. TF_LITE_ENSURE_MSG(context, NumDimensions(op_context.input) <= 5,
  35. "Transpose op only supports 1D-5D input arrays.");
  36. TF_LITE_ENSURE_TYPES_EQ(context, op_context.input->type,
  37. op_context.output->type);
  38. int dims = NumDimensions(op_context.input);
  39. const int32_t* perm_data = GetTensorData<int32_t>(op_context.perm);
  40. // Ensure validity of the permutations tensor as a 1D tensor.
  41. TF_LITE_ENSURE_EQ(context, NumDimensions(op_context.perm), 1);
  42. TF_LITE_ENSURE_EQ(context, op_context.perm->dims->data[0], dims);
  43. for (int idx = 0; idx < dims; ++idx) {
  44. TF_LITE_ENSURE_MSG(context, (perm_data[idx] >= 0 && perm_data[idx] < dims),
  45. "Transpose op permutations array is out of bounds.");
  46. }
  47. return kTfLiteOk;
  48. }
  49. TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
  50. TransposeContext op_context(context, node);
  51. const int32_t* perm_data = GetTensorData<int32_t>(op_context.perm);
  52. const int size = op_context.perm->dims->data[0];
  53. TransposeParams params;
  54. params.perm_count = size;
  55. for (int i = 0; i < size; ++i) {
  56. params.perm[i] = perm_data[i];
  57. }
  58. // Transpose kernel only does rearranging values not numeric evaluations
  59. // on each cell. It's safe to implement per size of scalar type and this
  60. // trick keeps the total code size in a reasonable range.
  61. switch (op_context.input->type) {
  62. case kTfLiteFloat32:
  63. reference_ops::Transpose(params, GetTensorShape(op_context.input),
  64. GetTensorData<float>(op_context.input),
  65. GetTensorShape(op_context.output),
  66. GetTensorData<float>(op_context.output));
  67. break;
  68. case kTfLiteInt8:
  69. reference_ops::Transpose(params, GetTensorShape(op_context.input),
  70. GetTensorData<int8_t>(op_context.input),
  71. GetTensorShape(op_context.output),
  72. GetTensorData<int8_t>(op_context.output));
  73. break;
  74. default:
  75. TF_LITE_KERNEL_LOG(context,
  76. "Type %s is currently not supported by Transpose. "
  77. "Only float32 and int8 is supported",
  78. TfLiteTypeGetName(op_context.input->type));
  79. return kTfLiteError;
  80. }
  81. return kTfLiteOk;
  82. }
  83. } // namespace
  84. TfLiteRegistration Register_TRANSPOSE() {
  85. return {/*init=*/nullptr,
  86. /*free=*/nullptr,
  87. /*prepare=*/Prepare,
  88. /*invoke=*/Eval,
  89. /*profiling_string=*/nullptr,
  90. /*builtin_code=*/0,
  91. /*custom_name=*/nullptr,
  92. /*version=*/0};
  93. }
  94. } // namespace tflite