Imported Upstream version 1.4.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / kernel / PackLayer.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 "PackLayer.h"
18
19 #include "OperationUtils.h"
20
21 #include <cker/operation/Pack.h>
22
23 namespace onert
24 {
25 namespace backend
26 {
27 namespace cpu
28 {
29 namespace kernel
30 {
31
32 PackLayer::PackLayer() : _inputs(), _output(nullptr), _axis(0)
33 {
34   // DO NOTHING
35 }
36
37 void PackLayer::packFloat32()
38 {
39   uint32_t num_inputs = _inputs.size();
40   nnfw::cker::PackParams op_params;
41   op_params.axis = _axis;
42   op_params.inputs_count = num_inputs;
43
44   std::vector<nnfw::cker::Shape *> inputDimsPtr;
45   std::vector<nnfw::cker::Shape> inputDims;
46   inputDimsPtr.reserve(num_inputs);
47   inputDims.reserve(num_inputs);
48
49   for (uint32_t i = 0; i < num_inputs; i++)
50   {
51     inputDims.push_back(convertTensorToCkerShape(_inputs[i]));
52     inputDimsPtr.push_back(&inputDims[i]);
53   }
54
55   std::vector<const float *> inputFloatPtrs;
56
57   for (const auto input : _inputs)
58   {
59     inputFloatPtrs.emplace_back(reinterpret_cast<const float *>(input->buffer()));
60   }
61
62   nnfw::cker::Pack<float>(op_params, inputFloatPtrs.data(), convertTensorToCkerShape(_output),
63                           reinterpret_cast<float *>(_output->buffer()));
64 }
65
66 void PackLayer::packQuant8()
67 {
68   // cker quant8 pack is not implemented yet
69   throw std::runtime_error{"NYI"};
70 }
71
72 void PackLayer::configure(const std::vector<const operand::Tensor *> &inputs, int32_t axis,
73                           operand::Tensor *output)
74 {
75   assert(inputs.size() > 0);
76   assert(output != nullptr);
77
78   _inputs = inputs;
79   _axis = axis;
80   _output = output;
81 }
82
83 void PackLayer::run()
84 {
85   if (_output->data_type() == OperandType::FLOAT32)
86   {
87     packFloat32();
88   }
89   else if (_output->data_type() == OperandType::QUANT8_ASYMM)
90   {
91     packQuant8();
92   }
93 }
94
95 } // namespace kernel
96 } // namespace cpu
97 } // namespace backend
98 } // namespace onert