← Back to Pull requests

[CPU] Keep GatherCompressed for quantized models when LPT can't handle the Gather #40113

Merged
JasonOA888 merged 4 files (205 lines) on Nov 27, 2026 23:17 UTC
View on GitHub
JasonOA888 JasonOA888 commented on Nov 20, 2026 10:04 UTC
Author

Details:


- With LPT enabled (any int8 FakeQuantize in the model), the CPU ConvertGatherToGatherCompressed callback skipped every u8/i8 table and left its dequantization to LPT. LPT rejects a Gather whose dequantization is non-scalar on the gathered axis with runtime indices (GatherTransformation::canBeTransformed; [V, 1] is also rejected by the per-channel check in LayerTransformation::canBeTransformedStatic). Per-row-scaled embeddings ([V, 1] scale, runtime token ids, as written by nncf.quantize) were therefore left to LPT, rejected, and ConstantFolding expanded the i8 table to f32 (~4x the table's memory).
- The PreLpt callback is unchanged. Instead, runLptPasses registers ConvertGatherToGatherCompressed (u8/i8) as an additional main LPT pass (lowPrecPass->add_main). It runs in the same GraphRewrite right after GatherTransformation, so it only converts the Gathers LPT declined, before LPT's final ConstantFolding; the plugin doesn't need to predict LPT's decision. Scalar and per-column ([1, D], axis 0) scales keep the existing LPT path.
- GatherCompressed implementations (CPU, and GPU's gather_ref.cl) compute the scale / zero-point index as flat_idx / (weights_size / param_size), so each value has to cover a contiguous block of the flattened table. ConvertGatherToGatherCompressed now only matches such layouts: scale / zero point inputs with a static shape that is the leading weights dims followed by ones (scalar, [V, 1], [V, D], grouped [V, G, 1] on [V, G, K] weights before the reshape). Other layouts (e.g. a [1, D] scale or zero point) keep the plain decompression subgraph. Previously the pass converted them on all devices, for models without FakeQuantize and for u4/i4, and gave wrong results / out-of-bounds reads.
- Extended DisableGatherCompressedForQuantizedModel with per-row (i8/u8, GatherCompressed) and per-column (i8/u8, LPT path, plain Gather) scale cases, checking the Gather and MatMul precisions; the per-row cases fail on master. Added a parametrized negative ConvertGatherToGatherCompressed transformation suite for per-column scale / zero point, non-leading grouped scales and a scale that broadcasts the weights.

Tickets:


- Closes #40080

AI Assistance:


- AI assistance used: yes
- An AI coding assistant helped locate the cause and write the change and tests. Validation: built the CPU plugin, Python bindings and ov_cpu_func_tests on Linux x86_64 on top of current master; the new per-row tests fail on master and pass with the fix; ran ov_transformations_tests ConvertGatherToCompressed, DisableGatherCompressed, GatherWeightsDecompression, smoke_Gather, smoke_LPTGather, MatmulWeightsDecompression and the full smoke_LPT* suite (15399 passed; the only 2 failures are smoke_LPT/ConvolutionTransformation cases that fail identically on master); a Python reproduction from the issue (i8 table with per-row scale and one FakeQuantize) shows an f32 Gather table on master and an i8 GatherCompressed table with the fix, with scalar-scale models still taking the LPT path; GatherCompressed outputs for per-row scale on axis 0 and 1 match a NumPy reference; the LPT/Gather tests also pass under Intel SDE -spr (bf16).

Diff: 205 lines (+188 / -17) across 4 files Reviewer: v-Golubev Review duration: 10873m
All Vouch review confidence checks passed
Residual risk 0.06 = Change Risk (0.44) × (1 − Review Confidence (0.87)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.44 · 205 lines diff across 4 files
0.44
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.90 · 5 substantive comment(s)
0.90
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
87%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.319
Ai Authored ai_authored
0.050
Real PR Conversation Timeline
33 items
v-Golubev v-Golubev commented on Nov 23, 2026 09:46 UTC
Contributor

build_jenkins

JasonOA888 JasonOA888 commented on Nov 23, 2026 14:54 UTC
Author

Thanks for triggering Jenkins, @v-Golubev. The internal Jenkins logs aren't reachable from outside Intel's network, so I can't see why ie_tests_cpu_spr_ubuntu22_release and the build_ubuntu24_release, build_manylinux_2_28_release and build_rhel96_release jobs failed. All public GitHub Actions checks are green.

Locally, this branch merged with current master builds cleanly, and the GatherCompressed, Gather LPT and weights-decompression functional tests (603 tests) pass both natively on AVX2 and under Intel SDE emulating Sapphire Rapids.

Could you share the first error from those logs? If it's caused by this change, I'll fix it right away.

v-Golubev v-Golubev commented on code on Nov 24, 2026 19:04 UTC
Code Review
src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp:1
@@ -500,16 +534,36 @@ void Transformations::PreLpt(const std::vector<ov::element::Type>& defaultPrecis
     CPU_SET_CALLBACK_COMMON(
         decompression_handling_manager,
         [&](const_node_ptr& node) -> bool {
+            if (ov::is_type<ov::op::v8::Gather>(node)) {

Can we avoid predicting LPT's decision inside this callback? GatherTransformation is instantiated here with a copy of the LPT params (plus const_pointer_cast to call canBeTransformed), so the plugin repeats LPT's applicability logic and has to follow it whenever LPT changes.

As far as I see, LPT can make this decision itself. Keep the pre-PR behavior in this callback (useLpt + u8/i8 → leave the Gather to LPT). In runLptPasses, register the conversion inside LPT the same way activations_scaling.cpp does: lowPrecPass->add_main<ov::pass::ConvertGatherToGatherCompressed>(); with a callback that only checks has_blocked_decompression_params. Matchers of one GraphRewrite are tried in registration order, so it would see only the Gathers that GatherTransformation declined, before LPT's final ConstantFolding.

Note: the current ConvertGatherToGatherCompressed registration should be kept as is as well in order to keep the behavior for the currently supported scenarios.

v-Golubev v-Golubev commented on code on Nov 24, 2026 19:04 UTC
Code Review
src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/disable_gathercompressed_quantized_model.cpp:1
@@ -63,7 +81,13 @@ class DisableGatherCompressedForQuantizedModel : public testing::WithParamInterf
         auto weights_shape = Shape{64, 64};
         auto weights = utils::make_constant(weight_prec, weights_shape, utils::InputGenerateData(-1, 2, 32768));
         auto convert = std::make_shared<op::v0::Convert>(weights, element::f32);
-        auto multiply = std::make_shared<op::v1::Multiply>(convert, op::v0::Constant::create(type, {1, 1}, {0.625}));
+        const auto scale_const_shape = scale_shape == GatherScaleShape::PER_ROW      ? Shape{weights_shape[0], 1}

Minor: the nested ternary for the scale shape is hard to read; consider a switch or a small helper next to operator<<.

v-Golubev v-Golubev commented on code on Nov 24, 2026 20:05 UTC
Code Review
src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/disable_gathercompressed_quantized_model.cpp:1
@@ -98,6 +123,17 @@ class DisableGatherCompressedForQuantizedModel : public testing::WithParamInterf
             }
         }
 
+        if (scale_shape == GatherScaleShape::PER_ROW) {

Why are the matmul precision checks from the switch below skipped for PER_ROW and PER_COLUMN? Please check the expected input / runtime precisions for these cases too and add a comment explaining why they are expected (who handles the dequantization in each case: GatherCompressed or LPT).

JasonOA888 JasonOA888 commented on code on Nov 25, 2026 06:53 UTC
Code Review
src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp:1
@@ -500,16 +534,36 @@ void Transformations::PreLpt(const std::vector<ov::element::Type>& defaultPrecis
     CPU_SET_CALLBACK_COMMON(
         decompression_handling_manager,
         [&](const_node_ptr& node) -> bool {
+            if (ov::is_type<ov::op::v8::Gather>(node)) {

Good point, thanks. Done in a455707f: the PreLpt callback is back to its original behavior, and runLptPasses now registers ConvertGatherToGatherCompressed (u8/i8 only) via lowPrecPass->add_main, with a callback that only rejects GatherCompressed nodes failing has_blocked_decompression_params. Since it is added to the same GraphRewrite after GatherTransformation, it only converts the Gathers LPT declined, before LPT's final ConstantFolding. The new tests still fail without this registration and pass with it.

JasonOA888 JasonOA888 commented on code on Nov 25, 2026 06:53 UTC
Code Review
src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/disable_gathercompressed_quantized_model.cpp:1
@@ -63,7 +81,13 @@ class DisableGatherCompressedForQuantizedModel : public testing::WithParamInterf
         auto weights_shape = Shape{64, 64};
         auto weights = utils::make_constant(weight_prec, weights_shape, utils::InputGenerateData(-1, 2, 32768));
         auto convert = std::make_shared<op::v0::Convert>(weights, element::f32);
-        auto multiply = std::make_shared<op::v1::Multiply>(convert, op::v0::Constant::create(type, {1, 1}, {0.625}));
+        const auto scale_const_shape = scale_shape == GatherScaleShape::PER_ROW      ? Shape{weights_shape[0], 1}

Replaced it with a small get_scale_shape helper next to operator<< in a455707f.

JasonOA888 JasonOA888 commented on code on Nov 25, 2026 06:53 UTC
Code Review
src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/disable_gathercompressed_quantized_model.cpp:1
@@ -98,6 +123,17 @@ class DisableGatherCompressedForQuantizedModel : public testing::WithParamInterf
             }
         }
 
+        if (scale_shape == GatherScaleShape::PER_ROW) {

Added in a455707f, with comments on who dequantizes:

  • per-row: GatherCompressed dequantizes, so the test checks that it still takes the i8/u8 weights and that MatMul gets f32 weights and runs in f32;

  • per-column: LPT moves the dequantization after Gather, so the test checks that Gather outputs i8/u8. The scale is along the MatMul reduction axis, so LPT keeps the Multiply before MatMul and MatMul runs in f32, which the test checks too.
  • v-Golubev v-Golubev commented on Nov 25, 2026 09:44 UTC
    Contributor

    build_jenkins

    v-Golubev v-Golubev commented on code on Nov 25, 2026 11:00 UTC
    Code Review
    src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/disable_gathercompressed_quantized_model.cpp:1
    @@ -98,6 +135,31 @@ class DisableGatherCompressedForQuantizedModel : public testing::WithParamInterf
                 }
             }
     
    +        const auto matmul_runtime_precision =
    +            matmul->get_rt_info().at(ov::exec_model_info::RUNTIME_PRECISION).as<ov::element::Type>();
    +        if (scale_shape == GatherScaleShape::PER_ROW) {
    +            // LPT can't move a per-row scale through Gather with non-constant indices, so GatherCompressed
    +            // dequantizes the compressed weights itself and MatMul gets f32 weights.
    +            EXPECT_TRUE(have_gather_compressed);
    +            EXPECT_FALSE(have_gather);
    +            ASSERT_NE(gather, nullptr);
    +            EXPECT_EQ(gather->get_input_element_type(0), compressed_weights_precision);
    +            EXPECT_EQ(matmul->get_input_element_type(1), element::f32);

    It looks like the f32 expectations depend on the platform: the CPU plugin uses bf16 by default on bf16-capable hosts, so the matmul input and runtime precision can be bf16 on SPR. Let's relax these checks and verify that the precisions are real (is_real()). This comment is actual for the same checks in the PER_COLUMN branch.

    v-Golubev v-Golubev commented on code on Nov 25, 2026 11:00 UTC
    Code Review
    src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/disable_gathercompressed_quantized_model.cpp:1
    @@ -28,23 +28,53 @@ namespace test {
      *              \       /
      *               \     /
      *               MatMul
    + *
    + * With a per-row weights scale and non-constant indices, LPT can't move the dequantization through Gather,
    + * so GatherCompressed must be kept for i8/u8 weights as well. A per-column scale is still handled by LPT.
      */
    -using DisableGatherCompressedForQuantizedModelParams = std::tuple<element::Type, InputShape, InputShape>;
    +enum class GatherScaleShape { SCALAR, PER_ROW, PER_COLUMN };
    +
    +static std::ostream& operator<<(std::ostream& os, GatherScaleShape scale_shape) {
    +    switch (scale_shape) {
    +    case GatherScaleShape::SCALAR:
    +        return os << "scalar";
    +    case GatherScaleShape::PER_ROW:
    +        return os << "per_row";
    +    case GatherScaleShape::PER_COLUMN:
    +        return os << "per_column";
    +    }
    +    return os;
    +}
    +
    +static Shape get_scale_shape(GatherScaleShape scale_shape, const Shape& weights_shape) {
    +    switch (scale_shape) {
    +    case GatherScaleShape::PER_ROW:
    +        return {weights_shape[0], 1};
    +    case GatherScaleShape::PER_COLUMN:
    +        return {1, weights_shape[1]};
    +    case GatherScaleShape::SCALAR:
    +        return {1, 1};
    +    }
    +    return {1, 1};

    Minor: this silently returns a shape for a value the switch was not written for; let's throw instead. This comment is actual for return os; in operator<< above.

        OPENVINO_THROW("Unexpected GatherScaleShape");
    

    same for operator<<

    v-Golubev v-Golubev commented on code on Nov 25, 2026 11:00 UTC
    Code Review
    src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/disable_gathercompressed_quantized_model.cpp:1
    @@ -98,6 +135,31 @@ class DisableGatherCompressedForQuantizedModel : public testing::WithParamInterf
                 }
             }
     
    +        const auto matmul_runtime_precision =
    +            matmul->get_rt_info().at(ov::exec_model_info::RUNTIME_PRECISION).as<ov::element::Type>();

    Minor: let's reuse matmul_runtime_precision in the i8 case below instead of the second get_rt_info() lookup.

    v-Golubev v-Golubev commented on code on Nov 25, 2026 11:09 UTC
    Code Review
    src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/disable_gathercompressed_quantized_model.cpp:1
    @@ -98,6 +135,31 @@ class DisableGatherCompressedForQuantizedModel : public testing::WithParamInterf
                 }
             }
     
    +        const auto matmul_runtime_precision =
    +            matmul->get_rt_info().at(ov::exec_model_info::RUNTIME_PRECISION).as<ov::element::Type>();
    +        if (scale_shape == GatherScaleShape::PER_ROW) {

    Can we avoid the early returns? Let's fold the new expectations into the switch (compressed_weights_precision) below: for i8/u8 the PER_ROW case expects GatherCompressed with compressed weights and a real matmul precision, PER_COLUMN expects Gather with compressed output, and SCALAR keeps the existing checks. The u4/i4 cases stay as they are, since they are instantiated with SCALAR only.

    v-Golubev v-Golubev commented on code on Nov 25, 2026 11:22 UTC
    Code Review
    src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp:1
    @@ -990,6 +1022,17 @@ void Transformations::runLptPasses(const std::vector<ov::element::Type>& default
     #if defined(OPENVINO_ARCH_ARM) || defined(OPENVINO_ARCH_ARM64)
         lowPrecPass->add_markup<AlignUnsupportedLPConvFQPrecision>();
     #endif
    +    // Gathers that GatherTransformation can't handle (e.g. a per-row scale with non-constant indices) are converted to
    +    // GatherCompressed here, otherwise the final ConstantFolding of LPT would decompress the u8/i8 weights.
    +    lowPrecPass->add_main<ov::pass::ConvertGatherToGatherCompressed>(
    +        std::vector<ov::element::Type>{ov::element::u8, ov::element::i8});
    +    CPU_SET_CALLBACK_COMMON(
    +        lptManager,
    +        [](const_node_ptr& node) -> bool {
    +            // GatherCompressed expects each scale / zero point value to cover a contiguous block of weights
    +            return ov::is_type<ov::op::internal::GatherCompressed>(node) && !has_blocked_decompression_params(node);

    Can we move this check into ConvertGatherToGatherCompressed instead of a CPU callback? It looks like a limitation of the op rather than of the CPU kernel: the GPU kernel derives the scale index the same way (flat_idx / (dictionary size / scale size) in gather_ref.cl), so a [1, D] scale gives wrong results there too, and the GPU pipeline registers the pass without any guard. A check in the pass would cover all devices and the non-LPT and u4/i4 cases mentioned in the PR description.

    Please note: the condition has to accept grouped scales after the 3D→2D reshape (e.g. weights [32, 16] and scale [32, 4], as in ConvertGatherToCompressed3). It looks like has_blocked_decompression_params returns false for them now: the scale shape should be the leading weights dims, then one dim that divides the weights dim, then ones.

    Please also add negative TransformationTestsF instances for ConvertGatherToGatherCompressed, covering these corner cases

    JasonOA888 JasonOA888 commented on code on Nov 25, 2026 14:19 UTC
    Code Review
    src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/disable_gathercompressed_quantized_model.cpp:1
    @@ -98,6 +135,31 @@ class DisableGatherCompressedForQuantizedModel : public testing::WithParamInterf
                 }
             }
     
    +        const auto matmul_runtime_precision =
    +            matmul->get_rt_info().at(ov::exec_model_info::RUNTIME_PRECISION).as<ov::element::Type>();
    +        if (scale_shape == GatherScaleShape::PER_ROW) {
    +            // LPT can't move a per-row scale through Gather with non-constant indices, so GatherCompressed
    +            // dequantizes the compressed weights itself and MatMul gets f32 weights.
    +            EXPECT_TRUE(have_gather_compressed);
    +            EXPECT_FALSE(have_gather);
    +            ASSERT_NE(gather, nullptr);
    +            EXPECT_EQ(gather->get_input_element_type(0), compressed_weights_precision);
    +            EXPECT_EQ(matmul->get_input_element_type(1), element::f32);

    Good catch, thanks. Both the per-row and the per-column cases now only check that the MatMul input and runtime precisions are real (is_real()), so bf16 on SPR is accepted. Ran the tests natively and under SDE -spr.

    JasonOA888 JasonOA888 commented on code on Nov 25, 2026 14:19 UTC
    Code Review
    src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/disable_gathercompressed_quantized_model.cpp:1
    @@ -28,23 +28,53 @@ namespace test {
      *              \       /
      *               \     /
      *               MatMul
    + *
    + * With a per-row weights scale and non-constant indices, LPT can't move the dequantization through Gather,
    + * so GatherCompressed must be kept for i8/u8 weights as well. A per-column scale is still handled by LPT.
      */
    -using DisableGatherCompressedForQuantizedModelParams = std::tuple<element::Type, InputShape, InputShape>;
    +enum class GatherScaleShape { SCALAR, PER_ROW, PER_COLUMN };
    +
    +static std::ostream& operator<<(std::ostream& os, GatherScaleShape scale_shape) {
    +    switch (scale_shape) {
    +    case GatherScaleShape::SCALAR:
    +        return os << "scalar";
    +    case GatherScaleShape::PER_ROW:
    +        return os << "per_row";
    +    case GatherScaleShape::PER_COLUMN:
    +        return os << "per_column";
    +    }
    +    return os;
    +}
    +
    +static Shape get_scale_shape(GatherScaleShape scale_shape, const Shape& weights_shape) {
    +    switch (scale_shape) {
    +    case GatherScaleShape::PER_ROW:
    +        return {weights_shape[0], 1};
    +    case GatherScaleShape::PER_COLUMN:
    +        return {1, weights_shape[1]};
    +    case GatherScaleShape::SCALAR:
    +        return {1, 1};
    +    }
    +    return {1, 1};

    Done: both get_scale_shape and operator<< now end with OPENVINO_THROW("Unexpected GatherScaleShape").

    JasonOA888 JasonOA888 commented on code on Nov 25, 2026 14:20 UTC
    Code Review
    src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/disable_gathercompressed_quantized_model.cpp:1
    @@ -98,6 +135,31 @@ class DisableGatherCompressedForQuantizedModel : public testing::WithParamInterf
                 }
             }
     
    +        const auto matmul_runtime_precision =
    +            matmul->get_rt_info().at(ov::exec_model_info::RUNTIME_PRECISION).as<ov::element::Type>();

    Done: the i8 case reuses matmul_runtime_precision.

    JasonOA888 JasonOA888 commented on code on Nov 25, 2026 14:20 UTC
    Code Review
    src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/disable_gathercompressed_quantized_model.cpp:1
    @@ -98,6 +135,31 @@ class DisableGatherCompressedForQuantizedModel : public testing::WithParamInterf
                 }
             }
     
    +        const auto matmul_runtime_precision =
    +            matmul->get_rt_info().at(ov::exec_model_info::RUNTIME_PRECISION).as<ov::element::Type>();
    +        if (scale_shape == GatherScaleShape::PER_ROW) {

    Done: the early returns are gone. The expectations are now in the switch (compressed_weights_precision): for i8/u8, PER_ROW expects GatherCompressed on the compressed weights with a real MatMul precision, PER_COLUMN expects a plain Gather with compressed output, and SCALAR keeps the existing checks. u4/i4 are unchanged.

    JasonOA888 JasonOA888 commented on code on Nov 25, 2026 14:20 UTC
    Code Review
    src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp:1
    @@ -990,6 +1022,17 @@ void Transformations::runLptPasses(const std::vector<ov::element::Type>& default
     #if defined(OPENVINO_ARCH_ARM) || defined(OPENVINO_ARCH_ARM64)
         lowPrecPass->add_markup<AlignUnsupportedLPConvFQPrecision>();
     #endif
    +    // Gathers that GatherTransformation can't handle (e.g. a per-row scale with non-constant indices) are converted to
    +    // GatherCompressed here, otherwise the final ConstantFolding of LPT would decompress the u8/i8 weights.
    +    lowPrecPass->add_main<ov::pass::ConvertGatherToGatherCompressed>(
    +        std::vector<ov::element::Type>{ov::element::u8, ov::element::i8});
    +    CPU_SET_CALLBACK_COMMON(
    +        lptManager,
    +        [](const_node_ptr& node) -> bool {
    +            // GatherCompressed expects each scale / zero point value to cover a contiguous block of weights
    +            return ov::is_type<ov::op::internal::GatherCompressed>(node) && !has_blocked_decompression_params(node);

    Agreed, it's a limitation of the op. The check is now in ConvertGatherToGatherCompressed itself, and the CPU callback and has_blocked_decompression_params are removed. The pass only matches a scale / zero point whose shape is the leading weights dims, then one dim that divides the next weights dim, then ones. It runs on the original (pre-reshape) shapes, so grouped scales such as [32, 4, 1] for [32, 4, 4] weights (ConvertGatherToCompressed3) are still converted.

    New negative TransformationTestsF cases cover a per-column [1, 16] scale (u8 and u4), a per-column zero point, and a [1, 4, 1] scale on [32, 4, 4] weights. All existing ConvertGatherToCompressed* tests still pass, and so do the CPU GatherWeightsDecompression / GatherCompressed / DisableGatherCompressed tests. I also updated the PR description: the non-LPT and u4/i4 case it listed as not addressed is now handled.

    v-Golubev v-Golubev commented on code on Nov 25, 2026 15:22 UTC
    Code Review
    src/common/transformations/src/transformations/op_conversions/convert_gather_to_compressed.cpp:1
    @@ -29,6 +30,33 @@ using ov::pass::pattern::op::Or;
     namespace v0 = ov::op::v0;
     namespace v1 = ov::op::v1;
     namespace v8 = ov::op::v8;
    +
    +namespace {
    +// GatherCompressed computes the scale / zero point index of a weights element as
    +// flat_idx / (weights_size / param_size), so each value has to cover a contiguous block of the flattened weights:
    +// the parameter shape must be the leading weights dims, then one dim that divides the next weights dim, then ones.
    +bool is_blocked_decompression_param(const ov::Shape& weights_shape, const ov::PartialShape& param_shape) {
    +    if (param_shape.is_dynamic() || param_shape.size() > weights_shape.size()) {
    +        return false;
    +    }
    +    auto shape = param_shape.to_shape();
    +    shape.insert(shape.begin(), weights_shape.size() - shape.size(), 1);
    +    size_t i = 0;
    +    while (i < shape.size() && shape[i] == weights_shape[i]) {
    +        ++i;
    +    }
    +    if (i == shape.size()) {
    +        return true;
    +    }
    +    if (shape[i] == 0 || weights_shape[i] % shape[i] != 0) {

    Can we cover the branch where the scale dim divides the weights dim but is not equal to it? None of the tests I see reaches it: [32, 4, 1] for [32, 4, 4] stops at a dim of 1. E.g. a positive case with [32, 16] weights and a [32, 4] scale, and a negative one with a [32, 3] scale.

    v-Golubev v-Golubev commented on code on Nov 25, 2026 15:22 UTC
    Code Review
    src/common/transformations/tests/op_conversions/convert_gather_to_compressed_test.cpp:1
    @@ -153,6 +153,52 @@ TEST_F(TransformationTestsF, ConvertGatherToCompressed4) {
         }
     }
     
    +namespace {
    +// GatherCompressed requires each scale / zero point value to cover a contiguous block of the flattened weights,
    +// so the decompression subgraph must be kept as is for the other layouts.
    +std::shared_ptr<ov::Model> make_gather_decompression_model(const ov::element::Type& weights_type,
    +                                                           const ov::Shape& weights_shape,
    +                                                           const ov::Shape& scale_shape,
    +                                                           const ov::Shape& zp_shape = {}) {
    +    auto input1 = std::make_shared<v0::Parameter>(ov::element::i32, ov::PartialShape{-1, 16});
    +    auto axis_const = v0::Constant::create(ov::element::i32, ov::Shape{1}, {1});
    +    auto weights_const = v0::Constant::create(weights_type, weights_shape, {1});
    +    std::shared_ptr<ov::Node> decompression = std::make_shared<v0::Convert>(weights_const, ov::element::f32);
    +    if (!zp_shape.empty()) {
    +        auto zp_const = v0::Constant::create(ov::element::f32, zp_shape, {1});
    +        decompression = std::make_shared<v1::Subtract>(decompression, zp_const);
    +    }
    +    auto scale_const = v0::Constant::create(ov::element::f32, scale_shape, {1});
    +    decompression = std::make_shared<v1::Multiply>(decompression, scale_const);
    +    if (weights_shape.size() == 3) {
    +        auto reshape_const = v0::Constant::create(ov::element::i32, ov::Shape{2}, {-1, 16});
    +        decompression = std::make_shared<v1::Reshape>(decompression, reshape_const, false);
    +    }
    +    auto gather = std::make_shared<v8::Gather>(decompression, input1, axis_const);
    +    return std::make_shared<ov::Model>(ov::OutputVector{gather}, ov::ParameterVector{input1});
    +}
    +}  // namespace
    +
    +TEST_F(TransformationTestsF, ConvertGatherToCompressed_Negative_PerColumnScale) {

    Minor: these four tests differ only by the arguments of make_gather_decompression_model; let's parametrize them.

    v-Golubev v-Golubev commented on code on Nov 25, 2026 16:18 UTC
    Code Review
    src/common/transformations/src/transformations/op_conversions/convert_gather_to_compressed.cpp:1
    @@ -72,6 +100,12 @@ ov::pass::ConvertGatherToGatherCompressed::ConvertGatherToGatherCompressed(
                 return false;
             }
     
    +        if (!is_blocked_decompression_param(dicts_shape, pattern_map.at(mul_const_m).get_partial_shape()) ||

    sub_const_m and mul_const_m are any_input(), so a dynamic scale / zero point reaches the callback and is only rejected by the helper. Let's express it in the pattern: any_input(has_static_shape()) for both (with using ov::pass::pattern::has_static_shape;). Then the dynamic-shape branch of the helper is not needed and the callback can use get_shape() here.

    v-Golubev v-Golubev commented on code on Nov 25, 2026 16:20 UTC
    Code Review
    src/common/transformations/src/transformations/op_conversions/convert_gather_to_compressed.cpp:1
    @@ -29,6 +30,33 @@ using ov::pass::pattern::op::Or;
     namespace v0 = ov::op::v0;
     namespace v1 = ov::op::v1;
     namespace v8 = ov::op::v8;
    +
    +namespace {
    +// GatherCompressed computes the scale / zero point index of a weights element as
    +// flat_idx / (weights_size / param_size), so each value has to cover a contiguous block of the flattened weights:
    +// the parameter shape must be the leading weights dims, then one dim that divides the next weights dim, then ones.
    +bool is_blocked_decompression_param(const ov::Shape& weights_shape, const ov::PartialShape& param_shape) {

    Can we simplify this check with std::mismatch? It finds the first differing dim directly, so the index loop and the i == shape.size() special case are not needed. Smth like this (the helper takes ov::Shape, see the comment on the call site below):

    bool is_blocked_decompression_param(const ov::Shape& weights_shape, ov::Shape param_shape) {
        if (param_shape.size() > weights_shape.size()) {
            return false;
        }
        param_shape.insert(param_shape.begin(), weights_shape.size() - param_shape.size(), 1);
        const auto [param_it, weights_it] = std::mismatch(param_shape.begin(), param_shape.end(), weights_shape.begin());
        // Either the shapes are equal (one value per weights element), or the first differing dim splits the weights dim
        // into equal groups and the following dims are ones (one value per group of contiguous elements).
        return param_it == param_shape.end() ||
               (*param_it != 0 && *weights_it % *param_it == 0 &&
                std::all_of(param_it + 1, param_shape.end(), [](size_t dim) {
                    return dim == 1;
                }));
    }
    

    JasonOA888 JasonOA888 commented on code on Nov 25, 2026 20:12 UTC
    Code Review
    src/common/transformations/src/transformations/op_conversions/convert_gather_to_compressed.cpp:1
    @@ -29,6 +30,33 @@ using ov::pass::pattern::op::Or;
     namespace v0 = ov::op::v0;
     namespace v1 = ov::op::v1;
     namespace v8 = ov::op::v8;
    +
    +namespace {
    +// GatherCompressed computes the scale / zero point index of a weights element as
    +// flat_idx / (weights_size / param_size), so each value has to cover a contiguous block of the flattened weights:
    +// the parameter shape must be the leading weights dims, then one dim that divides the next weights dim, then ones.
    +bool is_blocked_decompression_param(const ov::Shape& weights_shape, const ov::PartialShape& param_shape) {
    +    if (param_shape.is_dynamic() || param_shape.size() > weights_shape.size()) {
    +        return false;
    +    }
    +    auto shape = param_shape.to_shape();
    +    shape.insert(shape.begin(), weights_shape.size() - shape.size(), 1);
    +    size_t i = 0;
    +    while (i < shape.size() && shape[i] == weights_shape[i]) {
    +        ++i;
    +    }
    +    if (i == shape.size()) {
    +        return true;
    +    }
    +    if (shape[i] == 0 || weights_shape[i] % shape[i] != 0) {

    Good point. Looking into it, that branch can't be reached. The check runs on the original (pre-reshape) scale / zero point shapes, which are Multiply / Subtract operands of the weights. Broadcasting makes each of their dims either 1 or equal to the weights dim, so a [32, 4] or [32, 3] scale for [32, 16] weights fails Multiply validation (Argument shapes are inconsistent). The grouped [32, 4] layout only appears after the 3D->2D reshape, i.e. [32, 4, 1] on [32, 4, 4], which ConvertGatherToCompressed3 covers. So I dropped the divisibility branch: after the first mismatching dim, every remaining dim has to be 1. I also added a negative case where the scale broadcasts the weights ([32, 4] scale on [32, 1] weights), which is the remaining way a dim can differ.

    JasonOA888 JasonOA888 commented on code on Nov 25, 2026 20:12 UTC
    Code Review
    src/common/transformations/tests/op_conversions/convert_gather_to_compressed_test.cpp:1
    @@ -153,6 +153,52 @@ TEST_F(TransformationTestsF, ConvertGatherToCompressed4) {
         }
     }
     
    +namespace {
    +// GatherCompressed requires each scale / zero point value to cover a contiguous block of the flattened weights,
    +// so the decompression subgraph must be kept as is for the other layouts.
    +std::shared_ptr<ov::Model> make_gather_decompression_model(const ov::element::Type& weights_type,
    +                                                           const ov::Shape& weights_shape,
    +                                                           const ov::Shape& scale_shape,
    +                                                           const ov::Shape& zp_shape = {}) {
    +    auto input1 = std::make_shared<v0::Parameter>(ov::element::i32, ov::PartialShape{-1, 16});
    +    auto axis_const = v0::Constant::create(ov::element::i32, ov::Shape{1}, {1});
    +    auto weights_const = v0::Constant::create(weights_type, weights_shape, {1});
    +    std::shared_ptr<ov::Node> decompression = std::make_shared<v0::Convert>(weights_const, ov::element::f32);
    +    if (!zp_shape.empty()) {
    +        auto zp_const = v0::Constant::create(ov::element::f32, zp_shape, {1});
    +        decompression = std::make_shared<v1::Subtract>(decompression, zp_const);
    +    }
    +    auto scale_const = v0::Constant::create(ov::element::f32, scale_shape, {1});
    +    decompression = std::make_shared<v1::Multiply>(decompression, scale_const);
    +    if (weights_shape.size() == 3) {
    +        auto reshape_const = v0::Constant::create(ov::element::i32, ov::Shape{2}, {-1, 16});
    +        decompression = std::make_shared<v1::Reshape>(decompression, reshape_const, false);
    +    }
    +    auto gather = std::make_shared<v8::Gather>(decompression, input1, axis_const);
    +    return std::make_shared<ov::Model>(ov::OutputVector{gather}, ov::ParameterVector{input1});
    +}
    +}  // namespace
    +
    +TEST_F(TransformationTestsF, ConvertGatherToCompressed_Negative_PerColumnScale) {

    Done: they're now ConvertGatherToCompressedNegativeTests, a TEST_P on TransformationTestsF instantiated with the weights type and the weights / scale / zero point shapes, plus the broadcasting case above.

    JasonOA888 JasonOA888 commented on code on Nov 25, 2026 20:12 UTC
    Code Review
    src/common/transformations/src/transformations/op_conversions/convert_gather_to_compressed.cpp:1
    @@ -72,6 +100,12 @@ ov::pass::ConvertGatherToGatherCompressed::ConvertGatherToGatherCompressed(
                 return false;
             }
     
    +        if (!is_blocked_decompression_param(dicts_shape, pattern_map.at(mul_const_m).get_partial_shape()) ||

    Done: sub_const_m and mul_const_m are now any_input(has_static_shape()), the helper takes an ov::Shape, and the call site uses get_shape().

    JasonOA888 JasonOA888 commented on code on Nov 25, 2026 20:12 UTC
    Code Review
    src/common/transformations/src/transformations/op_conversions/convert_gather_to_compressed.cpp:1
    @@ -29,6 +30,33 @@ using ov::pass::pattern::op::Or;
     namespace v0 = ov::op::v0;
     namespace v1 = ov::op::v1;
     namespace v8 = ov::op::v8;
    +
    +namespace {
    +// GatherCompressed computes the scale / zero point index of a weights element as
    +// flat_idx / (weights_size / param_size), so each value has to cover a contiguous block of the flattened weights:
    +// the parameter shape must be the leading weights dims, then one dim that divides the next weights dim, then ones.
    +bool is_blocked_decompression_param(const ov::Shape& weights_shape, const ov::PartialShape& param_shape) {

    Done, thanks for the snippet. The helper now uses std::mismatch, followed by the all-ones check described above.

    v-Golubev v-Golubev commented on Nov 26, 2026 09:14 UTC
    Contributor

    build_jenkins

    v-Golubev v-Golubev commented on code on Nov 26, 2026 09:56 UTC
    Code Review
    src/common/transformations/src/transformations/op_conversions/convert_gather_to_compressed.cpp:1
    @@ -29,6 +31,24 @@ using ov::pass::pattern::op::Or;
     namespace v0 = ov::op::v0;
     namespace v1 = ov::op::v1;
     namespace v8 = ov::op::v8;
    +
    +namespace {
    +// GatherCompressed computes the scale / zero point index of a weights element as
    +// flat_idx / (weights_size / param_size), so each value has to cover a contiguous block of the flattened weights.
    +// Multiply broadcasting makes every scale / zero point dim either 1 or equal to the weights dim, so the parameter
    +// shape has to be the leading weights dims followed by ones.

    Minor: the dim can also be greater than a weights dim of 1 (the new broadcast test case), and Subtract broadcasts the zero point as well.

    // Multiply / Subtract broadcasting makes every scale / zero point dim 1, equal to the weights dim or (for a weights dim
    // of 1) greater than it, so the parameter shape has to be the leading weights dims followed by ones.
    

    v-Golubev v-Golubev approved these changes on Nov 26, 2026 09:58 UTC
    Approved Reviewer

    Great job 👍

    v-Golubev v-Golubev commented on Nov 26, 2026 12:03 UTC
    Contributor

    build_jenkins

    v-Golubev v-Golubev commented on Nov 30, 2026 10:11 UTC
    Contributor

    @JasonOA888 thanks a lot for the fix!

    Reviewers
    Assignees
    JasonOA888 JasonOA888
    Labels
    risk: low confidence: 87.4% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 44.3% (MEDIUM) · Higher = larger blast radius
    44.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 87.4% · Lower = rushed or shallow review
    87.4%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 5.6% · Safe to merge
    5.6%