Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / compute / cker / include / cker / operation / DepthToSpace.h
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #ifndef __NNFW_CKER_DEPTH_TO_SPACE_H__
19 #define __NNFW_CKER_DEPTH_TO_SPACE_H__
20
21 #include "cker/Shape.h"
22 #include "cker/Types.h"
23
24 namespace nnfw
25 {
26 namespace cker
27 {
28
29 template <typename T>
30 inline void DepthToSpace(const Shape &unextended_input_shape, const T *input_data,
31                          const Shape &unextended_output_shape, T *output_data, int32_t block_size)
32 {
33   assert(unextended_input_shape.DimensionsCount() <= 4);
34   assert(unextended_output_shape.DimensionsCount() <= 4);
35   const Shape input_shape = Shape::ExtendedShape(4, unextended_input_shape);
36   const Shape output_shape = Shape::ExtendedShape(4, unextended_output_shape);
37
38   const int input_depth = input_shape.Dims(3);
39   const int input_width = input_shape.Dims(2);
40   const int input_height = input_shape.Dims(1);
41
42   const int output_depth = output_shape.Dims(3);
43   const int batch_size = output_shape.Dims(0);
44
45   // Number of continuous values that we can copy in one interation.
46   const int stride = block_size * output_depth;
47
48   for (int batch = 0; batch < batch_size; ++batch)
49   {
50     for (int in_h = 0; in_h < input_height; ++in_h)
51     {
52       const T *input_ptr = input_data + Offset(input_shape, batch, in_h, 0, 0);
53       for (int offset_h = 0; offset_h < block_size; ++offset_h)
54       {
55         const T *src = input_ptr;
56         for (int in_w = 0; in_w < input_width; ++in_w)
57         {
58           memcpy(output_data, src, stride * sizeof(T));
59           output_data += stride;
60           src += input_depth;
61         }
62         input_ptr += stride;
63       }
64     }
65   }
66 }
67
68 } // namespace cker
69 } // namespace nnfw
70
71 #endif // __NNFW_CKER_SPACE_TO_DEPTH_H__