[NPU][diffusion] npu support enable_torch_compile for torchair backend on diffusion models #20687
Conversation
Summary of ChangesHello, 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 introduces support for Highlights
🧠 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. Changelog
Activity
Using Gemini Code AssistThe 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
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 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. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request adds support for torch.compile on NPU devices by using the torchair backend. The implementation correctly isolates the NPU-specific compilation logic. However, the current structure could be improved to be more robust and extensible for other hardware backends like HPU, which are also handled by get_compiler_backend. I've suggested a refactoring to make the backend selection more generic.
| compile_kwargs: dict[str, Any] = {"fullgraph": False, "dynamic": None} | ||
|
|
||
| if current_platform.is_npu(): | ||
| backend = get_compiler_backend() | ||
| compile_kwargs["backend"] = backend | ||
| compile_kwargs["dynamic"] = False | ||
| logger.info("Compiling transformer with torchair backend on NPU") | ||
| else: | ||
| try: | ||
| import torch._inductor.config as _inductor_cfg | ||
|
|
||
| _inductor_cfg.reorder_for_compute_comm_overlap = True | ||
| except ImportError: | ||
| pass | ||
| mode = os.environ.get( | ||
| "SGLANG_TORCH_COMPILE_MODE", "max-autotune-no-cudagraphs" | ||
| ) | ||
| compile_kwargs["mode"] = mode | ||
| logger.info(f"Compiling transformer with mode: {mode}") | ||
|
|
||
| _inductor_cfg.reorder_for_compute_comm_overlap = True | ||
| except ImportError: | ||
| pass | ||
| mode = os.environ.get("SGLANG_TORCH_COMPILE_MODE", "max-autotune-no-cudagraphs") | ||
| logger.info(f"Compiling transformer with mode: {mode}") | ||
| # TODO(triple-mu): support customized fullgraph and dynamic in the future | ||
| module.compile(mode=mode, fullgraph=False, dynamic=None) | ||
| module.compile(**compile_kwargs) |
There was a problem hiding this comment.
The current implementation correctly adds support for NPU, but the if/else structure is a bit rigid. It specifically checks for NPU and assumes the else block is for inductor-based backends (like CUDA). This doesn't account for other potential backends that get_compiler_backend might return, such as for HPU.
A more robust approach would be to fetch the backend name first and then use a dispatcher-like pattern to set the appropriate compilation arguments. This would make the code more extensible for future hardware support.
Here is a suggested refactoring:
compile_kwargs: dict[str, Any] = {"fullgraph": False, "dynamic": None}
backend = get_compiler_backend()
if current_platform.is_npu():
compile_kwargs["backend"] = backend
compile_kwargs["dynamic"] = False
logger.info("Compiling transformer with torchair backend on NPU")
elif current_platform.is_hpu():
compile_kwargs["backend"] = backend
logger.info("Compiling transformer with hpu_backend on HPU")
else: # Default to inductor for CUDA, etc.
try:
import torch._inductor.config as _inductor_cfg
_inductor_cfg.reorder_for_compute_comm_overlap = True
except ImportError:
pass
mode = os.environ.get(
"SGLANG_TORCH_COMPILE_MODE", "max-autotune-no-cudagraphs"
)
compile_kwargs["mode"] = mode
logger.info(f"Compiling transformer with mode: {mode}")
# TODO(triple-mu): support customized fullgraph and dynamic in the future
module.compile(**compile_kwargs)|
/tag-and-rerun-ci |
| return | ||
| try: | ||
| import torch._inductor.config as _inductor_cfg | ||
| compile_kwargs: dict[str, Any] = {"fullgraph": False, "dynamic": None} |
There was a problem hiding this comment.
how about _maybe_enable_torch_compile of MOVADenoisingStage in mova.py?
There was a problem hiding this comment.
Thanks, I’ve aligned it with the denoising-stage change and now explicitly use the torchair backend on NPU there as well.
177dd2f to
cf57f0d
Compare
| logger.info("Compiling %s with mode: %s", module.__class__.__name__, mode) | ||
| compile_kwargs: dict[str, object] = {"fullgraph": False, "dynamic": None} | ||
|
|
||
| if current_platform.is_npu(): |
There was a problem hiding this comment.
The platform split itself is hard to avoid here because the torch.compile configuration is inherently different on NPU vs non-NPU. We need an explicit backend selection on NPU, while other platforms should continue to use the existing inductor-mode path. I’m happy to move _maybe_enable_torch_compile to a shared helper, but some form of platform-specific branching is still required.
…d on diffusion models (sgl-project#20687)
…d on diffusion models (sgl-project#20687)
…d on diffusion models (sgl-project#20687)
…d on diffusion models (sgl-project#20687)
…d on diffusion models (sgl-project#20687)
Motivation
support torch.compile for npu
Modifications
Explicitly use the
torchairbackend fortorch.compileon the NPU path in the denoising stage.Benchmarking and Profiling
910B, Qwen-image
910B, FLUX.1
Checklist
Review Process
/tag-run-ci-label,/rerun-failed-ci,/tag-and-rerun-ci