Skip to content

fix(rotation): persist rotation config and apply online Hadamard for inference and calibration - #2980

Merged
Qubitium merged 3 commits into
mainfrom
devin/cherrypick-rotation
Jul 25, 2026
Merged

fix(rotation): persist rotation config and apply online Hadamard for inference and calibration#2980
Qubitium merged 3 commits into
mainfrom
devin/cherrypick-rotation

Conversation

@Qubitium

@Qubitium Qubitium commented Jul 25, 2026

Copy link
Copy Markdown
Collaborator

Summary

Cherry-pick/adapt the rotation persistence + online-Hadamard work from GPT-QModel-Ultra#66, plus a calibration fix. The original inference-only fix caused a gsm8k_platinum regression because mlp.down_proj weights contained the fused Hadamard H while GPTQ was still calibrating on the raw down_proj input m. This PR makes HookedLinear apply the same online Hadamard during calibration/replay so the GPTQ Hessian matches the inference activation distribution.

What Changed

  • QuantizeConfig emits/round-trips rotation in quantize_config.json.
  • rotate_mlp_output fuses a full exact Hadamard into mlp.down_proj weights and now stores online_full_had/had_K/K on the nn.Linear so the state survives the HookedLinear conversion used during GPTQ.
  • Added apply_online_hadamard(...) in gptqmodel/quantization/rotation/hadamard_utils.py; BaseQuantLinear._apply_rotation_to_input and HookedLinear.forward both call it.
  • HookedLinear.from_linear copies rotation state from the source nn.Linear.
  • TorchLinear._forward and TritonV2Linear.forward call _apply_rotation_to_input before the quantized matmul.
  • BaseGPTQModel.quantize()/from_quantized() attach online-Hadamard state to mlp.down_proj and reject rotation for unsupported backends.
  • BaseQuantizeConfig disables offload_to_disk when rotation is set (rotation needs materialized weights).
  • tests/test_rotation_persistence.py verifies config round-trip, online-state setup, forward routing, Hadamard cancellation, and HookedLinear calibration-path cancellation.
  • Removed an unreachable trailing return x flagged by code-quality review.

Tests

  • I added a new simple/fast unit test for this change.
  • I ran the new targeted test locally.
  • I ran other directly relevant local tests.
cd /root/repos/GPTQModel
PYTHON_GIL=0 pytest -q tests/test_rotation_persistence.py
# 9 passed, 15 warnings in 7.49s

python -m ruff check gptqmodel/quantization/rotation/hadamard_utils.py \
  gptqmodel/nn_modules/hooked_linear.py gptqmodel/nn_modules/qlinear/__init__.py \
  gptqmodel/quantization/rotation/rotation.py tests/test_rotation_persistence.py
# All checks passed!

Full-dataset Evalution on Llama-3.2-1B-Instruct (4-bit GPTQ, rotation='hadamard') against a non-rotated 4-bit baseline and a dense BF16 reference:

checkpoint gsm8k_platinum acc,num arc_challenge acc,exam
dense BF16 0.3821 0.3460
baseline GPTQ (no rotation) 0.3160 0.3528
rotated GPTQ (this fix) 0.3565 0.3514

The rotated model recovers the gsm8k_platinum regression and scores between the dense reference and the non-rotated baseline; arc_challenge is unchanged within noise.

Review Requirements

  • I personally reviewed every file in this diff.
  • I checked that the code matches existing project structure, APIs, and conventions.
  • I avoided unnecessary monkeypatching and used the project's normal extension points where possible.

Notes

The JIT fast_hadamard_transform extension is vendored from Dao-AILab/fast-hadamard-transform (Tri Dao, BSD-3-Clause) under gptqmodel_ext/hadamard/ and only used by the gptq_torch/gptq_triton backends.

Qubitium added 2 commits July 25, 2026 07:15
…inference

Cherry-pick/adapt from GPT-QModel-Ultra#66:
- Persist  in  and round-trip via .
- Fuse exact Hadamard into  weights during rotation.
- Add JIT  Hadamard extension in  (vendored
  from Dao-AILab/fast-hadamard-transform, BSD-3-Clause) plus .
- Attach // to  QuantLinear modules at
   and after . Restrict rotation to /.
- Call  in  and .
- Disable  when  is enabled.
- Add  and README/MANIFEST attribution/license.
@Qubitium Qubitium self-assigned this Jul 25, 2026
@devin-ai-integration

Copy link
Copy Markdown

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

Comment thread gptqmodel/quantization/rotation/hadamard_utils.py Fixed
…down_proj

- Extract apply_online_hadamard so HookedLinear and BaseQuantLinear share the same transform.
- rotate_mlp_output now sets online_full_had/had_K/K on the nn.Linear before it becomes HookedLinear.
- HookedLinear.from_linear copies rotation state and forward applies H to the activation used for GPTQ calibration.
- Add unit test verifying HookedLinear cancellation and hook input transform.
@devin-ai-integration
devin-ai-integration Bot force-pushed the devin/cherrypick-rotation branch from 98de772 to d192083 Compare July 25, 2026 08:03
@devin-ai-integration devin-ai-integration Bot changed the title fix(rotation): persist rotation config and apply online Hadamard for inference fix(rotation): persist rotation config and apply online Hadamard for inference and calibration Jul 25, 2026
@Qubitium
Qubitium merged commit 185a2a4 into main Jul 25, 2026
6 checks passed
@Qubitium
Qubitium deleted the devin/cherrypick-rotation branch July 25, 2026 09:18
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.

1 participant