From 9f7d38ffd3c463f103942bfdfe1cab1886b4c97b Mon Sep 17 00:00:00 2001 From: Aleksi Vesanto Date: Mon, 5 Oct 2026 10:22:53 +0300 Subject: [PATCH] [diffusion] attention: Allow disabling sequence masking in SP (#33855) Co-authored-by: jacky.cheng --- python/sglang/multimodal_gen/envs.py | 8 ++++++++ .../runtime/layers/attention/layer.py | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/python/sglang/multimodal_gen/envs.py b/python/sglang/multimodal_gen/envs.py index af3fe5413e..4f0c8cfd0b 100644 --- a/python/sglang/multimodal_gen/envs.py +++ b/python/sglang/multimodal_gen/envs.py @@ -35,6 +35,7 @@ if TYPE_CHECKING: NVCC_THREADS: str | None = None CMAKE_BUILD_TYPE: str | None = None VERBOSE: bool = False + SGLANG_DIFFUSION_DISABLE_SP_PAD_MASK: bool = False SGLANG_DIFFUSION_SERVER_DEV_MODE: bool = False SGLANG_DIFFUSION_DISABLE_MAPPED_COURIER: bool = False SGLANG_DIFFUSION_HOST_SPILL_DIR: str = os.path.expanduser( @@ -255,6 +256,13 @@ environment_variables: dict[str, Callable[[], Any]] = { "SGLANG_DIFFUSION_MXFP8_FA_HEAD_CHUNK_SIZE": _lazy_int( "SGLANG_DIFFUSION_MXFP8_FA_HEAD_CHUNK_SIZE", 4 ), + # Drop the SP tail-pad attention mask and run dense (unmasked) attention on + # the padded layout, instead of the packed-varlen path. Applies only when + # sequence parallelism is active and the mask is derived purely from the + # shard pad span. + "SGLANG_DIFFUSION_DISABLE_SP_PAD_MASK": _lazy_bool( + "SGLANG_DIFFUSION_DISABLE_SP_PAD_MASK" + ), # Select a built-in platform or an installed platform entry point. # Empty means automatic plugin activation followed by built-in detection. "SGLANG_DIFFUSION_PLATFORM_OVERRIDE": _lazy_str( diff --git a/python/sglang/multimodal_gen/runtime/layers/attention/layer.py b/python/sglang/multimodal_gen/runtime/layers/attention/layer.py index c1374fec86..be4416d414 100644 --- a/python/sglang/multimodal_gen/runtime/layers/attention/layer.py +++ b/python/sglang/multimodal_gen/runtime/layers/attention/layer.py @@ -18,6 +18,7 @@ from sglang.kernels.ops.diffusion import ( fused_pack_segmented_qkv, fused_scatter_to_padded, ) +from sglang.multimodal_gen import envs from sglang.multimodal_gen.runtime import server_args as server_args_module from sglang.multimodal_gen.runtime.breakable_cuda_graph.replay_token import ( get_current_replay_token, @@ -90,6 +91,10 @@ _PYTORCH_DEFAULT_CUDA_SDP_BACKENDS = [ # USPAttention masked branch and fall back to SDPA. _VARLEN_FA_ENABLED = os.environ.get("SGLANG_VARLEN_FA", "1") != "0" +# Set ``SGLANG_DIFFUSION_DISABLE_SP_PAD_MASK=1`` to drop the SP tail-pad mask +# and run dense attention on the padded layout. +_SP_PAD_MASK_DISABLED = envs.SGLANG_DIFFUSION_DISABLE_SP_PAD_MASK + def _resolve_sp_attention_mode( *, causal: bool, sparse_backend: bool @@ -988,6 +993,15 @@ class USPAttention(nn.Module): if isinstance(attn_mask_meta, DynamicVarlenMaskMeta) else attn_mask ) + + if ( + _SP_PAD_MASK_DISABLED + and attn_mask is None + and not effective_skip_sp + and get_sequence_parallel_world_size() > 1 + ): + attn_mask_meta = None + if isinstance(attn_mask_meta, DynamicVarlenMaskMeta): attn_mask_meta = attn_mask_meta.resolve(attn_mask)