Remove compile bottlenecks from ZImage pipeline#13461
Open
hitchhiker3010 wants to merge 3 commits intohuggingface:mainfrom
Open
Remove compile bottlenecks from ZImage pipeline#13461hitchhiker3010 wants to merge 3 commits intohuggingface:mainfrom
hitchhiker3010 wants to merge 3 commits intohuggingface:mainfrom
Conversation
Member
|
Thanks for your PR! Can we eliminate all the |
…former Boolean mask indexing (tensor[mask] = val) implicitly calls nonzero(), which triggers a DtoH sync that stalls the CPU while the GPU queue drains. Replacing it with torch.where eliminates these syncs from the transformer's pad-token assignment. Profiling (4-step turbo, fix_2 vs fix_1): - Eager: nonzero CPU time drops from ~2091 ms to <1 ms; index_put eliminated - Compile: nonzero CPU time drops from ~3057 ms to <1 ms; index_put eliminated
Author
|
Here are some comparison stats between commit_1 and commit_2
|
Author
|
all the trace files can be accessed here. The cc: @sayakpaul |
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?
Fixes performance issues identified by profiling
ZImagePipelinewithtorch.profileras part of #13401 .What does this PR do?
Profiled
ZImagePipeline(usingTongyi-MAI/Z-Image-Turbo) in both eager andtorch.compilemodes following the profiling guide. The Chrome traces revealed two device-to-host (DtoH) synchronization points that break asynchronous GPU execution and preventtorch.compilefrom yielding its full speedup.Pipeline denoising loop:
t_norm = timestep[0].item()DtoH synctimestep[0].item()triggers a GPU→CPU sync every step to readt_normfor CFG truncation logic. Since the full timestep schedule is known before the loop begins, we precompute allt_normvalues into a plain Python list before entering the loop and index into it withi.scheduler.set_begin_index(0)upfront to avoid the DtoH sync in_init_step_index(same pattern as Avoid DtoH sync from access of nonzero() item in scheduler #11696 )Profiling ZImagePipeline
GPU - L4
num_inference_steps - 4,
guidance_scale - 0.0 ( Guidance should be 0 for the Turbo models)
Before

The first scheduler_step took 657.8µs
Number of cudaStreamSynchronize blocks - 19
After

The first scheduler_step took 15.49 µs after this fix
Number of cudaStreamSynchronize blocks - 13
Part of #13401 .
Before submitting
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
@sayakpaul @dg845