Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROperator_RNN.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_ROPERATOR_RNN
2#define TMVA_SOFIE_ROPERATOR_RNN
3
4#include "TMVA/RModel.hxx"
5#include "TMVA/ROperator.hxx"
7
8#include <memory>
9#include <sstream>
10#include <vector>
11
13
14/*! \brief Recurrent Neural Network operator
15 *
16 * Inference code generation for one-layer vanilla RNN. Supports forward, reverse and bidirectional RNNs.
17 * See the <a href="https://github.com/onnx/onnx/blob/master/docs/Operators.md#RNN">ONNX documentation</a>
18 * for details about the supported RNN architectures.
19 */
20template <typename T> class ROperator_RNN final : public ROperator {
21 private:
22 std::vector<float> fAttrActivationAlpha; ///< Scaling values used by some activation functions
23 std::vector<float> fAttrActivationBeta; ///< Scaling values used by some activation functions
24 std::vector<std::string> fAttrActivations; ///< Activation functions
25 float fAttrClip; ///< Clip threshold
26 std::string fAttrDirection; ///< Direction of processing
27 size_t fAttrHiddenSize; ///< Number of the hidden layers
28 size_t fAttrLayout; ///< Data layout
29
30 std::string fNX; ///< Name of the input
31 std::string fNW; ///< Name of the weights
32 std::string fNR; ///< Name of the recurrence
33 std::string fNB; ///< Name of the bias
34 std::string fNSequence_lens; ///< Name of the length of the sequences
35 std::string fNInitial_h; ///< Name of the initial value of the hidden states
36 std::string fNY; ///< Name of the output
37 std::string fNY_h; ///< Name of the last sequence of the output
38
39 std::vector<size_t> fShapeX; ///< Shape of the input
40 std::vector<size_t> fShapeW; ///< Shape of the weights
41 std::vector<size_t> fShapeR; ///< Shape of the recurrence
42 std::vector<size_t> fShapeB; ///< Shape of the bias
43 std::vector<size_t> fShapeSequence_lens; ///< Shape of the length of the sequences
44 std::vector<size_t> fShapeInitial_h; ///< Shape of the initial value of the hidden states
45 std::vector<size_t> fShapeY; ///< Shape of the output
46 std::vector<size_t> fShapeY_h; ///< Shape of the last sequence of the output
47
48 std::string fType; ///< Type of the tensors
49
50 public:
51 /*! Default constructor of ROperator_RNN */
53
54 /*! \brief Constructor of ROperator_RNN from the attributes
55 *
56 * \param activation_alpha scaling values used by some activation functions
57 * \param activation_beta scaling values used by some activation functions
58 * \param activations activation functions
59 * \param clip clip threshold
60 * \param direction direction of processing of the sequneces
61 * \param hidden_size number of hidden layers
62 * \param layout data layout
63 * \param nameX name of the input tensor
64 * \param nameW name of the weight tensor
65 * \param nameR name of the recurrence tensor
66 * \param nameB name of the bias tensor
67 * \param nameSequence_lens name of the length of the sequences
68 * \param nameInitial_h name of the initial value of the hidden states
69 * \param nameY name of the output
70 * \param nameY_h name of the last sequence of the output
71 */
72 ROperator_RNN(std::vector<float> activation_alpha,
73 std::vector<float> activation_beta,
74 std::vector<std::string> activations, float clip,
75 std::string direction, size_t hidden_size, size_t layout,
76 std::string nameX, std::string nameW, std::string nameR,
77 std::string nameB, std::string nameSequence_lens,
78 std::string nameInitial_h, std::string nameY,
79 std::string nameY_h)
84 fNX(UTILITY::Clean_name(nameX)), fNW(UTILITY::Clean_name(nameW)),
85 fNR(UTILITY::Clean_name(nameR)), fNB(UTILITY::Clean_name(nameB)),
86 fNSequence_lens(UTILITY::Clean_name(nameSequence_lens)),
87 fNInitial_h(UTILITY::Clean_name(nameInitial_h)),
88 fNY(UTILITY::Clean_name(nameY)), fNY_h(UTILITY::Clean_name(nameY_h)) {
89 if (std::is_same<T, float>::value) {
90 fType = "float";
91 } else {
92 throw std::runtime_error(
93 "TMVA SOFIE Encountered unsupported type parsing a RNN operator");
94 }
95
97 if(!fNB.empty()){
98 fInputTensorNames.emplace_back(fNB);
99 }
100 if(!fNSequence_lens.empty()){
102 }
103 if(!fNInitial_h.empty()){
104 fInputTensorNames.emplace_back(fNInitial_h);
105 }
106
107 fOutputTensorNames = { };
108 if(!fNY.empty()){
109 fOutputTensorNames.emplace_back(fNY);
110 }
111 if(!fNY_h.empty()){
112 fOutputTensorNames.emplace_back(fNY_h);
113 }
114 }
115
116 /*! \brief Infers the type of the output tensors
117 *
118 * \param input type of the input tensors
119 */
120 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override;
121
122 /*! \brief Infers the shape of the output tensors
123 *
124 * \param input shape of the input tensors
125 */
126 std::vector<std::vector<size_t>>
127 ShapeInference(std::vector<std::vector<size_t>> input) override;
128
129 /*! \brief Initialize the model
130 *
131 * \param model Model
132 */
133 void Initialize(RModel &) override;
134
135 /*! \brief Generates the inference code
136 *
137 * \param OpName name of the operator
138 */
139 std::string Generate(std::string OpName) override;
140
141 // generate code for Session data members (e.g. internal vectors)
142 std::string GenerateSessionMembersCode(std::string opName) override;
143
144 /*! \brief Returns the blas routines needed to compile the generated code
145 */
146 std::vector<std::string> GetBlasRoutines() override { return { std::string("Gemm"), std::string("Axpy") }; }
147};
148
149template <typename T>
150auto ROperator_RNN<T>::TypeInference(std::vector<ETensorType> input) -> std::vector<ETensorType>
151{
152 ETensorType out = input[0];
153 return {out, out};
154}
155
156template <typename T>
157auto ROperator_RNN<T>::ShapeInference(std::vector<std::vector<size_t>> input) -> std::vector<std::vector<size_t>>
158{
159 size_t num_directions = input[1][0];
160 size_t hidden_size = input[1][1];
161 if (fAttrLayout == 0) {
162 size_t seq_length = input[0][0];
163 size_t batch_size = input[0][1];
164 std::vector<std::vector<size_t>> ret(
166 return ret;
167 } else {
168 size_t batch_size = input[0][0];
169 size_t seq_length = input[0][1];
170 std::vector<std::vector<size_t>> ret(
172 return ret;
173 }
174}
175
176template <typename T>
178{
179 fUseSession = model.UseSession();
180 // Check the input and output tensors
181 if (!model.CheckIfTensorAlreadyExist(fNX)) {
182 throw std::runtime_error("TMVA SOFIE RNN Op input tensor " + fNX + " is not found in model.");
183 }
184 fShapeX = model.GetTensorShape(fNX);
185 if (fShapeX.size() != 3) {
186 throw std::runtime_error("TMVA SOFIE RNN Op input tensor " + fNX + " is not of 3 dimensions.");
187 }
188 if (!model.CheckIfTensorAlreadyExist(fNW)) {
189 throw std::runtime_error("TMVA SOFIE RNN Op input tensor " + fNW + " is not found in model.");
190 }
191 fShapeW = model.GetTensorShape(fNW);
192 if (fShapeW.size() != 3) {
193 throw std::runtime_error("TMVA SOFIE RNN Op input tensor " + fNW + " is not of 3 dimensions.");
194 }
195 if (!model.CheckIfTensorAlreadyExist(fNR)) {
196 throw std::runtime_error("TMVA SOFIE RNN Op input tensor " + fNR + " is not found in model.");
197 }
198 fShapeR = model.GetTensorShape(fNR);
199 if (fShapeR.size() != 3) {
200 throw std::runtime_error("TMVA SOFIE RNN Op input tensor " + fNR + " is not of 3 dimensions.");
201 }
202 if (!fNB.empty()) {
203 if (!model.CheckIfTensorAlreadyExist(fNB)) {
204 throw std::runtime_error("TMVA SOFIE RNN op input tensor " + fNB + " is not found in model.");
205 }
206 fShapeB = model.GetTensorShape(fNB);
207 if (fShapeB.size() != 2 && fShapeB.size() != 4) {
208 throw std::runtime_error("TMVA SOFIE RNN op input tensor " + fNB + " is not of 2 or 4 dimensions.");
209 }
210 if (fShapeB.size() == 2) {
211 // Broadcasting the bias
212 auto original_data = model.GetInitializedTensorData(fNB);
213 size_t num_directions = fShapeW[0];
214 size_t seq_length = (fAttrLayout == 0) ? fShapeX[0] : fShapeX[1];
215 size_t batch_size = (fAttrLayout == 0) ? fShapeX[1] : fShapeX[0];
216 if (fType == "float") {
217 float *original_bias = static_cast<float *>(original_data.get());
218 float *new_bias = new float[num_directions * seq_length * batch_size * fAttrHiddenSize];
219 std::vector<float> sum(fAttrHiddenSize);
220 for (size_t direction = 0; direction < num_directions; direction++) {
221 for (size_t h = 0; h < fAttrHiddenSize; h++) {
222 sum[h] = original_bias[direction * 2 * fAttrHiddenSize + h] +
223 original_bias[(2 * direction + 1) * fAttrHiddenSize + h];
224 }
225 for (size_t seq = 0; seq < seq_length; seq++) {
226 for (size_t batch = 0; batch < batch_size; batch++) {
227 size_t bias_offset = direction * seq_length * batch_size * fAttrHiddenSize +
228 seq * batch_size * fAttrHiddenSize + batch * fAttrHiddenSize;
229 std::copy(sum.begin(), sum.end(), new_bias + bias_offset);
230 }
231 }
232 }
233 std::vector<size_t> new_bias_shape = {num_directions, seq_length, batch_size, fAttrHiddenSize};
234 std::shared_ptr<void> new_bias_ptr(new_bias, std::default_delete<float[]>());
235 model.UpdateInitializedTensor(fNB, model.GetTensorType(fNB), new_bias_shape, new_bias_ptr);
236 fShapeB = model.GetTensorShape(fNB);
237 }
238 }
239 }
240 if (!fNSequence_lens.empty()) {
241 if (!model.CheckIfTensorAlreadyExist(fNSequence_lens)) {
242 throw std::runtime_error("TMVA SOFIE RNN Op input tensor " + fNSequence_lens + "is not found in model.");
243 }
244 fShapeSequence_lens = model.GetTensorShape(fNSequence_lens);
245 if (fShapeSequence_lens.size() != 1) {
246 throw std::runtime_error("TMVA SOFIE RNN Op input tensor " + fNSequence_lens + " is not of 1 dimension.");
247 }
248 }
249 if (!fNInitial_h.empty()) {
250 if (!model.CheckIfTensorAlreadyExist(fNInitial_h)) {
251 throw std::runtime_error("TMVA SOFIE RNN Op input tensor " + fNInitial_h + " is not found in model.");
252 }
253 fShapeInitial_h = model.GetTensorShape(fNInitial_h);
254 if (fShapeInitial_h.size() != 3) {
255 throw std::runtime_error("TMVA SOFIE RNN Op input tensor " + fNInitial_h + " is not of 3 dimensions.");
256 }
257 }
258 if (!fNY.empty()) {
259 fShapeY = ShapeInference({fShapeX, fShapeW})[0];
260 if (!model.CheckIfTensorAlreadyExist(fNY)) {
261 model.AddIntermediateTensor(fNY, model.GetTensorType(fNX), fShapeY);
262 }
263 }
264 if (!fNY_h.empty()) {
265 fShapeY_h = ShapeInference({fShapeX, fShapeW})[1];
266 if (!model.CheckIfTensorAlreadyExist(fNY_h)) {
267 model.AddIntermediateTensor(fNY_h, model.GetTensorType(fNX), fShapeY_h);
268 }
269 }
270 // Check the attributes
271 for (auto &activation : fAttrActivations) {
272 if (activation != "Relu" && activation != "Tanh" && activation != "Sigmoid" && activation != "Affine" &&
273 activation != "LeakyRelu" && activation != "ThresholdRelu" && activation != "ScaledTanh" &&
274 activation != "HardSigmoid" && activation != "Elu" && activation != "Softsign" && activation != "Softplus") {
275 throw std::runtime_error("TMVA SOFIE - Activation function " + activation + " not implemented");
276 }
277 }
278 if (fAttrDirection != "forward" && fAttrDirection != "backward" && fAttrDirection != "bidirectional") {
279 throw std::runtime_error("TMVA SOFIE - Invalid RNN direction fAttrDirection = " + fAttrDirection);
280 }
281 if (fAttrHiddenSize != fShapeW[1]) {
282 throw std::runtime_error("TMVA SOFIE - fAttrHiddenSize must be equal to " + std::to_string(fShapeW[1]));
283 }
284 if (fAttrLayout > 1) {
285 throw std::runtime_error("TMVA SOFIE - Layout fAttrLayout = " + std::to_string(fAttrLayout) +
286 " must be 0 (timewise) or 1 (batchwise)");
287 }
288 if (fAttrActivations.empty()) {
289 if (fAttrDirection == "bidirectional") {
290 fAttrActivations = {"Tanh", "Tanh"};
291 } else {
292 fAttrActivations = {"Tanh"};
293 }
294 }
295 // Add needed standard library headers
296 model.AddNeededStdLib("cmath");
297}
298
299// generate code for Session data members (e.g. internal vectors)
300template <typename T>
302{
303 opName = "op_" + opName;
304 std::stringstream out;
305
306 size_t num_directions = fShapeW[0];
307 size_t seq_length = (fAttrLayout == 0) ? fShapeX[0] : fShapeX[1];
308 size_t batch_size = (fAttrLayout == 0) ? fShapeX[1] : fShapeX[0];
309 size_t input_size = fShapeX[2];
310
311 if (fAttrLayout != 0) {
312 out << "std::vector<" << fType << "> fVec_" << opName << "_input = std::vector<" << fType << ">("
313 << seq_length * batch_size * input_size << ");\n";
314 out << "std::vector<" << fType << "> fVec_" << opName << "_initial_hidden_state = std::vector<" << fType << ">("
315 << num_directions * batch_size * fAttrHiddenSize << ");\n";
316 }
317 out << "std::vector<" << fType << "> fVec_" << opName << "_feedforward = std::vector<" << fType << ">("
318 << seq_length * batch_size * fAttrHiddenSize << ");\n";
319
320 if (fAttrLayout != 0 || fNY.empty()) {
321 out << "std::vector<" << fType << "> fVec_" << opName << "_hidden_state = std::vector<" << fType << ">("
322 << seq_length * num_directions * batch_size * fAttrHiddenSize << ");\n";
323 }
324
325 out << "\n";
326
327 return out.str();
328}
329
330//////////////////////////////////////////////////////////////////////////////////////////////////
331template <typename T>
332auto ROperator_RNN<T>::Generate(std::string OpName) -> std::string
333{
334 OpName = "op_" + OpName;
335 std::stringstream out;
336
337 size_t seq_length = (fAttrLayout == 0) ? fShapeX[0] : fShapeX[1];
338 size_t batch_size = (fAttrLayout == 0) ? fShapeX[1] : fShapeX[0];
339 size_t input_size = fShapeX[2];
340 size_t num_directions = fShapeW[0];
341
342 // set the input
343 if (fAttrLayout == 0) {
344 if (fType == "float") {
345 out << SP << "float const*" << OpName << "_input = tensor_" << fNX << ";\n";
346 }
347 } else {
348 if (fUseSession)
349 out << SP << fType << " * " << OpName << "_input = this->fVec_" << OpName << "_input.data();\n";
350 else
351 out << SP << fType << " " << OpName << "_input[" << seq_length * batch_size * input_size << "];\n";
352 out << SP << "for(size_t seq = 0; seq < " << seq_length << "; seq++) {\n";
353 out << SP << SP << "for(size_t batch = 0; batch < " << batch_size << "; batch++) {\n";
354 out << SP << SP << SP << "for(size_t i = 0; i < " << input_size << "; i++) {\n";
355 out << SP << SP << SP << SP << OpName << "_input[seq * " << batch_size * input_size << " + batch * " << input_size
356 << " + i] = " << "tensor_" << fNX << "[batch * " << seq_length * input_size << " + seq * " << input_size
357 << " + i];\n";
358 out << SP << SP << SP << "}\n";
359 out << SP << SP << "}\n";
360 out << SP << "}\n";
361 }
362
363 // Set the initial hidden state
364 if (!fNInitial_h.empty()) {
365 if (fAttrLayout == 0) {
366 out << SP << fType << " *" << OpName << "_initial_hidden_state = " << " tensor_" << fNInitial_h << ";\n";
367 } else {
368 if (fUseSession)
369 out << SP << fType << " * " << OpName << "_initial_hidden_state = this->fVec_" << OpName
370 << "_initial_hidden_state.data();\n";
371 else
372 out << fType << " " << OpName << "_initial_hidden_state[" << num_directions * batch_size * fAttrHiddenSize
373 << "] = {0};\n";
374
375 for (size_t direction = 0; direction < num_directions; direction++) {
376 out << SP << "for(size_t batch = 0; batch < " << batch_size << "; batch++) {\n";
377 out << SP << SP << "for(size_t h = 0; h < " << fAttrHiddenSize << "; h++) {\n";
378 out << SP << SP << SP << OpName << "_initial_hidden_state[" << direction * batch_size * fAttrHiddenSize
379 << " + batch * " << fAttrHiddenSize << " + h] = tensor_" << fNInitial_h << "[batch * "
380 << num_directions * fAttrHiddenSize << " + " << direction * fAttrHiddenSize << " + h];\n";
381 out << SP << SP << "}\n";
382 out << SP << "}\n";
383 }
384 }
385 }
386
387 if (fUseSession)
388 out << SP << fType << " * " << OpName << "_feedforward = this->fVec_" << OpName << "_feedforward.data();\n";
389 else
390 out << SP << fType << " " << OpName << "_feedforward[" << seq_length * batch_size * fAttrHiddenSize
391 << "] = {0};\n";
392
393 // Set the hidden state
394 if (fAttrLayout == 0 && !fNY.empty()) {
395 out << SP << fType << " *" << OpName << "_hidden_state = tensor_" << fNY << ";\n";
396 } else {
397 if (fUseSession)
398 out << SP << fType << " * " << OpName << "_hidden_state = this->fVec_" << OpName << "_hidden_state.data();\n";
399 else
400 out << SP << fType << " " << OpName << "_hidden_state["
401 << seq_length * num_directions * batch_size * fAttrHiddenSize << "] = {0};\n";
402 }
403
404 out << SP << "char " << OpName << "_transA = 'N';\n";
405 out << SP << "char " << OpName << "_transB = 'T';\n";
406 out << SP << "int " << OpName << "_m = " << seq_length * batch_size << ";\n";
407 out << SP << "int " << OpName << "_n = " << fAttrHiddenSize << ";\n";
408 out << SP << "int " << OpName << "_k = " << input_size << ";\n";
409 if (fType == "float") {
410 out << SP << "float " << OpName << "_alpha = 1.;\n";
411 out << SP << "float " << OpName << "_beta = .0;\n";
412 }
413 if (!fNB.empty()) {
414 out << SP << "int " << OpName << "_bias_size = " << seq_length * batch_size * fAttrHiddenSize << ";\n";
415 out << SP << "int " << OpName << "_incx = 1;\n";
416 out << SP << "int " << OpName << "_incy = 1;\n";
417 }
418
419 for (size_t direction = 0; direction < num_directions; direction++) {
420 // feedforward = input * W^T + bias
421 if (fType == "float") {
422 if (direction == 0) {
423 out << SP << "BLAS::sgemm_(&" << OpName << "_transB, &" << OpName << "_transA, &" << OpName << "_n, &"
424 << OpName << "_m, &" << OpName << "_k, &" << OpName << "_alpha, tensor_" << fNW << ", &" << OpName
425 << "_k, " << OpName << "_input, &" << OpName << "_k, &" << OpName << "_beta, " << OpName
426 << "_feedforward, &" << OpName << "_n);\n";
427 } else {
428 out << SP << "size_t " << OpName << "_w_offset = " << fAttrHiddenSize * input_size << ";\n";
429 out << SP << "BLAS::sgemm_(&" << OpName << "_transB, &" << OpName << "_transA, &" << OpName << "_n, &"
430 << OpName << "_m, &" << OpName << "_k, &" << OpName << "_alpha, tensor_" << fNW << " + " << OpName
431 << "_w_offset, &" << OpName << "_k, " << OpName << "_input, &" << OpName << "_k, &" << OpName
432 << "_beta, " << OpName << "_feedforward, &" << OpName << "_n);\n";
433 }
434 }
435 // Add the bias
436 if (!fNB.empty()) {
437 if (fType == "float") {
438 if (direction == 0) {
439 out << SP << "BLAS::saxpy_(&" << OpName << "_bias_size, &" << OpName << "_alpha, tensor_" << fNB << ", &"
440 << OpName << "_incx, " << OpName << "_feedforward, &" << OpName << "_incy);\n";
441 } else {
442 out << SP << "size_t " << OpName << "_bias_offset = " << seq_length * batch_size * fAttrHiddenSize
443 << ";\n";
444 out << SP << "BLAS::saxpy_(&" << OpName << "_bias_size, &" << OpName << "_alpha, tensor_" << fNB << " + "
445 << OpName << "_bias_offset, &" << OpName << "_incx, " << OpName << "_feedforward, &" << OpName
446 << "_incy);\n";
447 }
448 }
449 }
450
451 // Copy feedforward into hidden state
452 out << SP << "for (size_t seq = 0; seq < " << seq_length << "; seq++) {\n";
453 out << SP << SP << "size_t offset = seq * " << batch_size * fAttrHiddenSize << ";\n";
454 out << SP << SP << "size_t size = " << batch_size * fAttrHiddenSize << ";\n";
455 out << SP << SP << "size_t h_offset = seq * " << num_directions * batch_size * fAttrHiddenSize << " + "
456 << direction * batch_size * fAttrHiddenSize << ";\n";
457 out << SP << SP << "std::copy(" << OpName << "_feedforward + offset, " << OpName
458 << "_feedforward + offset + size, " << OpName << "_hidden_state + h_offset);\n";
459 out << SP << "}\n";
460
461 out << SP << "for (size_t seq = 0; seq < " << seq_length << "; seq++) {\n";
462 if (fAttrDirection == "backward" || direction == 1) {
463 out << SP << SP << "size_t index = " << seq_length - 1 << " - seq;\n";
464 } else {
465 out << SP << SP << "size_t index = seq;\n";
466 }
467
468 out << SP << SP << "int m2 = " << batch_size << ";\n";
469 out << SP << SP << "size_t offset = index * " << num_directions * batch_size * fAttrHiddenSize << " + "
470 << direction * batch_size * fAttrHiddenSize << ";\n";
471 out << SP << SP << "size_t size = " << batch_size * fAttrHiddenSize << ";\n";
472 out << SP << SP << "if (seq == 0) {\n";
473 if (!fNInitial_h.empty()) {
474 // hidden_state = hidden_state + initial_hidden_state * R^T
475 out << SP << SP << SP << "size_t r_offset = " << direction * fAttrHiddenSize * fAttrHiddenSize << ";\n";
476 out << SP << SP << SP << "size_t initial_hidden_state_offset = " << direction * batch_size * fAttrHiddenSize
477 << ";\n";
478 if (fType == "float") {
479 out << SP << SP << SP << "BLAS::sgemm_(&" << OpName << "_transB, &" << OpName << "_transA, &" << OpName
480 << "_n, &m2, &" << OpName << "_n, &" << OpName << "_alpha, tensor_" << fNR << " + r_offset, &" << OpName
481 << "_n, " << OpName << "_initial_hidden_state + initial_hidden_state_offset, &" << OpName << "_n, &"
482 << OpName << "_alpha, " << OpName << "_hidden_state + offset, &" << OpName << "_n);\n";
483 }
484 }
485 out << SP << SP << "} else {\n";
486 // hidden_state = hidden_state + previous_hidden_state * R^T
487 out << SP << SP << SP << "size_t r_offset = " << direction * fAttrHiddenSize * fAttrHiddenSize << ";\n";
488 if (fAttrDirection == "backward" || direction == 1) {
489 out << SP << SP << SP << "size_t previous_offset = (index + 1) * "
490 << num_directions * batch_size * fAttrHiddenSize << " + " << direction * batch_size * fAttrHiddenSize
491 << ";\n";
492 } else {
493 out << SP << SP << SP << "size_t previous_offset = (seq - 1) * "
494 << num_directions * batch_size * fAttrHiddenSize << " + " << direction * batch_size * fAttrHiddenSize
495 << ";\n";
496 }
497 if (fType == "float") {
498 out << SP << SP << SP << "BLAS::sgemm_(&" << OpName << "_transB, &" << OpName << "_transA, &" << OpName
499 << "_n, &m2, &" << OpName << "_n, &" << OpName << "_alpha, tensor_" << fNR << " + r_offset, &" << OpName
500 << "_n, " << OpName << "_hidden_state + previous_offset, &" << OpName << "_n, &" << OpName << "_alpha, "
501 << OpName << "_hidden_state + offset, &" << OpName << "_n);\n";
502 }
503 out << SP << SP << "}\n";
504
505 // Clip the elements of the hidden state into the range [-fAttrClip, fAttrClip]
506 if (fAttrClip > .0) {
507 out << SP << SP << "for (size_t i = offset; i < offset + size; i++) {\n";
508 if (fType == "float") {
509 out << SP << SP << SP << "float x = (" << OpName << "_hidden_state[i] > " << -fAttrClip << ") ? " << OpName
510 << "_hidden_state[i] : " << -fAttrClip << ";\n";
511 }
512 out << SP << SP << SP << OpName << "_hidden_state[i] = (x < " << fAttrClip << ") ? x : " << fAttrClip << ";\n";
513 out << SP << SP << "}\n";
514 }
515
516 // Apply the activation function to the hidden state
517 if (fAttrActivations[direction] == "Relu") {
518 out << SP << SP << "for (size_t i = offset; i < offset + size; i++) {\n";
519 out << SP << SP << SP << "if (" << OpName << "_hidden_state[i] < 0.)\n";
520 out << SP << SP << SP << SP << OpName << "_hidden_state[i] = 0.;\n";
521 out << SP << SP << "}\n";
522 } else if (fAttrActivations[direction] == "Tanh") {
523 out << SP << SP << "for (size_t i = offset; i < offset + size; i++) {\n";
524 if (fType == "float") {
525 out << SP << SP << SP << "float ex = std::exp(-2 * " << OpName << "_hidden_state[i]);\n";
526 }
527 out << SP << SP << SP << SP << OpName << "_hidden_state[i] = (1. - ex) / (1. + ex);\n";
528 out << SP << SP << "}\n";
529 } else if (fAttrActivations[direction] == "Sigmoid") {
530 out << SP << SP << "for (size_t i = offset; i < offset + size; i++) {\n";
531 out << SP << SP << SP << SP << OpName << "_hidden_state[i] = 1. / (1. + std::exp(-" << OpName
532 << "_hidden_state[i]));\n";
533 out << SP << SP << "}\n";
534 } else if (fAttrActivations[direction] == "Affine") {
535 out << SP << SP << "for (size_t i = offset; i < offset + size; i++) {\n";
536 out << SP << SP << SP << SP << OpName << "_hidden_state[i] = " << fAttrActivationAlpha[direction] << " * "
537 << OpName << "_hidden_state[i] + " << fAttrActivationBeta[direction] << ";\n";
538 out << SP << SP << "}\n";
539 } else if (fAttrActivations[direction] == "ScaledTanh") {
540 out << SP << SP << "for (size_t i = offset; i < offset + size; i++) {\n";
541 if (fType == "float") {
542 out << SP << SP << SP << "float ex = std::exp(-2 * " << fAttrActivationBeta[direction] << " * " << OpName
543 << "_hidden_state[i]);\n";
544 }
545 out << SP << SP << SP << SP << OpName << "_hidden_state[i] = " << fAttrActivationAlpha[direction]
546 << " * (1. - ex) / (1. + ex);\n";
547 out << SP << SP << "}\n";
548 } else if (fAttrActivations[direction] == "HardSigmoid") {
549 out << SP << SP << "for (size_t i = offset; i < offset + size; i++) {\n";
550 if (fType == "float") {
551 out << SP << SP << SP << "float a = " << fAttrActivationAlpha[direction] << " * " << OpName
552 << "_hidden_state[i] + " << fAttrActivationBeta[direction] << ";\n";
553 out << SP << SP << SP << "float b = (a > 0.) ? a : 0.;\n";
554 }
555 out << SP << SP << SP << SP << OpName << "_hidden_state[i] = (b < 1.) ? b : 1.;\n";
556 out << SP << SP << "}\n";
557 } else if (fAttrActivations[direction] == "LeakyRelu") {
558 out << SP << SP << "for (size_t i = offset; i < offset + size; i++) {\n";
559 out << SP << SP << SP << "if (" << OpName << "_hidden_state[i] < 0.)\n";
560 out << SP << SP << SP << SP << OpName << "_hidden_state[i] = " << fAttrActivationAlpha[direction] << " * "
561 << OpName << "_hidden_state[i];\n";
562 out << SP << SP << "}\n";
563 } else if (fAttrActivations[direction] == "ThresholdRelu") {
564 out << SP << SP << "for (size_t i = offset; i < offset + size; i++) {\n";
565 out << SP << SP << SP << "if (" << OpName << "_hidden_state[i] < " << fAttrActivationAlpha[direction] << ")\n";
566 out << SP << SP << SP << SP << OpName << "_hidden_state[i] = 0.;\n";
567 out << SP << SP << "}";
568 } else if (fAttrActivations[direction] == "Elu") {
569 out << SP << SP << "for (size_t i = offset; i < offset + size; i++) {\n";
570 out << SP << SP << SP << "if (" << OpName << "_hidden_state[i] < 0.)\n";
571 out << SP << SP << SP << SP << OpName << "_hidden_state[i] = " << fAttrActivationAlpha[direction]
572 << " * std::exp(" << OpName << "_hidden_state[i] - 1.);\n";
573 out << SP << SP << "}\n";
574 } else if (fAttrActivations[direction] == "Softsign") {
575 out << SP << SP << "for (size_t i = offset; i < offset + size; i++) {\n";
576 out << SP << SP << SP << SP << OpName << "_hidden_state[i] = " << OpName << "_hidden_state[i] / (1. + abs("
577 << OpName << "_hidden_state[i]));\n";
578 out << SP << SP << "}\n";
579 } else { // fAttrActivations[direction] = Softplus
580 out << SP << SP << "for (size_t i = offset; i < offset + size; i++) {\n";
581 out << SP << SP << SP << SP << OpName << "_hidden_state[i] = log(1. + std::exp(" << OpName
582 << "_hidden_state[i]));\n";
583 out << SP << SP << "}\n";
584 out << SP << "}\n";
585 }
586 out << SP << "}\n";
587 }
588
589 // Padding the hidden state for RNN with different sequence lengths
590 if (!fNSequence_lens.empty()) {
591 out << SP << "for (size_t seq = 0; seq < " << seq_length << "; seq++) {\n";
592 out << SP << SP << "for (size_t batch = 0; batch < " << batch_size << "; batch++) {\n";
593 out << SP << SP << SP << "if (seq >= tensor_" << fNSequence_lens << "[batch]) {\n";
594 out << SP << SP << SP << SP << "for (size_t h = 0; h < " << fAttrHiddenSize << "; h++) {\n";
595 if (num_directions == 1) {
596 out << SP << SP << SP << SP << SP << OpName << "_hidden_state[seq * "
597 << num_directions * batch_size * fAttrHiddenSize << " + batch * " << fAttrHiddenSize << " + h] = 0.;\n";
598 } else {
599 out << SP << SP << SP << SP << SP << OpName << "_hidden_state[seq * "
600 << num_directions * batch_size * fAttrHiddenSize << " + batch * " << fAttrHiddenSize << " + h] = 0.;\n";
601 out << SP << SP << SP << SP << SP << OpName << "_hidden_state[seq * "
602 << num_directions * batch_size * fAttrHiddenSize << " + " << batch_size * fAttrHiddenSize << " + batch * "
603 << fAttrHiddenSize << " + h] = 0.;\n";
604 }
605 out << SP << SP << SP << SP << "}\n";
606 out << SP << SP << SP << "}\n";
607 out << SP << SP << "}\n";
608 out << SP << "}\n";
609 }
610
611 // Copy the hidden state into y and y_h
612 if (fAttrLayout == 0) {
613 if (!fNY_h.empty()) {
614 if (fNSequence_lens.empty()) {
615 size_t yh_size = batch_size * fAttrHiddenSize;
616 if (fAttrDirection == "backward") {
617 out << SP << "std::copy(" << OpName << "_hidden_state, " << OpName << "_hidden_state + " << yh_size
618 << ", tensor_" << fNY_h << ");\n";
619 } else {
620 size_t offset = (seq_length - 1) * num_directions * batch_size * fAttrHiddenSize;
621 out << SP << "std::copy(" << OpName << "_hidden_state + " << offset << ", " << OpName
622 << "_hidden_state + " << offset << " + " << yh_size << ", tensor_" << fNY_h << ");\n";
623 }
624 if (num_directions == 2) {
625 out << SP << "std::copy(" << OpName << "_hidden_state + " << yh_size << ", " << OpName
626 << "_hidden_state + " << 2 * yh_size << ", tensor_" << fNY_h << " + " << yh_size << ");\n";
627 }
628 } else { // RNN with different sequence lengths
629 if (fAttrDirection == "backward") {
630 out << SP << "for (size_t batch = 0; batch < " << batch_size << "; batch++) {\n";
631 out << SP << SP << "size_t offset = batch * " << fAttrHiddenSize << ";\n";
632 out << SP << SP << "std::copy(" << OpName << "_hidden_state + offset, " << OpName
633 << "_hidden_state + offset + " << fAttrHiddenSize << ", tensor_" << fNY_h << " + offset);\n";
634 out << SP << "}\n";
635 } else {
636 out << SP << "for (size_t batch = 0; batch < " << batch_size << "; batch++) {\n";
637 out << SP << SP << "size_t seq = " << "tensor_" << fNSequence_lens << "[batch] - 1;\n";
638 out << SP << SP << "size_t offset = seq * " << num_directions * batch_size * fAttrHiddenSize
639 << " + batch * " << fAttrHiddenSize << ";\n";
640 out << SP << SP << "size_t yh_offset = batch * " << fAttrHiddenSize << ";\n";
641 out << SP << SP << "std::copy(" << OpName << "_hidden_state + offset, " << OpName
642 << "_hidden_state + offset + " << fAttrHiddenSize << ", tensor_" << fNY_h << " + yh_offset);\n";
643 out << SP << "}\n";
644 }
645 if (num_directions == 2) {
646 out << SP << "for (size_t batch = 0; batch < " << batch_size << "; batch++) {\n";
647 out << SP << SP << "size_t offset = " << batch_size * fAttrHiddenSize << " + batch * " << fAttrHiddenSize
648 << ";\n";
649 out << SP << SP << "size_t yh_offset = " << batch_size * fAttrHiddenSize << " + batch * "
650 << fAttrHiddenSize << ";\n";
651 out << SP << SP << "std::copy(" << OpName << "_hidden_state + offset, " << OpName
652 << "_hidden_state + offset + " << fAttrHiddenSize << ", tensor_" << fNY_h << " + yh_offset);\n";
653 out << SP << "}\n";
654 }
655 }
656 }
657 } else { // fAttrLayout=1
658 if (!fNY.empty()) {
659 for (size_t direction = 0; direction < num_directions; direction++) {
660 out << SP << "for (size_t seq = 0; seq < " << seq_length << "; seq++) {\n";
661 out << SP << SP << "for (size_t batch = 0; batch < " << batch_size << "; batch++) {\n";
662 out << SP << SP << SP << "size_t offset = seq * " << num_directions * batch_size * fAttrHiddenSize << " + "
663 << direction * batch_size * fAttrHiddenSize << " + batch * " << fAttrHiddenSize << ";\n";
664 out << SP << SP << SP << "size_t y_offset = batch * " << seq_length * num_directions * fAttrHiddenSize
665 << " + seq * " << num_directions * fAttrHiddenSize << " + " << direction * fAttrHiddenSize << ";\n";
666 out << SP << SP << SP << "std::copy(" << OpName << "_hidden_state + offset, " << OpName
667 << "_hidden_state + offset + " << fAttrHiddenSize << ", tensor_" << fNY << " + y_offset);\n";
668 out << SP << SP << "}\n";
669 out << SP << "}\n";
670 }
671 }
672 if (!fNY_h.empty()) {
673 if (fAttrDirection == "backward") {
674 out << SP << "for (size_t batch = 0; batch < " << batch_size << "; batch++) {\n";
675 out << SP << SP << "size_t offset = batch * " << fAttrHiddenSize << ";\n";
676 out << SP << SP << "size_t yh_offset = batch * " << num_directions * fAttrHiddenSize << ";\n";
677 out << SP << SP << "std::copy(" << OpName << "_hidden_state + offset, " << OpName
678 << "_hidden_state + offset + " << fAttrHiddenSize << ", tensor_" << fNY_h << " + yh_offset);\n";
679 out << SP << "}\n";
680 } else {
681 out << SP << "for (size_t batch = 0; batch < " << batch_size << "; batch++) {\n";
682 if (fNSequence_lens.empty()) {
683 out << SP << SP << "size_t seq = " << seq_length - 1 << ";\n";
684 } else {
685 out << SP << SP << "size_t seq = " << "tensor_" << fNSequence_lens << "[batch] - 1;\n";
686 }
687 out << SP << SP << "size_t offset = seq * " << num_directions * batch_size * fAttrHiddenSize
688 << " + batch * " << fAttrHiddenSize << ";\n";
689 out << SP << SP << "size_t yh_offset = batch * " << num_directions * fAttrHiddenSize << ";\n";
690 out << SP << SP << "std::copy(" << OpName << "_hidden_state + offset, " << OpName
691 << "_hidden_state + offset + " << fAttrHiddenSize << ", tensor_" << fNY_h << " + yh_offset);\n";
692 out << SP << "}\n";
693 }
694 if (num_directions == 2) {
695 out << SP << "for (size_t batch = 0; batch < " << batch_size << "; batch++) {\n";
696 out << SP << SP << "size_t offset = " << batch_size * fAttrHiddenSize << " + batch * " << fAttrHiddenSize
697 << ";\n";
698 out << SP << SP << "size_t yh_offset = batch * " << num_directions * fAttrHiddenSize << " + "
699 << fAttrHiddenSize << ";\n";
700 out << SP << SP << "std::copy(" << OpName << "_hidden_state + offset, " << OpName
701 << "_hidden_state + offset + " << fAttrHiddenSize << ", tensor_" << fNY_h << " + yh_offset);\n";
702 out << SP << "}\n";
703 }
704 }
705 }
706
707 return out.str();
708}
709
710} // namespace TMVA::Experimental::SOFIE
711
712#endif
#define h(i)
Definition RSha256.hxx:106
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
const_iterator begin() const
const_iterator end() const
Recurrent Neural Network operator.
std::vector< size_t > fShapeB
Shape of the bias.
std::vector< float > fAttrActivationBeta
Scaling values used by some activation functions.
size_t fAttrHiddenSize
Number of the hidden layers.
std::string fNInitial_h
Name of the initial value of the hidden states.
ROperator_RNN(std::vector< float > activation_alpha, std::vector< float > activation_beta, std::vector< std::string > activations, float clip, std::string direction, size_t hidden_size, size_t layout, std::string nameX, std::string nameW, std::string nameR, std::string nameB, std::string nameSequence_lens, std::string nameInitial_h, std::string nameY, std::string nameY_h)
Constructor of ROperator_RNN from the attributes.
void Initialize(RModel &) override
Initialize the model.
std::vector< size_t > fShapeR
Shape of the recurrence.
std::string fNW
Name of the weights.
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input) override
Infers the type of the output tensors.
std::vector< size_t > fShapeY
Shape of the output.
std::string fType
Type of the tensors.
std::string fNY
Name of the output.
std::string fNSequence_lens
Name of the length of the sequences.
std::vector< size_t > fShapeSequence_lens
Shape of the length of the sequences.
std::vector< std::string > GetBlasRoutines() override
Returns the blas routines needed to compile the generated code.
std::string fNR
Name of the recurrence.
std::string GenerateSessionMembersCode(std::string opName) override
std::vector< float > fAttrActivationAlpha
Scaling values used by some activation functions.
ROperator_RNN()
Default constructor of ROperator_RNN.
std::vector< size_t > fShapeX
Shape of the input.
std::string fAttrDirection
Direction of processing.
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input) override
Infers the shape of the output tensors.
std::string fNX
Name of the input.
std::string Generate(std::string OpName) override
Generates the inference code.
std::string fNY_h
Name of the last sequence of the output.
std::vector< size_t > fShapeInitial_h
Shape of the initial value of the hidden states.
std::vector< size_t > fShapeW
Shape of the weights.
std::vector< std::string > fAttrActivations
Activation functions.
std::vector< size_t > fShapeY_h
Shape of the last sequence of the output.
std::vector< std::string_view > fInputTensorNames
Definition ROperator.hxx:49
std::vector< std::string_view > fOutputTensorNames
Definition ROperator.hxx:50
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2338