From c7a71740a591e891a6c9d2bf17dd1d4e88cfe19d Mon Sep 17 00:00:00 2001 From: Yaochen Han <48639761+Alisehen@users.noreply.github.com> Date: Thu, 19 Mar 2026 03:40:35 +0800 Subject: [PATCH] [NPU][diffusion] npu support enable_torch_compile for torchair backend on diffusion models (#20687) --- .../pipelines_core/stages/denoising.py | 29 ++++++++++++----- .../stages/model_specific_stages/mova.py | 32 ++++++++++++++----- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/python/sglang/multimodal_gen/runtime/pipelines_core/stages/denoising.py b/python/sglang/multimodal_gen/runtime/pipelines_core/stages/denoising.py index ba169c85d..c130f9260 100644 --- a/python/sglang/multimodal_gen/runtime/pipelines_core/stages/denoising.py +++ b/python/sglang/multimodal_gen/runtime/pipelines_core/stages/denoising.py @@ -79,6 +79,7 @@ from sglang.multimodal_gen.runtime.utils.logging_utils import init_logger from sglang.multimodal_gen.runtime.utils.perf_logger import StageProfiler from sglang.multimodal_gen.runtime.utils.profiler import SGLDiffusionProfiler from sglang.multimodal_gen.utils import dict_to_3d_list, masks_like +from sglang.srt.utils.common import get_compiler_backend logger = init_logger(__name__) @@ -137,16 +138,28 @@ class DenoisingStage(PipelineStage): module, nn.Module ): return - try: - import torch._inductor.config as _inductor_cfg + 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) def _maybe_enable_cache_dit( self, num_inference_steps: int | tuple[int, int], batch: Req diff --git a/python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/mova.py b/python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/mova.py index 4b77e5444..afd43238e 100644 --- a/python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/mova.py +++ b/python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/mova.py @@ -67,6 +67,7 @@ from sglang.multimodal_gen.runtime.utils.logging_utils import init_logger from sglang.multimodal_gen.runtime.utils.perf_logger import StageProfiler from sglang.multimodal_gen.runtime.utils.profiler import SGLDiffusionProfiler from sglang.multimodal_gen.utils import PRECISION_TO_TYPE +from sglang.srt.utils.common import get_compiler_backend logger = init_logger(__name__) @@ -208,16 +209,31 @@ class MOVADenoisingStage(PipelineStage): """ if not server_args.enable_torch_compile or not isinstance(module, nn.Module): return - try: - import torch._inductor.config as _inductor_cfg + compile_kwargs: dict[str, object] = {"fullgraph": False, "dynamic": None} + + if current_platform.is_npu(): + backend = get_compiler_backend() + compile_kwargs["backend"] = backend + compile_kwargs["dynamic"] = False + logger.info( + "Compiling %s with torchair backend on NPU", + module.__class__.__name__, + ) + 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("Compiling %s with mode: %s", module.__class__.__name__, 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("Compiling %s with mode: %s", module.__class__.__name__, mode) # TODO(triple-mu): support customized fullgraph and dynamic in the future - module.compile(mode=mode, fullgraph=False, dynamic=None) + module.compile(**compile_kwargs) def _maybe_compile_dits(self, server_args: ServerArgs): if self._torch_compiled or not server_args.enable_torch_compile: