[core] Tensor parallelism for Qwen-Image-2.1 - #14865
Open
JingyaHuang wants to merge 4 commits into
Open
JingyaHuang wants to merge 4 commits into
JingyaHuang wants to merge 4 commits into
Conversation
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
JingyaHuang
force-pushed
the
add-qwen-image-21-tp-plan
branch
from
September 25, 2026 14:16
cc669f0 to
12e2b9c
Compare
JingyaHuang
marked this pull request as ready for review
September 25, 2026 14:17
sayakpaul
reviewed
Sep 30, 2026
sayakpaul
left a comment
Member
There was a problem hiding this comment.
Left some comments on the Qwen specific stuff.
| _ROPE_ANGLE_DEVICES = ("neuron",) | ||
| ROPE_PER_DEVICE = { | ||
| "cuda": functools.partial(apply_rotary_emb_qwen, use_real=False), | ||
| **dict.fromkeys(_ROPE_ANGLE_DEVICES, apply_rotary_emb_qwen_neuron), |
Member
There was a problem hiding this comment.
I don't think we need this kind of dict munging. Let's just do: "neuron": apply_rotary_emb_qwen_neuron.
| self, img_shapes: list[tuple[int, int, int]], image_pad_mask: torch.Tensor, device: torch.device | ||
| ) -> torch.Tensor: | ||
| self.freqs = [freq.to(device) for freq in self.freqs] | ||
| freqs = self._get_device_freqs(torch.device(device)) |
Member
There was a problem hiding this comment.
Maybe we lru_cache this? Diff below:
diff --git a/src/diffusers/models/transformers/transformer_qwenimage21.py b/src/diffusers/models/transformers/transformer_qwenimage21.py
--- a/src/diffusers/models/transformers/transformer_qwenimage21.py
+++ b/src/diffusers/models/transformers/transformer_qwenimage21.py
@@ -710,24 +710,19 @@
torch.cat([self.rope_params(pos_index, dim, theta), self.rope_params(neg_index, dim, theta)], dim=0)
for dim in axes_dim
]
- # Per-device copies of `freqs`, kept on the instance so they are freed with the model. A class-level
- # `lru_cache` would key on `self` and keep every instance's device freqs alive for the life of the process.
- self._device_freqs: dict[torch.device, list[torch.Tensor]] = {}
def rope_params(self, index: torch.Tensor, dim: int, theta: int = 10000) -> torch.Tensor:
freqs = torch.outer(index, 1.0 / torch.pow(theta, torch.arange(0, dim, 2).to(torch.float32).div(dim)))
return torch.polar(torch.ones_like(freqs), freqs)
+ @functools.lru_cache(maxsize=128)
def _get_device_freqs(self, device: torch.device) -> list[torch.Tensor]:
"""Return the per-axis freqs on `device`: complex exponentials, or rotation angles where complex is missing."""
- if device not in self._device_freqs:
- if device.type in _ROPE_ANGLE_DEVICES:
- # `torch.angle` runs on CPU while the freqs are still complex; wrapping into (-pi, pi] is harmless
- # because only cos/sin of the angle are used.
- self._device_freqs[device] = [torch.angle(freq).to(device) for freq in self.freqs]
- else:
- self._device_freqs[device] = [freq.to(device) for freq in self.freqs]
- return self._device_freqs[device]
+ if device.type in _ROPE_ANGLE_DEVICES:
+ # `torch.angle` runs on CPU while the freqs are still complex; wrapping into (-pi, pi] is harmless
+ # because only cos/sin of the angle are used.
+ return [torch.angle(freq).to(device) for freq in self.freqs]
+ return [freq.to(device) for freq in self.freqs]
def forward(
self, img_shapes: list[tuple[int, int, int]], image_pad_mask: torch.Tensor, device: torch.device
Comment on lines
-836
to
907
| block_ids = torch.repeat_interleave( | ||
| torch.arange(len(block_lengths), device=image_pad_mask.device), | ||
| torch.tensor(block_lengths, device=image_pad_mask.device), | ||
| # Built from the Python block lengths rather than with a tensor-repeats `repeat_interleave`, whose | ||
| # data-dependent output size some compiled backends (e.g. Neuron) cannot lower. | ||
| block_ids = torch.tensor( | ||
| [block for block, length in enumerate(block_lengths) for _ in range(length)], device=image_pad_mask.device | ||
| ) |
Member
There was a problem hiding this comment.
Let's keep it explicitly conditioned on neuron then. @DN6 WDYT?
TorchTPU reports TPU tensors as "tpu" and supports complex dtypes, so only Neuron needs the angle-based RoPE path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
JingyaHuang
force-pushed
the
add-qwen-image-21-tp-plan
branch
from
October 2, 2026 16:15
fb74251 to
f679fe5
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Adds tensor-parallel support to
QwenImage21Transformer2DModel.(Builds on #14544 sharded checkpoint loading, merge that one first.)
Changes in
transformer_qwenimage21.py:_tp_plan: attentionto_q/to_k/to_vand SwiGLUproj/gate_layerare colwise;to_out.0andimg_mlp.outare rowwise. The sharedmodulation, input projections,norm_outandproj_outstay replicated.head_diminstead ofattn.heads, so it still works when each rank only holds part of the heads (same fix as Qwen-Image).build_token_metadatabuilds the block ids from a Python list instead of a tensor-repeatsrepeat_interleave, which Neuron can't compile. This one was needed for 2.1 to run on Neuron at all, TP or not.Run with
torchrun --nproc-per-node 8 qwen21_tp.py.Validation
Before submitting
self-reviewskill on the diff?documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.