ONE - On-device Neural Engine
Loading...
Searching...
No Matches
GRU.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 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 <core/OMDataType.h>
18#include "OMStatus.h"
19
20#include "core/OMUtils.h"
21#include "core/OMKernelData.h"
23
25#include "execute/OMUtils.h"
27
28#include "PALGRU.h"
29
30using namespace onert_micro;
31using namespace onert_micro::core;
32using namespace onert_micro::execute;
33
34namespace
35{
36
37constexpr uint32_t inputTensorIdx = 0;
38constexpr uint32_t hiddenHiddenTensorIdx = 1;
39constexpr uint32_t hiddenHiddenBiasTensorIdx = 2;
40constexpr uint32_t hiddenInputTensorIdx = 3;
41constexpr uint32_t hiddenInputBiasTensorIdx = 4;
42constexpr uint32_t stateTensorIdx = 5;
43
44constexpr uint32_t outputTensorIdx = 0;
45
46} // namespace
47
48// NOTE: doesnt currently support dynamic shapes
49OMStatus onert_micro::execute::execute_kernel_CircleGRU(const OMExecuteArgs &execute_args)
50{
51 core::OMRuntimeContext &runtime_context = execute_args.runtime_context;
52 core::OMRuntimeStorage &runtime_storage = execute_args.runtime_storage;
53 uint16_t op_index = execute_args.kernel_index;
54
55 const circle::Tensor *input;
56 const circle::Tensor *hidden_hidden;
57 const circle::Tensor *hidden_hidden_bias;
58 const circle::Tensor *hidden_input;
59 const circle::Tensor *hidden_input_bias;
60 const circle::Tensor *state;
61
62 const circle::Tensor *output;
63
64 uint8_t *input_data;
65 uint8_t *hidden_hidden_data;
66 uint8_t *hidden_hidden_bias_data;
67 uint8_t *hidden_input_data;
68 uint8_t *hidden_input_bias_data;
69 uint8_t *state_data;
70 uint8_t *output_data;
71
72 uint16_t state_tensor_index = 0;
73
74 // Read kernel
75 {
76 execute::OMRuntimeKernel runtime_kernel;
77 runtime_kernel.readKernel(op_index, runtime_context);
78
79 input = runtime_kernel.inputs[inputTensorIdx];
80 hidden_hidden = runtime_kernel.inputs[hiddenHiddenTensorIdx];
81 hidden_hidden_bias = runtime_kernel.inputs[hiddenHiddenBiasTensorIdx];
82 hidden_input = runtime_kernel.inputs[hiddenInputTensorIdx];
83 hidden_input_bias = runtime_kernel.inputs[hiddenInputBiasTensorIdx];
84 state = runtime_kernel.inputs[stateTensorIdx];
85
86 output = runtime_kernel.outputs[outputTensorIdx];
87 assert(input != nullptr);
88 assert(hidden_hidden != nullptr);
89 assert(hidden_input != nullptr);
90 assert(state != nullptr);
91 // Biases can be nullptr
92 assert(output != nullptr);
93
94 runtime_kernel.getDataFromStorage(op_index, runtime_storage, runtime_context);
95
96 input_data = runtime_kernel.inputs_data[inputTensorIdx];
97 hidden_hidden_data = runtime_kernel.inputs_data[hiddenHiddenTensorIdx];
98 hidden_hidden_bias_data = runtime_kernel.inputs_data[hiddenHiddenBiasTensorIdx];
99 hidden_input_data = runtime_kernel.inputs_data[hiddenInputTensorIdx];
100 hidden_input_bias_data = runtime_kernel.inputs_data[hiddenInputBiasTensorIdx];
101 state_data = runtime_kernel.inputs_data[stateTensorIdx];
102
103 output_data = runtime_kernel.outputs_data[outputTensorIdx];
104 assert(input_data != nullptr);
105 assert(hidden_hidden_data != nullptr);
106 assert(hidden_input_data != nullptr);
107 assert(state_data != nullptr);
108 // Bias can be nullptr
109 assert(output_data != nullptr);
110
111 state_tensor_index = runtime_kernel.inputs_index[stateTensorIdx];
112 }
113
114 OMStatus status;
115
116 uint8_t *output_hidden_data;
117 uint8_t *output_input_data;
118
119 status =
121 sizeof(core::OMDataType(hidden_hidden->type())),
122 &output_hidden_data);
123 if (status != Ok)
124 return status;
126 core::OMRuntimeShape(hidden_input).flatSize() * sizeof(core::OMDataType(hidden_input->type())),
127 &output_input_data);
128 if (status != Ok)
129 return status;
130
131 // If train mode need to allocate memory for internal intermediate tensors for calculation
132 // gradients further Number of intermediate tensors
133 const int32_t num_of_intermediate_tensors = 9;
134 // Note: size of the intermediate is equal to output size (should be checked during import phase)
135 const int32_t size_of_intermediate_tensors = core::OMRuntimeShape(output).flatSize();
136 assert(size_of_intermediate_tensors > 0);
137 if (size_of_intermediate_tensors == 0)
138 return UnknownError;
139
140 const int32_t input_size = core::OMRuntimeShape(input).flatSize();
141 const int32_t output_size = size_of_intermediate_tensors;
142
143 // Allocate buffer with following schema:
144 // times * [output_size * sizeof(data_type),
145 // num_of_intermediate_tensors * size_of_intermediate_tensors * sizeof(data_type)]
146 // Note: need to save all necessary intermediate data to calculate gradients
147 // Deallocation should perform train/GRU kernel
148 const size_t data_type_size = sizeof(core::OMDataType(input->type()));
149 const int32_t time = OMRuntimeShape(input).dims(0);
150 size_t intermediate_buffer_size = 0;
151 uint8_t *intermediate_buffer = nullptr;
152 if (execute_args.is_train_mode)
153 {
154 const auto num_operators = runtime_context.getCircleOperators()->size();
155
156 uint32_t num_train_layers =
157 execute_args.num_train_layers == 0 ? num_operators : execute_args.num_train_layers;
158 uint32_t last_node_pos = std::min(num_operators, num_train_layers);
159 uint32_t last_train_op_index = num_operators - last_node_pos;
160
161 if (execute_args.kernel_index >= last_train_op_index)
162 {
163 intermediate_buffer_size = num_of_intermediate_tensors * size_of_intermediate_tensors;
164
166 time * intermediate_buffer_size * data_type_size, &intermediate_buffer);
167 if (status != Ok)
168 return status;
169
170 // Save its buffer to state tensor index
171 runtime_storage.saveDataToTensorIndex(intermediate_buffer, state_tensor_index);
172 }
173 }
174
175 switch (input->type())
176 {
177#ifndef DIS_FLOAT
178 case circle::TensorType_FLOAT32:
179 {
180 status =
181 pal::GRU(core::utils::castInputData<float>(input_data),
182 core::utils::castInputData<float>(hidden_input_data),
183 core::utils::castInputData<float>(hidden_hidden_data),
184 core::utils::castInputData<float>(hidden_input_bias_data),
185 core::utils::castInputData<float>(hidden_hidden_bias_data),
186 core::utils::castInputData<float>(state_data),
187 core::utils::castOutputData<float>(output_data),
188 core::utils::castOutputData<float>(output_input_data),
189 core::utils::castOutputData<float>(output_hidden_data),
191 core::OMRuntimeShape(hidden_input), core::OMRuntimeShape(hidden_hidden),
192 intermediate_buffer_size, core::utils::castOutputData<float>(intermediate_buffer));
193 }
194 break;
195#endif // DIS_FLOAT
196 default:
197 {
198 status = UnsupportedType;
199 assert(false && "Unsupported type.");
200 }
201 }
202
205
206 return status;
207}
uoffset_t size() const
const reader::CircleOperators * getCircleOperators()
OMStatus saveDataToTensorIndex(uint8_t *data, uint16_t tensor_index)
uint8_t * outputs_data[maxOutputSize]
OMStatus getDataFromStorage(uint16_t op_index, core::OMRuntimeStorage &storage, core::OMRuntimeContext &context)
OMStatus readKernel(uint16_t op_index, core::OMRuntimeContext &runtime_context)
const circle::Tensor * outputs[maxOutputSize]
const circle::Tensor * inputs[maxInputSize]
constexpr uint32_t outputTensorIdx
list input_data
Definition infer.py:29
OMDataType
"scalar" value type
Definition OMDataType.h:35
OMStatus GRU(const float *input_data, const float *weight_input_data, const float *weight_hidden_data, const float *bias_input_data, const float *bias_hidden_data, const float *hidden_state_data, float *output_data, float *output_input_data, float *output_hidden_data, const core::OMRuntimeShape &input_shape, const core::OMRuntimeShape &output_shape, const core::OMRuntimeShape &weight_input_shape, const core::OMRuntimeShape &weight_hidden_shape, const size_t intermediate_buffer_size, float *intermediate_buffer)
@ UnsupportedType
Definition OMStatus.h:26
static OMStatus deallocateMemory(uint8_t *data)
static OMStatus allocateMemory(uint32_t size, uint8_t **data)
core::OMRuntimeContext & runtime_context
core::OMRuntimeStorage & runtime_storage