1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_TRANSPOSE_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_TRANSPOSE_H_
17 
18 #include "tensorflow/lite/kernels/internal/common.h"
19 #include "tensorflow/lite/kernels/internal/types.h"
20 
21 namespace tflite {
22 
23 namespace reference_ops {
24 
25 template <typename T, int N>
TransposeImpl(const TransposeParams & params,const RuntimeShape & unextended_input_shape,const T * input_data,const RuntimeShape & unextended_output_shape,T * output_data)26 void TransposeImpl(const TransposeParams& params,
27                    const RuntimeShape& unextended_input_shape,
28                    const T* input_data,
29                    const RuntimeShape& unextended_output_shape,
30                    T* output_data) {
31   const int unextended_input_size = unextended_input_shape.DimensionsCount();
32   const int unextended_output_size = unextended_output_shape.DimensionsCount();
33   TFLITE_DCHECK_LE(unextended_input_size, N);
34   TFLITE_DCHECK_LE(unextended_output_size, N);
35   TFLITE_DCHECK_EQ(unextended_output_size, params.perm_count);
36   const int input_ext_size = N - unextended_input_size;
37   const int output_ext_size = N - unextended_output_size;
38   NdArrayDesc<N> input_desc;
39   NdArrayDesc<N> output_desc;
40   CopyDimsToDesc(RuntimeShape::ExtendedShape(N, unextended_input_shape),
41                  &input_desc);
42   CopyDimsToDesc(RuntimeShape::ExtendedShape(N, unextended_output_shape),
43                  &output_desc);
44 
45   // The perm data is extended to match the output, each index incremented by
46   // the amount of front padding of the input shape.
47   int extended_perm[N];
48   for (int i = 0; i < N; ++i) {
49     extended_perm[i] = i < output_ext_size
50                            ? i
51                            : params.perm[i - output_ext_size] + input_ext_size;
52   }
53 
54   // Permutes the input shape so we don't need to permute the indexes inside
55   // the loop. Check to make sure output_dims is matching input_dims.
56   NdArrayDesc<N> perm_input_desc;
57   for (int k = 0; k < N; ++k) {
58     TFLITE_DCHECK_EQ(input_desc.extents[extended_perm[k]],
59                      output_desc.extents[k]);
60     perm_input_desc.extents[k] = input_desc.extents[extended_perm[k]];
61     perm_input_desc.strides[k] = input_desc.strides[extended_perm[k]];
62   }
63 
64   // Naive transpose loop (iterate on output index and compute input index).
65   auto tranpose_func = [&](int indexes[N]) {
66     output_data[SubscriptToIndex(output_desc, indexes)] =
67         input_data[SubscriptToIndex(perm_input_desc, indexes)];
68   };
69   NDOpsHelper<N>(output_desc, tranpose_func);
70 }
71 
72 template <typename T, int N = 5>
Transpose(const TransposeParams & params,const RuntimeShape & unextended_input_shape,const T * input_data,const RuntimeShape & unextended_output_shape,T * output_data)73 void Transpose(const TransposeParams& params,
74                const RuntimeShape& unextended_input_shape, const T* input_data,
75                const RuntimeShape& unextended_output_shape, T* output_data) {
76   // Transpose kernel only does rearranging values not numeric evaluations on
77   // each cell. It's safe to implement per size of scalar type and this trick
78   // keeps the total code size in a reasonable range.
79   switch (sizeof(T)) {
80     case 1:
81       TransposeImpl<int8_t, N>(params, unextended_input_shape,
82                                reinterpret_cast<const int8_t*>(input_data),
83                                unextended_output_shape,
84                                reinterpret_cast<int8_t*>(output_data));
85       break;
86     case 2:
87       TransposeImpl<int16_t, N>(params, unextended_input_shape,
88                                 reinterpret_cast<const int16_t*>(input_data),
89                                 unextended_output_shape,
90                                 reinterpret_cast<int16_t*>(output_data));
91       break;
92 
93     case 4:
94       TransposeImpl<int32_t, N>(params, unextended_input_shape,
95                                 reinterpret_cast<const int32_t*>(input_data),
96                                 unextended_output_shape,
97                                 reinterpret_cast<int32_t*>(output_data));
98       break;
99     case 8:
100       TransposeImpl<int64_t, N>(params, unextended_input_shape,
101                                 reinterpret_cast<const int64_t*>(input_data),
102                                 unextended_output_shape,
103                                 reinterpret_cast<int64_t*>(output_data));
104       break;
105   }
106 }
107 
108 }  // namespace reference_ops
109 }  // namespace tflite
110 
111 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_TRANSPOSE_H_
112