Skip to content

[MiniMax-H3] Call the Qwen3-VL text encoder top-level forward so offload hooks fire - #15395

Open
Shan2L wants to merge 1 commit into
huggingface:mainfrom
Shan2L:rose/h3-te-toplevel-forward
Open

Shan2L wants to merge 1 commit into
huggingface:mainfrom
Shan2L:rose/h3-te-toplevel-forward

Conversation

@Shan2L

@Shan2L Shan2L commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #15399

get_qwen3vl_prompt_embeds (MiniMax-H3, MiniMax-H3-Image, and its copy in Pika-H3-Flex) calls text_encoder.model(...) and fires accelerate's _hf_hook.pre_forward by hand. The code carries a TODO saying a block should not do this. As a result, hooks that only wrap the top-level forward never fire. With diffusers group offloading on the text encoder, modules that are not in a block (the token embedding) stay on the CPU:

RuntimeError: Expected all tensors to be on the same device, but got index is on cuda:0, different from other tensors on cpu

This PR calls text_encoder(...) with logits_to_keep=1, so the language-model head projects only one token, and it drops the manual hook call. utils/check_copies.py --fix_and_overwrite propagated the change to the Pika-H3-Flex copy.

Results

  • hidden_states[50] is bitwise identical between the old and the new call. Setup: same process, a MiniMax-H3-Image prompt, leaf-level group offloading on one 32 GB Radeon AI PRO R9600D. Encoding takes 2.36 s with the old call and 2.44 s with the new one.
  • Tests: tests/modular_pipelines/minimax_h3 + tests/modular_pipelines/minimax_h3_image on a Radeon AI PRO R9700: 142 passed, 52 skipped; tests/modular_pipelines/pika_h3_flex: 81 passed, 41 skipped (at e3f8451)

This change makes the following setup work on a single 32 GB GPU: transformer block_level + text encoder leaf_level group offloading (use_stream=True, record_stream=True), with the VAE on the GPU. It takes 31.7 s per 2048x2048 image, with 6.2 GiB peak. The documented ComponentsManager auto offload moves the ~62 GB text encoder as a whole and does not fit there.

Self-review notes (diffusers self-review skill)

  • Blocking: none. Non-blocking: none.
  • Dead code: none. get_qwen3vl_prompt_embeds is used by the H3, H3-Image and Pika-H3-Flex encoder blocks.
  • check_copies.py and ruff 0.9.10 (check + format) pass.
  • Verdict: READY.

Before submitting

  • Did you use an AI agent (Claude Code, Codex, Cursor, etc.) to help with this PR? If so:
    • Did you read the Coding with AI agents guide?
    • Did you run the self-review skill on the diff?
    • Did you share the final self-review notes in the PR description or a comment?
  • Did you read the contributor guideline?
  • Did you read our philosophy doc? (important for complex PRs)
  • Was this discussed/approved via a GitHub issue or GitHub Discussions?
  • Did you make sure to update the documentation with your changes? (no API change)
  • Did you write any new necessary tests? (behaviour change only: same outputs, hooks now fire; existing modular pipeline tests cover the call)
  • Are you the author (or part of the team we work with) of the model/pipeline this PR relates to?

Who can review?

@yiyixuxu @DN6

AI assistance: prepared with the help of an AI coding assistant (GitHub Copilot). All results above were run on the listed hardware.

…oad hooks fire

get_qwen3vl_prompt_embeds called text_encoder.model(...) and fired accelerate's
_hf_hook by hand, so hooks that only run on the top-level forward (diffusers group
offloading of the unmatched modules, for example) never ran and left the embedding
on the CPU. Call text_encoder(...) with logits_to_keep=1 instead: hidden_states[50]
is bitwise identical, the language-model head only sees one token.

Signed-off-by: Sylvan Liu <Sylvan.Liu@amd.com>
@github-actions github-actions Bot added modular-pipelines size/M PR with diff < 200 LOC labels Nov 25, 2026
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Hi @Shan2L, thanks for the PR! It does not appear to link an issue it fixes.

  • If this PR addresses an existing issue, please add a closing keyword (e.g. Fixes #1234) to the PR description so the issue is linked.
  • If you are the author of the model/pipeline this PR relates to, or part of the team we work with on it, tick the Are you the author (or part of the team we work with) of the model/pipeline this PR relates to? box in the PR description (if you removed the template, paste that line back, checked). This reminder then no longer applies.
  • Otherwise there is nothing you need to do: a maintainer will look at the PR and add the no-issue-needed label to silence this reminder if it should stay open without an issue.

See the contribution guide for more details.

Please note that PRs without a linked issue are likely to be automatically closed 10 days after this notice.

Once the PR links an existing issue, the author box is ticked, or the no-issue-needed label is added, you can ignore this message — it stays here as a comment, but it no longer applies.

@yiyixuxu yiyixuxu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks a lot!

@yiyixuxu

Copy link
Copy Markdown
Collaborator

@Shan2L
thanks a lot for your PR!
in the future, could you open issue before the PR fix, this way you won't be getting the bot message like this one #15395 (comment)

@Shan2L

Shan2L commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @yiyixuxu! Understood, I will open an issue first for future fixes.

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

modular-pipelines size/M PR with diff < 200 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MiniMax-H3] Block-level group offloading of the Qwen3-VL text encoder fails: the encoder step bypasses its top-level forward

2 participants