8a77459ea542240137b27e7896465322e67e99c8
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / loader / nodes / SVDF.cpp
1 /*
2  * Copyright (c) 2022 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 "Builders.h"
18
19 #include "kernels/SVDF.h"
20
21 namespace luci_interpreter
22 {
23
24 std::unique_ptr<Kernel> build_kernel_CircleSVDF(std::vector<const Tensor *> &&inputs,
25                                                 std::vector<Tensor *> &&outputs,
26                                                 const uint32_t op_index, KernelBuilder &builder)
27 {
28   assert(inputs.size() == 5);
29
30   const Tensor *input = inputs.at(0);
31   const Tensor *feature = inputs.at(1);
32   const Tensor *time = inputs.at(2);
33   const Tensor *bias = inputs.at(3);
34   const Tensor *input_activation_state = inputs.at(4);
35   Tensor *output = outputs.at(0);
36
37   auto scratchpad_tensor =
38     std::make_unique<Tensor>(input_activation_state->element_type(), Shape({}), nullptr);
39   scratchpad_tensor->set_data_buffer(nullptr);
40   Tensor *tmp = builder.get_runtime_graph()->addTensor(std::move(scratchpad_tensor));
41
42   DataType data_type = input->element_type() == DataType::S8 ? DataType::S32 : DataType::FLOAT32;
43
44   scratchpad_tensor = std::make_unique<Tensor>(data_type, Shape({}), nullptr);
45   scratchpad_tensor->set_data_buffer(nullptr);
46   Tensor *tmp_1 = builder.get_runtime_graph()->addTensor(std::move(scratchpad_tensor));
47
48   if (data_type == DataType::FLOAT32 &&
49       (feature->element_type() == DataType::S8 || feature->element_type() == DataType::U8))
50   {
51     data_type = feature->element_type();
52   }
53
54   scratchpad_tensor = std::make_unique<Tensor>(data_type, Shape({}), nullptr);
55   scratchpad_tensor->set_data_buffer(nullptr);
56   Tensor *tmp_2 = builder.get_runtime_graph()->addTensor(std::move(scratchpad_tensor));
57
58   data_type = DataType::FLOAT32;
59
60   scratchpad_tensor = std::make_unique<Tensor>(data_type, Shape({}), nullptr);
61   scratchpad_tensor->set_data_buffer(nullptr);
62   Tensor *tmp_3 = builder.get_runtime_graph()->addTensor(std::move(scratchpad_tensor));
63
64   scratchpad_tensor = std::make_unique<Tensor>(data_type, Shape({}), nullptr);
65   scratchpad_tensor->set_data_buffer(nullptr);
66   Tensor *tmp_4 = builder.get_runtime_graph()->addTensor(std::move(scratchpad_tensor));
67
68   scratchpad_tensor = std::make_unique<Tensor>(data_type, Shape({}), nullptr);
69   scratchpad_tensor->set_data_buffer(nullptr);
70   Tensor *tmp_5 = builder.get_runtime_graph()->addTensor(std::move(scratchpad_tensor));
71
72   scratchpad_tensor = std::make_unique<Tensor>(data_type, Shape({}), nullptr);
73   scratchpad_tensor->set_data_buffer(nullptr);
74   Tensor *tmp_6 = builder.get_runtime_graph()->addTensor(std::move(scratchpad_tensor));
75
76   circle::OperatorT oper_t;
77   builder.get_circle_reader()->operators()[op_index]->UnPackTo(&oper_t);
78   const auto *options = oper_t.builtin_options.AsSVDFOptions();
79
80   SVDFParams params{};
81   params.activation = luci_actfunc(options->fused_activation_function);
82   params.svdf_rank = options->rank;
83   params.asymmetric_quantize_inputs = options->asymmetric_quantize_inputs;
84
85   return std::make_unique<kernels::SVDF>(input, feature, time, bias, input_activation_state, output,
86                                          tmp, tmp_1, tmp_2, tmp_3, tmp_4, tmp_5, tmp_6, params);
87 }
88
89 } // namespace luci_interpreter