Skip to content

fix(hf): resolve an accelerate keyword map before validating GPU capabilities (#5084) - #5123

Merged
Qubitium merged 1 commit into
mainfrom
fix/5084-hf-keyword-device-map
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/5084-hf-keyword-device-map

Conversation

@ZX-ModelCloud

Copy link
Copy Markdown
Collaborator

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.load with the same budget has worked
since #4632, and explicit maps ({"": 1}, 1, "cuda:1") loaded fine through
the same bridge; only the accelerate keyword path failed.

Cause: Transformers calls the quantizer's preprocess_model before it resolves
device_map from max_memory, and max_memory is never forwarded. The bridge
expanded the "auto" keyword to every visible GPU and validated that span, so
kernel selection refused GPUs the budget never touched.

Fixes #5084

What Changed

  • gptqmodel/utils/importer.py
    • hf_normalize_device_device_map (the bridge entry point) now normalizes an
      accelerate keyword map to the accelerator family instead of a concrete
      ordinal (defer_keyword_placement). Family selectors make kernel selection
      validate the candidate on 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.
    • validate_device_map_capability_groups validates the map Transformers
      actually resolved; _validate_selector_device_families accepts a remedy so
      the post-dispatch refusal does not recommend the max_memory budget that
      just produced that map.
  • gptqmodel/utils/hf_load.py: the native prepare hook uses the same deferred
    normalizer, and the post-init hook validates the dispatched hf_device_map.
  • docs/hf-interop.md § 6 documents the mechanism and links from README.md
    (existing link).
  • Out of scope: the native loader and explicit maps are untouched; explicit
    mixed maps still fail closed up front.

Old flow (keyword map validated too early) — before:
gptqmodel/utils/importer.py:1083 hf_normalize_device_device_map →
_accelerate_keyword_device → _validate_selector_device_families at
gptqmodel/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;
Loading

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;
Loading
Situation Before After
device_map="auto", max_memory={1: ...} on a mixed host ValueError before the budget applies Loads on the budgeted card
device_map="auto", max_memory={1: ..., "cpu": ...} on a mixed host Same ValueError Loads; CPU-offload kernel contract still applies (#5022)
max_memory naming both capability groups ValueError before weights load ValueError after dispatch, naming a remedy that works here
Homogeneous host, any keyword One kernel for all visible GPUs Unchanged
Explicit mixed device_map dict ValueError Unchanged, still refused up front

Tests

  • Unit tests have been executed and passed.
  • Added a new focused unit test for this change.

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-1B checkpoint, dtype=float16, capability
pairs simulated as cuda:0=8.9, cuda:1=12.0 on the kernel-selection path. Same
script before and after:

Call base this branch
from_pretrained(device_map="auto", max_memory={1: "20GiB"}) ValueError: cuda:0=8.9, cuda:1=12.0 loads on cuda:1, forward (1, 4, 128256)
max_memory={0: "20GiB"} same ValueError loads on cuda:0
max_memory={1: "700MiB", "cpu": "20GiB"} same ValueError loads, layers on cuda:1 + CPU offload
max_memory={1: ..., 0: ...} same ValueError still refused, with the new remedy text
uniform-capability host, same budgets loads loads (unchanged)

Regressions: tests/test_hf_bridge_mixed_capability_5084.py — 9 passed.

Test node Invariant
test_keyword_map_is_not_rejected_before_max_memory[auto,balanced,sequential,balanced_low_0] The bridge does not refuse a keyword map before max_memory
test_keyword_map_keeps_every_visible_device_selectable An unresolved keyword selects a kernel valid on every visible card
test_a_homogeneous_host_keeps_the_previous_expansion Uniform hosts keep the concrete expansion
test_an_explicit_mixed_map_is_still_refused Explicit mixed maps still fail closed
test_post_init_refuses_a_resolved_mixed_map The resolved mixed map is refused after dispatch
test_post_init_accepts_a_resolved_single_card_map A one-card budget plus CPU offload passes

Red 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):

cd tests
python -m pytest test_hf_bridge_mixed_capability_5084.py \
  test_default_device_mixed_capability_4705.py test_max_memory_kernel_devices_4632.py \
  test_layerwise_device_placement.py test_hf_gptqmodel_load_bridge.py \
  test_hf_packing_api.py kernels/test_selection.py test_hf_bridge_cpu_offload_5022.py \
  test_hf_bridge_v2_save_qzeros_4952.py test_quantize_device_sequence_4875.py
# 306 passed, 48 skipped

ruff check gptqmodel tests setup.py
# All checks passed

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

  • Only the accelerate keyword path changes. expand_selector_device_family is
    untouched, so no other caller of the normalizer changes behaviour.
  • Trade-off: on a mixed-capability host the unresolved keyword selects the
    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.
  • Follow-up (out of scope): duplicate the same deferred placement in any future
    quantizer hook that receives an unresolved map.

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants