Repository navigation
perf(sinq): reuse the Torch fused decode paths - #6619
Merged
Merged
Conversation
Route the uniform affine SinqLinear forward through TorchLinear._forward so ROCm decode uses the shared GPTQ fused GEMV, small-M GEMM and streaming tile decoder instead of reconstructing a dense weight per token. Pass sinq_scale into the kernels: GEMV scales the FP32 activation, GEMM and streaming fold it into the FP32 weight before the single cast. Add the scaled-weight range gate, per-signature dispatch caching, eval mode at post_init, and the codebook/wide fallbacks. Extend the ROCm kernel and SINQ device tests with scaled-kernel oracles and fused-path selection checks.
Qubitium
marked this pull request as ready for review
December 1, 2026 08:41
This was referenced Dec 1, 2026
Qubitium
pushed a commit
that referenced
this pull request
Dec 1, 2026
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
SINQ's uniform affine forward reconstructed a dense FP32 weight on every
token, so decode on ROCm ran 1.85x slower than RTN on the same model and
backend. This change routes the 1D uniform path through
TorchLinear._forwardand passes
sinq_scaleinto the shared GPTQ kernels, so decode reuses thefused GEMV, small-M GEMM and streaming tile decoder with the fused-bias
epilogue. On Qwen2.5-0.5B (4-bit, group 128, FP16, 64 greedy tokens), SINQ
decode is now 0.837-0.877x the RTN wall time instead of 1.848x.
2D tiling (#6614, merged) stores one column scale per output-row block, which
the per-input-channel kernel argument cannot represent, so
tiling_mode="2d"keeps the existing dense wide reduction. NF4/NF3 keep theTorch gather.
Related: #6614 (merged, 2D tiling), #6615 (open, A-SINQ). This PR does not
implement A-SINQ or 2D tiling and does not change their quantizer code. It
does touch
docs/sinq.md,tests/test_sinq.pyandtests/test_sinq_gpu.py,which #6615 also edits.
What Changed
gptqmodel/nn_modules/qlinear/torch.pyTorchLinear._input_channel_scale()hook (defaultNone)._forward_eagerpassesinput_scaletogptq_gemv/gptq_gemm; thedense fallback decodes FP32, folds the scale with
unsqueeze(-1), anduses
linear_wide_bias._forward_streaming/_stream_dequantize_tilefold the scale into theFP32 tile before the activation-dtype cast and record it on the dequant
stream.
gptqmodel/nn_modules/triton_utils/gptq_gemv.pyinput_scale_ptrargument andhas_input_scale: tl.constexpr; thekernel scales the FP32 activation, which is safe for large Sinkhorn
column factors.
gptqmodel/nn_modules/triton_utils/gptq_gemm.pynon-uniform branches before the single activation-dtype cast.
gptqmodel/nn_modules/qlinear/sinq.pypost_initsets eval because the Triton and streaming gates areeval-only paths.
clear_weight_cachealso clears the scaled-range and dispatch caches._input_channel_scalereturnssinq_scaleonly for the 1D uniform path._decoded_weight_fitsadds the scaled-weight boundmax|scales| * maxq * max|sinq_scale| <= finfo(dtype).max._fused_dispatchcaches the GEMV/GEMM decision per activationsignature; codebook and 2D scales return
(False, False)._forwardskips the identity rotation and routes codebook/2D inputs tothe dense wide reduction;
forwardvalidates the input and scale once.parity tests, streaming parity, a 2D dense-path guard, and a scaled-range
overflow test.
scripts/benchmark_sinq_decode.py: parameterized SINQ-vs-RTN decodebenchmark.
docs/sinq.md: inference-dispatch diagram, before/after table, 2Dbehavior, limitations and validation.
No public API changes.
TorchLinearsubclasses that do not override_input_channel_scalekeep the previous behavior because the default isNone, and the new kernel argument is optional and keyword-only.Tests
Exact commands and results on the final rebased tree:
python -m pytest tests/test_sinq.py tests/test_torch_weight_storage.py \ tests/test_pack_tails.py tests/kernels/test_torch_wide_bias_fallback.py \ tests/test_rocm_kernel_contracts.py -q # 915 passed python -m pytest tests/test_sinq_gpu.py -q # 23 passed python -m pytest tests/kernels/test_gptq_rocm_gemv.py \ tests/kernels/test_gptq_rocm_gemm.py -q # 129 passed, 92 skipped ./format/check.sh # All checks passed (ruff 0.14.2)The 92 skips are kernel cases gated to an architecture that is not present on
the test host; they are not counted as validation. No test failed.
New regression tests:
tests/test_sinq.py::test_sinq_scaled_decode_range_gate_rejects_activation_overflowtests/test_sinq_gpu.py::test_sinq_device_rocm_decode_uses_fused_gemvtests/test_sinq_gpu.py::test_sinq_device_rocm_small_m_gemm_and_streamingtests/test_sinq_gpu.py::test_sinq_device_2d_tiling_keeps_dense_wide_pathtests/kernels/test_gptq_rocm_gemv.py::test_gptq_gemv_applies_input_channel_scaletests/kernels/test_gptq_rocm_gemm.py::test_gptq_gemm_applies_input_channel_scaleMeasured Impact
4-bit,
group_size=128, FP16, 64 greedy tokens, median of 5 runs, same modeland backend. The metric is SINQ decode wall time divided by RTN decode wall
time, so lower is better.
The 1.5B before measurement was not collected. The after range covers five
0.5B runs and three 1.5B runs. The microbenchmark compares the fused GEMV
against RTN's decode kernel at the same shape.
Benchmark command:
python scripts/benchmark_sinq_decode.py \ --model Qwen/Qwen2.5-0.5B-Instruct --tokens 64 --repeats 5The before number used the same quantize and greedy-decode loop on the base
revision. The benchmark script is new in this PR, so the base run used an
equivalent local script rather than this file.
Model Support / Module Plan
Not applicable: no model, alias, definition, module-plan or fusion change.
AWQ onboarding safety
Not applicable: no AWQ grouping, search, replay or folding change.
Logic Flow Documentation
included below.
names.
success.
docs/repeats the diagrams, titled for themechanism, and is linked from
README.md.Removed mechanism: the base
SinqLinear.forwardrebuilt the dense effectiveweight and called
linear_wide_biason every forward(
gptqmodel/nn_modules/qlinear/sinq.py, base revision line 519).flowchart LR A["SinqLinear.forward"] --> B["effective_weight: dense FP32 decode per token"] B --> C["multiply by sinq_scale"] C --> D["linear_wide_bias"]New flow:
flowchart TD A["SinqLinear.forward"] --> B{"sinq_scale present and width matches?"} B -- "no" --> E["ValueError"] B -- "yes" --> C{"codebook or 2D scale?"} C -- "nf4/nf3" --> D["effective_weight gather times sinq_scale"] C -- "2d" --> D2["effective_weight: per-block column scale"] D --> W["linear_wide_bias: FP32 reduction, bias, one round"] D2 --> W C -- "uniform 1D" --> F["TorchLinear._forward, identity rotation skipped"] F --> G{"streaming enabled and gate passes?"} G -- "yes" --> H["_forward_streaming: fold sinq_scale into each FP32 tile before the cast"] G -- "no" --> I["_forward_eager"] I --> J{"fused GEMV gate"} J -- "M=1" --> K["gptq_gemv scales the FP32 activation"] J -- "no" --> L{"fused small-M GEMM gate"} L -- "yes" --> M["gptq_gemm folds sinq_scale into the FP32 weight"] L -- "no" --> N["dense FP32 weight times sinq_scale, linear_wide_bias"] K --> O["fused bias epilogue, one output round"] M --> O H --> P["tile addmm and bias, one output round"] N --> PShipped symbols:
SinqLinear.forwardandSinqLinear._forward(
gptqmodel/nn_modules/qlinear/sinq.py:636and:578),TorchLinear._input_channel_scale(gptqmodel/nn_modules/qlinear/torch.py:771),_forward_eager(:781),_forward_streaming(:1030), and the kernelscale argument at
gptqmodel/nn_modules/triton_utils/gptq_gemv.py:155andgptq_gemm.py:269.clear_weight_cacheeval()post_initsets evalFailure paths are explicit: a missing or wrong-length
sinq_scaleraisesValueError/RuntimeError, a scaled weight outside the activation dtype'sfinite range keeps the wide dense reduction, and codebook/2D inputs never
enter the affine fused kernels.
Reference page:
docs/sinq.md, section 5 "Inference dispatch", linked fromREADME.md.Review Requirements
Open the PR as a draft. Mark it ready only after review and after GitHub
confirms the pushed head has no conflicts with the current remote base.
Kernel Accuracy Requirements
applicable independent Torch FP32/FP64 oracle, or the reason this is not
applicable is documented.
FP16/BF16 tests where supported, native shape/workspace/descriptor
checks, mutation, direct source calls, exact restoration and
packed-storage checks.
dependencies, architecture restrictions and unqualified formats; skips
are not validation.
The scaled GEMV and GEMM tests compare against FP64 oracles: the GEMV oracle
applies the multiplier to the activation, and the GEMM oracle applies it to
the decoded weight before the one activation-dtype rounding. No fusion
support declarations changed.
Notes
output-row block. Extending the kernels to per-block scales is a possible
follow-up, not part of this PR.
split path by the measured dtype gate; this PR does not change that gate.
Noneby default. Thehas_input_scaleconstexpr removes the extra load for ordinary GPTQ calls.