d26ed7378d34be8812b0b18d8a575458b5548b83
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / ops / ConcatLayer.cc
1 /*
2  * Copyright (c) 2018 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 "ConcatLayer.h"
18
19 #include "OperationUtils.h"
20
21 #include <cker/operation/Concatenation.h>
22
23 namespace onert
24 {
25 namespace backend
26 {
27 namespace cpu
28 {
29 namespace ops
30 {
31
32 ConcatLayer::ConcatLayer() : _inputs(), _output(nullptr), _axis(0)
33 {
34   // DO NOTHING
35 }
36
37 template <typename T> void ConcatLayer::concatenationGeneral()
38 {
39   uint32_t num_inputs = _inputs.size();
40
41   nnfw::cker::ConcatenationParams op_params;
42   op_params.axis = _axis;
43   op_params.inputs_count = num_inputs;
44
45   std::vector<nnfw::cker::Shape *> inputDimsPtr;
46   std::vector<nnfw::cker::Shape> inputDims;
47   inputDimsPtr.reserve(num_inputs);
48   inputDims.reserve(num_inputs);
49
50   for (uint32_t i = 0; i < num_inputs; i++)
51   {
52     inputDims.push_back(getTensorShape(_inputs[i]));
53     inputDimsPtr.push_back(&inputDims[i]);
54   }
55
56   std::vector<const T *> inputDataPtrs;
57
58   for (const auto input : _inputs)
59   {
60     inputDataPtrs.emplace_back(reinterpret_cast<const T *>(input->buffer()));
61   }
62
63   nnfw::cker::Concatenation<T>(op_params, inputDimsPtr.data(), inputDataPtrs.data(),
64                                getTensorShape(_output), reinterpret_cast<T *>(_output->buffer()));
65 }
66 void ConcatLayer::concatenationQuant8()
67 {
68   uint32_t num_inputs = _inputs.size();
69
70   std::vector<int32_t> input_zeropoints(num_inputs);
71   std::vector<float> input_scales(num_inputs);
72   for (uint32_t i = 0; i < num_inputs; i++)
73   {
74     input_zeropoints[i] = _inputs[i]->data_offset();
75     input_scales[i] = _inputs[i]->data_scale();
76   }
77
78   nnfw::cker::ConcatenationParams op_params;
79   op_params.axis = _axis;
80   op_params.inputs_count = num_inputs;
81   op_params.input_zeropoint = input_zeropoints.data();
82   op_params.input_scale = input_scales.data();
83   op_params.output_zeropoint = _output->data_offset();
84   op_params.output_scale = _output->data_scale();
85
86   std::vector<nnfw::cker::Shape *> inputDimsPtr;
87   std::vector<nnfw::cker::Shape> inputDims;
88   inputDimsPtr.reserve(num_inputs);
89   inputDims.reserve(num_inputs);
90   for (uint32_t i = 0; i < num_inputs; i++)
91   {
92     inputDims.push_back(getTensorShape(_inputs[i]));
93     inputDimsPtr.push_back(&inputDims[i]);
94   }
95
96   std::vector<const uint8_t *> inputDataPtrs;
97   for (const auto input : _inputs)
98   {
99     inputDataPtrs.emplace_back(reinterpret_cast<const uint8_t *>(input->buffer()));
100   }
101
102   nnfw::cker::ConcatenationWithScaling(op_params, inputDimsPtr.data(), inputDataPtrs.data(),
103                                        getTensorShape(_output),
104                                        reinterpret_cast<uint8_t *>(_output->buffer()));
105 }
106
107 void ConcatLayer::configure(const std::vector<const IPortableTensor *> &inputs, int32_t axis,
108                             IPortableTensor *output)
109 {
110   assert(inputs.size() > 0);
111   assert(output != nullptr);
112
113   _inputs = inputs;
114   _axis = axis;
115   _output = output;
116 }
117
118 void ConcatLayer::run()
119 {
120   if (_output->data_type() == OperandType::FLOAT32)
121   {
122     concatenationGeneral<float>();
123   }
124   else if (_output->data_type() == OperandType::QUANT_UINT8_ASYMM)
125   {
126     concatenationQuant8();
127   }
128   else if (_output->data_type() == OperandType::INT32)
129   {
130     concatenationGeneral<int32_t>();
131   }
132   else if (_output->data_type() == OperandType::INT64)
133   {
134     concatenationGeneral<int64_t>();
135   }
136   else
137     throw std::runtime_error("Concat: unsupported data type");
138 }
139
140 } // namespace ops
141 } // namespace cpu
142 } // namespace backend
143 } // namespace onert