|
| 1 | +# EXL3 runtime weight lifecycle |
| 2 | + |
| 3 | +EXL3 packs a projection into trellis/sign tensors and then drops the dense |
| 4 | +source weight. The dense weight is reconstructed only for the layer forward |
| 5 | +that produces the next layer's calibration inputs, and is replaced by the |
| 6 | +packed module at finalization. This page describes that lifecycle, the layer |
| 7 | +wide refresh that keeps it correct across subsets, and the 128-channel geometry |
| 8 | +contract for the generic `EXL3Processor`. The refresh was introduced with |
| 9 | +[PR #4026](https://github.com/ModelCloud/GPTQModel/pull/4026). |
| 10 | + |
| 11 | +## Why the layer inventory is refreshed, not only the subset |
| 12 | + |
| 13 | +One decoder layer is quantized in subsets. After subset `n` is packed, |
| 14 | +`EXL3Processor._stage_runtime_weight` (`gptqmodel/looper/exllamav3_processor.py:2388`) |
| 15 | +replaces that subset's dense weight with a META placeholder and stores a marker |
| 16 | +in `NamedModule.state`. The layer forward that follows still executes every |
| 17 | +module in the layer: earlier subsets are already packed, and later subsets are |
| 18 | +still dense. Therefore the subset that is currently being quantized is not the |
| 19 | +set of modules whose runtime weights must be ready. |
| 20 | + |
| 21 | +`prepare_layer_runtime_weights_for_forward` |
| 22 | +(`gptqmodel/looper/loop_processor.py:87`) walks the complete layer inventory |
| 23 | +and offers every `NamedModule` to each processor's |
| 24 | +`prepare_runtime_weight_for_forward` hook. The hook materializes a deferred |
| 25 | +dense weight directly on the forward device and clears the marker, so a second |
| 26 | +call is a no-op. The two call sites are the subset capture forward |
| 27 | +(`gptqmodel/looper/stage_subset.py:946`) and the post-process replay |
| 28 | +(`gptqmodel/looper/stage_layer.py:321`). |
| 29 | + |
| 30 | +```mermaid |
| 31 | +flowchart TD |
| 32 | + A["run_subset_stage: subset n of N"] --> B["_run_single_subset_pass"] |
| 33 | + B --> C["_prepare_layer_direct_state_for_forward"] |
| 34 | + C --> D["prepare_layer_runtime_weights_for_forward"] |
| 35 | + D --> E["prepare_runtime_weight_for_forward for every NamedModule in full"] |
| 36 | + E --> F{"deferred marker present?"} |
| 37 | + F -- "yes" --> G["reconstruct_exl3_tensors on forward device"] |
| 38 | + G --> H["_restore_module_weight crops padded output columns"] |
| 39 | + H --> I["clear exl3_deferred_runtime_weight marker"] |
| 40 | + F -- "no" --> J["return None, leave module unchanged"] |
| 41 | + I --> K["_run_forward_batches for the whole layer"] |
| 42 | + J --> K |
| 43 | + K --> L["process subset n and stage its META placeholder"] |
| 44 | + L --> M["_replay_layer_outputs"] |
| 45 | + M --> D |
| 46 | + M --> N["prepare_layer_post_quantize: terminal layer retires META to sentinel"] |
| 47 | + N --> O["submodule_finalize installs ExllamaV3Linear"] |
| 48 | +``` |
| 49 | + |
| 50 | +## Before the fix |
| 51 | + |
| 52 | +Only the current subset was prepared before its forward, and the layer-wide |
| 53 | +sweep ran only for a fully skipped layer. A projection packed by an earlier |
| 54 | +subset therefore reached the next forward as a META parameter. |
| 55 | + |
| 56 | +```mermaid |
| 57 | +flowchart TD |
| 58 | + A["subset 0 capture forward"] --> B["subset 0 process: stage META weight"] |
| 59 | + B --> C["subset 1 capture forward"] |
| 60 | + C --> D["_prepare_named_module_for_forward for subset 1 only"] |
| 61 | + D --> E["layer forward executes subset 0 projections"] |
| 62 | + E --> F["META weight reaches HookedLinear.forward"] |
| 63 | + F --> G["NotImplementedError: Cannot copy out of meta tensor; no data"] |
| 64 | + style G fill:#ffd6d6,stroke:#b00020,stroke-width:2px |
| 65 | +``` |
| 66 | + |
| 67 | +| Old mechanism | Location | New mechanism | |
| 68 | +| --- | --- | --- | |
| 69 | +| Prepare only `subset.values()` before a subset forward | `gptqmodel/looper/stage_subset.py` (pre-fix loop over the subset) | `prepare_layer_runtime_weights_for_forward(..., full=full)` before every subset forward | |
| 70 | +| Prepare `full` only when the whole layer is skipped | `gptqmodel/looper/stage_layer.py` (pre-fix skipped-layer branch) | The same sweep runs inside `_replay_layer_outputs` for every replay | |
| 71 | +| Deferred weight is reconstructed without an output crop | `_restore_module_weight` | Padded stored columns are cropped back to the logical `out_features` | |
| 72 | + |
| 73 | +## 128-channel geometry contract |
| 74 | + |
| 75 | +EXL3 applies a 128-wide signed Hadamard transform to the weight and the |
| 76 | +captured Hessian, and the runtime reconstructs in 128-wide output blocks. |
| 77 | +`validate_exl3_quantization_geometry` |
| 78 | +(`gptqmodel/exllamav3/modules/quant/exl3_lib/quantize.py:48`) enforces the |
| 79 | +contract: |
| 80 | + |
| 81 | +| Dimension | Rule | Action | |
| 82 | +| --- | --- | --- | |
| 83 | +| `in_features` | must be a positive multiple of 128 | reject in `EXL3Processor.preprocess` and in `quantize_exl3`, before capture | |
| 84 | +| `out_features` | any positive value | zero-pad stored columns to `ceil(out_features / 128) * 128`; `ExllamaV3Linear.forward` crops the result | |
| 85 | +| weight layout passed to `quantize_exl3` | `(in_features, stored_out_features)` | `_quant_input_weight` builds the padded tensor | |
| 86 | + |
| 87 | +Input channels are not padded. The runtime's `_stored_features` requires |
| 88 | +`trellis.shape[0] * 16 == in_features` and has no activation-padding boundary, |
| 89 | +so a padded input would change the stored module geometry. Failing before |
| 90 | +calibration is the supported behavior for such models; output padding is fully |
| 91 | +supported because the runtime already crops padded stored features. |
| 92 | + |
| 93 | +```mermaid |
| 94 | +flowchart TD |
| 95 | + A["EXL3Processor.preprocess"] --> B["validate_exl3_quantization_geometry"] |
| 96 | + B --> C{"in_features % 128 == 0?"} |
| 97 | + C -- "no" --> D["ValueError with module name and in_features"] |
| 98 | + style D fill:#ffd6d6,stroke:#b00020,stroke-width:2px |
| 99 | + C -- "yes" --> E["build capture task"] |
| 100 | + E --> F["_quant_input_weight pads output columns"] |
| 101 | + F --> G["quantize_exl3 validates the same geometry again"] |
| 102 | + G --> H["packed trellis stores padded outputs"] |
| 103 | +``` |
| 104 | + |
| 105 | +## Failure and ambiguity contract |
| 106 | + |
| 107 | +| Situation | Before | After | |
| 108 | +| --- | --- | --- | |
| 109 | +| Earlier subset packed, next subset forward runs | META tensor reaches the forward | Whole layer is refreshed first | |
| 110 | +| Deferred weight exists during post-process replay | Not prepared unless a device override touched it | Refreshed before `_run_forward_batches` | |
| 111 | +| `in_features` is not 128-aligned | bare `AssertionError` after calibration | `ValueError` from `preprocess` before capture | |
| 112 | +| `out_features` is not 128-aligned | bare `AssertionError` in the quantizer | zero-padded stored columns, cropped at forward | |
| 113 | +| Module has no deferred marker | unchanged | unchanged; the hook returns `None` | |
| 114 | + |
| 115 | +## Not in scope |
| 116 | + |
| 117 | +This page does not add activation padding for non-128-aligned input widths. |
| 118 | +That would require a runtime input boundary and a different stored geometry. |
0 commit comments