Repository navigation
fix(allspark): add the linear bias in the FP32 epilogue - #3794
Conversation
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]>
# Conflicts: # tests/test_allspark_awq.py # tests/test_allspark_awq_cuda.py # tests/test_allspark_linear.py
|
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: |
TL;DR
AllSparkLinearandAllSparkAwqLinearnarrowed the W8A16 GEMM result to FP16/BF16 and only then added the checkpoint bias. A finite result could therefore becomeinf. 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 is65536; the correct result is32768.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 failureMeasured before the fix on both device families (
AllSparkLinear.forward, and the same shape throughAllSparkAwqLinear): output wasinfon every element; after the fix it is32768.0.Mechanisms being removed, with
origin/main(f7e416346) evidence:gptqmodel/nn_modules/qlinear/allspark.py:207-210gptqmodel/nn_modules/qlinear/allspark_awq.py:261-264gptqmodel_ext/allspark_hip/allspark_hip.cu:296-323gptqmodel_ext/allspark_hip/allspark_hip.cu:322stgnarrowsC_fragdirectlygptqmodel_ext/allspark/allspark_qgemm_w8a16.cu:453gptqmodel_ext/allspark/allspark_utils.cuh:74allspark_torch.cpp,allspark_torch_hip.cpp2. 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 failurebiasis an optional trailingTensor? bias=Noneon 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 diagnosticC32/allspark_hip_gemm_f32output keeps plain accumulators so the numerics oracles stay comparable.3. Per-subsystem detail
allspark_hip.cu:277-327): per-column bias loaded once, added toc0[j]/c1[j]before the cast.allspark_hip.cu:335-352): the reduce kernel adds the bias to the FP32 sum; it is not applied to the partials.stg(allspark_qgemm_w8a16.cu:452-476): bias added toC_fragbefore the FType packing.allspark_utils.cuh): bias added inf16_gemm_splitk_reduce_kernel.allspark_qgemm_w8a16.cu:900-966): biased calls request an FP32cublasGemmExresult andadd_bias_cast_kernelapplies the bias before the cast; unbiased calls keep the previous direct path.AllSparkLinearkeeps an FP32 bias buffer (allspark.py:153-161),AllSparkAwqLinearkeeps the source width (allspark_awq.py:183-189), andgptqmodel/models/loader.py:2366-2374keeps that buffer FP32 through an FP16/BF16 checkpoint cast, mirroring GGUF/FP8/VPTQ.4. Failure and ambiguity contract
inf32768bias=None)5. Change inventory
gptqmodel_ext/allspark_hip/allspark_hip.cugptqmodel_ext/allspark_hip/allspark_utils_hip.cuhC_bias_ptr,C_bias_f32gptqmodel_ext/allspark_hip/allspark_torch_hip.cppTensor? bias=Noneschema, validationgptqmodel_ext/allspark/allspark_qgemm_w8a16.custgbias, cuBLAS FP32 pathgptqmodel_ext/allspark/allspark_utils.cuhgptqmodel_ext/allspark/allspark_torch.cppTensor? bias=Noneschema, validationgptqmodel/utils/allspark.py,allspark_hip.py_prepare_bias, fallback bias handlingallspark.py,allspark_awq.pygptqmodel/models/loader.pydocs/allspark-bias-epilogue.md,README.mdtests/test_allspark_*.pyfiles6. Tests / guarantees
test_allspark_jit.py::test_allspark_bias_added_before_output_rounding(m=1/32/64/1025)test_allspark_jit.py::test_allspark_bias_matches_fp64_oracletest_allspark_jit.py::test_allspark_bias_rejected_for_wrong_shape_and_devicetest_allspark_hip.py::test_bias_*test_allspark_linear.py::test_linear_bias_*test_allspark_linear.py::test_bias_survives_checkpoint_reload_at_fp32_widthtest_allspark_awq*.py::test_awq_module_bias_*7. Validation
Target hardware and runtimes were as reported in the AllSpark kernel tests; only the last two AMD devices were used. Commands:
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.pyandtests/test_fp8_packed_weight_lifecycle.pystill 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):
Storage: no new persistent tensors; the bias is the existing per-channel buffer, added in the epilogue. The
M > CUBLAS_M_THRESHOLDfallback 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
-100000bias, which would otherwise become-inf, and exercise the resultingfinite 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, andtest_allspark_cuda_graph.py; 12 AllSpark CUDA fusion tests passed. Ruff andgit diff --checkpassed. AMD validation in the original section was performedbefore this integration and has not been rerun by the reviewer.
8. Reviewer notes
SplitKtimes.C32/allspark_hip_gemm_f32) intentionally excludes the bias so existing oracles remain meaningful.