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
[NPUW] added unit-tests for transpose vtensors optimisation (#28958)
Eugene Smirnov46a2c93fd6c2
diff --git a/.github/workflows/job_cxx_unit_tests.yml b/.github/workflows/job_cxx_unit_tests.ymlindex d79691bcac6..63ee8b1ef09 100644--- a/.github/workflows/job_cxx_unit_tests.yml+++ b/.github/workflows/job_cxx_unit_tests.yml@@ -201,6 +201,12 @@ jobs:${{ env.SETUPVARS_COMMAND }}${{ env.INSTALL_TEST_DIR }}/ov_cpu_unit_tests_vectorized --gtest_print_time=1 --gtest_output=xml:${{ env.INSTALL_TEST_DIR }}/TEST-CPUUnitTestsVectorized.xml+ - name: NPU plugin unit tests+ if: ${{fromJSON(inputs.affected-components).NPU.test && runner.os == 'Windows'}}+ run: |+ ${{ env.SETUPVARS_COMMAND }}+ ${{ env.INSTALL_TEST_DIR }}/ov_npu_unit_tests --gtest_print_time=1 --gtest_output=xml:${{ env.INSTALL_TEST_DIR }}/TEST-NPUUnitTests.xml+- name: ov_subgraphs_dumper_tests testsrun: |${{ env.SETUPVARS_COMMAND }}diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cppindex a7e3881b76c..e94fac8b6cb 100644--- a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp@@ -10,6 +10,7 @@#include "openvino/opsets/opset13.hpp"#include "openvino/pass/graph_rewrite.hpp"#include "openvino/pass/matcher_pass.hpp"+#include "openvino/pass/pattern/op/optional.hpp"#include "openvino/pass/pattern/op/wrap_type.hpp"#include "openvino/pass/stateful_to_stateless.hpp"#include "openvino/pass/validate.hpp"@@ -17,15 +18,15 @@#include "openvino/runtime/properties.hpp"#include "serialization.hpp"#include "transformations/convert_precision.hpp"+#include "util.hpp"namespace opp = ov::pass::pattern;class TransposeValueTensors : public ov::pass::MatcherPass {public:struct Context {- std::vector<std::shared_ptr<ov::opset13::Parameter>> new_params;- std::vector<std::shared_ptr<ov::opset13::Parameter>> old_params;using Ref = std::reference_wrapper<Context>;+ bool bTransposed = false;};protected:@@ -45,27 +46,15 @@ protected:// NB: Transpose Parameter that correspond to V-tensor it will// speed-up its multiplication with attention scoresstd::swap(param_shape[2], param_shape[3]);- auto new_param = std::make_shared<ov::opset13::Parameter>(matched_param->get_element_type(), param_shape);- new_param->set_friendly_name(matched_param->get_friendly_name());- new_param->outputs().begin()->get_tensor().set_names(- matched_param->outputs().begin()->get_tensor().get_names());- ov::replace_node(matched_param, new_param);- // NB: Save in order to add/remove to the model later on- ctx.get().new_params.push_back(new_param);- ctx.get().old_params.push_back(matched_param);- auto order_cst = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{4}, {0, 2, 3, 1});- auto new_transpose =- std::make_shared<ov::opset13::Transpose>(matched_transpose->input_value(0), order_cst->output(0));- new_transpose->set_friendly_name(matched_transpose->get_friendly_name());- ov::replace_node(matched_transpose, new_transpose);+ matched_param->set_partial_shape(param_shape);- auto new_concat =- std::make_shared<ov::opset13::Concat>(ov::OutputVector{new_param->output(0), new_transpose->output(0)}, 3u);- new_concat->set_friendly_name(matched_concat->get_friendly_name());- ov::replace_node(matched_concat, new_concat);+ 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);matched_matmul->set_transpose_b(true);+ ctx.get().bTransposed = true;}};@@ -81,7 +70,8 @@ private:void register_matcher_llama2(Context::Ref ctx) {auto param = opp::wrap_type<ov::op::v0::Parameter>();auto transpose = opp::wrap_type<ov::op::v1::Transpose>({opp::any_input(), opp::any_input()});- auto concat = opp::wrap_type<ov::op::v0::Concat>({param, transpose});+ auto convert = opp::optional<ov::op::v0::Convert>({param->output(0)});+ auto concat = opp::wrap_type<ov::op::v0::Concat>({convert, transpose});auto softmax = opp::wrap_type<ov::op::v8::Softmax>({opp::any_input()});auto matmul = opp::wrap_type<ov::op::v0::MatMul>({softmax, concat});@@ -117,7 +107,8 @@ private:void register_matcher_llama3(Context::Ref ctx) {auto param = opp::wrap_type<ov::op::v0::Parameter>();auto transpose = opp::wrap_type<ov::op::v1::Transpose>({opp::any_input(), opp::any_input()});- auto concat = opp::wrap_type<ov::op::v0::Concat>({param, transpose});+ auto convert = opp::optional<ov::op::v0::Convert>({param->output(0)});+ auto concat = opp::wrap_type<ov::op::v0::Concat>({convert, transpose});// only difference is that broadcast wrapped into unsquese/reshape, while transposed tensor didn't changeconst auto unsqueeze_axes = opp::wrap_type<ov::op::v0::Constant>();@@ -125,6 +116,7 @@ private:auto broadcast = opp::wrap_type<ov::op::v1::Broadcast, ov::op::v3::Broadcast>({unsqueeze, opp::any_input()});auto reshape = opp::wrap_type<ov::op::v1::Reshape>({broadcast, opp::any_input()});+ // v8 softmax? what? can be other softmaxesauto softmax = opp::wrap_type<ov::op::v8::Softmax>({opp::any_input()});auto matmul = opp::wrap_type<ov::op::v0::MatMul>({softmax, reshape});@@ -144,7 +136,6 @@ private:auto matched_concat = std::static_pointer_cast<ov::op::v0::Concat>(matched_node_concat);auto matched_transpose = std::static_pointer_cast<ov::op::v1::Transpose>(matched_node_transpose);auto matched_matmul = std::static_pointer_cast<ov::op::v0::MatMul>(matched_node_matmul);-auto matched_unsqueeze = std::static_pointer_cast<ov::op::v0::Unsqueeze>(matched_node_unsqueeze);auto matched_broadcast = std::static_pointer_cast<ov::op::v3::Broadcast>(matched_node_broadcast);auto matched_reshape = std::static_pointer_cast<ov::op::v1::Reshape>(matched_node_reshape);@@ -338,6 +329,10 @@ std::shared_ptr<ov::Model> cvt_value_tensors_layout(std::shared_ptr<ov::Model> mreturn ppp.build();}+} // namespace++// testability - should we have interface for that or move matchers to another file?+bool optimize_value_tensors(std::shared_ptr<ov::Model> model);bool optimize_value_tensors(std::shared_ptr<ov::Model> model) {ov::pass::GraphRewrite rewr;rewr.add_matcher<ScaledDotProductAttentionDecomposition>();@@ -346,16 +341,13 @@ bool optimize_value_tensors(std::shared_ptr<ov::Model> model) {rewr.add_matcher<TransposeValueTensors_llama3>(std::ref(ctx));rewr.run_on_model(model);- model->add_parameters(ctx.new_params);- for (auto old_param : ctx.old_params) {- model->remove_parameter(old_param);- }ov::pass::Validate().run_on_model(model);- // NB: if new_params is not empty - pass has been applied- return !ctx.new_params.empty();+ // NB: matmul parameters gets transposed, if pass applied+ return ctx.bTransposed;}+namespace {struct KVAxesPosition {uint32_t batch;uint32_t seq_len;diff --git a/src/plugins/intel_npu/tests/unit/CMakeLists.txt b/src/plugins/intel_npu/tests/unit/CMakeLists.txtindex 1097e183369..4bfc22c9be1 100644--- a/src/plugins/intel_npu/tests/unit/CMakeLists.txt+++ b/src/plugins/intel_npu/tests/unit/CMakeLists.txt@@ -31,6 +31,7 @@ ov_add_test_target(${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/al/includeOBJECT_FILES${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/src/metadata.cpp+ ${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cppLINK_LIBRARIES${MANDATORY_UNIT_TESTS_LIBS}LABELSdiff --git a/src/plugins/intel_npu/tests/unit/npuw/transpose_vt.cpp b/src/plugins/intel_npu/tests/unit/npuw/transpose_vt.cppnew file mode 100644index 00000000000..6625f24d2dd--- /dev/null+++ b/src/plugins/intel_npu/tests/unit/npuw/transpose_vt.cpp@@ -0,0 +1,182 @@+// Copyright (C) 2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//+#include <gtest/gtest.h>+#include <common_test_utils/test_common.hpp>+#include "openvino/op/ops.hpp"++namespace npuw_utest{+ using NodePtr = std::shared_ptr<ov::Node>;+}++//TODO: move to header maybe+bool optimize_value_tensors(std::shared_ptr<ov::Model> model);++enum class NetworkKind {+ llama2,+ llama3+};+++typedef std::tuple <+ ov::Shape,+ bool, // withConvert+ bool, // withTranspose - without transpose node - matcher shouldnt detect subgraph, easy way to negative case+ NetworkKind+> OptimizeVTTestParams;++// based on ConcatWithDifferentChildrenTransformation+class TransposeVTTest : public testing::WithParamInterface<OptimizeVTTestParams>,+ public ov::test::TestsCommon {+public:+ void Validate() const {+ auto isValidSubgraph = std::get<2>(GetParam());+ ASSERT_EQ(isValidSubgraph, optimize_value_tensors(model));+ }++ static std::string getTestCaseName(testing::TestParamInfo<OptimizeVTTestParams> obj) {+ auto inputShape = std::get<0>(obj.param);+ auto withConvert = std::get<1>(obj.param);+ auto withTranspose = std::get<2>(obj.param);+ auto kind = std::get<3>(obj.param);++ std::ostringstream result;+ result << "npuw_llm_pipeline_" << inputShape << "_"+ << (kind == NetworkKind::llama3 ? "LLAMA3" : "LLAMA2")+ << (withConvert ? "_with_convert" : "")+ << (!withTranspose ? "_NEGATIVE" : "");+ return result.str();+ }++protected:++ void SetUp() override {+ const auto& inputShape = std::get<0>(GetParam());+ const auto& withConvert = std::get<1>(GetParam());+ const auto& withTranspose = std::get<2>(GetParam());+ const auto& kind = std::get<3>(GetParam());++ model = CreateModel(inputShape, withConvert, withTranspose, kind);+ }++ std::shared_ptr<ov::Model> CreateModel(const ov::Shape& testShape,+ bool withConvert,+ bool withTranspose,+ NetworkKind kind) {++ auto create_shape_constant = [](const std::vector<int64_t> & const_data, const std::string& name) {+ auto pattern = std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{const_data.size()}, const_data);+ pattern->set_friendly_name("unsqueese_pattern");+ return pattern;+ };++ // in case of non broadcast number of input channels significantly smaller+ auto numChannels = (kind == NetworkKind::llama3) ? 8 : 32;+ auto input_shape = testShape;+ auto input_2 = static_cast<int>(testShape[2]);+ auto input_3 = static_cast<int>(testShape[3]);++ input_shape.at(1) = numChannels;++ // ov::Model with only a transpose node+ auto param = std::make_shared<ov::op::v0::Parameter>(withConvert ? ov::element::f16 : ov::element::f32, input_shape);+ param->set_friendly_name("past_key_value");++ std::shared_ptr<ov::Node> convert = withConvert ?+ std::static_pointer_cast<ov::Node>(std::make_shared<ov::op::v0::Convert>(param, ov::element::f32)) :+ std::static_pointer_cast<ov::Node>(param);+ if (withConvert) {+ convert->set_friendly_name("convert");+ }++ // todo parametrise optional reshape+ auto param2 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1, 1, input_shape[3] * numChannels});+ param2->set_friendly_name("new_token");++ auto reshape_pattern = create_shape_constant({0, 0, numChannels, input_3}, "reshape_pattern");+ auto transpose_pattern = create_shape_constant({1, numChannels, 1, input_3}, "transposed_pattern");++ auto reshape = std::make_shared<ov::op::v1::Reshape>(param2, withTranspose ? reshape_pattern : transpose_pattern, true);+ reshape->set_friendly_name("reshape");++ std::shared_ptr<ov::Node> transpose_or_reshape;++ if (withTranspose) {+ auto constOrder = create_shape_constant({0, 2, 1, 3}, "const_order");+ auto transpose = std::make_shared<ov::op::v1::Transpose>(reshape, constOrder);+ transpose->set_friendly_name("transpose");+ transpose_or_reshape = transpose;+ } else {+ transpose_or_reshape = reshape;+ }++ auto concat = std::make_shared<ov::op::v0::Concat>(ov::NodeVector{convert, transpose_or_reshape}, -2);+ concat->set_friendly_name("concat");++ std::shared_ptr<ov::Node> concat_or_reshape = concat;++ if (kind == NetworkKind::llama3) {+ auto unsqueeze_pattern = create_shape_constant({2}, "unsqueese_pattern");+ auto unsqueeze = std::make_shared<ov::op::v0::Unsqueeze>(concat, unsqueeze_pattern);+ unsqueeze->set_friendly_name("unsqueeze");+++ auto broadcast_pattern = create_shape_constant({1, 8, 4, input_2 + 1, input_3}, "broadcast_pattern");+ //TODO: v1::Broadcast not working+ auto broadcast = std::make_shared<ov::op::v3::Broadcast>(unsqueeze, broadcast_pattern, ov::op::BroadcastType::BIDIRECTIONAL);+ broadcast->set_friendly_name("broadcast");++ auto reshape_pattern2 = create_shape_constant({0, 32, -1, input_3}, "reshape_pattern2");+ auto reshape2 = std::make_shared<ov::op::v1::Reshape>(broadcast, reshape_pattern2, true);+ reshape2->set_friendly_name("reshape2");++ concat_or_reshape = reshape2;+ }+++ auto param3 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1, 32, 1, input_shape[2] + 1});+ param3->set_friendly_name("param3");++ // TODO: what if v1 softmax???+ auto softmax = std::make_shared<ov::op::v8::Softmax>(param3, -2);+ softmax->set_friendly_name("softmax");++ // entry point matmul for matcher+ auto matmul = std::make_shared<ov::op::v0::MatMul>(softmax, concat_or_reshape);+ matmul->set_friendly_name("matmul");++ auto result = std::make_shared<ov::op::v0::Result>(matmul);+ result->set_friendly_name("res");+ return std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{param, param2, param3});+ }++ std::shared_ptr<ov::Model> model;+};+++TEST_P(TransposeVTTest, smoke_Run_MatchAndTransposeVT) {+ Validate();+}++namespace {+// eliminate direct shape dependency to match llama2, as in test and in optimize function+const std::vector<ov::Shape> input_shapes{{1, 0, 1151, 128}, {1, 0, 1141, 64}};++const std::vector<bool> withTranspose{true, false};++const std::vector<bool> withBroadCast{true, false};++const std::vector<NetworkKind> networkKind = {+ // llama2 or llama3 type of concat, with convert layer or without+ NetworkKind::llama2, NetworkKind::llama3+};++ INSTANTIATE_TEST_SUITE_P(smoke_Run_MatchAndTransposeVT,+ TransposeVTTest,+ ::testing::Combine(::testing::ValuesIn(input_shapes),+ ::testing::ValuesIn(withTranspose),+ ::testing::ValuesIn(withBroadCast),+ ::testing::ValuesIn(networkKind)),+ TransposeVTTest::getTestCaseName);++} // namespace