transpose.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_TRANSPOSE_H_
  13. #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_TRANSPOSE_H_
  14. #include "tensorflow/lite/kernels/internal/common.h"
  15. #include "tensorflow/lite/kernels/internal/types.h"
  16. namespace tflite {
  17. namespace reference_ops {
  18. template <typename T, int N>
  19. void TransposeImpl(const TransposeParams& params,
  20. const RuntimeShape& unextended_input_shape,
  21. const T* input_data,
  22. const RuntimeShape& unextended_output_shape,
  23. T* output_data) {
  24. const int unextended_input_size = unextended_input_shape.DimensionsCount();
  25. const int unextended_output_size = unextended_output_shape.DimensionsCount();
  26. TFLITE_DCHECK_LE(unextended_input_size, N);
  27. TFLITE_DCHECK_LE(unextended_output_size, N);
  28. TFLITE_DCHECK_EQ(unextended_output_size, params.perm_count);
  29. const int input_ext_size = N - unextended_input_size;
  30. const int output_ext_size = N - unextended_output_size;
  31. NdArrayDesc<N> input_desc;
  32. NdArrayDesc<N> output_desc;
  33. CopyDimsToDesc(RuntimeShape::ExtendedShape(N, unextended_input_shape),
  34. &input_desc);
  35. CopyDimsToDesc(RuntimeShape::ExtendedShape(N, unextended_output_shape),
  36. &output_desc);
  37. // The perm data is extended to match the output, each index incremented by
  38. // the amount of front padding of the input shape.
  39. int extended_perm[N];
  40. for (int i = 0; i < N; ++i) {
  41. extended_perm[i] = i < output_ext_size
  42. ? i
  43. : params.perm[i - output_ext_size] + input_ext_size;
  44. }
  45. // Permutes the input shape so we don't need to permute the indexes inside
  46. // the loop. Check to make sure output_dims is matching input_dims.
  47. NdArrayDesc<N> perm_input_desc;
  48. for (int k = 0; k < N; ++k) {
  49. TFLITE_DCHECK_EQ(input_desc.extents[extended_perm[k]],
  50. output_desc.extents[k]);
  51. perm_input_desc.extents[k] = input_desc.extents[extended_perm[k]];
  52. perm_input_desc.strides[k] = input_desc.strides[extended_perm[k]];
  53. }
  54. // Naive transpose loop (iterate on output index and compute input index).
  55. auto tranpose_func = [&](int indexes[N]) {
  56. output_data[SubscriptToIndex(output_desc, indexes)] =
  57. input_data[SubscriptToIndex(perm_input_desc, indexes)];
  58. };
  59. NDOpsHelper<N>(output_desc, tranpose_func);
  60. }
  61. template <typename T, int N = 5>
  62. void Transpose(const TransposeParams& params,
  63. const RuntimeShape& unextended_input_shape, const T* input_data,
  64. const RuntimeShape& unextended_output_shape, T* output_data) {
  65. // Transpose kernel only does rearranging values not numeric evaluations on
  66. // each cell. It's safe to implement per size of scalar type and this trick
  67. // keeps the total code size in a reasonable range.
  68. switch (sizeof(T)) {
  69. case 1:
  70. TransposeImpl<int8_t, N>(params, unextended_input_shape,
  71. reinterpret_cast<const int8_t*>(input_data),
  72. unextended_output_shape,
  73. reinterpret_cast<int8_t*>(output_data));
  74. break;
  75. case 2:
  76. TransposeImpl<int16_t, N>(params, unextended_input_shape,
  77. reinterpret_cast<const int16_t*>(input_data),
  78. unextended_output_shape,
  79. reinterpret_cast<int16_t*>(output_data));
  80. break;
  81. case 4:
  82. TransposeImpl<int32_t, N>(params, unextended_input_shape,
  83. reinterpret_cast<const int32_t*>(input_data),
  84. unextended_output_shape,
  85. reinterpret_cast<int32_t*>(output_data));
  86. break;
  87. case 8:
  88. TransposeImpl<int64_t, N>(params, unextended_input_shape,
  89. reinterpret_cast<const int64_t*>(input_data),
  90. unextended_output_shape,
  91. reinterpret_cast<int64_t*>(output_data));
  92. break;
  93. }
  94. }
  95. } // namespace reference_ops
  96. } // namespace tflite
  97. #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_TRANSPOSE_H_