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