Skip to content

[GLM-Image] Fix image-to-image with enable_model_cpu_offload() - #15456

Draft
chsu-amd wants to merge 1 commit into
huggingface:mainfrom
chsu-amd:fix-glm-image-i2i-model-offload
Draft

chsu-amd wants to merge 1 commit into
huggingface:mainfrom
chsu-amd:fix-glm-image-i2i-model-offload

Conversation

@chsu-amd

@chsu-amd chsu-amd commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #15449

generate_prior_tokens calls vision_language_encoder.get_image_features(...) directly for condition images. enable_model_cpu_offload() hooks only forward, so the encoder is still on cpu and image-to-image fails.

This PR fires the encoder's accelerate hook before that call, as modular_pipelines/minimax_h3/encoders.py does. That code has a TODO against it, and the open PR #15395 replaces it with a top-level forward call, which is not possible here: GlmImageForConditionalGeneration.forward does not return the condition-image tokens.

Alternative: move the condition-image block after vision_language_encoder.generate(), whose forward has already moved the encoder. With transformers 5.16.1 it gives the same output (byte-identical PNG on the AMD Radeon 8060S), but I found no precedent for fixing offload by reordering. Happy to switch if preferred.

#15453 makes the same change without the hasattr check.

Results

main at facc812 + this diff, accelerate 1.9.0, transformers 5.16.1. On main the #15449 snippet raises the device-mismatch error on both GPUs.

NVIDIA GeForce RTX 5090 (torch 2.14.1+cu130) AMD Radeon 8060S, gfx1151 (torch 2.15.0a0+rocm10.1)
#15449 snippet, released weights completes, 1024x1056, 120.4 s completes, 1024x1056
peak GPU memory 20604 MiB (nvidia-smi, whole GPU) 19605 MiB (max_memory_allocated)
tiny components*, image-to-image + model offload vs main on GPU max abs diff 0.0 max abs diff 0.0

* test components with vision patch_size 16 / image_size 64 to match the processor. Unchanged vs main (diff 0.0): text-to-image on GPU and with model offload, image-to-image on GPU. The model card text-to-image example with model offload gives a PNG byte-identical to main (130.1 s, peak 20344 MiB), and pytest tests/pipelines/glm_image gives 35 passed, 2 skipped on main and on this branch (NVIDIA GeForce RTX 5090).

Outside the reported case:

  • The added lines do not run without condition images, with prior_token_ids given, or without an accelerate hook on the encoder (group offloading uses other hooks).
  • Other accelerate hooks on the encoder get one extra pre_forward(module) call per image-to-image call: no output change for device_map="balanced" / force_hooks=True; nothing moves if the encoder is already on the device. Not measured: a custom ModelHook whose pre_forward needs inputs (would raise), cpu_offload_with_hook on the encoder only (a device mismatch if the execution device is cpu), another model_cpu_offload_seq, multi-device maps, several condition images, batch > 1.

Self-review notes (self-review skill): blocking none. hasattr(hook, "pre_forward") is always true, kept to match apply_forward_hook. No test: MemoryTesterMixin uses text-to-image inputs and the test's tiny components cannot run image-to-image (vision patch_size 8, processor 16). ruff check / format pass.

Before submitting

Who can review?

Draft; reviewers will be tagged when it is ready.

@github-actions github-actions Bot added pipelines fixes-issue size/S PR with diff < 50 LOC labels Dec 2, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GlmImagePipeline: image-to-image fails with enable_model_cpu_offload() (device mismatch in get_image_features)

1 participant