Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 7.3k
[Neuron] Enable torch.compile compatibility with Neuron device#13485
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
98f6c8cc58b8b833674090c51734a76953c2480388929ab7252cac7628a508668689e5da793083bb9c7cc4facab1eb5ff9cbe8f2816b96067f13f68a46cb19a354b883d1a25917c77cac6501d5d0bba16d566ce04b36ec6b586e14cf20ff1d8d0db3f536c597df50b87f6a730f07d96090b94bc1e5ab32956af417f976669d8f244f72d3f8File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -24,7 +24,7 @@ | ||
| from ...models import AutoencoderKLFlux2, Flux2Transformer2DModel | ||
| from ...schedulers import FlowMatchEulerDiscreteScheduler | ||
| from ...utils import is_torch_xla_available, logging, replace_example_docstring | ||
| from ...utils.torch_utils import randn_tensor | ||
| from ...utils.torch_utils import maybe_adjust_dtype_for_device, randn_tensor | ||
| from ..pipeline_utils import DiffusionPipeline | ||
| from .image_processor import Flux2ImageProcessor | ||
| from .pipeline_output import Flux2PipelineOutput | ||
| @@ -405,8 +405,9 @@ def _unpack_latents_with_ids( | ||
| x_list = [] | ||
| for data, pos in zip(x, x_ids): | ||
| _, ch = data.shape # noqa: F841 | ||
| h_ids = pos[:, 1].to(torch.int64) | ||
| w_ids = pos[:, 2].to(torch.int64) | ||
| idx_dtype = maybe_adjust_dtype_for_device(torch.int64, data.device) | ||
| h_ids = pos[:, 1].to(idx_dtype) | ||
| w_ids = pos[:, 2].to(idx_dtype) | ||
| # Use provided height/width to avoid DtoH sync from torch.max().item() | ||
| h = height if height is not None else torch.max(h_ids) + 1 | ||
| @@ -826,7 +827,8 @@ def __call__( | ||
| # 7. Denoising loop | ||
| # We set the index here to remove DtoH sync, helpful especially during compilation. | ||
| # Check out more details here: https://github.com/huggingface/diffusers/pull/11696 | ||
| self.scheduler.set_begin_index(0) | ||
| if hasattr(self.scheduler, "set_begin_index"): | ||
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. This seems like a bigger change that should be propagated to other pipelines that use ContributorAuthor 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. yes, only a part of self.scheduler have | ||
| self.scheduler.set_begin_index(0) | ||
| with self.progress_bar(total=num_inference_steps) as progress_bar: | ||
| for i, t in enumerate(timesteps): | ||
| if self.interrupt: | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -861,6 +861,10 @@ def __call__( | ||
| prompt_embeds = torch.cat([negative_prompt_embeds, prompt_embeds], dim=0) | ||
| prompt_attention_mask = torch.cat([negative_prompt_attention_mask, prompt_attention_mask], dim=0) | ||
| prompt_attention_mask = prompt_attention_mask.to( | ||
| maybe_adjust_dtype_for_device(prompt_attention_mask.dtype, prompt_attention_mask.device) | ||
| ) | ||
| # 4. Prepare timesteps | ||
| is_neuron_device = device.type == "neuron" | ||
| if XLA_AVAILABLE or is_neuron_device: | ||
| @@ -903,7 +907,8 @@ def __call__( | ||
| # 7. Denoising loop | ||
| num_warmup_steps = max(len(timesteps) - num_inference_steps * self.scheduler.order, 0) | ||
| if hasattr(self.scheduler, "set_begin_index"): | ||
| self.scheduler.set_begin_index(0) | ||
Comment on lines
+910
to
+911
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. If the pipeline initially didn't have it, maybe we don't need it? ContributorAuthor 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. The change was suggested in the initial PR for supporting neuron: #13289 (comment). the line is needed for the compiled path otherwise the first | ||
| with self.progress_bar(total=num_inference_steps) as progress_bar: | ||
| for i, t in enumerate(timesteps): | ||
| latent_model_input = torch.cat([latents] * 2) if do_classifier_free_guidance else latents | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
Does it have any performance limitations?
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.
both of them are zero-copy tensor views so should be the same performance wise, the change just avoid the graph break for dynamo
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 think you meant the Neuron dynamo? Because it passes regular CUDA during compilation when
fullgraph=Trueduring compilation.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.
yeah, here is totally a fix for neuron compilation. neuron compiler lower
chunkto split/gather and previously introduced a compilation failure, now it's fixed by neuron's new beta drop. But the explicit static slicesx[..., :half] / x[..., half:]lower to clean slice ops, and the neuron compiler compiles it faster.