Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / Concatenation.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2019 The TensorFlow Authors. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *    http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "kernels/Concatenation.h"
19 #include "kernels/Utils.h"
20
21 #include <tensorflow/lite/kernels/internal/reference/concatenation.h>
22
23 #include <stdexcept>
24
25 namespace luci_interpreter
26 {
27 namespace kernels
28 {
29
30 Concatenation::Concatenation(std::vector<const Tensor *> inputs, Tensor *output,
31                              const ConcatenationParams &params)
32   : KernelWithParams<ConcatenationParams>(std::move(inputs), {output}, params)
33 {
34 }
35
36 void Concatenation::configure()
37 {
38   const int num_inputs = _inputs.size();
39   LUCI_INTERPRETER_CHECK(num_inputs > 0);
40   const Tensor *t0 = _inputs[0];
41
42   // TODO: Support concat with fused activation function
43   LUCI_INTERPRETER_CHECK(params().activation == luci::FusedActFunc::NONE);
44
45   int axis = _params.axis;
46   if (axis < 0)
47     axis += t0->shape().num_dims();
48   LUCI_INTERPRETER_CHECK(axis >= 0 && axis < t0->shape().num_dims());
49
50   int32_t sum_axis = t0->shape().dim(axis);
51   for (int i = 1; i < num_inputs; ++i)
52   {
53     const Tensor *tensor = _inputs[i];
54     LUCI_INTERPRETER_CHECK(tensor->element_type() == t0->element_type());
55     LUCI_INTERPRETER_CHECK(tensor->shape().num_dims() == t0->shape().num_dims());
56     for (int d = 0; d < t0->shape().num_dims(); ++d)
57     {
58       if (d == axis)
59       {
60         sum_axis += tensor->shape().dim(axis);
61       }
62       else
63       {
64         LUCI_INTERPRETER_CHECK(tensor->shape().dim(d) == t0->shape().dim(d));
65       }
66     }
67   }
68
69   Shape output_shape = t0->shape();
70   output_shape.dim(axis) = sum_axis;
71
72   // TODO S8 type needs more checking: quantization parameters of all input tensors and the output
73   //  tensor should be the same. Note that there is no such requirement for U8 type.
74   if (t0->element_type() == DataType::S8)
75     throw std::runtime_error("Unsupported type.");
76
77   output()->resize(output_shape);
78 }
79
80 void Concatenation::execute() const
81 {
82   switch (_inputs[0]->element_type())
83   {
84     case DataType::FLOAT32:
85       evalGeneric<float>();
86       break;
87     case DataType::U8:
88       evalQuantized();
89       break;
90     case DataType::S8:
91       evalGeneric<int8_t>();
92       break;
93     case DataType::S32:
94       evalGeneric<int32_t>();
95       break;
96     case DataType::S64:
97       evalGeneric<int64_t>();
98       break;
99     default:
100       throw std::runtime_error("Unsupported type.");
101   }
102 }
103
104 template <typename T> void Concatenation::evalGeneric() const
105 {
106   int axis = _params.axis;
107   if (axis < 0)
108     axis += output()->shape().num_dims();
109
110   VectorOfTensors<T, true> inputs(_inputs);
111   tflite::ConcatenationParams params{};
112   params.axis = axis;
113   params.inputs_count = _inputs.size();
114   tflite::reference_ops::Concatenation(params, inputs.shapes(), inputs.data(),
115                                        getTensorShape(output()), getTensorData<T>(output()));
116 }
117
118 void Concatenation::evalQuantized() const
119 {
120   int axis = _params.axis;
121   if (axis < 0)
122     axis += output()->shape().num_dims();
123
124   VectorOfQuantizedTensors<true> inputs(_inputs);
125   tflite::ConcatenationParams params{};
126   params.axis = axis;
127   params.input_zeropoint = inputs.zero_point();
128   params.input_scale = inputs.scale();
129   params.inputs_count = _inputs.size();
130   params.output_zeropoint = output()->zero_point();
131   params.output_scale = output()->scale();
132
133   tflite::reference_ops::ConcatenationWithScaling(params, inputs.shapes(), inputs.data(),
134                                                   getTensorShape(output()),
135                                                   getTensorData<uint8_t>(output()));
136 }
137
138 } // namespace kernels
139 } // namespace luci_interpreter