Skip to content

perf(sinq): decode the dense fallback in the activation dtype - #6632

Merged
Qubitium merged 1 commit into
mainfrom
perf/sinq-dense-fallback-decode
Dec 1, 2026
Merged

Qubitium merged 1 commit into
mainfrom
perf/sinq-dense-fallback-decode

Conversation

@ZX-ModelCloud

Copy link
Copy Markdown
Collaborator

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| fits
it, 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's input_scale is not None branch selects the decode
      dtype from _decoded_weight_fits (which SINQ overrides with the scaled
      bound), decodes and folds there, then reuses the existing dense epilogue.
    • The overflow case keeps linear_wide_bias with the FP32/FP64 weight.
  • tests/test_sinq.py, tests/test_sinq_gpu.py: dense-fallback decode-dtype
    regressions 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"]
Loading

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:#b58900
Loading
Area Before After
Dense fallback decode FP32 always activation dtype when the scaled weight fits; FP32/FP64 otherwise
Dense fallback fold FP32 same dtype as the decode
Dense fallback epilogue linear_wide_bias only shared epilogue: wide linear_wide_bias on overflow, CPU addmm, or matmul_fp32_reduce
Fused GEMV/GEMM/streaming unchanged unchanged

The same diagram and table are in docs/sinq.md, linked from README.md.

Tests

Commands (one device per backend):

PYTHONPATH=$PWD python -m pytest tests/test_sinq.py -q
# 112 passed

PYTHONPATH=$PWD python -m pytest tests/test_sinq_gpu.py \
    tests/kernels/test_gptq_ada_gemv.py \
    tests/kernels/test_gptq_bf16_fp16_scales_5740.py -q
# CUDA: 77 passed, 4 skipped (ROCm-only fused tests)

PYTHONPATH=$PWD python -m pytest tests/test_sinq_gpu.py \
    tests/kernels/test_gptq_rocm_gemv.py \
    tests/kernels/test_gptq_bf16_fp16_scales_5740.py -q
# ROCm: 72 passed, 29 skipped (gfx950 / Ada device gates only)

PYTHONPATH=$PWD python -m pytest \
    tests/test_gptq_torch_eval_decode_nan_5606.py \
    tests/kernels/test_gptq_ada_dequant.py \
    tests/kernels/test_gptq_ada_gemm.py -q
# 171 passed

PYTHONPATH=$PWD python -m pytest \
    tests/kernels/test_gptq_torch_cpu_bias_epilogue.py \
    tests/kernels/test_torch_wide_bias_fallback.py -q
# 49 passed

bash format/check.sh
# All checks passed

PYTHONPATH=$PWD python -m pytest tests/test_docs_contract_refs.py -q
# 267 passed

Red/green evidence (new tests against 5d0365a8f in a disposable worktree):

Invariant / trigger Test node Red on base Green on head
CPU dense fallback decodes in the activation dtype tests/test_sinq.py::test_sinq_dense_fallback_decodes_in_activation_dtype assert [torch.float32] == [torch.bfloat16] pass
CPU overflow keeps the wide decode tests/test_sinq.py::test_sinq_dense_fallback_keeps_wide_decode_when_scaled_weight_overflows pass (control) pass
GPU dense fallback decodes in the activation dtype tests/test_sinq_gpu.py::test_sinq_device_dense_fallback_decodes_in_activation_dtype[bf16/fp16] assert [torch.float32] == [torch.bfloat16] and [torch.float16] pass
GPU overflow keeps the wide decode tests/test_sinq_gpu.py::test_sinq_device_dense_fallback_keeps_wide_decode_on_scaled_overflow pass (control) pass

The dense tests compare against an FP64 oracle (_effective_reference on GPU,
dequantize_weight(dtype=float64) on CPU) with rtol=2e-3 and the
dtype-aware _visible_atol. The overflow tests compare against an FP64
oracle 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:

Metric Before After Change
Dense fallback forward (ms) 0.0901 0.0863 −4.2%

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

  • New-flow Mermaid uses shipped symbols.
  • The removed mechanism is shown with file:line evidence.
  • The overflow path is marked.
  • docs/sinq.md repeats the diagram and table and is linked from README.md.

Review Requirements

  • Reviewed this PR.
  • Code matches existing project structure, APIs, and conventions.
  • No monkeypatching; the existing _forward_decode_dtype /
    _decoded_weight_fits hooks are reused.
  • Minimized the impacted code surface: only the
    input_scale is not None dense branch changes.
  • Considered and eliminated potential regressions.

Kernel Accuracy Requirements

  • Accuracy measured against an independent FP64 oracle on CPU and CUDA
    and ROCm devices (dense fallback and overflow control).
  • The fused GEMV/GEMM/streaming kernels are unchanged.
  • The overflow case keeps the FP32/FP64 wide reduction.

Notes / Limits

  • Only TorchLinear._forward_eager's input_scale is not None branch changes;
    subclasses without an input-channel scale are unaffected.
  • The dense fallback now follows the same policy as the non-scaled dense path:
    decode in the activation dtype when every decoded weight fits.
  • No new cache is added; _decoded_weight_fits already caches the range answer
    and clear_weight_cache invalidates it.
  • The benchmark is one layer on one device, not a model sweep.

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.
@Qubitium
Qubitium marked this pull request as ready for review December 1, 2026 09:48
@Qubitium
Qubitium merged commit aa5799f into main Dec 1, 2026
6 checks passed
@Qubitium
Qubitium deleted the perf/sinq-dense-fallback-decode branch December 1, 2026 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants