Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / ops / UnpackLayer.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 "UnpackLayer.h"
18
19 #include "OperationUtils.h"
20
21 #include <cker/operation/Unpack.h>
22
23 namespace onert
24 {
25 namespace backend
26 {
27 namespace cpu
28 {
29 namespace ops
30 {
31
32 UnpackLayer::UnpackLayer() : _input(nullptr), _outputs(), _axis(0), _num_output(0)
33 {
34   // DO NOTHING
35 }
36
37 template <typename T> void UnpackLayer::unpackImpl()
38 {
39   nnfw::cker::UnpackParams op_params;
40   op_params.axis = _axis;
41   op_params.num_split = _num_output;
42
43   std::vector<nnfw::cker::Shape *> outputDimsPtr;
44   std::vector<nnfw::cker::Shape> outputDims;
45   outputDimsPtr.reserve(_num_output);
46   outputDims.reserve(_num_output);
47
48   for (int32_t i = 0; i < _num_output; i++)
49   {
50     outputDims.push_back(getTensorShape(_outputs[i]));
51     outputDimsPtr.push_back(&outputDims[i]);
52   }
53
54   std::vector<T *> outputPtrs;
55
56   for (const auto output : _outputs)
57   {
58     outputPtrs.emplace_back(reinterpret_cast<T *>(output->buffer()));
59   }
60
61   nnfw::cker::Unpack<T>(op_params, getTensorShape(_input), reinterpret_cast<T *>(_input->buffer()),
62                         getTensorShape(_outputs[0]), outputPtrs.data());
63 }
64
65 void UnpackLayer::configure(const IPortableTensor *input, uint32_t axis, int32_t num,
66                             std::vector<IPortableTensor *> &outputs)
67 {
68   assert(input != nullptr);
69   assert(outputs.size() > 0);
70   assert(outputs.size() == (size_t)num);
71
72   _input = input;
73   _axis = axis;
74   _num_output = num;
75   _outputs = outputs;
76 }
77
78 void UnpackLayer::run()
79 {
80   if (_input->data_type() == OperandType::FLOAT32)
81     unpackImpl<float>();
82   else if (_input->data_type() == OperandType::INT32)
83     unpackImpl<int32_t>();
84   else
85     throw std::runtime_error{"Unpack: Unsupported data type"};
86 }
87
88 } // namespace ops
89 } // namespace cpu
90 } // namespace backend
91 } // namespace onert