Skip to content

Compile#1368

Merged
Artiprocher merged 7 commits intomodelscope:mainfrom
mi804:compile
Mar 24, 2026
Merged

Compile#1368
Artiprocher merged 7 commits intomodelscope:mainfrom
mi804:compile

Conversation

@mi804
Copy link
Copy Markdown
Collaborator

@mi804 mi804 commented Mar 24, 2026

No description provided.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request integrates torch.compile functionality across the DiffSynth library, allowing for significant performance improvements by compiling diffusion models and their components. It introduces a centralized pipeline compilation method and extends model definitions to support both full and regional compilation strategies, making it easier to leverage PyTorch's performance optimizations.

Highlights

  • New Compilation Method: Introduced a compile_pipeline method in BasePipeline to enable torch.compile for models within a pipeline, supporting various compilation modes and dynamic shapes.
  • Regional Compilation Support: Implemented logic for regional compilation, where models with a _repeated_blocks attribute will have those specific blocks compiled, optimizing performance for repetitive structures.
  • Model Configuration for Compilation: Added _repeated_blocks attributes to several DiT models (AnimaDiT, Flux2DiT, FluxDiT, LTXModel, QwenImageDiT, WanModel, ZImageDiT) to specify which blocks should be targeted for regional compilation.
  • Pipeline Integration: Configured various pipelines (AnimaImagePipeline, Flux2ImagePipeline, FluxImagePipeline, LTX2AudioVideoPipeline, MovaAudioVideoPipeline, QwenImagePipeline, WanVideoPipeline, ZImagePipeline) with a compilable_models attribute to explicitly list the models that can be compiled within each pipeline.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a compile_pipeline method to leverage torch.compile for performance optimization, which is a valuable addition. However, the current implementation has a critical flaw in how torch.compile is invoked, as it incorrectly assumes an in-place .compile() method on torch.nn.Module instances. This will lead to a runtime error. I have provided a detailed comment with a suggested fix to address this. Additionally, I've recommended replacing print statements with a standard logger for better maintainability. The other changes to support compilation in various models and pipelines are well-structured.

Comment on lines +363 to +371
repeated_blocks = getattr(model, "_repeated_blocks", None)
# regional compilation for repeated blocks.
if repeated_blocks is not None:
for submod in model.modules():
if submod.__class__.__name__ in repeated_blocks:
submod.compile(mode=mode, dynamic=dynamic, fullgraph=fullgraph, **kwargs)
# compile the whole model.
else:
model.compile(mode=mode, dynamic=dynamic, fullgraph=fullgraph, **kwargs)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

critical

torch.nn.Module instances do not have a .compile() method, and torch.compile() is not an in-place operation. It returns a new compiled module that must be used to replace the original. This implementation will raise an AttributeError.

For whole-model compilation, you need to use setattr(self, name, torch.compile(model, ...)). For regional compilation, you need to recursively traverse the model and replace the submodules.

Suggested change
repeated_blocks = getattr(model, "_repeated_blocks", None)
# regional compilation for repeated blocks.
if repeated_blocks is not None:
for submod in model.modules():
if submod.__class__.__name__ in repeated_blocks:
submod.compile(mode=mode, dynamic=dynamic, fullgraph=fullgraph, **kwargs)
# compile the whole model.
else:
model.compile(mode=mode, dynamic=dynamic, fullgraph=fullgraph, **kwargs)
repeated_blocks = getattr(model, "_repeated_blocks", None)
# regional compilation for repeated blocks.
if repeated_blocks is not None:
# Recursively find and replace modules to be compiled.
def _replace_modules(module):
for name, child in module.named_children():
if child.__class__.__name__ in repeated_blocks:
setattr(module, name, torch.compile(child, mode=mode, dynamic=dynamic, fullgraph=fullgraph, **kwargs))
else:
_replace_modules(child)
_replace_modules(model)
# compile the whole model.
else:
setattr(self, name, torch.compile(model, mode=mode, dynamic=dynamic, fullgraph=fullgraph, **kwargs))

"""
compile_models = compile_models or getattr(self, "compilable_models", [])
if len(compile_models) == 0:
print("No compilable models in the pipeline. Skip compilation.")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Consider using the logging module instead of print for logging information. This provides more flexibility for users of the library to control log levels and output streams. You would need to add import logging at the top of the file. This advice applies to the other print statements in this method as well (lines 361 and 372).

@Artiprocher Artiprocher merged commit ae8cb13 into modelscope:main Mar 24, 2026
@mi804 mi804 deleted the compile branch March 24, 2026 08:12
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.

2 participants