[Model][MiniMax-H3] Multi-GPU serving fixes and layer-wise offload component selection (#7047)
Signed-off-by: heyuanliu-intel <heyuan.liu@intel.com>
This commit is contained in:
1 parent
2f1a18e69f
commit
40f8e94400
21 files changed
+1857
-60
No files matched your search
@@ -215,8 +215,9 @@ class AttentionMetadata:
|
||||
# variable-length query/key sequences for FlashAttention.
|
||||
# "max_seqlen_q" / "max_seqlen_k": maximum sequence lengths paired with
|
||||
# the packed cu_seqlens tensors.
|
||||
# "valid_kv_length": int — contiguous valid K/V prefix length for a
|
||||
# backend that advertises supports_prefix_kv_slicing.
|
||||
# "valid_kv_length": int — contiguous valid K/V prefix length. Local
|
||||
# backends may slice it directly; ring backends trim each circulated
|
||||
# K/V block from the same global prefix.
|
||||
# "npu_attn_varlen": bool — model opt-in for the NPU packed varlen path
|
||||
# (TND npu_fusion_attention driven by cu_seqlens, mask never read).
|
||||
# Requires the [real, pad] two-document packing contract; see
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
# Copyright (c) 2024, Jiarui Fang.
|
||||
# Adapted from https://github.com/feifeibear/long-context-attention
|
||||
|
||||
@@ -9,7 +10,33 @@ import torch.nn.functional as F
|
||||
|
||||
LseLayout = Literal["bhs", "bsh"]
|
||||
|
||||
__all__ = ["update_out_and_lse", "flatten_varlen_lse", "unflatten_varlen_lse"]
|
||||
__all__ = [
|
||||
"ring_kv_block_valid_length",
|
||||
"update_out_and_lse",
|
||||
"flatten_varlen_lse",
|
||||
"unflatten_varlen_lse",
|
||||
]
|
||||
|
||||
|
||||
def ring_kv_block_valid_length(
|
||||
valid_kv_length: int | None,
|
||||
block_size: int,
|
||||
block_rank: int,
|
||||
world_size: int,
|
||||
) -> int:
|
||||
"""Return the valid prefix length in one globally ordered ring K/V block."""
|
||||
if valid_kv_length is None:
|
||||
return block_size
|
||||
if isinstance(valid_kv_length, bool) or not isinstance(valid_kv_length, int):
|
||||
raise ValueError("valid_kv_length must be a Python integer")
|
||||
total_length = block_size * world_size
|
||||
if not 0 < valid_kv_length <= total_length:
|
||||
raise ValueError(
|
||||
"valid_kv_length must be within the global ring K/V sequence, "
|
||||
f"got {valid_kv_length} for length {total_length}"
|
||||
)
|
||||
block_start = block_rank * block_size
|
||||
return max(0, min(block_size, valid_kv_length - block_start))
|
||||
|
||||
|
||||
def _normalize_lse(block_lse: torch.Tensor, block_out: torch.Tensor, lse_layout: LseLayout) -> torch.Tensor:
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
# Copyright (c) 2024, Jiarui Fang.
|
||||
# Adapted from https://github.com/feifeibear/long-context-attention
|
||||
|
||||
@@ -6,7 +7,10 @@
|
||||
import torch
|
||||
|
||||
from vllm_omni.diffusion.attention.backends.ring.ring_selector import AttnType, select_flash_attn_impl
|
||||
from vllm_omni.diffusion.attention.backends.ring.ring_utils import update_out_and_lse
|
||||
from vllm_omni.diffusion.attention.backends.ring.ring_utils import (
|
||||
ring_kv_block_valid_length,
|
||||
update_out_and_lse,
|
||||
)
|
||||
from vllm_omni.diffusion.distributed.comm import RingComm
|
||||
|
||||
|
||||
@@ -27,6 +31,7 @@ def ring_flash_attn_forward(
|
||||
joint_tensor_key=None,
|
||||
joint_tensor_value=None,
|
||||
joint_strategy="front",
|
||||
valid_kv_length: int | None = None,
|
||||
):
|
||||
# Validate causal + joint_strategy combination
|
||||
# When causal=True and joint_strategy="rear", the causal mask would incorrectly
|
||||
@@ -42,6 +47,21 @@ def ring_flash_attn_forward(
|
||||
"from seeing the joint tokens."
|
||||
)
|
||||
|
||||
# Trimming the circulated K/V blocks changes their length while the query
|
||||
# block keeps the full padded length. Flash-Attention aligns a causal mask
|
||||
# to the bottom-right of a rectangular (seqlen_q, seqlen_k) tile, so a
|
||||
# trimmed block would shift the diagonal and let queries attend to the
|
||||
# wrong key positions -- silently, with no shape error. The two are
|
||||
# therefore an unsupported combination rather than a slow path: the caller
|
||||
# must either drop the padding before the ring or run non-causal.
|
||||
if causal and valid_kv_length is not None:
|
||||
raise ValueError(
|
||||
"valid_kv_length is not supported with causal=True in Ring Attention. "
|
||||
"Trimming a circulated K/V block shortens seqlen_k while seqlen_q keeps "
|
||||
"the padded length, and the causal diagonal is bottom-right aligned, so "
|
||||
"the mask would silently shift. Unpad before the ring, or use causal=False."
|
||||
)
|
||||
|
||||
comm = RingComm(process_group)
|
||||
|
||||
out = None
|
||||
@@ -66,8 +86,15 @@ def ring_flash_attn_forward(
|
||||
comm.commit()
|
||||
|
||||
if not causal or step <= comm.rank:
|
||||
step_k = k
|
||||
step_v = v
|
||||
block_rank = (comm.rank - step) % comm.world_size
|
||||
block_valid_length = ring_kv_block_valid_length(
|
||||
valid_kv_length,
|
||||
k.shape[1],
|
||||
block_rank,
|
||||
comm.world_size,
|
||||
)
|
||||
step_k = k[:, :block_valid_length]
|
||||
step_v = v[:, :block_valid_length]
|
||||
if step == 0 and joint_tensor_key is not None:
|
||||
if joint_strategy == "front":
|
||||
step_k = torch.cat([joint_tensor_key, step_k], dim=1)
|
||||
@@ -76,27 +103,28 @@ def ring_flash_attn_forward(
|
||||
step_k = torch.cat([step_k, joint_tensor_key], dim=1)
|
||||
step_v = torch.cat([step_v, joint_tensor_value], dim=1)
|
||||
|
||||
fn = select_flash_attn_impl(attn_type, stage="fwd-only", attn_processor=attn_processor)
|
||||
block_out, block_lse = fn(
|
||||
q,
|
||||
step_k,
|
||||
step_v,
|
||||
dropout_p=dropout_p,
|
||||
softmax_scale=softmax_scale,
|
||||
causal=causal and step == 0,
|
||||
window_size=window_size,
|
||||
softcap=softcap,
|
||||
alibi_slopes=alibi_slopes,
|
||||
return_softmax=True and dropout_p > 0,
|
||||
)
|
||||
if step_k.shape[1] > 0:
|
||||
fn = select_flash_attn_impl(attn_type, stage="fwd-only", attn_processor=attn_processor)
|
||||
block_out, block_lse = fn(
|
||||
q,
|
||||
step_k,
|
||||
step_v,
|
||||
dropout_p=dropout_p,
|
||||
softmax_scale=softmax_scale,
|
||||
causal=causal and step == 0,
|
||||
window_size=window_size,
|
||||
softcap=softcap,
|
||||
alibi_slopes=alibi_slopes,
|
||||
return_softmax=True and dropout_p > 0,
|
||||
)
|
||||
|
||||
# Ensure block_out is contiguous if needed, though usually it is from FA
|
||||
# Ensure block_out is contiguous if needed, though usually it is from FA
|
||||
|
||||
if attn_type == AttnType.SPARSE_SAGE:
|
||||
out, lse = block_out, block_lse
|
||||
else:
|
||||
# Ring kernel wrappers canonicalize LSE to (B, H, S).
|
||||
out, lse = update_out_and_lse(out, lse, block_out, block_lse, lse_layout="bhs")
|
||||
if attn_type == AttnType.SPARSE_SAGE:
|
||||
out, lse = block_out, block_lse
|
||||
else:
|
||||
# Ring kernel wrappers canonicalize LSE to (B, H, S).
|
||||
out, lse = update_out_and_lse(out, lse, block_out, block_lse, lse_layout="bhs")
|
||||
|
||||
if step + 1 != comm.world_size:
|
||||
comm.wait()
|
||||
@@ -132,6 +160,7 @@ class RingFlashAttnFunc(torch.autograd.Function):
|
||||
joint_tensor_key=None,
|
||||
joint_tensor_value=None,
|
||||
joint_strategy="front",
|
||||
valid_kv_length: int | None = None,
|
||||
):
|
||||
if softmax_scale is None:
|
||||
softmax_scale = q.shape[-1] ** (-0.5)
|
||||
@@ -158,6 +187,7 @@ class RingFlashAttnFunc(torch.autograd.Function):
|
||||
joint_tensor_key=joint_tensor_key,
|
||||
joint_tensor_value=joint_tensor_value,
|
||||
joint_strategy=joint_strategy,
|
||||
valid_kv_length=valid_kv_length,
|
||||
)
|
||||
return out if not return_softmax else (out, softmax_lse, None)
|
||||
|
||||
@@ -193,6 +223,7 @@ def ring_flash_attn_qkvpacked_func(
|
||||
None, # joint_tensor_key
|
||||
None, # joint_tensor_value
|
||||
"front", # joint_strategy
|
||||
None, # valid_kv_length
|
||||
)
|
||||
|
||||
|
||||
@@ -228,6 +259,7 @@ def ring_flash_attn_kvpacked_func(
|
||||
None, # joint_tensor_key
|
||||
None, # joint_tensor_value
|
||||
"front", # joint_strategy
|
||||
None, # valid_kv_length
|
||||
)
|
||||
|
||||
|
||||
@@ -249,6 +281,7 @@ def ring_flash_attn_func(
|
||||
joint_tensor_key=None,
|
||||
joint_tensor_value=None,
|
||||
joint_strategy="front",
|
||||
valid_kv_length: int | None = None,
|
||||
) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor, None]:
|
||||
"""Ring Attention forward pass using Flash Attention backend.
|
||||
|
||||
@@ -290,6 +323,8 @@ def ring_flash_attn_func(
|
||||
Defaults to None.
|
||||
joint_strategy (str): Concatenation strategy ("front" or "back").
|
||||
Defaults to "front".
|
||||
valid_kv_length (int | None): Length of the contiguous valid prefix in
|
||||
the global ring K/V sequence. Defaults to the full sequence.
|
||||
|
||||
Returns:
|
||||
Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor, None]]:
|
||||
@@ -314,4 +349,5 @@ def ring_flash_attn_func(
|
||||
joint_tensor_key,
|
||||
joint_tensor_value,
|
||||
joint_strategy,
|
||||
valid_kv_length,
|
||||
)
|
||||
@@ -1,4 +1,5 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
# Copyright (c) 2024, Jiarui Fang.
|
||||
# Adapted from https://github.com/feifeibear/long-context-attention
|
||||
|
||||
@@ -10,7 +11,10 @@ import torch
|
||||
from vllm.logger import init_logger
|
||||
|
||||
from vllm_omni.diffusion.attention.backends.ring.ring_kernels import pytorch_attn_forward
|
||||
from vllm_omni.diffusion.attention.backends.ring.ring_utils import update_out_and_lse
|
||||
from vllm_omni.diffusion.attention.backends.ring.ring_utils import (
|
||||
ring_kv_block_valid_length,
|
||||
update_out_and_lse,
|
||||
)
|
||||
from vllm_omni.diffusion.distributed.comm import RingComm
|
||||
|
||||
logger = init_logger(__name__)
|
||||
@@ -33,6 +37,7 @@ def ring_pytorch_attn_func(
|
||||
joint_tensor_key=None,
|
||||
joint_tensor_value=None,
|
||||
joint_strategy="front",
|
||||
valid_kv_length: int | None = None,
|
||||
):
|
||||
return RingAttentionFunc.apply(
|
||||
group,
|
||||
@@ -45,6 +50,7 @@ def ring_pytorch_attn_func(
|
||||
joint_tensor_key,
|
||||
joint_tensor_value,
|
||||
joint_strategy,
|
||||
valid_kv_length,
|
||||
)
|
||||
|
||||
|
||||
@@ -64,6 +70,7 @@ class RingAttentionFunc(torch.autograd.Function):
|
||||
joint_tensor_key=None,
|
||||
joint_tensor_value=None,
|
||||
joint_strategy="front",
|
||||
valid_kv_length: int | None = None,
|
||||
):
|
||||
# Validate causal + joint_strategy combination
|
||||
# When causal=True and joint_strategy="rear", the causal mask would incorrectly
|
||||
@@ -79,6 +86,22 @@ class RingAttentionFunc(torch.autograd.Function):
|
||||
"from seeing the joint tokens."
|
||||
)
|
||||
|
||||
# Trimming the circulated K/V blocks changes their length while the
|
||||
# query block keeps the full padded length. A causal SDPA mask over a
|
||||
# rectangular (seqlen_q, seqlen_k) tile is aligned to the bottom right,
|
||||
# so a trimmed block would shift the diagonal and let queries attend to
|
||||
# the wrong key positions -- silently, with no shape error. The two are
|
||||
# therefore an unsupported combination rather than a slow path: the
|
||||
# caller must either drop the padding before the ring or run
|
||||
# non-causal.
|
||||
if is_causal and valid_kv_length is not None:
|
||||
raise ValueError(
|
||||
"valid_kv_length is not supported with causal=True in Ring Attention. "
|
||||
"Trimming a circulated K/V block shortens seqlen_k while seqlen_q keeps "
|
||||
"the padded length, and the causal diagonal is bottom-right aligned, so "
|
||||
"the mask would silently shift. Unpad before the ring, or use causal=False."
|
||||
)
|
||||
|
||||
comm = RingComm(group)
|
||||
# Ensure tensors are contiguous for P2P communication
|
||||
q = q.contiguous()
|
||||
@@ -98,8 +121,15 @@ class RingAttentionFunc(torch.autograd.Function):
|
||||
comm.commit()
|
||||
|
||||
if not is_causal or step <= comm.rank:
|
||||
step_k = k
|
||||
step_v = v
|
||||
block_rank = (comm.rank - step) % comm.world_size
|
||||
block_valid_length = ring_kv_block_valid_length(
|
||||
valid_kv_length,
|
||||
k.shape[1],
|
||||
block_rank,
|
||||
comm.world_size,
|
||||
)
|
||||
step_k = k[:, :block_valid_length]
|
||||
step_v = v[:, :block_valid_length]
|
||||
if step == 0 and joint_tensor_key is not None:
|
||||
if joint_strategy == "front":
|
||||
step_k = torch.cat([joint_tensor_key, step_k], dim=1)
|
||||
@@ -108,15 +138,16 @@ class RingAttentionFunc(torch.autograd.Function):
|
||||
step_k = torch.cat([step_k, joint_tensor_key], dim=1)
|
||||
step_v = torch.cat([step_v, joint_tensor_value], dim=1)
|
||||
|
||||
block_out, block_lse = pytorch_attn_forward(
|
||||
q,
|
||||
step_k,
|
||||
step_v,
|
||||
softmax_scale=sm_scale,
|
||||
causal=is_causal and step == 0,
|
||||
op_type=op_type,
|
||||
)
|
||||
out, lse = update_out_and_lse(out, lse, block_out, block_lse, lse_layout="bhs")
|
||||
if step_k.shape[1] > 0:
|
||||
block_out, block_lse = pytorch_attn_forward(
|
||||
q,
|
||||
step_k,
|
||||
step_v,
|
||||
softmax_scale=sm_scale,
|
||||
causal=is_causal and step == 0,
|
||||
op_type=op_type,
|
||||
)
|
||||
out, lse = update_out_and_lse(out, lse, block_out, block_lse, lse_layout="bhs")
|
||||
|
||||
if step + 1 != comm.world_size:
|
||||
comm.wait()
|
||||
|
||||
Reference in new issue
Block a user