Commit 302a7ad
[NVBug: 6524370] use sequential device_map for DiffusionGemma (#2041)
### What does this PR do?
Type of change: Bug fix
Fixes NVBug 6524370.
`DiffusionGemma` ties weights between its encoder and decoder.
`get_model` loads with `device_map="auto"`
(`examples/hf_ptq/example_utils.py`), and `"auto"` is an alias for
`"balanced"` — accelerate splits the model evenly across all visible
GPUs by size, with no awareness of tied parameters. On multi-GPU it can
place the two sides of a tied pair on different devices; the tie then
cannot be honored and one side is left on the `meta` device.
The pre-quantization preview in `pre_quantize` then reaches `(input_ids
== self.config.image_token_id).any()` in `generation_diffusion_gemma.py`
and fails:
```
RuntimeError: Tensor.item() cannot be called on meta tensors
```
This is multi-GPU-only by construction: with one visible GPU the
balanced split is trivial, nothing is separated, and nothing lands on
`meta`.
This PR detects DiffusionGemma configs in `get_model` and selects
`device_map="sequential"`, which fills one GPU before spilling to the
next and so keeps tied modules together. It mirrors the existing
per-model handling for `bart` and `t5`, where `device_map="auto"`
similarly mis-shards tied encoder/decoder weights.
Detection reads `model_type` and `architectures` from the config and
ignores underscores, since the family is spelled `diffusion_gemma` in
the Transformers module path and `DiffusionGemma` in the class name.
### Usage
No API change. Previously this needed the flag passed manually:
```bash
python hf_ptq.py --model <diffusion-gemma-ckpt> --recipe <recipe> \
--export_path <out> --trust_remote_code --use_seq_device_map
```
It is now selected automatically, and the model load logs:
```
Detected DiffusionGemma model. Using device_map='sequential'; the balanced
'auto' mapping can split its tied encoder/decoder weights across GPUs.
```
Passing `--use_seq_device_map` explicitly still works and is unaffected.
### Testing
- Reproduced on 4x GB200 with `diffusiongemma-26B-A4B-it` and the
`nvfp4_experts_only` recipe; `--use_seq_device_map` resolves the crash,
confirming the device-mapping cause.
- Validated on oci-hsg (4x GB200): with this patch and no CLI flag,
`diffusiongemma-26B-A4B-it` loads correctly and the meta-tensor crash no
longer reproduces.
- `is_diffusion_gemma` checked against both config spellings,
`architectures=None`, `architectures=[]`, and a `gemma3` negative to
confirm no over-match — `get_model_type` already orders `DiffusionGemma`
before `Gemma` for exactly this substring-collision reason.
- `pre-commit run --files examples/hf_ptq/example_utils.py` passes
(ruff, ruff-format, mypy, bandit).
### Before your PR is "*Ready for review*"
- Is this change backward compatible?: ✅
- If you copied code from any other sources or added a new PIP
dependency, did you follow guidance in `CONTRIBUTING.md`: N/A
- Did you write any new necessary tests?: ❌ — no existing unit coverage
for `get_model` device-map selection; happy to add a config-level test
for `is_diffusion_gemma` if wanted.
- Did you update
[Changelog](https://github.com/NVIDIA/Model-Optimizer/blob/main/CHANGELOG.rst)?:
N/A
- Did you get Claude approval on this PR?: ❌ — pending
### Additional Information
NVBug 6524370. Same class of failure as the existing `t5` workaround in
`get_model`; a general "any tied encoder/decoder model" rule was
considered but rejected, since `tie_word_embeddings=True` holds for most
decoder-only LLMs where `auto` is fine and forcing sequential would
regress large-model runs.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Bug Fixes**
* Improved DiffusionGemma model loading on multi-GPU systems by keeping
related model weights together.
* Added more reliable DiffusionGemma model recognition across supported
configurations.
* Preserved existing automatic device allocation for single-GPU systems
and other supported models.
* Improved loading reliability by applying appropriate memory limits
during multi-GPU setup.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Signed-off-by: Juhi Mittal <juhim@nvidia.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>1 parent 14b20c0 commit 302a7ad
2 files changed
Lines changed: 130 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
304 | 304 | | |
305 | 305 | | |
306 | 306 | | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
307 | 321 | | |
308 | 322 | | |
309 | 323 | | |
| |||
696 | 710 | | |
697 | 711 | | |
698 | 712 | | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
699 | 726 | | |
700 | 727 | | |
701 | 728 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
316 | 316 | | |
317 | 317 | | |
318 | 318 | | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
0 commit comments