[CPU] Keep GatherCompressed for quantized models when LPT can't handle the Gather #40113
build_jenkins
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.
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.
Minor: the nested ternary for the scale shape is hard to read; consider a switch or a small helper next to operator<<.
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).
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.
Replaced it with a small get_scale_shape helper next to operator<< in a455707f.
Added in a455707f, with comments on who dequantizes:
build_jenkins
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.
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<<
Minor: let's reuse matmul_runtime_precision in the i8 case below instead of the second get_rt_info() lookup.
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.
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
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.
Done: both get_scale_shape and operator<< now end with OPENVINO_THROW("Unexpected GatherScaleShape").
Done: the i8 case reuses matmul_runtime_precision.
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.
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.
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.
Minor: these four tests differ only by the arguments of make_gather_decompression_model; let's parametrize them.
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.
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;
}));
}
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.
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.
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().
Done, thanks for the snippet. The helper now uses std::mismatch, followed by the all-ones check described above.
build_jenkins
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.
Great job 👍
build_jenkins
@JasonOA888 thanks a lot for the fix!
v-Golubev
JasonOA888
Details:
- With LPT enabled (any int8 FakeQuantize in the model), the CPU
ConvertGatherToGatherCompressedcallback 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 inLayerTransformation::canBeTransformedStatic). Per-row-scaled embeddings ([V, 1]scale, runtime token ids, as written bynncf.quantize) were therefore left to LPT, rejected, andConstantFoldingexpanded the i8 table to f32 (~4x the table's memory).- The PreLpt callback is unchanged. Instead,
runLptPassesregistersConvertGatherToGatherCompressed(u8/i8) as an additional main LPT pass (lowPrecPass->add_main). It runs in the sameGraphRewriteright afterGatherTransformation, so it only converts the Gathers LPT declined, before LPT's finalConstantFolding; the plugin doesn't need to predict LPT's decision. Scalar and per-column ([1, D], axis 0) scales keep the existing LPT path.-
GatherCompressedimplementations (CPU, and GPU'sgather_ref.cl) compute the scale / zero-point index asflat_idx / (weights_size / param_size), so each value has to cover a contiguous block of the flattened table.ConvertGatherToGatherCompressednow 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
DisableGatherCompressedForQuantizedModelwith per-row (i8/u8,GatherCompressed) and per-column (i8/u8, LPT path, plainGather) scale cases, checking the Gather and MatMul precisions; the per-row cases fail on master. Added a parametrized negativeConvertGatherToGatherCompressedtransformation 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_testson Linux x86_64 on top of current master; the new per-row tests fail on master and pass with the fix; ranov_transformations_testsConvertGatherToCompressed,DisableGatherCompressed,GatherWeightsDecompression,smoke_Gather,smoke_LPTGather,MatmulWeightsDecompressionand the fullsmoke_LPT*suite (15399 passed; the only 2 failures aresmoke_LPT/ConvolutionTransformationcases 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 i8GatherCompressedtable 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).