Skip to content

[rfc][compile] compile method for DiffusionPipeline #11705

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
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

anijain2305
Copy link
Contributor

@anijain2305 anijain2305 commented Jun 13, 2025

This PR adds a compile method to the DiffusionPipeline to make it really easy for diffusers users to apply torch.compile.

The default setting enables regional compilation, which for diffusion models give as good speedup as full model compilation, with 8x-10x smaller compilation time. More data to support regional compilation as default is below

image

To onboard a new model, we just have to define the compile_region_classes attribute in the models, and that should allow regional compilation.

cc @sayakpaul

Copy link
Member

@sayakpaul sayakpaul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for starting this! LMK what you think of the comments.

@@ -286,6 +286,8 @@ def __init__(

self.gradient_checkpointing = False

self.compile_region_classes = (FluxTransformerBlock, FluxSingleTransformerBlock)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a class-level attribute like this?

_automatically_saved_args = ["_diffusers_version", "_class_name", "_name_or_path"]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thats better.

@@ -2027,6 +2027,40 @@ def _maybe_raise_error_if_group_offload_active(
return True
return False

def compile(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could make it a part of ModelMixin rather than DiffusionPipeline I believe:

class ModelMixin(torch.nn.Module, PushToHubMixin):

For most cases, users want to just do pipe.transformer.compile(). So, perhaps easier with this being added to ModelMixin?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you expect any improvements from compile on the pre/post transformer blocks? If not, then yeah, moving it inside makes sense to me.

Copy link
Contributor Author

@anijain2305 anijain2305 Jun 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change, do we have to do pipe.transformer.compile()? Hmm, this could be confusing because torch.nn.Module also has a method named compile. If we move in the ModelMixin, we might want a different method name.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, to tackle those cases, we could do something like this:

@wraps(torch.nn.Module.cuda)

So, we include all the acceptable args and kwargs in the ModelMixin compile()` method but additionally include the regional/hierarchical compilation related kwargs. Would that work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Let me try this out.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like there are some advantages of Pipeline.compile:

  1. It captures the user intent better, e.g., "I just want to accelerate this pipeline" (the word "compile" is also great because it signals "expect some initial delay").
  2. It's more future proof (diffuser devs get to tweak things to accommodate changes from diffusers/pytorch versions, or even future hardware and pipeline architecture/perf-characteristics).
  3. It's more user friendly (small things like .transformer does make a noticeable effect on user experience).

(2) and (3) are effectively consequences of (1). What do you think? @sayakpaul

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sayak suggested to phase this out.

Have pipeline.transformer.compile as the first PR

And then pipeline.compile in future which can internally call pipeline.transformer.compile. Its actually better this way because then the compile region classes is also hidden from the pipe.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we will eventually ship pipe.compile() :)

Comment on lines +2032 to +2033
compile_regions_for_transformer: bool = True,
transformer_module_name: str = "transformer",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two will have to have changed if we proceed to have it inside ModelMixin.

@sayakpaul sayakpaul requested review from DN6 and yiyixuxu June 13, 2025 05:37
@yiyixuxu
Copy link
Collaborator

thanks for the PR !
How do we determine default compile_region_classes for each model?

@anijain2305
Copy link
Contributor Author

thanks for the PR ! How do we determine default compile_region_classes for each model?

This is something that we will have to do manually. I deliberately made that choice. It should be be fairly easy to find those classes, basically the transformer classes. In absence of compile_region_classes, we can fallback to the full model compilation with a warning.

I think this is a fair compromise because it gives control to the model author, and with little guidance it should be very easy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants