[perf] Add mask-free TRTLLM packed-padding path (#6542)
Signed-off-by: Bo Li <22713281+bobboli@users.noreply.github.com>
This commit is contained in:
1 parent
b454e34966
commit
51b7565f33
7 files changed
+314
-123
No files matched your search
@@ -1,5 +1,5 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from dataclasses import dataclass, field
|
||||
@@ -27,12 +27,13 @@ class AttentionBackend(ABC):
|
||||
|
||||
@classmethod
|
||||
def supports_packed_mask_free(cls) -> bool:
|
||||
"""Whether packed attention never reads attn_mask on this platform.
|
||||
"""Whether [real, pad] packed layouts can run without attn_mask.
|
||||
|
||||
When True, models that pack a [real, pad] two-document layout and
|
||||
carry cu_seqlens/max_seqlen in ``AttentionMetadata.extra`` may skip
|
||||
constructing the padding mask entirely. Backends whose mask-free
|
||||
behavior is platform-dependent must check current_omni_platform.
|
||||
provide ``AttentionMetadata.packed_padding`` alongside the packed
|
||||
cu_seqlens/max_seqlen metadata may skip constructing the padding mask
|
||||
entirely. Backends whose mask-free behavior is platform-dependent must
|
||||
check current_omni_platform.
|
||||
"""
|
||||
return False
|
||||
|
||||
@@ -141,6 +142,21 @@ class VideoTokenLayout:
|
||||
latent_grid: tuple[int, int, int]
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class PackedPaddingMetadata:
|
||||
"""Producer-validated mask-free view of padding in a [real, pad] packing.
|
||||
|
||||
The cumulative-length tensors are canonical two-element ``[0, length]``
|
||||
views. Consumers may use them without reading device scalars because the
|
||||
producer owns the packing.
|
||||
"""
|
||||
|
||||
q_length: int
|
||||
kv_length: int
|
||||
cu_seqlens_q: torch.Tensor
|
||||
cu_seqlens_k: torch.Tensor
|
||||
|
||||
|
||||
@dataclass
|
||||
class AttentionMetadata:
|
||||
attn_mask: torch.Tensor | None = None
|
||||
@@ -188,6 +204,10 @@ class AttentionMetadata:
|
||||
# locality (block-sparse selection, tiled masks). Dense backends ignore it.
|
||||
video_layout: VideoTokenLayout | None = None
|
||||
|
||||
# Canonical mask-free view of structural suffix padding. Backends that do
|
||||
# not advertise supports_packed_mask_free ignore it.
|
||||
packed_padding: PackedPaddingMetadata | None = None
|
||||
|
||||
|
||||
T = TypeVar("T", bound=AttentionMetadata)
|
||||
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
|
||||
import functools
|
||||
import inspect
|
||||
import math
|
||||
from dataclasses import dataclass, replace
|
||||
from typing import NamedTuple, cast
|
||||
|
||||
import torch
|
||||
from vllm.logger import init_logger
|
||||
@@ -13,6 +14,7 @@ from vllm_omni.diffusion.attention.backends.abstract import (
|
||||
AttentionBackend,
|
||||
AttentionImpl,
|
||||
AttentionMetadata,
|
||||
PackedPaddingMetadata,
|
||||
)
|
||||
|
||||
logger = init_logger(__name__)
|
||||
@@ -42,8 +44,9 @@ class SkipSoftmaxConfig:
|
||||
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
|
||||
disabled_until_timestep=cast(
|
||||
float,
|
||||
_validate_control(bk.get("disabled_until_timestep", 0.0), "disabled_until_timestep", 0.0, 1.0),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -131,7 +134,7 @@ class QuantConfig:
|
||||
return self.dtype_qk is not None
|
||||
|
||||
def quantize(self, q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, quantize_fn):
|
||||
qk_quant_dtype = _QK_QUANT_DTYPES[self.dtype_qk]
|
||||
qk_quant_dtype = _QK_QUANT_DTYPES[cast(str, self.dtype_qk)]
|
||||
q_q, k_q, v_q, q_sfs, k_sfs, v_sfs = quantize_fn(
|
||||
q,
|
||||
k,
|
||||
@@ -145,6 +148,18 @@ class QuantConfig:
|
||||
return q_q, k_q, v_q, sage_attn_sfs, num_elts_per_sage_attn_blk
|
||||
|
||||
|
||||
class _PackedLayout(NamedTuple):
|
||||
"""Active token ranges and kernel metadata for a flattened ragged batch."""
|
||||
|
||||
q_tokens: int
|
||||
kv_tokens: int
|
||||
cu_seqlens_q: torch.Tensor
|
||||
cu_seqlens_kv: torch.Tensor
|
||||
batch_size: int
|
||||
seq_lens: torch.Tensor
|
||||
known_min_kv_length: int | None = None
|
||||
|
||||
|
||||
def _workspace_bytes() -> int:
|
||||
import vllm.envs as envs
|
||||
|
||||
@@ -154,6 +169,14 @@ def _workspace_bytes() -> int:
|
||||
class TrtllmAttentionBackend(AttentionBackend):
|
||||
accept_output_buffer: bool = True
|
||||
|
||||
@classmethod
|
||||
def supports_packed_mask_free(cls) -> bool:
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def supports_multi_doc_packed_varlen(cls) -> bool:
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def get_supported_head_sizes() -> list[int]:
|
||||
return [128]
|
||||
@@ -250,6 +273,97 @@ class TrtllmAttentionImpl(AttentionImpl):
|
||||
cls._workspace = ws
|
||||
return ws
|
||||
|
||||
@staticmethod
|
||||
def _prepare_packed_padding_layout(
|
||||
physical_batch: int,
|
||||
q: torch.Tensor,
|
||||
k: torch.Tensor,
|
||||
packed_padding: PackedPaddingMetadata,
|
||||
extra: dict,
|
||||
) -> _PackedLayout:
|
||||
"""Prepare a [real, pad] layout from an explicit host-side boundary."""
|
||||
valid_q_tokens = packed_padding.q_length
|
||||
valid_kv_tokens = packed_padding.kv_length
|
||||
max_q_len = extra["max_seqlen_q"]
|
||||
max_kv_len = extra["max_seqlen_k"]
|
||||
lengths = (valid_q_tokens, valid_kv_tokens, max_q_len, max_kv_len)
|
||||
if any(isinstance(value, bool) or not isinstance(value, int) for value in lengths):
|
||||
raise ValueError("Mask-free packed TRTLLM attention lengths must be Python integers")
|
||||
if physical_batch != 1:
|
||||
raise ValueError("Mask-free packed TRTLLM attention requires a single packed batch")
|
||||
|
||||
total_q_tokens = q.shape[0]
|
||||
total_kv_tokens = k.shape[0]
|
||||
if not 0 < valid_q_tokens <= total_q_tokens:
|
||||
raise ValueError(
|
||||
"PackedPaddingMetadata.q_length must be within the packed Q sequence, "
|
||||
f"got {valid_q_tokens} for length {total_q_tokens}"
|
||||
)
|
||||
if not 0 < valid_kv_tokens <= total_kv_tokens:
|
||||
raise ValueError(
|
||||
"PackedPaddingMetadata.kv_length must be within the packed K/V sequence, "
|
||||
f"got {valid_kv_tokens} for length {total_kv_tokens}"
|
||||
)
|
||||
if max_q_len != valid_q_tokens or max_kv_len != valid_kv_tokens:
|
||||
raise ValueError("Mask-free packed TRTLLM attention lengths must match max_seqlen_q/k")
|
||||
published_kv_length = extra.get("valid_kv_length")
|
||||
if published_kv_length is not None:
|
||||
if isinstance(published_kv_length, bool) or not isinstance(published_kv_length, int):
|
||||
raise ValueError("valid_kv_length must be a Python integer")
|
||||
if published_kv_length != valid_kv_tokens:
|
||||
raise ValueError("PackedPaddingMetadata.kv_length must match valid_kv_length")
|
||||
|
||||
cu_seq_lens_q = packed_padding.cu_seqlens_q
|
||||
cu_seq_lens_kv = packed_padding.cu_seqlens_k
|
||||
if cu_seq_lens_q.dtype != torch.int32 or cu_seq_lens_kv.dtype != torch.int32:
|
||||
raise ValueError("Mask-free packed TRTLLM attention requires int32 cu_seqlens")
|
||||
if cu_seq_lens_q.device != q.device or cu_seq_lens_kv.device != k.device:
|
||||
raise ValueError("Mask-free packed TRTLLM attention metadata must be on the Q/K device")
|
||||
if cu_seq_lens_q.shape != (2,) or cu_seq_lens_kv.shape != (2,):
|
||||
raise ValueError("Mask-free packed TRTLLM attention requires canonical two-element cu_seqlens")
|
||||
return _PackedLayout(
|
||||
q_tokens=valid_q_tokens,
|
||||
kv_tokens=valid_kv_tokens,
|
||||
cu_seqlens_q=cu_seq_lens_q,
|
||||
cu_seqlens_kv=cu_seq_lens_kv,
|
||||
batch_size=1,
|
||||
# Canonical packed-padding metadata is [0, valid_kv_tokens], so this slice
|
||||
# is the one-element sequence-length view expected by the kernel.
|
||||
seq_lens=cu_seq_lens_kv[1:],
|
||||
known_min_kv_length=valid_kv_tokens,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _prepare_generic_packed_layout(
|
||||
q: torch.Tensor,
|
||||
k: torch.Tensor,
|
||||
cu_seq_lens_q: torch.Tensor,
|
||||
cu_seq_lens_kv: torch.Tensor,
|
||||
) -> _PackedLayout:
|
||||
"""Validate and normalize a general ragged batch."""
|
||||
if int(cu_seq_lens_q[-1].item()) != q.shape[0] or int(cu_seq_lens_kv[-1].item()) != k.shape[0]:
|
||||
raise ValueError("Packed TRTLLM attention cu_seqlens must cover all Q/K/V tokens")
|
||||
|
||||
# Packed producers may retain a zero-length trailing segment when the
|
||||
# input is already aligned. It is not a real sequence and is removed
|
||||
# before deriving the batch size and per-sequence lengths.
|
||||
while (
|
||||
cu_seq_lens_q.numel() > 2
|
||||
and int(cu_seq_lens_q[-1].item()) == int(cu_seq_lens_q[-2].item())
|
||||
and int(cu_seq_lens_kv[-1].item()) == int(cu_seq_lens_kv[-2].item())
|
||||
):
|
||||
cu_seq_lens_q = cu_seq_lens_q[:-1]
|
||||
cu_seq_lens_kv = cu_seq_lens_kv[:-1]
|
||||
|
||||
return _PackedLayout(
|
||||
q_tokens=q.shape[0],
|
||||
kv_tokens=k.shape[0],
|
||||
cu_seqlens_q=cu_seq_lens_q,
|
||||
cu_seqlens_kv=cu_seq_lens_kv,
|
||||
batch_size=cu_seq_lens_q.numel() - 1,
|
||||
seq_lens=(cu_seq_lens_kv[1:] - cu_seq_lens_kv[:-1]).to(dtype=torch.int32).contiguous(),
|
||||
)
|
||||
|
||||
def forward_cuda(
|
||||
self,
|
||||
query: torch.Tensor,
|
||||
@@ -266,12 +380,16 @@ class TrtllmAttentionImpl(AttentionImpl):
|
||||
has_packed_metadata = len(present_packed_keys) == len(packed_keys)
|
||||
|
||||
attn_mask = getattr(attn_metadata, "attn_mask", None) if attn_metadata is not None else None
|
||||
if attn_mask is not None and not has_packed_metadata:
|
||||
packed_padding = getattr(attn_metadata, "packed_padding", None) if attn_metadata is not None else None
|
||||
if packed_padding is not None and not isinstance(packed_padding, PackedPaddingMetadata):
|
||||
raise ValueError("packed_padding must be PackedPaddingMetadata")
|
||||
if packed_padding is not None and not has_packed_metadata:
|
||||
raise ValueError("PackedPaddingMetadata requires complete packed TRTLLM attention metadata")
|
||||
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)."
|
||||
"TRTLLM_ATTN does not support attn_mask. Represent structural suffix padding "
|
||||
"with packed-padding metadata, or select a mask-capable backend such as "
|
||||
"CUDNN_ATTN or TORCH_SDPA."
|
||||
)
|
||||
|
||||
if not HAS_FLASHINFER:
|
||||
@@ -282,7 +400,6 @@ class TrtllmAttentionImpl(AttentionImpl):
|
||||
|
||||
physical_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(physical_batch * q_len, num_q_heads, head_dim).contiguous()
|
||||
@@ -290,6 +407,7 @@ class TrtllmAttentionImpl(AttentionImpl):
|
||||
v = value.reshape(physical_batch * kv_len, num_kv_heads, head_dim).contiguous()
|
||||
output_tokens = q.shape[0]
|
||||
|
||||
known_min_kv_length: int | None = kv_len
|
||||
if has_packed_metadata:
|
||||
cu_seq_lens_q = extra["cu_seqlens_q"]
|
||||
cu_seq_lens_kv = extra["cu_seqlens_k"]
|
||||
@@ -297,48 +415,36 @@ class TrtllmAttentionImpl(AttentionImpl):
|
||||
raise ValueError("Packed TRTLLM attention cu_seqlens tensors must be one-dimensional")
|
||||
if cu_seq_lens_q.numel() != cu_seq_lens_kv.numel() or cu_seq_lens_q.numel() < 2:
|
||||
raise ValueError("Packed TRTLLM attention requires matching non-empty Q and KV sequence batches")
|
||||
if int(cu_seq_lens_q[-1].item()) != q.shape[0] or int(cu_seq_lens_kv[-1].item()) != k.shape[0]:
|
||||
raise ValueError("Packed TRTLLM attention cu_seqlens must cover all Q/K/V tokens")
|
||||
|
||||
# Drop trailing empty sequences retained by packed producers when
|
||||
# alignment adds no padding tokens.
|
||||
while (
|
||||
cu_seq_lens_q.numel() > 2
|
||||
and int(cu_seq_lens_q[-1].item()) == int(cu_seq_lens_q[-2].item())
|
||||
and int(cu_seq_lens_kv[-1].item()) == int(cu_seq_lens_kv[-2].item())
|
||||
):
|
||||
cu_seq_lens_q = cu_seq_lens_q[:-1]
|
||||
cu_seq_lens_kv = cu_seq_lens_kv[:-1]
|
||||
# PackedPaddingMetadata is an optional producer-published shortcut
|
||||
# for structural suffix padding. Inputs without it retain the
|
||||
# general cu_seqlens path for arbitrary ragged Q/KV batches.
|
||||
if packed_padding is not None:
|
||||
packed_layout = self._prepare_packed_padding_layout(
|
||||
physical_batch,
|
||||
q,
|
||||
k,
|
||||
packed_padding,
|
||||
extra,
|
||||
)
|
||||
else:
|
||||
# Generic metadata can describe any number of real sequences,
|
||||
# not only a single [real, pad] pair.
|
||||
packed_layout = self._prepare_generic_packed_layout(
|
||||
q,
|
||||
k,
|
||||
cu_seq_lens_q,
|
||||
cu_seq_lens_kv,
|
||||
)
|
||||
|
||||
# A packed prefix mask denotes structural suffix padding, not another
|
||||
# document. Exclude it before quantization and attention; in particular,
|
||||
# SAGE block quantization must not span the valid/padding boundary.
|
||||
if attn_mask is not None:
|
||||
if q.shape[0] != k.shape[0] or attn_mask.numel() != q.shape[0]:
|
||||
raise ValueError("Packed TRTLLM prefix masks require equal Q/K lengths")
|
||||
flat_mask = attn_mask.reshape(-1).to(dtype=torch.bool)
|
||||
valid_tokens = int(flat_mask.sum().item())
|
||||
expected_mask = torch.arange(flat_mask.numel(), device=device) < valid_tokens
|
||||
if not torch.equal(flat_mask, expected_mask):
|
||||
raise ValueError("TRTLLM_ATTN only supports prefix-valid packed attention masks")
|
||||
|
||||
q_boundary = (cu_seq_lens_q == valid_tokens).nonzero(as_tuple=False)
|
||||
kv_boundary = (cu_seq_lens_kv == valid_tokens).nonzero(as_tuple=False)
|
||||
if q_boundary.numel() != 1 or kv_boundary.numel() != 1:
|
||||
raise ValueError("Packed TRTLLM prefix length must be a Q/K sequence boundary")
|
||||
q_end = int(q_boundary.item()) + 1
|
||||
kv_end = int(kv_boundary.item()) + 1
|
||||
if q_end != kv_end:
|
||||
raise ValueError("Packed TRTLLM prefix masks require matching Q/K sequence batches")
|
||||
|
||||
q = q[:valid_tokens]
|
||||
k = k[:valid_tokens]
|
||||
v = v[:valid_tokens]
|
||||
cu_seq_lens_q = cu_seq_lens_q[:q_end].contiguous()
|
||||
cu_seq_lens_kv = cu_seq_lens_kv[:kv_end].contiguous()
|
||||
|
||||
batch = cu_seq_lens_q.numel() - 1
|
||||
seq_lens = (cu_seq_lens_kv[1:] - cu_seq_lens_kv[:-1]).to(dtype=torch.int32).contiguous()
|
||||
q = q[: packed_layout.q_tokens]
|
||||
k = k[: packed_layout.kv_tokens]
|
||||
v = v[: packed_layout.kv_tokens]
|
||||
cu_seq_lens_q = packed_layout.cu_seqlens_q
|
||||
cu_seq_lens_kv = packed_layout.cu_seqlens_kv
|
||||
batch = packed_layout.batch_size
|
||||
seq_lens = packed_layout.seq_lens
|
||||
known_min_kv_length = packed_layout.known_min_kv_length
|
||||
max_q_len = int(extra["max_seqlen_q"])
|
||||
max_kv_len = int(extra["max_seqlen_k"])
|
||||
else:
|
||||
@@ -361,7 +467,13 @@ class TrtllmAttentionImpl(AttentionImpl):
|
||||
sage_kwargs: dict = {}
|
||||
# The SAGE kernel requires every KV sequence to contain at least one full
|
||||
# quantization block. Small auxiliary attention sites use the dense kernel.
|
||||
use_sage = self.quant.enabled and bool(torch.all(seq_lens >= self.quant.k_block_size).item())
|
||||
use_sage = False
|
||||
if self.quant.enabled:
|
||||
if known_min_kv_length is not None:
|
||||
sage_lengths_supported = known_min_kv_length >= self.quant.k_block_size
|
||||
else:
|
||||
sage_lengths_supported = bool(torch.all(seq_lens >= self.quant.k_block_size).item())
|
||||
use_sage = sage_lengths_supported
|
||||
if self.quant.enabled and not use_sage:
|
||||
message = (
|
||||
f"TRTLLM_ATTN SAGE quantization is configured for attention role {self.role!r}, but at least one "
|
||||
|
||||
Reference in new issue
Block a user