Repository navigation
fix(load): never test an int-ordinal device map for truthiness (#4964) - #4983
Merged
Merged
Conversation
#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.
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
Since #4915 (the fix for #4863), every FP8 checkpoint loaded onto
cuda:0keeps its packed FP8weighton the CPU. Only scales and biases reach the GPU,TorchFP8Linearthen 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 ordinal0(_int_ordinal_device_map) sosafetensorsonly touches the requested device, and_restore_checkpoint_buffersthen placed a packed weight — which Accelerate never sees, because #4509 leaves it out of the checkpoint it reads — with0is falsy, so the fallback (slot.device, i.e. CPU) won. Ordinal1still 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 oncuda:0;beb153af3(#4915) →{'cuda:0': 290, 'cpu': 170}, all 168 FP8 weights on CPU.What Changed
gptqmodel/utils/model.py: the nestedmapped_device()closure becomes a module-level_device_map_target(device_map, name)that returns atorch.deviceorNone—Nonefor "the map says nothing" and for thedisk/metaopt-outs. An int ordinal is normalized totorch.device("cuda", n), so no caller can test the raw map value for truthiness again.mapped = mapped_device(name); if mapped is not None: target_device = mapped.disk/metaopt-out and theexact_source_namesgate are unchanged.Tests
tests/test_fp8_device_map_int_zero_4964.py(new):uint8weight (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 oncuda:0.origin/main@cdeb00e47:assert device(type='cpu') == device(type='cuda', index=0)→ 1 failed0/1→cuda:0/cuda:1,"cuda:0"andtorch.deviceforms unchanged,disk/meta/absent →None, longest-prefix selection, and ordinal1still reachingcuda:1(skipped when fewer than two GPUs are visible).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 skippedtests/test_load_device_context_4863.py(the #4863 guard) fails 2 nodes on this host identically on unmodified main: its subprocess imports the container'sflash_attn_2_cuda, which has an undefinedc10_cuda_check_implementationsymbol. Pre-existing, unrelated to this diff.ruff checkon both changed files: All checks passed.Model Support / Module Plan
Not applicable: no registry or definition change.
Logic Flow Documentation
Not applicable: no decision mechanism changed. The one predicate (
0is falsy) moved into a typed resolver; the reason is documented in its docstring and in the new test's header.Review Requirements
_int_ordinal_device_map, whose output it now interprets.utils/model.py+37/−12, one new test file.1, string andtorch.deviceforms all resolve as before (asserted), and thedisk/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.