-
Notifications
You must be signed in to change notification settings - Fork 6.2k
enable compilation in qwen image. #12061
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
base: main
Are you sure you want to change the base?
Conversation
if self.pos_freqs.device != device: | ||
self.pos_freqs = self.pos_freqs.to(device) | ||
self.neg_freqs = self.neg_freqs.to(device) | ||
|
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.
Recompilation trigger one.
if isinstance(video_fhw, list): | ||
video_fhw = video_fhw[0] | ||
frame, height, width = video_fhw | ||
rope_key = f"{frame}_{height}_{width}" | ||
|
||
if rope_key not in self.rope_cache: |
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.
Recompilation trigger two.
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.
Is this again because there is something special happening on the first run?
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.
The forward
method has a side effect with the dict caching method. It modifies the self.rope_cache
dictionary.
if self.model_class.__name__ == "QwenImageTransformer2DModel": | ||
pytest.skip( | ||
"QwenImageTransformer2DModel doesn't support group offloading with disk. Needs to be investigated." | ||
) | ||
|
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.
Will investigate in a follow-up.
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. |
@sayakpaul Could you rebase with main? Sorry I didn't see this before stacked over tests/qwen-image |
Done! |
@@ -236,6 +223,25 @@ def forward(self, video_fhw, txt_seq_lens, device): | |||
|
|||
return vid_freqs, txt_freqs | |||
|
|||
@functools.lru_cache(maxsize=None) | |||
def _compute_video_freqs(self, frame, height, width): |
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.
TODO: we need to remove frame (can be done in future PR)
@@ -236,6 +223,25 @@ def forward(self, video_fhw, txt_seq_lens, device): | |||
|
|||
return vid_freqs, txt_freqs | |||
|
|||
@functools.lru_cache(maxsize=None) |
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.
Let's remove the self.rope_cache
and just use the lru_cache implementation? WDYT @yiyixuxu?
WDYT about maybe putting maxsize=128 or something here so that long running services that use diffusers don't accidentally die with OOM (probably very unlikely though) @sayakpaul?
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.
maxsize=128
sounds reasonable to me.
@@ -179,6 +180,8 @@ def __init__(self, theta: int, axes_dim: List[int], scale_rope=False): | |||
dim=1, | |||
) | |||
self.rope_cache = {} | |||
self.register_buffer("pos_freqs", pos_freqs, persistent=False) |
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.
This is most likely not equivalent. When registered as buffer, if the model is loaded in bf16, the precision of these will bf16 instead of fp32. Doing RoPE in bf16 may harm image quality, so we need to be careful here. Not sure what's best to do here -- maybe for now we can put the rope layer in _keep_modules_in_fp32
?
This recompilation related problem seems to have become too common with RoPE. Maybe we need to rethink the design a bit.
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.
Just for the record, sharing the recompilation error we get without the buffer implementation:
> raise exc.RecompileError(message)
E torch._dynamo.exc.RecompileError: Recompiling function forward in /fsx/sayak/diffusers/src/diffusers/models/transformers/transformer_qwenimage.py:529
E triggered by the following guard failure(s):
E - 0/0: tensor 'self._modules['pos_embed'].neg_freqs' dispatch key set mismatch. expected DispatchKeySet(CPU, BackendSelect, ADInplaceOrView, AutogradCPU), actual DispatchKeySet(CUDA, BackendSelect, ADInplaceOrView, AutogradCUDA)
../miniconda3/envs/diffusers/lib/python3.10/site-packages/torch/_dynamo/guards.py:3822: RecompileError
But I agree with your first point.
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.
The recompilation message here says that on the first compilation 'self._modules['pos_embed'].neg_freqs'
was a CPU tensor, and on second it became a CUDA tensor. Does that match your expectation? If yes, is it possible to change that somehow. If there is something special happening on the first invocation, you can put compile on the second invocation onwards.
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.
If there is something special happening on the first invocation, you can put compile on the second invocation onwards.
Is, IMO, an involved user-experience we should probably avoid.
@a-r-r-o-w I dug deeper into the RoPE embed stuff you brought up in #12061 (comment). Summary below. I am using the code from the OP for all investigations with the change of First, I printed Then, I moved on to qualitative comparisons. Result is below: I think since the buffers we're registering in this PR aren't persistent, we should be okay because LMK your thoughts. |
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.
Trying to understand if there is something special happening on the first run, in which case, perhaps enabling compile from the second invocation onwards might be a better way.
@@ -179,6 +180,8 @@ def __init__(self, theta: int, axes_dim: List[int], scale_rope=False): | |||
dim=1, | |||
) | |||
self.rope_cache = {} | |||
self.register_buffer("pos_freqs", pos_freqs, persistent=False) |
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.
The recompilation message here says that on the first compilation 'self._modules['pos_embed'].neg_freqs'
was a CPU tensor, and on second it became a CUDA tensor. Does that match your expectation? If yes, is it possible to change that somehow. If there is something special happening on the first invocation, you can put compile on the second invocation onwards.
if isinstance(video_fhw, list): | ||
video_fhw = video_fhw[0] | ||
frame, height, width = video_fhw | ||
rope_key = f"{frame}_{height}_{width}" | ||
|
||
if rope_key not in self.rope_cache: |
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.
Is this again because there is something special happening on the first run?
Posting here a summary of what @anijain2305 and I discussed offline. Regional compilation is sweet!FWIW, So, to test this, I just added However, full compilation is needed when catering to LoRA hotswapping use cases as LoRA target modules tend to also target non-transformer blocks (the ones not in Full compilation modificationsThere are multiple ways to tackle this. The changes introduced in this PR are just one way to resolve the issues. One can enforce eager execution on the first invocation of the transformer and then fall back to default compilation. However, this is a bit of a shame in terms of user-experience. Perhaps we can document all of this in the Qwen pipeline doc page? WDYT? Cc: @a-r-r-o-w @yiyixuxu |
Ah I see now that the result returned from I remember that it was significantly slower to run RoPE computation with complex numbers, and that inductor does not support optimizations when complex numbers are involved (I don't remember the exact problem, but there is definitely some speedup to be gained by removing complex number use). Not for this PR, but this should be refactored like what was done for Wan |
What does this PR do?
Timing (compilation) gathered from an H100:
timings.mean()=tensor(42.8954)
main
:timings.mean()=tensor(75.9385)
Code