Skip to content

fix(load): never test an int-ordinal device map for truthiness (#4964) - #4983

Merged
Qubitium merged 1 commit into
mainfrom
root/fp8-int-zero-4964
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
root/fp8-int-zero-4964

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Since #4915 (the fix for #4863), every FP8 checkpoint loaded onto cuda:0 keeps its packed FP8 weight on the CPU. Only scales and biases reach the GPU, TorchFP8Linear then copies the weight host-to-device on every forward, and decode is ~4x slower with a fraction of the expected VRAM — no error, correct output.

Root cause is a falsy zero. #4915 rewrites "cuda:0" to the int ordinal 0 (_int_ordinal_device_map) so safetensors only touches the requested device, and _restore_checkpoint_buffers then placed a packed weight — which Accelerate never sees, because #4509 leaves it out of the checkpoint it reads — with

target_device = mapped_device(name) or target_device

0 is falsy, so the fallback (slot.device, i.e. CPU) won. Ordinal 1 still worked, which is why only the default single-GPU case regressed.

Bisect from the issue (same saved FP8 checkpoint): 3a5a5bea1 → {'cuda:0': 460 tensors}, weight on cuda:0; beb153af3 (#4915) → {'cuda:0': 290, 'cpu': 170}, all 168 FP8 weights on CPU.

What Changed

  • gptqmodel/utils/model.py: the nested mapped_device() closure becomes a module-level _device_map_target(device_map, name) that returns a torch.device or None — None for "the map says nothing" and for the disk/meta opt-outs. An int ordinal is normalized to torch.device("cuda", n), so no caller can test the raw map value for truthiness again.
  • The call site now reads mapped = mapped_device(name); if mapped is not None: target_device = mapped.
  • Longest-prefix lookup, the disk/meta opt-out and the exact_source_names gate are unchanged.

Tests

tests/test_fp8_device_map_int_zero_4964.py (new):

  • Behavioural (CUDA) — a module with a packed uint8 weight (the Accelerate-left-on-CPU shape) plus a tiny safetensors checkpoint; _restore_checkpoint_buffers(..., exact_source_names={"weight"}, device_map={"": 0}) must put the weight on cuda:0.
    • unmodified origin/main @ cdeb00e47: assert device(type='cpu') == device(type='cuda', index=0) → 1 failed
    • with the fix: passed
  • CPU-only resolver table — int 0/1 → cuda:0/cuda:1, "cuda:0" and torch.device forms unchanged, disk/meta/absent → None, longest-prefix selection, and ordinal 1 still reaching cuda:1 (skipped when fewer than two GPUs are visible).
CUDA_VISIBLE_DEVICES=3 python -m pytest tests/test_fp8_device_map_int_zero_4964.py -q
# 5 passed, 1 skipped   (the 2-GPU node)

CUDA_VISIBLE_DEVICES=3,4 python -m pytest tests/test_fp8_device_map_int_zero_4964.py -q
# all nodes pass (the 2-GPU node included)

Regression neighbours, same interpreter:

python -m pytest tests/test_load_device_context_4863.py tests/test_fp8_load_peak_4387.py \
                 tests/test_checkpoint_scale_dtype.py tests/test_fp8_device_map_int_zero_4964.py -q
# 9 passed, 3 skipped

tests/test_load_device_context_4863.py (the #4863 guard) fails 2 nodes on this host identically on unmodified main: its subprocess imports the container's flash_attn_2_cuda, which has an undefined c10_cuda_check_implementation symbol. Pre-existing, unrelated to this diff.

ruff check on both changed files: All checks passed.

  • Unit tests executed and passed (red on unmodified main, green here).
  • New targeted regression test added.

Model Support / Module Plan

Not applicable: no registry or definition change.

Logic Flow Documentation

Not applicable: no decision mechanism changed. The one predicate (0 is falsy) moved into a typed resolver; the reason is documented in its docstring and in the new test's header.

Review Requirements

  • Reviewed this PR.
  • Matches existing project structure: the closure is lifted to a module-level helper next to _int_ordinal_device_map, whose output it now interprets.
  • No monkeypatching.
  • Minimal surface: utils/model.py +37/−12, one new test file.
  • Regression considered: ordinal 1, string and torch.device forms all resolve as before (asserted), and the disk/meta/absent fallbacks are pinned.

Kernel Accuracy Requirements

Not applicable: no kernel or numerics changed; the fix only decides which device a restored packed tensor is moved to.

Closes #4964. Refs #4915, #4863, #4509.

#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 #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 #4964. Refs #4915, #4863, #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