2 * Copyright (c) 2021 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2019 The TensorFlow Authors. All Rights Reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "kernels/BatchToSpaceND.h"
19 #include "kernels/Utils.h"
21 #include "PALBatchToSpaceND.h"
25 namespace luci_interpreter
33 const int kInputMinDimensionNum = 3;
34 const int kInputMaxDimensionNum = 4;
37 BatchToSpaceND::BatchToSpaceND(const Tensor *input, const Tensor *block_shape, const Tensor *crops,
39 : Kernel({input, block_shape, crops}, {output})
43 void BatchToSpaceND::configure()
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());
52 int spatial_dims_num = input()->shape().num_dims() - 2;
54 LUCI_INTERPRETER_CHECK(block_shape()->shape().num_dims() == 1);
55 LUCI_INTERPRETER_CHECK(block_shape()->shape().dim(0) == spatial_dims_num);
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)
62 LUCI_INTERPRETER_CHECK(crops_data[i] >= 0);
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)
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];
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);
81 void BatchToSpaceND::execute() const
83 switch (input()->element_type())
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()));
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()));
99 throw std::runtime_error("Unsupported type.");
103 } // namespace kernels
104 } // namespace luci_interpreter