Skip to content

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

[TRANSFORMATION] Fixed bias detection in PagedCausalConv1d fusion (#35621)

Mikhail Ryzhove3f32f435824
diff --git a/src/common/transformations/src/transformations/paged_attention/paged_causal_conv1d_fusion.cpp b/src/common/transformations/src/transformations/paged_attention/paged_causal_conv1d_fusion.cpp
index c5455edc5da..be867eef1cf 100644
--- a/src/common/transformations/src/transformations/paged_attention/paged_causal_conv1d_fusion.cpp
+++ b/src/common/transformations/src/transformations/paged_attention/paged_causal_conv1d_fusion.cpp
@@ -15,6 +15,7 @@
#include "openvino/core/graph_util.hpp"
#include "openvino/core/rt_info.hpp"
#include "openvino/core/type/element_type.hpp"
+#include "openvino/op/add.hpp"
#include "openvino/op/assign.hpp"
#include "openvino/op/concat.hpp"
#include "openvino/op/constant.hpp"
@@ -48,11 +49,6 @@ namespace v8 = ov::op::v8;
namespace {
-std::shared_ptr<ov::Node> make_zero_bias(const ov::element::Type& type, const size_t channels) {
- std::vector<float> values(channels, 0.0f);
- return v0::Constant::create(type, ov::Shape{channels}, values);
-}
-
ov::PartialShape make_conv_state_table_shape(const ov::PartialShape& past_state_shape) {
if (past_state_shape.rank().is_static() && past_state_shape.rank().get_length() == 3) {
return ov::PartialShape{ov::Dimension::dynamic(), past_state_shape[1], past_state_shape[2]};
@@ -79,7 +75,10 @@ PagedCausalConv1DFusion::PagedCausalConv1DFusion(ov::pass::paged_attention::PaPa
auto p_weight_input = any_input(has_static_shape() && rank_equals(4));
auto p_group_conv = wrap_type<v1::GroupConvolution>({p_state_concat, p_weight_input});
- auto p_slice_out = wrap_type<v8::Slice>({p_group_conv, any_input(), any_input(), any_input(), any_input()});
+ auto p_bias_input = any_input(has_static_shape());
+ auto p_add_bias = ov::pass::pattern::optional<v1::Add>({p_group_conv, p_bias_input});
+
+ auto p_slice_out = wrap_type<v8::Slice>({p_add_bias, any_input(), any_input(), any_input(), any_input()});
ov::matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS, &pa_params, &var_ids_to_remove](
ov::pass::pattern::Matcher& m) {
@@ -141,7 +140,32 @@ PagedCausalConv1DFusion::PagedCausalConv1DFusion(ov::pass::paged_attention::PaPa
static_cast<int64_t>(kernel_size)});
const auto weight_reshaped = std::make_shared<v1::Reshape>(weight_node, pa_weight_shape, false);
- const auto bias_node = make_zero_bias(input_embeds_node->get_output_element_type(0), hidden_size);
+ const auto& elem_type = input_embeds_node->get_output_element_type(0);
+ std::shared_ptr<ov::Node> bias_node;
+ if (pm.count(p_add_bias)) {
+ const auto bias_input = pm.at(p_bias_input).get_node_shared_ptr();
+ const auto& bias_shape = bias_input->get_output_partial_shape(0);
+ if (bias_shape.rank().is_static() && bias_shape.rank().get_length() == 1 &&
+ bias_shape[0].compatible(hidden_size)) {
+ bias_node = bias_input;
+ } else {
+ if (bias_shape.is_static()) {
+ // Validate that the total element count matches hidden_size
+ size_t num_elements = 1;
+ for (size_t i = 0; i < static_cast<size_t>(bias_shape.rank().get_length()); ++i) {
+ num_elements *= bias_shape[i].get_length();
+ }
+ if (num_elements != hidden_size) {
+ return false;
+ }
+ }
+ const auto bias_shape_node =
+ v0::Constant::create(ov::element::i64, ov::Shape{1}, std::vector<int64_t>{-1});
+ bias_node = std::make_shared<v1::Reshape>(bias_input, bias_shape_node, false);
+ }
+ } else {
+ bias_node = v0::Constant::create(elem_type, ov::Shape{0}, std::vector<float>{});
+ }
const auto paged_conv =
std::make_shared<ov::op::internal::PagedCausalConv1D>(input_embeds_node,
diff --git a/src/common/transformations/tests/common_optimizations/paged_causal_conv1d_fusion.cpp b/src/common/transformations/tests/common_optimizations/paged_causal_conv1d_fusion.cpp
index 294b45d83ab..a4b3960f2a2 100644
--- a/src/common/transformations/tests/common_optimizations/paged_causal_conv1d_fusion.cpp
+++ b/src/common/transformations/tests/common_optimizations/paged_causal_conv1d_fusion.cpp
@@ -12,6 +12,7 @@
#include "common_test_utils/ov_test_utils.hpp"
#include "openvino/core/model.hpp"
+#include "openvino/op/add.hpp"
#include "openvino/op/concat.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/convert.hpp"
@@ -193,6 +194,151 @@ std::shared_ptr<ov::Model> build_model_with_converted_weights(bool add_present_s
return std::make_shared<ov::Model>(results, params);
}
+// GroupConvolution followed by Add(bias) before Slice.
+std::shared_ptr<ov::Model> build_model_with_add_bias() {
+ const Shape input_shape{2, 3};
+ const Shape state_shape{2, 3, 4};
+
+ auto input_embeds = std::make_shared<v0::Parameter>(element::f32, input_shape);
+ auto past_state_param = std::make_shared<v0::Parameter>(element::f32, state_shape);
+ past_state_param->set_friendly_name("past_state");
+ auto read_value = std::make_shared<ov::op::v3::ReadValue>(past_state_param->output(0), "past_cache");
+ read_value->get_output_tensor(0).set_names({"cache_params.past.conv.0"});
+
+ auto part_shape = v0::Constant::create(element::i64, Shape{3}, {2, 3, 1});
+ auto part0 = std::make_shared<v1::Reshape>(input_embeds, part_shape, false);
+ auto part1 = std::make_shared<v1::Reshape>(input_embeds, part_shape, false);
+ auto part2 = std::make_shared<v1::Reshape>(input_embeds, part_shape, false);
+ auto token_concat = std::make_shared<v0::Concat>(OutputVector{part0, part1, part2}, -1);
+ auto transpose_order = v0::Constant::create(element::i64, Shape{3}, {0, 2, 1});
+ auto token_transpose = std::make_shared<v1::Transpose>(token_concat, transpose_order);
+
+ auto state_concat = std::make_shared<v0::Concat>(OutputVector{read_value, token_transpose}, -1);
+
+ auto weights = v0::Constant::create(element::f32, Shape{3, 1, 1, 4}, std::vector<float>(12, 0.25f));
+ auto group_conv = std::make_shared<v1::GroupConvolution>(state_concat,
+ weights,
+ Strides{1},
+ CoordinateDiff{0},
+ CoordinateDiff{0},
+ Strides{1});
+
+ // explicit bias constant added after GroupConvolution
+ auto bias = v0::Constant::create(element::f32, Shape{3, 1}, std::vector<float>(3, 0.1f));
+ auto add_bias = std::make_shared<v1::Add>(group_conv, bias);
+
+ auto neg_one = v0::Constant::create(element::i64, Shape{1}, {-1});
+ auto one = v0::Constant::create(element::i64, Shape{1}, {1});
+ auto slice_begin = std::make_shared<v1::Multiply>(neg_one, one);
+ auto slice_end = v0::Constant::create(element::i64, Shape{1}, {std::numeric_limits<int64_t>::max()});
+ auto slice_step = v0::Constant::create(element::i64, Shape{1}, {1});
+ auto slice_axis = v0::Constant::create(element::i64, Shape{1}, {2});
+ auto slice2 = std::make_shared<v8::Slice>(add_bias, slice_begin, slice_end, slice_step, slice_axis);
+ auto swish = std::make_shared<v4::Swish>(slice2);
+
+ auto state_slice = std::make_shared<v8::Slice>(state_concat, slice_begin, slice_end, slice_step, slice_axis);
+ auto present_res = std::make_shared<v0::Result>(state_slice);
+ present_res->get_output_tensor(0).set_names({"cache_params.present.conv.0"});
+
+ ParameterVector params{input_embeds, past_state_param};
+ return std::make_shared<ov::Model>(ResultVector{std::make_shared<v0::Result>(swish), present_res}, params);
+}
+
+std::shared_ptr<ov::Model> build_fused_reference_model(const bool use_explicit_bias) {
+ const Shape input_shape{2, 3};
+ const Shape state_shape{2, 3, 4};
+
+ auto input_embeds = std::make_shared<v0::Parameter>(element::f32, input_shape);
+ auto past_state_param = std::make_shared<v0::Parameter>(element::f32, state_shape);
+ past_state_param->set_friendly_name("past_state");
+
+ auto read_value = std::make_shared<ov::op::v3::ReadValue>(past_state_param->output(0), "past_cache");
+ read_value->get_output_tensor(0).set_names({"cache_params.past.conv.0"});
+
+ auto part_shape = v0::Constant::create(element::i64, Shape{3}, {2, 3, 1});
+ auto part0 = std::make_shared<v1::Reshape>(input_embeds, part_shape, false);
+ auto part1 = std::make_shared<v1::Reshape>(input_embeds, part_shape, false);
+ auto part2 = std::make_shared<v1::Reshape>(input_embeds, part_shape, false);
+
+ auto token_concat = std::make_shared<v0::Concat>(OutputVector{part0, part1, part2}, -1);
+ auto transpose_order = v0::Constant::create(element::i64, Shape{3}, {0, 2, 1});
+ auto token_transpose = std::make_shared<v1::Transpose>(token_concat, transpose_order);
+ auto state_concat = std::make_shared<v0::Concat>(OutputVector{read_value, token_transpose}, -1);
+
+ auto input_embeds_shape = v0::Constant::create(element::i64, Shape{2}, std::vector<int64_t>{-1, 3});
+ auto input_embeds_node = std::make_shared<v1::Reshape>(token_transpose, input_embeds_shape, false);
+
+ auto weights = v0::Constant::create(element::f32, Shape{3, 1, 1, 4}, std::vector<float>(12, 0.25f));
+ auto pa_weight_shape = v0::Constant::create(element::i64, Shape{3}, std::vector<int64_t>{3, 1, 4});
+ auto weight_reshaped = std::make_shared<v1::Reshape>(weights, pa_weight_shape, false);
+
+ std::shared_ptr<ov::Node> bias;
+ if (use_explicit_bias) {
+ auto bias_const = v0::Constant::create(element::f32, Shape{3, 1}, std::vector<float>(3, 0.1f));
+ auto bias_shape = v0::Constant::create(element::i64, Shape{1}, {-1});
+ bias = std::make_shared<v1::Reshape>(bias_const, bias_shape, false);
+ } else {
+ bias = v0::Constant::create(element::f32, Shape{0}, std::vector<float>{});
+ }
+
+ auto subsequence_begins = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
+ subsequence_begins->set_friendly_name("subsequence_begins");
+ subsequence_begins->get_output_tensor(0).set_names({"subsequence_begins"});
+
+ auto block_indices = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
+ block_indices->set_friendly_name("la.block_indices");
+ block_indices->get_output_tensor(0).set_names({"la.block_indices"});
+
+ auto block_indices_begins = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
+ block_indices_begins->set_friendly_name("la.block_indices_begins");
+ block_indices_begins->get_output_tensor(0).set_names({"la.block_indices_begins"});
+
+ auto past_lens = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
+ past_lens->set_friendly_name("la.past_lens");
+ past_lens->get_output_tensor(0).set_names({"la.past_lens"});
+
+ auto cache_interval = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});
+ cache_interval->set_friendly_name("la.cache_interval");
+ cache_interval->get_output_tensor(0).set_names({"la.cache_interval"});
+
+ auto conv_state_table = std::make_shared<v0::Parameter>(element::dynamic, PartialShape{-1, 3, 4});
+ conv_state_table->set_friendly_name("conv_state_table.0");
+ conv_state_table->get_output_tensor(0).set_names({"conv_state_table.0"});
+
+ auto paged_conv = std::make_shared<ov::op::internal::PagedCausalConv1D>(input_embeds_node,
+ conv_state_table,
+ weight_reshaped,
+ bias,
+ subsequence_begins,
+ block_indices,
+ block_indices_begins,
+ past_lens,
+ cache_interval);
+ auto unsqueeze_axis = v0::Constant::create(element::i64, Shape{1}, {2});
+ auto unsqueeze = std::make_shared<v0::Unsqueeze>(paged_conv, unsqueeze_axis);
+ auto swish = std::make_shared<v4::Swish>(unsqueeze);
+
+ auto neg_one = v0::Constant::create(element::i64, Shape{1}, {-1});
+ auto one = v0::Constant::create(element::i64, Shape{1}, {1});
+ auto slice_begin = std::make_shared<v1::Multiply>(neg_one, one);
+ auto slice_end = v0::Constant::create(element::i64, Shape{1}, {std::numeric_limits<int64_t>::max()});
+ auto slice_step = v0::Constant::create(element::i64, Shape{1}, {1});
+ auto slice_axis = v0::Constant::create(element::i64, Shape{1}, {2});
+ auto state_slice = std::make_shared<v8::Slice>(state_concat, slice_begin, slice_end, slice_step, slice_axis);
+ auto present_res = std::make_shared<v0::Result>(state_slice);
+ present_res->get_output_tensor(0).set_names({"cache_params.present.conv.0"});
+
+ ParameterVector params{input_embeds,
+ past_state_param,
+ subsequence_begins,
+ block_indices,
+ block_indices_begins,
+ past_lens,
+ cache_interval,
+ conv_state_table};
+ return std::make_shared<ov::Model>(ResultVector{std::make_shared<v0::Result>(swish), present_res}, params);
+}
+
std::shared_ptr<ov::Model> build_model_without_concat_lfm2_like() {
const Shape state_shape{2, 3, 4};
const Shape token_shape{2, 3, 1};
@@ -425,3 +571,28 @@ TEST_F(PagedCausalConv1DFusionTest, FusesWhenWeightsComeFromConvertPath) {
EXPECT_EQ(pcc_count, 1u) << "Expected 1 PagedCausalConv1D after fusion";
EXPECT_EQ(group_conv_count, 0u) << "Expected 0 GroupConvolution after fusion";
}
+
+// Verifies that when GroupConvolution is followed by Add(bias_const) before Slice,
+// the fusion fires and the explicit bias constant is passed to PagedCausalConv1D.
+TEST_F(PagedCausalConv1DFusionTest, FusesWithExplicitBiasConstant) {
+ comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES);
+ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);
+
+ model = build_model_with_add_bias();
+ run_paged_causal_conv1d_fusion(model);
+
+ model_ref = build_fused_reference_model(true);
+}
+
+// Verifies that when GroupConvolution has no Add(bias) before Slice,
+// the fusion still fires and passes an empty bias Constant (shape {0}),
+// which represents "no bias" for PagedCausalConv1D.
+TEST_F(PagedCausalConv1DFusionTest, FusesNoBiasUsesEmptyBiasConstant) {
+ comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES);
+ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);
+
+ model = build_model(true);
+ run_paged_causal_conv1d_fusion(model);
+
+ model_ref = build_fused_reference_model(false);
+}