|
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 | // |
4 | 4 |
|
| 5 | +#include "partitioning/patterns/pre_compute.hpp" |
| 6 | + |
5 | 7 | #include <gtest/gtest.h> |
6 | 8 |
|
7 | 9 | #include <algorithm> |
|
11 | 13 |
|
12 | 14 | #include "openvino/core/except.hpp" |
13 | 15 | #include "openvino/op/ops.hpp" |
14 | | -#include "partitioning/patterns/pre_compute.hpp" |
15 | 16 |
|
16 | 17 | namespace { |
17 | 18 |
|
18 | 19 | std::shared_ptr<ov::Model> make_longrope_v5_model(const std::vector<float>& short_factor_values, |
19 | | - const std::vector<float>& long_factor_values, |
20 | | - const std::vector<float>& multiply_values, |
21 | | - const std::vector<float>& power_values) { |
| 20 | + const std::vector<float>& long_factor_values, |
| 21 | + const std::vector<float>& multiply_values, |
| 22 | + const std::vector<float>& power_values) { |
22 | 23 | auto data = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1, 4, 2}); |
23 | 24 | auto position_ids = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::Shape{2, 1}); |
24 | 25 |
|
25 | | - auto short_factor = ov::op::v0::Constant::create(ov::element::f32, |
26 | | - ov::Shape{short_factor_values.size()}, |
27 | | - short_factor_values); |
28 | | - auto long_factor = ov::op::v0::Constant::create(ov::element::f32, |
29 | | - ov::Shape{long_factor_values.size()}, |
30 | | - long_factor_values); |
31 | | - auto multiply_const = ov::op::v0::Constant::create(ov::element::f32, |
32 | | - ov::Shape{multiply_values.size()}, |
33 | | - multiply_values); |
| 26 | + auto short_factor = |
| 27 | + ov::op::v0::Constant::create(ov::element::f32, ov::Shape{short_factor_values.size()}, short_factor_values); |
| 28 | + auto long_factor = |
| 29 | + ov::op::v0::Constant::create(ov::element::f32, ov::Shape{long_factor_values.size()}, long_factor_values); |
| 30 | + auto multiply_const = |
| 31 | + ov::op::v0::Constant::create(ov::element::f32, ov::Shape{multiply_values.size()}, multiply_values); |
34 | 32 | auto power_const = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{power_values.size()}, power_values); |
35 | 33 |
|
36 | 34 | auto reduce_axes = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{2}, {0, 1}); |
@@ -79,6 +77,50 @@ std::shared_ptr<ov::Model> make_longrope_v5_model(const std::vector<float>& shor |
79 | 77 | "longrope_v5_test_model"); |
80 | 78 | } |
81 | 79 |
|
| 80 | +// Builds a minimal RoPE model matching RopePatternLLama2. |
| 81 | +// When with_concat2=true (LLama2 style): Transpose → Concat_2 → Sin/Cos, duplicate_freqs=true. |
| 82 | +// When with_concat2=false (GPT style): Transpose → Sin/Cos directly, duplicate_freqs=false. |
| 83 | +std::shared_ptr<ov::Model> make_rope_model(bool with_concat2) { |
| 84 | + auto data = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1, 4, 4}); |
| 85 | + auto position_ids = std::make_shared<ov::op::v0::Parameter>(ov::element::i64, ov::Shape{1, 4}); |
| 86 | + |
| 87 | + // inv_freq: constant [1, half_dim=2, 1] |
| 88 | + auto inv_freq = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{1, 2, 1}, {0.5f, 0.1f}); |
| 89 | + |
| 90 | + // ShapeOf → Gather(batch dim) → Concat_1 (broadcast target shape) |
| 91 | + auto shape_of = std::make_shared<ov::op::v3::ShapeOf>(data); |
| 92 | + auto gather_idx = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {0}); |
| 93 | + auto gather_axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {0}); |
| 94 | + auto gather = std::make_shared<ov::op::v8::Gather>(shape_of, gather_idx, gather_axis); |
| 95 | + auto ndims_const = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {2}); |
| 96 | + auto one_const = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {1}); |
| 97 | + auto concat_1 = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{gather, ndims_const, one_const}, 0); |
| 98 | + |
| 99 | + // Broadcast inv_freq to [1,2,1], MatMul with position_ids → Transpose |
| 100 | + auto broadcast = std::make_shared<ov::op::v3::Broadcast>(inv_freq, concat_1); |
| 101 | + auto unsq_axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {1}); |
| 102 | + auto unsqueeze = std::make_shared<ov::op::v0::Unsqueeze>(position_ids, unsq_axis); |
| 103 | + auto convert = std::make_shared<ov::op::v0::Convert>(unsqueeze, ov::element::f32); |
| 104 | + auto matmul = std::make_shared<ov::op::v0::MatMul>(broadcast, convert); // [1,2,4] |
| 105 | + auto perm = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{3}, std::vector<int64_t>{0, 2, 1}); |
| 106 | + auto transpose = std::make_shared<ov::op::v1::Transpose>(matmul, perm); // [1,4,2] |
| 107 | + |
| 108 | + // Sin/Cos either via Concat_2 (LLama2) or directly (GPT) |
| 109 | + ov::Output<ov::Node> sin_cos_input = transpose->output(0); |
| 110 | + if (with_concat2) { |
| 111 | + auto zeros = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{1, 4, 2}, std::vector<float>(8, 0.f)); |
| 112 | + sin_cos_input = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{transpose, zeros}, -1)->output(0); |
| 113 | + } |
| 114 | + |
| 115 | + auto sin = std::make_shared<ov::op::v0::Sin>(sin_cos_input); |
| 116 | + auto cos = std::make_shared<ov::op::v0::Cos>(sin_cos_input); |
| 117 | + |
| 118 | + return std::make_shared<ov::Model>( |
| 119 | + ov::ResultVector{std::make_shared<ov::op::v0::Result>(sin), std::make_shared<ov::op::v0::Result>(cos)}, |
| 120 | + ov::ParameterVector{data, position_ids}, |
| 121 | + with_concat2 ? "llama2_rope_test_model" : "gpt_rope_test_model"); |
| 122 | +} |
| 123 | + |
82 | 124 | bool has_input_name(const std::shared_ptr<ov::Model>& model, const std::string& name) { |
83 | 125 | const auto inputs = model->inputs(); |
84 | 126 | return std::any_of(inputs.begin(), inputs.end(), [&name](const auto& input) { |
@@ -113,16 +155,54 @@ TEST(PreComputeTest, RopeCacheThrowsOnMismatchedFactorSizesInLongRopeV5) { |
113 | 155 | auto model = make_longrope_v5_model({1.0f, 2.0f}, {4.0f, 5.0f}, {1.0f}, {1.0f}); |
114 | 156 | ov::npuw::patterns::pre_compute::RopeCache pass(/*max_prompt_len=*/16, "longrope_input"); |
115 | 157 |
|
116 | | - EXPECT_THROW(pass.run_on_model(model), |
117 | | - ov::AssertFailure); |
| 158 | + EXPECT_THROW(pass.run_on_model(model), ov::AssertFailure); |
118 | 159 | } |
119 | 160 |
|
120 | 161 | TEST(PreComputeTest, RopeCacheThrowsOnNonScalarPowerInLongRopeV5) { |
121 | 162 | auto model = make_longrope_v5_model({1.0f, 2.0f}, {4.0f, 5.0f}, {1.0f, 2.0f}, {1.0f, 2.0f}); |
122 | 163 | ov::npuw::patterns::pre_compute::RopeCache pass(/*max_prompt_len=*/16, "longrope_input"); |
123 | 164 |
|
124 | | - EXPECT_THROW(pass.run_on_model(model), |
125 | | - ov::AssertFailure); |
| 165 | + EXPECT_THROW(pass.run_on_model(model), ov::AssertFailure); |
| 166 | +} |
| 167 | + |
| 168 | +// Verifies that the merged RopePatternLLama2 correctly detects and removes the |
| 169 | +// LLama2-style sin/cos subgraph (with Concat_2 present, duplicate_freqs=true). |
| 170 | +TEST(PreComputeTest, RopeCacheTransformsLLama2Pattern) { |
| 171 | + auto model = make_rope_model(/*with_concat2=*/true); |
| 172 | + |
| 173 | + ov::npuw::patterns::pre_compute::RopeCache pass(/*max_prompt_len=*/16, /*longrope_input_name=*/{}); |
| 174 | + ASSERT_NO_THROW(pass.run_on_model(model)); |
| 175 | + |
| 176 | + const auto& ops = model->get_ops(); |
| 177 | + const auto sin_count = std::count_if(ops.begin(), ops.end(), [](const auto& op) { |
| 178 | + return ov::is_type<ov::op::v0::Sin>(op); |
| 179 | + }); |
| 180 | + const auto cos_count = std::count_if(ops.begin(), ops.end(), [](const auto& op) { |
| 181 | + return ov::is_type<ov::op::v0::Cos>(op); |
| 182 | + }); |
| 183 | + |
| 184 | + EXPECT_EQ(sin_count, 0) << "Sin should be replaced by Gather from the duplicated LUT"; |
| 185 | + EXPECT_EQ(cos_count, 0) << "Cos should be replaced by Gather from the duplicated LUT"; |
| 186 | +} |
| 187 | + |
| 188 | +// Verifies that the merged RopePatternLLama2 correctly detects and removes the |
| 189 | +// GPT-style sin/cos subgraph (Concat_2 absent, duplicate_freqs=false). |
| 190 | +TEST(PreComputeTest, RopeCacheTransformsGPTPattern) { |
| 191 | + auto model = make_rope_model(/*with_concat2=*/false); |
| 192 | + |
| 193 | + ov::npuw::patterns::pre_compute::RopeCache pass(/*max_prompt_len=*/16, /*longrope_input_name=*/{}); |
| 194 | + ASSERT_NO_THROW(pass.run_on_model(model)); |
| 195 | + |
| 196 | + const auto& ops = model->get_ops(); |
| 197 | + const auto sin_count = std::count_if(ops.begin(), ops.end(), [](const auto& op) { |
| 198 | + return ov::is_type<ov::op::v0::Sin>(op); |
| 199 | + }); |
| 200 | + const auto cos_count = std::count_if(ops.begin(), ops.end(), [](const auto& op) { |
| 201 | + return ov::is_type<ov::op::v0::Cos>(op); |
| 202 | + }); |
| 203 | + |
| 204 | + EXPECT_EQ(sin_count, 0) << "Sin should be replaced by Gather from the non-duplicated LUT"; |
| 205 | + EXPECT_EQ(cos_count, 0) << "Cos should be replaced by Gather from the non-duplicated LUT"; |
126 | 206 | } |
127 | 207 |
|
128 | 208 | } // namespace |
0 commit comments