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_SPACE_TO_DEPTH_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SPACE_TO_DEPTH_H_
17
18 #include "tensorflow/lite/kernels/internal/types.h"
19
20 namespace tflite {
21 namespace reference_ops {
22
23 template <typename T>
SpaceToDepth(const tflite::SpaceToDepthParams & op_params,const RuntimeShape & unextended_input_shape,const T * input_data,const RuntimeShape & unextended_output_shape,T * output_data)24 inline void SpaceToDepth(const tflite::SpaceToDepthParams& op_params,
25 const RuntimeShape& unextended_input_shape,
26 const T* input_data,
27 const RuntimeShape& unextended_output_shape,
28 T* output_data) {
29 TFLITE_DCHECK_LE(unextended_input_shape.DimensionsCount(), 4);
30 TFLITE_DCHECK_LE(unextended_output_shape.DimensionsCount(), 4);
31 const RuntimeShape input_shape =
32 RuntimeShape::ExtendedShape(4, unextended_input_shape);
33 const RuntimeShape output_shape =
34 RuntimeShape::ExtendedShape(4, unextended_output_shape);
35
36 const int input_depth = input_shape.Dims(3);
37 const int input_width = input_shape.Dims(2);
38 const int input_height = input_shape.Dims(1);
39 const int input_batch = input_shape.Dims(0);
40
41 const int output_depth = output_shape.Dims(3);
42 const int output_width = output_shape.Dims(2);
43 const int output_height = output_shape.Dims(1);
44 const int output_batch = output_shape.Dims(0);
45
46 const int32 block_size = op_params.block_size;
47
48 TFLITE_DCHECK_EQ(input_width, output_width * block_size);
49 TFLITE_DCHECK_EQ(input_height, output_height * block_size);
50 TFLITE_DCHECK_EQ(input_depth * block_size * block_size, output_depth);
51 TFLITE_DCHECK_EQ(input_batch, output_batch);
52
53 for (int in_b = 0; in_b < input_batch; ++in_b) {
54 for (int in_h = 0; in_h < input_height; ++in_h) {
55 for (int in_w = 0; in_w < input_width; ++in_w) {
56 for (int in_d = 0; in_d < input_depth; ++in_d) {
57 const int out_d =
58 in_d + ((in_h % block_size) * block_size + in_w % block_size) *
59 input_depth;
60 const int out_w = in_w / block_size;
61 const int out_h = in_h / block_size;
62 const int out_b = in_b;
63
64 const int input_index = Offset(input_shape, in_b, in_h, in_w, in_d);
65 const int output_index =
66 Offset(output_shape, out_b, out_h, out_w, out_d);
67
68 output_data[output_index] = input_data[input_index];
69 }
70 }
71 }
72 }
73 }
74
75 } // namespace reference_ops
76 } // namespace tflite
77
78 #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SPACE_TO_DEPTH_H_
79