Skip to content

fix(kernels): read the module kernel snapshot at forward dispatch (#4555) - #4585

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4555-kernel-policy-snapshot
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4555-kernel-policy-snapshot

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4555

TL;DR

Two forward-time dispatch sites read the process-wide KernelConfig instead of
the module's load-time snapshot, so a later GPTQModel.load() /
configure_kernel() silently retuned already-built modules. Both sites now
take the module's own policy (+260/−10 across 4 source files, 1 test file and
one docs section).

1. The problem

flowchart TD
    A["load A: KernelConfig P1"] --> B["BaseQuantLinear.__init__<br/>snapshot_kernel_config"]
    B --> C["module A: self.kernel = P1"]
    C --> D["forward A"]
    D --> E["available_for / paroquant_gemm_triton_*"]
    E --> F["get_kernel_config(): process _ACTIVE_CONFIG"]
    G["load B / configure_kernel(P2)"] --> F
    F --> H["P2 dispatch and fp32_accum, even for A"]
Loading
Site Before
bitsandbytes Triton gate get_kernel_config().bitsandbytes_triton at qlinear/bitsandbytes_triton.py:109; BitsAndBytesLinear.forward (bitsandbytes.py:901) calls should_use(...) without self.kernel
ParoQuant Triton GEMM get_kernel_config().fp32_accum at quantization/paroquant/modules/triton/gemm.py:476,501; ParoQuantTritonLinear._forward_triton_decode/_prefill never forward self.fp32_accum

2. The new flow

flowchart TD
    A["load A: KernelConfig P1"] --> B["BaseQuantLinear.__init__<br/>snapshot_kernel_config"]
    B --> C["module A: self.kernel = P1"]
    C --> D["forward A: pass self.kernel / self.fp32_accum"]
    E["load B / configure_kernel(P2)"] --> F["process _ACTIVE_CONFIG = P2"]
    F --> G["module B: self.kernel = P2"]
    G --> H["forward B: pass P2"]
    D --> I["P1 dispatch, independent of the active process policy"]
Loading
  • bitsandbytes_triton.available_for(..., kernel=None) and
    should_use(..., kernel=None) now read kernel.bitsandbytes_triton;
    BitsAndBytesLinear.forward passes self.kernel.
  • paroquant_gemm_triton_decode/prefill(..., fp32_accum=None) now forward the
    explicit value; ParoQuantTritonLinear passes self.fp32_accum, which is
    resolved from the module snapshot at construction.
  • None still falls back to get_kernel_config() for callers that do not own a
    module, so shared utilities and existing direct callers keep their behavior.

3. Before / after repro

Issue repro 2 (real ParoQuantTritonLinear, RTX 4090, module built with
KernelConfig(fp32_accum=False), then configure_kernel(None)):

Before:

kernel fp32_accum: False self.fp32_accum: False
seen: [True, True]

After:

kernel fp32_accum: False self.fp32_accum: False
seen: [False, False]

For bitsandbytes, the regression builds a module with
bitsandbytes_triton=True, flips the process policy to False, and asserts the
forward still passes the module's self.kernel to should_use; a module built
after the flip passes False. The reverse direction (process forced, module
snapshot off) is also pinned.

4. Tests

Test Invariant
tests/test_kernel_policy_snapshot_4555.py::test_bitsandbytes_triton_gate_uses_explicit_module_kernel the gate reads the explicit module kernel in both directions
...::test_bitsandbytes_forward_passes_the_module_kernel BitsAndBytesLinear.forward passes self.kernel after a policy change
...::test_paroquant_triton_forward_uses_the_module_fp32_accum A keeps P1 fp32_accum after P2 is installed; B uses P2
...::test_paroquant_gemm_helpers_forward_explicit_fp32_accum the GEMM helpers honor an explicit fp32_accum

5. Validation

All runs with OMP_NUM_THREADS=4 MKL_NUM_THREADS=4 OPENBLAS_NUM_THREADS=4 NUMEXPR_NUM_THREADS=4.

# New regression file, CUDA venv (post origin/main merge)
PYTHONPATH=$PWD CUDA_VISIBLE_DEVICES=GPU-d458708b-c760-bbf1-08fb-8b8803a2157d \
  /opt/venvs/cu130t/bin/python -m pytest tests/test_kernel_policy_snapshot_4555.py -q
# 4 passed

# New regression file, ROCm venv
PYTHONPATH=$PWD HIP_VISIBLE_DEVICES=2 \
  /opt/venvs/rocm101t/bin/python -m pytest tests/test_kernel_policy_snapshot_4555.py -q
# 4 passed

# Before the fix the same file fails: 4 failed

# Related ParoQuant files, CUDA venv
... pytest tests/kernels/test_paroquant_ada_launch.py \
           tests/kernels/test_paroquant_bias_epilogue.py \
           tests/test_paroquant_single_rounding_4317.py -q
# 62 passed

# Related bitsandbytes ROCm file
... pytest tests/kernels/test_bitsandbytes_rocm_triton.py -q
# 42 passed, 2 skipped

# Config-isolation file
... pytest tests/test_runtime_config_4448.py -q
# 10 passed

ruff check gptqmodel docs/eora tests setup.py -> All checks passed! (before
and after git merge --no-edit origin/main, main at 052888256).

6. Hardware / limitations

  • CUDA: RTX 4090 GPU-d458708b-c760-bbf1-08fb-8b8803a2157d; ROCm: AMD R9700
    (HIP_VISIBLE_DEVICES=2).
  • No full-checkpoint repro 1 (two real bnb model loads) was run; the bnb
    forward contract is covered by a unit test with a fake weight device and a
    recorder that stops before any packed-weight access. Real kernel numerics are
    not claimed here.
  • tests/test_backend_config_reuse_4446.py has 5 pre-existing failures on
    origin/main (auto.py:953 calls log_kv_cache_estimate() on the test's
    SimpleNamespace); verified on a clean origin/main worktree, not caused by
    this change and not claimed as a regression.
  • docs/kernel-config.md documents the forward-time snapshot contract and the
    regression file.

)

The bitsandbytes Triton gate and the ParoQuant Triton fp32_accum decision
read the process-wide KernelConfig at forward time, so a later load or
configure_kernel() retuned modules that had already been built.

Pass the module's snapshotted policy into both dispatch sites:
BitsAndBytesLinear.forward passes self.kernel to should_use(), and
ParoQuantTritonLinear passes self.fp32_accum to the GEMM helpers. The
helpers keep a process-global fallback when the caller passes None.

Add a regression test that builds A under P1, changes the process policy
to P2, and checks A still dispatches with P1 while B uses P2.
@Qubitium
Qubitium merged commit 34672a7 into main Nov 17, 2026
9 checks passed
@Qubitium
Qubitium deleted the fix/issue-4555-kernel-policy-snapshot branch November 17, 2026 01:59
Qubitium added a commit that referenced this pull request Nov 17, 2026
Fix the test-side fallout of #4716, #4657, #4627 and #4508. Only entries
that fail on this sm_80 host (PG506) and whose stated reasoning reproduces
were changed; the rest are recorded with their outcome.

1. 8587f0b (#4716, native FP8 GEMM for a wide bias)
   - tests/kernels/test_fp8_kernel.py::test_fp8_default_forward_is_w8a16
     REPRODUCED on sm_80: 6 bf16 cases failed with 1-ulp (0.0078125) diffs
     against rtol/atol 2e-3. The oracle now narrows the decoded weight to
     the activation dtype, matching the native path; the bound is the
     half-ulp at unity (2^-9 ~= 2e-3). FIXED.
   - tests/test_fp8_bias_range.py::test_fp8_gpu_dequant_fallback_keeps_bias_wide
     REPRODUCED on sm_80 (fp16: actual 0.0 vs oracle 0.0078125). Renamed to
     test_fp8_gpu_native_gemm_keeps_bias_wide and rewritten for the #3737
     intent: finite output, FP32 bias, residual bounded by one activation-
     dtype ulp of |x.w|. FIXED.

2. bf5adeb (#4657, Marlin bias kept in checkpoint order)
   - tests/test_marlin_scratch_cuda.py 288/200 cases REPRODUCED on sm_80:
     8 failed with "b_bias.size(0) != size_n". scripts/marlin_scratch_fixture.py
     now passes layer._kernel_bias(a) (padded, tile-ordered FP32) instead of
     the checkpoint-order layer.bias. FIXED (8 failed -> 68 passed, 2 skipped).

3. 60376e9 (#4714, default placement in one capability group)
   - tests/test_tenstorrent_support.py already passes on current main: the
     monkeypatched _record accepts **kwargs (fixed upstream by 50f1752).
     ALREADY PASSES on sm_80.

4. 34672a7 (#4585, modules snapshot the kernel policy)
   - tests/kernels/test_bitsandbytes_rocm_triton.py triton cases SKIP on
     sm_80 (no tuned bitsandbytes tile for this family). DEFERRED: the
     stated failure cannot be reproduced here.

5. dfa180d (#4627, GGUF path chosen from static gates)
   - both tests REPRODUCED on sm_80 with AttributeError
     _benchmark_forward_runner. The removed timing hook is no longer patched;
     each test asserts the hook stays absent. FIXED.

6. fd8d524 (#4599, AwqGEMVTorchLinear CPU reader)
   - both kernel-registry contract tests already pass on current main
     (AwqGEMVTorchLinear is in EXCLUDED and the ROCm audit table; fixed
     upstream by ab151fe). ALREADY PASSES on sm_80.

7. d2a0508 (#4283, dense-module adapters)
   - tests/kernels/test_gptq.py::TestKernelOutput errors on sm_80 before the
     adapter path: this host's flash_attn_2_cuda has an undefined symbol, so
     the stated Lora assertion is never reached. DEFERRED: blocked by the
     pre-existing host flash_attn failure; the fix cannot be verified here.

8. f201837 (#4508, activation-quantization wording)
   - both tests REPRODUCED on sm_80. The refusal still fires; the regex now
     matches "activation quantized|activation scales". FIXED.

9. b225a43 (#4727, static-cache compile test)
   - ROCm-only failure. On sm_80 the CUDA leg runs and fails earlier on the
     host flash_attn symbol, so the ROCm gate cannot be reproduced.
     DEFERRED.

Test run (GPU 4):
  tests/kernels/test_fp8_kernel.py tests/test_fp8_bias_range.py
  tests/test_marlin_scratch_cuda.py tests/test_marlin_scratch_benchmark.py
  tests/kernels/test_gguf_cpp_activation_quantization.py
  tests/kernels/test_gguf_cpp_path_selection.py tests/test_auto_module_decoder.py
  => 254 passed, 35 skipped, 1 failed.
The single failure is the pre-existing sm_80
test_fp8_w8a8_forward_is_batch_invariant (native torch._scaled_mm needs
sm_89+); it fails identically on origin/main.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant