OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[ONNX FE] Support "layout" attribute for RNN, GRU and LSTM (#38183)
Andrii Staikovfc3722f372ce
diff --git a/src/frontends/onnx/frontend/src/op/gru.cpp b/src/frontends/onnx/frontend/src/op/gru.cppindex c562dad4df2..1eb8482cd0c 100644--- a/src/frontends/onnx/frontend/src/op/gru.cpp+++ b/src/frontends/onnx/frontend/src/op/gru.cpp@@ -98,6 +98,11 @@ ov::OutputVector gru(const ov::frontend::onnx::Node& node) {const auto Y = gru_sequence->output(0);const auto Y_h = gru_sequence->output(1);+ if (attributes.m_layout == 1) {+ // OV [batch, num_directions, seq, hidden] -> ONNX [batch, seq, num_directions, hidden]; Y_h already matches.+ return {ov::op::util::reorder_axes(Y, {0, 2, 1, 3}), Y_h};+ }+return {ov::op::util::reorder_axes(Y, {2, 1, 0, 3}), ov::op::util::reorder_axes(Y_h, {1, 0, 2})};}ONNX_OP("GRU", OPSET_SINCE(1), ai_onnx::opset_1::gru);diff --git a/src/frontends/onnx/frontend/src/op/lstm.cpp b/src/frontends/onnx/frontend/src/op/lstm.cppindex af8efc2b6c5..77668c36092 100644--- a/src/frontends/onnx/frontend/src/op/lstm.cpp+++ b/src/frontends/onnx/frontend/src/op/lstm.cpp@@ -38,6 +38,9 @@ using ov::frontend::onnx::recurrent::normalize_tensor_rank;struct LSTMNgInputMap {explicit LSTMNgInputMap(const Node& node) {+ const auto layout = node.get_attribute_value<std::int64_t>("layout", 0);+ recurrent::validate_layout(node, layout);+const auto& ng_inputs = node.get_ov_inputs();// We have input, output, forget and cell gatesconstexpr std::size_t gates_count{4};@@ -45,14 +48,17 @@ struct LSTMNgInputMap {// ----- Mandatory inputs ------// Packed input sequences.- // ONNX Shape: [seq_length, batch_size, input_size]- // OpenVino Shape: [batch_size, seq_length, input_size]+ // ONNX Shape: [seq_length, batch_size, input_size] (layout=0) or+ // [batch_size, seq_length, input_size] (layout=1)+ // OpenVINO Shape: [batch_size, seq_length, input_size]// First normalize rank if needed, THEN reorder axes// This is important because Squeeze/Unsqueeze changes dimension indicesauto input_x = ng_inputs.at(0);input_x = normalize_tensor_rank(input_x, 3, "LSTM", "X");- input_x = ov::op::util::reorder_axes(input_x, {1, 0, 2});+ if (layout == 0) {+ input_x = ov::op::util::reorder_axes(input_x, {1, 0, 2});+ }m_input_map[LSTMInput::LSTM_INPUT_X] = input_x;@@ -100,7 +106,7 @@ struct LSTMNgInputMap {// ------ Optional inputs ------// `B` - The bias tensor for input gate.// ONNX Shape: [num_directions, 8*hidden_size]- // OpenVino Shape: [num_directions, 4*hidden_size]+ // OpenVINO Shape: [num_directions, 4*hidden_size]if (ng_inputs.size() > 3 && !ov::op::util::is_null(ng_inputs.at(3))) {auto bias = normalize_tensor_rank(ng_inputs.at(3), 2, "LSTM", "B");auto split_bias = ov::op::util::make_split(bias, 2, 1);@@ -122,13 +128,16 @@ struct LSTMNgInputMap {m_input_map[LSTMInput::LSTM_INPUT_SEQ_LENGTHS] = ov::frontend::onnx::recurrent::default_sequence_lens(dims);}// `initial_h` - The initial value of the hidden.- // ONNX Shape: [num_directions, batch_size, hidden_size]- // OpenVino Shape: [batch_size, num_directions, hidden_size]+ // ONNX Shape: [num_directions, batch_size, hidden_size] (layout=0) or+ // [batch_size, num_directions, hidden_size] (layout=1)+ // OpenVINO Shape: [batch_size, num_directions, hidden_size]if (ng_inputs.size() > 5 && !ov::op::util::is_null(ng_inputs.at(5))) {auto init_h = ng_inputs.at(5);// First normalize rank, THEN reorder axesinit_h = normalize_tensor_rank(init_h, 3, "LSTM", "initial_h");- init_h = ov::op::util::reorder_axes(init_h, {1, 0, 2});+ if (layout == 0) {+ init_h = ov::op::util::reorder_axes(init_h, {1, 0, 2});+ }m_input_map[LSTMInput::LSTM_INPUT_INIT_H] = init_h;} else {@@ -136,13 +145,16 @@ struct LSTMNgInputMap {ov::frontend::onnx::recurrent::default_initial_state(dims, x_type);}// `initial_c` - The initial value of the cell.- // ONNX Shape: [num_directions, batch_size, hidden_size]- // OpenVino Shape: [batch_size, num_directions, hidden_size]+ // ONNX Shape: [num_directions, batch_size, hidden_size] (layout=0) or+ // [batch_size, num_directions, hidden_size] (layout=1)+ // OpenVINO Shape: [batch_size, num_directions, hidden_size]if (ng_inputs.size() > 6 && !ov::op::util::is_null(ng_inputs.at(6))) {auto init_c = ng_inputs.at(6);// First normalize rank, THEN reorder axesinit_c = normalize_tensor_rank(init_c, 3, "LSTM", "initial_c");- init_c = ov::op::util::reorder_axes(init_c, {1, 0, 2});+ if (layout == 0) {+ init_c = ov::op::util::reorder_axes(init_c, {1, 0, 2});+ }m_input_map[LSTMInput::LSTM_INPUT_INIT_C] = init_c;} else {@@ -151,7 +163,7 @@ struct LSTMNgInputMap {}// `P` - The weight tensor for peepholes.// ONNX Shape: [num_directions, 3*hidden_size]- // OpenVino Shape: [num_directions, 4*hidden_size]+ // OpenVINO Shape: [num_directions, 4*hidden_size]if (ng_inputs.size() > 7 && !ov::op::util::is_null(ng_inputs.at(7))) {auto peepholes = normalize_tensor_rank(ng_inputs.at(7), 2, "LSTM", "P");m_input_map[LSTMInput::LSTM_INPUT_P] =@@ -187,7 +199,9 @@ struct LSTMAttributes {,m_activation_alpha{node.get_attribute_value<std::vector<float>>("activation_alpha", std::vector<float>{})},m_activation_beta{node.get_attribute_value<std::vector<float>>("activation_beta", std::vector<float>{})},- m_input_forget{static_cast<bool>(node.get_attribute_value<std::int64_t>("input_forget", 0))} {+ m_input_forget{static_cast<bool>(node.get_attribute_value<std::int64_t>("input_forget", 0))},+ m_layout{node.get_attribute_value<std::int64_t>("layout", 0)} {+ recurrent::validate_layout(node, m_layout);m_clip_threshold = std::abs(m_clip_threshold);std::string direction = ov::util::to_lower(node.get_attribute_value<std::string>("direction", "forward"));@@ -202,6 +216,7 @@ struct LSTMAttributes {std::vector<float> m_activation_alpha;std::vector<float> m_activation_beta;bool m_input_forget;+ std::int64_t m_layout;};} // anonymous namespace@@ -241,10 +256,20 @@ ov::OutputVector lstm(const ov::frontend::onnx::Node& node) {auto Y_h_squeezed = std::make_shared<v0::Squeeze>(Y_h, num_dir_axis);auto Y_c_squeezed = std::make_shared<v0::Squeeze>(Y_c, num_dir_axis);+ if (attributes.m_layout == 1) {+ return {Y_squeezed, Y_h_squeezed, Y_c_squeezed};+ }+// Y: [batch_size, seq_length, hidden_size] -> [seq_length, batch_size, hidden_size]return {ov::op::util::reorder_axes(Y_squeezed, {1, 0, 2}), Y_h_squeezed, Y_c_squeezed};}+ if (attributes.m_layout == 1) {+ // OV [batch, num_directions, seq, hidden] -> ONNX [batch, seq, num_directions, hidden];+ // Y_h and Y_c already match.+ return {ov::op::util::reorder_axes(Y, {0, 2, 1, 3}), Y_h, Y_c};+ }+return {ov::op::util::reorder_axes(Y, {2, 1, 0, 3}),ov::op::util::reorder_axes(Y_h, {1, 0, 2}),ov::op::util::reorder_axes(Y_c, {1, 0, 2})};diff --git a/src/frontends/onnx/frontend/src/op/rnn.cpp b/src/frontends/onnx/frontend/src/op/rnn.cppindex 6d0b194cdc0..3e88b3ab6a5 100644--- a/src/frontends/onnx/frontend/src/op/rnn.cpp+++ b/src/frontends/onnx/frontend/src/op/rnn.cpp@@ -48,6 +48,11 @@ ov::OutputVector rnn(const ov::frontend::onnx::Node& node) {const auto Y = rnn_sequence->output(0);const auto Y_h = rnn_sequence->output(1);+ if (attributes.m_layout == 1) {+ // OV [batch, num_directions, seq, hidden] -> ONNX [batch, seq, num_directions, hidden]; Y_h already matches.+ return {ov::op::util::reorder_axes(Y, {0, 2, 1, 3}), Y_h};+ }+return {ov::op::util::reorder_axes(Y, {2, 1, 0, 3}), ov::op::util::reorder_axes(Y_h, {1, 0, 2})};}ONNX_OP("RNN", OPSET_SINCE(1), ai_onnx::opset_1::rnn);diff --git a/src/frontends/onnx/frontend/src/utils/recurrent.cpp b/src/frontends/onnx/frontend/src/utils/recurrent.cppindex 84a50a4db7a..1accabf04bd 100644--- a/src/frontends/onnx/frontend/src/utils/recurrent.cpp+++ b/src/frontends/onnx/frontend/src/utils/recurrent.cpp@@ -10,6 +10,7 @@#include <vector>#include "core/null_node.hpp"+#include "exceptions.hpp"#include "openvino/core/enum_names.hpp"#include "openvino/op/add.hpp"#include "openvino/op/broadcast.hpp"@@ -133,9 +134,16 @@ ov::Output<ov::Node> default_initial_state(const LSTMDimensions& dims, const ov:}OpInputMap::OpInputMap(const ov::frontend::onnx::Node& node, std::size_t gates_count) {+ const auto layout = node.get_attribute_value<std::int64_t>("layout", 0);+ validate_layout(node, layout);+const auto& ng_inputs = node.get_ov_inputs();- m_map[OpInput::X] = ov::op::util::reorder_axes(ng_inputs.at(0), {1, 0, 2});+ // ONNX layout=1 keeps X as [batch, seq, input], which already matches the OV layout.+ m_map[OpInput::X] = ng_inputs.at(0);+ if (layout == 0) {+ m_map[OpInput::X] = ov::op::util::reorder_axes(m_map[OpInput::X], {1, 0, 2});+ }m_map[OpInput::W] = ng_inputs.at(1);m_map[OpInput::R] = ng_inputs.at(2);@@ -159,7 +167,11 @@ OpInputMap::OpInputMap(const ov::frontend::onnx::Node& node, std::size_t gates_c}// The initial value of the hidden.if (ng_inputs.size() > 5 && !ov::op::util::is_null(ng_inputs.at(5))) {- m_map[OpInput::INIT_H] = ov::op::util::reorder_axes(ng_inputs.at(5), {1, 0, 2});+ // ONNX layout=1 keeps initial_h as [batch, num_directions, hidden], which already matches the OV layout.+ m_map[OpInput::INIT_H] = ng_inputs.at(5);+ if (layout == 0) {+ m_map[OpInput::INIT_H] = ov::op::util::reorder_axes(m_map[OpInput::INIT_H], {1, 0, 2});+ }} else {m_map[OpInput::INIT_H] = default_initial_state(dims, x_type);}@@ -187,7 +199,9 @@ OpAttributes::OpAttributes(const Node& node)// as for corresponding ONNX operator.,m_activations_alpha{node.get_attribute_value<std::vector<float>>("activation_alpha", std::vector<float>{})},- m_activations_beta{node.get_attribute_value<std::vector<float>>("activation_beta", std::vector<float>{})} {+ m_activations_beta{node.get_attribute_value<std::vector<float>>("activation_beta", std::vector<float>{})},+ m_layout{node.get_attribute_value<std::int64_t>("layout", 0)} {+ validate_layout(node, m_layout);m_clip_threshold = std::abs(m_clip_threshold);std::string direction = ov::util::to_lower(node.get_attribute_value<std::string>("direction", "forward"));m_direction = ov::as_enum<ov::op::RecurrentSequenceDirection>(direction);diff --git a/src/frontends/onnx/frontend/src/utils/recurrent.hpp b/src/frontends/onnx/frontend/src/utils/recurrent.hppindex 8cf9944017b..484dc211a0e 100644--- a/src/frontends/onnx/frontend/src/utils/recurrent.hpp+++ b/src/frontends/onnx/frontend/src/utils/recurrent.hpp@@ -4,12 +4,14 @@#pragma once+#include <cstdint>#include <functional>#include <map>#include <memory>#include <string>#include "core/node.hpp"+#include "exceptions.hpp"#include "openvino/core/node.hpp"#include "openvino/core/type/element_type.hpp"#include "openvino/op/util/attr_types.hpp"@@ -28,6 +30,15 @@ ov::Output<ov::Node> normalize_tensor_rank(const ov::Output<ov::Node>& input,const std::string& op_name,const std::string& input_name);+// The `layout` attribute introduced in opset 14 accepts only 0 and 1+inline void validate_layout(const Node& node, std::int64_t layout) {+ CHECK_VALID_NODE(node,+ layout == 0 || layout == 1,+ "Unsupported value of the 'layout' attribute: ",+ layout,+ ". Only 0 and 1 are allowed.");+}+// Runtime dimension values extracted from OV-layout X [batch, seq, input]// and R [num_dir, gates*hidden, hidden]. Each member is a rank-1 i64 node.struct LSTMDimensions {@@ -101,6 +112,7 @@ struct OpAttributes {std::vector<std::string> m_activations;std::vector<float> m_activations_alpha;std::vector<float> m_activations_beta;+ std::int64_t m_layout;};} // namespace recurrentdiff --git a/src/frontends/onnx/tests/__init__.py b/src/frontends/onnx/tests/__init__.pyindex 36634822c29..281252411f1 100644--- a/src/frontends/onnx/tests/__init__.py+++ b/src/frontends/onnx/tests/__init__.py@@ -113,8 +113,6 @@ xfail_issue_58676 = xfail_test(reason="AssertionError: Not equal to tolerance rtskip_issue_58676 = pytest.mark.skip(reason="AssertionError: Not equal to tolerance rtol=0.001, atol=1e-07")xfail_issue_onnx_models_140 = xfail_test(reason="https://github.com/onnx/models/issues/140")-xfail_issue_63043 = xfail_test(reason="Recurrent node expects constants as W, R, B inputs.")-skip_rng_tests = pytest.mark.skip(reason="Tests use random number generator with no seed.")xfail_issue_63137 = xfail_test(reason="Unsupported operations: OptionalHasElement, OptionalGetElement")xfail_issue_68212 = xfail_test(reason="Unsupported reading model with bytes streams")diff --git a/src/frontends/onnx/tests/convert_tests.cpp b/src/frontends/onnx/tests/convert_tests.cppindex 936896fc366..457579a3550 100644--- a/src/frontends/onnx/tests/convert_tests.cpp+++ b/src/frontends/onnx/tests/convert_tests.cpp@@ -41,6 +41,12 @@ TEST(ONNXFeConvertException, exception_if_other_translation_exception) {testing::HasSubstr("only 'DCR' and 'CRD' modes are supported"));}+TEST(ONNXFeConvertException, exception_if_unsupported_recurrent_layout) {+ OV_EXPECT_THROW(convert_model("rnn_layout_invalid.onnx"),+ ov::AssertFailure,+ testing::HasSubstr("Unsupported value of the 'layout' attribute: 2"));+}+TEST(ONNXFeConvertException, exception_if_both_unsupported_and_other_translation_exception) {OV_EXPECT_THROW(convert_model("unsupported_ops/unsupported_add_and_incorrect_dts.onnx"),ov::AssertFailure,diff --git a/src/frontends/onnx/tests/models/gru_fwd_bias_initial_h_layout_1.prototxt b/src/frontends/onnx/tests/models/gru_fwd_bias_initial_h_layout_1.prototxtnew file mode 100644index 00000000000..5121de52267--- /dev/null+++ b/src/frontends/onnx/tests/models/gru_fwd_bias_initial_h_layout_1.prototxt@@ -0,0 +1,162 @@+ir_version: 7+producer_name: "OpenVINO ONNX Frontend"+graph {+ node {+ input: "X"+ input: "W"+ input: "R"+ input: "B"+ input: ""+ input: "initial_h"+ output: "Y"+ output: "Y_h"+ op_type: "GRU"+ attribute {+ name: "hidden_size"+ i: 5+ type: INT+ }+ attribute {+ name: "layout"+ i: 1+ type: INT+ }+ }+ name: "test_gru_fwd_bias_initial_h_layout_1"+ input {+ name: "X"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 3 # batch size+ }+ dim {+ dim_value: 4 # seq_length+ }+ dim {+ dim_value: 2 # input size+ }+ }+ }+ }+ }+ input {+ name: "W"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 15 # gates_count*hidden_size+ }+ dim {+ dim_value: 2 # input_size+ }+ }+ }+ }+ }+ input {+ name: "R"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 15 # gates_count*hidden_size+ }+ dim {+ dim_value: 5 # hidden_size+ }+ }+ }+ }+ }+ input {+ name: "B"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 30 # gates_count*hidden_size+ }+ }+ }+ }+ }+ input {+ name: "initial_h"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 3 # batch size+ }+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 5 # hidden size+ }+ }+ }+ }+ }+ output {+ name: "Y"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 3 # batch_size+ }+ dim {+ dim_value: 4 # seq_length+ }+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 5 # hidden_size+ }+ }+ }+ }+ }+ output {+ name: "Y_h"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 3 # batch_size+ }+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 5 # hidden_size+ }+ }+ }+ }+ }+}+opset_import {+ version: 14+}diff --git a/src/frontends/onnx/tests/models/lstm_fwd_initial_h_c.prototxt b/src/frontends/onnx/tests/models/lstm_fwd_initial_h_c.prototxtnew file mode 100644index 00000000000..406760477b5--- /dev/null+++ b/src/frontends/onnx/tests/models/lstm_fwd_initial_h_c.prototxt@@ -0,0 +1,202 @@+ir_version: 7+producer_name: "OpenVINO ONNX Frontend"+graph {+ node {+ input: "X"+ input: "W"+ input: "R"+ input: "B"+ input: ""+ input: "initial_h"+ input: "initial_c"+ output: "Y"+ output: "Y_h"+ output: "Y_c"+ op_type: "LSTM"+ attribute {+ name: "direction"+ s: "forward"+ type: STRING+ }+ attribute {+ name: "hidden_size"+ i: 3+ type: INT+ }+ }+ name: "test_lstm_fwd_initial_h_c"+ input {+ name: "X"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 3 # seq_length+ }+ dim {+ dim_value: 2 # batch size+ }+ dim {+ dim_value: 1 # input size+ }+ }+ }+ }+ }+ input {+ name: "W"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 12 # gates_count*hidden_size+ }+ dim {+ dim_value: 1 # input size+ }+ }+ }+ }+ }+ input {+ name: "R"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 12 # gates_count*hidden_size+ }+ dim {+ dim_value: 3 # hidden size+ }+ }+ }+ }+ }+ input {+ name: "B"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 24 # 2*gates_count*hidden_size+ }+ }+ }+ }+ }+ input {+ name: "initial_h"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 2 # batch size+ }+ dim {+ dim_value: 3 # hidden size+ }+ }+ }+ }+ }+ input {+ name: "initial_c"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 2 # batch size+ }+ dim {+ dim_value: 3 # hidden size+ }+ }+ }+ }+ }+ output {+ name: "Y"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 3 # seq_length+ }+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 2 # batch size+ }+ dim {+ dim_value: 3 # hidden size+ }+ }+ }+ }+ }+ output {+ name: "Y_h"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 2 # batch size+ }+ dim {+ dim_value: 3 # hidden size+ }+ }+ }+ }+ }+ output {+ name: "Y_c"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 2 # batch size+ }+ dim {+ dim_value: 3 # hidden size+ }+ }+ }+ }+ }+}+opset_import {+ version: 14+}diff --git a/src/frontends/onnx/tests/models/lstm_fwd_initial_h_c_layout_1.prototxt b/src/frontends/onnx/tests/models/lstm_fwd_initial_h_c_layout_1.prototxtnew file mode 100644index 00000000000..de5962537d2--- /dev/null+++ b/src/frontends/onnx/tests/models/lstm_fwd_initial_h_c_layout_1.prototxt@@ -0,0 +1,207 @@+ir_version: 7+producer_name: "OpenVINO ONNX Frontend"+graph {+ node {+ input: "X"+ input: "W"+ input: "R"+ input: "B"+ input: ""+ input: "initial_h"+ input: "initial_c"+ output: "Y"+ output: "Y_h"+ output: "Y_c"+ op_type: "LSTM"+ attribute {+ name: "direction"+ s: "forward"+ type: STRING+ }+ attribute {+ name: "hidden_size"+ i: 3+ type: INT+ }+ attribute {+ name: "layout"+ i: 1+ type: INT+ }+ }+ name: "test_lstm_fwd_initial_h_c_layout_1"+ input {+ name: "X"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2 # batch size+ }+ dim {+ dim_value: 3 # seq_length+ }+ dim {+ dim_value: 1 # input size+ }+ }+ }+ }+ }+ input {+ name: "W"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 12 # gates_count*hidden_size+ }+ dim {+ dim_value: 1 # input size+ }+ }+ }+ }+ }+ input {+ name: "R"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 12 # gates_count*hidden_size+ }+ dim {+ dim_value: 3 # hidden size+ }+ }+ }+ }+ }+ input {+ name: "B"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 24 # 2*gates_count*hidden_size+ }+ }+ }+ }+ }+ input {+ name: "initial_h"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2 # batch size+ }+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 3 # hidden size+ }+ }+ }+ }+ }+ input {+ name: "initial_c"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2 # batch size+ }+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 3 # hidden size+ }+ }+ }+ }+ }+ output {+ name: "Y"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2 # batch size+ }+ dim {+ dim_value: 3 # seq_length+ }+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 3 # hidden size+ }+ }+ }+ }+ }+ output {+ name: "Y_h"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2 # batch size+ }+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 3 # hidden size+ }+ }+ }+ }+ }+ output {+ name: "Y_c"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2 # batch size+ }+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 3 # hidden size+ }+ }+ }+ }+ }+}+opset_import {+ version: 14+}diff --git a/src/frontends/onnx/tests/models/lstm_fwd_mixed_seq_layout_1.prototxt b/src/frontends/onnx/tests/models/lstm_fwd_mixed_seq_layout_1.prototxtnew file mode 100644index 00000000000..7573f7a7ff7--- /dev/null+++ b/src/frontends/onnx/tests/models/lstm_fwd_mixed_seq_layout_1.prototxt@@ -0,0 +1,193 @@+ir_version: 7+producer_name: "OpenVINO ONNX Frontend"+graph {+ node {+ input: "X"+ input: "W"+ input: "R"+ input: "B"+ input: "sequence_lens"+ input: ""+ input: ""+ input: ""+ output: "Y"+ output: "Y_h"+ output: "Y_c"+ op_type: "LSTM"+ attribute {+ name: "clip"+ f: 9999.0+ type: FLOAT+ }+ attribute {+ name: "direction"+ s: "forward"+ type: STRING+ }+ attribute {+ name: "hidden_size"+ i: 3+ type: INT+ }+ attribute {+ name: "input_forget"+ i: 0+ type: INT+ }+ attribute {+ name: "layout"+ i: 1+ type: INT+ }+ }+ name: "compute_graph"+ input {+ name: "X"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2 # batch size+ }+ dim {+ dim_value: 2 # seq_length+ }+ dim {+ dim_value: 1 # input size+ }+ }+ }+ }+ }+ input {+ name: "W"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 12+ }+ dim {+ dim_value: 1+ }+ }+ }+ }+ }+ input {+ name: "R"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 12+ }+ dim {+ dim_value: 3+ }+ }+ }+ }+ }+ input {+ name: "B"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 24+ }+ }+ }+ }+ }+ input {+ name: "sequence_lens"+ type {+ tensor_type {+ elem_type: 6+ shape {+ dim {+ dim_value: 2 # batch size+ }+ }+ }+ }+ }+ output {+ name: "Y"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2 # batch size+ }+ dim {+ dim_value: 2 # seq_length+ }+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 3 # hidden size+ }+ }+ }+ }+ }+ output {+ name: "Y_h"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2 # batch size+ }+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 3 # hidden size+ }+ }+ }+ }+ }+ output {+ name: "Y_c"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2 # batch size+ }+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 3 # hidden size+ }+ }+ }+ }+ }+}+opset_import {+ version: 14+}diff --git a/src/frontends/onnx/tests/models/rnn_bidirectional_layout_1.prototxt b/src/frontends/onnx/tests/models/rnn_bidirectional_layout_1.prototxtnew file mode 100644index 00000000000..82f2b41ef06--- /dev/null+++ b/src/frontends/onnx/tests/models/rnn_bidirectional_layout_1.prototxt@@ -0,0 +1,129 @@+ir_version: 7+producer_name: "OpenVINO ONNX Frontend"+graph {+ node {+ input: "X"+ input: "W"+ input: "R"+ output: "Y"+ output: "Y_h"+ op_type: "RNN"+ attribute {+ name: "hidden_size"+ i: 5+ type: INT+ }+ attribute {+ name: "direction"+ s: "bidirectional"+ type: STRING+ }+ attribute {+ name: "layout"+ i: 1+ type: INT+ }+ }+ name: "test_rnn_bidirectional_layout_1"+ input {+ name: "X"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 3 # batch size+ }+ dim {+ dim_value: 4 # seq_length+ }+ dim {+ dim_value: 2 # input size+ }+ }+ }+ }+ }+ input {+ name: "W"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2 # num_directions+ }+ dim {+ dim_value: 5 # gates_count*hidden_size+ }+ dim {+ dim_value: 2 # input_size+ }+ }+ }+ }+ }+ input {+ name: "R"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2 # num_directions+ }+ dim {+ dim_value: 5 # gates_count*hidden_size+ }+ dim {+ dim_value: 5 # hidden_size+ }+ }+ }+ }+ }+ output {+ name: "Y"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 3 # batch_size+ }+ dim {+ dim_value: 4 # seq_length+ }+ dim {+ dim_value: 2 # num_directions+ }+ dim {+ dim_value: 5 # hidden_size+ }+ }+ }+ }+ }+ output {+ name: "Y_h"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 3 # batch_size+ }+ dim {+ dim_value: 2 # num_directions+ }+ dim {+ dim_value: 5 # hidden_size+ }+ }+ }+ }+ }+}+opset_import {+ version: 14+}diff --git a/src/frontends/onnx/tests/models/rnn_fwd_bias_initial_h_layout_1.prototxt b/src/frontends/onnx/tests/models/rnn_fwd_bias_initial_h_layout_1.prototxtnew file mode 100644index 00000000000..8216f0f6483--- /dev/null+++ b/src/frontends/onnx/tests/models/rnn_fwd_bias_initial_h_layout_1.prototxt@@ -0,0 +1,162 @@+ir_version: 7+producer_name: "OpenVINO ONNX Frontend"+graph {+ node {+ input: "X"+ input: "W"+ input: "R"+ input: "B"+ input: ""+ input: "initial_h"+ output: "Y"+ output: "Y_h"+ op_type: "RNN"+ attribute {+ name: "hidden_size"+ i: 5+ type: INT+ }+ attribute {+ name: "layout"+ i: 1+ type: INT+ }+ }+ name: "test_rnn_fwd_bias_initial_h_layout_1"+ input {+ name: "X"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 3 # batch size+ }+ dim {+ dim_value: 4 # seq_length+ }+ dim {+ dim_value: 2 # input size+ }+ }+ }+ }+ }+ input {+ name: "W"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 5 # gates_count*hidden_size+ }+ dim {+ dim_value: 2 # input_size+ }+ }+ }+ }+ }+ input {+ name: "R"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 5 # gates_count*hidden_size+ }+ dim {+ dim_value: 5 # hidden_size+ }+ }+ }+ }+ }+ input {+ name: "B"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 10 # 2 * gates_count*hidden_size+ }+ }+ }+ }+ }+ input {+ name: "initial_h"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 3 # batch size+ }+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 5 # hidden size+ }+ }+ }+ }+ }+ output {+ name: "Y"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 3 # batch_size+ }+ dim {+ dim_value: 4 # seq_length+ }+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 5 # hidden_size+ }+ }+ }+ }+ }+ output {+ name: "Y_h"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 3 # batch_size+ }+ dim {+ dim_value: 1 # num_directions+ }+ dim {+ dim_value: 5 # hidden_size+ }+ }+ }+ }+ }+}+opset_import {+ version: 14+}diff --git a/src/frontends/onnx/tests/models/rnn_layout_invalid.prototxt b/src/frontends/onnx/tests/models/rnn_layout_invalid.prototxtnew file mode 100644index 00000000000..e8e493380b8--- /dev/null+++ b/src/frontends/onnx/tests/models/rnn_layout_invalid.prototxt@@ -0,0 +1,155 @@+ir_version: 7+producer_name: "OpenVINO ONNX Frontend"+graph {+ node {+ input: "X"+ input: "W"+ input: "R"+ input: "B"+ input: ""+ input: "initial_h"+ output: "Y"+ output: "Y_h"+ op_type: "RNN"+ attribute {+ name: "direction"+ s: "forward"+ type: STRING+ }+ attribute {+ name: "hidden_size"+ i: 3+ type: INT+ }+ attribute {+ name: "layout"+ i: 2+ type: INT+ }+ }+ name: "test_rnn_layout_invalid"+ initializer {+ dims: 1+ dims: 3+ dims: 2+ data_type: 1+ float_data: -0.33+ float_data: -0.26+ float_data: -0.19+ float_data: -0.120000005+ float_data: -0.050000012+ float_data: 0.02000001+ name: "W"+ }+ initializer {+ dims: 1+ dims: 3+ dims: 3+ data_type: 1+ float_data: -0.25+ float_data: -0.20000002+ float_data: -0.15+ float_data: -0.10000001+ float_data: -0.050000012+ float_data: 0.0+ float_data: 0.049999982+ float_data: 0.099999994+ float_data: 0.15+ name: "R"+ }+ initializer {+ dims: 1+ dims: 6+ data_type: 1+ float_data: -0.17+ float_data: -0.14+ float_data: -0.11000001+ float_data: -0.080000006+ float_data: -0.050000012+ float_data: -0.02000001+ name: "B"+ }+ input {+ name: "X"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 3+ }+ dim {+ dim_value: 2+ }+ }+ }+ }+ }+ input {+ name: "initial_h"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 3+ }+ }+ }+ }+ }+ output {+ name: "Y"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 3+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 3+ }+ }+ }+ }+ }+ output {+ name: "Y_h"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 3+ }+ }+ }+ }+ }+}+opset_import {+ domain: ""+ version: 14+}diff --git a/src/frontends/onnx/tests/onnx_import_rnn.in.cpp b/src/frontends/onnx/tests/onnx_import_rnn.in.cppindex d949bb432b9..d60ab73ceb7 100644--- a/src/frontends/onnx/tests/onnx_import_rnn.in.cpp+++ b/src/frontends/onnx/tests/onnx_import_rnn.in.cpp@@ -32,6 +32,35 @@ using namespace ov::frontend::onnx::tests;static std::string s_manifest = onnx_backend_manifest("${MANIFEST}");static std::string s_device = backend_name_to_device("${BACKEND_NAME}");+static std::vector<float> transpose(const std::vector<float>& data,+ const Shape& shape,+ const std::vector<size_t>& order) {+ const size_t rank = shape.size();+ Shape out_shape(rank);+ for (size_t i = 0; i < rank; ++i)+ out_shape[i] = shape[order[i]];++ std::vector<size_t> in_strides(rank, 1);+ for (size_t i = rank; i > 1; --i)+ in_strides[i - 2] = in_strides[i - 1] * shape[i - 1];++ std::vector<float> out(data.size());+ std::vector<size_t> out_coord(rank, 0);+ for (size_t i = 0; i < out.size(); ++i) {+ size_t in_offset = 0;+ for (size_t d = 0; d < rank; ++d)+ in_offset += out_coord[d] * in_strides[order[d]];+ out[i] = data[in_offset];++ for (size_t d = rank; d > 0; --d) {+ if (++out_coord[d - 1] < out_shape[d - 1])+ break;+ out_coord[d - 1] = 0;+ }+ }+ return out;+}+// ONNX LSTM tests (implemented by OpenVINO LSTMCell and LSTMSequence)OPENVINO_TEST(${BACKEND_NAME}, onnx_model_lstm_fwd_default_const) {auto model = convert_model("lstm_fwd_default_const.onnx");@@ -202,42 +231,153 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_lstm_bidir_mixed_seq_const) {test_case.run(DEFAULT_FLOAT_TOLERANCE_BITS + 1);}+// Inputs and reference outputs of lstm_fwd_mixed_seq.onnx, in the ONNX layout=0 convention:+// X is [seq=2, batch=2, input=1], Y is [seq, num_directions=1, batch, hidden=3]+// and Y_h/Y_c are [num_directions, batch, hidden].+namespace lstm_fwd_mixed_seq {+const std::vector<float> in_X{1.f, 2.f, 10.f, 11.f};+const std::vector<float> in_W{0.1f, 0.2f, 0.3f, 0.4f, 1.f, 2.f, 3.f, 4.f, 10.f, 11.f, 12.f, 13.f};+const std::vector<float> in_R(36, 0.1f);+const std::vector<float> in_B(24, 0.f);+const std::vector<int> in_sequence_lens{1, 2};++const std::vector<float> out_Y{0.28828835f,+ 0.36581863f,+ 0.45679406f,+ 0.34526032f,+ 0.47220859f,+ 0.55850911f,+ 0.f,+ 0.f,+ 0.f,+ 0.85882828f,+ 0.90703777f,+ 0.92382453f};+const std::vector<float> out_Y_h{0.28828835f, 0.36581863f, 0.45679406f, 0.85882828f, 0.90703777f, 0.92382453f};+const std::vector<float> out_Y_c{0.52497941f, 0.54983425f, 0.5744428f, 1.3249796f, 1.51063104f, 1.61451544f};+} // namespace lstm_fwd_mixed_seq+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_lstm_fwd_mixed_seq) {auto model = convert_model("lstm_fwd_mixed_seq.onnx");auto test_case = ov::test::TestCase(model, s_device);- int hidden_size{3};- test_case.add_input<float>({1.f, 2.f, 10.f, 11.f}); // X- test_case.add_input<float>({0.1f, 0.2f, 0.3f, 0.4f, 1.f, 2.f, 3.f, 4.f, 10.f, 11.f, 12.f, 13.f}); // W- test_case.add_input(std::vector<float>(4 * hidden_size * hidden_size, 0.1f)); // R- test_case.add_input(std::vector<float>(8 * hidden_size, 0.0f)); // B- test_case.add_input<int>({1, 2}); // seq_lengths+ test_case.add_input<float>(lstm_fwd_mixed_seq::in_X);+ test_case.add_input<float>(lstm_fwd_mixed_seq::in_W);+ test_case.add_input<float>(lstm_fwd_mixed_seq::in_R);+ test_case.add_input<float>(lstm_fwd_mixed_seq::in_B);+ test_case.add_input<int>(lstm_fwd_mixed_seq::in_sequence_lens);- test_case.add_expected_output<float>(Shape{2, 1, 2, 3},- {0.28828835f,- 0.36581863f,- 0.45679406f,- 0.34526032f,- 0.47220859f,- 0.55850911f,- 0.f,- 0.f,- 0.f,- 0.85882828f,- 0.90703777f,- 0.92382453f}); // Y_data- test_case.add_expected_output<float>(- Shape{1, 2, 3},- {0.28828835f, 0.36581863f, 0.45679406f, 0.85882828f, 0.90703777f, 0.92382453f}); // Y_h_data- test_case.add_expected_output<float>(- Shape{1, 2, 3},- {0.52497941f, 0.54983425f, 0.5744428f, 1.3249796f, 1.51063104f, 1.61451544f}); // Y_c_data+ test_case.add_expected_output<float>(Shape{2, 1, 2, 3}, lstm_fwd_mixed_seq::out_Y);+ test_case.add_expected_output<float>(Shape{1, 2, 3}, lstm_fwd_mixed_seq::out_Y_h);+ test_case.add_expected_output<float>(Shape{1, 2, 3}, lstm_fwd_mixed_seq::out_Y_c);// We have to enlarge tolerance bits to 3 - it's only one bit more than default value.// The discrepancies may occur at most on 7th decimal position.test_case.run(DEFAULT_FLOAT_TOLERANCE_BITS + 1);}+// Same operator and the same weights as lstm_fwd_mixed_seq, but with the opset 14 `layout`+// attribute set to 1, so the batch axis comes first in X, Y, Y_h and Y_c. sequence_lens stays+// [batch] in both layouts, which also guards against permuting it by mistake.+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_lstm_fwd_mixed_seq_layout_1) {+ auto model = convert_model("lstm_fwd_mixed_seq_layout_1.onnx");++ auto test_case = ov::test::TestCase(model, s_device);+ // X: [seq, batch, input] -> [batch, seq, input]+ test_case.add_input<float>(transpose(lstm_fwd_mixed_seq::in_X, Shape{2, 2, 1}, {1, 0, 2}));+ test_case.add_input<float>(lstm_fwd_mixed_seq::in_W);+ test_case.add_input<float>(lstm_fwd_mixed_seq::in_R);+ test_case.add_input<float>(lstm_fwd_mixed_seq::in_B);+ test_case.add_input<int>(lstm_fwd_mixed_seq::in_sequence_lens);++ // Y: [seq, num_directions, batch, hidden] -> [batch, seq, num_directions, hidden]+ test_case.add_expected_output<float>(Shape{2, 2, 1, 3},+ transpose(lstm_fwd_mixed_seq::out_Y, Shape{2, 1, 2, 3}, {2, 0, 1, 3}));+ // Y_h, Y_c: [num_directions, batch, hidden] -> [batch, num_directions, hidden]+ test_case.add_expected_output<float>(Shape{2, 1, 3},+ transpose(lstm_fwd_mixed_seq::out_Y_h, Shape{1, 2, 3}, {1, 0, 2}));+ test_case.add_expected_output<float>(Shape{2, 1, 3},+ transpose(lstm_fwd_mixed_seq::out_Y_c, Shape{1, 2, 3}, {1, 0, 2}));++ test_case.run(DEFAULT_FLOAT_TOLERANCE_BITS + 1);+}++// Inputs and reference outputs of lstm_fwd_initial_h_c.onnx, in the ONNX layout=0 convention.+// W, R and B are reused from lstm_fwd_mixed_seq; seq_length and batch_size differ on purpose so+// that a wrong axis order changes the output shapes. The expected values were computed from the+// ONNX LSTM specification and cross-checked against onnx.reference.ReferenceEvaluator.+namespace lstm_fwd_initial_h_c {+const std::vector<float> in_X{1.f, 2.f, 10.f, 11.f, 5.f, 6.f};+const std::vector<float> in_initial_h{-0.16f, -0.07f, 0.02f, 0.11f, 0.20f, 0.29f};+const std::vector<float> in_initial_c{-0.09f, -0.03f, 0.03f, 0.09f, 0.15f, 0.21f};++const std::vector<float> out_Y{0.24263948f,+ 0.34453541f,+ 0.47140008f,+ 0.40377864f,+ 0.57015294f,+ 0.68911171f,+ 0.81572270f,+ 0.88674504f,+ 0.91484076f,+ 0.88340819f,+ 0.93248248f,+ 0.95069653f,+ 0.86339754f,+ 0.97003090f,+ 0.98395663f,+ 0.91011900f,+ 0.98452914f,+ 0.99148357f};+const std::vector<float> out_Y_h{0.86339754f, 0.97003090f, 0.98395663f, 0.91011900f, 0.98452914f, 0.99148357f};+const std::vector<float> out_Y_c{1.86717939f, 2.18597698f, 2.40985966f, 2.14066553f, 2.49125576f, 2.72758770f};+} // namespace lstm_fwd_initial_h_c++OPENVINO_TEST(${BACKEND_NAME}, onnx_model_lstm_fwd_initial_h_c) {+ auto model = convert_model("lstm_fwd_initial_h_c.onnx");++ auto test_case = ov::test::TestCase(model, s_device);+ test_case.add_input<float>(lstm_fwd_initial_h_c::in_X);+ test_case.add_input<float>(lstm_fwd_mixed_seq::in_W);+ test_case.add_input<float>(lstm_fwd_mixed_seq::in_R);+ test_case.add_input<float>(lstm_fwd_mixed_seq::in_B);+ test_case.add_input<float>(lstm_fwd_initial_h_c::in_initial_h);+ test_case.add_input<float>(lstm_fwd_initial_h_c::in_initial_c);++ test_case.add_expected_output<float>(Shape{3, 1, 2, 3}, lstm_fwd_initial_h_c::out_Y);+ test_case.add_expected_output<float>(Shape{1, 2, 3}, lstm_fwd_initial_h_c::out_Y_h);+ test_case.add_expected_output<float>(Shape{1, 2, 3}, lstm_fwd_initial_h_c::out_Y_c);++ test_case.run(DEFAULT_FLOAT_TOLERANCE_BITS + 1);+}++// layout=1 twin of lstm_fwd_initial_h_c, covering the permutation of the explicitly provided+// initial_h and initial_c against the same reference values.+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_lstm_fwd_initial_h_c_layout_1) {+ auto model = convert_model("lstm_fwd_initial_h_c_layout_1.onnx");++ auto test_case = ov::test::TestCase(model, s_device);+ // X: [seq, batch, input] -> [batch, seq, input]+ test_case.add_input<float>(transpose(lstm_fwd_initial_h_c::in_X, Shape{3, 2, 1}, {1, 0, 2}));+ test_case.add_input<float>(lstm_fwd_mixed_seq::in_W);+ test_case.add_input<float>(lstm_fwd_mixed_seq::in_R);+ test_case.add_input<float>(lstm_fwd_mixed_seq::in_B);+ // initial_h, initial_c: [num_directions, batch, hidden] -> [batch, num_directions, hidden]+ test_case.add_input<float>(transpose(lstm_fwd_initial_h_c::in_initial_h, Shape{1, 2, 3}, {1, 0, 2}));+ test_case.add_input<float>(transpose(lstm_fwd_initial_h_c::in_initial_c, Shape{1, 2, 3}, {1, 0, 2}));++ // Y: [seq, num_directions, batch, hidden] -> [batch, seq, num_directions, hidden]+ test_case.add_expected_output<float>(Shape{2, 3, 1, 3},+ transpose(lstm_fwd_initial_h_c::out_Y, Shape{3, 1, 2, 3}, {2, 0, 1, 3}));+ // Y_h, Y_c: [num_directions, batch, hidden] -> [batch, num_directions, hidden]+ test_case.add_expected_output<float>(Shape{2, 1, 3},+ transpose(lstm_fwd_initial_h_c::out_Y_h, Shape{1, 2, 3}, {1, 0, 2}));+ test_case.add_expected_output<float>(Shape{2, 1, 3},+ transpose(lstm_fwd_initial_h_c::out_Y_c, Shape{1, 2, 3}, {1, 0, 2}));++ test_case.run(DEFAULT_FLOAT_TOLERANCE_BITS + 1);+}+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_lstm_fwd_hardsigmoid_activation) {auto model = convert_model("lstm_fwd_hardsigmoid_activation.onnx");@@ -806,6 +946,37 @@ public:-3.52258608e-02f, -4.47186083e-02f, 6.82157278e-01f, -2.59570718e-01f, 1.50172567e+00f, -2.76523419e-02f,};+ // Reference outputs of gru_fwd_bias_initial_h.onnx, in the ONNX layout=0 convention:+ // Y is [seq=4, num_directions=1, batch=3, hidden=5] and Y_h is [num_directions=1, batch=3, hidden=5].+ std::vector<float> out_fwd_bias_initial_h_Y{+ -0.9559332f, 0.4372494f, 0.9967716f, -0.9079381f, -1.2538278f, 1.9265908f, -0.8437393f, -1.2057271f,+ -0.25887525f, -0.52679026f, -0.3619178f, 0.67928517f, 0.9486744f, -0.12006134f, -1.3862017f, -0.98941356f,+ 0.80389524f, 0.97586197f, -0.9343586f, -0.74858856f, 1.797039f, -0.7873732f, -0.72469383f, -0.5866635f,+ -0.42103744f, -0.8406298f, 0.85877097f, 0.6349921f, -0.55897295f, -0.6168443f, -0.99686503f, 0.87408733f,+ 0.87070423f, -0.9564345f, 0.52932394f, 1.577129f, -0.6935871f, -0.304804f, -0.75392795f, -0.20703818f,+ -0.93796504f, 0.9220495f, 0.36017662f, -0.7007159f, 0.06962098f, -0.22581682f, 0.9119905f, -0.64628327f,+ -0.79374063f, -0.82321495f, 1.2853851f, -0.6176347f, 0.6865668f, -0.85147655f, 0.0379298f, -0.96323603f,+ 0.9265786f, 0.54976916f, -0.8037839f, 0.73501444f,+ };++ std::vector<float> out_fwd_bias_initial_h_Y_h{+ -0.22581682f,+ 0.9119905f,+ -0.64628327f,+ -0.79374063f,+ -0.82321495f,+ 1.2853851f,+ -0.6176347f,+ 0.6865668f,+ -0.85147655f,+ 0.0379298f,+ -0.96323603f,+ 0.9265786f,+ 0.54976916f,+ -0.8037839f,+ 0.73501444f,+ };+protected:void SetUp() override {}};@@ -1377,38 +1548,34 @@ OPENVINO_TEST_F(${BACKEND_NAME}, GRUSequenceOp, onnx_model_gru_fwd_bias_initial_test_case.add_input<float>(in_initial_h);// Y- test_case.add_expected_output<float>(- Shape{4, 1, 3, 5},- std::vector<float>{- -0.9559332f, 0.4372494f, 0.9967716f, -0.9079381f, -1.2538278f, 1.9265908f, -0.8437393f,- -1.2057271f, -0.25887525f, -0.52679026f, -0.3619178f, 0.67928517f, 0.9486744f, -0.12006134f,- -1.3862017f, -0.98941356f, 0.80389524f, 0.97586197f, -0.9343586f, -0.74858856f, 1.797039f,- -0.7873732f, -0.72469383f, -0.5866635f, -0.42103744f, -0.8406298f, 0.85877097f, 0.6349921f,- -0.55897295f, -0.6168443f, -0.99686503f, 0.87408733f, 0.87070423f, -0.9564345f, 0.52932394f,- 1.577129f, -0.6935871f, -0.304804f, -0.75392795f, -0.20703818f, -0.93796504f, 0.9220495f,- 0.36017662f, -0.7007159f, 0.06962098f, -0.22581682f, 0.9119905f, -0.64628327f, -0.79374063f,- -0.82321495f, 1.2853851f, -0.6176347f, 0.6865668f, -0.85147655f, 0.0379298f, -0.96323603f,- 0.9265786f, 0.54976916f, -0.8037839f, 0.73501444f,- });+ test_case.add_expected_output<float>(Shape{4, 1, 3, 5}, out_fwd_bias_initial_h_Y);// Y_h- test_case.add_expected_output<float>(Shape{1, 3, 5},- std::vector<float>{- -0.22581682f,- 0.9119905f,- -0.64628327f,- -0.79374063f,- -0.82321495f,- 1.2853851f,- -0.6176347f,- 0.6865668f,- -0.85147655f,- 0.0379298f,- -0.96323603f,- 0.9265786f,- 0.54976916f,- -0.8037839f,- 0.73501444f,- });+ test_case.add_expected_output<float>(Shape{1, 3, 5}, out_fwd_bias_initial_h_Y_h);+ test_case.run(DEFAULT_FLOAT_TOLERANCE_BITS + 4);+}++// Same operator and the same weights as gru_fwd_bias_initial_h, but with the opset 14+// `layout` attribute set to 1, so the batch axis comes first in X, initial_h, Y and Y_h.+// Feeding the permuted inputs must yield the permuted outputs of the layout=0 model.+OPENVINO_TEST_F(${BACKEND_NAME}, GRUSequenceOp, onnx_model_gru_fwd_bias_initial_h_layout_1) {+ auto model = convert_model("gru_fwd_bias_initial_h_layout_1.onnx");++ auto test_case = ov::test::TestCase(model, s_device);++ // X: [seq, batch, input] -> [batch, seq, input]+ test_case.add_input<float>(transpose(in_X, Shape{4, 3, 2}, {1, 0, 2}));+ test_case.add_input<float>(in_W);+ test_case.add_input<float>(in_R);+ test_case.add_input<float>(in_B);+ // initial_h: [num_directions, batch, hidden] -> [batch, num_directions, hidden]+ test_case.add_input<float>(transpose(in_initial_h, Shape{1, 3, 5}, {1, 0, 2}));++ // Y: [seq, num_directions, batch, hidden] -> [batch, seq, num_directions, hidden]+ test_case.add_expected_output<float>(Shape{3, 4, 1, 5},+ transpose(out_fwd_bias_initial_h_Y, Shape{4, 1, 3, 5}, {2, 0, 1, 3}));+ // Y_h: [num_directions, batch, hidden] -> [batch, num_directions, hidden]+ test_case.add_expected_output<float>(Shape{3, 1, 5},+ transpose(out_fwd_bias_initial_h_Y_h, Shape{1, 3, 5}, {1, 0, 2}));test_case.run(DEFAULT_FLOAT_TOLERANCE_BITS + 4);}@@ -1722,6 +1889,64 @@ public:1.3470556f, -0.32972014f,};+ // Reference outputs of rnn_fwd_bias_initial_h.onnx, in the ONNX layout=0 convention:+ // Y is [seq=4, num_directions=1, batch=3, hidden=5] and Y_h is [num_directions=1, batch=3, hidden=5].+ std::vector<float> out_fwd_bias_initial_h_Y{+ 0.55277014f, 0.15672898f, -0.25152922f, -0.63345766f, 0.99974346f, 0.94002223f, -0.97647303f, -0.9999884f,+ 0.9752002f, 0.97388494f, 0.9967754f, 0.96745205f, 0.7899921f, 0.92003024f, -0.43116868f, 0.11219919f,+ 0.895327f, 0.21749747f, 0.6617017f, 0.99962795f, 0.37670398f, 0.7918401f, -0.99966455f, 0.9961897f,+ 0.9995159f, -0.84224236f, 0.92083716f, -0.99834263f, 0.9435711f, 0.8485148f, 0.699257f, 0.9983405f,+ -0.87222385f, 0.05191362f, 0.9878634f, 0.75459063f, 0.8326433f, -0.99705976f, 0.62511444f, 0.99979305f,+ 0.99925995f, 0.94032586f, -0.86841005f, -0.8692311f, 0.9974319f, -0.37055743f, -0.54580235f, -0.8618355f,+ 0.6927968f, 0.99997866f, 0.15482295f, 0.90996563f, -0.9992051f, 0.784014f, 0.9999677f, -0.30979204f,+ 0.99138904f, -0.10645419f, -0.18203181f, 0.9996245f,+ };++ std::vector<float> out_fwd_bias_initial_h_Y_h{+ -0.37055743f,+ -0.54580235f,+ -0.8618355f,+ 0.6927968f,+ 0.99997866f,+ 0.15482295f,+ 0.90996563f,+ -0.9992051f,+ 0.784014f,+ 0.9999677f,+ -0.30979204f,+ 0.99138904f,+ -0.10645419f,+ -0.18203181f,+ 0.9996245f,+ };++ // Reference outputs of rnn_bidirectional.onnx, in the ONNX layout=0 convention:+ // Y is [seq=4, num_directions=2, batch=3, hidden=5] and Y_h is [num_directions=2, batch=3, hidden=5].+ std::vector<float> out_bidirectional_Y{+ 0.02254748f, 0.15776646f, -0.8229023f, 0.19205809f, 0.76984656f, -0.00603169f, -0.02861464f, 0.04512155f,+ -0.0011912f, -0.02572936f, -0.13703543f, -0.49651444f, -0.78868157f, 0.3566854f, 0.8758509f, -0.9964788f,+ -0.15236056f, 0.5478349f, 0.14500666f, 0.61871886f, 0.03722596f, -0.81331265f, 0.99774206f, -0.888188f,+ -0.5575663f, -0.9284624f, -0.5595875f, 0.9986867f, -0.18373811f, 0.8451735f, -0.43823165f, -0.1904698f,+ 0.8320786f, 0.9830735f, 0.61861455f, 0.19109797f, 0.6440699f, 0.00962079f, -0.32752872f, -0.5050589f,+ -0.23455954f, 0.9517933f, 0.9050665f, 0.91091585f, -0.77941567f, -0.9915407f, -0.23976672f, 0.04337811f,+ 0.2958206f, -0.3979709f, -0.9083327f, -0.21814531f, 0.9981259f, -0.8650538f, -0.4886601f, -0.8349008f,+ -0.7880142f, 0.99017143f, -0.9816452f, -0.93827677f, 0.16374564f, 0.98451114f, -0.821692f, -0.6319715f,+ -0.01324981f, 0.28117967f, 0.20685172f, 0.01166677f, -0.5441829f, -0.5463746f, -0.85301256f, 0.52109087f,+ -0.8317892f, -0.9676957f, -0.30258918f, -0.9810498f, -0.83153796f, -0.9676579f, 0.5483788f, 0.42533123f,+ -0.9851954f, -0.5354376f, 0.6905062f, -0.46665573f, -0.851916f, -0.9073148f, 0.16276085f, 0.9518349f,+ -0.8635942f, -0.92539954f, 0.33436012f, -0.988292f, 0.9238765f, 0.94239855f, 0.24151397f, 0.5482547f,+ 0.76547384f, -0.81047577f, -0.6625802f, -0.09694612f, 0.9948462f, -0.6242633f, -0.19065344f, -0.36072153f,+ -0.99407107f, 0.94602585f, 0.55862486f, 0.2306763f, 0.22547626f, 0.37753606f, -0.9951596f, -0.74445903f,+ -0.6766813f, 0.32036817f, 0.33250773f, -0.9957684f, -0.7924f, -0.40261805f, -0.34061068f, -0.55580306f,+ };++ std::vector<float> out_bidirectional_Y_h{+ 0.33436012f, -0.988292f, 0.9238765f, 0.94239855f, 0.24151397f, 0.5482547f, 0.76547384f, -0.81047577f,+ -0.6625802f, -0.09694612f, 0.9948462f, -0.6242633f, -0.19065344f, -0.36072153f, -0.99407107f, -0.9964788f,+ -0.15236056f, 0.5478349f, 0.14500666f, 0.61871886f, 0.03722596f, -0.81331265f, 0.99774206f, -0.888188f,+ -0.5575663f, -0.9284624f, -0.5595875f, 0.9986867f, -0.18373811f, 0.8451735f,+ };+protected:void SetUp() override {}};@@ -2297,38 +2522,34 @@ OPENVINO_TEST_F(${BACKEND_NAME}, RNNSequenceOp, onnx_model_rnn_fwd_bias_initial_test_case.add_input<float>(in_initial_h);// Y- test_case.add_expected_output<float>(- Shape{4, 1, 3, 5},- std::vector<float>{- 0.55277014f, 0.15672898f, -0.25152922f, -0.63345766f, 0.99974346f, 0.94002223f, -0.97647303f,- -0.9999884f, 0.9752002f, 0.97388494f, 0.9967754f, 0.96745205f, 0.7899921f, 0.92003024f,- -0.43116868f, 0.11219919f, 0.895327f, 0.21749747f, 0.6617017f, 0.99962795f, 0.37670398f,- 0.7918401f, -0.99966455f, 0.9961897f, 0.9995159f, -0.84224236f, 0.92083716f, -0.99834263f,- 0.9435711f, 0.8485148f, 0.699257f, 0.9983405f, -0.87222385f, 0.05191362f, 0.9878634f,- 0.75459063f, 0.8326433f, -0.99705976f, 0.62511444f, 0.99979305f, 0.99925995f, 0.94032586f,- -0.86841005f, -0.8692311f, 0.9974319f, -0.37055743f, -0.54580235f, -0.8618355f, 0.6927968f,- 0.99997866f, 0.15482295f, 0.90996563f, -0.9992051f, 0.784014f, 0.9999677f, -0.30979204f,- 0.99138904f, -0.10645419f, -0.18203181f, 0.9996245f,- });+ test_case.add_expected_output<float>(Shape{4, 1, 3, 5}, out_fwd_bias_initial_h_Y);// Y_h- test_case.add_expected_output<float>(Shape{1, 3, 5},- std::vector<float>{- -0.37055743f,- -0.54580235f,- -0.8618355f,- 0.6927968f,- 0.99997866f,- 0.15482295f,- 0.90996563f,- -0.9992051f,- 0.784014f,- 0.9999677f,- -0.30979204f,- 0.99138904f,- -0.10645419f,- -0.18203181f,- 0.9996245f,- });+ test_case.add_expected_output<float>(Shape{1, 3, 5}, out_fwd_bias_initial_h_Y_h);+ test_case.run(DEFAULT_FLOAT_TOLERANCE_BITS + 5);+}++// Same operator and the same weights as rnn_fwd_bias_initial_h, but with the opset 14+// `layout` attribute set to 1, so the batch axis comes first in X, initial_h, Y and Y_h.+// Feeding the permuted inputs must yield the permuted outputs of the layout=0 model.+OPENVINO_TEST_F(${BACKEND_NAME}, RNNSequenceOp, onnx_model_rnn_fwd_bias_initial_h_layout_1) {+ auto model = convert_model("rnn_fwd_bias_initial_h_layout_1.onnx");++ auto test_case = ov::test::TestCase(model, s_device);++ // X: [seq, batch, input] -> [batch, seq, input]+ test_case.add_input<float>(transpose(in_X, Shape{4, 3, 2}, {1, 0, 2}));+ test_case.add_input<float>(in_W);+ test_case.add_input<float>(in_R);+ test_case.add_input<float>(in_B);+ // initial_h: [num_directions, batch, hidden] -> [batch, num_directions, hidden]+ test_case.add_input<float>(transpose(in_initial_h, Shape{1, 3, 5}, {1, 0, 2}));++ // Y: [seq, num_directions, batch, hidden] -> [batch, seq, num_directions, hidden]+ test_case.add_expected_output<float>(Shape{3, 4, 1, 5},+ transpose(out_fwd_bias_initial_h_Y, Shape{4, 1, 3, 5}, {2, 0, 1, 3}));+ // Y_h: [num_directions, batch, hidden] -> [batch, num_directions, hidden]+ test_case.add_expected_output<float>(Shape{3, 1, 5},+ transpose(out_fwd_bias_initial_h_Y_h, Shape{1, 3, 5}, {1, 0, 2}));test_case.run(DEFAULT_FLOAT_TOLERANCE_BITS + 5);}@@ -2342,38 +2563,29 @@ OPENVINO_TEST_F(${BACKEND_NAME}, RNNSequenceOp, onnx_model_rnn_bidirectional) {test_case.add_input<float>(in_bdir_R);// Y- test_case.add_expected_output<float>(- Shape{4, 2, 3, 5},- std::vector<float>{- 0.02254748f, 0.15776646f, -0.8229023f, 0.19205809f, 0.76984656f, -0.00603169f, -0.02861464f,- 0.04512155f, -0.0011912f, -0.02572936f, -0.13703543f, -0.49651444f, -0.78868157f, 0.3566854f,- 0.8758509f, -0.9964788f, -0.15236056f, 0.5478349f, 0.14500666f, 0.61871886f, 0.03722596f,- -0.81331265f, 0.99774206f, -0.888188f, -0.5575663f, -0.9284624f, -0.5595875f, 0.9986867f,- -0.18373811f, 0.8451735f, -0.43823165f, -0.1904698f, 0.8320786f, 0.9830735f, 0.61861455f,- 0.19109797f, 0.6440699f, 0.00962079f, -0.32752872f, -0.5050589f, -0.23455954f, 0.9517933f,- 0.9050665f, 0.91091585f, -0.77941567f, -0.9915407f, -0.23976672f, 0.04337811f, 0.2958206f,- -0.3979709f, -0.9083327f, -0.21814531f, 0.9981259f, -0.8650538f, -0.4886601f, -0.8349008f,- -0.7880142f, 0.99017143f, -0.9816452f, -0.93827677f, 0.16374564f, 0.98451114f, -0.821692f,- -0.6319715f, -0.01324981f, 0.28117967f, 0.20685172f, 0.01166677f, -0.5441829f, -0.5463746f,- -0.85301256f, 0.52109087f, -0.8317892f, -0.9676957f, -0.30258918f, -0.9810498f, -0.83153796f,- -0.9676579f, 0.5483788f, 0.42533123f, -0.9851954f, -0.5354376f, 0.6905062f, -0.46665573f,- -0.851916f, -0.9073148f, 0.16276085f, 0.9518349f, -0.8635942f, -0.92539954f, 0.33436012f,- -0.988292f, 0.9238765f, 0.94239855f, 0.24151397f, 0.5482547f, 0.76547384f, -0.81047577f,- -0.6625802f, -0.09694612f, 0.9948462f, -0.6242633f, -0.19065344f, -0.36072153f, -0.99407107f,- 0.94602585f, 0.55862486f, 0.2306763f, 0.22547626f, 0.37753606f, -0.9951596f, -0.74445903f,- -0.6766813f, 0.32036817f, 0.33250773f, -0.9957684f, -0.7924f, -0.40261805f, -0.34061068f,- -0.55580306f,- });+ test_case.add_expected_output<float>(Shape{4, 2, 3, 5}, out_bidirectional_Y);// Y_h- test_case.add_expected_output<float>(- Shape{2, 3, 5},- std::vector<float>{- 0.33436012f, -0.988292f, 0.9238765f, 0.94239855f, 0.24151397f, 0.5482547f,- 0.76547384f, -0.81047577f, -0.6625802f, -0.09694612f, 0.9948462f, -0.6242633f,- -0.19065344f, -0.36072153f, -0.99407107f, -0.9964788f, -0.15236056f, 0.5478349f,- 0.14500666f, 0.61871886f, 0.03722596f, -0.81331265f, 0.99774206f, -0.888188f,- -0.5575663f, -0.9284624f, -0.5595875f, 0.9986867f, -0.18373811f, 0.8451735f,- });+ test_case.add_expected_output<float>(Shape{2, 3, 5}, out_bidirectional_Y_h);+ test_case.run(DEFAULT_FLOAT_TOLERANCE_BITS + 6);+}++// layout=1 twin of rnn_bidirectional. num_directions=2 keeps the Y permutation honest, and+// the absent initial_h also covers building the default initial state from the permuted X.+OPENVINO_TEST_F(${BACKEND_NAME}, RNNSequenceOp, onnx_model_rnn_bidirectional_layout_1) {+ auto model = convert_model("rnn_bidirectional_layout_1.onnx");++ auto test_case = ov::test::TestCase(model, s_device);++ // X: [seq, batch, input] -> [batch, seq, input]+ test_case.add_input<float>(transpose(in_X, Shape{4, 3, 2}, {1, 0, 2}));+ test_case.add_input<float>(in_bdir_W);+ test_case.add_input<float>(in_bdir_R);++ // Y: [seq, num_directions, batch, hidden] -> [batch, seq, num_directions, hidden]+ test_case.add_expected_output<float>(Shape{3, 4, 2, 5},+ transpose(out_bidirectional_Y, Shape{4, 2, 3, 5}, {2, 0, 1, 3}));+ // Y_h: [num_directions, batch, hidden] -> [batch, num_directions, hidden]+ test_case.add_expected_output<float>(Shape{3, 2, 5}, transpose(out_bidirectional_Y_h, Shape{2, 3, 5}, {1, 0, 2}));test_case.run(DEFAULT_FLOAT_TOLERANCE_BITS + 6);}diff --git a/src/frontends/onnx/tests/tests_python/test_backend.py b/src/frontends/onnx/tests/tests_python/test_backend.pyindex 6b6679f93a6..3f02ef00d53 100644--- a/src/frontends/onnx/tests/tests_python/test_backend.py+++ b/src/frontends/onnx/tests/tests_python/test_backend.py@@ -29,7 +29,6 @@ from tests import (xfail_issue_47323,xfail_issue_48052,xfail_issue_52463,- xfail_issue_63043,xfail_issue_63137,xfail_issue_69444,xfail_issue_82039,@@ -269,12 +268,6 @@ tests_expected_to_fail = ["OnnxBackendNodeModelTest.test_sequence_insert_at_back_cpu","OnnxBackendNodeModelTest.test_sequence_insert_at_front_cpu",),- (- xfail_issue_63043,- "OnnxBackendNodeModelTest.test_gru_batchwise_cpu",- "OnnxBackendNodeModelTest.test_lstm_batchwise_cpu",- "OnnxBackendNodeModelTest.test_simple_rnn_batchwise_cpu",- ),(skip_rng_tests,"OnnxBackendNodeModelTest.test_bernoulli_cpu",