Skip to content

fix(marlin): refresh CPU offload snapshot after post-init - #3452

Merged
Qubitium merged 2 commits into
mainfrom
fix/marlin-cpu-offload-post-init-1499
Oct 27, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/marlin-cpu-offload-post-init-1499

Conversation

@ZX-ModelCloud

@ZX-ModelCloud ZX-ModelCloud commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Closes #1499.
With a mixed device_map, Accelerate snapshots an offloaded GPTQ Marlin layer's weights before Marlin runs post_init(). Marlin then empties g_idx when desc_act=False and repacks qweight and scales. The first forward tries to restore the older snapshot into the new tensor shapes and raises a shape error.

Fix

For a CPU-offloaded Marlin layer, gptqmodel_post_init() now uses the layer's existing Accelerate hook to materialize its weights, runs post_init(), and replaces the hook's CPU snapshot with the resulting packed quantized tensors. The layer returns to meta afterward, so later forwards load the post-init layout. This works with a direct AlignDevicesHook or one inside a SequentialHook. A layer whose weights are already resident still runs post_init() directly.

Validation

  • A deterministic reproduction using Accelerate's CPU offload hook fails against main on the first pre_forward(): the saved qweight is [32, 128], while Marlin's post-init placeholder is [16, 256]. The same reproduction succeeds with this change.
  • The new test covers both desc_act values and both hook forms. Two consecutive reloads match a separately initialized, never-offloaded Marlin layer tensor for tensor, and parameters return to meta after each reload.

@ZX-ModelCloud
ZX-ModelCloud marked this pull request as ready for review October 27, 2026 13:06
@Qubitium
Qubitium merged commit 028e8f8 into main Oct 27, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/marlin-cpu-offload-post-init-1499 branch October 27, 2026 13:36
Qubitium added a commit that referenced this pull request Nov 18, 2026
…5046)

Plain `transformers`/Optimum loading of a GPT-QModel GPTQ/RTN or AWQ-GEMM
checkpoint with a map that offloads layers to CPU failed at load time, although
`GPTQModel.load` serves the same maps since #4890:

* GPTQ/RTN: `RuntimeError: CPU-offloaded Marlin requires a writable in-memory
  weights map.`
* AWQ-GEMM: `ValueError: AWQ Marlin requires CUDA tensors, got 'meta'.`

Two independent causes.

1. The bridge never told the selector about the offload. `from_quantized`
   derives `cpu_offload` from the map and passes it to `select_quant_linear`,
   so AUTO skips a kernel whose `post_init` needs CUDA. The HF entry points did
   not: `hf_select_quant_linear`, `hf_select_quant_linear_v2` (Transformers'
   GPTQ quantizer, Optimum's `GPTQQuantizer.convert_model`, and AWQ's
   `replace_with_awq_linear`) and the native bridge's `make_quant` all selected
   `MarlinLinear`/`AwqMarlinLinear` for an offloading map. The check now lives
   in `utils.importer.device_map_uses_cpu` (the native loader's
   `_device_map_uses_cpu` delegates to it) and `auto_cpu_offload`, which
   narrows it to AUTO so an explicit `backend="marlin"` stays the caller's
   choice.

2. `_post_init_cpu_offloaded_marlin` accepted only a plain `dict` or a
   `PrefixedDataset` over a `dict`. Transformers dispatches through Accelerate,
   which attaches
   `AlignDevicesHook(weights_map=PrefixedDataset(OffloadedWeightsLoader(...)))`,
   so the #3452 path could never run from a real `from_pretrained`.
   `_cpu_offload_writable_storage` unwraps the prefix and writes through the
   loader's in-memory `state_dict`, refusing only entries that live in its disk
   `index` (same message, now naming the disk-backed keys). `AwqMarlinLinear`
   shares the post-init: it repacks into the Marlin layout too, and before this
   it ran `post_init` on `meta`.
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.

marlin g_idx issue when using dict devcie_map

2 participants