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
Add gather for ShapeOf in optimize value tensor pass and corresponding test case (#38841)
liang97968ef66729
diff --git a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/optimize_value_tensors.cpp b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/optimize_value_tensors.cppindex 686009fb499..f538e0da32b 100644--- a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/optimize_value_tensors.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/optimize_value_tensors.cpp@@ -4,8 +4,10 @@#include "optimize_value_tensors.hpp"+#include <algorithm>#include <transformations/op_conversions/scaled_dot_product_attention_decomposition.hpp>#include <unordered_set>+#include <vector>#include "../llm_compiled_model_utils.hpp"#include "../logging.hpp"@@ -34,6 +36,43 @@ public:using Ref = std::reference_wrapper<Context>;bool bTransposed = false;std::unordered_set<const ov::op::v0::Parameter*> transposed_params;+ ov::OutputVector changed_outputs;++ void track(const ov::Output<ov::Node>& output) {+ if (std::find(changed_outputs.begin(), changed_outputs.end(), output) == changed_outputs.end()) {+ changed_outputs.push_back(output);+ }+ }++ void restore_shape_of_consumers() const {+ for (const auto& output : changed_outputs) {+ const auto targets = output.get_target_inputs();+ for (const auto& input : targets) {+ auto consumer = input.get_node()->shared_from_this();+ if (!ov::is_type<ov::op::v0::ShapeOf>(consumer) && !ov::is_type<ov::op::v3::ShapeOf>(consumer)) {+ continue;+ }+ const auto& rank = output.get_partial_shape().rank();+ OPENVINO_ASSERT(rank.is_static() && rank.get_length() >= 2,+ "Cannot restore ShapeOf dimension order for a value tensor with dynamic rank");+ std::vector<int64_t> order(static_cast<size_t>(rank.get_length()));+ for (size_t i = 0; i < order.size(); ++i) {+ order[i] = static_cast<int64_t>(i);+ }+ std::swap(order[order.size() - 2], order[order.size() - 1]);+ auto order_constant =+ ov::op::v0::Constant::create(ov::element::i64, ov::Shape{order.size()}, order);+ // Reorder only the shape vector, preserving runtime dimensions and all data edges.+ auto shape_of = consumer->clone_with_new_inputs({output});+ shape_of->set_friendly_name(consumer->get_friendly_name() + "/transposed_layout");+ auto axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {0});+ auto restore_shape = std::make_shared<ov::op::v8::Gather>(shape_of, order_constant, axis);+ restore_shape->set_friendly_name(consumer->get_friendly_name());+ ov::copy_runtime_info(consumer, {shape_of, restore_shape});+ ov::replace_node(consumer, restore_shape);+ }+ }+ }};protected:@@ -43,8 +82,10 @@ protected:const std::shared_ptr<ov::op::v0::Concat>& matched_concat,const std::shared_ptr<ov::op::v1::Transpose>& matched_transpose,const std::shared_ptr<ov::op::v0::MatMul>& matched_matmul) {- // A V Parameter may feed multiple matched attention branches. Guard its shape mutation- // so it is applied exactly once; per-branch updates are always applied.+ // NB: The same param->concat pair may be matched multiple times when the+ // V-concat output feeds more than one downstream branch (e.g. shared KV-cache+ // across attention layers in Gemma4). Guard the shared-state mutations so they+ // are applied exactly once; per-branch matmul transpose_b is always set.if (matched_concat->get_axis() != 3u) {if (ctx.get().transposed_params.insert(matched_param.get()).second) {auto param_shape = matched_param->get_partial_shape();@@ -58,7 +99,20 @@ protected:auto order_cst = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{4}, {0, 2, 3, 1});matched_transpose->set_argument(1, order_cst);matched_concat->set_axis(3u);+ for (size_t i = 0; i < matched_concat->get_input_size(); ++i) {+ const auto input = matched_concat->input_value(i);+ if (input.get_node_shared_ptr() == matched_param) {+ ctx.get().track(matched_param->output(0));+ } else if (auto convert = ov::as_type_ptr<ov::op::v0::Convert>(input.get_node_shared_ptr())) {+ if (convert->input_value(0).get_node_shared_ptr() == matched_param) {+ ctx.get().track(matched_param->output(0));+ ctx.get().track(convert->output(0));+ }+ }+ }+ ctx.get().track(matched_transpose->output(0));}+ ctx.get().track(matched_concat->output(0));matched_matmul->set_transpose_b(true);ctx.get().bTransposed = true;}@@ -104,6 +158,7 @@ public:const auto order = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{4}, {0, 2, 3, 1});matched_transpose->set_argument(1, order);matched_matmul->set_transpose_b(true);+ ctx.get().track(matched_transpose->output(0));ctx.get().bTransposed = true;LOG_DEBUG("vtensors transposed: Whisper cross-attention prefill pattern");return true;@@ -142,6 +197,11 @@ public:auto transposed_shape = shape;std::swap(transposed_shape[2], transposed_shape[3]);matched_param->set_partial_shape(transposed_shape);+ ctx.get().track(matched_param->output(0));+ }+ const auto matched_convert = node_to_output.find(convert);+ if (matched_convert != node_to_output.end()) {+ ctx.get().track(matched_convert->second);}matched_matmul->set_transpose_b(true);ctx.get().bTransposed = true;@@ -300,6 +360,9 @@ private:matched_reshape->input(1).replace_source_output(reshape_axes_node);transpose_matmul_b(ctx, matched_param, matched_concat, matched_transpose, matched_matmul);+ ctx.get().track(matched_unsqueeze->output(0));+ ctx.get().track(matched_broadcast->output(0));+ ctx.get().track(matched_reshape->output(0));LOG_DEBUG("vtensors transposed: GQA pattern");return true;};@@ -457,6 +520,8 @@ bool ov::npuw::util::OptimizeValueTensors::run_on_model(const std::shared_ptr<ovrewr.run_on_model(model);+ ctx.restore_shape_of_consumers();+ov::pass::Validate().run_on_model(model);// NB: matmul parameters gets transposed, if pass applieddiff --git a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/optimize_value_tensors.hpp b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/optimize_value_tensors.hppindex 4369bb6d521..c750227bbe6 100644--- a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/optimize_value_tensors.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/optimize_value_tensors.hpp@@ -10,7 +10,9 @@ namespace ov {namespace npuw {namespace util {-// SDPA-unroll and transpose transformations+// Decompose SDPA and transpose V tensors, including past/present KV-cache ports.+// Direct ShapeOf consumers use a Gather to restore dimension order without freezing dynamic shapes.+// Other consumers are not rewired by the ShapeOf correction.class OptimizeValueTensors : public ov::pass::ModelPass {bool m_is_prefill;bool m_is_whisper;diff --git a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/optimize_value_tensors_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/optimize_value_tensors_test.cppindex e95dfdbb909..1088d593e6d 100644--- a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/optimize_value_tensors_test.cpp+++ b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/optimize_value_tensors_test.cpp@@ -6,14 +6,21 @@#include <gtest/gtest.h>+#include <tuple>++#include "embedding/redirect_new_kv_to_output.hpp"#include "llm_pass_test_fixture.hpp"#include "openvino/op/broadcast.hpp"#include "openvino/op/concat.hpp"#include "openvino/op/constant.hpp"+#include "openvino/op/convert.hpp"+#include "openvino/op/gather.hpp"#include "openvino/op/matmul.hpp"#include "openvino/op/parameter.hpp"#include "openvino/op/reshape.hpp"+#include "openvino/op/result.hpp"#include "openvino/op/scaled_dot_product_attention.hpp"+#include "openvino/op/shape_of.hpp"#include "openvino/op/softmax.hpp"#include "openvino/op/transpose.hpp"#include "openvino/op/unsqueeze.hpp"@@ -265,4 +272,177 @@ TEST_F(OptimizeValueTensorsPassTest, SharedGqaConcatGenerateValueIsTransposedOnc}}+class OptimizeValueTensorsCacheLayoutTest : public ov::test::npuw::LLMPassTestFixture,+ public ::testing::WithParamInterface<std::tuple<bool, bool, size_t>> {};++TEST_P(OptimizeValueTensorsCacheLayoutTest, ShapeCorrectionLeavesDataConsumersConnected) {+ const auto [gqa, prefill, past_length] = GetParam();+ const size_t heads = 2;+ const size_t query_heads = gqa ? 4 : heads;+ const size_t head_size = 4;+ const size_t new_length = prefill ? 3 : 1;+ const size_t total_length = past_length + new_length;+ auto past = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1, heads, past_length, head_size});+ auto value = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1, new_length, heads, head_size});+ auto order = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{4}, {0, 2, 1, 3});+ auto transpose = std::make_shared<ov::op::v1::Transpose>(value, order);+ auto concat = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{past, transpose}, 2);+ ov::Output<ov::Node> attention_value = concat;+ ov::OutputVector shape_outputs;+ std::vector<ov::Shape> original_shapes;+ if (gqa) {+ auto axes = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {2});+ auto unsqueeze = std::make_shared<ov::op::v0::Unsqueeze>(concat, axes);+ auto broadcast_shape =+ ov::op::v0::Constant::create(ov::element::i64,+ ov::Shape{5},+ ov::Shape{1, heads, query_heads / heads, total_length, head_size});+ auto broadcast = std::make_shared<ov::op::v3::Broadcast>(unsqueeze, broadcast_shape);+ auto reshape_shape = ov::op::v0::Constant::create(ov::element::i64,+ ov::Shape{4},+ ov::Shape{1, query_heads, total_length, head_size});+ attention_value = std::make_shared<ov::op::v1::Reshape>(broadcast, reshape_shape, false);+ for (const auto& output : ov::OutputVector{unsqueeze, broadcast, attention_value}) {+ original_shapes.push_back(output.get_shape());+ shape_outputs.push_back(std::make_shared<ov::op::v3::ShapeOf>(output));+ }+ }+ auto scores =+ std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1, query_heads, new_length, total_length});+ auto softmax = std::make_shared<ov::op::v8::Softmax>(scores);+ auto matmul = std::make_shared<ov::op::v0::MatMul>(softmax, attention_value);+ auto cache_result = std::make_shared<ov::op::v0::Result>(concat);+ cache_result->set_friendly_name("present.0.value");+ auto ordinary_result = std::make_shared<ov::op::v0::Result>(concat);+ ordinary_result->set_friendly_name("debug_value");+ auto external_consumer = std::make_shared<ov::op::v0::Convert>(concat, ov::element::f16);+ ov::OutputVector outputs{matmul, cache_result, ordinary_result, external_consumer};+ outputs.insert(outputs.end(), shape_outputs.begin(), shape_outputs.end());+ auto model = std::make_shared<ov::Model>(outputs, ov::ParameterVector{past, value, scores});++ ASSERT_TRUE(ov::npuw::util::OptimizeValueTensors(prefill).run_on_model(model));+ EXPECT_EQ(past->get_shape(), (ov::Shape{1, heads, head_size, past_length}));+ EXPECT_EQ(cache_result->input_value(0), concat->output(0));+ EXPECT_EQ(cache_result->get_shape(), (ov::Shape{1, heads, head_size, total_length}));+ EXPECT_EQ(ordinary_result->input_value(0), concat->output(0));+ EXPECT_EQ(external_consumer->input_value(0), concat->output(0));+ EXPECT_EQ(ordinary_result->get_shape(), (ov::Shape{1, heads, head_size, total_length}));+ EXPECT_EQ(external_consumer->get_shape(), (ov::Shape{1, heads, head_size, total_length}));+ EXPECT_EQ(count_ops<ov::op::v1::Transpose>(model), 1u);+ EXPECT_TRUE(matmul->get_transpose_b());+ EXPECT_EQ(matmul->get_shape(), (ov::Shape{1, query_heads, new_length, head_size}));+ if (gqa) {+ ov::OutputVector restored_shapes;+ ov::TensorVector shape_values;+ for (size_t i = 0; i < shape_outputs.size(); ++i) {+ auto restored_shape = model->get_results().at(4 + i)->input_value(0);+ ASSERT_TRUE(ov::is_type<ov::op::v8::Gather>(restored_shape.get_node()));+ restored_shapes.push_back(restored_shape);+ shape_values.emplace_back(ov::element::i64, ov::Shape{original_shapes[i].size()});+ }+ auto shape_model = std::make_shared<ov::Model>(restored_shapes, ov::ParameterVector{past, value});+ EXPECT_EQ(count_ops<ov::op::v1::Transpose>(shape_model), 1u);+ ASSERT_TRUE(shape_model->evaluate(shape_values,+ ov::TensorVector{ov::Tensor(ov::element::f32, past->get_shape()),+ ov::Tensor(ov::element::f32, value->get_shape())}));+ for (size_t i = 0; i < shape_values.size(); ++i) {+ EXPECT_EQ(std::vector<int64_t>(shape_values[i].data<int64_t>(),+ shape_values[i].data<int64_t>() + original_shapes[i].size()),+ std::vector<int64_t>(original_shapes[i].begin(), original_shapes[i].end()));+ }+ }++ ov::npuw::RedirectNewKvToOutput().run_on_model(model);+ EXPECT_EQ(cache_result->input_value(0), transpose->output(0));+ EXPECT_EQ(cache_result->get_shape(), (ov::Shape{1, heads, head_size, new_length}));+ EXPECT_EQ(cache_result->get_friendly_name(), "present.0.value");+ EXPECT_EQ(external_consumer->input_value(0), concat->output(0));+ EXPECT_EQ(external_consumer->get_shape(), (ov::Shape{1, heads, head_size, total_length}));+}++INSTANTIATE_TEST_SUITE_P(MhaAndGqa,+ OptimizeValueTensorsCacheLayoutTest,+ ::testing::Combine(::testing::Bool(),+ ::testing::Bool(),+ ::testing::Values(size_t{0}, size_t{8})),+ [](const ::testing::TestParamInfo<OptimizeValueTensorsCacheLayoutTest::ParamType>& info) {+ const auto gqa = std::get<0>(info.param);+ const auto prefill = std::get<1>(info.param);+ const auto past_length = std::get<2>(info.param);+ return std::string(gqa ? "GQA" : "MHA") + (prefill ? "_Prefill" : "_Generate") + "_Past" ++ std::to_string(past_length);+ });++class OptimizeValueTensorsDynamicShapeTest : public ov::test::npuw::LLMPassTestFixture,+ public ::testing::WithParamInterface<std::tuple<bool, bool, int>> {};++TEST_P(OptimizeValueTensorsDynamicShapeTest, ShapeQueriesPreserveRuntimeDimensions) {+ const auto [prefill, with_convert, shape_version] = GetParam();+ auto value = std::make_shared<ov::op::v0::Parameter>(+ ov::element::f32,+ prefill ? ov::PartialShape{1, ov::Dimension(1, 16), 2, ov::Dimension(1, 16)}+ : ov::PartialShape{1, 2, ov::Dimension(1, 16), ov::Dimension(1, 16)});+ ov::Output<ov::Node> attention_value = value;+ if (with_convert) {+ attention_value = std::make_shared<ov::op::v0::Convert>(attention_value, ov::element::f32);+ }+ if (prefill) {+ auto order = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{4}, {0, 2, 1, 3});+ attention_value = std::make_shared<ov::op::v1::Transpose>(attention_value, order);+ }+ std::shared_ptr<ov::Node> shape_of;+ if (shape_version == 0) {+ shape_of = std::make_shared<ov::op::v0::ShapeOf>(attention_value);+ } else {+ shape_of = std::make_shared<ov::op::v3::ShapeOf>(attention_value,+ shape_version == 1 ? ov::element::i32 : ov::element::i64);+ }+ shape_of->set_friendly_name("logical_value_shape");+ shape_of->get_rt_info()["test_shape_attribute"] = std::string("preserved");+ auto shape_reference = std::make_shared<ov::Model>(ov::OutputVector{shape_of}, ov::ParameterVector{value})->clone();+ ov::OutputVector outputs;+ ov::ParameterVector parameters{value};+ for (size_t query_length : {1u, 2u}) {+ auto scores = std::make_shared<ov::op::v0::Parameter>(+ ov::element::f32,+ ov::PartialShape{1, 2, static_cast<int64_t>(query_length), ov::Dimension(1, 16)});+ auto softmax = std::make_shared<ov::op::v8::Softmax>(scores);+ outputs.push_back(std::make_shared<ov::op::v0::MatMul>(softmax, attention_value));+ parameters.push_back(scores);+ }+ outputs.push_back(shape_of);+ auto model = std::make_shared<ov::Model>(outputs, parameters);+ ASSERT_TRUE(ov::npuw::util::OptimizeValueTensors(prefill, true).run_on_model(model));+ auto restored_shape = model->get_results().back()->input_value(0);+ ASSERT_TRUE(ov::is_type<ov::op::v8::Gather>(restored_shape.get_node()));+ EXPECT_EQ(restored_shape.get_element_type(), shape_of->get_output_element_type(0));+ EXPECT_EQ(restored_shape.get_node()->get_friendly_name(), "logical_value_shape");+ EXPECT_EQ(restored_shape.get_node()->get_rt_info().at("test_shape_attribute").as<std::string>(), "preserved");+ auto shape_model = std::make_shared<ov::Model>(ov::OutputVector{restored_shape}, ov::ParameterVector{value});+ EXPECT_EQ(count_ops<ov::op::v1::Transpose>(shape_model), prefill ? 1u : 0u);+ EXPECT_TRUE(value->get_partial_shape().is_dynamic());+ for (const auto& dimensions : {ov::Shape{3, 4}, ov::Shape{7, 9}}) {+ const auto sequence = dimensions[0];+ const auto embedding = dimensions[1];+ const auto logical = prefill ? ov::Shape{1, sequence, 2, embedding} : ov::Shape{1, 2, sequence, embedding};+ const auto physical = prefill ? logical : ov::Shape{1, 2, embedding, sequence};+ const auto type = restored_shape.get_element_type();+ ov::TensorVector expected{ov::Tensor(type, ov::Shape{4})};+ ov::TensorVector actual{ov::Tensor(type, ov::Shape{4})};+ ASSERT_TRUE(shape_reference->evaluate(expected, ov::TensorVector{ov::Tensor(ov::element::f32, logical)}));+ ASSERT_TRUE(shape_model->evaluate(actual, ov::TensorVector{ov::Tensor(ov::element::f32, physical)}));+ for (size_t i = 0; i < 4; ++i) {+ if (type == ov::element::i32) {+ EXPECT_EQ(actual[0].data<int32_t>()[i], expected[0].data<int32_t>()[i]);+ } else {+ EXPECT_EQ(actual[0].data<int64_t>()[i], expected[0].data<int64_t>()[i]);+ }+ }+ }+}++INSTANTIATE_TEST_SUITE_P(ShapeOfVersions,+ OptimizeValueTensorsDynamicShapeTest,+ ::testing::Combine(::testing::Bool(), ::testing::Bool(), ::testing::Values(0, 1, 2)));+} // namespacediff --git a/src/plugins/intel_npu/tests/unit/npuw/transpose_vt.cpp b/src/plugins/intel_npu/tests/unit/npuw/transpose_vt.cppindex 4684bd0182d..2f5ebc9ce9f 100644--- a/src/plugins/intel_npu/tests/unit/npuw/transpose_vt.cpp+++ b/src/plugins/intel_npu/tests/unit/npuw/transpose_vt.cpp@@ -584,3 +584,252 @@ TEST(TransposeVTMQATest, smoke_GQA_ReshapeAsTranspose_MQA) {ASSERT_TRUE(ov::is_type<ov::op::v1::Transpose>(concat_v_input))<< "concat port 1 should now be fed by a real Transpose; got: " << concat_v_input->get_type_name();}++// Regression test for a ShapeOf consumer outside the optimized GQA value path.+// Restore the logical shape using Gather without freezing dimensions or transposing data.+//+// Before optimization (B=1, Hkv=2, Hq=8, S=8, D=64, L=S+1):+// past_value[B,Hkv,S,D] new_value[B,1,Hkv,D]+// | |+// | Transpose{0,2,1,3}+// +-----> Concat(axis=2)[B,Hkv,L,D]+// | |+// Unsqueeze[B,Hkv,1,L,D] ShapeOf+// | |+// Broadcast[B,Hkv,4,L,D] Gather{2} -> L+// | |+// Reshape[B,Hq,L,D] Concat{1,1,1,L} -> Broadcast(0)+// | |+// | scores[B,Hq,1,L] -> Add+// | |+// +----> MatMul <--------------------- Softmax+//+// After OptimizeValueTensors:+// past_value[B,Hkv,D,S] + new V via Transpose{0,2,3,1}+// -> Concat(axis=3)[B,Hkv,D,L]+// | |+// Unsqueeze[B,Hkv,1,D,L] ShapeOf+// | |+// Broadcast[B,Hkv,4,D,L] Gather{0,1,3,2}+// | |+// Reshape[B,Hq,D,L] Gather{2} -> L+// | |+// | original mask branch+// +----> MatMul <-------- Softmax+// transpose_b=true+// The test evaluates the restored shape and checks mask/MatMul output shapes.+// Only the MatMul has a Result in the original test graph; no direct cache Results.+TEST(TransposeVTShapeComputeTest, smoke_GQA_ShapeOfVFeedsAttentionMask) {+ const int64_t B = 1, kv_heads = 2, q_heads = 8, S = 8, D = 64;+ const int64_t L = S + 1;++ auto past_value = std::make_shared<ov::op::v0::Parameter>(ov::element::f32,+ ov::Shape{static_cast<size_t>(B),+ static_cast<size_t>(kv_heads),+ static_cast<size_t>(S),+ static_cast<size_t>(D)});+ auto new_value = std::make_shared<ov::op::v0::Parameter>(+ ov::element::f32,+ ov::Shape{static_cast<size_t>(B), 1, static_cast<size_t>(kv_heads), static_cast<size_t>(D)});+ auto transpose_order = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{4}, {0, 2, 1, 3});+ auto transpose = std::make_shared<ov::op::v1::Transpose>(new_value, transpose_order);+ auto concat = std::make_shared<ov::op::v0::Concat>(ov::NodeVector{past_value, transpose}, 2);+ auto unsqueeze_axes = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {2});+ auto unsqueeze = std::make_shared<ov::op::v0::Unsqueeze>(concat, unsqueeze_axes);+ auto broadcast_shape = ov::op::v0::Constant::create(ov::element::i64,+ ov::Shape{5},+ std::vector<int64_t>{B, kv_heads, q_heads / kv_heads, L, D});+ auto broadcast =+ std::make_shared<ov::op::v3::Broadcast>(unsqueeze, broadcast_shape, ov::op::BroadcastType::BIDIRECTIONAL);+ auto reshape_shape =+ ov::op::v0::Constant::create(ov::element::i64, ov::Shape{4}, std::vector<int64_t>{B, q_heads, L, D});+ auto reshape = std::make_shared<ov::op::v1::Reshape>(broadcast, reshape_shape, false);++ auto value_shape = std::make_shared<ov::op::v3::ShapeOf>(concat);+ auto kv_len =+ std::make_shared<ov::op::v8::Gather>(value_shape,+ ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {2}),+ ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {0}));+ auto mask_shape = std::make_shared<ov::op::v0::Concat>(+ ov::OutputVector{ov::op::v0::Constant::create(ov::element::i64, ov::Shape{3}, {1, 1, 1}), kv_len},+ 0);+ auto mask =+ std::make_shared<ov::op::v3::Broadcast>(ov::op::v0::Constant::create(ov::element::f32, ov::Shape{}, {0.f}),+ mask_shape);+ auto attention_scores = std::make_shared<ov::op::v0::Parameter>(+ ov::element::f32,+ ov::Shape{static_cast<size_t>(B), static_cast<size_t>(q_heads), 1, static_cast<size_t>(L)});+ auto add = std::make_shared<ov::op::v1::Add>(attention_scores, mask);+ auto softmax = std::make_shared<ov::op::v8::Softmax>(add, -1);+ auto matmul = std::make_shared<ov::op::v0::MatMul>(softmax, reshape);+ auto model = std::make_shared<ov::Model>(ov::ResultVector{std::make_shared<ov::op::v0::Result>(matmul)},+ ov::ParameterVector{past_value, new_value, attention_scores});++ bool transposed = false;+ ASSERT_NO_THROW(transposed = ov::npuw::util::OptimizeValueTensors(false).run_on_model(model));+ ASSERT_TRUE(transposed);+ ASSERT_EQ(concat->get_axis(), 3);+ auto restored_shape = ov::as_type_ptr<ov::op::v8::Gather>(kv_len->get_input_node_shared_ptr(0));+ ASSERT_NE(restored_shape, nullptr);+ auto shape_of = ov::as_type_ptr<ov::op::v3::ShapeOf>(restored_shape->get_input_node_shared_ptr(0));+ ASSERT_NE(shape_of, nullptr);+ ASSERT_EQ(shape_of->input_value(0), concat->output(0));+ auto shape_model =+ std::make_shared<ov::Model>(ov::OutputVector{restored_shape}, ov::ParameterVector{past_value, new_value});+ ov::TensorVector shapes{ov::Tensor(ov::element::i64, ov::Shape{4})};+ ASSERT_TRUE(shape_model->evaluate(shapes,+ ov::TensorVector{ov::Tensor(ov::element::f32, past_value->get_shape()),+ ov::Tensor(ov::element::f32, new_value->get_shape())}));+ EXPECT_EQ(std::vector<int64_t>(shapes[0].data<int64_t>(), shapes[0].data<int64_t>() + 4),+ (std::vector<int64_t>{B, kv_heads, L, D}));+ ASSERT_EQ(add->get_output_partial_shape(0), ov::PartialShape({B, q_heads, 1, L}));+ ASSERT_EQ(matmul->get_output_partial_shape(0), ov::PartialShape({B, q_heads, 1, D}));+}++namespace {++void expect_shape_results(const std::shared_ptr<ov::Model>& model,+ const ov::ParameterVector& parameters,+ const std::vector<ov::Shape>& expected_shapes) {+ auto results = model->get_results();+ ASSERT_EQ(results.size(), expected_shapes.size() + 1);++ ov::OutputVector shape_outputs;+ ov::TensorVector output_tensors;+ for (size_t i = 0; i < expected_shapes.size(); ++i) {+ auto output = results[i + 1]->input_value(0);+ shape_outputs.push_back(output);+ output_tensors.emplace_back(output.get_element_type(), ov::Shape{expected_shapes[i].size()});+ }++ ov::TensorVector input_tensors;+ for (const auto& parameter : parameters) {+ input_tensors.emplace_back(parameter->get_element_type(), parameter->get_shape());+ }++ auto shape_model = std::make_shared<ov::Model>(shape_outputs, parameters);+ ASSERT_TRUE(shape_model->evaluate(output_tensors, input_tensors));+ for (size_t i = 0; i < expected_shapes.size(); ++i) {+ std::vector<int64_t> expected(expected_shapes[i].begin(), expected_shapes[i].end());+ if (output_tensors[i].get_element_type() == ov::element::i32) {+ const auto* values = output_tensors[i].data<int32_t>();+ std::vector<int32_t> expected_i32;+ expected_i32.reserve(expected.size());+ for (const auto dimension : expected) {+ expected_i32.push_back(static_cast<int32_t>(dimension));+ }+ EXPECT_EQ(std::vector<int32_t>(values, values + expected.size()), expected_i32);+ } else {+ const auto* values = output_tensors[i].data<int64_t>();+ EXPECT_EQ(std::vector<int64_t>(values, values + expected.size()), expected);+ }+ }+}++} // namespace++TEST(TransposeVTShapeComputeTest, smoke_MHA_ShapeOfTrackedValues) {+ const size_t B = 1, H = 2, S = 4, D = 3;+ auto past_value = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::Shape{B, H, S, D});+ auto convert = std::make_shared<ov::op::v0::Convert>(past_value, ov::element::f32);+ auto new_value = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{B, 1, H * D});+ auto reshape_shape =+ ov::op::v0::Constant::create(ov::element::i64,+ ov::Shape{4},+ std::vector<int64_t>{0, 0, static_cast<int64_t>(H), static_cast<int64_t>(D)});+ auto reshape = std::make_shared<ov::op::v1::Reshape>(new_value, reshape_shape, true);+ auto transpose_order = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{4}, {0, 2, 1, 3});+ auto transpose = std::make_shared<ov::op::v1::Transpose>(reshape, transpose_order);+ auto concat = std::make_shared<ov::op::v0::Concat>(ov::NodeVector{convert, transpose}, 2);+ auto scores = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{B, H, 1, S + 1});+ auto matmul = std::make_shared<ov::op::v0::MatMul>(std::make_shared<ov::op::v8::Softmax>(scores, -1), concat);++ auto past_shape = std::make_shared<ov::op::v3::ShapeOf>(past_value);+ auto convert_shape = std::make_shared<ov::op::v3::ShapeOf>(convert, ov::element::i32);+ auto transpose_shape = std::make_shared<ov::op::v3::ShapeOf>(transpose);+ auto model = std::make_shared<ov::Model>(ov::OutputVector{matmul, past_shape, convert_shape, transpose_shape},+ ov::ParameterVector{past_value, new_value, scores});++ ASSERT_TRUE(ov::npuw::util::OptimizeValueTensors(false).run_on_model(model));+ expect_shape_results(model,+ ov::ParameterVector{past_value, new_value, scores},+ {{B, H, S, D}, {B, H, S, D}, {B, H, 1, D}});+}++TEST(TransposeVTShapeComputeTest, smoke_GQA_ShapeOfTrackedValues) {+ const size_t B = 1, kv_heads = 2, q_heads = 8, S = 4, D = 3;+ const size_t L = S + 1;+ auto past_value = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::Shape{B, kv_heads, S, D});+ auto convert = std::make_shared<ov::op::v0::Convert>(past_value, ov::element::f32);+ auto new_value = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{B, 1, kv_heads, D});+ auto transpose_order = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{4}, {0, 2, 1, 3});+ auto transpose = std::make_shared<ov::op::v1::Transpose>(new_value, transpose_order);+ auto concat = std::make_shared<ov::op::v0::Concat>(ov::NodeVector{convert, transpose}, 2);+ auto unsqueeze_axes = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {2});+ auto unsqueeze = std::make_shared<ov::op::v0::Unsqueeze>(concat, unsqueeze_axes);+ auto broadcast_shape = ov::op::v0::Constant::create(ov::element::i64,+ ov::Shape{5},+ std::vector<int64_t>{B, kv_heads, q_heads / kv_heads, L, D});+ auto broadcast = std::make_shared<ov::op::v3::Broadcast>(unsqueeze, broadcast_shape);+ auto reshape_shape =+ ov::op::v0::Constant::create(ov::element::i64, ov::Shape{4}, std::vector<int64_t>{B, q_heads, L, D});+ auto reshape = std::make_shared<ov::op::v1::Reshape>(broadcast, reshape_shape, false);+ auto scores = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{B, q_heads, 1, L});+ auto matmul = std::make_shared<ov::op::v0::MatMul>(std::make_shared<ov::op::v8::Softmax>(scores, -1), reshape);++ auto past_shape = std::make_shared<ov::op::v3::ShapeOf>(past_value);+ auto convert_shape = std::make_shared<ov::op::v3::ShapeOf>(convert);+ auto transpose_shape = std::make_shared<ov::op::v3::ShapeOf>(transpose);+ auto concat_shape = std::make_shared<ov::op::v3::ShapeOf>(concat);+ auto unsqueeze_shape = std::make_shared<ov::op::v3::ShapeOf>(unsqueeze);+ auto broadcast_shape_of = std::make_shared<ov::op::v3::ShapeOf>(broadcast, ov::element::i32);+ auto reshape_shape_of = std::make_shared<ov::op::v3::ShapeOf>(reshape);+ auto model = std::make_shared<ov::Model>(ov::OutputVector{matmul,+ past_shape,+ convert_shape,+ transpose_shape,+ concat_shape,+ unsqueeze_shape,+ broadcast_shape_of,+ reshape_shape_of},+ ov::ParameterVector{past_value, new_value, scores});++ ASSERT_TRUE(ov::npuw::util::OptimizeValueTensors(false).run_on_model(model));+ expect_shape_results(model,+ ov::ParameterVector{past_value, new_value, scores},+ {{B, kv_heads, S, D},+ {B, kv_heads, S, D},+ {B, kv_heads, 1, D},+ {B, kv_heads, L, D},+ {B, kv_heads, 1, L, D},+ {B, kv_heads, q_heads / kv_heads, L, D},+ {B, q_heads, L, D}});+}++TEST(TransposeVTShapeComputeTest, smoke_WhisperGenerate_ShapeOfParameter) {+ const size_t B = 1, H = 2, S = 4, D = 3;+ auto past_value = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{B, H, S, D});+ auto scores = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{B, H, 1, S});+ auto matmul = std::make_shared<ov::op::v0::MatMul>(std::make_shared<ov::op::v8::Softmax>(scores, -1), past_value);+ auto past_shape = std::make_shared<ov::op::v3::ShapeOf>(past_value);+ auto model =+ std::make_shared<ov::Model>(ov::OutputVector{matmul, past_shape}, ov::ParameterVector{past_value, scores});++ ASSERT_TRUE(ov::npuw::util::OptimizeValueTensors(false, true).run_on_model(model));+ expect_shape_results(model, ov::ParameterVector{past_value, scores}, {{B, H, S, D}});+}++TEST(TransposeVTShapeComputeTest, smoke_WhisperPrefill_ShapeOfTranspose) {+ const size_t B = 1, H = 2, S = 4, D = 3;+ auto value = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{B, S, H, D});+ auto transpose_order = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{4}, {0, 2, 1, 3});+ auto transpose = std::make_shared<ov::op::v1::Transpose>(value, transpose_order);+ auto scores = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{B, H, 1, S});+ auto matmul = std::make_shared<ov::op::v0::MatMul>(std::make_shared<ov::op::v8::Softmax>(scores, -1), transpose);+ auto transpose_shape = std::make_shared<ov::op::v3::ShapeOf>(transpose, ov::element::i32);+ auto model =+ std::make_shared<ov::Model>(ov::OutputVector{matmul, transpose_shape}, ov::ParameterVector{value, scores});++ ASSERT_TRUE(ov::npuw::util::OptimizeValueTensors(true, true).run_on_model(model));+ expect_shape_results(model, ov::ParameterVector{value, scores}, {{B, H, S, D}});+}