Repository navigation
fix(load): read the checkpoint with the int device ordinal so cuda:0 stays untouched (#4863) - #4915
Merged
Conversation
…stays untouched (ModelCloud#4863) `GPTQModel.load(path, device="cuda:1")` also created a full CUDA primary context on `cuda:0` -- about 500 MiB on a GPU the caller never asked for, for the life of the process. ModelCloud#4320/ModelCloud#4653 fixed this class for the quantize path; the inference load path still took it. The trigger is the checkpoint read. `load_checkpoint_in_model_then_tie_weights` hands the device map to `accelerate.load_checkpoint_in_model`, which passes the map value straight to `safetensors.safe_open(device=...)`. With a *string* device, safetensors initializes the primary context of the current CUDA device in addition to the requested one. With an int ordinal it only touches the device that was asked for, and Accelerate already supports int ordinals everywhere it accepts a device map entry (`load_state_dict` maps an int to `safe_load_file(device=N)`; `set_module_tensor_to_device` accepts one). The map is converted on a local copy for the duration of the load, so callers and the later dispatch path still see the string form. Measured on two GPUs with `torch._C._cuda_hasPrimaryContext(0)` after `GPTQModel.load(..., dtype=torch.float16)`: | placement | before | after | | --- | --- | --- | | `device="cuda:1"` | context on cuda:0 | **no context on cuda:0** | | `device_map={"": "cuda:1"}` | context on cuda:0 | **no context on cuda:0** | | `max_memory={1: "20GiB"}` | context on cuda:0 | **no context on cuda:0** | | `device_map={"": 1}` (int, reference) | no context | no context | | `device="cpu"` | none | none | | map spanning cuda:0 and cuda:1 | both | both (both were requested) | Regression test: `tests/test_load_device_context_4863.py` loads a tiny saved GPTQ checkpoint in a subprocess (a primary context is process-wide and cannot be released) and asserts cuda:1 has a context while cuda:0 has none. Red on the parent commit for both the `device=` and `device_map=` forms.
This was referenced Nov 17, 2026
Qubitium
added a commit
that referenced
this pull request
Nov 17, 2026
* `tests/test_docs_contract_refs.py` + the four docs it pins — took main's #4971 symbol anchors (`path::symbol`) over this branch's line-number pins, which is what that guard was changed to for exactly this file. Also converted the one remaining `_quantize_summary_counts` pin to the symbol form, because this branch's `base.py` moved it from 347 to 356. * `gptqmodel/utils/model.py` — main refactored the local `mapped_device` into the shared `_device_map_target` helper (#4915/#4964). Took that, and added the `include_disk=` parameter back to it so this branch's intentional-disk-offload restore path can still tell a disk offload from a never-materialized slot. Verification: `tests/test_docs_contract_refs.py`, `test_cpu_offload_tied_both_copies_4943.py`, `test_cpu_offload_device_map_tied_head_4866.py`, `test_load_strict_kwargs_4945.py`, `test_loader_offload_staging_4385.py`, `test_hf_gptqmodel_load_bridge.py` -> 84 passed. `ruff check gptqmodel tests` clean.
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
GPTQModel.load(path, device="cuda:1")also created a full CUDA primary context on cuda:0 — about 500 MiB on a GPU the caller never asked for, held for the life of the process.#4320/#4653fixed this class for the quantize path; the inference load path still took it. Closes #4863.The trigger is the checkpoint read.
load_checkpoint_in_model_then_tie_weightshands the device map toaccelerate.load_checkpoint_in_model, which passes the map value straight tosafetensors.safe_open(device=...). With a string device, safetensors initializes the primary context of the current CUDA device in addition to the requested one. With an int ordinal it only touches the device that was asked for — and Accelerate already supports int ordinals everywhere it accepts a device map entry (load_state_dictmaps an int tosafe_load_file(device=N);set_module_tensor_to_deviceaccepts one).Fix
The map is converted to int ordinals on a local copy for the duration of the load (
_int_ordinal_device_map), so callers and the later dispatch path still see the string form."cpu","disk","meta","cuda"without an index, and non-CUDA backends pass through unchanged.Measurements
Two visible GPUs;
torch._C._cuda_hasPrimaryContext(0)afterGPTQModel.load(..., dtype=torch.float16):device="cuda:1"device_map={"": "cuda:1"}max_memory={1: "20GiB"}device_map={"": 1}(int, reference)device="cpu"Forward output is unchanged; every case still produces finite logits on the requested device.
Tests
New
tests/test_load_device_context_4863.py. A primary context is process-wide and cannot be released, so the check runs in a subprocess: the parent saves a tiny 4-bit GPTQ checkpoint, the child loads it ontocuda:1and prints the context state of both GPUs.Unrelated suites on this host are unchanged:
ruff checkon the loader and the new test: All checks passed.Logic Flow Documentation
Not applicable: the read path keeps its shape, only the device spelling changes.
Model Support / Module Plan
Not applicable: no definition or plan change.