1#ifndef TMVA_SOFIE_ROPERATOR_RNN
2#define TMVA_SOFIE_ROPERATOR_RNN
89 if (std::is_same<T, float>::value) {
92 throw std::runtime_error(
93 "TMVA SOFIE Encountered unsupported type parsing a RNN operator");
126 std::vector<std::vector<size_t>>
146 std::vector<std::string>
GetBlasRoutines()
override {
return { std::string(
"Gemm"), std::string(
"Axpy") }; }
161 if (fAttrLayout == 0) {
164 std::vector<std::vector<size_t>>
ret(
170 std::vector<std::vector<size_t>>
ret(
179 fUseSession = model.UseSession();
181 if (!model.CheckIfTensorAlreadyExist(fNX)) {
182 throw std::runtime_error(
"TMVA SOFIE RNN Op input tensor " + fNX +
" is not found in model.");
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.");
188 if (!model.CheckIfTensorAlreadyExist(fNW)) {
189 throw std::runtime_error(
"TMVA SOFIE RNN Op input tensor " + fNW +
" is not found in model.");
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.");
195 if (!model.CheckIfTensorAlreadyExist(fNR)) {
196 throw std::runtime_error(
"TMVA SOFIE RNN Op input tensor " + fNR +
" is not found in model.");
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.");
203 if (!model.CheckIfTensorAlreadyExist(fNB)) {
204 throw std::runtime_error(
"TMVA SOFIE RNN op input tensor " + fNB +
" is not found in model.");
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.");
210 if (fShapeB.size() == 2) {
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") {
219 std::vector<float>
sum(fAttrHiddenSize);
221 for (
size_t h = 0;
h < fAttrHiddenSize;
h++) {
236 fShapeB = model.GetTensorShape(fNB);
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.");
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.");
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.");
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.");
259 fShapeY = ShapeInference({fShapeX, fShapeW})[0];
260 if (!model.CheckIfTensorAlreadyExist(fNY)) {
261 model.AddIntermediateTensor(fNY, model.GetTensorType(fNX), fShapeY);
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);
275 throw std::runtime_error(
"TMVA SOFIE - Activation function " +
activation +
" not implemented");
278 if (fAttrDirection !=
"forward" && fAttrDirection !=
"backward" && fAttrDirection !=
"bidirectional") {
279 throw std::runtime_error(
"TMVA SOFIE - Invalid RNN direction fAttrDirection = " + fAttrDirection);
281 if (fAttrHiddenSize != fShapeW[1]) {
282 throw std::runtime_error(
"TMVA SOFIE - fAttrHiddenSize must be equal to " + std::to_string(fShapeW[1]));
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)");
288 if (fAttrActivations.empty()) {
289 if (fAttrDirection ==
"bidirectional") {
290 fAttrActivations = {
"Tanh",
"Tanh"};
292 fAttrActivations = {
"Tanh"};
296 model.AddNeededStdLib(
"cmath");
304 std::stringstream out;
307 size_t seq_length = (fAttrLayout == 0) ? fShapeX[0] : fShapeX[1];
308 size_t batch_size = (fAttrLayout == 0) ? fShapeX[1] : fShapeX[0];
316 std::vector<Block>
blocks;
318 if (fAttrLayout != 0) {
323 if (fAttrLayout != 0 || fNY.empty()) {
334 out <<
"std::vector<" << fType <<
"> fVec_" <<
opName <<
"_buffer = std::vector<" << fType <<
">(" <<
total_size
340 out << fType <<
"* fVec_" <<
opName <<
"_" <<
b.name <<
" = fVec_" <<
opName <<
"_buffer.data() + " <<
offset
355 std::stringstream out;
357 size_t seq_length = (fAttrLayout == 0) ? fShapeX[0] : fShapeX[1];
358 size_t batch_size = (fAttrLayout == 0) ? fShapeX[1] : fShapeX[0];
363 if (fAttrLayout == 0) {
364 if (fType ==
"float") {
365 out << SP <<
"float const*" <<
OpName <<
"_input = tensor_" << fNX <<
";\n";
369 out << SP << fType <<
" * " <<
OpName <<
"_input = this->fVec_" <<
OpName <<
"_input;\n";
372 out << SP <<
"for(size_t seq = 0; seq < " <<
seq_length <<
"; seq++) {\n";
373 out << SP << SP <<
"for(size_t batch = 0; batch < " <<
batch_size <<
"; batch++) {\n";
374 out << SP << SP << SP <<
"for(size_t i = 0; i < " <<
input_size <<
"; i++) {\n";
378 out << SP << SP << SP <<
"}\n";
379 out << SP << SP <<
"}\n";
384 if (!fNInitial_h.empty()) {
385 if (fAttrLayout == 0) {
386 out << SP << fType <<
" *" <<
OpName <<
"_initial_hidden_state = " <<
" tensor_" << fNInitial_h <<
";\n";
389 out << SP << fType <<
" * " <<
OpName <<
"_initial_hidden_state = this->fVec_" <<
OpName
390 <<
"_initial_hidden_state;\n";
396 out << SP <<
"for(size_t batch = 0; batch < " <<
batch_size <<
"; batch++) {\n";
397 out << SP << SP <<
"for(size_t h = 0; h < " << fAttrHiddenSize <<
"; h++) {\n";
399 <<
" + batch * " << fAttrHiddenSize <<
" + h] = tensor_" << fNInitial_h <<
"[batch * "
401 out << SP << SP <<
"}\n";
408 out << SP << fType <<
" * " <<
OpName <<
"_feedforward = this->fVec_" <<
OpName <<
"_feedforward;\n";
414 if (fAttrLayout == 0 && !fNY.empty()) {
415 out << SP << fType <<
" *" <<
OpName <<
"_hidden_state = tensor_" << fNY <<
";\n";
418 out << SP << fType <<
" * " <<
OpName <<
"_hidden_state = this->fVec_" <<
OpName <<
"_hidden_state;\n";
420 out << SP << fType <<
" " <<
OpName <<
"_hidden_state["
424 out << SP <<
"char " <<
OpName <<
"_transA = 'N';\n";
425 out << SP <<
"char " <<
OpName <<
"_transB = 'T';\n";
427 out << SP <<
"int " <<
OpName <<
"_n = " << fAttrHiddenSize <<
";\n";
429 if (fType ==
"float") {
430 out << SP <<
"float " <<
OpName <<
"_alpha = 1.;\n";
431 out << SP <<
"float " <<
OpName <<
"_beta = .0;\n";
435 out << SP <<
"int " <<
OpName <<
"_incx = 1;\n";
436 out << SP <<
"int " <<
OpName <<
"_incy = 1;\n";
441 if (fType ==
"float") {
443 out << SP <<
"BLAS::sgemm_(&" <<
OpName <<
"_transB, &" <<
OpName <<
"_transA, &" <<
OpName <<
"_n, &"
446 <<
"_feedforward, &" <<
OpName <<
"_n);\n";
448 out << SP <<
"size_t " <<
OpName <<
"_w_offset = " << fAttrHiddenSize *
input_size <<
";\n";
449 out << SP <<
"BLAS::sgemm_(&" <<
OpName <<
"_transB, &" <<
OpName <<
"_transA, &" <<
OpName <<
"_n, &"
452 <<
"_beta, " <<
OpName <<
"_feedforward, &" <<
OpName <<
"_n);\n";
457 if (fType ==
"float") {
459 out << SP <<
"BLAS::saxpy_(&" <<
OpName <<
"_bias_size, &" <<
OpName <<
"_alpha, tensor_" << fNB <<
", &"
464 out << SP <<
"BLAS::saxpy_(&" <<
OpName <<
"_bias_size, &" <<
OpName <<
"_alpha, tensor_" << fNB <<
" + "
472 out << SP <<
"for (size_t seq = 0; seq < " <<
seq_length <<
"; seq++) {\n";
473 out << SP << SP <<
"size_t offset = seq * " <<
batch_size * fAttrHiddenSize <<
";\n";
474 out << SP << SP <<
"size_t size = " <<
batch_size * fAttrHiddenSize <<
";\n";
477 out << SP << SP <<
"std::copy(" <<
OpName <<
"_feedforward + offset, " <<
OpName
478 <<
"_feedforward + offset + size, " <<
OpName <<
"_hidden_state + h_offset);\n";
481 out << SP <<
"for (size_t seq = 0; seq < " <<
seq_length <<
"; seq++) {\n";
482 if (fAttrDirection ==
"backward" ||
direction == 1) {
483 out << SP << SP <<
"size_t index = " <<
seq_length - 1 <<
" - seq;\n";
485 out << SP << SP <<
"size_t index = seq;\n";
488 out << SP << SP <<
"int m2 = " <<
batch_size <<
";\n";
491 out << SP << SP <<
"size_t size = " <<
batch_size * fAttrHiddenSize <<
";\n";
492 out << SP << SP <<
"if (seq == 0) {\n";
493 if (!fNInitial_h.empty()) {
495 out << SP << SP << SP <<
"size_t r_offset = " <<
direction * fAttrHiddenSize * fAttrHiddenSize <<
";\n";
496 out << SP << SP << SP <<
"size_t initial_hidden_state_offset = " <<
direction *
batch_size * fAttrHiddenSize
498 if (fType ==
"float") {
499 out << SP << SP << SP <<
"BLAS::sgemm_(&" <<
OpName <<
"_transB, &" <<
OpName <<
"_transA, &" <<
OpName
500 <<
"_n, &m2, &" <<
OpName <<
"_n, &" <<
OpName <<
"_alpha, tensor_" << fNR <<
" + r_offset, &" <<
OpName
501 <<
"_n, " <<
OpName <<
"_initial_hidden_state + initial_hidden_state_offset, &" <<
OpName <<
"_n, &"
502 <<
OpName <<
"_alpha, " <<
OpName <<
"_hidden_state + offset, &" <<
OpName <<
"_n);\n";
505 out << SP << SP <<
"} else {\n";
507 out << SP << SP << SP <<
"size_t r_offset = " <<
direction * fAttrHiddenSize * fAttrHiddenSize <<
";\n";
508 if (fAttrDirection ==
"backward" ||
direction == 1) {
509 out << SP << SP << SP <<
"size_t previous_offset = (index + 1) * "
513 out << SP << SP << SP <<
"size_t previous_offset = (seq - 1) * "
517 if (fType ==
"float") {
518 out << SP << SP << SP <<
"BLAS::sgemm_(&" <<
OpName <<
"_transB, &" <<
OpName <<
"_transA, &" <<
OpName
519 <<
"_n, &m2, &" <<
OpName <<
"_n, &" <<
OpName <<
"_alpha, tensor_" << fNR <<
" + r_offset, &" <<
OpName
520 <<
"_n, " <<
OpName <<
"_hidden_state + previous_offset, &" <<
OpName <<
"_n, &" <<
OpName <<
"_alpha, "
521 <<
OpName <<
"_hidden_state + offset, &" <<
OpName <<
"_n);\n";
523 out << SP << SP <<
"}\n";
526 if (fAttrClip > .0) {
527 out << SP << SP <<
"for (size_t i = offset; i < offset + size; i++) {\n";
528 if (fType ==
"float") {
529 out << SP << SP << SP <<
"float x = (" <<
OpName <<
"_hidden_state[i] > " << -fAttrClip <<
") ? " <<
OpName
530 <<
"_hidden_state[i] : " << -fAttrClip <<
";\n";
532 out << SP << SP << SP <<
OpName <<
"_hidden_state[i] = (x < " << fAttrClip <<
") ? x : " << fAttrClip <<
";\n";
533 out << SP << SP <<
"}\n";
537 if (fAttrActivations[
direction] ==
"Relu") {
538 out << SP << SP <<
"for (size_t i = offset; i < offset + size; i++) {\n";
539 out << SP << SP << SP <<
"if (" <<
OpName <<
"_hidden_state[i] < 0.)\n";
540 out << SP << SP << SP << SP <<
OpName <<
"_hidden_state[i] = 0.;\n";
541 out << SP << SP <<
"}\n";
542 }
else if (fAttrActivations[
direction] ==
"Tanh") {
543 out << SP << SP <<
"for (size_t i = offset; i < offset + size; i++) {\n";
544 if (fType ==
"float") {
545 out << SP << SP << SP <<
"float ex = std::exp(-2 * " <<
OpName <<
"_hidden_state[i]);\n";
547 out << SP << SP << SP << SP <<
OpName <<
"_hidden_state[i] = (1. - ex) / (1. + ex);\n";
548 out << SP << SP <<
"}\n";
549 }
else if (fAttrActivations[
direction] ==
"Sigmoid") {
550 out << SP << SP <<
"for (size_t i = offset; i < offset + size; i++) {\n";
551 out << SP << SP << SP << SP <<
OpName <<
"_hidden_state[i] = 1. / (1. + std::exp(-" <<
OpName
552 <<
"_hidden_state[i]));\n";
553 out << SP << SP <<
"}\n";
554 }
else if (fAttrActivations[
direction] ==
"Affine") {
555 out << SP << SP <<
"for (size_t i = offset; i < offset + size; i++) {\n";
556 out << SP << SP << SP << SP <<
OpName <<
"_hidden_state[i] = " << fAttrActivationAlpha[
direction] <<
" * "
557 <<
OpName <<
"_hidden_state[i] + " << fAttrActivationBeta[
direction] <<
";\n";
558 out << SP << SP <<
"}\n";
559 }
else if (fAttrActivations[
direction] ==
"ScaledTanh") {
560 out << SP << SP <<
"for (size_t i = offset; i < offset + size; i++) {\n";
561 if (fType ==
"float") {
562 out << SP << SP << SP <<
"float ex = std::exp(-2 * " << fAttrActivationBeta[
direction] <<
" * " <<
OpName
563 <<
"_hidden_state[i]);\n";
565 out << SP << SP << SP << SP <<
OpName <<
"_hidden_state[i] = " << fAttrActivationAlpha[
direction]
566 <<
" * (1. - ex) / (1. + ex);\n";
567 out << SP << SP <<
"}\n";
568 }
else if (fAttrActivations[
direction] ==
"HardSigmoid") {
569 out << SP << SP <<
"for (size_t i = offset; i < offset + size; i++) {\n";
570 if (fType ==
"float") {
571 out << SP << SP << SP <<
"float a = " << fAttrActivationAlpha[
direction] <<
" * " <<
OpName
572 <<
"_hidden_state[i] + " << fAttrActivationBeta[
direction] <<
";\n";
573 out << SP << SP << SP <<
"float b = (a > 0.) ? a : 0.;\n";
575 out << SP << SP << SP << SP <<
OpName <<
"_hidden_state[i] = (b < 1.) ? b : 1.;\n";
576 out << SP << SP <<
"}\n";
577 }
else if (fAttrActivations[
direction] ==
"LeakyRelu") {
578 out << SP << SP <<
"for (size_t i = offset; i < offset + size; i++) {\n";
579 out << SP << SP << SP <<
"if (" <<
OpName <<
"_hidden_state[i] < 0.)\n";
580 out << SP << SP << SP << SP <<
OpName <<
"_hidden_state[i] = " << fAttrActivationAlpha[
direction] <<
" * "
581 <<
OpName <<
"_hidden_state[i];\n";
582 out << SP << SP <<
"}\n";
583 }
else if (fAttrActivations[
direction] ==
"ThresholdRelu") {
584 out << SP << SP <<
"for (size_t i = offset; i < offset + size; i++) {\n";
585 out << SP << SP << SP <<
"if (" <<
OpName <<
"_hidden_state[i] < " << fAttrActivationAlpha[
direction] <<
")\n";
586 out << SP << SP << SP << SP <<
OpName <<
"_hidden_state[i] = 0.;\n";
587 out << SP << SP <<
"}";
588 }
else if (fAttrActivations[
direction] ==
"Elu") {
589 out << SP << SP <<
"for (size_t i = offset; i < offset + size; i++) {\n";
590 out << SP << SP << SP <<
"if (" <<
OpName <<
"_hidden_state[i] < 0.)\n";
591 out << SP << SP << SP << SP <<
OpName <<
"_hidden_state[i] = " << fAttrActivationAlpha[
direction]
592 <<
" * std::exp(" <<
OpName <<
"_hidden_state[i] - 1.);\n";
593 out << SP << SP <<
"}\n";
594 }
else if (fAttrActivations[
direction] ==
"Softsign") {
595 out << SP << SP <<
"for (size_t i = offset; i < offset + size; i++) {\n";
596 out << SP << SP << SP << SP <<
OpName <<
"_hidden_state[i] = " <<
OpName <<
"_hidden_state[i] / (1. + abs("
597 <<
OpName <<
"_hidden_state[i]));\n";
598 out << SP << SP <<
"}\n";
600 out << SP << SP <<
"for (size_t i = offset; i < offset + size; i++) {\n";
601 out << SP << SP << SP << SP <<
OpName <<
"_hidden_state[i] = log(1. + std::exp(" <<
OpName
602 <<
"_hidden_state[i]));\n";
603 out << SP << SP <<
"}\n";
610 if (!fNSequence_lens.empty()) {
611 out << SP <<
"for (size_t seq = 0; seq < " <<
seq_length <<
"; seq++) {\n";
612 out << SP << SP <<
"for (size_t batch = 0; batch < " <<
batch_size <<
"; batch++) {\n";
613 out << SP << SP << SP <<
"if (seq >= tensor_" << fNSequence_lens <<
"[batch]) {\n";
614 out << SP << SP << SP << SP <<
"for (size_t h = 0; h < " << fAttrHiddenSize <<
"; h++) {\n";
616 out << SP << SP << SP << SP << SP <<
OpName <<
"_hidden_state[seq * "
619 out << SP << SP << SP << SP << SP <<
OpName <<
"_hidden_state[seq * "
621 out << SP << SP << SP << SP << SP <<
OpName <<
"_hidden_state[seq * "
623 << fAttrHiddenSize <<
" + h] = 0.;\n";
625 out << SP << SP << SP << SP <<
"}\n";
626 out << SP << SP << SP <<
"}\n";
627 out << SP << SP <<
"}\n";
632 if (fAttrLayout == 0) {
633 if (!fNY_h.empty()) {
634 if (fNSequence_lens.empty()) {
636 if (fAttrDirection ==
"backward") {
637 out << SP <<
"std::copy(" <<
OpName <<
"_hidden_state, " <<
OpName <<
"_hidden_state + " <<
yh_size
638 <<
", tensor_" << fNY_h <<
");\n";
642 <<
"_hidden_state + " <<
offset <<
" + " <<
yh_size <<
", tensor_" << fNY_h <<
");\n";
646 <<
"_hidden_state + " << 2 *
yh_size <<
", tensor_" << fNY_h <<
" + " <<
yh_size <<
");\n";
649 if (fAttrDirection ==
"backward") {
650 out << SP <<
"for (size_t batch = 0; batch < " <<
batch_size <<
"; batch++) {\n";
651 out << SP << SP <<
"size_t offset = batch * " << fAttrHiddenSize <<
";\n";
652 out << SP << SP <<
"std::copy(" <<
OpName <<
"_hidden_state + offset, " <<
OpName
653 <<
"_hidden_state + offset + " << fAttrHiddenSize <<
", tensor_" << fNY_h <<
" + offset);\n";
656 out << SP <<
"for (size_t batch = 0; batch < " <<
batch_size <<
"; batch++) {\n";
657 out << SP << SP <<
"size_t seq = " <<
"tensor_" << fNSequence_lens <<
"[batch] - 1;\n";
659 <<
" + batch * " << fAttrHiddenSize <<
";\n";
660 out << SP << SP <<
"size_t yh_offset = batch * " << fAttrHiddenSize <<
";\n";
661 out << SP << SP <<
"std::copy(" <<
OpName <<
"_hidden_state + offset, " <<
OpName
662 <<
"_hidden_state + offset + " << fAttrHiddenSize <<
", tensor_" << fNY_h <<
" + yh_offset);\n";
666 out << SP <<
"for (size_t batch = 0; batch < " <<
batch_size <<
"; batch++) {\n";
667 out << SP << SP <<
"size_t offset = " <<
batch_size * fAttrHiddenSize <<
" + batch * " << fAttrHiddenSize
669 out << SP << SP <<
"size_t yh_offset = " <<
batch_size * fAttrHiddenSize <<
" + batch * "
670 << fAttrHiddenSize <<
";\n";
671 out << SP << SP <<
"std::copy(" <<
OpName <<
"_hidden_state + offset, " <<
OpName
672 <<
"_hidden_state + offset + " << fAttrHiddenSize <<
", tensor_" << fNY_h <<
" + yh_offset);\n";
680 out << SP <<
"for (size_t seq = 0; seq < " <<
seq_length <<
"; seq++) {\n";
681 out << SP << SP <<
"for (size_t batch = 0; batch < " <<
batch_size <<
"; batch++) {\n";
686 out << SP << SP << SP <<
"std::copy(" <<
OpName <<
"_hidden_state + offset, " <<
OpName
687 <<
"_hidden_state + offset + " << fAttrHiddenSize <<
", tensor_" << fNY <<
" + y_offset);\n";
688 out << SP << SP <<
"}\n";
692 if (!fNY_h.empty()) {
693 if (fAttrDirection ==
"backward") {
694 out << SP <<
"for (size_t batch = 0; batch < " <<
batch_size <<
"; batch++) {\n";
695 out << SP << SP <<
"size_t offset = batch * " << fAttrHiddenSize <<
";\n";
696 out << SP << SP <<
"size_t yh_offset = batch * " <<
num_directions * fAttrHiddenSize <<
";\n";
697 out << SP << SP <<
"std::copy(" <<
OpName <<
"_hidden_state + offset, " <<
OpName
698 <<
"_hidden_state + offset + " << fAttrHiddenSize <<
", tensor_" << fNY_h <<
" + yh_offset);\n";
701 out << SP <<
"for (size_t batch = 0; batch < " <<
batch_size <<
"; batch++) {\n";
702 if (fNSequence_lens.empty()) {
703 out << SP << SP <<
"size_t seq = " <<
seq_length - 1 <<
";\n";
705 out << SP << SP <<
"size_t seq = " <<
"tensor_" << fNSequence_lens <<
"[batch] - 1;\n";
708 <<
" + batch * " << fAttrHiddenSize <<
";\n";
709 out << SP << SP <<
"size_t yh_offset = batch * " <<
num_directions * fAttrHiddenSize <<
";\n";
710 out << SP << SP <<
"std::copy(" <<
OpName <<
"_hidden_state + offset, " <<
OpName
711 <<
"_hidden_state + offset + " << fAttrHiddenSize <<
", tensor_" << fNY_h <<
" + yh_offset);\n";
715 out << SP <<
"for (size_t batch = 0; batch < " <<
batch_size <<
"; batch++) {\n";
716 out << SP << SP <<
"size_t offset = " <<
batch_size * fAttrHiddenSize <<
" + batch * " << fAttrHiddenSize
718 out << SP << SP <<
"size_t yh_offset = batch * " <<
num_directions * fAttrHiddenSize <<
" + "
719 << fAttrHiddenSize <<
";\n";
720 out << SP << SP <<
"std::copy(" <<
OpName <<
"_hidden_state + offset, " <<
OpName
721 <<
"_hidden_state + offset + " << fAttrHiddenSize <<
", tensor_" << fNY_h <<
" + yh_offset);\n";
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
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::string fNB
Name of the bias.
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.
float fAttrClip
Clip threshold.
std::string fType
Type of the tensors.
size_t fAttrLayout
Data layout.
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
std::vector< std::string_view > fOutputTensorNames
static uint64_t sum(uint64_t i)