3fab4028a9070143bf107bb12c282a331cd62bab
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / SpaceToDepth.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2019 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 #include "SpaceToDepth.h"
19 #include "Utils.h"
20 #include "PALSpaceToDepth.h"
21
22 namespace luci_interpreter
23 {
24 namespace kernels
25 {
26
27 SpaceToDepth::SpaceToDepth(const Tensor *input, Tensor *output, const SpaceToDepthParams &params)
28   : KernelWithParams<SpaceToDepthParams>({input}, {output}, params)
29 {
30 }
31
32 void SpaceToDepth::configure()
33 {
34   assert(input()->shape().num_dims() == 4);
35   assert(output()->element_type() == DataType::FLOAT32 ||
36          output()->element_type() == DataType::U8 || output()->element_type() == DataType::S8 ||
37          output()->element_type() == DataType::S32 || output()->element_type() == DataType::S64);
38   assert(input()->element_type() == output()->element_type());
39
40   const int block_size = params().block_size;
41   const int32_t input_height = input()->shape().dim(1);
42   const int32_t input_width = input()->shape().dim(2);
43   int32_t output_height = input_height / block_size;
44   int32_t output_width = input_width / block_size;
45
46   assert(input_height == output_height * block_size);
47   assert(input_width == output_width * block_size);
48
49   Shape output_shape(4);
50   output_shape.dim(0) = input()->shape().dim(0);
51   output_shape.dim(1) = output_height;
52   output_shape.dim(2) = output_width;
53   output_shape.dim(3) = input()->shape().dim(3) * block_size * block_size;
54   // TODO: enable it only if kernel with dynamic shapes
55   output()->resize(output_shape);
56 }
57
58 void SpaceToDepth::execute() const
59 {
60   tflite::SpaceToDepthParams op_params{};
61   op_params.block_size = params().block_size;
62   switch (input()->element_type())
63   {
64     case DataType::FLOAT32:
65       luci_interpreter_pal::SpaceToDepth(op_params, getTensorShape(input()),
66                                          getTensorData<float>(input()), getTensorShape(output()),
67                                          getTensorData<float>(output()));
68       break;
69     case DataType::U8:
70       luci_interpreter_pal::SpaceToDepth(op_params, getTensorShape(input()),
71                                          getTensorData<uint8_t>(input()), getTensorShape(output()),
72                                          getTensorData<uint8_t>(output()));
73       break;
74     default:
75       assert(false && "Unsupported type.");
76   }
77 }
78
79 } // namespace kernels
80 } // namespace luci_interpreter