Repository navigation
perf(sinq): decode the dense fallback in the activation dtype - #6632
Merged
Merged
Conversation
The scaled dense fallback always decoded and folded in FP32, even when the scaled weight fits the module's activation dtype. Pick the decode dtype from the scaled range answer and reuse the shared dense epilogue, so a fitting weight decodes and folds in the activation dtype while an overflowing one keeps the FP32/FP64 wide reduction. This is the remaining non-fused path after #6619; the fused GEMV, GEMM and streaming branches are unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #6619. The non-fused dense fallback for the 1D uniform SINQ
path still decoded and folded the weight in FP32 on every forward, even when
the scaled weight fits the module's activation dtype. #6619 routes the fused
GEMV, small-M GEMM and streaming paths through the activation dtype but leaves
this fallback on FP32.
This change picks the dense decode dtype from the existing scaled range
answer: the activation dtype when
max|scales| * maxq * max|sinq_scale|fitsit, FP32 when it does not, and FP64 for a pathological scale. The fold runs
in the same dtype, and the shared dense epilogue handles the wide overflow,
CPU half and general cases. The fused paths are unchanged.
Net diff: 4 files, +209/−48.
What Changed
gptqmodel/nn_modules/qlinear/torch.py_forward_eager'sinput_scale is not Nonebranch selects the decodedtype from
_decoded_weight_fits(which SINQ overrides with the scaledbound), decodes and folds there, then reuses the existing dense epilogue.
linear_wide_biaswith the FP32/FP64 weight.tests/test_sinq.py,tests/test_sinq_gpu.py: dense-fallback decode-dtyperegressions plus overflow controls.
docs/sinq.md: decode-path diagram and decision table.Old and New Flow
Old flow: the scaled dense branch always decoded in FP32 and folded in FP32.
flowchart TD A["_forward_eager: input_scale present (torch.py:810)"] --> B["dequantize_weight(dtype=float32)"] B --> C["fold sinq_scale in FP32"] C --> D["linear_wide_bias: FP32 reduction, one rounding"]New flow:
flowchart TD A["_forward_eager: input_scale present"] --> B{"_decoded_weight_fits activation dtype?"} B -- yes --> C["decode and fold in activation dtype"] B -- no --> D{"_decoded_weight_fits FP32?"} D -- yes --> E["decode and fold in FP32"] D -- no --> F["decode and fold in FP64"] C --> G{"weight dtype != activation dtype?"} E --> G F --> G G -- yes --> H["linear_wide_bias: wide reduction, one rounding"] G -- no --> I{"CPU half with bias?"} I -- yes --> J["addmm epilogue"] I -- no --> K["matmul_fp32_reduce: FP32 reduction, one rounding"] style F fill:#fff3cd,stroke:#b58900linear_wide_biasonlylinear_wide_biason overflow, CPU addmm, ormatmul_fp32_reduceThe same diagram and table are in
docs/sinq.md, linked fromREADME.md.Tests
Commands (one device per backend):
Red/green evidence (new tests against
5d0365a8fin a disposable worktree):tests/test_sinq.py::test_sinq_dense_fallback_decodes_in_activation_dtypeassert [torch.float32] == [torch.bfloat16]tests/test_sinq.py::test_sinq_dense_fallback_keeps_wide_decode_when_scaled_weight_overflowstests/test_sinq_gpu.py::test_sinq_device_dense_fallback_decodes_in_activation_dtype[bf16/fp16]assert [torch.float32] == [torch.bfloat16]and[torch.float16]tests/test_sinq_gpu.py::test_sinq_device_dense_fallback_keeps_wide_decode_on_scaled_overflowThe dense tests compare against an FP64 oracle (
_effective_referenceon GPU,dequantize_weight(dtype=float64)on CPU) withrtol=2e-3and thedtype-aware
_visible_atol. The overflow tests compare against an FP64oracle with the folded weight outside the activation dtype's finite range and
require an FP32 decode.
Measured Impact
Real 4-bit SINQ checkpoint layer (
in_features=896,out_features=4864,FP16, batch 2) with the fused GEMV/GEMM and streaming gates disabled to force
the dense fallback. Median of three runs, 200 iterations each:
The win is bounded by the fallback's weight traffic; the fused paths that
carry the model-level decode are unchanged. This is a consistency and small
fallback win, not a second model-level speedup.
Model Support / Module Plan
Not applicable: no model, alias, definition, port, or module-plan change.
AWQ onboarding safety
Not applicable: no AWQ grouping, search, replay, or fold change.
Logic Flow Documentation
file:lineevidence.docs/sinq.mdrepeats the diagram and table and is linked fromREADME.md.Review Requirements
_forward_decode_dtype/_decoded_weight_fitshooks are reused.input_scale is not Nonedense branch changes.Kernel Accuracy Requirements
and ROCm devices (dense fallback and overflow control).
Notes / Limits
TorchLinear._forward_eager'sinput_scale is not Nonebranch changes;subclasses without an input-channel scale are unaffected.
decode in the activation dtype when every decoded weight fits.
_decoded_weight_fitsalready caches the range answerand
clear_weight_cacheinvalidates it.