Skip to content

fix(loader): keep packed FP8 weights out of the accelerate cast (#4387) - #4509

Merged
Qubitium merged 3 commits into
mainfrom
fix/fp8-load-peak-4387
Nov 17, 2026
Merged

Qubitium merged 3 commits into
mainfrom
fix/fp8-load-peak-4387

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

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 FP8 weight names are now excluded from the checkpoint Accelerate sees and restored from the source checkpoint directly on the device_map target. 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):

  • resident 602.3 MiB
  • pre-fix peak 1285.4 MiB
  • fixed peak 602.3 MiB (ratio 1.000; delta 683.1 MiB matches the issue's widened-copy figure)
  • all 168 packed weight buffers byte-identical; fixed-prompt logits bitwise identical between loads

Tests

  • tests/test_fp8_load_peak_4387.py → 3 passed (CUDA peak test executed, not skipped)
  • fp8/resave/loader adjacent sweep (15 files) → 283 passed, 2 skipped (bnb)
  • tests/test_resave_missing_modules_4452.py stub updated for the new keyword-only extra_keys; no assertion changed
  • ruff check clean

Residual risk

device_map is 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.

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.
Comment thread gptqmodel/utils/model.py Resolved
…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.
@Qubitium
Qubitium merged commit 5290b72 into main Nov 17, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/fp8-load-peak-4387 branch November 17, 2026 00:24
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.
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