Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / DepthToSpace.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 "DepthToSpace.h"
18 #include "Utils.h"
19 #include <tensorflow/lite/kernels/internal/optimized/optimized_ops.h>
20
21 namespace luci_interpreter
22 {
23 namespace kernels
24 {
25
26 DepthToSpace::DepthToSpace(const Tensor *input, Tensor *output, const DepthToSpaceParams &params)
27     : KernelWithParams<DepthToSpaceParams>({input}, {output}, params)
28 {
29 }
30
31 void DepthToSpace::configure()
32 {
33   if (input()->shape().num_dims() != 4)
34   {
35     throw std::runtime_error("Invalid input num_dims.");
36   }
37   if (output()->element_type() != DataType::FLOAT32 && output()->element_type() != DataType::U8 &&
38       output()->element_type() != DataType::S8 && output()->element_type() != DataType::S32 &&
39       output()->element_type() != DataType::S64)
40   {
41     throw std::runtime_error("Invalid output type");
42   }
43   if (input()->element_type() != output()->element_type())
44   {
45     throw std::runtime_error("Type mismatch on input and output.");
46   }
47   const int block_size = params().block_size;
48   const int32_t input_height = input()->shape().dim(1);
49   const int32_t input_width = input()->shape().dim(2);
50   const int32_t input_channels = input()->shape().dim(3);
51   int32_t output_height = input_height * block_size;
52   int32_t output_width = input_width * block_size;
53   int32_t output_channels = input_channels / block_size / block_size;
54
55   assert(input_height == output_height / block_size);
56   assert(input_width == output_width / block_size);
57   assert(input_channels == output_channels * block_size * block_size);
58
59   Shape output_shape(4);
60   output_shape.dim(0) = input()->shape().dim(0);
61   output_shape.dim(1) = output_height;
62   output_shape.dim(2) = output_width;
63   output_shape.dim(3) = output_channels;
64
65   output()->resize(output_shape);
66 }
67
68 void DepthToSpace::execute() const
69 {
70   tflite::DepthToSpaceParams op_params;
71   op_params.block_size = params().block_size;
72   switch (input()->element_type())
73   {
74     case DataType::FLOAT32:
75       tflite::optimized_ops::DepthToSpace(op_params, getTensorShape(input()),
76                                           getTensorData<float>(input()), getTensorShape(output()),
77                                           getTensorData<float>(output()));
78       break;
79     case DataType::U8:
80       tflite::optimized_ops::DepthToSpace(op_params, getTensorShape(input()),
81                                           getTensorData<uint8_t>(input()), getTensorShape(output()),
82                                           getTensorData<uint8_t>(output()));
83       break;
84     default:
85       throw std::runtime_error("Unsupported Type.");
86   }
87 }
88
89 } // namespace kernels
90 } // namespace luci_interpreter