2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "SpaceToBatchNDLayer.h"
19 #include "OperationUtils.h"
21 #include <cker/operation/SpaceToBatchND.h>
31 SpaceToBatchNDLayer::SpaceToBatchNDLayer()
32 : _input(nullptr), _block_shape(nullptr), _padding(nullptr), _output(nullptr)
37 // TO DO : move into shape inferer
38 void SpaceToBatchNDLayer::checkDimension()
40 const int kSpatialDimensionNum = 2;
41 if (_block_shape->getShape().dim(0) != kSpatialDimensionNum)
43 throw std::runtime_error("SpaceToBatchND : block_shape(block_size) tensor's rank is wrong\n");
46 // Ensures the input height and width (with padding) is a multiple of block
47 // shape height and width.
48 for (int dim = 0; dim < kSpatialDimensionNum; ++dim)
50 int final_dim_size = (_input->getShape().dim(dim + 1) + getBuffer<int32_t>(_padding)[dim * 2] +
51 getBuffer<int32_t>(_padding)[dim * 2 + 1]);
53 if (final_dim_size % getBuffer<int32_t>(_block_shape)[dim] != 0)
55 throw std::runtime_error(
56 "SpaceToBatchND : padded input's dimension is not a multiple of block size\n");
59 if ((int32_t)_output->getShape().dim(dim + 1) !=
60 final_dim_size / getBuffer<int32_t>(_block_shape)[dim])
62 throw std::runtime_error("SpaceToBatchND : wrong output dimension\n");
67 template <> uint32_t SpaceToBatchNDLayer::getPad<float>() { return 0; }
68 template <> uint32_t SpaceToBatchNDLayer::getPad<uint8_t>() { return _output->data_zero_point(); }
70 template <typename T> void SpaceToBatchNDLayer::spaceToBatchND()
74 nnfw::cker::SpaceToBatchParams params;
75 params.output_offset = getPad<T>();
77 nnfw::cker::SpaceToBatchND(params, getShape(_input), getBuffer<T>(_input), getShape(_block_shape),
78 getBuffer<int32_t>(_block_shape), getShape(_padding),
79 getBuffer<int32_t>(_padding), getShape(_output),
80 getBuffer<T>(_output));
83 void SpaceToBatchNDLayer::configure(const IPortableTensor *input,
84 const IPortableTensor *block_shape,
85 const IPortableTensor *padding, IPortableTensor *output)
88 _block_shape = block_shape;
93 void SpaceToBatchNDLayer::run()
95 if (_input->data_type() == OperandType::FLOAT32)
97 spaceToBatchND<float>();
99 else if (_input->data_type() == OperandType::QUANT_UINT8_ASYMM)
101 spaceToBatchND<uint8_t>();
105 throw std::runtime_error{"SpaceToBatchND: unsupported data type"};
111 } // namespace backend