Imported Upstream version 1.4.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / kernel / 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 kernel
30 {
31
32 UnpackLayer::UnpackLayer() : _input(nullptr), _outputs(), _axis(0), _num_output(0)
33 {
34   // DO NOTHING
35 }
36
37 void UnpackLayer::unpackFloat32()
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(convertTensorToCkerShape(_outputs[i]));
51     outputDimsPtr.push_back(&outputDims[i]);
52   }
53
54   std::vector<float *> outputFloatPtrs;
55
56   for (const auto output : _outputs)
57   {
58     outputFloatPtrs.emplace_back(reinterpret_cast<float *>(output->buffer()));
59   }
60
61   nnfw::cker::Unpack<float>(op_params, convertTensorToCkerShape(_input),
62                             reinterpret_cast<float *>(_input->buffer()),
63                             convertTensorToCkerShape(_outputs[0]), outputFloatPtrs.data());
64 }
65
66 void UnpackLayer::unpackQuant8()
67 {
68   // cker quant8 pack is not implemented yet
69   throw std::runtime_error{"Unpack: NYI quant8 type"};
70 }
71
72 void UnpackLayer::configure(const operand::Tensor *input, uint32_t axis, int32_t num,
73                             std::vector<operand::Tensor *> &outputs)
74 {
75   assert(input != nullptr);
76   assert(outputs.size() > 0);
77   assert(outputs.size() == (size_t)num);
78
79   _input = input;
80   _axis = axis;
81   _num_output = num;
82   _outputs = outputs;
83 }
84
85 void UnpackLayer::run()
86 {
87   if (_input->data_type() == OperandType::FLOAT32)
88   {
89     unpackFloat32();
90   }
91   else if (_input->data_type() == OperandType::QUANT8_ASYMM)
92   {
93     unpackQuant8();
94   }
95   else
96   {
97     throw std::runtime_error{"Unpack: Unsupported input type"};
98   }
99 }
100
101 } // namespace kernel
102 } // namespace cpu
103 } // namespace backend
104 } // namespace onert