Skip to content

fix: scope Accelerate isolation to GPT-QModel calls; restore HF/Optimum/PEFT compat - #6501

Merged
Qubitium merged 1 commit into
mainfrom
accel-scoped-isolation
Nov 30, 2026
Merged

Qubitium merged 1 commit into
mainfrom
accel-scoped-isolation

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Follow-up to #6499 (tracking: #6471). Per maintainer decision: GPT-QModel's own paths stay Accelerate-free, while plain Transformers / Optimum / PEFT keep upstream Accelerate behavior, because Optimum and PEFT drive GPT-QModel kernels for GPTQ/AWQ training.

Problem (on current main)

PEFT LoRA training matrix (Llama-3.2-1B GPTQ + AWQ-Llama-3.2-1B-g128-gemm, 12 steps, save/reload adapter):

load path main this PR
GPTQModel.load(backend=trainable) GPTQ ✅ / AWQ ✅ ✅ / ✅
plain HF, device_map="auto", gptqmodel imported first ❌ "requires accelerate" (both) ✅ / ✅
plain HF, device_map="auto", Optimum imports gptqmodel late GPTQ ✅ / AWQ ❌ "requires accelerate" ✅ / ✅
plain HF, no device_map, gptqmodel imported first GPTQ ✅ / AWQ ❌ (the AWQ quantizer needs Accelerate even without a map) ✅ / ✅
plain HF, Accelerate 2-GPU split GPTQ ❌ refused by the bridge / AWQ ❌ ✅ / ✅

Every scenario trains: loss drops from 0.63/0.69 to 0.10–0.19, LoRA weights change and the base qweight stays frozen. The 6-scenario AWQ results match the pre-#6473 tree.

Change

  • Scoped isolation. _accelerate_isolation is now a ContextVar scope entered by GPT-QModel's entry points: GPTQModel.load / from_pretrained / from_quantized / quantize_mlx and BaseQModel.quantize / generate / save / serve.
    • Inside the scope, Transformers sees is_accelerate_available() == False, modeling_utils uses the weight_loader helpers, and generate builds weight_loader hooks.
    • Outside it, Transformers is exactly upstream.
    • A module first imported inside the scope executes unscoped, so its import-time Accelerate bindings are not lost.
  • Hook dispatch by type. add_hook_to_module hands a weight_loader hook to GPT-QModel and any other hook to Accelerate.
  • HF bridge. It accepts Accelerate's hooks and weights maps again (_hook_types), replacing fix: post-#6473 regressions (Trainer in a GPT-QModel process, late-import Accelerate dispatch, stale guards) #6499's refusal. Accelerate is never imported by this check.
  • cpu> / disk> dense loads (found while tracing; broken on main since Remove accelerate: explicit WeightLoader for weight loading, placement and offload (#6471) #6473):
    • The materialization check no longer rejects the offload hooks' meta placeholders.
    • An offloaded block gets an input hook, so kwargs consumed outside its leaves reach the execution device. Before, Llama position_embeddings failed with "Expected all tensors to be on the same device".
  • Docs. weight-loader.md (new "Accelerate scope" section with a mermaid flow) and hf-interop.md §6. The fix: post-#6473 regressions (Trainer in a GPT-QModel process, late-import Accelerate dispatch, stale guards) #6499 strict-contract edits are reverted in peft.md (the plain-Transformers flow is supported again) and tenstorrent.md.
flowchart TD
    A[call into Transformers] --> B{inside a GPT-QModel entry point?}
    B -- yes --> C[is_accelerate_available False; weight_loader hooks; no device_map]
    B -- no --> D[upstream: real flag, Accelerate dispatch]
    D --> E[HF bridge accepts Accelerate or weight_loader hooks]
    F[add_hook_to_module] --> G{hook type}
    G -- weight_loader --> H[GPT-QModel]
    G -- other --> I[Accelerate]
Loading

Validation (targeted)

  • Zero Accelerate calls on GPT-QModel paths. A call tracer recorded every call into accelerate.* from outside Accelerate during load, generate and save, with Accelerate installed. It found 0 calls for:
    • dense, single GPU;
    • dense, 2-GPU device pool;
    • dense with cpu>accel:1 layers (its output matches the single-GPU load);
    • quantized TinyLlama.
  • Red/green. The new tests fail on main and pass here:
    • test_accelerate_isolation.py (scope, hook-type dispatch, lazily imported module bindings, plain-HF GPTQ device_map load with gptqmodel imported first, Trainer);
    • test_hf_bridge_accelerate_hooks.py::test_accelerate_offload_hook_is_recognized;
    • test_offload_block_inputs.py (1 and 2 GPU).
  • A/B against main over the weight-loader, placement, dispatch, offload, isolation, HF bridge and loader suites: 418 passed on this branch, and the 4 failures are identical on main. Those are the Marlin JIT build, which can't build in this environment, plus two test_hf_bridge_cpu_offload_5022 cases that also fail before Remove accelerate: explicit WeightLoader for weight loading, placement and offload (#6471) #6473.
  • Isolation, bridge, offload and unpatched tests: 17 passed on the rebased branch. The ruff gate passes.

Note: Transformers imports Accelerate at startup again when it is installed, as upstream does. GPT-QModel itself still neither depends on nor calls it.

🤖 Generated with Claude Code

…um/PEFT compat

#6499 hid Accelerate from all of Transformers once gptqmodel was imported.
Optimum and PEFT drive GPT-QModel kernels through plain Transformers with
Accelerate placing the model, so that broke `device_map` loads of GPTQ/AWQ
checkpoints (AWQ failed even without a device_map: its quantizer requires
Accelerate) and refused Accelerate-dispatched models in the bridge.

- Isolation is now a ContextVar scope entered by GPT-QModel's entry points
  (GPTQModel.load/from_pretrained/from_quantized/quantize_mlx, BaseQModel
  quantize/generate/save/serve). Inside: is_accelerate_available() is False,
  modeling_utils uses weight_loader helpers, generate builds weight_loader
  hooks. Outside: Transformers is upstream (real flag and Accelerate bindings).
- add_hook_to_module dispatches by hook type (weight_loader hook -> ours,
  anything else -> Accelerate's).
- A Transformers module first imported inside the scope executes unscoped,
  so its import-time Accelerate bindings match an ordinary import.
- The HF bridge accepts Accelerate's hooks and weights maps again
  (`_hook_types`), replacing the #6499 refusal.
- Dense loads with a `cpu>`/`disk>` placement: the materialization check no
  longer rejects the offload hooks' meta placeholders, and an offloaded block
  gets an input hook so kwargs consumed outside its leaves (Llama
  `position_embeddings`) reach the execution device.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant