Skip to content

fix(paroquant): race-free, plan-invariant and correct fused rotation gradient; round-13 test triage (#4461, #4460, #4462) - #4499

Merged
Qubitium merged 5 commits into
mainfrom
fix/paroquant-race-and-test-triage-4461-4462
Nov 16, 2026
Merged

Qubitium merged 5 commits into
mainfrom
fix/paroquant-race-and-test-triage-4461-4462

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4461
Fixes #4462
Partially fixes #4460. CUDA is now bit-reproducible; the remaining ROCm source is documented below.

ParoQuant fused rotation backward

Race (#4461)

  • Cause: rotate_pair_grad_kernel had its only per-rotation barrier between apply_one and the readback. So rotation r-1's apply_one could overwrite columns that other threads were still reading for rotation r.
  • Fix: the barrier now comes after the readback.
  • Stress repro (2048 features, every cta_m/row_pad combination, 30 calls each):
    • before: 3/12 configs nondeterministic on CUDA (|err| up to 1.7), 4/12 on ROCm (up to 4.2);
    • after: 0/12 in 10/10 processes on both.

Launch-plan invariance (#4460)

  • Cause: the theta gradient was summed per autotuned CTA_M tile, and the autotune is timing-based. Two cold Llama-3.2-1B runs picked cta_m 8 vs 16 and diverged from layer 0 (22.43 vs 21.72 PPL).
  • Fix: partials now use a fixed 4-row granule, so results are bit-identical for every plan. cta_m=4 results are unchanged.
  • Result: cold-cache repeat runs of Llama-3.2-1B and Qwen2.5-0.5B are now bitwise identical on CUDA, even when the autotune plans differ.

Gradient formula

  • Bug: the backward applied A cos θ - B sin θ to the input-side gradient; the correct derivative is A = Σ(g_i x_j - g_j x_i). The unfused (autograd) path was already right.

| |θ| up to | max error vs fp64 autograd |
|---|---|
| 0.05 | 0.7 |
| 0.25 | 7.1 |
| 1.0 | 18.5 |

  • Fix: applied in rotate_pair_grad, in grad_theta (its sin/cos arguments are removed) and in the torch fallback.
  • PPL, mean over 3 seeds, 1+1 epochs:
Model Before After
Qwen2.5-0.5B 22.414 22.193
Llama-3.2-1B 22.274 22.249

Remaining ROCm nondeterminism

_needs_deterministic_hessian keeps hipBLASLt for ParoQuant on ROCm, and hipBLASLt picks GEMM kernels per process. With rocBLAS pinned, two ROCm runs give bitwise-identical checkpoints.

Tests: test_paroquant_pair_grad_sweep_is_race_free, test_paroquant_pair_grad_is_launch_plan_invariant, test_paroquant_fused_rotation_theta_grad_matches_autograd.
Docs: docs/paroquant.md#fused-rotation-backward.

Test triage (#4462)

Results

🤖 Generated with Claude Code

Qubitium and others added 5 commits November 16, 2026 19:57
…ion (#4461)

The fused backward sweep kept one barrier per rotation between apply_one and
the gradient readback. That barrier is not needed (each thread reads back
only the pair it just rotated) and it does not cover the real hazard: the
next rotation's apply_one overwrites columns other threads are still reading
for the current rotation (write after read). The theta gradient was
sporadically corrupted (|err| up to 4.2 on ROCm, 1.7 on CUDA at 2048 features).

Move the single barrier after the readback so it orders both the
cross-rotation read-after-write and write-after-read, and mask the readback
columns the same way apply_one does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…aunch plan (#4460)

rotate_pair_grad wrote one gradient partial per CTA_M-row tile, and CTA_M is
chosen per shape by a timing-based autotune. Two cold runs on the same host
picked different tiles for Llama-3.2-1B's 2048x2048 weight shape (cta_m 8 vs
16), so the fp32 summation order of the theta gradient changed and the
quantized checkpoints diverged from layer 0 (22.43 vs 21.72 PPL).

Sum every CTA's rows in fixed 4-row granules and fold the granules in
ascending order, so the rounding sequence is identical for every
cta_m/row_pad. cta_m=4 results are bit-identical to before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… triage (#4462)

- test_ret_idx_cache_invalidation: _fused_module writes raw buffers through
  setattr, which bypasses the load hook, without declaring their layout.
  Since #4377 (db31a55) an unresolved layout means raw v1 checkpoint words,
  the same contract every product writer follows (loader v1->v2 conversion,
  packing, offload restore all call qzero_format/establish). Declare the v2
  layout the fixture actually writes (logical zero codes, as the dense oracle
  decodes them) with qzero_format(format=2).
- test_offload_run_namespace: the stub processor lacks release_run_workspace,
  which loop() now calls on failure too (a427804).
- test_weight_only_looper: offload_to_disk=True binds a private per-run config
  copy (#4368), so the embed-only overrides are asserted on the run config.
- test_model_test_helpers: the mocked parquet loader ignored rows; hosts with
  the local parquet take that loader's early path for rows > 0.
- test_paroquant: _AFFINE_STE_TRITON_ENABLED was replaced by
  KernelConfig.paroquant_affine_ste_triton (27c6b7b); the AWQ fake must accept
  kernel=; skip the CUDA-only AWQ fast-path test on ROCm.
- test_qqq_kernel_memory_4387: exclude the BLAS library's per-call GEMM
  workspace (8 MiB cuBLAS, 80 MiB hipBLASLt) from QQQ's transient bound;
  QQQ's own transient is 20.82 MiB on both platforms.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…factors

The fused theta backward (rotate_pair_grad, the grad_theta op and the torch
fallback) sweeps both the activation and the incoming gradient back through
each rotation before measuring it, so it holds x_in and g_in = R^T g_out.
With dR/dtheta = R J the gradient is then sum(g_i x_j - g_j x_i), but the
code applied the output-side formula A cos(theta) - B sin(theta) to the
input-side gradient. The result is exact only at theta == 0 and drifts as
the angles train (max error 0.7 / 7.1 / 18.5 against fp64 autograd for
|theta| up to 0.05 / 0.25 / 1.0, on gradients of magnitude ~20). The
non-fused optimizer path uses plain autograd and was already correct, so the
fused (default CUDA/ROCm) and unfused ParoQuant runs optimized different
objectives.

Drop the sin/cos factors in all three paths (grad_theta loses its now-unused
sin_t/cos_t arguments) and test every path, with and without channel
scales, against fp64 autograd of the reference rotation.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Describe the shared-memory ordering (#4461), the launch-plan-invariant
gradient granule (#4460) and the input-side angle gradient, with before/after
file:line evidence and the guarding tests. Link it from the README.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Qubitium
Qubitium merged commit c7ea976 into main Nov 16, 2026
5 checks passed
@Qubitium
Qubitium deleted the fix/paroquant-race-and-test-triage-4461-4462 branch November 16, 2026 19:59
Qubitium added a commit that referenced this pull request Nov 16, 2026
main's #4499 rewrote the fused rotation-gradient partials around fixed
4-row granules; keep that tiling and apply the branch's mutable_data_ptr
accessors to it. main also dropped the unused sin_t/cos_t arguments from
grad_theta_kernel, so the launch follows the current signature.
Qubitium added a commit that referenced this pull request Nov 16, 2026
…bs (#4460) (#4505)

ParoQuant checkpoints are meant to be reproducible with a fixed opt_seed. PR #4499 made CUDA bitwise-identical, but ROCm kept hipBLASLt because ParoQuant accumulates no Hessian. hipBLASLt selects GEMM kernels per process, so identically seeded runs produced different checkpoints (21.26 vs 21.44 PPL). Include ProcessorStep.PARO in the deterministic rocBLAS selection and document the accepted speed-for-reproducibility trade-off.

The fused-rotation autotune timing knobs were read from GPTQMODEL_PAROQUANT_ROTATE_AUTOTUNE_* environment variables in rotation.cu, which AGENTS.md forbids. Replace them with internal constants that preserve the previous defaults; KernelConfig.paroquant_rotate_* remains the only control surface.

Tests: a mocked-ROCm selection test and a source-grep test that no ParoQuant environment reads remain. ROCm numerical determinism cannot be executed on this host (no ROCm device); it is validated structurally and with the mocked platform.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment