Skip to content

fix(allspark): add the linear bias in the FP32 epilogue - #3794

Merged
Qubitium merged 3 commits into
mainfrom
fix/allspark-bias-epilogue
Nov 12, 2026
Merged

Qubitium merged 3 commits into
mainfrom
fix/allspark-bias-epilogue

Conversation

@ZX-ModelCloud

@ZX-ModelCloud ZX-ModelCloud commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

TL;DR

AllSparkLinear and AllSparkAwqLinear narrowed the W8A16 GEMM result to FP16/BF16 and only then added the checkpoint bias. A finite result could therefore become inf. The bias now enters the kernel and is added to the FP32 accumulator before the single output cast, on both the CUDA (Ampere+) and RDNA4 (gfx1200/gfx1201) paths.

18 files, +882 / -62 against current main (including reviewer fixes and the merge of #3792). No packed layout, repack byte, or checkpoint change.

1. The problem

Deterministic repro: 4096 unit weights (GPTQ 8-bit code 255, scale 1/127), activations 16, bias -32768. The product is 65536; the correct result is 32768.

flowchart LR
    A["W8A16 GEMM: FP32 accumulators"] --> B["cast to FP16/BF16: 65536 becomes inf"]
    B --> C["module adds bias: inf + (-32768) = inf"]
    D["expected: 32768"] -.-> E["lost"]
    classDef failure fill:#fce8e6,stroke:#a33,color:#111
    class B,C,E failure
Loading

Measured before the fix on both device families (AllSparkLinear.forward, and the same shape through AllSparkAwqLinear): output was inf on every element; after the fix it is 32768.0.

Mechanisms being removed, with origin/main (f7e416346) evidence:

Mechanism Location on main
Bias added to the already-narrowed module output (GPTQ) gptqmodel/nn_modules/qlinear/allspark.py:207-210
Bias added to the already-narrowed module output (AWQ) gptqmodel/nn_modules/qlinear/allspark_awq.py:261-264
HIP epilogue casts the FP32 accumulator directly gptqmodel_ext/allspark_hip/allspark_hip.cu:296-323
HIP split-K reduce casts the FP32 sum directly gptqmodel_ext/allspark_hip/allspark_hip.cu:322
CUDA MMA stg narrows C_frag directly gptqmodel_ext/allspark/allspark_qgemm_w8a16.cu:453
CUDA split-K reduce casts the FP32 sum directly gptqmodel_ext/allspark/allspark_utils.cuh:74
No bias argument in either op schema allspark_torch.cpp, allspark_torch_hip.cpp

2. The new flow

flowchart TD
    A["AllSparkLinear.forward / AllSparkAwqLinear.forward"] --> B["allspark_gemm / allspark_hip_gemm with bias"]
    B --> C{"_prepare_bias: dtype fp16, bf16 or fp32 in module dtype?"}
    C -- "mismatched 16-bit" --> C2["widen losslessly to FP32"]
    C -- "match" --> D{"m within the native M limit"}
    C2 --> D
    D -- "No: HIP prefill M greater than 16" --> P["allspark_hip_unpack + torch.addmm / FP32 product"]
    D -- "No: CUDA M greater than CUBLAS_M_THRESHOLD" --> Q["dequant + cublasGemmEx into FP32 buffer"]
    Q --> R["add_bias_cast_kernel"]
    D -- "Yes" --> E{"split-K"}
    E -- "Yes" --> F["FP32 slice partials, unbiased"]
    F --> G["splitk_reduce_kernel / f16_gemm_splitk_reduce: sum + bias"]
    E -- "No" --> H["FP32 C_frag / c0 c1 epilogue: add bias per column"]
    G --> I["cast once to the activation dtype"]
    H --> I
    R --> I
    P --> I
    classDef failure fill:#fce8e6,stroke:#a33,color:#111
    J["rejected: wrong shape, device, or dtype"] --> K["ValueError / RuntimeError before launch"]
    class J,K failure
Loading

bias is an optional trailing Tensor? bias=None on both schemas, so every existing caller and benchmark keeps working unchanged. The bias buffer is read either as the activation dtype or as FP32 (bias32). Split-K applies the bias exactly once in the reduce; the diagnostic C32 / allspark_hip_gemm_f32 output keeps plain accumulators so the numerics oracles stay comparable.

3. Per-subsystem detail

  • HIP native epilogue (allspark_hip.cu:277-327): per-column bias loaded once, added to c0[j]/c1[j] before the cast.
  • HIP split-K (allspark_hip.cu:335-352): the reduce kernel adds the bias to the FP32 sum; it is not applied to the partials.
  • CUDA fused stg (allspark_qgemm_w8a16.cu:452-476): bias added to C_frag before the FType packing.
  • CUDA unfused split-K (allspark_utils.cuh): bias added in f16_gemm_splitk_reduce_kernel.
  • CUDA cuBLAS fallback (allspark_qgemm_w8a16.cu:900-966): biased calls request an FP32 cublasGemmEx result and add_bias_cast_kernel applies the bias before the cast; unbiased calls keep the previous direct path.
  • Module/loader: AllSparkLinear keeps an FP32 bias buffer (allspark.py:153-161), AllSparkAwqLinear keeps the source width (allspark_awq.py:183-189), and gptqmodel/models/loader.py:2366-2374 keeps that buffer FP32 through an FP16/BF16 checkpoint cast, mirroring GGUF/FP8/VPTQ.

4. Failure and ambiguity contract

Situation Before After
FP32-range bias with an FP16 product of 65536 inf 32768
Bias stored in FP32, activation FP16/BF16 module narrowed it first added in the FP32 epilogue
Bias shape is not one element per output channel broadcast shape error deep in the call explicit rejection in the helper and both bindings
Bias on another device / unsupported dtype device or dtype error at kernel launch explicit rejection before launch
No bias unchanged unchanged (bias=None)
Split-K with a bias bias applied after the reduce, on the narrowed tensor bias applied once inside the FP32 reduce

5. Change inventory

Area File +/- Purpose
HIP kernel gptqmodel_ext/allspark_hip/allspark_hip.cu +39/-7 epilogue and split-K bias
HIP params gptqmodel_ext/allspark_hip/allspark_utils_hip.cuh +5/-0 C_bias_ptr, C_bias_f32
HIP binding gptqmodel_ext/allspark_hip/allspark_torch_hip.cpp +30/-7 Tensor? bias=None schema, validation
CUDA kernel gptqmodel_ext/allspark/allspark_qgemm_w8a16.cu +97/-16 stg bias, cuBLAS FP32 path
CUDA params/helpers gptqmodel_ext/allspark/allspark_utils.cuh +24/-4 bias fields and reduce bias
CUDA binding gptqmodel_ext/allspark/allspark_torch.cpp +16/-4 Tensor? bias=None schema, validation
Python helpers gptqmodel/utils/allspark.py, allspark_hip.py +64/-3 _prepare_bias, fallback bias handling
Modules allspark.py, allspark_awq.py +32/-18 pass bias into the GEMM, drop the post-add
Loader gptqmodel/models/loader.py +5/-2 keep the bias FP32 on FP16/BF16 load
Docs docs/allspark-bias-epilogue.md, README.md +79/-0 reference page and link
Tests 5 tests/test_allspark_*.py files +438/-0 see below

6. Tests / guarantees

Test Invariant
test_allspark_jit.py::test_allspark_bias_added_before_output_rounding (m=1/32/64/1025) 65536 + (-32768) is exactly 32768 for FP16 and BF16, with an activation-dtype and an FP32 bias, across MMA, split-K, and the cuBLAS fallback
test_allspark_jit.py::test_allspark_bias_matches_fp64_oracle ordinary random biases match an independent FP64 oracle on every path
test_allspark_jit.py::test_allspark_bias_rejected_for_wrong_shape_and_device shape, device, and dtype rejection; a cross 16-bit bias is widened, not misread
test_allspark_hip.py::test_bias_* same cancellation, oracle, rejection, and storage checks on the HIP kernel and its prefill fallback
test_allspark_linear.py::test_linear_bias_* module-level ordering and no persistent weight expansion
test_allspark_linear.py::test_bias_survives_checkpoint_reload_at_fp32_width the loader keeps an FP32 bias across an FP16/BF16 cast
test_allspark_awq*.py::test_awq_module_bias_* the AWQ wrapper (both device paths) has the same ordering and keeps only packed buffers

7. Validation

Target hardware and runtimes were as reported in the AllSpark kernel tests; only the last two AMD devices were used. Commands:

python -m pytest tests/test_allspark_hip.py tests/test_allspark_awq.py tests/test_allspark_linear.py \
                 tests/test_allspark_jit.py tests/test_allspark_cuda_graph.py tests/test_allspark_awq_cuda.py \
                 tests/test_rocm_kernel_contracts.py
python -m pytest tests/test_allspark_jit.py tests/test_allspark_cuda_graph.py tests/test_allspark_awq_cuda.py \
                 tests/test_allspark_linear.py tests/test_rocm_kernel_contracts.py
python -m pytest tests/module_tree/test_fusion_kernel_backends.py

Results: 587 passed / 81 skipped on the AMD path; 480 passed on the NVIDIA path; 89 passed / 101 skipped for the fusion backend contract. tests/test_gguf_bias_range.py and tests/test_fp8_packed_weight_lifecycle.py still pass (38 passed) after the loader change.

Numerics: outputs match the independent FP64 oracle rounded once, within one output-dtype ULP; measured maximum absolute drift is 0.04-0.6 depending on dtype and shape, i.e. normal BF16/FP16 output rounding.

Timing (4096x4096, M=1/4/16, median of 20 CUDA-event calls, five warmups, no flush):

Device dtype M no bias with bias delta
NVIDIA FP16 1 / 4 / 16 0.021 / 0.020 / 0.022 ms 0.020 / 0.020 / 0.023 ms -1.9% / +1.3% / +4.8%
NVIDIA BF16 1 / 4 / 16 0.020 / 0.019 / 0.021 ms 0.020 / 0.020 / 0.022 ms +5.0% / +5.6% / +4.6%
AMD FP16 1 / 4 / 16 0.035 / 0.035 / 0.047 ms 0.035 / 0.035 / 0.047 ms -0.2% / +0.3% / -0.1%
AMD BF16 1 / 4 / 16 0.035 / 0.035 / 0.049 ms 0.035 / 0.036 / 0.054 ms -0.1% / +0.9% / +9.8%

Storage: no new persistent tensors; the bias is the existing per-channel buffer, added in the epilogue. The M > CUBLAS_M_THRESHOLD fallback allocates an FP32 result for the current call only, in addition to the workspace it already allocated.

CUDA graph capture still replays with a bias (checked for FP16 and BF16 at M=8).

Reviewer follow-up

Review found that a normal module .to(torch.float16) or .to(torch.bfloat16)
would still narrow an FP32 checkpoint bias. Both AllSpark modules now preserve
its FP32 storage during dtype/device moves. The regression tests use an FP32
-100000 bias, which would otherwise become -inf, and exercise the resulting
finite output. The CodeQL dead assignment in the AWQ CUDA test was also removed.

After merging #3792 into this branch and resolving the shared test additions,
the combined CUDA run passed 103 tests in test_allspark_jit.py,
test_allspark_awq_cuda.py, test_allspark_linear.py, and
test_allspark_cuda_graph.py; 12 AllSpark CUDA fusion tests passed. Ruff and
git diff --check passed. AMD validation in the original section was performed
before this integration and has not been rerun by the reviewer.

8. Reviewer notes

AllSparkLinear and AllSparkAwqLinear ran the W8A16 GEMM, narrowed its FP32
accumulators to the activation dtype, and only then added the bias. With 4096
unit weights and 16-valued activations the product is 65536; that is already
`inf` as FP16, so a finite -32768 bias could not restore the correct 32768.
Both device paths reproduced it (NVIDIA Ada and RDNA4 gfx1201).

The optional bias now enters the kernel and is added to the FP32 accumulator
before the single output cast:

- HIP: added in the native epilogue and in `splitk_reduce_kernel`; the
  `M > 16` prefill fallback uses `torch.addmm`, or an FP32 product when the
  stored bias is wider than the activation dtype.
- CUDA: added to `C_frag` in `stg` and in `f16_gemm_splitk_reduce`; the
  `M > CUBLAS_M_THRESHOLD` cuBLAS fallback writes an FP32 result and
  `add_bias_cast_kernel` applies the bias before casting.
- Split-K partials stay unbiased and the bias is applied once, in the reduce.
  The diagnostic `C32`/`gemm_f32` output keeps plain accumulators.

The op schemas gain `Tensor? bias=None`, so existing callers are unchanged and
no packed layout or checkpoint byte changes. The module keeps an FP32 bias
buffer, and the loader preserves it across an FP16/BF16 checkpoint cast.

Tests pin the cancellation case and ordinary random biases on every execution
path for FP16 and BF16, the shape/device/dtype rejection paths, the reload
width, and that repeated forwards retain no expanded weight. Reference page:
docs/allspark-bias-epilogue.md.

Signed-off-by: ZX-ModelCloud <[email protected]>
Comment thread tests/test_allspark_awq_cuda.py Fixed
# Conflicts:
#	tests/test_allspark_awq.py
#	tests/test_allspark_awq_cuda.py
#	tests/test_allspark_linear.py

Copy link
Copy Markdown
Collaborator

Review: the bias reaches the FP32 epilogue on the CUDA and HIP native and split-K paths, and the CUDA cuBLAS fallback keeps a wide temporary until bias addition. I reproduced a missed module-level case: .to(float16) narrowed an FP32 -100000 bias to -inf. I pushed the FP32-preserving _apply fix for both GPTQ and AWQ, expanded the cancellation tests, and removed the dead assignment flagged by CodeQL. I also merged #3792 into this branch and resolved the shared test additions. Verified on RTX 4090: 103 combined AllSpark CUDA tests and 12 CUDA fusion tests passed; Ruff, git diff --check, and all current PR checks passed. The original HIP validation is documented in the PR; this reviewer environment has no ROCm runtime. No remaining blockers found.

@Qubitium
Qubitium marked this pull request as ready for review November 12, 2026 05:33
@Qubitium
Qubitium merged commit bab2358 into main Nov 12, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/allspark-bias-epilogue branch November 12, 2026 05:33
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