Repository navigation
fix(pangu): materialize non-layer replay modules for the last layer (#5135) - #5169
Merged
Merged
Conversation
…5135) prepare_layer_replay_kwargs replayed ln_f and wqe as if they were resident on the layer device. Under the default lazy/offload load ln_f/wqe are meta placeholders behind an Accelerate SequentialHook, and with offload_to_disk=False wqe stays on CPU while position_ids moves to the target. The last decoder layer therefore always failed after the rest of the model was quantized. Restore each non-layer module through the looper's helpers (undo_offload_to_disk for offload hooks, shell_module_materialize for lazy shells) before move_to, and place wqe on the replay device before the embedding lookup. Resident models keep the previous code path, so replay numerics are unchanged. Adds hook-level regressions for the offloaded, lazy-shell and cross-device cases plus an end-to-end 2-layer PanGu quantize/reload test for CPU-offload, CUDA-offload and CUDA-resident configurations.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #5135.
PanguAlphaQModel.prepare_layer_replay_kwargsreplays PanGu-α's top-queryblock with
ln_fapplied to the layer input andcustom_query = wqe(position_ids).Both modules live outside the quantized decoder-layer tree, so nothing had
materialized them for the last layer's replay. The hook assumed they were
resident on the layer's device:
offload_to_disk=True) leavesln_f/wqeas metaplaceholders behind an Accelerate
SequentialHook.move_to()did notrecognize that hook and raised
NotImplementedErrorat the last layer.offload_to_disk=Falsematerializes them on CPU, but replay runs on thetarget device, so
wqe(position_ids.to(cuda:0))indexed a CPU weight andraised a device-mismatch
RuntimeError.Every earlier layer was already quantized when the last layer crashed.
Root cause
gptqmodel/models/definitions/pangu_alpha.py:91-125(pre-fix) had tworesidency assumptions:
move_to(norm, device=hidden_states.device)only restores a meta module whenaccelerate.utils.has_offloaded_params()sees a directAlignDevicesHook.The repo's
offload_to_disk()installs aSequentialHook, so a legitimatelyoffloaded
ln_flooked like a bare meta module.transformer.wqe(position_ids.to(target_device))never movedwqe; a CPUembedding cannot be indexed by a CUDA
position_ids.The existing tests used a fully resident CPU
nn.Moduledouble or a tiny4-layer model whose
ln_fpayload is below the 4 KiB offload threshold, so theynever reached either state.
Fix
_materialize_replay_module()(gptqmodel/models/definitions/pangu_alpha.py:132)now places a non-layer module before replay:
SequentialHook, viahas_cpu_offload_hooks)→
undo_offload_to_disk()from the repo's offload helpers;shell_module_materialize();move_to()for any remaining device relocation.ln_fis materialized on the hidden-state device andwqeon the resolvedreplay_device(target_device, falling back to the hidden/position device).For a resident model both calls are no-ops, so replay numerics are unchanged.
Before
flowchart TD A["ForwardExecutor.run_single<br/>forward_executor.py:457"] --> B["prepare_layer_replay_kwargs<br/>pangu_alpha.py:64"] B --> C{"last decoder layer?"} C -- no --> D["return cached kwargs"] C -- yes --> E["move_to(ln_f, hidden.device)<br/>pangu_alpha.py:101"] E --> F{"meta with recognized hook?"} F -- no --> G["NotImplementedError<br/>meta tensors without offload hooks"] F -- yes --> H["ln_f output"] H --> I["wqe(position_ids.to(target_device))<br/>pangu_alpha.py:121"] I --> J{"wqe on target_device?"} J -- no --> K["RuntimeError<br/>index cuda:0 vs weight cpu"] J -- yes --> L["custom_query"] style G fill:#ffccc7,stroke:#cf1322 style K fill:#ffccc7,stroke:#cf1322After
flowchart TD A["ForwardExecutor.run_single"] --> B["prepare_layer_replay_kwargs"] B --> C{"last decoder layer?"} C -- no --> D["return cached kwargs"] C -- yes --> E["_materialize_replay_module(ln_f, hidden.device)<br/>pangu_alpha.py:95"] E --> F{"meta?"} F -- no --> H["move_to(ln_f, hidden.device)"] F -- yes --> G{"offload hook?"} G -- yes --> I["undo_offload_to_disk(ln_f, device)"] G -- no --> J{"turtle_model?"} J -- yes --> K["shell_module_materialize(ln_f, device)"] J -- no --> L["move_to raises clear error"] I --> H K --> H H --> M["ln_f(hidden)"] M --> N["_materialize_replay_module(wqe, replay_device)<br/>pangu_alpha.py:128"] N --> O["wqe(position_ids.to(replay_device))"] O --> P["custom_query on replay_device"] style L fill:#ffccc7,stroke:#cf1322 style P fill:#f6ffed,stroke:#389e0dThe same diagrams and the failure contracts are in
docs/calibration-replay.md,linked from
README.md.Evidence
Repro fixture: 2-layer tiny PanGu built from the real
/monster/data/model/pangu_alpha_2_6Bconfig and remote code viatests/tiny_remote_models.build_tiny_remote_model,hidden_size=1024so thefinal norm exceeds the 4 KiB offload threshold. GPU 0 = RTX 4090.
Before (a978bf1)
device="cuda:0",offload_to_disk=TrueNotImplementedError: Cannot move a module that still contains meta tensors without offload hooksatpangu_alpha.py:101device="cpu",offload_to_disk=TrueNotImplementedErrordevice="cuda:0",offload_to_disk=FalseRuntimeError: Expected all tensors to be on the same device, but got index is on cuda:0, different from other tensors on cpuatpangu_alpha.py:121After
All three configurations complete quantization, save, reload and forward with
finite logits (
(1, 32, 128)), and the replayed last-layer values match aresident HF reference:
ln_foutcustom_query7.153e-070.00.00.0offload_to_disk=False7.153e-070.0The CUDA
ln_fdifference is float32 CPU-vs-GPU rounding. On the resident CPUfixture the new hook output equals the pre-fix values exactly (
torch.equal).The
q_projdiff vs resident FP (~6.9e-4) is ordinary 4-bit quantizationerror, not replay input drift.
Red / green tests
New regressions fail on
a978bf18cand pass after:After:
CPU-only collection (
CUDA_VISIBLE_DEVICES=) is7 passed, 3 skipped; theCUDA-only regressions skip with an explicit reason.
Test plan
Scope / unverified
config and remote code, not a full 32-layer 2.6B run (the 10.5 GB checkpoint
is present locally but a full GPTQ pass is outside this task's budget). The
hook-level tests are independent of checkpoint size.
tests/test_docs_contract_refs.pyhas 7 pre-existing failures ona978bf18cin unrelated docs (gguf-config-resolution.md,rocm-default-environment.md,generate-cpu-offload-autograd.md); verifiedagainst a clean
HEADworktree. Our docs/README edits add no new failures.