Skip to content

fix(exl3): refresh deferred runtime weights per layer forward and align generic geometry (#4006) - #4026

Merged
Qubitium merged 4 commits into
mainfrom
fix/issue-4006
Nov 14, 2026
Merged

Qubitium merged 4 commits into
mainfrom
fix/issue-4006

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4006

TL;DR

EXL3 now refreshes deferred dense replay weights for the whole layer before every subset capture forward and before _replay_layer_outputs, instead of only for the current subset. The generic path also zero-pads output columns to a 128 multiple and rejects non-128-aligned input features in preprocess, before calibration, with a named ValueError. The two repo recovery tests that fail on main now pass on CUDA.

1. The problem

EXL3Processor._stage_runtime_weight (gptqmodel/looper/exllamav3_processor.py:2388) replaces a packed projection's dense weight with a META placeholder. The layer forward still executes every module, but only the current subset was prepared, so a projection packed by an earlier subset reached the next capture forward (and the post-process replay) as META.

flowchart TD
    A["subset 0 capture forward"] --> B["subset 0 process: stage META weight"]
    B --> C["subset 1 capture forward"]
    C --> D["_prepare_named_module_for_forward for subset 1 only"]
    D --> E["layer forward executes subset 0 projections"]
    E --> F["META weight reaches HookedLinear.forward"]
    F --> G["NotImplementedError: Cannot copy out of meta tensor; no data"]
    style G fill:#ffd6d6,stroke:#b00020,stroke-width:2px
Loading

Before/after repro on the issue's command (RTX 4090, CUDA):

$ cd tests && CUDA_VISIBLE_DEVICES=GPU-d458708b-c760-bbf1-08fb-8b8803a2157d \
    PYTHONPATH=$PWD/.. python -m pytest -q \
    "test_checkpoint_method_compatibility.py::test_specialized_checkpoint_recovery[term-exl3]"
E   failed to move parameter 'weight' of HookedLinear to cuda:0
E   NotImplementedError: Cannot copy out of meta tensor; no data!
1 failed

test_moe_method_checkpoint_recovery[term-exl3] failed the same way.

2. The new flow

prepare_layer_runtime_weights_for_forward (gptqmodel/looper/loop_processor.py:87) walks the complete layer inventory and offers every NamedModule to each processor's prepare_runtime_weight_for_forward hook. The hook materializes the deferred weight on the forward device and clears its marker, so a repeat call is a no-op.

flowchart TD
    A["run_subset_stage: subset n of N"] --> B["_run_single_subset_pass"]
    B --> C["_prepare_layer_direct_state_for_forward"]
    C --> D["prepare_layer_runtime_weights_for_forward"]
    D --> E["prepare_runtime_weight_for_forward for every NamedModule in full"]
    E --> F{"deferred marker present?"}
    F -- "yes" --> G["reconstruct_exl3_tensors on forward device"]
    G --> H["_restore_module_weight crops padded output columns"]
    H --> I["clear exl3_deferred_runtime_weight marker"]
    F -- "no" --> J["return None, leave module unchanged"]
    I --> K["_run_forward_batches for the whole layer"]
    J --> K
    K --> L["process subset n and stage its META placeholder"]
    L --> M["_replay_layer_outputs"]
    M --> D
    M --> N["prepare_layer_post_quantize: terminal layer retires META to sentinel"]
    N --> O["submodule_finalize installs ExllamaV3Linear"]
Loading

Call sites:

Call site File
subset capture forward gptqmodel/looper/stage_subset.py:946
post-process / catch-up replay gptqmodel/looper/stage_layer.py:321

3. Geometry contract

EXL3 applies a 128-wide signed Hadamard transform to the weight and the captured Hessian, and the runtime reconstructs in 128-wide output blocks.

Dimension Rule Action
in_features positive multiple of 128 reject in preprocess and quantize_exl3 before capture
out_features any positive value zero-pad stored columns to ceil(out/128)*128; ExllamaV3Linear.forward crops
weight layout (in_features, stored_out_features) built by _quant_input_weight

Input channels are not padded: ExllamaV3Linear._stored_features requires trellis.shape[0] * 16 == in_features and there is no activation-padding boundary. This is why the generic path fails closed for unaligned input widths instead of pretending to support them. Output padding is fully supported because the runtime already crops padded stored features (tests/test_kimi_k3_core_format.py).

flowchart TD
    A["EXL3Processor.preprocess"] --> B["validate_exl3_quantization_geometry"]
    B --> C{"in_features % 128 == 0?"}
    C -- "no" --> D["ValueError with module name and in_features"]
    style D fill:#ffd6d6,stroke:#b00020,stroke-width:2px
    C -- "yes" --> E["build capture task"]
    E --> F["_quant_input_weight pads output columns"]
    F --> G["quantize_exl3 validates the same geometry again"]
    G --> H["packed trellis stores padded outputs"]
Loading

hidden=192, intermediate=320 now fails before calibration:

$ CUDA_VISIBLE_DEVICES=GPU-d458708b-... python /tmp/check_192.py
RAISED ValueError: EXL3 projection `model.layers.0.self_attn.q_proj` has in_features=192,
which is not a multiple of 128; EXL3 requires 128-aligned input features
(pad the model width or exclude the projection).

Direct quantize_exl3 on (144,208), (48,32), (16,16) now raises the same clear ValueError instead of a bare AssertionError.

4. Failure and ambiguity contract

Situation Before After
Earlier subset packed, next subset forward runs META tensor reaches forward whole layer refreshed first
Deferred weight during replay prepared only if a device override touched it refreshed before _run_forward_batches
in_features not 128-aligned bare AssertionError after calibration named ValueError from preprocess
out_features not 128-aligned bare AssertionError in the quantizer zero-padded stored columns, cropped at forward
No deferred marker unchanged unchanged, hook returns None

5. Change inventory

Area File +/- Purpose
geometry gptqmodel/exllamav3/modules/quant/exl3_lib/quantize.py +57 128-alignment helpers and quantize_exl3 validation
processor gptqmodel/looper/exllamav3_processor.py +45/-6 early validation, output padding, padded-output crop
layer refresh gptqmodel/looper/loop_processor.py +43 prepare_layer_runtime_weights_for_forward
subset gptqmodel/looper/stage_subset.py +10/-4 refresh before subset forward
replay gptqmodel/looper/stage_layer.py +10 refresh before replay
tests tests/test_exl3_generic_geometry.py +227 8 regressions
docs docs/exl3-runtime-weights.md, README.md new, +5 lifecycle and geometry reference

6. Tests / guarantees

Test Invariant
test_quant_input_weight_pads_output_columns_to_128 (320,128) weight exports as (128,384) with a zero tail
test_restore_module_weight_crops_padded_output_columns padded reconstruction is cropped back to (320,128)
test_preprocess_rejects_unaligned_input_features_before_capture in_features=192 raises before any task exists
test_direct_quantize_exl3_rejects_unaligned_geometry (144,208), (48,32), (16,16) raise ValueError
test_layer_runtime_weight_preparation_covers_every_named_module every NamedModule is offered to the hook
test_replay_prepares_deferred_weights_for_modules_outside_last_subset replay materializes a module packed by an earlier subset
test_specialized_checkpoint_recovery[term-exl3] repo recovery path completes and saves
test_moe_method_checkpoint_recovery[term-exl3] same for the MoE family

Fail-before evidence (stashed fix, same commands): all 8 new tests failed and both repo tests failed with Cannot copy out of meta tensor. After the fix: 10 passed on CUDA.

7. Validation

Hardware: RTX 4090 (Ada, GPU-d458708b-c760-bbf1-08fb-8b8803a2157d, nvidia-smi index 3) and AMD R9700 (HIP_VISIBLE_DEVICES=2). The RTX 5090 was never used.

CUDA (/opt/venvs/cu130t):

$ cd tests && CUDA_VISIBLE_DEVICES=GPU-d458708b-c760-bbf1-08fb-8b8803a2157d \
    PYTHONPATH=/root/stableabi/gptqmodel-exl3 python -m pytest -q \
    test_exl3_generic_geometry.py \
    "test_checkpoint_method_compatibility.py::test_specialized_checkpoint_recovery[term-exl3]" \
    "test_checkpoint_method_compatibility.py::test_moe_method_checkpoint_recovery[term-exl3]"
10 passed

ruff check gptqmodel docs/eora tests setup.py -> All checks passed!

Reload identity on the 2-layer Llama fixture: 14 ExllamaV3Linear modules, 0 bitwise mismatches, logits bitwise equal between the in-memory and reloaded model. Logits vs the FP16 native model on this random fixture: relative Frobenius error 0.222, top-1 agreement 0.719 at bits=3 (0.113 / 0.844 at bits=4). The issue's ~0.048 / ~95% figures are checkpoint-specific and were not reproduced with the repo's randomly initialized fixture; the structural bitwise claim is reproduced exactly.

ROCm (/opt/venvs/rocm101t, HIP_VISIBLE_DEVICES=2): the 8 new tests pass. The two repo recovery tests remain blocked by pre-existing ROCm issues, not by this change:

  1. test_moe_method_checkpoint_recovery[term-exl3] hits torch.device("rocm") in the routed-expert Hessian-owner branch (exllamav3_processor.py:2188). The same failure occurs on unmodified origin/main (b12cf3f).
  2. test_specialized_checkpoint_recovery[term-exl3] now gets past the META crash but fails assert_identical_tensors on a *.trellis key. Two independent baseline quantizations of the same native model on ROCm already produce 11 differing trellis keys, so the bitwise resume assertion is blocked by pre-existing ROCm pipeline nondeterminism; on main the test crashes before this is observable.

8. Reviewer notes

  • The dense replay weight is materialized when the next layer forward needs it and lives until submodule_finalize installs the packed module. That is the existing lifecycle for the post-process replay; the change only makes earlier subsets follow it too.
  • No environment-variable switches, no dense inference weight caches, no main push, no force-push.
  • Unaligned input widths stay unsupported by design; padding them would require an activation boundary in ExllamaV3Linear, which this fix intentionally does not add.

@Qubitium
Qubitium merged commit 9c8d61b into main Nov 14, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4006 branch November 14, 2026 16:00
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