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_DEPTH_TO_SPACE_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEPTH_TO_SPACE_H_
17 
18 #include "tensorflow/lite/kernels/internal/types.h"
19 
20 namespace tflite {
21 namespace reference_ops {
22 
23 template <typename T>
DepthToSpace(const tflite::DepthToSpaceParams & op_params,const RuntimeShape & unextended_input_shape,const T * input_data,const RuntimeShape & unextended_output_shape,T * output_data)24 inline void DepthToSpace(const tflite::DepthToSpaceParams& 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 * block_size, output_width);
49   TFLITE_DCHECK_EQ(input_height * block_size, output_height);
50   TFLITE_DCHECK_EQ(input_depth, output_depth * block_size * block_size);
51   TFLITE_DCHECK_EQ(input_batch, output_batch);
52 
53   for (int out_b = 0; out_b < output_batch; ++out_b) {
54     for (int out_h = 0; out_h < output_height; ++out_h) {
55       for (int out_w = 0; out_w < output_width; ++out_w) {
56         for (int out_d = 0; out_d < output_depth; ++out_d) {
57           const int in_d =
58               out_d + ((out_h % block_size) * block_size + out_w % block_size) *
59                           output_depth;
60 
61           const int in_w = out_w / block_size;
62           const int in_h = out_h / block_size;
63           const int in_b = out_b;
64 
65           const int input_index = Offset(input_shape, in_b, in_h, in_w, in_d);
66           const int output_index =
67               Offset(output_shape, out_b, out_h, out_w, out_d);
68 
69           output_data[output_index] = input_data[input_index];
70         }
71       }
72     }
73   }
74 }
75 
76 }  // namespace reference_ops
77 }  // namespace tflite
78 
79 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEPTH_TO_SPACE_H_
80