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 passes tests (#34827)
Alexander Kalistratove277ebaab762
diff --git a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/convert_kvcache_to_precision_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/convert_kvcache_to_precision_test.cppnew file mode 100644index 00000000000..7a60c42f2c5--- /dev/null+++ b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/convert_kvcache_to_precision_test.cpp@@ -0,0 +1,124 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include <gtest/gtest.h>++#include <sstream>+#include <string>++#include "llm_pass_test_fixture.hpp"+#include "openvino/runtime/properties.hpp"++// --- Design note -------------------------------------------------------------------------+// The model builder creates KV cache state with ov::element::f32 (the default+// ModelConfig::precision). ConvertKVCacheToPrecision is therefore doing *real*+// work on the test model: it lowers f32 past_key inputs and present outputs to+// the requested storage type (f16 by default, or whatever+// ov::hint::kv_cache_precision selects).+// -----------------------------------------------------------------------------------------++namespace {++using ov::test::npuw::RecordingFactory;++// --- Parametrized fixture -------------------------------------------------------------------------+// Parametrized over ov::element::Type so that f16, f8e4m3, and f8e5m2 are each+// exercised in exactly the same test bodies.++class ConvertKVCacheHintPrecisionTest : public ov::test::npuw::LLMPassTestFixture,+ public ::testing::WithParamInterface<ov::element::Type> {};++INSTANTIATE_TEST_SUITE_P(+ KVCachePrecisions,+ ConvertKVCacheHintPrecisionTest,+ ::testing::Values(ov::element::f16, ov::element::f8e4m3, ov::element::f8e5m2),+ [](const ::testing::TestParamInfo<ov::element::Type>& info) -> std::string {+ std::ostringstream ss;+ ss << info.param;+ return ss.str();+ });++// past_key inputs of the generate model have the requested precision.+TEST_P(ConvertKVCacheHintPrecisionTest, GenerateModelPastKeyInputsHaveExpectedPrecision) {+ const auto kv_type = GetParam();+ RecordingFactory recorder;+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;++ ASSERT_NO_THROW(compiled = create_compiled_model(+ {{ov::hint::kv_cache_precision.name(), kv_type}}, recorder));+ ASSERT_NE(compiled, nullptr);++ const auto& generate = require_sub_model_containing(recorder, "_kv");++ EXPECT_TRUE(all_inputs_with_name_have_type(generate.model, "past_key", kv_type))+ << "past_key inputs must have type " << kv_type;+}++// present outputs of the generate model have the requested precision.+TEST_P(ConvertKVCacheHintPrecisionTest, GenerateModelPresentOutputsHaveExpectedPrecision) {+ const auto kv_type = GetParam();+ RecordingFactory recorder;+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;++ ASSERT_NO_THROW(compiled = create_compiled_model(+ {{ov::hint::kv_cache_precision.name(), kv_type}}, recorder));+ ASSERT_NE(compiled, nullptr);++ const auto& generate = require_sub_model_containing(recorder, "_kv");++ EXPECT_TRUE(all_outputs_with_name_have_type(generate.model, "present", kv_type))+ << "present outputs must have type " << kv_type;+}++// present outputs of the prefill model have the requested precision.+TEST_P(ConvertKVCacheHintPrecisionTest, PrefillModelPresentOutputsHaveExpectedPrecision) {+ const auto kv_type = GetParam();+ RecordingFactory recorder;+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;++ ASSERT_NO_THROW(compiled = create_compiled_model(+ {{ov::hint::kv_cache_precision.name(), kv_type}}, recorder));+ ASSERT_NE(compiled, nullptr);++ const auto& prefill = require_sub_model(recorder, "_prefill");++ EXPECT_TRUE(all_outputs_with_name_have_type(prefill.model, "present", kv_type))+ << "present outputs in the prefill model must have type " << kv_type;+}++// --- Non-parametric tests -------------------------------------------------------------------------++class ConvertKVCacheToPrecisionPassTest : public ov::test::npuw::LLMPassTestFixture {};++// Chunked-prefill: past_key inputs of the prefill model are converted to f16.+TEST_F(ConvertKVCacheToPrecisionPassTest, ChunkedPrefillModelPastKeyInputsAreF16) {+ RecordingFactory recorder;+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;++ ASSERT_NO_THROW(compiled = create_compiled_model({{"NPUW_LLM_PREFILL_HINT", "DYNAMIC"},+ {"NPUW_LLM_PREFILL_CHUNK_SIZE", "32"}},+ recorder));+ ASSERT_NE(compiled, nullptr);++ const auto& prefill = require_sub_model(recorder, "_prefill");++ EXPECT_TRUE(all_inputs_with_name_have_type(prefill.model, "past_key", ov::element::f16))+ << "past_key inputs in chunked prefill must be f16 after ConvertKVCacheToPrecision";+}++// Non-KV inputs (input_ids) must not be touched by ConvertKVCacheToPrecision.+TEST_F(ConvertKVCacheToPrecisionPassTest, NonKVInputsAreNotConverted) {+ RecordingFactory recorder;+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;++ ASSERT_NO_THROW(compiled = create_compiled_model({}, recorder));+ ASSERT_NE(compiled, nullptr);++ const auto& generate = require_sub_model_containing(recorder, "_kv");++ EXPECT_TRUE(no_inputs_with_name_have_type(generate.model, "input_ids", ov::element::f16))+ << "input_ids must NOT be f16 -- ConvertKVCacheToPrecision must not touch it";+}++} // namespacediff --git a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/llm_pass_test_fixture.hpp b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/llm_pass_test_fixture.hppnew file mode 100644index 00000000000..8d7e38e59c3--- /dev/null+++ b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/llm_pass_test_fixture.hpp@@ -0,0 +1,234 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++// Shared GTest fixture for NPUW LLM pass unit tests.+//+// All pass-specific test fixtures should derive from LLMPassTestFixture instead of+// ::testing::Test directly. The base provides:+// * SetUp() - creates m_plugin (NullPlugin)+// * base_props() - minimal LLM compile properties+// * merge_props() - merge two AnyMap instances+// * create_compiled_model() - builds a standard LLM sub-pipeline+// * create_whisper_compiled_model() - builds a Whisper-decoder sub-pipeline+// * count_ops<Op>() - count nodes of a given type in a model graph+// * port_has_name() - checks whether any name on a port contains a needle+// * count_inputs() - counts inputs whose name contains a needle+// * find_input() - returns the first input port whose name contains a needle+// * find_output() - returns the first output port whose name contains a needle+// * all_inputs_static() - returns true when every model input is fully static+// * input_shape() - returns the concrete shape of a named input+// * all_inputs_with_name_have_type() - true iff every input matching needle has expected_type; throws if needle unmatched+// * all_outputs_with_name_have_type() - true iff every output matching needle has expected_type; throws if needle unmatched+// * no_inputs_with_name_have_type() - true iff no input matching needle has excluded_type; throws if needle unmatched+// * require_sub_model() - asserts a CompileCall with the given exact suffix exists and returns it+// * require_sub_model_containing() - asserts a CompileCall whose name contains fragment exists and returns it+// Prefer this over require_sub_model() for KV-cache sub-models whose suffix+// encodes a concrete cache size (e.g. "_kv192") that changes with properties.+// Use the stable "_kv" fragment instead of a size-specific suffix.+//+// Pass-specific helpers (e.g. any_matmul_has_transpose_b) should remain in the individual test files.++#pragma once++#include <algorithm>+#include <memory>+#include <optional>+#include <stdexcept>+#include <string>+#include <string_view>++#include <gtest/gtest.h>++#include "llm_compiled_model.hpp"+#include "../llm_test_helpers.hpp"++namespace ov::test::npuw {++class LLMPassTestFixture : public ::testing::Test {+protected:+ void SetUp() override {+ m_plugin = std::make_shared<NullPlugin>();+ }++ // -- Properties -------------------------------------------------------------++ static ov::AnyMap base_props() {+ return {{"NPUW_LLM", "YES"}, {"NPUW_LLM_MAX_PROMPT_LEN", "128"}, {"NPUW_LLM_MIN_RESPONSE_LEN", "64"}};+ }++ static void merge_props(ov::AnyMap& dst, const ov::AnyMap& src) {+ for (const auto& [key, value] : src) {+ dst[key] = value;+ }+ }++ // -- Model construction --------------------------------------------------------++ std::unique_ptr<ov::npuw::LLMCompiledModel> create_compiled_model(const ov::AnyMap& extra_props,+ RecordingFactory& recorder) const {+ auto props = base_props();+ merge_props(props, extra_props);+ return std::make_unique<ov::npuw::LLMCompiledModel>(+ build_llm_test_model(), m_plugin, props, recorder.make_factory());+ }++ std::unique_ptr<ov::npuw::LLMCompiledModel> create_whisper_compiled_model(const ov::AnyMap& extra_props,+ RecordingFactory& recorder) const {+ auto props = base_props();+ merge_props(props, extra_props);+ return std::make_unique<ov::npuw::LLMCompiledModel>(+ build_whisper_decoder_test_model(), m_plugin, props, recorder.make_factory());+ }++ // -- Graph inspection ----------------------------------------------------------++ template <class Op>+ static std::size_t count_ops(const std::shared_ptr<ov::Model>& model) {+ const auto ops = model->get_ops();+ return std::count_if(ops.begin(), ops.end(), [](const auto& op) {+ return ov::is_type<Op>(op);+ });+ }++ template <class Port>+ static bool port_has_name(const Port& port, std::string_view needle) {+ const auto& names = port.get_names();+ return std::any_of(names.begin(), names.end(), [needle](const std::string& name) {+ return name.find(needle) != std::string::npos;+ });+ }++ static std::size_t count_inputs(const std::shared_ptr<ov::Model>& model, std::string_view needle) {+ const auto inputs = model->inputs();+ return std::count_if(inputs.begin(), inputs.end(), [needle](const auto& input) {+ return port_has_name(input, needle);+ });+ }++ static std::optional<ov::Output<const ov::Node>> find_input(const std::shared_ptr<ov::Model>& model,+ std::string_view needle) {+ const auto inputs = model->inputs();+ const auto it = std::find_if(inputs.begin(), inputs.end(), [needle](const auto& input) {+ return port_has_name(input, needle);+ });+ return it == inputs.end() ? std::nullopt : std::optional<ov::Output<const ov::Node>>{*it};+ }++ static std::optional<ov::Output<const ov::Node>> find_output(const std::shared_ptr<ov::Model>& model,+ std::string_view needle) {+ const auto outputs = model->outputs();+ const auto it = std::find_if(outputs.begin(), outputs.end(), [needle](const auto& output) {+ return port_has_name(output, needle);+ });+ return it == outputs.end() ? std::nullopt : std::optional<ov::Output<const ov::Node>>{*it};+ }++ // Returns true only when every input of the model has a fully static partial shape.+ static bool all_inputs_static(const std::shared_ptr<ov::Model>& model) {+ const auto inputs = model->inputs();+ return std::all_of(inputs.begin(), inputs.end(), [](const auto& input) {+ return input.get_partial_shape().is_static();+ });+ }++ // Finds the first input whose name contains `needle` and returns its concrete shape.+ // Returns std::nullopt when no such input exists or its shape is not yet static.+ static std::optional<ov::Shape> input_shape(const std::shared_ptr<ov::Model>& model, std::string_view needle) {+ const auto port = find_input(model, needle);+ if (!port.has_value()) {+ return std::nullopt;+ }+ const auto& ps = port->get_partial_shape();+ if (!ps.is_static()) {+ return std::nullopt;+ }+ return ps.to_shape();+ }++ // Returns true iff every input whose name contains `needle` has `expected_type`.+ // Throws std::runtime_error if no input matching `needle` is found.+ static bool all_inputs_with_name_have_type(const std::shared_ptr<ov::Model>& model,+ std::string_view needle,+ ov::element::Type expected_type) {+ bool found_any = false;+ for (const auto& input : model->inputs()) {+ if (port_has_name(input, needle)) {+ found_any = true;+ if (input.get_element_type() != expected_type)+ return false;+ }+ }+ if (!found_any)+ throw std::runtime_error(std::string("No input matching needle '") + std::string(needle) ++ "' found in model '" + model->get_friendly_name() + "'");+ return true;+ }++ // Returns true iff every output whose name contains `needle` has `expected_type`.+ // Throws std::runtime_error if no output matching `needle` is found.+ static bool all_outputs_with_name_have_type(const std::shared_ptr<ov::Model>& model,+ std::string_view needle,+ ov::element::Type expected_type) {+ bool found_any = false;+ for (const auto& output : model->outputs()) {+ if (port_has_name(output, needle)) {+ found_any = true;+ if (output.get_element_type() != expected_type)+ return false;+ }+ }+ if (!found_any)+ throw std::runtime_error(std::string("No output matching needle '") + std::string(needle) ++ "' found in model '" + model->get_friendly_name() + "'");+ return true;+ }++ // Returns true iff no input whose name contains `needle` has `excluded_type`.+ // Throws std::runtime_error if no input matching `needle` is found.+ static bool no_inputs_with_name_have_type(const std::shared_ptr<ov::Model>& model,+ std::string_view needle,+ ov::element::Type excluded_type) {+ bool found_any = false;+ for (const auto& input : model->inputs()) {+ if (port_has_name(input, needle)) {+ found_any = true;+ if (input.get_element_type() == excluded_type)+ return false;+ }+ }+ if (!found_any)+ throw std::runtime_error(std::string("No input matching needle '") + std::string(needle) ++ "' found in model '" + model->get_friendly_name() + "'");+ return true;+ }++ // -- Sub-model lookup ----------------------------------------------------------++ // Returns the CompileCall whose friendly name ends with `suffix`.+ // Triggers a fatal assertion if no such call exists.+ // Use for sub-models with a stable naming convention (_prefill, _lm_head, ...).+ static const CompileCall& require_sub_model(const RecordingFactory& recorder, std::string_view suffix) {+ const auto* call = recorder.find_suffix(suffix);+ OPENVINO_ASSERT(call != nullptr, "Missing compile call with suffix: ", std::string(suffix));+ return *call;+ }++ // Returns the CompileCall whose friendly name contains `fragment`.+ // Triggers a fatal assertion if no such call exists.+ // Prefer this over require_sub_model() for KV-cache sub-models: their suffix+ // encodes the concrete cache size (e.g. "_kv192") which changes with compile+ // properties. Match on the stable fragment "_kv" instead.+ static const CompileCall& require_sub_model_containing(const RecordingFactory& recorder,+ std::string_view fragment) {+ const auto* call = recorder.find_contains(fragment);+ OPENVINO_ASSERT(call != nullptr, "Missing compile call containing: ", std::string(fragment));+ return *call;+ }++ // -- Shared state ----------------------------------------------------------++ std::shared_ptr<ov::IPlugin> m_plugin;+};++} // namespace ov::test::npuwdiff --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.cppnew file mode 100644index 00000000000..69ccfadd4b3--- /dev/null+++ b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/optimize_value_tensors_test.cpp@@ -0,0 +1,119 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include <gtest/gtest.h>++#include "llm_pass_test_fixture.hpp"+#include "openvino/op/matmul.hpp"+#include "openvino/op/scaled_dot_product_attention.hpp"+#include "openvino/op/softmax.hpp"++namespace {++using ov::test::npuw::RecordingFactory;++class OptimizeValueTensorsPassTest : public ov::test::npuw::LLMPassTestFixture {+protected:+ static bool any_matmul_has_transpose_b(const std::shared_ptr<ov::Model>& model) {+ for (const auto& op : model->get_ops()) {+ if (auto matmul = ov::as_type_ptr<ov::op::v0::MatMul>(op)) {+ if (matmul->get_transpose_b()) {+ return true;+ }+ }+ }+ return false;+ }+};++// ScaledDotProductAttentionDecomposition replaces every SDPA node in both the+// prefill and generate sub-models when OptimizeValueTensors is enabled.+// Parametrize over sub-model suffix to avoid duplicating the identical test body.+class OptimizeValueTensorsNoSDPATest : public ov::test::npuw::LLMPassTestFixture,+ public ::testing::WithParamInterface<std::string> {};++INSTANTIATE_TEST_SUITE_P(SubModels,+ OptimizeValueTensorsNoSDPATest,+ ::testing::Values(std::string{"_prefill"}, std::string{"_kv"}),+ [](const ::testing::TestParamInfo<std::string>& info) {+ auto name = info.param;+ if (!name.empty() && name[0] == '_')+ name = name.substr(1);+ return name;+ });++TEST_P(OptimizeValueTensorsNoSDPATest, NoSDPAOpsAfterOptimization) {+ const auto& fragment = GetParam();+ RecordingFactory recorder;+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;++ ASSERT_NO_THROW(compiled = create_compiled_model({{"NPUW_LLM_OPTIMIZE_V_TENSORS", "YES"}}, recorder));+ ASSERT_NE(compiled, nullptr);++ const auto& sub = require_sub_model_containing(recorder, fragment);++ EXPECT_EQ(count_ops<ov::op::v13::ScaledDotProductAttention>(sub.model), 0u);+}++// After SDPA decomposition each layer produces two MatMul ops (QK and SV multiplications).+// With 2 layers the generate model must contain at least 4 MatMul ops.+TEST_F(OptimizeValueTensorsPassTest, GenerateModelHasMatMulFromDecomposedSDPA) {+ RecordingFactory recorder;+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;++ ASSERT_NO_THROW(compiled = create_compiled_model({{"NPUW_LLM_OPTIMIZE_V_TENSORS", "YES"}}, recorder));+ ASSERT_NE(compiled, nullptr);++ const auto& generate = require_sub_model_containing(recorder, "_kv");++ // 2 layers x 2 MatMuls per SDPA decomposition = at least 4+ EXPECT_GE(count_ops<ov::op::v0::MatMul>(generate.model), 4u);+}++// SDPA decomposition inserts a Softmax node for each layer.+// With 2 layers the generate model must contain at least 2 Softmax ops.+TEST_F(OptimizeValueTensorsPassTest, GenerateModelHasSoftmaxFromDecomposedSDPA) {+ RecordingFactory recorder;+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;++ ASSERT_NO_THROW(compiled = create_compiled_model({{"NPUW_LLM_OPTIMIZE_V_TENSORS", "YES"}}, recorder));+ ASSERT_NE(compiled, nullptr);++ const auto& generate = require_sub_model_containing(recorder, "_kv");++ // At least one Softmax per attention layer; SDPA decomposition creates v8::Softmax+ EXPECT_GE(count_ops<ov::op::v8::Softmax>(generate.model), 2u);+}++// When OptimizeValueTensors is disabled the ScaledDotProductAttentionDecomposition+// sub-pass is skipped. DecomposeGQA only targets GroupQueryAttention, so the+// standard test model's SDPA nodes must remain intact.+TEST_F(OptimizeValueTensorsPassTest, SDPAOpsRemainsWhenOptimizationDisabled) {+ RecordingFactory recorder;+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;++ ASSERT_NO_THROW(compiled = create_compiled_model({{"NPUW_LLM_OPTIMIZE_V_TENSORS", "NO"}}, recorder));+ ASSERT_NE(compiled, nullptr);++ const auto& generate = require_sub_model_containing(recorder, "_kv");++ EXPECT_GT(count_ops<ov::op::v13::ScaledDotProductAttention>(generate.model), 0u);+}++// TransposeValueTensors sets transpose_b=true on the value-multiplication MatMul+// when the optimisation fires. At least one MatMul in the generate model must+// have transpose_b set after the pass runs.+TEST_F(OptimizeValueTensorsPassTest, AtLeastOneMatMulHasTransposeBSet) {+ RecordingFactory recorder;+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;++ ASSERT_NO_THROW(compiled = create_compiled_model({{"NPUW_LLM_OPTIMIZE_V_TENSORS", "YES"}}, recorder));+ ASSERT_NE(compiled, nullptr);++ const auto& generate = require_sub_model_containing(recorder, "_kv");++ EXPECT_TRUE(any_matmul_has_transpose_b(generate.model));+}++} // namespacediff --git a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/reshape_sliced_head_to_static_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/reshape_sliced_head_to_static_test.cppnew file mode 100644index 00000000000..1b8288f515a--- /dev/null+++ b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/reshape_sliced_head_to_static_test.cpp@@ -0,0 +1,109 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include <gtest/gtest.h>++#include <string>++#include "llm_pass_test_fixture.hpp"++namespace {++using ov::test::npuw::RecordingFactory;++class ReshapeSlicedHeadToStaticPassTest : public ov::test::npuw::LLMPassTestFixture {};++// Test 1: With SHARED_HEAD=YES and MAX_GENERATION_TOKEN_LEN=8, the lm_head input+// must be fully static and shaped {1, 8, 64} (batch=1, gen_len=8, hidden_size=64).+TEST_F(ReshapeSlicedHeadToStaticPassTest, LMHeadInputIsFullyStatic) {+ RecordingFactory recorder;+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;++ ASSERT_NO_THROW(compiled = create_compiled_model({{"NPUW_LLM_SHARED_HEAD", "YES"},+ {"NPUW_LLM_MAX_GENERATION_TOKEN_LEN", "8"}},+ recorder));+ ASSERT_NE(compiled, nullptr);++ const auto& lm_head = require_sub_model(recorder, "_lm_head");++ const auto head_input = lm_head.model->input(0);+ EXPECT_TRUE(head_input.get_partial_shape().is_static())+ << "lm_head input shape is not fully static: " << head_input.get_partial_shape();+ EXPECT_EQ(head_input.get_shape(), (ov::Shape{1, 8, 64}));+}++// Test 2 (parametrized): The static sequence-length dimension in the lm_head input must+// equal MAX_GENERATION_TOKEN_LEN.+//+// Note: LLMCompiledModel aligns MAX_GENERATION_TOKEN_LEN to the nearest multiple of 8+// (except for the special value 1). Only multiples of 8 are used as parameters here so+// that the expected shape matches the value actually stored in the compiled model.+class ReshapeSlicedHeadTokenLenTest : public ov::test::npuw::LLMPassTestFixture,+ public ::testing::WithParamInterface<size_t> {};++TEST_P(ReshapeSlicedHeadTokenLenTest, LMHeadInputShapeReflectsMaxGenerationTokenLen) {+ const size_t gen_len = GetParam();++ RecordingFactory recorder;+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;++ ASSERT_NO_THROW(compiled = create_compiled_model(+ {{"NPUW_LLM_SHARED_HEAD", "YES"},+ {"NPUW_LLM_MAX_GENERATION_TOKEN_LEN", std::to_string(gen_len)}},+ recorder));+ ASSERT_NE(compiled, nullptr);++ const auto& lm_head = require_sub_model(recorder, "_lm_head");++ const auto head_input = lm_head.model->input(0);+ ASSERT_TRUE(head_input.get_partial_shape().is_static())+ << "lm_head input shape is not fully static: " << head_input.get_partial_shape();+ EXPECT_EQ(head_input.get_shape(), (ov::Shape{1, gen_len, 64}));+}++INSTANTIATE_TEST_SUITE_P(ShapeSweep,+ ReshapeSlicedHeadTokenLenTest,+ ::testing::Values(8u, 16u, 24u));++// Test 3: When SHARED_HEAD=NO the pipeline must not produce an lm_head sub-model at all.+TEST_F(ReshapeSlicedHeadToStaticPassTest, LMHeadNotCreatedWithoutSharedHead) {+ RecordingFactory recorder;+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;++ ASSERT_NO_THROW(compiled = create_compiled_model({{"NPUW_LLM_SHARED_HEAD", "NO"},+ {"NPUW_LLM_MAX_GENERATION_TOKEN_LEN", "8"}},+ recorder));+ ASSERT_NE(compiled, nullptr);++ const auto* lm_head = recorder.find_suffix("_lm_head");+ EXPECT_EQ(lm_head, nullptr)+ << "lm_head sub-model should not be created when SHARED_HEAD=NO, but it was found";+}++// Test 4: With MAX_GENERATION_TOKEN_LEN=1 the third dimension (hidden_size=64 from the+// model configuration) must remain unchanged in the static lm_head input shape.+TEST_F(ReshapeSlicedHeadToStaticPassTest, LMHeadInputHiddenSizeMatchesModelConfig) {+ RecordingFactory recorder;+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;++ ASSERT_NO_THROW(compiled = create_compiled_model({{"NPUW_LLM_SHARED_HEAD", "YES"},+ {"NPUW_LLM_MAX_GENERATION_TOKEN_LEN", "1"}},+ recorder));+ ASSERT_NE(compiled, nullptr);++ const auto& lm_head = require_sub_model(recorder, "_lm_head");++ const auto head_input = lm_head.model->input(0);+ ASSERT_TRUE(head_input.get_partial_shape().is_static())+ << "lm_head input shape is not fully static: " << head_input.get_partial_shape();++ const auto shape = head_input.get_shape();+ // batch must be 1, seq_len must equal MAX_GENERATION_TOKEN_LEN=1, hidden_size must be 64.+ ASSERT_EQ(shape.size(), 3u);+ EXPECT_EQ(shape[0], 1u) << "batch dimension mismatch";+ EXPECT_EQ(shape[1], 1u) << "sequence-length dimension should equal MAX_GENERATION_TOKEN_LEN=1";+ EXPECT_EQ(shape[2], 64u) << "hidden_size dimension must match the model configuration (hidden_size=64)";+}++} // namespacediff --git a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/reshape_to_static_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/reshape_to_static_test.cppnew file mode 100644index 00000000000..5b0ddf7f95f--- /dev/null+++ b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/reshape_to_static_test.cpp@@ -0,0 +1,147 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include <gtest/gtest.h>++#include "llm_pass_test_fixture.hpp"++namespace {++using ov::test::npuw::RecordingFactory;++class ReshapeToStaticPassTest : public ov::test::npuw::LLMPassTestFixture {};++// --- Test 1 -------------------------------------------------------------------------+// Every input of the prefill sub-model must be fully static after ReshapeToStatic.+// We additionally verify the concrete shapes of the two most representative inputs.+TEST_F(ReshapeToStaticPassTest, AllPrefillInputsAreStatic) {+ RecordingFactory recorder;+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;++ // Default props: MAX_PROMPT_LEN=128, MIN_RESPONSE_LEN=64+ ASSERT_NO_THROW(compiled = create_compiled_model({}, recorder));+ ASSERT_NE(compiled, nullptr);++ const auto& prefill = require_sub_model(recorder, "_prefill");++ EXPECT_TRUE(all_inputs_static(prefill.model))+ << "At least one prefill input still has a dynamic dimension after ReshapeToStatic";++ // Spot-check concrete shapes+ const auto ids_shape = input_shape(prefill.model, "input_ids");+ ASSERT_TRUE(ids_shape.has_value()) << "input_ids not found in prefill model";+ EXPECT_EQ(*ids_shape, (ov::Shape{1, 128}));++ const auto mask_shape = input_shape(prefill.model, "attention_mask");+ ASSERT_TRUE(mask_shape.has_value()) << "attention_mask not found in prefill model";+ EXPECT_EQ(*mask_shape, (ov::Shape{1, 128}));+}++// --- Test 2 -------------------------------------------------------------------------+// Every input of the generate sub-model must be fully static after ReshapeToStatic.+// kvcache_size = 128 + 64 = 192, so the model is named _kv192.+// Model config: num_kv_heads=4, head_dim=16, max_generation_token_len=1 (default).+TEST_F(ReshapeToStaticPassTest, AllGenerateInputsAreStatic) {+ RecordingFactory recorder;+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;++ // Default props: MAX_PROMPT_LEN=128, MIN_RESPONSE_LEN=64, single-token generate+ ASSERT_NO_THROW(compiled = create_compiled_model({}, recorder));+ ASSERT_NE(compiled, nullptr);++ const auto& generate = require_sub_model_containing(recorder, "_kv");++ EXPECT_TRUE(all_inputs_static(generate.model))+ << "At least one generate input still has a dynamic dimension after ReshapeToStatic";++ // input_ids: one token at a time (default max_generation_token_len=1)+ const auto ids_shape = input_shape(generate.model, "input_ids");+ ASSERT_TRUE(ids_shape.has_value()) << "input_ids not found in generate model";+ EXPECT_EQ(*ids_shape, (ov::Shape{1, 1}));++ // attention_mask covers the full kvcache window+ const auto mask_shape = input_shape(generate.model, "attention_mask");+ ASSERT_TRUE(mask_shape.has_value()) << "attention_mask not found in generate model";+ EXPECT_EQ(*mask_shape, (ov::Shape{1, 192}));++ // past_key_values: [batch=1, num_kv_heads=4, past_seq=kvcache-input_size=191, head_dim=16]+ const auto kv_shape = input_shape(generate.model, "past_key_values");+ ASSERT_TRUE(kv_shape.has_value()) << "past_key_values not found in generate model";+ ASSERT_EQ(kv_shape->size(), 4u);+ EXPECT_EQ((*kv_shape)[0], 1u); // batch+ EXPECT_EQ((*kv_shape)[1], 4u); // num_kv_heads+ EXPECT_EQ((*kv_shape)[2], 191u); // kvcache_size(192) - input_size(1)+ EXPECT_EQ((*kv_shape)[3], 16u); // head_dim+}++// --- Test 3 -------------------------------------------------------------------------+// The past_key_values sequence dimension in the generate model must equal+// kvcache_size - input_size. With MAX_PROMPT_LEN=256 and MIN_RESPONSE_LEN=128:+// kvcache_size = 256 + 128 = 384+// past seq dim = 384 - 1 = 383+TEST_F(ReshapeToStaticPassTest, GenerateModelKVCacheShapeReflectsKVCacheSize) {+ RecordingFactory recorder;+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;++ ASSERT_NO_THROW(compiled = create_compiled_model({{"NPUW_LLM_MAX_PROMPT_LEN", "256"},+ {"NPUW_LLM_MIN_RESPONSE_LEN", "128"}},+ recorder));+ ASSERT_NE(compiled, nullptr);++ // kvcache_size = 256 + 128 = 384+ const auto& generate = require_sub_model_containing(recorder, "_kv");++ EXPECT_TRUE(all_inputs_static(generate.model))+ << "At least one generate input still has a dynamic dimension after ReshapeToStatic";++ const auto kv_shape = input_shape(generate.model, "past_key_values");+ ASSERT_TRUE(kv_shape.has_value()) << "past_key_values not found in generate model";+ ASSERT_EQ(kv_shape->size(), 4u);+ // seq dim = kvcache_size(384) - input_size(1) = 383+ EXPECT_EQ((*kv_shape)[2], 383u);++ // Sanity-check the other dims while we're here+ EXPECT_EQ((*kv_shape)[0], 1u); // batch+ EXPECT_EQ((*kv_shape)[1], 4u); // num_kv_heads+ EXPECT_EQ((*kv_shape)[3], 16u); // head_dim+}++// --- Test 4 -------------------------------------------------------------------------+// When NPUW_LLM_MAX_GENERATION_TOKEN_LEN=8:+// * input_ids in the generate model has shape {1, 8}+// * past_key_values seq dim = kvcache_size(192) - input_size(8) = 184+TEST_F(ReshapeToStaticPassTest, MaxGenerationTokenLenDrivesGenerateInputShape) {+ RecordingFactory recorder;+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;++ ASSERT_NO_THROW(compiled = create_compiled_model({{"NPUW_LLM_MAX_GENERATION_TOKEN_LEN", "8"}}, recorder));+ ASSERT_NE(compiled, nullptr);++ // MAX_PROMPT_LEN=128, MIN_RESPONSE_LEN=64 from base_props -> kvcache_size=192+ const auto& generate = require_sub_model_containing(recorder, "_kv");++ EXPECT_TRUE(all_inputs_static(generate.model))+ << "At least one generate input still has a dynamic dimension after ReshapeToStatic";++ // input_ids must accommodate MAX_GENERATION_TOKEN_LEN tokens+ const auto ids_shape = input_shape(generate.model, "input_ids");+ ASSERT_TRUE(ids_shape.has_value()) << "input_ids not found in generate model";+ EXPECT_EQ(*ids_shape, (ov::Shape{1, 8}));++ // attention_mask still spans the full kvcache window+ const auto mask_shape = input_shape(generate.model, "attention_mask");+ ASSERT_TRUE(mask_shape.has_value()) << "attention_mask not found in generate model";+ EXPECT_EQ(*mask_shape, (ov::Shape{1, 192}));++ // past_key_values seq dim = 192 - 8 = 184+ const auto kv_shape = input_shape(generate.model, "past_key_values");+ ASSERT_TRUE(kv_shape.has_value()) << "past_key_values not found in generate model";+ ASSERT_EQ(kv_shape->size(), 4u);+ EXPECT_EQ((*kv_shape)[0], 1u); // batch+ EXPECT_EQ((*kv_shape)[1], 4u); // num_kv_heads+ EXPECT_EQ((*kv_shape)[2], 184u); // kvcache_size(192) - input_size(8)+ EXPECT_EQ((*kv_shape)[3], 16u); // head_dim+}++} // namespacediff --git a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/slice_out_embeds_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/slice_out_embeds_test.cppnew file mode 100644index 00000000000..1341589c207--- /dev/null+++ b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/slice_out_embeds_test.cpp@@ -0,0 +1,121 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include <gtest/gtest.h>++#include "llm_pass_test_fixture.hpp"+#include "openvino/op/constant.hpp"+#include "openvino/op/result.hpp"+#include "openvino/op/slice.hpp"++namespace {++using ov::test::npuw::RecordingFactory;++class SliceOutEmbedsPassTest : public ov::test::npuw::LLMPassTestFixture {+protected:+ // Returns true iff the direct producer of the output_embeds Result is a v8::Slice.+ // This is precisely what SliceOutEmbeds inserts; other Slice ops (e.g. from RoPE+ // half-rotation) are NOT on this path and must not be counted.+ static bool output_embeds_has_slice_producer(const std::shared_ptr<ov::Model>& model) {+ for (const auto& output : model->outputs()) {+ for (const auto& name : output.get_names()) {+ if (name.find(ov::npuw::LLMCompiledModel::output_embeds) != std::string::npos) {+ // output is an Output<const Node> over the Result node+ auto result_node = output.get_node_shared_ptr();+ if (!result_node || result_node->inputs().empty())+ return false;+ auto producer = result_node->input(0).get_source_output().get_node_shared_ptr();+ return ov::is_type<ov::op::v8::Slice>(producer);+ }+ }+ }+ return false;+ }+};++// Test 1: When SHARED_HEAD=YES and MAX_GENERATION_TOKEN_LEN < MAX_PROMPT_LEN,+// SliceOutEmbeds should insert a v8::Slice on the output_embeds path and the+// resulting output shape should be {1, MAX_GENERATION_TOKEN_LEN, hidden_size}.+TEST_F(SliceOutEmbedsPassTest, SliceIsInsertedWhenGenerationTokenLenIsLessThanPromptLen) {+ RecordingFactory recorder;+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;++ ASSERT_NO_THROW(compiled = create_compiled_model({{"NPUW_LLM_SHARED_HEAD", "YES"},+ {"NPUW_LLM_MAX_GENERATION_TOKEN_LEN", "8"}},+ recorder));+ ASSERT_NE(compiled, nullptr);++ const auto& prefill = require_sub_model(recorder, "_prefill");+ const auto& lm_head = require_sub_model(recorder, "_lm_head");+ (void)lm_head; // asserted to exist; contents checked by reshape_sliced_head tests++ // output_embeds must exist on the prefill model+ const auto embeds = find_output(prefill.model, ov::npuw::LLMCompiledModel::output_embeds);+ ASSERT_TRUE(embeds.has_value()) << "output_embeds output not found on prefill model";++ // Shape must be static and trimmed to generation token length+ ASSERT_TRUE(embeds->get_partial_shape().is_static())+ << "output_embeds shape is not static: " << embeds->get_partial_shape();+ EXPECT_EQ(embeds->get_shape(), (ov::Shape{1, 8, 64}));++ // The direct producer of the output_embeds Result must be a v8::Slice (inserted by SliceOutEmbeds).+ // Other v8::Slice ops (e.g. from HalfRotationRoPE halving) are intentionally not counted.+ EXPECT_TRUE(output_embeds_has_slice_producer(prefill.model))+ << "Expected a v8::Slice directly feeding output_embeds, but none was found";+}++// Test 2: When SHARED_HEAD=YES but MAX_GENERATION_TOKEN_LEN == MAX_PROMPT_LEN,+// SliceOutEmbeds is a no-op: no Slice is inserted and the shape remains full.+TEST_F(SliceOutEmbedsPassTest, SliceIsNotInsertedWhenGenerationTokenLenEqualsPromptLen) {+ RecordingFactory recorder;+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;++ // MAX_GENERATION_TOKEN_LEN=128 equals MAX_PROMPT_LEN=128, so no slicing is needed.+ ASSERT_NO_THROW(compiled = create_compiled_model({{"NPUW_LLM_SHARED_HEAD", "YES"},+ {"NPUW_LLM_MAX_GENERATION_TOKEN_LEN", "128"}},+ recorder));+ ASSERT_NE(compiled, nullptr);++ const auto& prefill = require_sub_model(recorder, "_prefill");++ // output_embeds must still be exported+ const auto embeds = find_output(prefill.model, ov::npuw::LLMCompiledModel::output_embeds);+ ASSERT_TRUE(embeds.has_value()) << "output_embeds output not found on prefill model";++ // Shape should be the full prompt-length shape, unmodified+ ASSERT_TRUE(embeds->get_partial_shape().is_static())+ << "output_embeds shape is not static: " << embeds->get_partial_shape();+ EXPECT_EQ(embeds->get_shape(), (ov::Shape{1, 128, 64}));++ // The direct producer of the output_embeds Result must NOT be a v8::Slice.+ // (Other v8::Slices from RoPE etc. are irrelevant and deliberately ignored.)+ EXPECT_FALSE(output_embeds_has_slice_producer(prefill.model))+ << "Expected no v8::Slice directly feeding output_embeds when generation len == prompt len";+}++// Test 3: When SHARED_HEAD=NO, SliceOutEmbeds does not fire. The lm_head sub-model+// must not be created and the prefill model must not expose an output_embeds output.+TEST_F(SliceOutEmbedsPassTest, SliceNotAppliedWhenNoSharedHead) {+ RecordingFactory recorder;+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;++ ASSERT_NO_THROW(compiled = create_compiled_model({{"NPUW_LLM_SHARED_HEAD", "NO"},+ {"NPUW_LLM_MAX_GENERATION_TOKEN_LEN", "8"}},+ recorder));+ ASSERT_NE(compiled, nullptr);++ // lm_head sub-model must not exist when SHARED_HEAD=NO+ const auto* lm_head = recorder.find_suffix("_lm_head");+ EXPECT_EQ(lm_head, nullptr) << "lm_head sub-model should not exist when SHARED_HEAD=NO";++ // prefill model must not export output_embeds+ const auto& prefill = require_sub_model(recorder, "_prefill");++ const auto embeds = find_output(prefill.model, ov::npuw::LLMCompiledModel::output_embeds);+ EXPECT_FALSE(embeds.has_value())+ << "output_embeds output should not exist in the prefill model when SHARED_HEAD=NO";+}++} // namespace