Repository navigation
Description
Summary
After #6143 (aac58e2b3), requantize(embed_quant_mode=...) no longer crashes inside input capture when the checkpoint
is loaded with the default GPTQModel.load(path) and two GPUs are visible (the second symptom of #6132). But the model
is still unusable in-session: the first generate() after requantize() fails with
File "gptqmodel/models/ports/native/qwen2/modeling_qwen2.py", line 393, in forward
hidden_states = self.norm(hidden_states)
File "gptqmodel/models/ports/native/qwen2/modeling_qwen2.py", line 247, in forward
return self.weight * hidden_states.to(input_dtype)
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:1 and cuda:0!
Root cause: with the default offload_to_disk=True, the looper offloads the base modules (model.norm,
model.rotary_emb, ... ) to disk before the layer loop (module_looper.py:2244-2263) and restores them at the end
through restore_offloaded_modules() -> undo_offload_to_disk(). undo_offload_to_disk() calls
remove_hook_from_submodules(module) + remove_hook_from_module(module), which removes every accelerate hook on the
module, including the GPTQModelAlignDevicesHook that the multi-GPU loader map installed. Nothing re-attaches it.
The weights go back to the right device (model.norm stays on cuda:1), but without its hook the norm no longer
moves its input to cuda:1, so the forward mixes devices. (The replacement lm_head endpoint also comes back without
the old hook, with or without disk offload; that alone does not crash here because it sits on the same device as the
embedding, where the norm hook returns its output.)
Hooks per module, before vs after requantize(embed_quant_mode=QuantizeEmbed.OUTPUT) (2 GPUs, default load):
model.embed_tokens BEFORE (['cuda:0'], GPTQModelAlignDevicesHook, cuda:0) AFTER (['cuda:0'], GPTQModelAlignDevicesHook, cuda:0)
model.norm BEFORE (['cuda:1'], GPTQModelAlignDevicesHook, cuda:1) AFTER (['cuda:1'], None, None)
model.rotary_emb BEFORE (['cuda:1'], GPTQModelAlignDevicesHook, cuda:1) AFTER (['cuda:1'], None, None)
lm_head BEFORE (['cuda:0'], GPTQModelAlignDevicesHook, cuda:0) AFTER (['cuda:0'], None, None)
A stack trace on the hook removal for model.norm points at:
run_exit_recovery.py:120 lambda: self._restore_offloaded(strict=True)
module_looper.py:1928 restore_offloaded_modules(self.gptq_model.model, entries, strict=strict)
offload.py:279 undo_offload_to_disk(module, device=device)
offload.py:1808 remove_hook_from_submodules(module)
Setting model.quantize_config.offload_to_disk = False before requantize() avoids it: the hooks stay, in-session
generate() works, and save -> reload works. With one GPU the default load uses no dispatch hooks, so it is not
affected (generate after requantize works there).
Introduced by
The hook stripping dates from 46ae741d9 / #4295 (restore offloaded base modules after quantization through
undo_offload_to_disk). It was hidden on multi-GPU until aac58e2b3 / #6143, because before that requantize() crashed
earlier in input capture. Still present at HEAD b3532f1d7.
Location
gptqmodel/utils/offload.py:279 (HEAD b3532f1d7), in restore_offloaded_modules:
undo_offload_to_disk(module, device=device)gptqmodel/utils/offload.py:1806-1809, in undo_offload_to_disk:
with torch.inference_mode(False), torch.no_grad():
remove_hook_from_submodules(module) # public API
remove_hook_from_module(module, recurse=False) # ensure root is also cleanOffload of the base modules: gptqmodel/looper/module_looper.py:2244-2263.
Reproduction
Ran on 2x RTX 4090 at HEAD b3532f1d7 (torch 2.15, transformers 5.23, accelerate). The checkpoint is
Qwen/Qwen2.5-0.5B-Instruct quantized with GPTQConfig(bits=4, group_size=128) (64 wikitext rows), saved with
model.save().
# CUDA_VISIBLE_DEVICES=0,1
from datasets import load_dataset
from gptqmodel import CalibrationConfig, GPTQModel, QuantizeEmbed
model = GPTQModel.load("Qwen2.5-0.5B-Instruct-gptq-4bit") # default load -> "Built map across 2 GPU(s)"
print(model.generate("The capital of France is", max_new_tokens=8, do_sample=False)[0]) # works
texts = [t for t in load_dataset("Salesforce/wikitext", "wikitext-2-raw-v1", split="train")["text"] if len(t) > 200][:16]
model.requantize(CalibrationConfig(dataset=texts), embed_quant_mode=QuantizeEmbed.OUTPUT) # completes now
model.generate("The capital of France is", max_new_tokens=8, do_sample=False) # RuntimeError (above)Results:
QuantizeEmbed.OUTPUTandQuantizeEmbed.BOTH, default load: requantize completes, generate raises the device error.- Same with
GPTQModel.load(path, device_map="auto")andGPTQModel.load(path, device=(0, 1)). - Same script with
model.quantize_config.offload_to_disk = Falsebeforerequantize(): generate works for OUTPUT and
BOTH, and the saved checkpoint reloads and generates. - Same script with
CUDA_VISIBLE_DEVICES=0: works.
Expected vs actual
Expected (the #6132 / #5116 contract): after requantize() the in-session model runs on the devices it was loaded on.
Actual: on a multi-GPU dispatched model the default offload_to_disk=True restore removes the dispatch hooks of the
non-layer modules, and the first forward fails.
Suggested fix
Keep the dispatch hook across the offload round trip: in capture_offloaded_base_modules() record the module's
non-offload AlignDevicesHook (or its execution_device), and after undo_offload_to_disk() in
restore_offloaded_modules() re-attach it with add_hook_to_module(module, hook). Alternatively, skip disk-offloading
base modules that carry a resident (non-offload) dispatch hook, as #6143 already does for input capture
(_dispatch_execution_device). The same applies to the new endpoint modules that replace a hooked lm_head /
embed_tokens should get the replaced module's hook. A regression test: dispatch a tiny model over two devices (or
two hooks with different execution devices on CPU/meta), run requantize(), and assert that model.norm,
rotary_emb and lm_head still carry their hooks.
Activity
fix(offload): keep loader dispatch hooks and release skipped modules/…
tests: pin file-backed offload instead of meta + hook offload (#6471)
Remove accelerate: explicit WeightLoader for weight loading, placemen…