Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / ops / BatchToSpaceNDLayer.cc
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 "BatchToSpaceNDLayer.h"
18
19 #include <cker/operation/BatchToSpaceND.h>
20
21 namespace onert
22 {
23 namespace backend
24 {
25 namespace cpu
26 {
27 namespace ops
28 {
29
30 BatchToSpaceNDLayer::BatchToSpaceNDLayer()
31     : _input(nullptr), _output(nullptr), _block_shape(nullptr), _crops(nullptr)
32 {
33   // DO NOTHING
34 }
35
36 template <typename T> void BatchToSpaceNDLayer::batchToSpaceNDGeneric()
37 {
38   const int32_t NNapiCrops[]{0, 0, 0, 0};
39   const int32_t *_crops_buffer;
40
41   if (_crops == nullptr)
42   {
43     _crops_buffer = NNapiCrops;
44   }
45   else
46   {
47     _crops_buffer = reinterpret_cast<const int32_t *>(_crops->buffer());
48   }
49   nnfw::cker::BatchToSpaceND<T>(
50       getTensorShape(_input), reinterpret_cast<const T *>(_input->buffer()),
51       reinterpret_cast<const int32_t *>(_block_shape->buffer()), _crops_buffer,
52       getTensorShape(_output), reinterpret_cast<T *>(_output->buffer()));
53 }
54
55 void BatchToSpaceNDLayer::configure(const IPortableTensor *input, IPortableTensor *output,
56                                     IPortableTensor *block_shape, IPortableTensor *crops)
57 {
58   _output = output;
59   _input = input;
60   _block_shape = block_shape;
61   _crops = crops;
62 }
63
64 void BatchToSpaceNDLayer::run()
65 {
66   if (_output->data_type() == OperandType::FLOAT32)
67   {
68     batchToSpaceNDGeneric<float>();
69   }
70   else if (_output->data_type() == OperandType::QUANT_UINT8_ASYMM)
71   {
72     batchToSpaceNDGeneric<uint8_t>();
73   }
74   else
75   {
76     throw std::runtime_error{"NYI"};
77   }
78 }
79
80 } // namespace ops
81 } // namespace cpu
82 } // namespace backend
83 } // namespace onert