Repository navigation
fix(loader): keep packed FP8 weights out of the accelerate cast (#4387) - #4509
Merged
Merged
Conversation
Accelerate casts every floating checkpoint tensor to the requested model dtype. For an FP8 checkpoint that materialized each packed `weight` as FP16/BF16 on the target device, and staged the whole shard there, until the loader re-read the FP8 bytes; the load peak was ~2.1x resident. Write a temporary safetensors checkpoint without the packed weights, let Accelerate load only the dense tensors, and restore the packed bytes from the source checkpoint on the `device_map` target. Key-converted checkpoints use a runtime-to-source name map for the restore. Pickle and non-safetensors checkpoints keep the previous path. Measured on a GPTQModel FP8 export of SmolLM2-135M (210 packed E4M3 weights): BF16 peak allocated 376 MB -> 164 MB (= resident), host VmHWM 2.44 GB -> 2.13 GB; the state-dict SHA-256 and a fixed forward pass are identical. A two-shard copy measured 296 MB -> 164 MB.
…ne refs `_tie_weights_after_checkpoint_load` gained a keyword-only `extra_keys` argument so the packed FP8 names the loader restores itself are not reported as missing. The stub in the #4494 test only accepted positional arguments and failed on the new keyword; accept keywords like the stub in `test_checkpoint_scale_dtype.py` already does. No assertion changes. Also point the checkpoint-load row in the FP8 lifecycle doc at the current `accelerate.load_checkpoint_in_model` and `_restore_checkpoint_buffers` call sites (`:3478`, `:3540`); the old `:3422`/`:3461` refs drifted during the branch work.
…me/device mapping #4507 taught _restore_checkpoint_buffers to restore dense Parameters and to fall back to the slot device; keep that and the packed-FP8 runtime->source name map plus device_map placement.
This was referenced Nov 17, 2026
sLaffey
pushed a commit
to sLaffey/GPTQModel
that referenced
this pull request
Nov 18, 2026
…Cloud#4964) (ModelCloud#4983) ModelCloud#4915 spells `cuda:0` as the int ordinal `0` so `safetensors` only touches the requested device. `_restore_checkpoint_buffers` then placed a packed FP8 weight — which Accelerate never sees, because ModelCloud#4509 leaves it out of the checkpoint it reads — with target_device = mapped_device(name) or target_device and `0` is falsy, so the fallback won: every packed FP8 weight stayed on the CPU slot while its scales and biases reached the GPU. `TorchFP8Linear` then copied the weight host-to-device on every forward, so decode was ~4x slower and the reported VRAM was a fraction of the checkpoint, with no error. Ordinal `1` worked, so only the default single-GPU case was hit. The resolver is now a module-level `_device_map_target(device_map, name)` that returns a `torch.device` (or `None` for "no entry" / `disk` / `meta`), and the call site tests `is not None`. An int ordinal is normalized to `torch.device("cuda", n)`, so ordinal `0` and ordinal `1` behave alike and no caller can reintroduce the truthiness bug. The longest-prefix lookup and the `disk`/`meta` opt-out are unchanged. Tests: `tests/test_fp8_device_map_int_zero_4964.py`. The behavioural node drives `_restore_checkpoint_buffers` with a packed-uint8 buffer Accelerate left on the CPU and `device_map={"": 0}`; on unmodified main it fails with `assert device(type='cpu') == device(type='cuda', index=0)`, and with the fix the weight lands on `cuda:0`. CPU-only nodes pin the resolver: int 0/1, string and `torch.device` forms, `disk`/`meta`/absent, and longest-prefix selection. Closes ModelCloud#4964. Refs ModelCloud#4915, ModelCloud#4863, ModelCloud#4509.
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 #4387 item 3.
accelerate.load_checkpoint_in_model(dtype=...)cast every packed FP8 weight to the model dtype and kept those widened copies alive until the dtype-restore pass re-read the checkpoint, so an FP8 load peaked at ~2.1x resident. The packed FP8weightnames are now excluded from the checkpoint Accelerate sees and restored from the source checkpoint directly on thedevice_maptarget. A runtime→source name map handles key-converted checkpoints, and the tie-weights missing-key set accepts the packed names.Measured evidence
Synthetic CUDA test model: peak 49.4 MiB → 17.3 MiB (= checkpoint bytes, ratio 1.000).
Real FP8 export of Qwen2.5-0.5B-Instruct (load → quantize FP8 → save → reload on CUDA):
Tests
tests/test_fp8_load_peak_4387.py→ 3 passed (CUDA peak test executed, not skipped)tests/test_resave_missing_modules_4452.pystub updated for the new keyword-onlyextra_keys; no assertion changedruff checkcleanResidual risk
device_mapis exercised with dict maps only; integer maps, multi-GPU splits, and disk/meta offload targets are unverified (falls back to the buffer's current device). Pickle checkpoints keep the old path.Resolves #4387.