[Attention] Add trtllm diffusion attention backend with Skip-Softmax (#5283)

Signed-off-by: lishunyang12 <lishunyang03@gmail.com>
Co-authored-by: lishunyang12 <lishunyang03@gmail.com>
This commit is contained in:
SYLARandlishunyang12 authored and GitHub committed 2026-07-27 22:48:11 +08:00
1 parent 4dd1b47f47
commit 7762d664d8
24 files changed
+1512 -52

No files matched your search

@@ -64,6 +64,7 @@ class DiffusionAttentionBackendEnum(Enum, metaclass=_DiffusionBackendEnumMeta):
FLASHINFER_ATTN = "vllm_omni.diffusion.attention.backends.flashinfer_attn.FlashInferAttentionBackend"
FLASH_ATTN_HUB = "vllm_omni.diffusion.attention.backends.flash_attn_hub.FlashAttentionHubBackend"
FLASH_ATTN_3_HUB = "vllm_omni.diffusion.attention.backends.flash_attn_hub.FlashAttention3HubBackend"
TRTLLM_ATTN = "vllm_omni.diffusion.attention.backends.trtllm_attn.TrtllmAttentionBackend"
def get_path(self, include_classname: bool = True) -> str:
"""Get the class path for this backend (respects overrides).
@@ -0,0 +1,226 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import math
from dataclasses import dataclass, replace
import torch
from vllm.logger import init_logger
from vllm_omni.diffusion.attention.backends.abstract import (
AttentionBackend,
AttentionImpl,
AttentionMetadata,
)
logger = init_logger(__name__)
def _validate_control(value, name: str, lo: float, hi: float | None) -> float | None:
if value is None:
return None
v = float(value)
if not math.isfinite(v) or v < lo or (hi is not None and v > hi):
rng = f"in [{lo}, {hi}]" if hi is not None else f">= {lo}"
raise ValueError(f"{name} must be finite and {rng}; got {value!r}.")
return v
@dataclass(frozen=True)
class SkipSoftmaxConfig:
threshold: float | None = None
target_sparsity: float | None = None
disabled_until_timestep: float = 0.0
a: float | None = None
b: float | None = None
@classmethod
def from_backend_kwargs(cls, backend_kwargs: dict | None) -> "SkipSoftmaxConfig":
bk = backend_kwargs or {}
return cls(
threshold=_validate_control(bk.get("skip_softmax_threshold"), "skip_softmax_threshold", 0.0, None),
target_sparsity=_validate_control(bk.get("target_sparsity"), "target_sparsity", 0.0, 1.0),
disabled_until_timestep=_validate_control(
bk.get("disabled_until_timestep", 0.0), "disabled_until_timestep", 0.0, 1.0
),
)
@property
def enabled(self) -> bool:
return self.threshold is not None or (
self.target_sparsity is not None and self.a is not None and self.b is not None
)
@property
def configured(self) -> bool:
return self.threshold is not None or self.target_sparsity is not None
@property
def gated(self) -> bool:
return self.disabled_until_timestep > 0.0
def resolve_factor(self, seqlen: int, timestep: float | None) -> float | None:
if self.threshold is not None:
factor = self.threshold * seqlen
elif self.target_sparsity is not None and self.a is not None and self.b is not None:
factor = self.a * math.exp(self.b * self.target_sparsity)
else:
return None
if self.gated and timestep is not None and timestep > self.disabled_until_timestep:
return None
return factor
try:
from flashinfer.prefill import trtllm_ragged_attention_deepseek
HAS_FLASHINFER = True
except Exception as e: # pragma: no cover - import guard
HAS_FLASHINFER = False
logger.warning(
"FlashInfer is unavailable; TRTLLM_ATTN backend will not work. Reason: %s",
e,
)
def _workspace_bytes() -> int:
import vllm.envs as envs
return getattr(envs, "VLLM_FLASHINFER_WORKSPACE_BUFFER_SIZE", 394 * 1024 * 1024)
class TrtllmAttentionBackend(AttentionBackend):
accept_output_buffer: bool = True
@staticmethod
def get_supported_head_sizes() -> list[int]:
return [128]
@staticmethod
def get_name() -> str:
return "TRTLLM_ATTN"
@staticmethod
def get_impl_cls() -> type["TrtllmAttentionImpl"]:
return TrtllmAttentionImpl
class TrtllmAttentionImpl(AttentionImpl):
_workspace: torch.Tensor | None = None
def __init__(
self,
num_heads: int,
head_size: int,
softmax_scale: float,
causal: bool = False,
num_kv_heads: int | None = None,
prefix: str = "",
qkv_layout: str | None = None,
backend_kwargs: dict | None = None,
**extra_impl_args,
) -> None:
self.num_heads = num_heads
self.head_size = head_size
self.softmax_scale = softmax_scale
self.causal = causal
self.num_kv_heads = num_kv_heads if num_kv_heads is not None else num_heads
self.skip = SkipSoftmaxConfig.from_backend_kwargs(backend_kwargs)
self._warned_missing_timestep = False
def set_layer_calibration(self, a: float, b: float) -> None:
self.skip = replace(self.skip, a=a, b=b)
def _resolve_skip_factor(self, seqlen: int) -> float | None:
if not self.skip.enabled:
return None
timestep = None
if self.skip.gated:
from vllm_omni.diffusion.forward_context import get_forward_context
timestep = getattr(get_forward_context(), "denoise_timestep", None)
if timestep is None:
if not self._warned_missing_timestep:
logger.warning(
"TRTLLM skip: disabled_until_timestep=%s set but this pipeline does not "
"publish denoise_timestep; staying dense. Have the pipeline call "
"DenoiseProgressMixin.record_denoise_step to enable timestep gating.",
self.skip.disabled_until_timestep,
)
self._warned_missing_timestep = True
return None
return self.skip.resolve_factor(seqlen, timestep)
@classmethod
def _get_workspace(cls, device: torch.device) -> torch.Tensor:
nbytes = _workspace_bytes()
ws = cls._workspace
if ws is None or ws.device != device or ws.numel() < nbytes:
ws = torch.zeros(nbytes, dtype=torch.uint8, device=device)
cls._workspace = ws
return ws
def forward_cuda(
self,
query: torch.Tensor,
key: torch.Tensor,
value: torch.Tensor,
attn_metadata: AttentionMetadata | None = None,
) -> torch.Tensor:
attn_mask = getattr(attn_metadata, "attn_mask", None) if attn_metadata is not None else None
if attn_mask is not None:
raise ValueError(
"TRTLLM_ATTN does not support attn_mask (Wan sets one only under SP with "
"mask_sp_padding=True and a non-divisible seq len). Either set "
"parallel_config.mask_sp_padding=False, or select a mask-capable backend "
"(e.g. CUDNN_ATTN / TORCH_SDPA)."
)
if not HAS_FLASHINFER:
raise ImportError(
"TRTLLM_ATTN backend requires flashinfer. Install it or select "
"another backend via --diffusion-attention-backend."
)
batch, q_len, num_q_heads, head_dim = query.shape
kv_len, num_kv_heads = key.shape[1], key.shape[2]
device = query.device
q = query.reshape(batch * q_len, num_q_heads, head_dim).contiguous()
k = key.reshape(batch * kv_len, num_kv_heads, head_dim).contiguous()
v = value.reshape(batch * kv_len, num_kv_heads, head_dim).contiguous()
seq_lens = torch.full((batch,), kv_len, dtype=torch.int32, device=device)
cu_seq_lens_q = torch.arange(0, (batch + 1) * q_len, step=q_len, dtype=torch.int32, device=device)
cu_seq_lens_kv = torch.arange(0, (batch + 1) * kv_len, step=kv_len, dtype=torch.int32, device=device)
workspace = self._get_workspace(device)
bmm1_scale = self.softmax_scale
bmm2_scale = 1.0
_skip_factor = self._resolve_skip_factor(kv_len)
out = trtllm_ragged_attention_deepseek(
query=q,
key=k,
value=v,
workspace_buffer=workspace,
seq_lens=seq_lens,
max_q_len=q_len,
max_kv_len=kv_len,
bmm1_scale=bmm1_scale,
bmm2_scale=bmm2_scale,
o_sf_scale=-1.0,
batch_size=batch,
window_left=-1,
cum_seq_lens_q=cu_seq_lens_q,
cum_seq_lens_kv=cu_seq_lens_kv,
enable_pdl=False,
is_causal=self.causal,
return_lse=False,
skip_softmax_threshold_scale_factor=_skip_factor,
)
return out.reshape(batch, q_len, num_q_heads, head_dim)
@@ -0,0 +1,152 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from __future__ import annotations
import fnmatch
import logging
from typing import Any
logger = logging.getLogger(__name__)
_SUPPORTED_FORMULA = "a*exp(b*target_sparsity)"
def parse_sparse_attention_config(sac: dict | None) -> dict | None:
if not sac:
return None
group: dict = {}
for g in (sac.get("config_groups") or {}).values():
if isinstance(g, dict) and (g.get("algorithm") == "skip_softmax" or g.get("threshold_scale_factor")):
group = g
break
tsf = group.get("threshold_scale_factor") or sac.get("threshold_scale_factor") or {}
ab = tsf.get("coefficients") or tsf.get("prefill") or {}
if "a" not in ab or "b" not in ab:
return None
formula = (tsf.get("formula") or _SUPPORTED_FORMULA).replace(" ", "")
if formula != _SUPPORTED_FORMULA:
raise ValueError(
f"unsupported skip-softmax formula {tsf.get('formula')!r}; this backend implements '{_SUPPORTED_FORMULA}'."
)
return {"a": float(ab["a"]), "b": float(ab["b"]), "ignore": list(group.get("ignore") or [])}
def parse_secondary_expert_calibration(model: str | None) -> dict | None:
from vllm.transformers_utils.config import get_hf_file_to_dict
if not model:
return None
try:
cfg = get_hf_file_to_dict("transformer_2/config.json", model)
return parse_sparse_attention_config(cfg.get("sparse_attention_config")) if cfg else None
except Exception as exc: # noqa: BLE001
logger.warning(
"Could not read transformer_2 skip-softmax calibration (%s); transformer_2 will stay uncalibrated (dense).",
exc,
)
return None
def propagate_skip_softmax_calibration(specs: list, model: str | None, tf_config: Any) -> None:
calibration = parse_sparse_attention_config(
tf_config.get("sparse_attention_config") if tf_config is not None else None
)
if calibration is not None:
by_expert = {"transformer": calibration}
ckpt2 = parse_secondary_expert_calibration(model)
if ckpt2 is not None:
by_expert["transformer_2"] = ckpt2
calibration = {"by_expert": by_expert}
if calibration is None:
for spec in specs:
ss = spec.skip_softmax
if ss is not None and ss.target_sparsity is not None and ss.threshold is None:
raise ValueError(
f"target_sparsity was requested but the checkpoint for model '{model}' carries no "
f"skip-softmax calibration. Either set skip_softmax.threshold for the "
f"calibration-free path or load a calibrated checkpoint."
)
return
for spec in specs:
if spec.skip_calibration is None:
spec.skip_calibration = calibration
experts = list(calibration["by_expert"])
logger.info("Loaded skip-softmax calibration from checkpoint (%d curve(s): %s).", len(experts), ", ".join(experts))
def apply_skip_softmax_calibration(attention_config: Any, model: Any) -> None:
cfg = attention_config
specs = getattr(cfg, "per_role", None)
calibration = None
for spec in (getattr(cfg, "default", None), *(specs.values() if specs else ())):
calibration = calibration or getattr(spec, "skip_calibration", None)
if not calibration:
return
stamped = apply_to_pipeline(model, calibration)
logger.info("Skip-softmax: stamped calibration onto %d attention layer(s).", stamped)
_EXPERT_PREFIXES = ("transformer.", "transformer_2.")
def layer_match_names(module_name: str) -> tuple[str, ...]:
names = {module_name, module_name.replace("._orig_mod.", ".")}
for name in tuple(names):
for pfx in _EXPERT_PREFIXES:
if name.startswith(pfx):
names.add(name[len(pfx) :])
return tuple(names)
def is_ignored(module_name: str, ignore_patterns) -> bool:
names = layer_match_names(module_name)
for p in ignore_patterns or ():
for n in names:
if fnmatch.fnmatch(n, p) or fnmatch.fnmatch(n, p + ".*"):
return True
return False
def select_expert(module_name: str, expert_keys) -> str | None:
for key in sorted(expert_keys or (), key=len, reverse=True):
if module_name == key or module_name.startswith(key + "."):
return key
return None
def resolve_layer_calibration(module_name: str, calibration: dict) -> dict | None:
if not calibration:
return None
by_expert = calibration.get("by_expert")
if by_expert:
key = select_expert(module_name, by_expert.keys())
if key is None:
return None
entry = by_expert[key]
else:
entry = calibration
if is_ignored(module_name, entry.get("ignore")):
return None
return {"a": entry.get("a"), "b": entry.get("b")}
def apply_to_pipeline(pipeline, calibration: dict) -> int:
if not calibration:
return 0
stamped = 0
for name, module in pipeline.named_modules():
impl = getattr(module, "attention", None)
set_layer = getattr(impl, "set_layer_calibration", None)
if set_layer is None:
continue
per = resolve_layer_calibration(name, calibration)
if per and per.get("a") is not None and per.get("b") is not None:
set_layer(per["a"], per["b"])
stamped += 1
return stamped