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
[GPU] Fix missing input idx in lstmseq primitive desc and weights optimizat (#31434)
Wilson Seok126ef4e5bc03
diff --git a/src/plugins/intel_gpu/include/intel_gpu/primitives/rnn.hpp b/src/plugins/intel_gpu/include/intel_gpu/primitives/rnn.hppindex bba301cd3d0..9ceaa5f21ff 100644--- a/src/plugins/intel_gpu/include/intel_gpu/primitives/rnn.hpp+++ b/src/plugins/intel_gpu/include/intel_gpu/primitives/rnn.hpp@@ -61,10 +61,10 @@ struct RNNParams : public primitive_base<PType> {activation_params(activation_params),offset_order(offset_order),direction(direction) {- std::vector<std::string> pids{initial_hidden_state.pid, initial_cell_state.pid, W.pid, R.pid, B.pid, seq_lenghts.pid};- for (auto pid : pids) {- if (!pid.empty()) {- primitive_base<PType>::input.push_back(pid);+ std::vector<input_info> infos{initial_hidden_state, initial_cell_state, W, R, B, seq_lenghts};+ for (const auto& info : infos) {+ if (!info.pid.empty()) {+ primitive_base<PType>::input.push_back(info);}}}diff --git a/src/plugins/intel_gpu/src/graph/graph_optimizer/post_optimize_weights.cpp b/src/plugins/intel_gpu/src/graph/graph_optimizer/post_optimize_weights.cppindex f339d937b74..c613b747050 100644--- a/src/plugins/intel_gpu/src/graph/graph_optimizer/post_optimize_weights.cpp+++ b/src/plugins/intel_gpu/src/graph/graph_optimizer/post_optimize_weights.cpp@@ -127,7 +127,7 @@ void post_optimize_weights::optimize_weights(T& node, program& p) {if (node.type() == lstm_seq::type_id()) {program_node& prev_node = node.get_dependency(i);if (i == 5) {- add_lstm_bias_reorder(prev_node.id(), weights_reorder_params, p, prev_node, node);+ add_lstm_bias_reorder(prev_node.id(), weights_reorder_params, p, prev_node, node, i);} else {add_lstm_weights_reorder(prev_node.id(), weights_reorder_params, p, prev_node, node, i);}@@ -187,6 +187,17 @@ void post_optimize_weights::add_gru_weights_reorder(primitive_id input_id, std::void post_optimize_weights::add_lstm_weights_reorder(primitive_id input_id, std::shared_ptr<WeightsReorderParams> reorder_params, program& p, \cldnn::program_node& prev, cldnn::program_node& node, size_t i) {OPENVINO_ASSERT(reorder_params != nullptr, "[GPU] WeightsReorderParams is not initialized.");++ reorder_cache_key ckey{prev.id(), reorder_params->get_output_layout()};+ auto itr = _cached_lstm_weights_reorder.find(ckey);++ // If we already did the lstm weight optimization, reuse existing node.+ if (itr != _cached_lstm_weights_reorder.end()) {+ node.replace_dependency(i, *itr->second, false);+ return;+ }++ // This is first time. Run lstm weight optimization.std::string reorder_id = input_id + "_reo_" + std::to_string(i);const auto dir_num = static_cast<int>(reorder_params->get_input_layout().get_shape()[0]);auto hiddenSize = reorder_params->get_input_layout().get_shape()[1] / 4;@@ -238,11 +249,24 @@ void post_optimize_weights::add_lstm_weights_reorder(primitive_id input_id, std:set_implementation_and_output(crop2_node);set_implementation_and_output(con_node);set_implementation_and_output(permute_node);++ _cached_lstm_weights_reorder[ckey] = &permute_node;}void post_optimize_weights::add_lstm_bias_reorder(primitive_id input_id, std::shared_ptr<WeightsReorderParams> reorder_params, program& p, \- cldnn::program_node& prev, cldnn::program_node& node) {+ cldnn::program_node& prev, cldnn::program_node& node, size_t i) {OPENVINO_ASSERT(reorder_params != nullptr, "[GPU] WeightsReorderParams is not initialized.");++ reorder_cache_key ckey{prev.id(), reorder_params->get_output_layout()};+ auto itr = _cached_lstm_bias_reorder.find(ckey);++ // If we already did the lstm bias optimization, reuse existing node.+ if (itr != _cached_lstm_bias_reorder.end()) {+ node.replace_dependency(i, *itr->second, false);+ return;+ }++ // This is first time. Run lstm bias optimization.const auto dir_num = static_cast<int>(reorder_params->get_input_layout().get_shape()[0]);auto hiddenSize = reorder_params->get_output_layout().get_shape()[1] / 4;auto cropSize = cldnn::tensor{dir_num, static_cast<int>(hiddenSize), 1, 1};@@ -278,6 +302,8 @@ void post_optimize_weights::add_lstm_bias_reorder(primitive_id input_id, std::shset_implementation_and_output(crop1_node);set_implementation_and_output(crop2_node);set_implementation_and_output(con_node);++ _cached_lstm_bias_reorder[ckey] = &con_node;}void post_optimize_weights::run(program& p) {@@ -299,5 +325,4 @@ void post_optimize_weights::run(program& p) {if (found_lstm)p.get_processing_order().calc_processing_order(p);}--} // namespace cldnn+} // namespace cldnn\ No newline at end of filediff --git a/src/plugins/intel_gpu/src/graph/include/layout_optimizer.h b/src/plugins/intel_gpu/src/graph/include/layout_optimizer.hindex 2745abfbc87..42724e933cb 100644--- a/src/plugins/intel_gpu/src/graph/include/layout_optimizer.h+++ b/src/plugins/intel_gpu/src/graph/include/layout_optimizer.h@@ -24,6 +24,31 @@namespace cldnn {class primitive_inst;+struct reorder_cache_key {+ primitive_id data_source;+ layout expected_layout;++ friend bool operator==(reorder_cache_key const& lhs, reorder_cache_key const& rhs) {+ bool ret = lhs.data_source == rhs.data_source && lhs.expected_layout == rhs.expected_layout;++ if (ret && lhs.expected_layout.format == cldnn::format::custom) {+ ret &= (lhs.expected_layout.format.traits().block_sizes ==+ rhs.expected_layout.format.traits().block_sizes);+ }+ return ret;+ }++ friend bool operator!=(reorder_cache_key const& lhs, reorder_cache_key const& rhs) { return !(lhs == rhs); }++ friend bool operator<(reorder_cache_key const& lhs, reorder_cache_key const& rhs) {+ if (lhs.data_source != rhs.data_source)+ return (lhs.data_source < rhs.data_source);+ else if (lhs.expected_layout != rhs.expected_layout)+ return (lhs.expected_layout < rhs.expected_layout);+ else+ return lhs.expected_layout.format.traits().block_sizes < rhs.expected_layout.format.traits().block_sizes;+ }+};// this class is used for both static and dynamic reordering of data withing network.// static reordering is done for cldnn::data (i.e. immutable) primitives via internal network@@ -54,36 +79,7 @@ public:std::shared_ptr<WeightsReorderParams> reorder_params);private:- struct cache_key {- primitive_id data_source;- layout expected_layout;- bool needs_split_reorder;-- friend bool operator==(cache_key const& lhs, cache_key const& rhs) {- bool ret = lhs.data_source == rhs.data_source && lhs.expected_layout == rhs.expected_layout &&- lhs.needs_split_reorder == rhs.needs_split_reorder;-- if (ret && lhs.expected_layout.format == cldnn::format::custom) {- ret &= (lhs.expected_layout.format.traits().block_sizes ==- rhs.expected_layout.format.traits().block_sizes);- }- return ret;- }-- friend bool operator!=(cache_key const& lhs, cache_key const& rhs) { return !(lhs == rhs); }-- friend bool operator<(cache_key const& lhs, cache_key const& rhs) {- if (lhs.data_source != rhs.data_source)- return (lhs.data_source < rhs.data_source);- else if (lhs.expected_layout != rhs.expected_layout)- return (lhs.expected_layout < rhs.expected_layout);- else if (lhs.expected_layout.format == cldnn::format::custom)- return lhs.expected_layout.format.traits().block_sizes < rhs.expected_layout.format.traits().block_sizes;- return lhs.needs_split_reorder < rhs.needs_split_reorder;- }- };-- std::map<cache_key, std::shared_ptr<reorder>> _cached_reorders;+ std::map<reorder_cache_key, std::shared_ptr<reorder>> _cached_reorders;};class layout_optimizer {diff --git a/src/plugins/intel_gpu/src/graph/include/pass_manager.h b/src/plugins/intel_gpu/src/graph/include/pass_manager.hindex c7f64aa9015..3105e5e5148 100644--- a/src/plugins/intel_gpu/src/graph/include/pass_manager.h+++ b/src/plugins/intel_gpu/src/graph/include/pass_manager.h@@ -215,8 +215,11 @@ private:void add_gru_weights_reorder(primitive_id input_id, std::shared_ptr<WeightsReorderParams> reorder_params, program& p, cldnn::program_node&, \cldnn::program_node&, size_t);void add_lstm_bias_reorder(primitive_id input_id, std::shared_ptr<WeightsReorderParams> reorder_params, program& p, cldnn::program_node&, \- cldnn::program_node&);+ cldnn::program_node&, size_t);reorder_factory& _rf;++ std::map<reorder_cache_key, program_node*> _cached_lstm_weights_reorder;+ std::map<reorder_cache_key, program_node*> _cached_lstm_bias_reorder;};class propagate_constants : public base_pass {diff --git a/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp b/src/plugins/intel_gpu/src/graph/layout_optimizer.cppindex c4cbb08d311..97dac832866 100644--- a/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp+++ b/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp@@ -73,7 +73,7 @@ std::pair<std::shared_ptr<reorder>, bool> reorder_factory::get_reorder(primitiveif (in_layout == out_layout)return std::make_pair(nullptr, true);- cache_key ckey{ src_id + "." + std::to_string(src_port), out_layout };+ reorder_cache_key ckey{ src_id + "." + std::to_string(src_port), out_layout };auto itr = _cached_reorders.find(ckey);if (itr != _cached_reorders.end())return std::make_pair(itr->second, true);@@ -98,7 +98,7 @@ std::pair<std::shared_ptr<primitive>, bool> reorder_factory::get_weights_reorderstd::shared_ptr<WeightsReorderParams> reorder_params) {OPENVINO_ASSERT(reorder_params != nullptr, "[GPU] WeightsReorderParams is not initialized.");- cache_key ckey{ input_id, reorder_params->get_output_layout(), false };+ reorder_cache_key ckey{ input_id, reorder_params->get_output_layout()};auto itr = _cached_reorders.find(ckey);if (itr != _cached_reorders.end()) {return std::make_pair(itr->second, true);diff --git a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/single_layer_tests/lstm_sequence.cpp b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/single_layer_tests/lstm_sequence.cppindex e137188ed0e..7469a82aa37 100644--- a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/single_layer_tests/lstm_sequence.cpp+++ b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/single_layer_tests/lstm_sequence.cpp@@ -3,11 +3,18 @@//#include "single_op_tests/lstm_sequence.hpp"+#include "openvino/op/lstm_sequence.hpp"+#include "openvino/op/concat.hpp"#include "common_test_utils/test_constants.hpp"+#include "common_test_utils/ov_tensor_utils.hpp"#include "common_test_utils/ov_test_utils.hpp"+#include "transformations/op_conversions/bidirectional_sequences_decomposition.hpp"+#include "transformations/op_conversions/convert_sequences_to_tensor_iterator.hpp"namespace {using ov::test::LSTMSequenceTest;+using ov::test::utils::SequenceTestsMode;+using ov::test::utils::InputLayerType;class LSTMSequenceGPUTest : public LSTMSequenceTest {void SetUp() override {LSTMSequenceTest::SetUp();@@ -139,4 +146,193 @@ INSTANTIATE_TEST_SUITE_P(LSTMSequenceCM, LSTMSequenceGPUTest,::testing::Values(ov::test::utils::DEVICE_GPU)),LSTMSequenceGPUTest::getTestCaseName);+class MultipleLSTMSequenceGPUTest : public LSTMSequenceTest {+private:+ size_t max_seq_lengths;++public:+ void SetUp() override {+ SequenceTestsMode mode;+ size_t seq_lengths;+ size_t batch;+ size_t hidden_size;+ size_t input_size;+ std::vector<std::string> activations;+ std::vector<float> activations_alpha;+ std::vector<float> activations_beta;+ float clip;+ ov::op::RecurrentSequenceDirection direction;+ InputLayerType WRBType;+ ov::element::Type model_type;+ std::tie(mode, seq_lengths, batch, hidden_size, input_size, activations, clip, direction,+ WRBType, model_type, targetDevice) = this->GetParam();++ max_seq_lengths = seq_lengths;+ size_t num_directions = direction == ov::op::RecurrentSequenceDirection::BIDIRECTIONAL ? 2 : 1;+ std::vector<ov::Shape> inputShapes = {+ {batch, seq_lengths, input_size},+ {batch, num_directions, hidden_size},+ {batch, num_directions, hidden_size},+ {batch},+ {num_directions, 4 * hidden_size, input_size},+ {num_directions, 4 * hidden_size, hidden_size},+ {num_directions, 4 * hidden_size},+ };++ const auto& W_shape = inputShapes[4];+ const auto& R_shape = inputShapes[5];+ const auto& B_shape = inputShapes[6];++ std::vector<ov::Shape> param_shapes{inputShapes[0], inputShapes[1], inputShapes[2]};+ std::vector<ov::Shape> const_input_shapes;+ if (mode == SequenceTestsMode::CONVERT_TO_TI_MAX_SEQ_LEN_PARAM ||+ mode == SequenceTestsMode::CONVERT_TO_TI_RAND_SEQ_LEN_PARAM ||+ mode == SequenceTestsMode::PURE_SEQ_RAND_SEQ_LEN_PARAM) {+ param_shapes.push_back(inputShapes[3]);+ }++ if (WRBType == InputLayerType::PARAMETER) {+ param_shapes.push_back(inputShapes[4]);+ param_shapes.push_back(inputShapes[5]);+ param_shapes.push_back(inputShapes[6]);+ }+ init_input_shapes(ov::test::static_shapes_to_test_representation(param_shapes));++ ov::ParameterVector params{std::make_shared<ov::op::v0::Parameter>(model_type, inputDynamicShapes[0]),+ std::make_shared<ov::op::v0::Parameter>(model_type, inputDynamicShapes[1]),+ std::make_shared<ov::op::v0::Parameter>(model_type, inputDynamicShapes[2])};++ std::shared_ptr<ov::Node> seq_lengths_node;+ if (mode == SequenceTestsMode::CONVERT_TO_TI_MAX_SEQ_LEN_PARAM ||+ mode == SequenceTestsMode::CONVERT_TO_TI_RAND_SEQ_LEN_PARAM ||+ mode == SequenceTestsMode::PURE_SEQ_RAND_SEQ_LEN_PARAM) {+ auto param = std::make_shared<ov::op::v0::Parameter>(ov::element::i64, inputDynamicShapes[3]);+ seq_lengths_node = param;+ seq_lengths_node->set_friendly_name("seq_lengths");+ params.push_back(param);+ } else if (mode == SequenceTestsMode::CONVERT_TO_TI_RAND_SEQ_LEN_CONST ||+ mode == SequenceTestsMode::PURE_SEQ_RAND_SEQ_LEN_CONST) {+ ov::test::utils::InputGenerateData in_data;+ in_data.start_from = 0;+ in_data.range = seq_lengths;+ auto tensor = ov::test::utils::create_and_fill_tensor(ov::element::i64, inputShapes[3], in_data);+ seq_lengths_node = std::make_shared<ov::op::v0::Constant>(tensor);+ } else {+ std::vector<int64_t> lengths(inputShapes[3][0], seq_lengths);+ seq_lengths_node = ov::op::v0::Constant::create(ov::element::i64, inputShapes[3], lengths);+ }++ std::shared_ptr<ov::Node> W, R, B;+ if (WRBType == InputLayerType::PARAMETER) {+ auto param_num = inputDynamicShapes.size();+ const auto W_param = std::make_shared<ov::op::v0::Parameter>(model_type, inputDynamicShapes[param_num - 3]);+ const auto R_param = std::make_shared<ov::op::v0::Parameter>(model_type, inputDynamicShapes[param_num - 2]);+ const auto B_param = std::make_shared<ov::op::v0::Parameter>(model_type, inputDynamicShapes[param_num - 1]);+ W = W_param;+ R = R_param;+ B = B_param;+ params.push_back(W_param);+ params.push_back(R_param);+ params.push_back(B_param);+ } else {+ auto tensor_w = ov::test::utils::create_and_fill_tensor_real_distribution(model_type, W_shape, -1, 1, 0);+ W = std::make_shared<ov::op::v0::Constant>(tensor_w);++ auto tensor_r = ov::test::utils::create_and_fill_tensor_real_distribution(model_type, R_shape, -1, 1, 0);+ R = std::make_shared<ov::op::v0::Constant>(tensor_r);++ auto tensor_b = ov::test::utils::create_and_fill_tensor_real_distribution(model_type, B_shape, -1, 1, 0);+ B = std::make_shared<ov::op::v0::Constant>(tensor_b);+ }++ auto lstm_sequence0 = std::make_shared<ov::op::v5::LSTMSequence>(params[0], params[1], params[2], seq_lengths_node, W, R, B, hidden_size, direction,+ std::vector<float>{}, std::vector<float>{}, activations, clip);+ auto lstm_sequence1 = std::make_shared<ov::op::v5::LSTMSequence>(params[0], lstm_sequence0->output(1), lstm_sequence0->output(2),+ seq_lengths_node, W, R, B, hidden_size, direction,+ std::vector<float>{}, std::vector<float>{}, activations, clip);+ auto lstm_sequence2 = std::make_shared<ov::op::v5::LSTMSequence>(params[0], lstm_sequence0->output(1), lstm_sequence0->output(2),+ seq_lengths_node, W, R, B, hidden_size, direction,+ std::vector<float>{}, std::vector<float>{}, activations, clip);+ auto concat0 = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{lstm_sequence1->output(0), lstm_sequence2->output(0)}, 0);+ auto concat1 = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{lstm_sequence1->output(1), lstm_sequence2->output(1)}, 0);+ auto concat2 = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{lstm_sequence1->output(2), lstm_sequence2->output(2)}, 0);++ ov::ResultVector results{std::make_shared<ov::op::v0::Result>(concat0),+ std::make_shared<ov::op::v0::Result>(concat1),+ std::make_shared<ov::op::v0::Result>(concat2)};++ function = std::make_shared<ov::Model>(results, params, "multiple_lstm_sequence");+ bool is_pure_sequence = mode == SequenceTestsMode::PURE_SEQ ||+ mode == SequenceTestsMode::PURE_SEQ_RAND_SEQ_LEN_PARAM ||+ mode == SequenceTestsMode::PURE_SEQ_RAND_SEQ_LEN_CONST;++ if (!is_pure_sequence) {+ ov::pass::Manager manager;+ if (direction == ov::op::RecurrentSequenceDirection::BIDIRECTIONAL)+ manager.register_pass<ov::pass::BidirectionalLSTMSequenceDecomposition>();+ manager.register_pass<ov::pass::ConvertLSTMSequenceToTensorIterator>();+ manager.run_passes(function);+ bool ti_found = ov::test::utils::is_tensor_iterator_exist(function);+ EXPECT_EQ(ti_found, true);+ } else {+ bool ti_found = ov::test::utils::is_tensor_iterator_exist(function);+ EXPECT_EQ(ti_found, false);+ }+ if (model_type == ov::element::f16) {+ rel_threshold = 0.03f;+ abs_threshold = 0.0025f;+ if (activations == std::vector<std::string>{"tanh", "tanh", "tanh"}) {+ rel_threshold = 0.05f;+ abs_threshold = 0.005f;+ }+ }+}++void generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) override {+ inputs.clear();+ const auto& func_inputs = function->inputs();+ ov::test::utils::InputGenerateData in_data;+ in_data.start_from = 0;+ in_data.range = 10;++ for (size_t i = 0; i < func_inputs.size(); ++i) {+ ov::Tensor tensor;++ if (i == 3) {+ in_data.range = max_seq_lengths;+ } else {+ in_data.range = 10;+ }+ if (i < 3) {+ tensor = ov::test::utils::create_and_fill_tensor_real_distribution(func_inputs[i].get_element_type(), targetInputStaticShapes[i], 0, 1, 0);+ } else {+ tensor = ov::test::utils::create_and_fill_tensor(func_inputs[i].get_element_type(), targetInputStaticShapes[i], in_data);+ }+ inputs.insert({func_inputs[i].get_node_shared_ptr(), tensor});+ }+}+};++std::vector<std::vector<std::string>> multilstmseq_activations = {{"sigmoid", "tanh", "tanh"}};++TEST_P(MultipleLSTMSequenceGPUTest, Inference) {+ run();+};++// Check whether inputs of LSTMSequence have right idx and weight optimization works properly when constant is shared by multiple LSTMSequence+// for onednn primitive+INSTANTIATE_TEST_SUITE_P(MultipleLSTMSequenceGPUTest, MultipleLSTMSequenceGPUTest,+ ::testing::Combine(+ ::testing::Values(ov::test::utils::SequenceTestsMode::PURE_SEQ),+ ::testing::ValuesIn(seq_lengths_zero_clip),+ ::testing::ValuesIn(batch),+ ::testing::ValuesIn(hidden_size),+ ::testing::ValuesIn(input_size),+ ::testing::ValuesIn(multilstmseq_activations),+ ::testing::ValuesIn(clip),+ ::testing::Values(ov::op::RecurrentSequenceDirection::FORWARD),+ ::testing::Values(ov::test::utils::InputLayerType::CONSTANT),+ ::testing::Values(ov::element::f16),+ ::testing::Values(ov::test::utils::DEVICE_GPU)),+ LSTMSequenceGPUTest::getTestCaseName);} // namespace