Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / BatchToSpaceND.cpp
1 /*
2  * Copyright (c) 2021 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 "kernels/BatchToSpaceND.h"
19 #include "kernels/Utils.h"
20
21 #include "PALBatchToSpaceND.h"
22
23 #include <stdexcept>
24
25 namespace luci_interpreter
26 {
27
28 namespace kernels
29 {
30
31 namespace
32 {
33 const int kInputMinDimensionNum = 3;
34 const int kInputMaxDimensionNum = 4;
35 } // namespace
36
37 BatchToSpaceND::BatchToSpaceND(const Tensor *input, const Tensor *block_shape, const Tensor *crops,
38                                Tensor *output)
39   : Kernel({input, block_shape, crops}, {output})
40 {
41 }
42
43 void BatchToSpaceND::configure()
44 {
45
46   const auto *block_shape_data = block_shape()->data<int32_t>();
47   const auto *crops_data = crops()->data<int32_t>();
48   LUCI_INTERPRETER_CHECK(input()->shape().num_dims() >= kInputMinDimensionNum);
49   LUCI_INTERPRETER_CHECK(input()->shape().num_dims() <= kInputMaxDimensionNum);
50   LUCI_INTERPRETER_CHECK(input()->element_type() == output()->element_type());
51
52   int spatial_dims_num = input()->shape().num_dims() - 2;
53
54   LUCI_INTERPRETER_CHECK(block_shape()->shape().num_dims() == 1);
55   LUCI_INTERPRETER_CHECK(block_shape()->shape().dim(0) == spatial_dims_num);
56
57   LUCI_INTERPRETER_CHECK(crops()->shape().num_dims() == 2);
58   LUCI_INTERPRETER_CHECK(crops()->shape().dim(0) == spatial_dims_num);
59   LUCI_INTERPRETER_CHECK(crops()->shape().dim(1) == 2);
60   for (int i = 0; i < spatial_dims_num * 2; ++i)
61   {
62     LUCI_INTERPRETER_CHECK(crops_data[i] >= 0);
63   }
64
65   Shape output_shape = Shape(input()->shape().num_dims());
66   int output_batch_size = input()->shape().dim(0);
67   for (int i = 0; i < spatial_dims_num; ++i)
68   {
69     LUCI_INTERPRETER_CHECK(output_batch_size % block_shape_data[i] == 0);
70     output_batch_size = output_batch_size / block_shape_data[i];
71     output_shape.dim(i + 1) =
72       input()->shape().dim(i + 1) * block_shape_data[i] - crops_data[i * 2] - crops_data[i * 2 + 1];
73   }
74
75   output_shape.dim(0) = output_batch_size;
76   output_shape.dim(input()->shape().num_dims() - 1) =
77     input()->shape().dim(input()->shape().num_dims() - 1);
78   output()->resize(output_shape);
79 }
80
81 void BatchToSpaceND::execute() const
82 {
83   switch (input()->element_type())
84   {
85     case DataType::FLOAT32:
86       luci_interpreter_pal::BatchToSpaceND(
87         getTensorShape(input()), getTensorData<float>(input()), getTensorShape(block_shape()),
88         getTensorData<int32_t>(block_shape()), getTensorShape(crops()),
89         getTensorData<int32_t>(crops()), getTensorShape(output()), getTensorData<float>(output()));
90       break;
91     case DataType::U8:
92       luci_interpreter_pal::BatchToSpaceND(
93         getTensorShape(input()), getTensorData<uint8_t>(input()), getTensorShape(block_shape()),
94         getTensorData<int32_t>(block_shape()), getTensorShape(crops()),
95         getTensorData<int32_t>(crops()), getTensorShape(output()),
96         getTensorData<uint8_t>(output()));
97       break;
98     default:
99       throw std::runtime_error("Unsupported type.");
100   }
101 }
102
103 } // namespace kernels
104 } // namespace luci_interpreter