Skip to content

fix: post-#6473 regressions (Trainer in a GPT-QModel process, late-import Accelerate dispatch, stale guards) - #6499

Merged
Qubitium merged 1 commit into
mainfrom
wl-post-refactor-fixes
Nov 30, 2026
Merged

Qubitium merged 1 commit into
mainfrom
wl-post-refactor-fixes

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Follow-up to #6473 / #6494 (tracking: #6471). It comes from re-auditing the accelerate removal for completeness and regressions.

Regressions fixed

  1. transformers.Trainer broke in any process that imports gptqmodel, even with Accelerate installed (TrainingArguments raised "requires accelerate"). This also broke the PEFT fine-tuning workflow in docs/peft.md.
    • Cause: _accelerate_isolation patched is_accelerate_available for all of Transformers.
    • Fix: a scoped import hook executes only transformers.trainer and transformers.training_args with the real flag.
    • Weight loading, placement, generate, load_adapter (integrations.peft) and the quantizers stay isolated. from_pretrained(device_map=...) still raises after the Trainer is imported (pinned by the test).
  2. Late-import Accelerate dispatch. A plain-Transformers script that does not import gptqmodel first gets past Transformers' device_map check, because Optimum imports gptqmodel only inside the GPTQ quantizer. Accelerate then dispatched the model, and the HF bridge mishandled Accelerate's hooks: it only understands weight_loader hooks, so disk-offloaded layers raised and CPU-offloaded Marlin layers were treated as resident.
    • Fix: hf_gptqmodel_post_init now refuses Accelerate-owned hooks with a clear message pointing to GPTQModel.load(placement=...). This applies "strict everywhere" regardless of import order.
  3. OSTQuant's lazy-checkpoint-shell guard still read owner.turtle_model, so it never fired. It now reads checkpoint_source.
  4. Three unchanged tests regressed (they pass on aff56aa1a):

Cleanup

  • utils/offload.py: remove the preexisting-hook take/restore plumbing; its setter was removed in Delete the dead hook-based offload machinery (#6471) #6494, so it was always a no-op.
  • weight_loader/plan.py: remove the unreferenced _split_dim_cache_key.
  • Docs, comments and strings that still described Accelerate dispatch or LazyTurtle names: weight-loader.md, peft.md, hf-interop.md §6, loader-quantized-redirect.md, fp8-save-offload.md, calibration-replay.md, tenstorrent.md, the isolation docstring, bridge comments in utils/model.py, the importer.py docstring, and the logger.py region label.
  • Kept on purpose: the max_memory remedy string in importer.py, because test_default_device_mixed_capability_4705.py asserts on it.
flowchart TD
    A[import gptqmodel] --> B[is_accelerate_available = False for Transformers]
    B --> C{module being imported}
    C -- transformers.trainer / training_args --> D[exec with real flag: Trainer gets Accelerator]
    C -- modeling_utils / generation / integrations.accelerate / peft / quantizers --> E[isolated: device_map raises, hooks = weight_loader]
    F[plain Transformers, gptqmodel imported late by Optimum] --> G[Accelerate dispatches]
    G --> H[hf_gptqmodel_post_init]
    H --> I[ValueError: use GPTQModel.load placement=]
Loading

Validation (targeted only)

  • Red/green: the new tests fail on main and pass here:
    • test_accelerate_isolation.py::test_transformers_trainer_still_trains_in_a_gptqmodel_process[gptqmodel_first|trainer_first]
    • test_hf_bridge_refuses_accelerate_dispatch.py
    • test_ostquant_config.py::test_ostquant_refuses_a_lazy_checkpoint_shell
  • A/B over 198 unchanged test files covering loading, offload, checkpoint, bridge, device and GGUF (CPU, per file, run on aff56aa1a, main and this branch):
    • main had 3 regressions against aff56aa1a;
    • this branch has 0 files worse than aff56aa1a.
  • Isolation, unpatched-Transformers and bridge tests: 15 passed. Offload, undo and bridge tests show the same failure set as main (environmental: no AVX512-BF16, no optimum).
  • The ruff gate passes.

Also measured (no change needed)

🤖 Generated with Claude Code

…mport Accelerate dispatch, stale guards

Regressions found by re-auditing the accelerate removal (#6471):

- Importing gptqmodel hid Accelerate from all of Transformers, so
  `TrainingArguments`/`Trainer` raised "requires accelerate" even with it
  installed (PEFT fine-tuning of GPT-QModel models broke). The isolation now
  executes only `transformers.trainer` / `transformers.training_args` with the
  real flag via a scoped import hook; loading, placement, generation and
  `load_adapter` stay isolated.
- A plain-Transformers load that imports gptqmodel late (Optimum imports it
  inside the GPTQ quantizer, after `device_map` was accepted) got dispatched by
  Accelerate and then half-initialized by the bridge, which only understands
  weight_loader hooks. `hf_gptqmodel_post_init` now refuses Accelerate hooks
  with a pointer to `GPTQModel.load(placement=...)` (strict everywhere).
- OSTQuant's lazy-shell guard still read `turtle_model`; it never fired.
- Three unchanged tests still used LazyTurtle's private interface or a
  deleted test file.

Cleanup: dead preexisting-hook plumbing in utils/offload.py (its setter went
in #6494), unused `_split_dim_cache_key`, and stale docs/comments that still
described Accelerate dispatch or LazyTurtle names.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Qubitium
Qubitium marked this pull request as ready for review November 30, 2026 18:38
@Qubitium
Qubitium merged commit e7527e0 into main Nov 30, 2026
6 checks passed
@Qubitium
Qubitium deleted the wl-post-refactor-fixes branch November 30, 2026 18:39
Qubitium added a commit that referenced this pull request Nov 30, 2026
…um/PEFT compat (#6501)

#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