Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / SpaceToDepth.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "SpaceToDepth.h"
18 #include "Utils.h"
19 #include "PALSpaceToDepth.h"
20
21 namespace luci_interpreter
22 {
23 namespace kernels
24 {
25
26 SpaceToDepth::SpaceToDepth(const Tensor *input, Tensor *output, const SpaceToDepthParams &params)
27   : KernelWithParams<SpaceToDepthParams>({input}, {output}, params)
28 {
29 }
30
31 void SpaceToDepth::configure()
32 {
33   assert(input()->shape().num_dims() == 4);
34   assert(output()->element_type() == DataType::FLOAT32 ||
35          output()->element_type() == DataType::U8 || output()->element_type() == DataType::S8 ||
36          output()->element_type() == DataType::S32 || output()->element_type() == DataType::S64);
37   assert(input()->element_type() == output()->element_type());
38
39   const int block_size = params().block_size;
40   const int32_t input_height = input()->shape().dim(1);
41   const int32_t input_width = input()->shape().dim(2);
42   int32_t output_height = input_height / block_size;
43   int32_t output_width = input_width / block_size;
44
45   assert(input_height == output_height * block_size);
46   assert(input_width == output_width * block_size);
47
48   Shape output_shape(4);
49   output_shape.dim(0) = input()->shape().dim(0);
50   output_shape.dim(1) = output_height;
51   output_shape.dim(2) = output_width;
52   output_shape.dim(3) = input()->shape().dim(3) * block_size * block_size;
53
54   output()->resize(output_shape);
55 }
56
57 void SpaceToDepth::execute() const
58 {
59   tflite::SpaceToDepthParams op_params{};
60   op_params.block_size = params().block_size;
61   switch (input()->element_type())
62   {
63     case DataType::FLOAT32:
64       luci_interpreter_pal::SpaceToDepth(op_params, getTensorShape(input()),
65                                          getTensorData<float>(input()), getTensorShape(output()),
66                                          getTensorData<float>(output()));
67       break;
68     case DataType::U8:
69       luci_interpreter_pal::SpaceToDepth(op_params, getTensorShape(input()),
70                                          getTensorData<uint8_t>(input()), getTensorShape(output()),
71                                          getTensorData<uint8_t>(output()));
72       break;
73     default:
74       throw std::runtime_error("Unsupported type.");
75   }
76 }
77
78 } // namespace kernels
79 } // namespace luci_interpreter