Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / ops / 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 ops
30 {
31
32 PackLayer::PackLayer() : _inputs(), _output(nullptr), _axis(0)
33 {
34   // DO NOTHING
35 }
36
37 template <typename T> void PackLayer::packImpl()
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(getShape(_inputs[i]));
52     inputDimsPtr.push_back(&inputDims[i]);
53   }
54
55   std::vector<const T *> inputPtrs;
56
57   for (const auto input : _inputs)
58   {
59     inputPtrs.emplace_back(getBuffer<T>(input));
60   }
61
62   nnfw::cker::Pack<T>(op_params, inputPtrs.data(), getShape(_output), getBuffer<T>(_output));
63 }
64
65 void PackLayer::configure(const std::vector<const IPortableTensor *> &inputs, int32_t axis,
66                           IPortableTensor *output)
67 {
68   assert(inputs.size() > 0);
69   assert(output != nullptr);
70
71   _inputs = inputs;
72   _axis = axis;
73   _output = output;
74 }
75
76 void PackLayer::run()
77 {
78   if (_output->data_type() == OperandType::FLOAT32)
79   {
80     packImpl<float>();
81   }
82   else if (_output->data_type() == OperandType::INT32)
83   {
84     packImpl<int32_t>();
85   }
86   else
87   {
88     throw std::runtime_error{"Pack: unsupported data type"};
89   }
90 }
91
92 } // namespace ops
93 } // namespace cpu
94 } // namespace backend
95 } // namespace onert