-
Notifications
You must be signed in to change notification settings - Fork 7.4k
[core] Tensor parallelism for Qwen-Image-2.1 #14865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JingyaHuang
wants to merge
4
commits into
huggingface:main
Choose a base branch
from
JingyaHuang:add-qwen-image-21-tp-plan
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import functools | ||
| import math | ||
| from typing import Any | ||
|
|
||
|
|
@@ -133,6 +134,42 @@ def apply_rotary_emb_qwen( | |
| return x_out.type_as(x) | ||
|
|
||
|
|
||
| # Copied from diffusers.models.transformers.transformer_qwenimage.apply_rotary_emb_qwen_neuron | ||
| def apply_rotary_emb_qwen_neuron(x: torch.Tensor, freqs: torch.Tensor) -> torch.Tensor: | ||
| """ | ||
| Apply rotary embeddings to `x` using real-valued cos/sin, for backends without a complex dtype. | ||
|
|
||
| Numerically equivalent to `apply_rotary_emb_qwen(..., use_real=False)`, which multiplies `x` by a complex | ||
| exponential. Neuron has no complex tensor support, so the rotation angles are carried as reals and cos/sin are | ||
| taken here instead. | ||
|
|
||
| Args: | ||
| x (`torch.Tensor`): Query or key tensor to rotate, shape `[B, S, H, D]`. | ||
| freqs (`torch.Tensor`): Rotation angles, shape `[S, D // 2]`. | ||
|
|
||
| Returns: | ||
| `torch.Tensor`: `x` with rotary embeddings applied. | ||
| """ | ||
| # Adjacent feature pairs (2k, 2k+1) share angle k, so each angle is repeated twice along the last dim; unsqueeze | ||
| # the head axis so the freqs broadcast over heads (this is what keeps it tensor-parallel-agnostic). | ||
| cos = torch.cos(freqs).repeat_interleave(2, dim=-1).unsqueeze(1) # [S, 1, D] | ||
| sin = torch.sin(freqs).repeat_interleave(2, dim=-1).unsqueeze(1) # [S, 1, D] | ||
| x_real, x_imag = x.reshape(*x.shape[:-1], -1, 2).unbind(-1) # [B, S, H, D//2] | ||
| x_rotated = torch.stack([-x_imag, x_real], dim=-1).flatten(3) # [B, S, H, D] | ||
| return (x.float() * cos + x_rotated.float() * sin).to(x.dtype) | ||
|
|
||
|
|
||
| # RoPE application is backend-dependent: the default path multiplies by a complex exponential, which Neuron cannot | ||
| # represent. On those backends `QwenImage21Rope` hands out rotation angles instead of complex freqs, and | ||
| # `apply_rotary_emb_qwen_neuron` takes cos/sin on device. Callers select by `device.type`. Every other backend (CUDA, | ||
| # CPU, TPU, …) uses the default complex path; only backends without complex dtypes are listed here. | ||
| _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), | ||
| } | ||
|
|
||
|
|
||
| class QwenImage21TemporalTimesteps(nn.Module): | ||
| r"""Sinusoidal timestep embedding. `cos` occupies the first half of the channels and `sin` the second.""" | ||
|
|
||
|
|
@@ -337,16 +374,20 @@ def _qwenimage21_prepare_qkv( | |
| key = attn.to_k(hidden_states) | ||
| value = attn.to_v(hidden_states) | ||
|
|
||
| query = query.unflatten(-1, (attn.heads, -1)) | ||
| key = key.unflatten(-1, (attn.heads, -1)) | ||
| value = value.unflatten(-1, (attn.heads, -1)) | ||
| # Split by `head_dim` rather than by `attn.heads`: under tensor parallelism each rank holds only its share of the | ||
| # heads, while `attn.heads` and `attn.inner_dim` keep their full values. | ||
| head_dim = attn.inner_dim // attn.heads | ||
| query = query.unflatten(-1, (-1, head_dim)) | ||
| key = key.unflatten(-1, (-1, head_dim)) | ||
| value = value.unflatten(-1, (-1, head_dim)) | ||
|
|
||
| query = attn.norm_q(query).to(value.dtype) | ||
| key = attn.norm_k(key).to(value.dtype) | ||
|
|
||
| if rotary_emb is not None: | ||
| query = apply_rotary_emb_qwen(query, rotary_emb, use_real=False) | ||
| key = apply_rotary_emb_qwen(key, rotary_emb, use_real=False) | ||
| apply_rope = ROPE_PER_DEVICE.get(query.device.type, ROPE_PER_DEVICE["cuda"]) | ||
| query = apply_rope(query, rotary_emb) | ||
| key = apply_rope(key, rotary_emb) | ||
|
|
||
| if layer_cache is not None: | ||
| if kv_cache_mode == "extract" and cache_write_slice is not None: | ||
|
|
@@ -669,15 +710,29 @@ def __init__(self, theta: int, axes_dim: list[int]): | |
| 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) | ||
|
|
||
| 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] | ||
|
|
||
| def forward( | ||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we 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 |
||
|
|
||
| frame_index, height_index, width_index = [], [], [] | ||
| image_height_index, image_width_index = [], [] | ||
|
|
@@ -707,7 +762,7 @@ def forward( | |
| height_index[image_pad_mask] = torch.tensor(image_height_index, dtype=torch.long, device=device) | ||
| width_index[image_pad_mask] = torch.tensor(image_width_index, dtype=torch.long, device=device) | ||
|
|
||
| return torch.cat([self.freqs[0][frame_index], self.freqs[1][height_index], self.freqs[2][width_index]], dim=-1) | ||
| return torch.cat([freqs[0][frame_index], freqs[1][height_index], freqs[2][width_index]], dim=-1) | ||
|
|
||
|
|
||
| class QwenImage21Transformer2DModel( | ||
|
|
@@ -758,6 +813,18 @@ class QwenImage21Transformer2DModel( | |
| _skip_layerwise_casting_patterns = ["pos_embed", "norm"] | ||
| _repeated_blocks = ["QwenImage21TransformerBlock"] | ||
| _skip_keys = ["kv_cache"] | ||
| # Tensor-parallel plan: every block's attention and SwiGLU projections are separate, bias-free Linears, so each | ||
| # entry is a plain "colwise"/"rowwise" pair and no packed sharding is needed. The shared `modulation`, the input | ||
| # projections (`img_in`, `txt_in`), `norm_out` and `proj_out` stay replicated (intentionally absent here). | ||
| _tp_plan = { | ||
| "transformer_blocks.*.attn.to_q": "colwise", | ||
| "transformer_blocks.*.attn.to_k": "colwise", | ||
| "transformer_blocks.*.attn.to_v": "colwise", | ||
| "transformer_blocks.*.attn.to_out.0": "rowwise", | ||
| "transformer_blocks.*.img_mlp.proj": "colwise", | ||
| "transformer_blocks.*.img_mlp.gate_layer": "colwise", | ||
| "transformer_blocks.*.img_mlp.out": "rowwise", | ||
| } | ||
|
|
||
| @register_to_config | ||
| def __init__( | ||
|
|
@@ -833,9 +900,10 @@ def build_token_metadata( | |
| ) | ||
|
|
||
| image_ids = torch.full_like(image_pad_mask, -1, dtype=torch.long) | ||
| 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 | ||
| ) | ||
|
Comment on lines
-836
to
907
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's keep it explicitly conditioned on neuron then. @DN6 WDYT? |
||
| image_ids[image_positions] = block_ids | ||
|
|
||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need this kind of dict munging. Let's just do:
"neuron": apply_rotary_emb_qwen_neuron.