Repository navigation
fix(hf): resolve an accelerate keyword map before validating GPU capabilities (#5084) - #5123
Merged
Merged
Conversation
…ties Transformers calls a quantizer's `preprocess_model` before it computes the placement from `max_memory`, and it never forwards `max_memory`. The bridge expanded `device_map="auto"` to every visible GPU and validated the result, so a host whose GPUs span compute-capability groups refused every keyword-map load even when the budget named one card. `GPTQModel.load` was already fixed for that path (#4632/#4747); opt-in `device_map={"": N}` maps loaded fine. - `hf_normalize_device_device_map` now normalizes an accelerate keyword to the accelerator family instead of a concrete ordinal. A family selector makes kernel selection validate the candidate against every visible device, so the card the budget names is served. - A homogeneous host keeps the historical concrete expansion, so the selected kernel and every existing load are unchanged; only a host that spans capability groups defers to the family. - `validate_device_map_capability_groups` runs in the post-weight hook on the dispatched `model.hf_device_map`. A resolved map that really spans generations is still refused, now naming remedies that work on this path instead of the `max_memory` budget that just produced the map. - Explicit maps keep failing closed up front. Tests: tests/test_hf_bridge_mixed_capability_5084.py (9 CPU-mocked nodes). Six fail on the parent commit for the intended reason (the keyword refusal and the missing post-dispatch check); the homogeneous and explicit-map controls pass on both. Affected families: 306 passed, 48 skipped.
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
AutoModelForCausalLM.from_pretrained(..., device_map="auto", max_memory=<one card>)failed on every host whose visible GPUs span compute-capability groups, even when
the budget named a single card.
GPTQModel.loadwith the same budget has workedsince #4632, and explicit maps (
{"": 1},1,"cuda:1") loaded fine throughthe same bridge; only the accelerate keyword path failed.
Cause: Transformers calls the quantizer's
preprocess_modelbefore it resolvesdevice_mapfrommax_memory, andmax_memoryis never forwarded. The bridgeexpanded the
"auto"keyword to every visible GPU and validated that span, sokernel selection refused GPUs the budget never touched.
Fixes #5084
What Changed
gptqmodel/utils/importer.pyhf_normalize_device_device_map(the bridge entry point) now normalizes anaccelerate keyword map to the accelerator family instead of a concrete
ordinal (
defer_keyword_placement). Family selectors make kernel selectionvalidate the candidate on every visible device, so the card the budget names
is served.
kernel and every existing load are unchanged.
validate_device_map_capability_groupsvalidates the map Transformersactually resolved;
_validate_selector_device_familiesaccepts a remedy sothe post-dispatch refusal does not recommend the
max_memorybudget thatjust produced that map.
gptqmodel/utils/hf_load.py: the native prepare hook uses the same deferrednormalizer, and the post-init hook validates the dispatched
hf_device_map.docs/hf-interop.md§ 6 documents the mechanism and links fromREADME.md(existing link).
mixed maps still fail closed up front.
Old flow (keyword map validated too early) — before:
gptqmodel/utils/importer.py:1083hf_normalize_device_device_map→_accelerate_keyword_device→_validate_selector_device_familiesatgptqmodel/utils/importer.py:1075.flowchart TD A["from_pretrained device_map=auto max_memory one card"] --> B["preprocess_model device_map=auto"] B --> C["hf_normalize_device_device_map expands every visible GPU"] C --> D{"capabilities differ?"} D -->|"yes"| E["ValueError: mixed compute capabilities"] D -->|"no"| F["kernel valid on every GPU"] E --> G["max_memory never applied"] classDef failure fill:#ffe8e8,stroke:#b30000,color:#111; class E,G failure;New flow:
flowchart TD A["from_pretrained device_map=auto max_memory one card"] --> B["preprocess_model device_map=auto"] B --> C{"host spans capability groups?"} C -->|"no"| D["expand to every visible GPU as before"] C -->|"yes"| E["keep accelerator family"] D --> F["kernel valid on every expanded GPU"] E --> G["kernel valid on every visible GPU"] F --> H["Transformers resolves max_memory"] G --> H H --> I["Accelerate dispatches and sets hf_device_map"] I --> J["validate_device_map_capability_groups"] J -->|"one capability group"| K["load succeeds"] J -->|"mixed groups"| L["ValueError: explicit map or CUDA_VISIBLE_DEVICES"] classDef failure fill:#ffe8e8,stroke:#b30000,color:#111; classDef ok fill:#d9f2d9,stroke:#2e7d32; class L failure; class K ok;device_map="auto", max_memory={1: ...}on a mixed hostValueErrorbefore the budget appliesdevice_map="auto", max_memory={1: ..., "cpu": ...}on a mixed hostValueErrormax_memorynaming both capability groupsValueErrorbefore weights loadValueErrorafter dispatch, naming a remedy that works heredevice_mapdictValueErrorTests
Environment: torch 2.15.0+cu132, transformers 5.22.2, optimum 2.3.0,
accelerate 1.15.0, Python 3.14t.
End-to-end, one GPTQ-4bit
Llama-3.2-1Bcheckpoint,dtype=float16, capabilitypairs simulated as
cuda:0=8.9,cuda:1=12.0on the kernel-selection path. Samescript before and after:
from_pretrained(device_map="auto", max_memory={1: "20GiB"})ValueError: cuda:0=8.9, cuda:1=12.0cuda:1, forward(1, 4, 128256)max_memory={0: "20GiB"}ValueErrorcuda:0max_memory={1: "700MiB", "cpu": "20GiB"}ValueErrorcuda:1+ CPU offloadmax_memory={1: ..., 0: ...}ValueErrorRegressions:
tests/test_hf_bridge_mixed_capability_5084.py— 9 passed.test_keyword_map_is_not_rejected_before_max_memory[auto,balanced,sequential,balanced_low_0]max_memorytest_keyword_map_keeps_every_visible_device_selectabletest_a_homogeneous_host_keeps_the_previous_expansiontest_an_explicit_mixed_map_is_still_refusedtest_post_init_refuses_a_resolved_mixed_maptest_post_init_accepts_a_resolved_single_card_mapRed evidence on the parent commit (
0c0da3e96, disposable worktree, same file):6 failed, 3 passed. Failures are the intended assertion
(
ValueError: ... different GPU compute capabilities: cuda:0=8.9, cuda:1=12.0),not collection or import errors; the homogeneous and explicit-map controls pass
on both commits.
Affected families (current base
0c0da3e96):Skips: 48. The placement/capability nodes skip on ROCm by design (the HIP
capability tuple is not an SM version); the rest are optional-dependency and
hardware gates inside the bridge/packing suites. Not verified: a physical host
with two GPU generations in one process (the capability pairs are simulated for
kernel selection; the end-to-end loads and the forward pass ran on real GPUs),
and ROCm, where this contract does not apply.
Model Support / Module Plan
Not applicable: no model type, alias, definition, module plan, fusion, or AWQ
policy change.
Reviewer notes
expand_selector_device_familyisuntouched, so no other caller of the normalizer changes behaviour.
best kernel that is valid on every visible card, not the best kernel for one
card. That is required because the budget is not known yet, and it keeps the
selection safe for whichever card is chosen.
quantizer hook that receives an unresolved map.