ONE - On-device Neural Engine
Loading...
Searching...
No Matches
BatchToSpaceND.cpp
Go to the documentation of this file.
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
19#include "kernels/Utils.h"
20
21#include "PALBatchToSpaceND.h"
22
23#include <stdexcept>
24
25namespace luci_interpreter
26{
27
28namespace kernels
29{
30
31namespace
32{
33const int kInputMinDimensionNum = 3;
34const int kInputMaxDimensionNum = 4;
35} // namespace
36
37BatchToSpaceND::BatchToSpaceND(const Tensor *input, const Tensor *block_shape, const Tensor *crops,
38 Tensor *output)
39 : Kernel({input, block_shape, crops}, {output})
40{
41}
42
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);
79}
80
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("luci-intp BatchToSpaceND Unsupported type.");
100 }
101}
102
103} // namespace kernels
104} // namespace luci_interpreter
int32_t dim(int i) const
Definition Tensor.h:41
int num_dims() const
Definition Tensor.h:39
void resize(const Shape &new_shape)
Definition Tensor.cpp:56
const Shape & shape() const
Definition Tensor.h:107
const T * data() const
Definition Tensor.h:127
BatchToSpaceND(const Tensor *input, const Tensor *block_shape, const Tensor *crops, Tensor *output)
#define LUCI_INTERPRETER_CHECK(cond)
Definition Utils.h:36
const luci_interpreter::RuntimeShape output_shape
tflite::RuntimeShape getTensorShape(const Tensor *tensor)
Definition Utils.h:194