Skip to content

Commit 7929ddd

Browse files
juhi10071998claude
andcommitted
fix(hf_ptq): use sequential device_map for DiffusionGemma
DiffusionGemma ties weights between its encoder and decoder. Loading it with device_map="auto" (balanced) can place the two sides of a tied pair on different GPUs; the tie cannot then be honored and one side is left on the meta device, so the pre-quantization preview fails with: RuntimeError: Tensor.item() cannot be called on meta tensors Detect DiffusionGemma configs in get_model and select device_map="sequential", which keeps tied modules together. This mirrors the existing per-model handling for bart and t5, where device_map="auto" similarly mis-shards tied encoder/decoder weights. Multi-GPU only; single-GPU runs were unaffected. Previously this required passing --use_seq_device_map manually. Fixes NVBug 6524370 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Juhi Mittal <juhim@nvidia.com>
1 parent a23390d commit 7929ddd

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

examples/hf_ptq/example_utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,17 @@ def is_speculative(hf_config):
304304
)
305305

306306

307+
def is_diffusion_gemma(hf_config) -> bool:
308+
"""Check if the model architecture is DiffusionGemma.
309+
310+
Matches on both ``model_type`` and ``architectures`` and ignores underscores,
311+
since the family is spelled ``diffusion_gemma`` in some places and
312+
``DiffusionGemma`` in others.
313+
"""
314+
names = [getattr(hf_config, "model_type", None) or "", *(hf_config.architectures or [])]
315+
return any("diffusiongemma" in name.lower().replace("_", "") for name in names)
316+
317+
307318
def get_tokenizer(ckpt_path, trust_remote_code=False, **kwargs) -> PreTrainedTokenizerBase:
308319
print(f"Initializing tokenizer from {ckpt_path}")
309320

@@ -696,6 +707,18 @@ def get_model(
696707
model_kwargs = config_kwargs.copy()
697708
model_kwargs.setdefault("dtype", "auto")
698709

710+
# DiffusionGemma ties weights between its encoder and decoder. device_map "auto"
711+
# (balanced) can place the two sides of a tied pair on different GPUs; the tie then
712+
# cannot be honored and one side is left on the meta device, so generation dies with
713+
# "Tensor.item() cannot be called on meta tensors". Sequential mapping keeps tied
714+
# modules together. Same class of failure as the T5 case handled below.
715+
if device != "cpu" and is_diffusion_gemma(hf_config):
716+
print(
717+
"Detected DiffusionGemma model. Using device_map='sequential'; the balanced "
718+
"'auto' mapping can split its tied encoder/decoder weights across GPUs."
719+
)
720+
use_seq_device_map = True
721+
699722
if use_seq_device_map:
700723
device_map = "sequential"
701724
# If we use sequential, set max_memory limit to ensure that the model does not occupy the full GPU

0 commit comments

Comments
 (0)