Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[Snippets] Fix static kernel cache collision for constant-repacked inputs (#34521)

Vladislav Golubev9d05f9c145db
diff --git a/src/plugins/intel_cpu/src/emitters/snippets/input_repacker.cpp b/src/plugins/intel_cpu/src/emitters/snippets/input_repacker.cpp
index c095bf344a3..4a4677f302f 100644
--- a/src/plugins/intel_cpu/src/emitters/snippets/input_repacker.cpp
+++ b/src/plugins/intel_cpu/src/emitters/snippets/input_repacker.cpp
@@ -29,6 +29,10 @@ const CpuBlockedMemoryDescPtr& InputRepacker::desc() const {
return m_desc;
}
+bool InputRepacker::already_repacked() const {
+ return m_desc != nullptr;
+}
+
const VectorDims& InputRepacker::in_offsets() const {
return m_in_offsets;
}
diff --git a/src/plugins/intel_cpu/src/emitters/snippets/input_repacker.hpp b/src/plugins/intel_cpu/src/emitters/snippets/input_repacker.hpp
index cc25dfd4dca..921cc978629 100644
--- a/src/plugins/intel_cpu/src/emitters/snippets/input_repacker.hpp
+++ b/src/plugins/intel_cpu/src/emitters/snippets/input_repacker.hpp
@@ -38,6 +38,8 @@ struct InputRepacker {
[[nodiscard]] const CpuBlockedMemoryDescPtr& desc() const;
[[nodiscard]] const VectorDims& in_offsets() const;
[[nodiscard]] const VectorDims& out_offsets() const;
+ // Returns true if the input has been already pre-packed (e.g. at compile time by RepackMatMulWeights).
+ [[nodiscard]] bool already_repacked() const;
private:
std::shared_ptr<const InputRepackerKernel> m_kernel{nullptr};
diff --git a/src/plugins/intel_cpu/src/nodes/subgraph.cpp b/src/plugins/intel_cpu/src/nodes/subgraph.cpp
index 89330b18254..74d25739c34 100644
--- a/src/plugins/intel_cpu/src/nodes/subgraph.cpp
+++ b/src/plugins/intel_cpu/src/nodes/subgraph.cpp
@@ -122,9 +122,12 @@ namespace {
#if defined(OPENVINO_ARCH_X86_64) || defined(OPENVINO_ARCH_ARM64) || defined(OPENVINO_ARCH_RISCV64)
struct SubgraphKey {
SubgraphKey() = default;
- SubgraphKey(std::shared_ptr<SubgraphAttrs> attrs_, std::vector<VectorDims> in_shapes_)
+ SubgraphKey(std::shared_ptr<SubgraphAttrs> attrs_,
+ std::vector<VectorDims> in_shapes_,
+ uint32_t constant_repacked_mask_ = 0)
: attrs(std::move(attrs_)),
- in_shapes(std::move(in_shapes_)) {}
+ in_shapes(std::move(in_shapes_)),
+ constant_repacked_mask(constant_repacked_mask_) {}
virtual ~SubgraphKey() = default;
[[nodiscard]] size_t hash() const {
@@ -135,35 +138,44 @@ struct SubgraphKey {
for (const auto& shape : in_shapes) {
seed = get_vector_hash(seed, shape);
}
+ seed = hash_combine(seed, constant_repacked_mask);
return seed;
}
bool operator==(const SubgraphKey& rhs) const {
- return *attrs == *rhs.attrs && in_shapes == rhs.in_shapes;
+ return *attrs == *rhs.attrs && in_shapes == rhs.in_shapes &&
+ constant_repacked_mask == rhs.constant_repacked_mask;
}
std::shared_ptr<SubgraphAttrs> attrs = nullptr;
std::vector<VectorDims> in_shapes;
+ uint32_t constant_repacked_mask = 0;
};
struct SubgraphCodeGeneratorKey {
- SubgraphCodeGeneratorKey(std::shared_ptr<SubgraphAttrs> attrs_, uint8_t mask_)
+ SubgraphCodeGeneratorKey(std::shared_ptr<SubgraphAttrs> attrs_,
+ uint32_t broadcasting_mask_,
+ uint32_t constant_repacked_mask_ = 0)
: attrs(std::move(attrs_)),
- broadcasting_mask(mask_) {}
+ broadcasting_mask(broadcasting_mask_),
+ constant_repacked_mask(constant_repacked_mask_) {}
[[nodiscard]] size_t hash() const {
using namespace dnnl::impl;
using namespace dnnl::impl::primitive_hashing;
size_t seed = get_attr_hash(0, attrs);
- return hash_combine(seed, broadcasting_mask);
+ seed = hash_combine(seed, broadcasting_mask);
+ return hash_combine(seed, constant_repacked_mask);
}
bool operator==(const SubgraphCodeGeneratorKey& rhs) const {
- return *attrs == *rhs.attrs && broadcasting_mask == rhs.broadcasting_mask;
+ return *attrs == *rhs.attrs && broadcasting_mask == rhs.broadcasting_mask &&
+ constant_repacked_mask == rhs.constant_repacked_mask;
}
std::shared_ptr<SubgraphAttrs> attrs = nullptr;
uint32_t broadcasting_mask = 0;
+ uint32_t constant_repacked_mask = 0;
};
#endif
@@ -735,6 +747,22 @@ uint32_t Subgraph::getBroadcastingMask(const std::vector<VectorDims>& input_shap
return mask;
}
+#if defined(OPENVINO_ARCH_X86_64)
+uint32_t Subgraph::getConstantRepackedMask() const {
+ // Returns a bitmask of inputs pre-packed at compile time (constant weights via RepackMatMulWeights).
+ const auto& preconfigured =
+ ov::as_type_ptr<CPURuntimeConfig>(subgraph_attrs->snippet->get_runtime_configurator()->get_config());
+ uint32_t mask = 0;
+ for (const auto& [i, repacker] : preconfigured->input_repackers) {
+ if (repacker.already_repacked()) {
+ CPU_NODE_ASSERT(i < sizeof(mask) * CHAR_BIT, "Input index ", i, " exceeds constant_repacked_mask capacity");
+ mask |= (1U << i);
+ }
+ }
+ return mask;
+}
+#endif
+
std::set<size_t> Subgraph::getConstantInputIndexes() const {
// TODO [153480]: Some constant inputs can have additional operations
// between Constant and target node input: Convert, Transpose etc.
@@ -847,7 +875,7 @@ void Subgraph::prepareParams() {
// 3. Create SubgraphStaticExecutor
const auto& snippet_config = ov::as_type_ptr<CPURuntimeConfig>(snippet->update_runtime_config());
const auto code_gen_result = cache->getOrCreate(
- SubgraphCodeGeneratorKey(subgraph_attrs, getBroadcastingMask(in_shapes)),
+ SubgraphCodeGeneratorKey(subgraph_attrs, getBroadcastingMask(in_shapes), key.constant_repacked_mask),
[this, &snippet_config](const SubgraphCodeGeneratorKey& key) -> std::shared_ptr<SubgraphCodeGenerator> {
return std::make_shared<SubgraphCodeGenerator>(key.attrs, snippet_config, external_ptrs_idces);
});
@@ -862,10 +890,18 @@ void Subgraph::prepareParams() {
cache);
};
- const auto result = cache->getOrCreate(SubgraphKey(subgraph_attrs, in_shapes), builder);
+ // In the static case, io_data_offsets are baked into the JIT kernel, so two nodes with identical
+ // body structure and input shapes but different weight packing states produce different kernels
+ // and must be cached separately. In the dynamic case, offsets are recomputed at runtime, so the
+ // packing state has no effect on the generated code
+# if defined(OPENVINO_ARCH_X86_64)
+ const auto constant_repacked_mask = is_dynamic ? 0U : getConstantRepackedMask();
+# else
+ constexpr uint32_t constant_repacked_mask = 0;
+# endif
+ const auto result = cache->getOrCreate(SubgraphKey(subgraph_attrs, in_shapes, constant_repacked_mask), builder);
execPtr = result.first;
#endif
-
CPU_NODE_ASSERT(execPtr, "Executor is not created for node ", getName(), ".");
}
diff --git a/src/plugins/intel_cpu/src/nodes/subgraph.h b/src/plugins/intel_cpu/src/nodes/subgraph.h
index 53872dae387..af8c50b3156 100644
--- a/src/plugins/intel_cpu/src/nodes/subgraph.h
+++ b/src/plugins/intel_cpu/src/nodes/subgraph.h
@@ -1,4 +1,4 @@
-// Copyright (C) 2020-2022 Intel Corporation
+// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
@@ -71,6 +71,9 @@ private:
static uint64_t getBodyHash(const std::shared_ptr<snippets::op::Subgraph>& snippet);
uint32_t getBroadcastingMask(const std::vector<VectorDims>& input_shapes);
+#if defined(OPENVINO_ARCH_X86_64)
+ uint32_t getConstantRepackedMask() const;
+#endif
std::set<size_t> getConstantInputIndexes() const;
using DataFlowPasses = std::vector<ov::snippets::pass::Manager::PositionedPassBase>;
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/external_repacking_adjuster.cpp b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/external_repacking_adjuster.cpp
index dae2907e84e..a9f870367cd 100644
--- a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/external_repacking_adjuster.cpp
+++ b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/external_repacking_adjuster.cpp
@@ -48,9 +48,8 @@ BrgemmExternalRepackingAdjuster::BrgemmExternalRepackingAdjuster(const ov::snipp
OPENVINO_ASSERT(idx < params.size(), "Incorrect index of repacked input");
auto config = RepackedInputConfig{get_kernel_config(params[idx])};
- // `desc() == nullptr` means repacking has to be executed at runtime.
- // If descriptor is already set, weights were prepacked beforehand and only offsets must be adjusted.
- if (!input_repacker.desc()) {
+ // If not already repacked at compile time, create a runtime executor for it.
+ if (!input_repacker.already_repacked()) {
config.executor = create_executor(config.kernel_config, configurator->get_cache());
config.needs_runtime_repacking = true;
}
diff --git a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/snippets/mha.cpp b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/snippets/mha.cpp
index 5294740eb1d..34e4c328213 100644
--- a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/snippets/mha.cpp
+++ b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/snippets/mha.cpp
@@ -56,6 +56,17 @@ std::vector<std::vector<InputShape>> transposedShape_4D_matmul1_const_b() {
return SNIPPETS_TESTS_STATIC_SHAPES({{1, 300, 8, 32}, {1, 300, 8, 32}, {1, 8, 300, 300}, {1, 300, 8, 32}});
}
+std::vector<std::vector<InputShape>> twoConstBShape_4D() {
+ return SNIPPETS_TESTS_STATIC_SHAPES({{1, 300, 8, 32}, // Q1
+ {1, 300, 8, 32}, // K1
+ {1, 8, 300, 300}, // Add1
+ // V1 is Constant
+ {1, 300, 8, 32}, // Q2
+ {1, 300, 8, 32}, // K2
+ {1, 8, 300, 300}, // Add2
+ {1, 8, 300, 32}}); // V2
+}
+
std::vector<std::vector<InputShape>> transposedShape_3D(bool with_dynamic = true) {
auto shapes = SNIPPETS_TESTS_STATIC_SHAPES({{128, 12, 64}, {128, 12, 64}, {12, 128, 128}, {128, 12, 64}},
{{68, 6, 92}, {68, 6, 92}, {1, 68, 68}, {68, 6, 92}},
@@ -116,6 +127,21 @@ INSTANTIATE_TEST_SUITE_P(smoke_Snippets_MHA_4D_MatMul1_Const_B_Are_Wei_Blocked,
::testing::Values(CPUTestUtils::empty_plugin_config)),
MHAConstB::getTestCaseName);
+// Ticket: CVS-180477
+INSTANTIATE_TEST_SUITE_P(smoke_Snippets_MHA_4D_TwoConstB_StaticShapesCacheCollisionRegression,
+ MHATwoConstB,
+ ::testing::Combine(::testing::ValuesIn(twoConstBShape_4D()),
+ ::testing::ValuesIn(precision_f32(4)),
+ ::testing::Values(ov::element::f32),
+ ::testing::Values(false), // with_mul (unused by MHATwoConstB)
+ ::testing::Values(false), // const_b_matmul0 (unused)
+ ::testing::Values(false), // const_b_matmul1 (unused)
+ ::testing::Values(4),
+ ::testing::Values(4),
+ ::testing::Values(ov::test::utils::DEVICE_CPU),
+ ::testing::Values(CPUTestUtils::empty_plugin_config)),
+ MHAConstB::getTestCaseName);
+
INSTANTIATE_TEST_SUITE_P(smoke_Snippets_MHA_4D_WithScalarMul,
MHA,
::testing::Combine(::testing::ValuesIn(transposedShape_4D(true, false)),
diff --git a/src/tests/functional/plugin/shared/include/snippets/mha.hpp b/src/tests/functional/plugin/shared/include/snippets/mha.hpp
index af485329041..13362632eea 100644
--- a/src/tests/functional/plugin/shared/include/snippets/mha.hpp
+++ b/src/tests/functional/plugin/shared/include/snippets/mha.hpp
@@ -83,6 +83,15 @@ protected:
bool m_const_b_matmul1 = false;
};
+// Regression test for cache-collision fix for static shapes.
+// Builds a model with two structurally-identical MHA branches that differ ONLY in how V is
+// provided: branch 1 uses a Constant V (→ pre-packed at compile time, constant_repacked_mask=1)
+// while branch 2 uses a runtime Parameter V (→ no pre-packing, constant_repacked_mask=0).
+class MHATwoConstB : public MHAConstB {
+protected:
+ std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
+};
+
class MHA2D : public MHA {
protected:
std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
diff --git a/src/tests/functional/plugin/shared/src/snippets/mha.cpp b/src/tests/functional/plugin/shared/src/snippets/mha.cpp
index 2bfd330bfe4..241ba1f11d2 100644
--- a/src/tests/functional/plugin/shared/src/snippets/mha.cpp
+++ b/src/tests/functional/plugin/shared/src/snippets/mha.cpp
@@ -228,6 +228,16 @@ std::shared_ptr<SnippetsFunctionBase> MHAConstB::get_subgraph() const {
m_const_b_matmul1);
}
+std::shared_ptr<SnippetsFunctionBase> MHATwoConstB::get_subgraph() const {
+ const bool is_with_reshape =
+ std::all_of(inputDynamicShapes.begin(), inputDynamicShapes.end(), [](const PartialShape& ps) { return ps.is_static(); });
+ OPENVINO_ASSERT(inputDynamicShapes.size() == 7,
+ "MHATwoConstB expects 7 input shapes: [Q1, K1, Add1, Q2, K2, Add2, V]");
+ return std::make_shared<ov::test::snippets::MHATwoConstBFunction>(inputDynamicShapes,
+ m_input_types,
+ is_with_reshape);
+}
+
void MHA::init_thresholds() {
MHABase::init_thresholds();
auto precision_hint = configuration.count(ov::hint::inference_precision.name())
@@ -359,6 +369,12 @@ TEST_P(MHAConstB, CompareWithRefImpl) {
validateNumSubgraphs();
}
+TEST_P(MHATwoConstB, CompareWithRefImpl) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+ run();
+ validateNumSubgraphs();
+}
+
TEST_P(MHAWithThreadCount, CompareWithRefImpl) {
SKIP_IF_CURRENT_TEST_IS_DISABLED()
run();
diff --git a/src/tests/ov_helpers/ov_snippets_models/include/subgraph_mha.hpp b/src/tests/ov_helpers/ov_snippets_models/include/subgraph_mha.hpp
index aabcb532f36..57fa1b5b621 100644
--- a/src/tests/ov_helpers/ov_snippets_models/include/subgraph_mha.hpp
+++ b/src/tests/ov_helpers/ov_snippets_models/include/subgraph_mha.hpp
@@ -90,6 +90,43 @@ protected:
const std::vector<ov::element::Type> precisions;
};
+// Builds a model with two structurally-identical MHA branches:
+// - Branch 1: Q1/K1/Add1 runtime parameters, V1 CONSTANT
+// → post-softmax Subgraph has are_wei_constant=true → RepackMatMulWeights pre-packs V1
+// → constant_repacked_mask bit is SET
+// - Branch 2: Q2/K2/Add2 runtime parameters, V2 RUNTIME PARAMETER
+// → post-softmax Subgraph has are_wei_constant=false → no pre-packing
+// → constant_repacked_mask bit is CLEAR
+class MHATwoConstBFunction : public SnippetsFunctionBase {
+public:
+ explicit MHATwoConstBFunction(const std::vector<PartialShape>& inputShapes,
+ const std::vector<ov::element::Type>& precisions,
+ bool with_reshape = true)
+ : SnippetsFunctionBase(inputShapes),
+ precisions(precisions),
+ with_reshape(with_reshape) {
+ OPENVINO_ASSERT(input_shapes.size() == 7, "Got invalid number of input shapes (expected 7)");
+ OPENVINO_ASSERT(precisions.size() == 4, "Got invalid number of input precisions");
+ OPENVINO_ASSERT(input_shapes[6].is_static(),
+ "V shape must be static (used to create V1 constant and V2 parameter)");
+ }
+
+protected:
+ std::shared_ptr<ov::Model> initOriginal() const override;
+ // 7 runtime parameters: Q1, K1, Add1, Q2, K2, Add2, V2.
+ // V1 is embedded as a Constant (not a runtime Parameter).
+ void validate_function(const std::shared_ptr<Model>& f) const override {
+ OPENVINO_ASSERT(f != nullptr, "The test requires Model to be defined");
+ validate_params_shape({input_shapes[0], input_shapes[1], input_shapes[2],
+ input_shapes[3], input_shapes[4], input_shapes[5],
+ input_shapes[6]},
+ f->get_parameters());
+ }
+
+ const std::vector<ov::element::Type> precisions;
+ const bool with_reshape = true;
+};
+
class MHA2DFunction : public SnippetsFunctionBase {
public:
explicit MHA2DFunction(const std::vector<PartialShape>& inputShapes, const std::vector<ov::element::Type>& precisions)
diff --git a/src/tests/ov_helpers/ov_snippets_models/src/subgraph_mha.cpp b/src/tests/ov_helpers/ov_snippets_models/src/subgraph_mha.cpp
index 3dd7492720f..25ca8882296 100644
--- a/src/tests/ov_helpers/ov_snippets_models/src/subgraph_mha.cpp
+++ b/src/tests/ov_helpers/ov_snippets_models/src/subgraph_mha.cpp
@@ -237,6 +237,75 @@ void MHAConstBFunction::validate_function(const std::shared_ptr<Model>& f) const
validate_params_shape(expected_shapes, f->get_parameters());
}
+std::shared_ptr<ov::Model> MHATwoConstBFunction::initOriginal() const {
+ OPENVINO_ASSERT(std::all_of(input_shapes.begin(), input_shapes.end(),
+ [](const PartialShape& s) { return s.is_static(); }),
+ "MHATwoConstBFunction requires static input shapes");
+
+ auto q1Param = std::make_shared<ov::opset1::Parameter>(precisions[0], input_shapes[0]);
+ auto k1Param = std::make_shared<ov::opset1::Parameter>(precisions[1], input_shapes[1]);
+ auto add1Param = std::make_shared<ov::opset1::Parameter>(precisions[2], input_shapes[2]);
+
+ auto vConst1 = ov::test::utils::make_constant(precisions[3], input_shapes[6].to_shape());
+
+ auto q2Param = std::make_shared<ov::opset1::Parameter>(precisions[0], input_shapes[3]);
+ auto k2Param = std::make_shared<ov::opset1::Parameter>(precisions[1], input_shapes[4]);
+ auto add2Param = std::make_shared<ov::opset1::Parameter>(precisions[2], input_shapes[5]);
+
+ auto v2Param = std::make_shared<ov::opset1::Parameter>(precisions[3], input_shapes[6]);
+
+ const size_t rank = input_shapes[0].size();
+ const auto fusion_order_vals = get_fusion_order(rank);
+ const auto decomposed_order_vals = get_decomposed_order(rank);
+
+ auto build_branch = [&](std::shared_ptr<ov::Node> q_in,
+ std::shared_ptr<ov::Node> k_in,
+ std::shared_ptr<ov::Node> add_in,
+ std::shared_ptr<ov::Node> v_in) -> std::shared_ptr<ov::Node> {
+ auto t0c = ov::op::v0::Constant::create(ov::element::i64, {rank}, fusion_order_vals);
+ auto t1c = ov::op::v0::Constant::create(ov::element::i64, {rank}, decomposed_order_vals);
+ auto t3c = ov::op::v0::Constant::create(ov::element::i64, {rank}, fusion_order_vals);
+
+ auto t0 = std::make_shared<ov::op::v1::Transpose>(q_in, t0c);
+ auto t1 = std::make_shared<ov::op::v1::Transpose>(k_in, t1c);
+ auto mm0 = std::make_shared<ov::op::v0::MatMul>(t0, t1);
+ auto add = std::make_shared<ov::op::v1::Add>(mm0, add_in);
+
+ std::shared_ptr<ov::Node> softmax_out;
+ if (with_reshape) {
+ const auto interm_shape = add->get_output_shape(0);
+ const size_t batch = std::accumulate(interm_shape.begin(),
+ interm_shape.begin() + static_cast<std::ptrdiff_t>(rank - 1),
+ size_t{1},
+ std::multiplies<size_t>());
+ auto r0c = ov::op::v0::Constant::create(ov::element::i64, {2},
+ std::vector<int64_t>{static_cast<int64_t>(batch), -1});
+ auto r1c = ov::op::v0::Constant::create(ov::element::i64,
+ {interm_shape.size()},
+ std::vector<int64_t>(interm_shape.begin(), interm_shape.end()));
+ auto r0 = std::make_shared<ov::opset1::Reshape>(add, r0c, true);
+ auto sm = std::make_shared<ov::opset1::Softmax>(r0, 1);
+ softmax_out = std::make_shared<ov::opset1::Reshape>(sm, r1c, true);
+ } else {
+ softmax_out = std::make_shared<ov::opset1::Softmax>(add, rank - 1);
+ }
+
+ // v_in is pre-transposed → use directly (no Transpose here)
+ auto mm1 = std::make_shared<ov::op::v0::MatMul>(softmax_out, v_in);
+ return std::make_shared<ov::op::v1::Transpose>(mm1, t3c);
+ };
+
+ auto out1 = build_branch(q1Param, k1Param, add1Param, vConst1);
+ auto out2 = build_branch(q2Param, k2Param, add2Param, v2Param);
+
+ ov::ParameterVector params = {q1Param, k1Param, add1Param, q2Param, k2Param, add2Param, v2Param};
+ ov::ResultVector results = {
+ std::make_shared<ov::opset1::Result>(out1),
+ std::make_shared<ov::opset1::Result>(out2),
+ };
+ return std::make_shared<ov::Model>(results, params, "mha_two_const_b");
+}
+
std::shared_ptr<ov::Model> MHA2DFunction::initOriginal() const {
auto param0 = std::make_shared<ov::opset1::Parameter>(precisions[0], input_shapes[0]);
auto param1 = std::make_shared<ov::opset1::Parameter>(precisions[1], input_shapes[1]);