Merge branch 'main' into qwen4exp-qsa-fused-draft-metadata
This commit is contained in:
@@ -3362,6 +3362,115 @@ class rocm_aiter_ops:
|
||||
rotary_dim=kernel_rotary_dim,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def fused_qk_norm_mrope_and_cache(
|
||||
qkv: torch.Tensor,
|
||||
q_weight: torch.Tensor,
|
||||
k_weight: torch.Tensor,
|
||||
cos_sin_cache: torch.Tensor,
|
||||
positions: torch.Tensor,
|
||||
num_heads_q: int,
|
||||
num_heads_k: int,
|
||||
head_dim: int,
|
||||
is_neox: bool,
|
||||
mrope_section: list[int],
|
||||
is_interleaved: bool,
|
||||
rms_norm_eps: float,
|
||||
q_out: torch.Tensor,
|
||||
k_cache: torch.Tensor,
|
||||
v_cache: torch.Tensor,
|
||||
slot_mapping: torch.Tensor,
|
||||
k_scale: torch.Tensor,
|
||||
v_scale: torch.Tensor,
|
||||
block_size: int,
|
||||
x: int,
|
||||
rotary_dim: int = 0,
|
||||
) -> None:
|
||||
from aiter.ops.fused_qk_norm_mrope_cache_quant import (
|
||||
fused_qk_norm_mrope_3d_cache_pts_quant_shuffle,
|
||||
)
|
||||
|
||||
fused_qk_norm_mrope_3d_cache_pts_quant_shuffle(
|
||||
qkv,
|
||||
q_weight,
|
||||
k_weight,
|
||||
cos_sin_cache,
|
||||
positions,
|
||||
qkv.size(0),
|
||||
num_heads_q,
|
||||
num_heads_k,
|
||||
num_heads_k,
|
||||
head_dim,
|
||||
is_neox,
|
||||
mrope_section,
|
||||
is_interleaved,
|
||||
rms_norm_eps,
|
||||
q_out,
|
||||
k_cache,
|
||||
v_cache,
|
||||
slot_mapping,
|
||||
k_scale,
|
||||
v_scale,
|
||||
None,
|
||||
None,
|
||||
False,
|
||||
False,
|
||||
block_size,
|
||||
x,
|
||||
rotary_dim,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def do_qk_norm_mrope_kvcache_update(
|
||||
qkv: torch.Tensor,
|
||||
q_weight: torch.Tensor,
|
||||
k_weight: torch.Tensor,
|
||||
cos_sin_cache: torch.Tensor,
|
||||
positions: torch.Tensor,
|
||||
num_heads_q: int,
|
||||
num_heads_k: int,
|
||||
head_dim: int,
|
||||
is_neox: bool,
|
||||
mrope_section: list[int],
|
||||
is_interleaved: bool,
|
||||
rms_norm_eps: float,
|
||||
q_out: torch.Tensor,
|
||||
key_cache: torch.Tensor,
|
||||
value_cache: torch.Tensor,
|
||||
slot_mapping: torch.Tensor,
|
||||
k_scale: torch.Tensor,
|
||||
v_scale: torch.Tensor,
|
||||
kv_cache_dtype: str,
|
||||
rotary_dim: int = 0,
|
||||
) -> None:
|
||||
if kv_cache_dtype.startswith("fp8"):
|
||||
key_cache = key_cache.view(current_platform.fp8_dtype())
|
||||
value_cache = value_cache.view(current_platform.fp8_dtype())
|
||||
|
||||
rocm_aiter_ops.fused_qk_norm_mrope_and_cache(
|
||||
qkv=qkv,
|
||||
q_weight=q_weight,
|
||||
k_weight=k_weight,
|
||||
cos_sin_cache=cos_sin_cache,
|
||||
positions=positions,
|
||||
num_heads_q=num_heads_q,
|
||||
num_heads_k=num_heads_k,
|
||||
head_dim=head_dim,
|
||||
is_neox=is_neox,
|
||||
mrope_section=mrope_section,
|
||||
is_interleaved=is_interleaved,
|
||||
rms_norm_eps=rms_norm_eps,
|
||||
q_out=q_out,
|
||||
k_cache=key_cache,
|
||||
v_cache=value_cache,
|
||||
slot_mapping=slot_mapping,
|
||||
k_scale=k_scale,
|
||||
v_scale=v_scale,
|
||||
block_size=key_cache.shape[1],
|
||||
x=16 // key_cache.element_size(),
|
||||
rotary_dim=rotary_dim,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def triton_rope_and_cache(
|
||||
query: torch.Tensor,
|
||||
|
||||
@@ -31,6 +31,7 @@ from vllm.platforms import current_platform
|
||||
|
||||
ROTARY_OP = torch.ops._C.rotary_embedding.default
|
||||
FLASHINFER_ROTARY_OP = torch.ops.vllm.flashinfer_rotary_embedding.default
|
||||
MROPE_OP = torch.ops.vllm.mrope.default
|
||||
|
||||
QUANT_OPS: dict[QuantKey, OpOverload] = {
|
||||
kFp8StaticTensorSym: torch.ops._C.static_scaled_fp8_quant.default, # noqa: E501
|
||||
@@ -162,6 +163,74 @@ class MatcherRotaryEmbedding(MatcherCustomOp):
|
||||
return result
|
||||
|
||||
|
||||
class MatcherMRotaryEmbedding(MatcherCustomOp):
|
||||
def __init__(
|
||||
self,
|
||||
is_neox: bool,
|
||||
head_size: int,
|
||||
rotary_dim: int,
|
||||
num_heads: int,
|
||||
num_kv_heads: int,
|
||||
mrope_section: tuple[int, int, int],
|
||||
mrope_interleaved: bool,
|
||||
enabled: bool | None = None,
|
||||
) -> None:
|
||||
if enabled is None:
|
||||
enabled = RotaryEmbedding.enabled()
|
||||
|
||||
super().__init__(enabled)
|
||||
self.is_neox = is_neox
|
||||
self.head_size = head_size
|
||||
self.rotary_dim = rotary_dim
|
||||
self.num_heads = num_heads
|
||||
self.num_kv_heads = num_kv_heads
|
||||
self.q_size = self.num_heads * self.head_size
|
||||
self.kv_size = self.num_kv_heads * self.head_size
|
||||
self.mrope_section = mrope_section
|
||||
self.mrope_interleaved = mrope_interleaved
|
||||
|
||||
def inputs(self) -> list[torch.Tensor]:
|
||||
positions = self.empty_int64(3, 5)
|
||||
query = self.empty(5, self.q_size)
|
||||
key = self.empty(5, self.kv_size)
|
||||
cos_sin_cache = self.empty(4096, self.rotary_dim)
|
||||
return [positions, query, key, cos_sin_cache]
|
||||
|
||||
def forward_custom(
|
||||
self,
|
||||
positions: torch.Tensor,
|
||||
query: torch.Tensor,
|
||||
key: torch.Tensor,
|
||||
cos_sin_cache: torch.Tensor,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
result = auto_functionalized(
|
||||
MROPE_OP,
|
||||
positions=positions,
|
||||
query=query,
|
||||
key=key,
|
||||
cos_sin_cache=cos_sin_cache,
|
||||
head_size=self.head_size,
|
||||
rotary_dim=self.rotary_dim,
|
||||
mrope_section_t=self.mrope_section[0],
|
||||
mrope_section_h=self.mrope_section[1],
|
||||
mrope_section_w=self.mrope_section[2],
|
||||
mrope_interleaved=self.mrope_interleaved,
|
||||
is_neox_style=self.is_neox,
|
||||
)
|
||||
return result[1], result[2]
|
||||
|
||||
def forward_native(
|
||||
self,
|
||||
positions: torch.Tensor,
|
||||
query: torch.Tensor,
|
||||
key: torch.Tensor,
|
||||
cos_sin_cache: torch.Tensor,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
raise NotImplementedError(
|
||||
"MRoPE fusion requires the rotary_embedding custom op"
|
||||
)
|
||||
|
||||
|
||||
class MatcherRMSNormGated(MatcherCustomOp):
|
||||
"""Matches RMSNormGated with norm_before_gate=True and group_size=None."""
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
import inspect
|
||||
from collections.abc import Callable
|
||||
from collections.abc import Callable, Mapping
|
||||
from typing import ParamSpec
|
||||
|
||||
import torch
|
||||
@@ -19,12 +19,19 @@ from vllm.model_executor.layers.attention.attention import (
|
||||
Attention,
|
||||
get_attention_context,
|
||||
)
|
||||
from vllm.model_executor.layers.quantization.utils.quant_utils import GroupShape
|
||||
from vllm.platforms import current_platform
|
||||
from vllm.utils.torch_utils import direct_register_custom_op
|
||||
from vllm.utils.torch_utils import (
|
||||
_USE_LAYERNAME,
|
||||
LayerNameType,
|
||||
_encode_layer_name,
|
||||
_resolve_layer_name,
|
||||
direct_register_custom_op,
|
||||
)
|
||||
|
||||
from ..inductor_pass import enable_fake_mode
|
||||
from ..vllm_inductor_pass import VllmInductorPass, VllmPatternMatcherPass
|
||||
from .matcher_utils import MatcherRotaryEmbedding
|
||||
from .matcher_utils import MROPE_OP, MatcherMRotaryEmbedding, MatcherRotaryEmbedding
|
||||
from .rms_quant_fusion import empty_bf16, empty_fp32, empty_i64
|
||||
|
||||
logger = init_logger(__name__)
|
||||
@@ -51,8 +58,9 @@ def fused_qk_norm_rope_and_unified_kv_cache_update_impl(
|
||||
rms_norm_eps: float,
|
||||
cos_sin_cache: torch.Tensor,
|
||||
is_neox: bool,
|
||||
layer_name: str = "",
|
||||
layer_name: LayerNameType,
|
||||
) -> torch.Tensor:
|
||||
layer_name = _resolve_layer_name(layer_name)
|
||||
_, attn_layer, kv_cache, layer_slot_mapping = get_attention_context(layer_name)
|
||||
if layer_slot_mapping is not None:
|
||||
attn_layer.impl.do_qk_norm_rope_kvcache_update(
|
||||
@@ -87,7 +95,7 @@ def fused_qk_norm_rope_and_unified_kv_cache_update_fake(
|
||||
rms_norm_eps: float,
|
||||
cos_sin_cache: torch.Tensor,
|
||||
is_neox: bool,
|
||||
layer_name: str = "",
|
||||
layer_name: LayerNameType,
|
||||
) -> torch.Tensor:
|
||||
return torch.empty(0, device=qkv.device, dtype=qkv.dtype)
|
||||
|
||||
@@ -100,6 +108,76 @@ direct_register_custom_op(
|
||||
)
|
||||
|
||||
|
||||
def fused_qk_norm_mrope_and_unified_kv_cache_update_impl(
|
||||
q_out: torch.Tensor,
|
||||
qkv: torch.Tensor,
|
||||
positions: torch.Tensor,
|
||||
q_weight: torch.Tensor,
|
||||
k_weight: torch.Tensor,
|
||||
rms_norm_eps: float,
|
||||
cos_sin_cache: torch.Tensor,
|
||||
is_neox: bool,
|
||||
mrope_section_t: int,
|
||||
mrope_section_h: int,
|
||||
mrope_section_w: int,
|
||||
is_interleaved: bool,
|
||||
rotary_dim: int,
|
||||
layer_name: LayerNameType,
|
||||
) -> torch.Tensor:
|
||||
layer_name = _resolve_layer_name(layer_name)
|
||||
_, attn_layer, kv_cache, layer_slot_mapping = get_attention_context(layer_name)
|
||||
if layer_slot_mapping is not None:
|
||||
if positions.ndim == 1:
|
||||
positions = positions.unsqueeze(0).expand(3, -1).contiguous()
|
||||
attn_layer.impl.do_qk_norm_mrope_kvcache_update(
|
||||
attn_layer,
|
||||
qkv,
|
||||
q_out,
|
||||
positions,
|
||||
q_weight,
|
||||
k_weight,
|
||||
rms_norm_eps,
|
||||
cos_sin_cache,
|
||||
is_neox,
|
||||
(mrope_section_t, mrope_section_h, mrope_section_w),
|
||||
is_interleaved,
|
||||
rotary_dim,
|
||||
kv_cache,
|
||||
layer_slot_mapping,
|
||||
)
|
||||
else:
|
||||
q_out.zero_()
|
||||
|
||||
return torch.empty(0, device=qkv.device, dtype=qkv.dtype)
|
||||
|
||||
|
||||
def fused_qk_norm_mrope_and_unified_kv_cache_update_fake(
|
||||
q_out: torch.Tensor,
|
||||
qkv: torch.Tensor,
|
||||
positions: torch.Tensor,
|
||||
q_weight: torch.Tensor,
|
||||
k_weight: torch.Tensor,
|
||||
rms_norm_eps: float,
|
||||
cos_sin_cache: torch.Tensor,
|
||||
is_neox: bool,
|
||||
mrope_section_t: int,
|
||||
mrope_section_h: int,
|
||||
mrope_section_w: int,
|
||||
is_interleaved: bool,
|
||||
rotary_dim: int,
|
||||
layer_name: LayerNameType,
|
||||
) -> torch.Tensor:
|
||||
return torch.empty(0, device=qkv.device, dtype=qkv.dtype)
|
||||
|
||||
|
||||
direct_register_custom_op(
|
||||
op_name="fused_qk_norm_mrope_and_unified_kv_cache_update",
|
||||
op_func=fused_qk_norm_mrope_and_unified_kv_cache_update_impl,
|
||||
mutates_args=["q_out"],
|
||||
fake_impl=fused_qk_norm_mrope_and_unified_kv_cache_update_fake,
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Pattern: QK-norm + RoPE + unified_kv_cache_update
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -142,19 +220,27 @@ class QkNormRopeKvCachePattern:
|
||||
self.eps = eps
|
||||
self.is_neox = is_neox
|
||||
self.quant_query = quant_query
|
||||
self.encoded_layer_name = _encode_layer_name(self.layer_name)
|
||||
self.query_quant_group_shape = (
|
||||
layer.query_quant.group_shape
|
||||
if layer.query_quant is not None
|
||||
else GroupShape.PER_TENSOR
|
||||
)
|
||||
|
||||
self.q_size = self.num_heads * self.head_size
|
||||
self.k_size = self.num_kv_heads * self.head_size
|
||||
self.v_size = self.num_kv_heads * self.head_size_v
|
||||
|
||||
self.rope_matcher = MatcherRotaryEmbedding(
|
||||
is_neox=is_neox,
|
||||
head_size=self.head_size,
|
||||
num_heads=self.num_heads,
|
||||
num_kv_heads=self.num_kv_heads,
|
||||
self.rope_matcher: MatcherRotaryEmbedding | MatcherMRotaryEmbedding = (
|
||||
MatcherRotaryEmbedding(
|
||||
is_neox=is_neox,
|
||||
head_size=self.head_size,
|
||||
num_heads=self.num_heads,
|
||||
num_kv_heads=self.num_kv_heads,
|
||||
)
|
||||
)
|
||||
|
||||
def get_inputs(self) -> list[torch.Tensor]:
|
||||
def get_inputs(self) -> list:
|
||||
T = 5
|
||||
L = 4096
|
||||
qkv = empty_bf16(T, self.q_size + self.k_size + self.v_size)
|
||||
@@ -166,8 +252,13 @@ class QkNormRopeKvCachePattern:
|
||||
if self.quant_query:
|
||||
q_scale = empty_fp32(1)
|
||||
inputs += [q_scale]
|
||||
if _USE_LAYERNAME:
|
||||
inputs.append(self.encoded_layer_name)
|
||||
return inputs
|
||||
|
||||
def _get_layer_name(self, layer_name: LayerNameType | None) -> LayerNameType:
|
||||
return self.encoded_layer_name if layer_name is None else layer_name
|
||||
|
||||
def pattern_non_fp8_quant_query(
|
||||
self,
|
||||
qkv: torch.Tensor,
|
||||
@@ -175,6 +266,7 @@ class QkNormRopeKvCachePattern:
|
||||
q_weight: torch.Tensor,
|
||||
k_weight: torch.Tensor,
|
||||
cos_sin_cache: torch.Tensor,
|
||||
layer_name: LayerNameType | None = None,
|
||||
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
|
||||
q, k, v = qkv.split([self.q_size, self.k_size, self.v_size], dim=-1)
|
||||
q_by_head = q.view(-1, self.q_size // self.head_size, self.head_size)
|
||||
@@ -190,7 +282,9 @@ class QkNormRopeKvCachePattern:
|
||||
q_rope = q_rope.view(-1, self.num_heads, self.head_size)
|
||||
k_rope = k_rope.view(-1, self.num_kv_heads, self.head_size)
|
||||
v = v.view(-1, self.num_kv_heads, self.head_size_v)
|
||||
dummy = torch.ops.vllm.unified_kv_cache_update(k_rope, v, self.layer_name)
|
||||
dummy = torch.ops.vllm.unified_kv_cache_update(
|
||||
k_rope, v, self._get_layer_name(layer_name)
|
||||
)
|
||||
return dummy, q_rope, k_rope, v
|
||||
|
||||
def replacement_non_fp8_quant_query(
|
||||
@@ -200,6 +294,7 @@ class QkNormRopeKvCachePattern:
|
||||
q_weight: torch.Tensor,
|
||||
k_weight: torch.Tensor,
|
||||
cos_sin_cache: torch.Tensor,
|
||||
layer_name: LayerNameType | None = None,
|
||||
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
|
||||
q_out = torch.empty(
|
||||
qkv.shape[0],
|
||||
@@ -228,10 +323,49 @@ class QkNormRopeKvCachePattern:
|
||||
rms_norm_eps=self.eps,
|
||||
cos_sin_cache=cos_sin_cache,
|
||||
is_neox=self.is_neox,
|
||||
layer_name=self.layer_name,
|
||||
layer_name=self._get_layer_name(layer_name),
|
||||
)
|
||||
return results[0], results[1], results[2], v
|
||||
|
||||
def _quantize_query(
|
||||
self,
|
||||
query: torch.Tensor,
|
||||
q_scale: torch.Tensor,
|
||||
) -> tuple[torch.Tensor, torch.Tensor | None]:
|
||||
if self.query_quant_group_shape.is_per_tensor():
|
||||
q_out = torch.empty_like(query, dtype=current_platform.fp8_dtype())
|
||||
q_quant = auto_functionalized(
|
||||
torch.ops.vllm.rocm_aiter_per_tensor_quant.default,
|
||||
out=q_out,
|
||||
x=query,
|
||||
scale=q_scale,
|
||||
is_dynamic=False,
|
||||
)
|
||||
# The AITER op's schema marks scale mutable, so preserve its
|
||||
# functionalized write-back as an output of the matched region.
|
||||
return q_quant[1], q_quant[2]
|
||||
|
||||
# QuantFP8.forward_hip falls back to the generic static op for the
|
||||
# historical per-head shape (-1, block_size). This also covers the
|
||||
# degenerate one-local-KV-head case where a scalar scale is represented
|
||||
# as (-1, q_size), rather than changing Attention's classification.
|
||||
q_out = torch.empty(
|
||||
query.shape,
|
||||
device=query.device,
|
||||
dtype=current_platform.fp8_dtype(),
|
||||
)
|
||||
q_quant = auto_functionalized(
|
||||
torch.ops._C.static_scaled_fp8_quant.default,
|
||||
result=q_out,
|
||||
input=query,
|
||||
scale=q_scale,
|
||||
group_shape=[
|
||||
self.query_quant_group_shape.row,
|
||||
self.query_quant_group_shape.col,
|
||||
],
|
||||
)
|
||||
return q_quant[1], None
|
||||
|
||||
def pattern_fp8_quant_query(
|
||||
self,
|
||||
qkv: torch.Tensor,
|
||||
@@ -240,7 +374,8 @@ class QkNormRopeKvCachePattern:
|
||||
k_weight: torch.Tensor,
|
||||
cos_sin_cache: torch.Tensor,
|
||||
q_scale: torch.Tensor,
|
||||
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
|
||||
layer_name: LayerNameType | None = None,
|
||||
) -> tuple[torch.Tensor, ...]:
|
||||
q, k, v = qkv.split([self.q_size, self.k_size, self.v_size], dim=-1)
|
||||
q_by_head = q.view(-1, self.q_size // self.head_size, self.head_size)
|
||||
q_normed = vllm.ir.ops.rms_norm(q_by_head, q_weight, self.eps)
|
||||
@@ -251,24 +386,15 @@ class QkNormRopeKvCachePattern:
|
||||
k_flat = k_normed.view(-1, self.k_size)
|
||||
|
||||
q_rope, k_rope = self.rope_matcher(positions, q_flat, k_flat, cos_sin_cache)
|
||||
# Match the quant-query op Attention.forward inserts (fp8 KV + UNIFIED).
|
||||
# Explicit auto_functionalized (out=[1]) keeps the quant node in the pattern.
|
||||
q_out = torch.empty_like(q_rope, dtype=current_platform.fp8_dtype())
|
||||
q_quant = auto_functionalized(
|
||||
torch.ops.vllm.rocm_aiter_per_tensor_quant.default,
|
||||
out=q_out,
|
||||
x=q_rope,
|
||||
scale=q_scale,
|
||||
is_dynamic=False,
|
||||
)
|
||||
# `scale` is mutable: its copy_ write-back to _q_scale bumps the mutation
|
||||
# region, so keep q flat (a reshape lands past the barrier and won't match).
|
||||
q_rope_fp8 = q_quant[1]
|
||||
q_scale_out = q_quant[2]
|
||||
q_rope_fp8, q_scale_out = self._quantize_query(q_rope, q_scale)
|
||||
|
||||
k_rope = k_rope.view(-1, self.num_kv_heads, self.head_size)
|
||||
v = v.view(-1, self.num_kv_heads, self.head_size_v)
|
||||
dummy = torch.ops.vllm.unified_kv_cache_update(k_rope, v, self.layer_name)
|
||||
dummy = torch.ops.vllm.unified_kv_cache_update(
|
||||
k_rope, v, self._get_layer_name(layer_name)
|
||||
)
|
||||
if q_scale_out is None:
|
||||
return dummy, q_rope_fp8, k_rope, v
|
||||
return dummy, q_rope_fp8, k_rope, v, q_scale_out
|
||||
|
||||
def replacement_fp8_quant_query(
|
||||
@@ -279,7 +405,8 @@ class QkNormRopeKvCachePattern:
|
||||
k_weight: torch.Tensor,
|
||||
cos_sin_cache: torch.Tensor,
|
||||
q_scale: torch.Tensor,
|
||||
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
|
||||
layer_name: LayerNameType | None = None,
|
||||
) -> tuple[torch.Tensor, ...]:
|
||||
q_out = torch.empty(
|
||||
qkv.shape[0],
|
||||
self.num_heads,
|
||||
@@ -307,22 +434,14 @@ class QkNormRopeKvCachePattern:
|
||||
rms_norm_eps=self.eps,
|
||||
cos_sin_cache=cos_sin_cache,
|
||||
is_neox=self.is_neox,
|
||||
layer_name=self.layer_name,
|
||||
layer_name=self._get_layer_name(layer_name),
|
||||
)
|
||||
# Re-apply the quant on the kernel's bf16 q_out; fused op does not quant q.
|
||||
# Same explicit auto_functionalized form as the pattern: [1] = quantized
|
||||
# q, [2] = scale (returned so the buffer-writeback use is preserved).
|
||||
# Re-apply the same quant form on the kernel's bf16 q_out; the fused
|
||||
# operation does not quantize Q.
|
||||
q_fp8_flat = results[1].view(-1, self.q_size)
|
||||
q_fp8_out = torch.empty_like(q_fp8_flat, dtype=current_platform.fp8_dtype())
|
||||
q_requant = auto_functionalized(
|
||||
torch.ops.vllm.rocm_aiter_per_tensor_quant.default,
|
||||
out=q_fp8_out,
|
||||
x=q_fp8_flat,
|
||||
scale=q_scale,
|
||||
is_dynamic=False,
|
||||
)
|
||||
q_fp8 = q_requant[1] # flat to mirror the pattern (see note above)
|
||||
q_scale_out = q_requant[2]
|
||||
q_fp8, q_scale_out = self._quantize_query(q_fp8_flat, q_scale)
|
||||
if q_scale_out is None:
|
||||
return results[0], q_fp8, results[2], v
|
||||
return results[0], q_fp8, results[2], v, q_scale_out
|
||||
|
||||
@staticmethod
|
||||
@@ -345,6 +464,9 @@ class QkNormRopeKvCachePattern:
|
||||
|
||||
view_to_reshape(gm)
|
||||
|
||||
def _extra_check(self, match: pm.Match) -> bool:
|
||||
return True
|
||||
|
||||
def _register(self, pattern, replacement, pm_pass) -> None:
|
||||
trace_fn = QkNormRopeKvCachePattern.wrap_trace_fn(
|
||||
pm.fwd_only,
|
||||
@@ -370,40 +492,338 @@ class QkNormRopeKvCachePattern:
|
||||
inputs,
|
||||
trace_fn,
|
||||
pm_pass,
|
||||
extra_check=self._extra_check,
|
||||
search_fn_pattern=search_fn_pattern,
|
||||
)
|
||||
|
||||
def register(self, pm_pass: PatternMatcherPass) -> None:
|
||||
# make_fx counts `self` in bound-method code params; wrap as plain fns.
|
||||
# Distinct names per branch so mypy doesn't see one name, two signatures.
|
||||
if self.quant_query:
|
||||
if _USE_LAYERNAME:
|
||||
|
||||
def pattern_q(qkv, positions, q_weight, k_weight, cos_sin_cache, q_scale):
|
||||
return self.pattern_fp8_quant_query(
|
||||
def pattern_q_with_layer(
|
||||
qkv,
|
||||
positions,
|
||||
q_weight,
|
||||
k_weight,
|
||||
cos_sin_cache,
|
||||
q_scale,
|
||||
layer_name,
|
||||
):
|
||||
return self.pattern_fp8_quant_query(
|
||||
qkv,
|
||||
positions,
|
||||
q_weight,
|
||||
k_weight,
|
||||
cos_sin_cache,
|
||||
q_scale,
|
||||
layer_name,
|
||||
)
|
||||
|
||||
def replacement_q_with_layer(
|
||||
qkv,
|
||||
positions,
|
||||
q_weight,
|
||||
k_weight,
|
||||
cos_sin_cache,
|
||||
q_scale,
|
||||
layer_name,
|
||||
):
|
||||
return self.replacement_fp8_quant_query(
|
||||
qkv,
|
||||
positions,
|
||||
q_weight,
|
||||
k_weight,
|
||||
cos_sin_cache,
|
||||
q_scale,
|
||||
layer_name,
|
||||
)
|
||||
|
||||
self._register(pattern_q_with_layer, replacement_q_with_layer, pm_pass)
|
||||
else:
|
||||
|
||||
def pattern_q_without_layer(
|
||||
qkv, positions, q_weight, k_weight, cos_sin_cache, q_scale
|
||||
)
|
||||
):
|
||||
return self.pattern_fp8_quant_query(
|
||||
qkv, positions, q_weight, k_weight, cos_sin_cache, q_scale
|
||||
)
|
||||
|
||||
def replacement_q(
|
||||
qkv, positions, q_weight, k_weight, cos_sin_cache, q_scale
|
||||
):
|
||||
return self.replacement_fp8_quant_query(
|
||||
def replacement_q_without_layer(
|
||||
qkv, positions, q_weight, k_weight, cos_sin_cache, q_scale
|
||||
)
|
||||
):
|
||||
return self.replacement_fp8_quant_query(
|
||||
qkv, positions, q_weight, k_weight, cos_sin_cache, q_scale
|
||||
)
|
||||
|
||||
self._register(pattern_q, replacement_q, pm_pass)
|
||||
self._register(
|
||||
pattern_q_without_layer, replacement_q_without_layer, pm_pass
|
||||
)
|
||||
else:
|
||||
if _USE_LAYERNAME:
|
||||
|
||||
def pattern_noq(qkv, positions, q_weight, k_weight, cos_sin_cache):
|
||||
return self.pattern_non_fp8_quant_query(
|
||||
def pattern_noq_with_layer(
|
||||
qkv,
|
||||
positions,
|
||||
q_weight,
|
||||
k_weight,
|
||||
cos_sin_cache,
|
||||
layer_name,
|
||||
):
|
||||
return self.pattern_non_fp8_quant_query(
|
||||
qkv,
|
||||
positions,
|
||||
q_weight,
|
||||
k_weight,
|
||||
cos_sin_cache,
|
||||
layer_name,
|
||||
)
|
||||
|
||||
def replacement_noq_with_layer(
|
||||
qkv,
|
||||
positions,
|
||||
q_weight,
|
||||
k_weight,
|
||||
cos_sin_cache,
|
||||
layer_name,
|
||||
):
|
||||
return self.replacement_non_fp8_quant_query(
|
||||
qkv,
|
||||
positions,
|
||||
q_weight,
|
||||
k_weight,
|
||||
cos_sin_cache,
|
||||
layer_name,
|
||||
)
|
||||
|
||||
self._register(
|
||||
pattern_noq_with_layer, replacement_noq_with_layer, pm_pass
|
||||
)
|
||||
else:
|
||||
|
||||
def pattern_noq_without_layer(
|
||||
qkv, positions, q_weight, k_weight, cos_sin_cache
|
||||
):
|
||||
return self.pattern_non_fp8_quant_query(
|
||||
qkv, positions, q_weight, k_weight, cos_sin_cache
|
||||
)
|
||||
|
||||
def replacement_noq_without_layer(
|
||||
qkv, positions, q_weight, k_weight, cos_sin_cache
|
||||
):
|
||||
return self.replacement_non_fp8_quant_query(
|
||||
qkv, positions, q_weight, k_weight, cos_sin_cache
|
||||
)
|
||||
|
||||
self._register(
|
||||
pattern_noq_without_layer,
|
||||
replacement_noq_without_layer,
|
||||
pm_pass,
|
||||
)
|
||||
|
||||
def replacement_noq(qkv, positions, q_weight, k_weight, cos_sin_cache):
|
||||
return self.replacement_non_fp8_quant_query(
|
||||
qkv, positions, q_weight, k_weight, cos_sin_cache
|
||||
)
|
||||
|
||||
self._register(pattern_noq, replacement_noq, pm_pass)
|
||||
class QkNormMRopeKvCachePattern(QkNormRopeKvCachePattern):
|
||||
FUSED_OP = torch.ops.vllm.fused_qk_norm_mrope_and_unified_kv_cache_update.default
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
layer: Attention,
|
||||
eps: float,
|
||||
is_neox: bool,
|
||||
quant_query: bool,
|
||||
rotary_dim: int,
|
||||
mrope_section: tuple[int, int, int],
|
||||
is_interleaved: bool,
|
||||
) -> None:
|
||||
super().__init__(
|
||||
layer=layer,
|
||||
eps=eps,
|
||||
is_neox=is_neox,
|
||||
quant_query=quant_query,
|
||||
)
|
||||
self.rotary_dim = rotary_dim
|
||||
self.mrope_section = mrope_section
|
||||
self.is_interleaved = is_interleaved
|
||||
self.rope_matcher = MatcherMRotaryEmbedding(
|
||||
is_neox=is_neox,
|
||||
head_size=self.head_size,
|
||||
rotary_dim=rotary_dim,
|
||||
num_heads=self.num_heads,
|
||||
num_kv_heads=self.num_kv_heads,
|
||||
mrope_section=mrope_section,
|
||||
mrope_interleaved=is_interleaved,
|
||||
)
|
||||
|
||||
def get_inputs(self) -> list[torch.Tensor]:
|
||||
T = 5
|
||||
L = 4096
|
||||
qkv = empty_bf16(T, self.q_size + self.k_size + self.v_size)
|
||||
positions = empty_i64(3, T)
|
||||
q_weight = empty_bf16(1, self.head_size)
|
||||
k_weight = empty_bf16(1, self.head_size)
|
||||
cos_sin_cache = empty_bf16(L, self.rotary_dim)
|
||||
inputs = [qkv, positions, q_weight, k_weight, cos_sin_cache]
|
||||
if self.quant_query:
|
||||
inputs.append(empty_fp32(1))
|
||||
if _USE_LAYERNAME:
|
||||
inputs.append(self.encoded_layer_name)
|
||||
return inputs
|
||||
|
||||
def _unfused_kv_views(self, qkv: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
# The supported backend reads K/V from the cache after the fused write.
|
||||
# Keep view-compatible placeholders for the attention custom-op ABI.
|
||||
_, k, v = qkv.split([self.q_size, self.k_size, self.v_size], dim=-1)
|
||||
k = k.view(qkv.shape[0], self.num_kv_heads, self.head_size)
|
||||
v = v.view(qkv.shape[0], self.num_kv_heads, self.head_size_v)
|
||||
return k, v
|
||||
|
||||
def _fused_results(
|
||||
self,
|
||||
qkv: torch.Tensor,
|
||||
positions: torch.Tensor,
|
||||
q_weight: torch.Tensor,
|
||||
k_weight: torch.Tensor,
|
||||
cos_sin_cache: torch.Tensor,
|
||||
layer_name: LayerNameType | None,
|
||||
) -> tuple[torch.Tensor, ...]:
|
||||
q_out = torch.empty(
|
||||
qkv.shape[0],
|
||||
self.num_heads,
|
||||
self.head_size,
|
||||
device=qkv.device,
|
||||
dtype=qkv.dtype,
|
||||
)
|
||||
return auto_functionalized(
|
||||
self.FUSED_OP,
|
||||
q_out=q_out,
|
||||
qkv=qkv,
|
||||
positions=positions,
|
||||
q_weight=q_weight,
|
||||
k_weight=k_weight,
|
||||
rms_norm_eps=self.eps,
|
||||
cos_sin_cache=cos_sin_cache,
|
||||
is_neox=self.is_neox,
|
||||
mrope_section_t=self.mrope_section[0],
|
||||
mrope_section_h=self.mrope_section[1],
|
||||
mrope_section_w=self.mrope_section[2],
|
||||
is_interleaved=self.is_interleaved,
|
||||
rotary_dim=self.rotary_dim,
|
||||
layer_name=self._get_layer_name(layer_name),
|
||||
)
|
||||
|
||||
def replacement_non_fp8_quant_query(
|
||||
self,
|
||||
qkv: torch.Tensor,
|
||||
positions: torch.Tensor,
|
||||
q_weight: torch.Tensor,
|
||||
k_weight: torch.Tensor,
|
||||
cos_sin_cache: torch.Tensor,
|
||||
layer_name: LayerNameType | None = None,
|
||||
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
|
||||
k, v = self._unfused_kv_views(qkv)
|
||||
results = self._fused_results(
|
||||
qkv, positions, q_weight, k_weight, cos_sin_cache, layer_name
|
||||
)
|
||||
return results[0], results[1], k, v
|
||||
|
||||
def replacement_fp8_quant_query(
|
||||
self,
|
||||
qkv: torch.Tensor,
|
||||
positions: torch.Tensor,
|
||||
q_weight: torch.Tensor,
|
||||
k_weight: torch.Tensor,
|
||||
cos_sin_cache: torch.Tensor,
|
||||
q_scale: torch.Tensor,
|
||||
layer_name: LayerNameType | None = None,
|
||||
) -> tuple[torch.Tensor, ...]:
|
||||
k, v = self._unfused_kv_views(qkv)
|
||||
results = self._fused_results(
|
||||
qkv, positions, q_weight, k_weight, cos_sin_cache, layer_name
|
||||
)
|
||||
q_flat = results[1].view(-1, self.q_size)
|
||||
q_fp8, q_scale_out = self._quantize_query(q_flat, q_scale)
|
||||
if q_scale_out is None:
|
||||
return results[0], q_fp8, k, v
|
||||
return results[0], q_fp8, k, v, q_scale_out
|
||||
|
||||
def _extra_check(self, match: pm.Match) -> bool:
|
||||
expected = {
|
||||
"head_size": self.head_size,
|
||||
"rotary_dim": self.rotary_dim,
|
||||
"mrope_section_t": self.mrope_section[0],
|
||||
"mrope_section_h": self.mrope_section[1],
|
||||
"mrope_section_w": self.mrope_section[2],
|
||||
"mrope_interleaved": self.is_interleaved,
|
||||
"is_neox_style": self.is_neox,
|
||||
}
|
||||
mrope_node = next(
|
||||
(node for node in match.nodes if node.args and node.args[0] == MROPE_OP),
|
||||
None,
|
||||
)
|
||||
if mrope_node is None or any(
|
||||
mrope_node.kwargs.get(name) != value for name, value in expected.items()
|
||||
):
|
||||
return False
|
||||
|
||||
# The fused AITER MRoPE entrypoint writes rotated K directly to the
|
||||
# cache and does not expose a BF16 K result. Returning the raw K view is
|
||||
# valid only for decoder unified attention, whose backend consumes K
|
||||
# from that cache. Reject graphs that observe K in any other way.
|
||||
output_nodes = match.output_nodes()
|
||||
if len(output_nodes) < 3 or output_nodes[2] is None:
|
||||
return False
|
||||
matched_nodes = set(match.nodes)
|
||||
attention_op = torch.ops.vllm.unified_attention_with_output.default
|
||||
|
||||
def is_unified_attention_user(user: fx.Node) -> bool:
|
||||
return user.op == "call_function" and (
|
||||
user.target == attention_op
|
||||
or (user.args and user.args[0] == attention_op)
|
||||
)
|
||||
|
||||
return all(
|
||||
user in matched_nodes or is_unified_attention_user(user)
|
||||
for user in output_nodes[2].users
|
||||
)
|
||||
|
||||
|
||||
def _discover_mrope_configs(
|
||||
config: VllmConfig,
|
||||
) -> tuple[tuple[tuple[int, int, int], bool], ...]:
|
||||
model_config = config.model_config
|
||||
hf_configs = (
|
||||
getattr(model_config, "hf_text_config", None),
|
||||
getattr(model_config, "hf_config", None),
|
||||
)
|
||||
configs: set[tuple[tuple[int, int, int], bool]] = set()
|
||||
for hf_config in hf_configs:
|
||||
if hf_config is None:
|
||||
continue
|
||||
rope_parameters = getattr(hf_config, "rope_parameters", None)
|
||||
if rope_parameters is None:
|
||||
rope_parameters = getattr(hf_config, "rope_scaling", None)
|
||||
if not isinstance(rope_parameters, Mapping):
|
||||
continue
|
||||
section = rope_parameters.get("mrope_section")
|
||||
if not isinstance(section, (list, tuple)) or len(section) != 3:
|
||||
continue
|
||||
try:
|
||||
parsed_section = (
|
||||
int(section[0]),
|
||||
int(section[1]),
|
||||
int(section[2]),
|
||||
)
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
if any(value <= 0 for value in parsed_section):
|
||||
continue
|
||||
configs.add(
|
||||
(
|
||||
parsed_section,
|
||||
bool(rope_parameters.get("mrope_interleaved", False)),
|
||||
)
|
||||
)
|
||||
return tuple(sorted(configs))
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -412,7 +832,7 @@ class QkNormRopeKvCachePattern:
|
||||
|
||||
|
||||
class QkNormRopeKvCacheFusionPass(VllmPatternMatcherPass):
|
||||
"""Fuse QK-norm + RoPE + KV cache update into a single AITER HIP kernel.
|
||||
"""Fuse QK-norm + RoPE/MRoPE + KV cache update into an AITER HIP kernel.
|
||||
|
||||
Supersedes both QKNormRoPEFusionPass and RopeKVCacheFusionPass for
|
||||
attention layers that support the combined operation, eliminating two
|
||||
@@ -429,22 +849,33 @@ class QkNormRopeKvCacheFusionPass(VllmPatternMatcherPass):
|
||||
|
||||
cc = config.compilation_config
|
||||
self.max_token_num = cc.pass_config.rope_kvcache_fusion_max_token_num
|
||||
self.mrope_configs = (
|
||||
_discover_mrope_configs(config)
|
||||
if cc.is_custom_op_enabled("rotary_embedding")
|
||||
else ()
|
||||
)
|
||||
|
||||
dtype = config.model_config.dtype
|
||||
if dtype not in (torch.bfloat16, torch.float16):
|
||||
logger.warning_once(
|
||||
"QK Norm+RoPE+KVCache fusion not enabled: unsupported dtype %s", dtype
|
||||
"QK Norm+RoPE/MRoPE+KVCache fusion not enabled: unsupported dtype %s",
|
||||
dtype,
|
||||
)
|
||||
return
|
||||
|
||||
attn_layers = get_layers_from_vllm_config(config, Attention)
|
||||
|
||||
for _, layer in attn_layers.items():
|
||||
if not layer.impl.fused_qk_norm_rope_kvcache_supported():
|
||||
supports_rope = layer.impl.fused_qk_norm_rope_kvcache_supported()
|
||||
supports_mrope = (
|
||||
bool(self.mrope_configs)
|
||||
and layer.impl.fused_qk_norm_mrope_kvcache_supported()
|
||||
)
|
||||
if not supports_rope and not supports_mrope:
|
||||
continue
|
||||
if layer.head_size not in SUPPORTED_FUSED_QK_NORM_ROPE_KVCACHE_HEAD_DIMS:
|
||||
logger.warning_once(
|
||||
"QK Norm+RoPE+KVCache fusion not enabled for a layer: "
|
||||
"QK Norm+RoPE/MRoPE+KVCache fusion not enabled for a layer: "
|
||||
"head_size=%d is not supported by the "
|
||||
"fused_qk_norm_rope_cache_pts_quant_shuffle kernel "
|
||||
"(supported: %s). Falling back to the unfused path.",
|
||||
@@ -455,22 +886,71 @@ class QkNormRopeKvCacheFusionPass(VllmPatternMatcherPass):
|
||||
if layer.head_size_v != layer.head_size:
|
||||
# The fused kernel uses a single head_dim for q/k/v.
|
||||
logger.warning_once(
|
||||
"QK Norm+RoPE+KVCache fusion not enabled for a layer: "
|
||||
"QK Norm+RoPE/MRoPE+KVCache fusion not enabled for a layer: "
|
||||
"head_size_v=%d differs from head_size=%d, which the fused "
|
||||
"kernel does not support. Falling back to the unfused path.",
|
||||
layer.head_size_v,
|
||||
layer.head_size,
|
||||
)
|
||||
continue
|
||||
for epsilon in [1e-5, 1e-6]:
|
||||
for neox in [True, False]:
|
||||
for quant_q in [False, True]:
|
||||
QkNormRopeKvCachePattern(
|
||||
layer=layer,
|
||||
eps=epsilon,
|
||||
is_neox=neox,
|
||||
quant_query=quant_q,
|
||||
).register(self.patterns)
|
||||
if supports_rope:
|
||||
for epsilon in [1e-5, 1e-6]:
|
||||
for neox in [True, False]:
|
||||
for quant_q in [False, True]:
|
||||
QkNormRopeKvCachePattern(
|
||||
layer=layer,
|
||||
eps=epsilon,
|
||||
is_neox=neox,
|
||||
quant_query=quant_q,
|
||||
).register(self.patterns)
|
||||
|
||||
if supports_mrope:
|
||||
for section, is_interleaved in self.mrope_configs:
|
||||
rotary_dim = 2 * sum(section)
|
||||
if rotary_dim > layer.head_size:
|
||||
logger.warning_once(
|
||||
"QK Norm+MRoPE+KVCache fusion not enabled for a "
|
||||
"layer: rotary_dim=%d exceeds head_size=%d.",
|
||||
rotary_dim,
|
||||
layer.head_size,
|
||||
)
|
||||
continue
|
||||
vector_width = layer.head_size // 32
|
||||
for epsilon in [1e-5, 1e-6]:
|
||||
for neox in [True, False]:
|
||||
# Each warp lane processes `vector_width` adjacent
|
||||
# values. NeoX additionally exchanges vectors
|
||||
# across the half-rotary boundary.
|
||||
rotary_alignment = vector_width * (2 if neox else 1)
|
||||
if rotary_dim % rotary_alignment != 0:
|
||||
logger.warning_once(
|
||||
"QK Norm+MRoPE+KVCache fusion not enabled "
|
||||
"for a layer/style: rotary_dim=%d must be "
|
||||
"divisible by %d for head_size=%d and "
|
||||
"is_neox=%s.",
|
||||
rotary_dim,
|
||||
rotary_alignment,
|
||||
layer.head_size,
|
||||
neox,
|
||||
)
|
||||
continue
|
||||
for quant_q in [False, True]:
|
||||
QkNormMRopeKvCachePattern(
|
||||
layer=layer,
|
||||
eps=epsilon,
|
||||
is_neox=neox,
|
||||
quant_query=quant_q,
|
||||
rotary_dim=rotary_dim,
|
||||
mrope_section=section,
|
||||
is_interleaved=is_interleaved,
|
||||
).register(self.patterns)
|
||||
|
||||
# Opaque LayerName is a pattern wildcard on torch >= 2.11, so
|
||||
# homogeneous attention layers produce duplicate search patterns.
|
||||
# As in the other LayerName-aware fusion passes, one supported
|
||||
# layer registers all shape/style variants for the model.
|
||||
if _USE_LAYERNAME:
|
||||
break
|
||||
|
||||
self.dump_patterns(config, self.patterns)
|
||||
|
||||
@@ -478,8 +958,8 @@ class QkNormRopeKvCacheFusionPass(VllmPatternMatcherPass):
|
||||
def __call__(self, graph: fx.Graph) -> None:
|
||||
self.matched_count = self.patterns.apply(graph)
|
||||
logger.info(
|
||||
"QK-Norm+RoPE+KVCache fusion: replaced %s pattern(s) "
|
||||
"with AITER fused_qk_norm_rope_cache_pts_quant_shuffle",
|
||||
"QK-Norm+RoPE/MRoPE+KVCache fusion: replaced %s pattern(s) "
|
||||
"with an AITER fused attention-prologue kernel",
|
||||
self.matched_count,
|
||||
)
|
||||
|
||||
@@ -487,4 +967,10 @@ class QkNormRopeKvCacheFusionPass(VllmPatternMatcherPass):
|
||||
return compile_range.end <= self.max_token_num
|
||||
|
||||
def uuid(self) -> str:
|
||||
return VllmInductorPass.hash_source(self, QkNormRopeKvCachePattern)
|
||||
return VllmInductorPass.hash_source(
|
||||
self,
|
||||
QkNormRopeKvCachePattern,
|
||||
QkNormMRopeKvCachePattern,
|
||||
_discover_mrope_configs,
|
||||
repr(self.mrope_configs),
|
||||
)
|
||||
|
||||
@@ -154,15 +154,15 @@ class PassConfig:
|
||||
fuse_rope_kvcache: bool = None # type: ignore[assignment]
|
||||
"""Fuse the QK rope + KV cache ops."""
|
||||
fuse_qk_norm_rope_kvcache: bool = Field(default=None) # type: ignore[assignment]
|
||||
"""Fuse QK RMSNorm + RoPE + KV cache update into a single AITER HIP
|
||||
"""Fuse QK RMSNorm + RoPE/MRoPE + KV cache update into an AITER HIP
|
||||
kernel. Supersedes both enable_qk_norm_rope_fusion and fuse_rope_kvcache
|
||||
for layers that support it. Auto-enabled at O1+ on ROCm for models
|
||||
with QK-norm (e.g. Qwen3-MoE)."""
|
||||
for layers that support it. Auto-enabled at O2+ on ROCm for models
|
||||
with QK-norm (e.g. Qwen3-MoE and Qwen3-VL-class architectures)."""
|
||||
|
||||
rope_kvcache_fusion_max_token_num: int = 256
|
||||
"""The threshold for ROCm AITER RoPE+KVCache fusion e.g. for small batch decode.
|
||||
Larger batch sizes e.g. during prefill will use the unfused kernels.
|
||||
Also applies to the fused QK-Norm+RoPE+KVCache pass.
|
||||
Also applies to the fused QK-Norm+RoPE/MRoPE+KVCache pass.
|
||||
"""
|
||||
|
||||
fi_allreduce_fusion_max_size_mb: float | None = None
|
||||
|
||||
+50
-1
@@ -242,7 +242,7 @@ def enable_mla_dual_rms_norm_fusion(cfg: "VllmConfig") -> bool:
|
||||
|
||||
|
||||
def enable_qk_norm_rope_kvcache(cfg: "VllmConfig") -> bool:
|
||||
"""Enable fused QK-norm + RoPE + KV cache update on ROCm with AITER."""
|
||||
"""Enable fused QK-norm + RoPE/MRoPE + KV cache update with AITER."""
|
||||
from vllm._aiter_ops import rocm_aiter_ops
|
||||
|
||||
if not rocm_aiter_ops.is_enabled():
|
||||
@@ -776,6 +776,45 @@ class VllmConfig:
|
||||
architectures = set(model_config.architectures)
|
||||
return bool(architectures & default_breakable_cudagraph_architectures())
|
||||
|
||||
def _uses_breakable_cudagraph_for_batch_invariance(self) -> bool:
|
||||
"""Avoid freezing runtime-M tile lookup in compiled forward (#54243).
|
||||
Breakable graphs look up tuned bf16, unquantized qkv/o/gate_up/down tiles
|
||||
at capture; lm_head runs outside compiled forward and does not benefit."""
|
||||
from vllm.model_executor.determinism import batch_invariant_configs as bi
|
||||
from vllm.platforms import current_platform
|
||||
|
||||
model = self.model_config
|
||||
if (
|
||||
not envs.VLLM_BATCH_INVARIANT
|
||||
or model is None
|
||||
or model.enforce_eager
|
||||
or model.dtype != torch.bfloat16
|
||||
or model.quantization is not None
|
||||
or not current_platform.is_cuda()
|
||||
# Sequence parallelism / async TP are torch.compile passes.
|
||||
or self.compilation_config.pass_config.enable_sp
|
||||
or self.compilation_config.pass_config.fuse_gemm_comms
|
||||
):
|
||||
return False
|
||||
family = bi._get_tuned_matmul_arch_family(
|
||||
current_platform.get_device_capability()
|
||||
)
|
||||
if family is None or family not in bi._BATCH_INVARIANT_MATMUL_TUNED_CONFIGS:
|
||||
return False
|
||||
table = bi._BATCH_INVARIANT_MATMUL_TUNED_CONFIGS[family]
|
||||
parallel = self.parallel_config
|
||||
tp = parallel.tensor_parallel_size
|
||||
hidden = model.get_hidden_size()
|
||||
head = model.get_head_size()
|
||||
heads = model.get_num_attention_heads(parallel)
|
||||
kv_heads = model.get_num_kv_heads(parallel)
|
||||
shapes = [((heads + 2 * kv_heads) * head, hidden), (hidden, heads * head)]
|
||||
intermediate = getattr(model.hf_text_config, "intermediate_size", None)
|
||||
# Per-layer sizes (e.g. Gemma3n) are not modeled.
|
||||
if isinstance(intermediate, int):
|
||||
shapes += [(2 * intermediate // tp, hidden), (hidden, intermediate // tp)]
|
||||
return any(shape in table for shape in shapes)
|
||||
|
||||
def _maybe_enable_breakable_cudagraph(self) -> bool:
|
||||
if (
|
||||
"VLLM_USE_BREAKABLE_CUDAGRAPH" not in os.environ
|
||||
@@ -786,6 +825,16 @@ class VllmConfig:
|
||||
"Auto-enabling VLLM_USE_BREAKABLE_CUDAGRAPH=1. "
|
||||
"Set VLLM_USE_BREAKABLE_CUDAGRAPH=0 to opt out."
|
||||
)
|
||||
elif (
|
||||
"VLLM_USE_BREAKABLE_CUDAGRAPH" not in os.environ
|
||||
and envs.VLLM_BATCH_INVARIANT
|
||||
and self._uses_breakable_cudagraph_for_batch_invariance()
|
||||
):
|
||||
os.environ["VLLM_USE_BREAKABLE_CUDAGRAPH"] = "1"
|
||||
logger.info_once(
|
||||
"VLLM_BATCH_INVARIANT=1: auto-enabling VLLM_USE_BREAKABLE_CUDAGRAPH=1. "
|
||||
"Set VLLM_USE_BREAKABLE_CUDAGRAPH=0 to opt out."
|
||||
)
|
||||
|
||||
from vllm.compilation.breakable_cudagraph import (
|
||||
is_breakable_cudagraph_enabled,
|
||||
|
||||
@@ -103,6 +103,7 @@ class NixlPushConnectorWorker(NixlBaseConnectorWorker):
|
||||
# (``_pop_done_transfers``); guarded by
|
||||
# ``_sending_transfers_lock``.
|
||||
self._sending_transfers = defaultdict[ReqId, list[TransferHandle]](list)
|
||||
self._send_failures: set[ReqId] = set()
|
||||
self._sending_transfers_lock = threading.Lock()
|
||||
|
||||
# Writer-thread owned matching state.
|
||||
@@ -162,6 +163,7 @@ class NixlPushConnectorWorker(NixlBaseConnectorWorker):
|
||||
for handle in handles:
|
||||
self.nixl_wrapper.release_xfer_handle(handle)
|
||||
self._sending_transfers.clear()
|
||||
self._send_failures.clear()
|
||||
super().shutdown()
|
||||
|
||||
# --- Engine-main-thread entry point -------------------------------- #
|
||||
@@ -728,6 +730,8 @@ class NixlPushConnectorWorker(NixlBaseConnectorWorker):
|
||||
# don't have a ``_recving_metadata`` entry to invalidate, so
|
||||
# we just release the handle and let the engine reschedule
|
||||
# via the lease / watchdog.
|
||||
with self._sending_transfers_lock:
|
||||
self._send_failures.add(request_id)
|
||||
if not self._handle_failed_transfer(request_id, handle):
|
||||
return handle
|
||||
return None
|
||||
@@ -811,13 +815,16 @@ class NixlPushConnectorWorker(NixlBaseConnectorWorker):
|
||||
done_pushing, failed_pushing = self._pop_done_transfers(
|
||||
self._sending_transfers
|
||||
)
|
||||
# A failed send must never be reported as done: its blocks
|
||||
# are freed via the lease / watchdog instead.
|
||||
done_pushing = {
|
||||
req_id
|
||||
for req_id in done_pushing - failed_pushing
|
||||
if req_id in self._recving_metadata
|
||||
}
|
||||
# Remember failures until the final sibling WRITE completes.
|
||||
self._send_failures.update(failed_pushing)
|
||||
successful = {
|
||||
req_id
|
||||
for req_id in done_pushing - self._send_failures
|
||||
if req_id in self._reqs_to_send or req_id in self._reqs_to_process
|
||||
}
|
||||
self._send_failures.difference_update(done_pushing | done_sending)
|
||||
# Expired requests were already reported, even if their WRITEs finish later.
|
||||
done_pushing = successful
|
||||
for req_id in done_pushing:
|
||||
self._reqs_to_send.pop(req_id, None)
|
||||
self._reqs_to_process.discard(req_id)
|
||||
|
||||
@@ -116,9 +116,7 @@ class OpenAIServingCompletion(GenerateBaseServing):
|
||||
See https://platform.openai.com/docs/api-reference/completions/create
|
||||
for the API specification. This API mimics the OpenAI Completion API.
|
||||
|
||||
NOTE: Currently we do not support the following feature:
|
||||
- suffix (the language models we currently support do not support
|
||||
suffix)
|
||||
NOTE: suffix is only supported by models that implement FIM rendering.
|
||||
"""
|
||||
return await self._with_kv_transfer_rejection_cleanup(
|
||||
self._create_completion(request, raw_request), request, raw_request
|
||||
|
||||
@@ -31,11 +31,14 @@ class HummingLinearKernel(MPLinearKernel):
|
||||
)
|
||||
|
||||
name_map = {"weight": self.w_q_name, "weight_scale": self.w_s_name}
|
||||
if self.w_zp_name is not None and hasattr(layer, self.w_zp_name):
|
||||
name_map["zero_point"] = self.w_zp_name
|
||||
group_size = self.config.group_size
|
||||
quant_config = {
|
||||
"quant_method": "humming",
|
||||
"dtype": "int" + str(self.config.weight_type.size_bits),
|
||||
"group_size": 0 if group_size == -1 else group_size,
|
||||
"has_zero_point": self.config.zero_points,
|
||||
}
|
||||
|
||||
if self.config.zero_points:
|
||||
|
||||
@@ -337,6 +337,25 @@ class TritonExperts(LoRAExpertsMixin, mk.FusedMoEExpertsModular):
|
||||
)
|
||||
intermediate_cache3 = _resize_cache(workspace2, (num_tokens, top_k_num, K))
|
||||
|
||||
# Under EP, drop the top-k slots routed to remote experts at alignment
|
||||
# time instead of launching `off_experts == -1` GEMM blocks that only
|
||||
# write zeros; the pad-aware `moe_sum` below skips the same slots.
|
||||
# Their rows in the workspace caches are then never written, so every
|
||||
# consumer of those caches must be row-local: a dynamic per-tensor
|
||||
# scale for the second GEMM's activation would take its amax over the
|
||||
# untouched rows, and LoRA aligns the full `topk_ids` itself.
|
||||
a2_scale_is_global = (
|
||||
self.quant_dtype is not None
|
||||
and a2_scale is None
|
||||
and self.block_shape is None
|
||||
and (not self.per_act_token_quant or self.quantization_emulation)
|
||||
)
|
||||
skip_invalid = (
|
||||
expert_map is not None
|
||||
and self._lora_context is None
|
||||
and not a2_scale_is_global
|
||||
)
|
||||
|
||||
# Include fused shared-expert rows while preserving EP remapping.
|
||||
num_align_experts = w1.shape[0] if expert_map is None else global_num_experts
|
||||
sorted_token_ids, expert_ids, num_tokens_post_padded = (
|
||||
@@ -350,6 +369,7 @@ class TritonExperts(LoRAExpertsMixin, mk.FusedMoEExpertsModular):
|
||||
use_int8_w8a16=self.quant_config.use_int8_w8a16,
|
||||
use_int4_w4a16=self.quant_config.use_int4_w4a16,
|
||||
block_shape=self.block_shape,
|
||||
ignore_invalid_experts=skip_invalid,
|
||||
)
|
||||
)
|
||||
|
||||
@@ -585,10 +605,21 @@ class TritonExperts(LoRAExpertsMixin, mk.FusedMoEExpertsModular):
|
||||
)
|
||||
|
||||
# separate function is required for MoE + LoRA
|
||||
self.moe_sum(intermediate_cache3, output)
|
||||
self.moe_sum(intermediate_cache3, output, topk_ids, expert_map)
|
||||
|
||||
def moe_sum(self, input: torch.Tensor, output: torch.Tensor) -> None:
|
||||
ops.moe_sum(input, output)
|
||||
def moe_sum(
|
||||
self,
|
||||
input: torch.Tensor,
|
||||
output: torch.Tensor,
|
||||
topk_ids: torch.Tensor | None = None,
|
||||
expert_map: torch.Tensor | None = None,
|
||||
) -> None:
|
||||
if expert_map is not None:
|
||||
# Skip the slots whose expert is not on this rank: the rows the
|
||||
# alignment dropped, or the zeros the `-1` blocks wrote.
|
||||
ops.moe_sum(input, output, topk_ids, expert_map)
|
||||
else:
|
||||
ops.moe_sum(input, output)
|
||||
|
||||
|
||||
class TritonWNA16Experts(TritonExperts):
|
||||
|
||||
@@ -41,6 +41,7 @@ if current_platform.is_cuda():
|
||||
def _dsv4_topk_kernel(
|
||||
gating_output_ptr,
|
||||
correction_bias_ptr,
|
||||
is_padding_ptr,
|
||||
topk_weights_ptr,
|
||||
topk_ids_ptr,
|
||||
routed_scaling_factor,
|
||||
@@ -49,6 +50,7 @@ if current_platform.is_cuda():
|
||||
image_sentinel_lo,
|
||||
NUM_EXPERTS: tl.constexpr,
|
||||
BLOCK_N: tl.constexpr,
|
||||
HAS_PADDING: tl.constexpr,
|
||||
HAS_VL: tl.constexpr,
|
||||
launch_pdl: tl.constexpr,
|
||||
):
|
||||
@@ -106,6 +108,11 @@ if current_platform.is_cuda():
|
||||
output_mask = topk_offsets < 6
|
||||
output_offsets = row * 6 + topk_offsets
|
||||
|
||||
if HAS_PADDING:
|
||||
is_padding = tl.load(is_padding_ptr + row)
|
||||
selected_weights = tl.where(is_padding, 0.0, selected_weights)
|
||||
selected_ids = tl.where(is_padding, -1, selected_ids)
|
||||
|
||||
if launch_pdl:
|
||||
tl.extra.cuda.gdc_launch_dependents()
|
||||
|
||||
@@ -118,11 +125,20 @@ def dsv4_topk(
|
||||
correction_bias: torch.Tensor,
|
||||
indices_dtype: torch.dtype,
|
||||
routed_scaling_factor: float,
|
||||
is_padding: torch.Tensor | None = None,
|
||||
input_ids: torch.Tensor | None = None,
|
||||
bias_vl: torch.Tensor | None = None,
|
||||
image_sentinel_lo: int = 0,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
num_tokens, num_experts = gating_output.shape
|
||||
if is_padding is not None:
|
||||
assert is_padding.dtype == torch.bool
|
||||
assert is_padding.shape == (num_tokens,)
|
||||
assert is_padding.device == gating_output.device
|
||||
assert is_padding.is_contiguous()
|
||||
assert indices_dtype in (torch.int32, torch.int64), (
|
||||
"Padding requires a signed indices dtype for the -1 sentinel."
|
||||
)
|
||||
has_vl = bias_vl is not None and image_sentinel_lo > 0
|
||||
if bias_vl is not None:
|
||||
assert input_ids is not None, "bias_vl routing requires input_ids"
|
||||
@@ -136,6 +152,7 @@ def dsv4_topk(
|
||||
_dsv4_topk_kernel[(num_tokens,)](
|
||||
gating_output,
|
||||
correction_bias,
|
||||
is_padding,
|
||||
topk_weights,
|
||||
topk_ids,
|
||||
routed_scaling_factor,
|
||||
@@ -144,6 +161,7 @@ def dsv4_topk(
|
||||
image_sentinel_lo,
|
||||
NUM_EXPERTS=num_experts,
|
||||
BLOCK_N=triton.next_power_of_2(num_experts),
|
||||
HAS_PADDING=is_padding is not None,
|
||||
HAS_VL=has_vl,
|
||||
num_warps=1,
|
||||
launch_pdl=current_platform.is_arch_support_pdl(),
|
||||
|
||||
@@ -148,9 +148,13 @@ def fused_topk_bias(
|
||||
)
|
||||
|
||||
output_indices_dtype = torch.int32 if indices_type is None else indices_type
|
||||
padding_mask = _get_padding_mask(gating_output.shape[0])
|
||||
if (
|
||||
scoring_func == "sqrtsoftplus"
|
||||
and hash_indices_table is None
|
||||
# dsv4_topk marks padded rows with a -1 sentinel, which unsigned
|
||||
# indices cannot represent; keep the generic path for that pair.
|
||||
and (padding_mask is None or output_indices_dtype != torch.uint32)
|
||||
and can_use_dsv4_topk(
|
||||
gating_output,
|
||||
e_score_correction_bias,
|
||||
@@ -165,6 +169,7 @@ def fused_topk_bias(
|
||||
e_score_correction_bias,
|
||||
output_indices_dtype,
|
||||
routed_scaling_factor,
|
||||
is_padding=padding_mask,
|
||||
input_ids=input_tokens,
|
||||
bias_vl=bias_vl,
|
||||
image_sentinel_lo=image_sentinel_lo,
|
||||
|
||||
@@ -787,6 +787,7 @@ class CompressedTensorsConfig(QuantizationConfig):
|
||||
num_bits=weight_quant.num_bits,
|
||||
strategy=weight_quant.strategy,
|
||||
group_size=weight_quant.group_size,
|
||||
symmetric=weight_quant.symmetric,
|
||||
input_quant=input_quant,
|
||||
layer_name=layer_name,
|
||||
quant_format=format,
|
||||
@@ -799,6 +800,7 @@ class CompressedTensorsConfig(QuantizationConfig):
|
||||
num_bits=weight_quant.num_bits,
|
||||
strategy=weight_quant.strategy,
|
||||
group_size=weight_quant.group_size,
|
||||
symmetric=weight_quant.symmetric,
|
||||
input_quant=input_quant,
|
||||
layer_name=layer_name,
|
||||
quant_format=format,
|
||||
|
||||
+9
-1
@@ -42,7 +42,15 @@ WNA16_SUPPORTED_TYPES_MAP = {
|
||||
7: scalar_types.uint7b64,
|
||||
8: scalar_types.uint8b128,
|
||||
}
|
||||
WNA16_ZP_SUPPORTED_TYPES_MAP = {4: scalar_types.uint4, 8: scalar_types.uint8}
|
||||
WNA16_ZP_SUPPORTED_TYPES_MAP = {
|
||||
2: scalar_types.uint2,
|
||||
3: scalar_types.uint3,
|
||||
4: scalar_types.uint4,
|
||||
5: scalar_types.uint5,
|
||||
6: scalar_types.uint6,
|
||||
7: scalar_types.uint7,
|
||||
8: scalar_types.uint8,
|
||||
}
|
||||
WNA16_SUPPORTED_BITS = list(WNA16_SUPPORTED_TYPES_MAP.keys())
|
||||
|
||||
|
||||
|
||||
+59
-14
@@ -1,11 +1,11 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Weight N-bit INT scheme with symmetric INT4 activation quant via Humming.
|
||||
"""Weight N-bit INT scheme with INT4 activation quant via Humming.
|
||||
|
||||
Handles compressed-tensors pack-quantized INT weight checkpoints (2-8 bit)
|
||||
with INT4 symmetric dynamic per-token/per-group input activation
|
||||
quantization. Static, per-tensor, and asymmetric activation quantization
|
||||
are not supported.
|
||||
quantization. Supports both symmetric and asymmetric weight quantization.
|
||||
Static, per-tensor, and asymmetric activation quantization are not supported.
|
||||
"""
|
||||
|
||||
import math
|
||||
@@ -28,6 +28,7 @@ from vllm.model_executor.layers.quantization.compressed_tensors.schemes import (
|
||||
)
|
||||
from vllm.model_executor.layers.quantization.compressed_tensors.schemes.compressed_tensors_wNa16 import ( # noqa: E501
|
||||
WNA16_SUPPORTED_TYPES_MAP,
|
||||
WNA16_ZP_SUPPORTED_TYPES_MAP,
|
||||
)
|
||||
from vllm.model_executor.layers.quantization.utils.marlin_utils import (
|
||||
marlin_repeat_scales_on_all_ranks,
|
||||
@@ -36,6 +37,7 @@ from vllm.model_executor.parameter import (
|
||||
BasevLLMParameter,
|
||||
ChannelQuantScaleParameter,
|
||||
GroupQuantScaleParameter,
|
||||
PackedColumnParameter,
|
||||
PackedvLLMParameter,
|
||||
)
|
||||
|
||||
@@ -52,6 +54,7 @@ class CompressedTensorsWNA4Int(CompressedTensorsScheme):
|
||||
num_bits: int,
|
||||
strategy: str,
|
||||
group_size: int | None = None,
|
||||
symmetric: bool = True,
|
||||
input_quant: QuantizationArgs | None = None,
|
||||
layer_name: str | None = None,
|
||||
quant_format: str = "pack-quantized",
|
||||
@@ -60,6 +63,7 @@ class CompressedTensorsWNA4Int(CompressedTensorsScheme):
|
||||
self.pack_factor = Fraction(32, num_bits)
|
||||
self.strategy = strategy
|
||||
self.group_size = -1 if group_size is None else group_size
|
||||
self.symmetric = symmetric
|
||||
self.input_quant = input_quant
|
||||
self.layer_name = layer_name
|
||||
self.quant_format = quant_format
|
||||
@@ -69,7 +73,19 @@ class CompressedTensorsWNA4Int(CompressedTensorsScheme):
|
||||
f"Unsupported num_bits = {num_bits} for WNA4Int; "
|
||||
f"supported = {sorted(WNA16_SUPPORTED_TYPES_MAP)}"
|
||||
)
|
||||
self.quant_type = WNA16_SUPPORTED_TYPES_MAP[num_bits]
|
||||
|
||||
if not self.symmetric and num_bits not in WNA16_ZP_SUPPORTED_TYPES_MAP:
|
||||
raise ValueError(
|
||||
f"Asymmetric quantization not supported for "
|
||||
f"num_bits = {num_bits}. Supported: "
|
||||
f"{list(WNA16_ZP_SUPPORTED_TYPES_MAP)}"
|
||||
)
|
||||
|
||||
self.quant_type = (
|
||||
WNA16_ZP_SUPPORTED_TYPES_MAP[num_bits]
|
||||
if not self.symmetric
|
||||
else WNA16_SUPPORTED_TYPES_MAP[num_bits]
|
||||
)
|
||||
|
||||
if input_quant is not None:
|
||||
if not input_quant.symmetric:
|
||||
@@ -143,7 +159,7 @@ class CompressedTensorsWNA4Int(CompressedTensorsScheme):
|
||||
weight_type=self.quant_type,
|
||||
act_type=params_dtype,
|
||||
group_size=self.group_size,
|
||||
zero_points=False,
|
||||
zero_points=not self.symmetric,
|
||||
)
|
||||
|
||||
kernel_type = choose_mp_linear_kernel(mp_config)
|
||||
@@ -151,12 +167,6 @@ class CompressedTensorsWNA4Int(CompressedTensorsScheme):
|
||||
logger.info("Using %s for CompressedTensorsWNA4Int", kernel_type.__name__)
|
||||
self._kernel_backends_being_used.add(kernel_type.__name__)
|
||||
|
||||
self.kernel = kernel_type(
|
||||
mp_config,
|
||||
w_q_param_name="weight_packed",
|
||||
w_s_param_name="weight_scale",
|
||||
)
|
||||
|
||||
input_quant_config = self._build_input_quant_config()
|
||||
if input_quant_config is not None:
|
||||
layer._humming_input_quant_config = input_quant_config
|
||||
@@ -167,10 +177,10 @@ class CompressedTensorsWNA4Int(CompressedTensorsScheme):
|
||||
partition_scales = not marlin_repeat_scales_on_all_ranks(
|
||||
self.group_size, row_parallel
|
||||
)
|
||||
scales_size = input_size // group_size
|
||||
scales_and_zp_size = input_size // group_size
|
||||
if partition_scales:
|
||||
assert input_size_per_partition % group_size == 0
|
||||
scales_size = input_size_per_partition // group_size
|
||||
scales_and_zp_size = input_size_per_partition // group_size
|
||||
|
||||
packed_input_dim = math.ceil(input_size_per_partition * self.num_bits / 32)
|
||||
layer.register_parameter(
|
||||
@@ -190,7 +200,7 @@ class CompressedTensorsWNA4Int(CompressedTensorsScheme):
|
||||
)
|
||||
|
||||
scale_data = torch.empty(
|
||||
output_size_per_partition, scales_size, dtype=params_dtype
|
||||
output_size_per_partition, scales_and_zp_size, dtype=params_dtype
|
||||
)
|
||||
if partition_scales:
|
||||
weight_scale = GroupQuantScaleParameter(
|
||||
@@ -207,6 +217,34 @@ class CompressedTensorsWNA4Int(CompressedTensorsScheme):
|
||||
)
|
||||
layer.register_parameter("weight_scale", weight_scale)
|
||||
|
||||
if not self.symmetric:
|
||||
packed_output_dim = math.ceil(
|
||||
output_size_per_partition * self.num_bits / 32
|
||||
)
|
||||
zeros_data = torch.zeros(
|
||||
packed_output_dim,
|
||||
scales_and_zp_size,
|
||||
dtype=torch.int32,
|
||||
)
|
||||
if not partition_scales:
|
||||
qzeros = PackedColumnParameter(
|
||||
output_dim=0,
|
||||
packed_dim=0,
|
||||
packed_factor=self.pack_factor,
|
||||
weight_loader=weight_loader,
|
||||
data=zeros_data,
|
||||
)
|
||||
else:
|
||||
qzeros = PackedvLLMParameter(
|
||||
input_dim=1,
|
||||
output_dim=0,
|
||||
packed_dim=0,
|
||||
packed_factor=self.pack_factor,
|
||||
weight_loader=weight_loader,
|
||||
data=zeros_data,
|
||||
)
|
||||
layer.register_parameter("weight_zero_point", qzeros)
|
||||
|
||||
layer.register_parameter(
|
||||
"weight_shape",
|
||||
BasevLLMParameter(
|
||||
@@ -215,6 +253,13 @@ class CompressedTensorsWNA4Int(CompressedTensorsScheme):
|
||||
),
|
||||
)
|
||||
|
||||
self.kernel = kernel_type(
|
||||
mp_config,
|
||||
w_q_param_name="weight_packed",
|
||||
w_s_param_name="weight_scale",
|
||||
w_zp_param_name="weight_zero_point",
|
||||
)
|
||||
|
||||
def process_weights_after_loading(self, layer: torch.nn.Module) -> None:
|
||||
self.kernel.process_weights_after_loading(layer)
|
||||
|
||||
|
||||
+59
-14
@@ -1,11 +1,11 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Weight N-bit INT scheme with symmetric INT8 activation quant via Humming.
|
||||
"""Weight N-bit INT scheme with INT8 activation quant via Humming.
|
||||
|
||||
Handles compressed-tensors pack-quantized INT weight checkpoints (2-8 bit)
|
||||
with INT8 symmetric dynamic per-token/per-group input activation
|
||||
quantization. Static, per-tensor, and asymmetric activation quantization
|
||||
are not supported.
|
||||
quantization. Supports both symmetric and asymmetric weight quantization.
|
||||
Static, per-tensor, and asymmetric activation quantization are not supported.
|
||||
"""
|
||||
|
||||
import math
|
||||
@@ -28,6 +28,7 @@ from vllm.model_executor.layers.quantization.compressed_tensors.schemes import (
|
||||
)
|
||||
from vllm.model_executor.layers.quantization.compressed_tensors.schemes.compressed_tensors_wNa16 import ( # noqa: E501
|
||||
WNA16_SUPPORTED_TYPES_MAP,
|
||||
WNA16_ZP_SUPPORTED_TYPES_MAP,
|
||||
)
|
||||
from vllm.model_executor.layers.quantization.utils.marlin_utils import (
|
||||
marlin_repeat_scales_on_all_ranks,
|
||||
@@ -36,6 +37,7 @@ from vllm.model_executor.parameter import (
|
||||
BasevLLMParameter,
|
||||
ChannelQuantScaleParameter,
|
||||
GroupQuantScaleParameter,
|
||||
PackedColumnParameter,
|
||||
PackedvLLMParameter,
|
||||
)
|
||||
|
||||
@@ -52,6 +54,7 @@ class CompressedTensorsWNA8Int(CompressedTensorsScheme):
|
||||
num_bits: int,
|
||||
strategy: str,
|
||||
group_size: int | None = None,
|
||||
symmetric: bool = True,
|
||||
input_quant: QuantizationArgs | None = None,
|
||||
layer_name: str | None = None,
|
||||
quant_format: str = "pack-quantized",
|
||||
@@ -60,6 +63,7 @@ class CompressedTensorsWNA8Int(CompressedTensorsScheme):
|
||||
self.pack_factor = Fraction(32, num_bits)
|
||||
self.strategy = strategy
|
||||
self.group_size = -1 if group_size is None else group_size
|
||||
self.symmetric = symmetric
|
||||
self.input_quant = input_quant
|
||||
self.layer_name = layer_name
|
||||
self.quant_format = quant_format
|
||||
@@ -69,7 +73,19 @@ class CompressedTensorsWNA8Int(CompressedTensorsScheme):
|
||||
f"Unsupported num_bits = {num_bits} for WNA8Int; "
|
||||
f"supported = {sorted(WNA16_SUPPORTED_TYPES_MAP)}"
|
||||
)
|
||||
self.quant_type = WNA16_SUPPORTED_TYPES_MAP[num_bits]
|
||||
|
||||
if not self.symmetric and num_bits not in WNA16_ZP_SUPPORTED_TYPES_MAP:
|
||||
raise ValueError(
|
||||
f"Asymmetric quantization not supported for "
|
||||
f"num_bits = {num_bits}. Supported: "
|
||||
f"{list(WNA16_ZP_SUPPORTED_TYPES_MAP)}"
|
||||
)
|
||||
|
||||
self.quant_type = (
|
||||
WNA16_ZP_SUPPORTED_TYPES_MAP[num_bits]
|
||||
if not self.symmetric
|
||||
else WNA16_SUPPORTED_TYPES_MAP[num_bits]
|
||||
)
|
||||
|
||||
if input_quant is not None:
|
||||
if not input_quant.symmetric:
|
||||
@@ -143,7 +159,7 @@ class CompressedTensorsWNA8Int(CompressedTensorsScheme):
|
||||
weight_type=self.quant_type,
|
||||
act_type=params_dtype,
|
||||
group_size=self.group_size,
|
||||
zero_points=False,
|
||||
zero_points=not self.symmetric,
|
||||
)
|
||||
|
||||
kernel_type = choose_mp_linear_kernel(mp_config)
|
||||
@@ -151,12 +167,6 @@ class CompressedTensorsWNA8Int(CompressedTensorsScheme):
|
||||
logger.info("Using %s for CompressedTensorsWNA8Int", kernel_type.__name__)
|
||||
self._kernel_backends_being_used.add(kernel_type.__name__)
|
||||
|
||||
self.kernel = kernel_type(
|
||||
mp_config,
|
||||
w_q_param_name="weight_packed",
|
||||
w_s_param_name="weight_scale",
|
||||
)
|
||||
|
||||
input_quant_config = self._build_input_quant_config()
|
||||
if input_quant_config is not None:
|
||||
layer._humming_input_quant_config = input_quant_config
|
||||
@@ -167,10 +177,10 @@ class CompressedTensorsWNA8Int(CompressedTensorsScheme):
|
||||
partition_scales = not marlin_repeat_scales_on_all_ranks(
|
||||
self.group_size, row_parallel
|
||||
)
|
||||
scales_size = input_size // group_size
|
||||
scales_and_zp_size = input_size // group_size
|
||||
if partition_scales:
|
||||
assert input_size_per_partition % group_size == 0
|
||||
scales_size = input_size_per_partition // group_size
|
||||
scales_and_zp_size = input_size_per_partition // group_size
|
||||
|
||||
packed_input_dim = math.ceil(input_size_per_partition * self.num_bits / 32)
|
||||
layer.register_parameter(
|
||||
@@ -190,7 +200,7 @@ class CompressedTensorsWNA8Int(CompressedTensorsScheme):
|
||||
)
|
||||
|
||||
scale_data = torch.empty(
|
||||
output_size_per_partition, scales_size, dtype=params_dtype
|
||||
output_size_per_partition, scales_and_zp_size, dtype=params_dtype
|
||||
)
|
||||
if partition_scales:
|
||||
weight_scale = GroupQuantScaleParameter(
|
||||
@@ -207,6 +217,34 @@ class CompressedTensorsWNA8Int(CompressedTensorsScheme):
|
||||
)
|
||||
layer.register_parameter("weight_scale", weight_scale)
|
||||
|
||||
if not self.symmetric:
|
||||
packed_output_dim = math.ceil(
|
||||
output_size_per_partition * self.num_bits / 32
|
||||
)
|
||||
zeros_data = torch.zeros(
|
||||
packed_output_dim,
|
||||
scales_and_zp_size,
|
||||
dtype=torch.int32,
|
||||
)
|
||||
if not partition_scales:
|
||||
qzeros = PackedColumnParameter(
|
||||
output_dim=0,
|
||||
packed_dim=0,
|
||||
packed_factor=self.pack_factor,
|
||||
weight_loader=weight_loader,
|
||||
data=zeros_data,
|
||||
)
|
||||
else:
|
||||
qzeros = PackedvLLMParameter(
|
||||
input_dim=1,
|
||||
output_dim=0,
|
||||
packed_dim=0,
|
||||
packed_factor=self.pack_factor,
|
||||
weight_loader=weight_loader,
|
||||
data=zeros_data,
|
||||
)
|
||||
layer.register_parameter("weight_zero_point", qzeros)
|
||||
|
||||
layer.register_parameter(
|
||||
"weight_shape",
|
||||
BasevLLMParameter(
|
||||
@@ -215,6 +253,13 @@ class CompressedTensorsWNA8Int(CompressedTensorsScheme):
|
||||
),
|
||||
)
|
||||
|
||||
self.kernel = kernel_type(
|
||||
mp_config,
|
||||
w_q_param_name="weight_packed",
|
||||
w_s_param_name="weight_scale",
|
||||
w_zp_param_name="weight_zero_point",
|
||||
)
|
||||
|
||||
def process_weights_after_loading(self, layer: torch.nn.Module) -> None:
|
||||
self.kernel.process_weights_after_loading(layer)
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ import fnmatch
|
||||
from typing import TYPE_CHECKING, Any, NamedTuple, cast
|
||||
|
||||
import torch
|
||||
from transformers import PretrainedConfig
|
||||
|
||||
from vllm.logger import init_logger
|
||||
from vllm.model_executor.layers.attention import Attention
|
||||
@@ -82,10 +81,6 @@ __all__ = ["QuarkLinearMethod"]
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
# model_type values that use dynamic MXFP4 re-quantization for
|
||||
# OCP MX fp4 Quark checkpoints
|
||||
_DEEPSEEK_V3_FAMILY_MODEL_TYPES = frozenset({"deepseek_v3", "deepseek_v32"})
|
||||
|
||||
|
||||
class QuantKeyMatch(NamedTuple):
|
||||
matches: bool
|
||||
@@ -119,37 +114,6 @@ class QuarkConfig(QuantizationConfig):
|
||||
self.kv_cache_group = kv_cache_group
|
||||
self.kv_cache_config = kv_cache_config
|
||||
self.pack_method = pack_method
|
||||
# Note : this flag is kept disabled because the overhead of
|
||||
# dynamic mxfp4 quantization negates the performance gains
|
||||
# that come from shifting to mxfp4. It is left here in case
|
||||
# we want to re-enable it in the future.
|
||||
self.dynamic_mxfp4_quant = False
|
||||
|
||||
def maybe_update_config(
|
||||
self,
|
||||
model_name: str,
|
||||
hf_config: PretrainedConfig | None = None,
|
||||
revision: str | None = None,
|
||||
):
|
||||
"""Enable dynamic MXFP4 only for DeepSeek-V3-family fp4 checkpoints."""
|
||||
if hf_config is None:
|
||||
return
|
||||
|
||||
if (
|
||||
getattr(hf_config, "model_type", None)
|
||||
not in _DEEPSEEK_V3_FAMILY_MODEL_TYPES
|
||||
):
|
||||
return
|
||||
|
||||
quant_config = getattr(hf_config, "quantization_config", None)
|
||||
if isinstance(quant_config, dict):
|
||||
quant_dtype = (
|
||||
quant_config.get("global_quant_config", {})
|
||||
.get("weight", {})
|
||||
.get("dtype")
|
||||
)
|
||||
if quant_dtype == "fp4":
|
||||
self.dynamic_mxfp4_quant = True
|
||||
|
||||
def get_linear_method(self) -> "QuarkLinearMethod":
|
||||
return QuarkLinearMethod(self)
|
||||
@@ -201,17 +165,6 @@ class QuarkConfig(QuantizationConfig):
|
||||
self.get_quant_method_target(prefix, type(layer))
|
||||
)
|
||||
|
||||
exclude_layers = cast(list[str], self.quant_config.get("exclude"))
|
||||
is_ignored = should_ignore_layer(
|
||||
prefix,
|
||||
ignore=exclude_layers,
|
||||
fused_mapping=self.packed_modules_mapping,
|
||||
check_children=isinstance(layer, RoutedExperts),
|
||||
)
|
||||
dynamic_mxfp4_quant = (
|
||||
is_ignored and "self_attn" in prefix and self.dynamic_mxfp4_quant
|
||||
)
|
||||
|
||||
if method_cls is UnquantizedFusedMoEMethod:
|
||||
return UnquantizedFusedMoEMethod(layer.moe_config)
|
||||
if method_cls is UnquantizedLinearMethod:
|
||||
@@ -222,7 +175,6 @@ class QuarkConfig(QuantizationConfig):
|
||||
scheme_cls,
|
||||
weight_quant_key=weight_quant_key,
|
||||
activation_quant_key=activation_quant_key,
|
||||
dynamic_mxfp4_quant=dynamic_mxfp4_quant,
|
||||
weight_config=self._find_matched_config(prefix, type(layer)).get(
|
||||
"weight"
|
||||
),
|
||||
@@ -274,11 +226,6 @@ class QuarkConfig(QuantizationConfig):
|
||||
if is_routed_experts:
|
||||
return None, None, UnquantizedFusedMoEMethod
|
||||
if issubclass(layer_type, LinearBase):
|
||||
if "self_attn" in prefix and self.dynamic_mxfp4_quant:
|
||||
weight_quant_key, activation_key, _ = self.get_scheme_cls(
|
||||
layer_type, prefix
|
||||
)
|
||||
return weight_quant_key, activation_key, QuarkLinearMethod
|
||||
return None, None, UnquantizedLinearMethod
|
||||
return None, None, None
|
||||
if issubclass(layer_type, LinearBase):
|
||||
@@ -956,7 +903,6 @@ class QuarkConfig(QuantizationConfig):
|
||||
scheme_cls: type["QuarkScheme"],
|
||||
weight_quant_key: QuantKey | None,
|
||||
activation_quant_key: QuantKey | None,
|
||||
dynamic_mxfp4_quant: bool = False,
|
||||
weight_config: dict[str, Any] | None = None,
|
||||
) -> "QuarkScheme":
|
||||
"""Construct a Quark scheme selected by get_scheme_cls."""
|
||||
@@ -996,9 +942,6 @@ class QuarkConfig(QuantizationConfig):
|
||||
):
|
||||
kwargs["weight_quant_key"] = weight_quant_key
|
||||
|
||||
if scheme_cls is QuarkOCP_MX:
|
||||
kwargs["dynamic_mxfp4_quant"] = dynamic_mxfp4_quant
|
||||
|
||||
scheme = scheme_cls(**kwargs)
|
||||
|
||||
# Raise error if device does not support the scheme
|
||||
|
||||
@@ -26,10 +26,8 @@ from vllm.model_executor.layers.quantization.utils.quant_utils import (
|
||||
)
|
||||
from vllm.model_executor.parameter import (
|
||||
GroupQuantScaleParameter,
|
||||
ModelWeightParameter,
|
||||
PackedvLLMParameter,
|
||||
)
|
||||
from vllm.model_executor.utils import set_weight_attrs
|
||||
from vllm.platforms import current_platform
|
||||
|
||||
from .quark_scheme import QuarkScheme
|
||||
@@ -46,10 +44,9 @@ class QuarkOCP_MX(QuarkScheme):
|
||||
self,
|
||||
weight_quant_key: QuantKey,
|
||||
activation_quant_key: QuantKey | None,
|
||||
dynamic_mxfp4_quant: bool = False,
|
||||
):
|
||||
super().__init__(weight_quant_key, activation_quant_key)
|
||||
self.dynamic_mxfp4_quant = dynamic_mxfp4_quant
|
||||
|
||||
self.weight_dtype = next(
|
||||
dtype
|
||||
for dtype, quant_key in _WEIGHT_QUANT_KEY_MAP.items()
|
||||
@@ -109,21 +106,9 @@ class QuarkOCP_MX(QuarkScheme):
|
||||
def get_min_capability(cls) -> int:
|
||||
return 70
|
||||
|
||||
def process_dynamic_mxfp4_weights_after_loading(
|
||||
self, layer: torch.nn.Module
|
||||
) -> None:
|
||||
from aiter.ops.triton.quant import dynamic_mxfp4_quant
|
||||
|
||||
w_q, w_s = dynamic_mxfp4_quant(layer.weight)
|
||||
layer.weight_scale = torch.nn.Parameter(w_s, requires_grad=False)
|
||||
layer.weight = torch.nn.Parameter(w_q, requires_grad=False)
|
||||
|
||||
def process_weights_after_loading(self, layer: torch.nn.Module) -> None:
|
||||
layer.weight = torch.nn.Parameter(layer.weight.data, requires_grad=False)
|
||||
|
||||
if self.dynamic_mxfp4_quant:
|
||||
self.process_dynamic_mxfp4_weights_after_loading(layer)
|
||||
|
||||
self.ocp_mx_linear.process_weights_after_loading(layer)
|
||||
|
||||
def create_weights(
|
||||
@@ -145,51 +130,36 @@ class QuarkOCP_MX(QuarkScheme):
|
||||
"tensor-parallel sharding for this layer."
|
||||
)
|
||||
|
||||
if self.dynamic_mxfp4_quant:
|
||||
weight = ModelWeightParameter(
|
||||
data=torch.empty(
|
||||
sum(output_partition_sizes),
|
||||
input_size_per_partition,
|
||||
dtype=params_dtype,
|
||||
),
|
||||
input_dim=1,
|
||||
output_dim=0,
|
||||
weight_loader=weight_loader,
|
||||
)
|
||||
output_size_per_partition = sum(output_partition_sizes)
|
||||
layer.logical_widths = output_partition_sizes
|
||||
|
||||
layer.register_parameter("weight", weight)
|
||||
set_weight_attrs(weight, kwargs)
|
||||
else:
|
||||
output_size_per_partition = sum(output_partition_sizes)
|
||||
layer.logical_widths = output_partition_sizes
|
||||
# WEIGHT
|
||||
weight = PackedvLLMParameter(
|
||||
data=torch.empty(
|
||||
output_size_per_partition,
|
||||
self.get_packed_dim(input_size_per_partition, self.weight_dtype),
|
||||
dtype=torch.uint8,
|
||||
),
|
||||
input_dim=1,
|
||||
output_dim=0,
|
||||
packed_dim=1,
|
||||
packed_factor=self.packed_factor,
|
||||
weight_loader=weight_loader,
|
||||
)
|
||||
layer.register_parameter("weight", weight)
|
||||
|
||||
# WEIGHT
|
||||
weight = PackedvLLMParameter(
|
||||
data=torch.empty(
|
||||
output_size_per_partition,
|
||||
self.get_packed_dim(input_size_per_partition, self.weight_dtype),
|
||||
dtype=torch.uint8,
|
||||
),
|
||||
input_dim=1,
|
||||
output_dim=0,
|
||||
packed_dim=1,
|
||||
packed_factor=self.packed_factor,
|
||||
weight_loader=weight_loader,
|
||||
)
|
||||
layer.register_parameter("weight", weight)
|
||||
|
||||
# WEIGHT SCALE
|
||||
weight_scale = GroupQuantScaleParameter(
|
||||
data=torch.empty(
|
||||
output_size_per_partition,
|
||||
input_size_per_partition // OCP_MX_BLOCK_SIZE,
|
||||
dtype=torch.uint8,
|
||||
),
|
||||
input_dim=1,
|
||||
output_dim=0,
|
||||
weight_loader=weight_loader,
|
||||
)
|
||||
layer.register_parameter("weight_scale", weight_scale)
|
||||
# WEIGHT SCALE
|
||||
weight_scale = GroupQuantScaleParameter(
|
||||
data=torch.empty(
|
||||
output_size_per_partition,
|
||||
input_size_per_partition // OCP_MX_BLOCK_SIZE,
|
||||
dtype=torch.uint8,
|
||||
),
|
||||
input_dim=1,
|
||||
output_dim=0,
|
||||
weight_loader=weight_loader,
|
||||
)
|
||||
layer.register_parameter("weight_scale", weight_scale)
|
||||
|
||||
if self.weight_quant_key == kMxfp4Static:
|
||||
self.ocp_mx_linear = init_mxfp4_linear_kernel(
|
||||
|
||||
@@ -101,6 +101,19 @@ def prepare_humming_linear_layer_config(
|
||||
param_dtype=layer.params_dtype,
|
||||
)
|
||||
|
||||
if (
|
||||
weight_schema.has_zero_point
|
||||
and input_schema.a_dtype is not None
|
||||
and weight_schema.b_dtype.num_bits == input_schema.a_dtype.num_bits
|
||||
):
|
||||
raise ValueError(
|
||||
"Humming does not support asymmetric (zero_point) weights "
|
||||
"when weight and activation bit widths are equal "
|
||||
f"(W{weight_schema.b_dtype.num_bits}"
|
||||
f"A{input_schema.a_dtype.num_bits}). "
|
||||
"Use symmetric weights or a different activation bit width."
|
||||
)
|
||||
|
||||
tensors.update(input_tensors)
|
||||
layer.weight_schema = weight_schema
|
||||
input_schema = check_and_fallback_input_schema(
|
||||
|
||||
@@ -7,6 +7,7 @@ import torch
|
||||
|
||||
from vllm.platforms import current_platform
|
||||
from vllm.triton_utils import tl, triton
|
||||
from vllm.utils.torch_utils import direct_register_custom_op
|
||||
|
||||
from .base import RotaryEmbeddingBase
|
||||
from .yarn_scaling_rope import YaRNScalingRotaryEmbedding, yarn_get_mscale
|
||||
@@ -240,6 +241,62 @@ def apply_interleaved_rope(x: torch.Tensor, mrope_section: list[int]) -> torch.T
|
||||
return torch.where(is_width, x[2], result)
|
||||
|
||||
|
||||
def _mrope_apply(
|
||||
positions: torch.Tensor,
|
||||
query: torch.Tensor,
|
||||
key: torch.Tensor,
|
||||
cos_sin_cache: torch.Tensor,
|
||||
head_size: int,
|
||||
rotary_dim: int,
|
||||
mrope_section_t: int,
|
||||
mrope_section_h: int,
|
||||
mrope_section_w: int,
|
||||
mrope_interleaved: bool,
|
||||
is_neox_style: bool,
|
||||
) -> None:
|
||||
cos_sin = cos_sin_cache[positions]
|
||||
cos, sin = cos_sin.chunk(2, dim=-1)
|
||||
query_out, key_out = triton_mrope(
|
||||
query,
|
||||
key,
|
||||
cos,
|
||||
sin,
|
||||
[mrope_section_t, mrope_section_h, mrope_section_w],
|
||||
head_size,
|
||||
rotary_dim,
|
||||
mrope_interleaved,
|
||||
is_neox_style,
|
||||
)
|
||||
if query_out.data_ptr() != query.data_ptr():
|
||||
query.copy_(query_out.view_as(query))
|
||||
if key_out.data_ptr() != key.data_ptr():
|
||||
key.copy_(key_out.view_as(key))
|
||||
|
||||
|
||||
def _mrope_apply_fake(
|
||||
positions: torch.Tensor,
|
||||
query: torch.Tensor,
|
||||
key: torch.Tensor,
|
||||
cos_sin_cache: torch.Tensor,
|
||||
head_size: int,
|
||||
rotary_dim: int,
|
||||
mrope_section_t: int,
|
||||
mrope_section_h: int,
|
||||
mrope_section_w: int,
|
||||
mrope_interleaved: bool,
|
||||
is_neox_style: bool,
|
||||
) -> None:
|
||||
return
|
||||
|
||||
|
||||
direct_register_custom_op(
|
||||
op_name="mrope",
|
||||
op_func=_mrope_apply,
|
||||
mutates_args=["query", "key"],
|
||||
fake_impl=_mrope_apply_fake,
|
||||
)
|
||||
|
||||
|
||||
class MRotaryEmbedding(RotaryEmbeddingBase):
|
||||
"""Rotary Embedding with Multimodal Sections."""
|
||||
|
||||
@@ -426,6 +483,35 @@ class MRotaryEmbedding(RotaryEmbeddingBase):
|
||||
key = torch.cat((key_rot, key_pass), dim=-1).reshape(key_shape)
|
||||
return query, key
|
||||
|
||||
def forward_hip(
|
||||
self,
|
||||
positions: torch.Tensor,
|
||||
query: torch.Tensor,
|
||||
key: torch.Tensor | None = None,
|
||||
offsets: torch.Tensor | None = None,
|
||||
) -> tuple[torch.Tensor, torch.Tensor | None]:
|
||||
if positions.ndim == 2:
|
||||
assert key is not None
|
||||
assert self.mrope_section
|
||||
query_shape = query.shape
|
||||
key_shape = key.shape
|
||||
cos_sin_cache = self._match_cos_sin_cache_dtype(query)
|
||||
torch.ops.vllm.mrope(
|
||||
positions,
|
||||
query,
|
||||
key,
|
||||
cos_sin_cache,
|
||||
self.head_size,
|
||||
self.rotary_dim,
|
||||
self.mrope_section[0],
|
||||
self.mrope_section[1],
|
||||
self.mrope_section[2],
|
||||
self.mrope_interleaved,
|
||||
self.is_neox_style,
|
||||
)
|
||||
return query.reshape(query_shape), key.reshape(key_shape)
|
||||
return super().forward_hip(positions, query, key)
|
||||
|
||||
def forward_cpu(
|
||||
self,
|
||||
positions: torch.Tensor,
|
||||
|
||||
@@ -498,11 +498,11 @@ class CpuPlatform(Platform):
|
||||
return
|
||||
|
||||
# reconcile attention and mamba page sizes
|
||||
backend_cls = cls._find_non_ssm_backend(vllm_config)
|
||||
if backend_cls is None:
|
||||
backend_classes = cls._find_non_ssm_backends(vllm_config)
|
||||
if not backend_classes:
|
||||
return
|
||||
|
||||
cls._align_hybrid_block_size(vllm_config, backend_cls)
|
||||
cls._align_hybrid_block_size(vllm_config, backend_classes[0])
|
||||
|
||||
@classmethod
|
||||
def discover_numa_topology(cls) -> list[list[int]]:
|
||||
|
||||
+72
-18
@@ -3,6 +3,8 @@
|
||||
import contextlib
|
||||
import enum
|
||||
import functools
|
||||
import itertools
|
||||
import math
|
||||
import os
|
||||
import platform
|
||||
import sys
|
||||
@@ -573,10 +575,10 @@ class Platform:
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def _find_non_ssm_backend(
|
||||
def _find_non_ssm_backends(
|
||||
cls, vllm_config: "VllmConfig"
|
||||
) -> "type[AttentionBackend] | None":
|
||||
"""Find the first non-SSM attention backend from model layers."""
|
||||
) -> "list[type[AttentionBackend]]":
|
||||
"""Distinct non-SSM attention backends, in layer order."""
|
||||
from vllm.config.vllm import get_layers_from_vllm_config
|
||||
from vllm.model_executor.layers.attention_layer_base import (
|
||||
AttentionLayerBase,
|
||||
@@ -586,11 +588,62 @@ class Platform:
|
||||
vllm_config,
|
||||
AttentionLayerBase, # type: ignore[type-abstract]
|
||||
)
|
||||
backends: list[type[AttentionBackend]] = []
|
||||
for layer in attn_layers.values():
|
||||
b = layer.get_attn_backend()
|
||||
if not b.is_ssm():
|
||||
return b
|
||||
return None
|
||||
if not b.is_ssm() and b not in backends:
|
||||
backends.append(b)
|
||||
return backends
|
||||
|
||||
@classmethod
|
||||
def _preferred_block_size_for_backends(
|
||||
cls,
|
||||
backend_classes: "list[type[AttentionBackend]]",
|
||||
default_block_size: int,
|
||||
vllm_config: "VllmConfig",
|
||||
) -> int:
|
||||
"""Smallest block size every backend accepts.
|
||||
|
||||
``supports_block_size`` may be overridden to accept exact sizes only
|
||||
(CPU_MLA takes 16 and no multiple of it), so candidates are the LCMs of
|
||||
one supported size per backend, each checked against every backend.
|
||||
|
||||
Raises:
|
||||
ValueError: If the backends share no supported block size.
|
||||
|
||||
"""
|
||||
from vllm.config.vllm import set_current_vllm_config
|
||||
from vllm.v1.attention.backend import MultipleOf
|
||||
|
||||
# Backends may read the current config to decide a preference.
|
||||
with set_current_vllm_config(vllm_config):
|
||||
if len(backend_classes) == 1:
|
||||
return backend_classes[0].get_preferred_block_size(default_block_size)
|
||||
if all(b.supports_block_size(default_block_size) for b in backend_classes):
|
||||
return default_block_size
|
||||
# A backend declaring no sizes accepts any, so it contributes 1.
|
||||
per_backend_sizes = [
|
||||
[
|
||||
s.base if isinstance(s, MultipleOf) else s
|
||||
for s in b.get_supported_kernel_block_sizes()
|
||||
]
|
||||
or [1]
|
||||
for b in backend_classes
|
||||
]
|
||||
candidates = sorted(
|
||||
{math.lcm(*sizes) for sizes in itertools.product(*per_backend_sizes)}
|
||||
)
|
||||
for candidate in candidates:
|
||||
if all(b.supports_block_size(candidate) for b in backend_classes):
|
||||
return candidate
|
||||
raise ValueError(
|
||||
"The attention backends share no supported KV cache block size ("
|
||||
+ "; ".join(
|
||||
f"{b.get_name()}: {b.get_supported_kernel_block_sizes()}"
|
||||
for b in backend_classes
|
||||
)
|
||||
+ ")."
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def update_block_size_for_backend(cls, vllm_config: "VllmConfig") -> None:
|
||||
@@ -598,7 +651,6 @@ class Platform:
|
||||
For hybrid models, also aligns block_size with mamba page sizes.
|
||||
"""
|
||||
from vllm.config.cache import CacheConfig
|
||||
from vllm.config.vllm import set_current_vllm_config
|
||||
|
||||
cache_config = vllm_config.cache_config
|
||||
model_config = vllm_config.model_config
|
||||
@@ -607,34 +659,36 @@ class Platform:
|
||||
if not model_config:
|
||||
return
|
||||
|
||||
backend_cls = cls._find_non_ssm_backend(vllm_config)
|
||||
if backend_cls is None:
|
||||
backend_classes = cls._find_non_ssm_backends(vllm_config)
|
||||
if not backend_classes:
|
||||
return
|
||||
|
||||
# Phase 1: Pick block size from backend (skip if user set --block-size)
|
||||
# Phase 1: Pick a block size every attention backend supports (skip if
|
||||
# user set --block-size). Models can mix backends with disjoint
|
||||
# preferences, and a size from the first alone later fails
|
||||
# select_common_block_size().
|
||||
if not cache_config.user_specified_block_size:
|
||||
with set_current_vllm_config(vllm_config):
|
||||
preferred = backend_cls.get_preferred_block_size(
|
||||
CacheConfig.DEFAULT_BLOCK_SIZE
|
||||
)
|
||||
preferred = cls._preferred_block_size_for_backends(
|
||||
backend_classes, CacheConfig.DEFAULT_BLOCK_SIZE, vllm_config
|
||||
)
|
||||
if preferred != CacheConfig.DEFAULT_BLOCK_SIZE:
|
||||
logger.info(
|
||||
"Setting kv cache block size to %d for %s backend.",
|
||||
"Setting kv cache block size to %d for %s backend(s).",
|
||||
preferred,
|
||||
backend_cls.get_name(),
|
||||
"/".join(b.get_name() for b in backend_classes),
|
||||
)
|
||||
cache_config.block_size = preferred
|
||||
|
||||
# Phase 2: Align block/mamba sizes for hybrid models
|
||||
# (may override user settings).
|
||||
if model_config.is_hybrid:
|
||||
cls._align_hybrid_block_size(vllm_config, backend_cls)
|
||||
cls._align_hybrid_block_size(vllm_config, backend_classes[0])
|
||||
|
||||
# Phase 3: Align block/page sizes when multiple KV dtypes share the
|
||||
# block pool (e.g. nvfp4 primary + unquantized skip layers).
|
||||
# May override the user's --block-size.
|
||||
if cache_config.kv_cache_dtype_skip_layers:
|
||||
cls._align_heterogeneous_kv_block_size(vllm_config, backend_cls)
|
||||
cls._align_heterogeneous_kv_block_size(vllm_config, backend_classes[0])
|
||||
|
||||
@classmethod
|
||||
def _align_heterogeneous_kv_block_size(
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
import importlib.metadata
|
||||
import os
|
||||
import platform
|
||||
from datetime import timedelta
|
||||
@@ -361,6 +362,25 @@ def get_cdna_version() -> int:
|
||||
return 0
|
||||
|
||||
|
||||
@cache
|
||||
def get_rocm_version() -> tuple[int, ...] | None:
|
||||
"""Return the installed ROCm release as (major, minor, patch), or None."""
|
||||
# ROCm 10+ ships as the `rocm` pip SDK; older releases install to /opt/rocm.
|
||||
try:
|
||||
version = importlib.metadata.version("rocm")
|
||||
except importlib.metadata.PackageNotFoundError:
|
||||
rocm_path = os.environ.get("ROCM_PATH", "/opt/rocm")
|
||||
try:
|
||||
with open(os.path.join(rocm_path, ".info", "version")) as f:
|
||||
version = f.read()
|
||||
except OSError:
|
||||
return None
|
||||
match = re.match(r"\s*(\d+)\.(\d+)(?:\.(\d+))?", version)
|
||||
if match is None:
|
||||
return None
|
||||
return tuple(int(part) for part in match.groups() if part is not None)
|
||||
|
||||
|
||||
# Enable HIP online tuning early, before hipBLASLt initializes.
|
||||
# Turn on hipBLASLt online tuning if use AITER hipBLASLt GEMM.
|
||||
if (
|
||||
|
||||
@@ -193,6 +193,10 @@ class BaseRenderer(ABC, Generic[_T]):
|
||||
|
||||
return tokenizer
|
||||
|
||||
def render_completion_suffix(self, prompt: str, suffix: str) -> str | None:
|
||||
"""Render OpenAI completion suffix input when the renderer supports FIM."""
|
||||
return None
|
||||
|
||||
def _decode(self, *args, **kwargs):
|
||||
return self.get_tokenizer().decode(*args, **kwargs)
|
||||
|
||||
|
||||
@@ -16,6 +16,10 @@ from .inputs import DictPrompt
|
||||
from .inputs.preprocess import parse_dec_only_prompt
|
||||
from .params import ChatParams
|
||||
|
||||
_FIM_BEGIN = "<|fim▁begin|>"
|
||||
_FIM_HOLE = "<|fim▁hole|>"
|
||||
_FIM_END = "<|fim▁end|>"
|
||||
|
||||
|
||||
class DeepseekV4Renderer(BaseRenderer[DeepseekV4Tokenizer]):
|
||||
def __init__(
|
||||
@@ -32,6 +36,9 @@ class DeepseekV4Renderer(BaseRenderer[DeepseekV4Tokenizer]):
|
||||
def _apply_chat_template(self, *args, **kwargs):
|
||||
return self.get_tokenizer().apply_chat_template(*args, **kwargs)
|
||||
|
||||
def render_completion_suffix(self, prompt: str, suffix: str) -> str | None:
|
||||
return f"{_FIM_BEGIN}{prompt}{_FIM_HOLE}{suffix}{_FIM_END}"
|
||||
|
||||
def render_messages(
|
||||
self,
|
||||
messages: list[ChatCompletionMessageParam],
|
||||
|
||||
@@ -4,7 +4,7 @@ import time
|
||||
from collections.abc import Sequence
|
||||
from dataclasses import dataclass
|
||||
from http import HTTPStatus
|
||||
from typing import TYPE_CHECKING, Any, TypeAlias
|
||||
from typing import TYPE_CHECKING, Any, TypeAlias, cast
|
||||
|
||||
from openai.types.responses import ResponseFunctionToolCall, ResponseOutputItem
|
||||
from openai.types.responses.tool import Mcp, Tool
|
||||
@@ -548,9 +548,58 @@ class OnlineRenderer:
|
||||
Called directly by render_completion_request and delegated to by
|
||||
OpenAIServingCompletion.render_completion_request after its engine-aware checks.
|
||||
"""
|
||||
# Return error for unsupported features.
|
||||
prompt_input = request.prompt
|
||||
if request.suffix is not None:
|
||||
return self.create_error_response("suffix is not currently supported")
|
||||
if request.echo:
|
||||
return self.create_error_response(
|
||||
"Echo is unsupported with suffix.",
|
||||
param="suffix",
|
||||
)
|
||||
|
||||
if request.prompt_embeds is not None:
|
||||
return self.create_error_response(
|
||||
"suffix is not supported with prompt_embeds",
|
||||
param="suffix",
|
||||
)
|
||||
|
||||
if request.truncate_prompt_tokens is not None:
|
||||
return self.create_error_response(
|
||||
"suffix is not supported with truncate_prompt_tokens",
|
||||
param="suffix",
|
||||
)
|
||||
|
||||
if isinstance(request.prompt, str):
|
||||
rendered_prompt = self.renderer.render_completion_suffix(
|
||||
request.prompt, request.suffix
|
||||
)
|
||||
if rendered_prompt is None:
|
||||
return self.create_error_response(
|
||||
"suffix is only supported for models with FIM completion "
|
||||
"rendering",
|
||||
param="suffix",
|
||||
)
|
||||
prompt_input = rendered_prompt
|
||||
elif isinstance(request.prompt, list) and all(
|
||||
isinstance(prompt, str) for prompt in request.prompt
|
||||
):
|
||||
rendered_prompts = []
|
||||
for prompt in cast(list[str], request.prompt):
|
||||
rendered_prompt = self.renderer.render_completion_suffix(
|
||||
prompt, request.suffix
|
||||
)
|
||||
if rendered_prompt is None:
|
||||
return self.create_error_response(
|
||||
"suffix is only supported for models with FIM completion "
|
||||
"rendering",
|
||||
param="suffix",
|
||||
)
|
||||
rendered_prompts.append(rendered_prompt)
|
||||
prompt_input = rendered_prompts
|
||||
else:
|
||||
return self.create_error_response(
|
||||
"suffix requires text prompt input for FIM completion rendering",
|
||||
param="suffix",
|
||||
)
|
||||
|
||||
if request.echo and request.prompt_embeds is not None:
|
||||
return self.create_error_response("Echo is unsupported with prompt embeds.")
|
||||
@@ -562,7 +611,7 @@ class OnlineRenderer:
|
||||
|
||||
engine_inputs = await self.preprocess_completion(
|
||||
request,
|
||||
prompt_input=request.prompt,
|
||||
prompt_input=prompt_input,
|
||||
prompt_embeds=request.prompt_embeds,
|
||||
skip_mm_cache=skip_mm_cache,
|
||||
)
|
||||
|
||||
@@ -316,8 +316,13 @@ class ScalarType:
|
||||
|
||||
|
||||
class scalar_types:
|
||||
uint2 = ScalarType.uint(2, None)
|
||||
uint3 = ScalarType.uint(3, None)
|
||||
int4 = ScalarType.int_(4, None)
|
||||
uint4 = ScalarType.uint(4, None)
|
||||
uint5 = ScalarType.uint(5, None)
|
||||
uint6 = ScalarType.uint(6, None)
|
||||
uint7 = ScalarType.uint(7, None)
|
||||
int8 = ScalarType.int_(8, None)
|
||||
uint8 = ScalarType.uint(8, None)
|
||||
float8_e4m3fn = ScalarType.float_(4, 3, True, NanRepr.EXTD_RANGE_MAX_MIN)
|
||||
|
||||
@@ -110,7 +110,7 @@ class OvisProcessor(ProcessorMixin):
|
||||
) -> BatchFeature:
|
||||
"""Main method to prepare for the model one or several sequences(s) and image(s). This method forwards the `text`
|
||||
and `kwargs` arguments to Qwen2TokenizerFast's [`~Qwen2TokenizerFast.__call__`] if `text` is not `None` to encode
|
||||
the text. To prepare the vision inputs, this method forwards the `vision_infos` and `kwrags` arguments to
|
||||
the text. To prepare the vision inputs, this method forwards the `vision_infos` and `kwargs` arguments to
|
||||
Qwen2VLImageProcessor's [`~Qwen2VLImageProcessor.__call__`] if `vision_infos` is not `None`.
|
||||
|
||||
Args:
|
||||
|
||||
@@ -115,7 +115,7 @@ class Ovis2_5Processor(ProcessorMixin):
|
||||
and image(s). This method forwards the `text`and `kwargs` arguments
|
||||
to Qwen2TokenizerFast's [`~Qwen2TokenizerFast.__call__`] if `text`
|
||||
is not `None` to encode the text. To prepare the vision inputs,
|
||||
this method forwards the `vision_infos` and `kwrags` arguments to
|
||||
this method forwards the `vision_infos` and `kwargs` arguments to
|
||||
Qwen2VLImageProcessor's [`~Qwen2VLImageProcessor.__call__`]
|
||||
if `vision_infos` is not `None`.
|
||||
|
||||
|
||||
@@ -926,6 +926,10 @@ class AttentionImpl(AttentionImplBase[T], Generic[T]):
|
||||
"""
|
||||
return False
|
||||
|
||||
def fused_qk_norm_mrope_kvcache_supported(self):
|
||||
"""Whether this implementation supports fused QKNorm+MRoPE+KVCache."""
|
||||
return False
|
||||
|
||||
def fused_rope_kvcache_supported(self):
|
||||
"""Does this attention implementation support RoPE+KVCache fusion.
|
||||
This is used by the RopeKVCacheFusionPass to only fuse the RoPE ops
|
||||
@@ -955,6 +959,26 @@ class AttentionImpl(AttentionImplBase[T], Generic[T]):
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def do_qk_norm_mrope_kvcache_update(
|
||||
self,
|
||||
layer: AttentionLayer,
|
||||
qkv: torch.Tensor,
|
||||
q_out: torch.Tensor,
|
||||
positions: torch.Tensor,
|
||||
q_weight: torch.Tensor,
|
||||
k_weight: torch.Tensor,
|
||||
rms_norm_eps: float,
|
||||
cos_sin_cache: torch.Tensor,
|
||||
is_neox: bool,
|
||||
mrope_section: tuple[int, int, int],
|
||||
is_interleaved: bool,
|
||||
rotary_dim: int,
|
||||
kv_cache: torch.Tensor,
|
||||
layer_slot_mapping: torch.Tensor,
|
||||
):
|
||||
"""Apply QK-norm and MRoPE, then write K/V to the cache."""
|
||||
raise NotImplementedError
|
||||
|
||||
def do_rope_and_kv_cache_update(
|
||||
self,
|
||||
layer: AttentionLayer,
|
||||
|
||||
@@ -406,6 +406,15 @@ class CPUAttentionBackendImpl(AttentionImpl):
|
||||
kv_cache_dtype=self.kv_cache_dtype,
|
||||
)
|
||||
|
||||
# The CPU kernel executes attention sinks natively in bf16. If the
|
||||
# sinks tensor is anything other than bf16, cast it to fp32 so it is
|
||||
# executed in full float precision (done lazily here, after weights
|
||||
# are loaded, rather than at __init__ time).
|
||||
if self.sinks is not None and self.sinks.dtype not in [
|
||||
torch.bfloat16,
|
||||
torch.float32,
|
||||
]:
|
||||
self.sinks = self.sinks.to(torch.float32)
|
||||
ops.cpu_attention_with_kv_cache(
|
||||
query=query[:num_actual_tokens],
|
||||
key_cache=key_cache,
|
||||
|
||||
@@ -772,9 +772,18 @@ class AiterMLAMetadataBuilder(MLACommonMetadataBuilder[AiterMLAMetadata]):
|
||||
device=device,
|
||||
)
|
||||
|
||||
from vllm.platforms import current_platform
|
||||
from vllm.v1.worker.workspace import current_workspace_manager
|
||||
|
||||
max_num_partial_tiles = reduce_partial_map_size
|
||||
# AITER metadata sizing assumes all requests can carry max_prefill_qlen tokens,
|
||||
# which is a loose worst case in the number of QO tiles. Rather, the sum of
|
||||
# qlens is bounded by max_num_batched_tokens. Manually compute the number of
|
||||
# qo tiles to avoid OOM at startup.
|
||||
# TODO: AITER should give us this budget constrained value
|
||||
qo_tile_cnt = (
|
||||
cdiv(max_num_batched_tokens, _FP8_PREFILL_TILE_Q) + max_num_reqs - 1
|
||||
)
|
||||
max_num_partial_tiles = qo_tile_cnt + current_platform.num_compute_units()
|
||||
reservations: list[tuple[tuple[int, ...], torch.dtype]] = [
|
||||
(
|
||||
(max_num_partial_tiles * _FP8_PREFILL_TILE_Q, num_head_k, v_head_dim),
|
||||
|
||||
@@ -7,7 +7,7 @@ from typing import ClassVar
|
||||
import torch
|
||||
|
||||
from vllm import _custom_ops as ops
|
||||
from vllm._aiter_ops import rocm_aiter_ops
|
||||
from vllm._aiter_ops import IS_AITER_FOUND, rocm_aiter_ops
|
||||
from vllm.config import get_current_vllm_config_or_none
|
||||
from vllm.config.cache import CacheDType
|
||||
from vllm.logger import init_logger
|
||||
@@ -359,6 +359,9 @@ class RocmAiterUnifiedAttentionImpl(RocmAttentionImpl):
|
||||
def fused_qk_norm_rope_kvcache_supported(self):
|
||||
return rocm_aiter_ops.is_enabled()
|
||||
|
||||
def fused_qk_norm_mrope_kvcache_supported(self) -> bool:
|
||||
return IS_AITER_FOUND
|
||||
|
||||
def do_qk_norm_rope_kvcache_update(
|
||||
self,
|
||||
layer: AttentionLayer,
|
||||
@@ -397,6 +400,47 @@ class RocmAiterUnifiedAttentionImpl(RocmAttentionImpl):
|
||||
use_shuffle_layout=False,
|
||||
)
|
||||
|
||||
def do_qk_norm_mrope_kvcache_update(
|
||||
self,
|
||||
layer: AttentionLayer,
|
||||
qkv: torch.Tensor,
|
||||
q_out: torch.Tensor,
|
||||
positions: torch.Tensor,
|
||||
q_weight: torch.Tensor,
|
||||
k_weight: torch.Tensor,
|
||||
rms_norm_eps: float,
|
||||
cos_sin_cache: torch.Tensor,
|
||||
is_neox: bool,
|
||||
mrope_section: tuple[int, int, int],
|
||||
is_interleaved: bool,
|
||||
rotary_dim: int,
|
||||
kv_cache: torch.Tensor,
|
||||
layer_slot_mapping: torch.Tensor,
|
||||
):
|
||||
key_cache, value_cache = self._split_kv_cache(kv_cache)
|
||||
rocm_aiter_ops.do_qk_norm_mrope_kvcache_update(
|
||||
qkv=qkv,
|
||||
q_weight=q_weight,
|
||||
k_weight=k_weight,
|
||||
cos_sin_cache=cos_sin_cache,
|
||||
positions=positions,
|
||||
num_heads_q=self.num_heads,
|
||||
num_heads_k=self.num_kv_heads,
|
||||
head_dim=self.head_size,
|
||||
is_neox=is_neox,
|
||||
mrope_section=list(mrope_section),
|
||||
is_interleaved=is_interleaved,
|
||||
rms_norm_eps=rms_norm_eps,
|
||||
q_out=q_out,
|
||||
key_cache=key_cache,
|
||||
value_cache=value_cache,
|
||||
slot_mapping=layer_slot_mapping,
|
||||
k_scale=layer._k_scale_cpu,
|
||||
v_scale=layer._v_scale_cpu,
|
||||
kv_cache_dtype=self.kv_cache_dtype,
|
||||
rotary_dim=rotary_dim,
|
||||
)
|
||||
|
||||
def do_rope_and_kv_cache_update(
|
||||
self,
|
||||
layer: AttentionLayer,
|
||||
|
||||
@@ -437,7 +437,7 @@ class KVCacheManager:
|
||||
----------------------------------------------------------------------
|
||||
```
|
||||
|
||||
Abbrivations:
|
||||
Abbreviations:
|
||||
|
||||
```
|
||||
comp = request.num_computed_tokens
|
||||
|
||||
@@ -555,6 +555,11 @@ def _post_update_kernel(
|
||||
all_token_ids_ptr,
|
||||
all_token_ids_stride,
|
||||
total_len_ptr,
|
||||
broadcast_drafts_ptr,
|
||||
broadcast_drafts_stride,
|
||||
draft_tokens_ptr,
|
||||
draft_tokens_stride,
|
||||
num_spec,
|
||||
):
|
||||
req_id = tl.program_id(0)
|
||||
req_state_idx = tl.load(idx_mapping_ptr + req_id)
|
||||
@@ -562,6 +567,17 @@ def _post_update_kernel(
|
||||
# Filter rows with negative index entries.
|
||||
return
|
||||
|
||||
if broadcast_drafts_ptr is not None:
|
||||
# PP path: adopt the draft tokens proposed by the last rank's
|
||||
# speculator so the next verification step embeds the real drafts.
|
||||
for i in range(num_spec):
|
||||
token_id = tl.load(
|
||||
broadcast_drafts_ptr + req_id * broadcast_drafts_stride + i
|
||||
)
|
||||
tl.store(
|
||||
draft_tokens_ptr + req_state_idx * draft_tokens_stride + i, token_id
|
||||
)
|
||||
|
||||
total_len = tl.load(total_len_ptr + req_state_idx)
|
||||
num_sampled = tl.load(num_sampled_ptr + req_id)
|
||||
if num_sampled > 0:
|
||||
@@ -622,6 +638,11 @@ def post_update(
|
||||
all_token_ids: torch.Tensor,
|
||||
# [max_num_reqs]
|
||||
total_len: torch.Tensor,
|
||||
# [num_reqs, num_spec]; drafts broadcast from the last PP rank. Only
|
||||
# passed on non-last PP ranks, which never run the speculator.
|
||||
broadcast_drafts: torch.Tensor | None = None,
|
||||
# [max_num_reqs, num_spec]
|
||||
draft_tokens_out: torch.Tensor | None = None,
|
||||
) -> None:
|
||||
num_reqs = idx_mapping.shape[0]
|
||||
_post_update_kernel[(num_reqs,)](
|
||||
@@ -638,6 +659,11 @@ def post_update(
|
||||
all_token_ids,
|
||||
all_token_ids.stride(0),
|
||||
total_len,
|
||||
broadcast_drafts,
|
||||
broadcast_drafts.stride(0) if broadcast_drafts is not None else 0,
|
||||
draft_tokens_out,
|
||||
draft_tokens_out.stride(0) if draft_tokens_out is not None else 0,
|
||||
broadcast_drafts.shape[1] if broadcast_drafts is not None else 0,
|
||||
num_warps=1,
|
||||
)
|
||||
|
||||
|
||||
@@ -860,6 +860,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
# dummy run the eagle speculator's propose to ensure DP/EP sync.
|
||||
if self.speculator is not None:
|
||||
assert self.sampler is not None
|
||||
assert hidden_states is not None
|
||||
self.step_timing.drafter_start()
|
||||
mm_inputs: tuple[list[torch.Tensor], torch.Tensor] | None = None
|
||||
if self.speculator.supports_mm_inputs:
|
||||
@@ -875,7 +876,8 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
spec_hidden_states = hidden_states
|
||||
if hasattr(self.model, "get_mtp_target_hidden_states"):
|
||||
pre_hc_hidden_states = self.model.get_mtp_target_hidden_states()
|
||||
spec_hidden_states = pre_hc_hidden_states[: hidden_states.shape[0]] # type: ignore[union-attr]
|
||||
if pre_hc_hidden_states is not None:
|
||||
spec_hidden_states = pre_hc_hidden_states[: hidden_states.shape[0]]
|
||||
if isinstance(self.sampler, GPUWatermarkSampler):
|
||||
self.speculator.prepare_watermarking(
|
||||
self.sampler._get_contexts(input_batch.idx_mapping),
|
||||
@@ -1107,6 +1109,44 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
if outputs is not None:
|
||||
self.postprocess_sampled(**outputs)
|
||||
|
||||
def warmup_pp_decode_update(self) -> None:
|
||||
"""JIT-compile the kernel behind ``update_pp_decode_requests``.
|
||||
|
||||
That path only runs on real steps, so the warmup steps never reach it
|
||||
on non-last PP ranks. Its first triton compile must not happen
|
||||
mid-serving: the in-flight sampled-token broadcast keeps a NCCL kernel
|
||||
spinning on this device, which blocks the CUDA module load and
|
||||
deadlocks the pipeline. An all -1 idx_mapping makes this a no-op.
|
||||
The freshly allocated int32 tensors are 16-byte aligned, matching the
|
||||
padded views `PPHandler` produces at serving time (triton specializes
|
||||
on pointer alignment).
|
||||
"""
|
||||
assert self.pp_handler is not None
|
||||
num_spec = self.pp_handler.max_sample_len - 1
|
||||
broadcast_drafts = (
|
||||
torch.zeros((1, num_spec), dtype=torch.int64, device=self.device)
|
||||
if num_spec > 0
|
||||
else None
|
||||
)
|
||||
post_update(
|
||||
torch.full((1,), -1, dtype=torch.int64, device=self.device),
|
||||
self.req_states.num_computed_tokens.gpu,
|
||||
self.req_states.last_sampled_tokens,
|
||||
None,
|
||||
torch.zeros(
|
||||
(1, self.pp_handler.max_sample_len),
|
||||
dtype=torch.int64,
|
||||
device=self.device,
|
||||
),
|
||||
torch.zeros(1, dtype=torch.int32, device=self.device),
|
||||
torch.zeros(1, dtype=torch.int32, device=self.device),
|
||||
None,
|
||||
self.req_states.all_token_ids.gpu,
|
||||
self.req_states.total_len.gpu,
|
||||
broadcast_drafts,
|
||||
self.req_states.draft_tokens if broadcast_drafts is not None else None,
|
||||
)
|
||||
|
||||
def add_requests(self, scheduler_output: SchedulerOutput) -> None:
|
||||
for new_req_data in scheduler_output.scheduled_new_reqs:
|
||||
assert new_req_data.prefill_token_ids is not None
|
||||
@@ -1588,6 +1628,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
num_sampled: torch.Tensor,
|
||||
num_rejected: torch.Tensor,
|
||||
query_start_loc: torch.Tensor | None = None,
|
||||
broadcast_drafts: torch.Tensor | None = None,
|
||||
) -> None:
|
||||
# Update the number of computed tokens.
|
||||
output_bin_counts = None
|
||||
@@ -1605,6 +1646,8 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
query_start_loc,
|
||||
self.req_states.all_token_ids.gpu,
|
||||
self.req_states.total_len.gpu,
|
||||
broadcast_drafts,
|
||||
self.req_states.draft_tokens if broadcast_drafts is not None else None,
|
||||
)
|
||||
|
||||
self.model_state.postprocess_state(
|
||||
@@ -2125,7 +2168,10 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
spec_hidden_states = draft_hidden_states
|
||||
if hasattr(self.model, "get_mtp_target_hidden_states"):
|
||||
pre_hc_hidden_states = self.model.get_mtp_target_hidden_states()
|
||||
spec_hidden_states = pre_hc_hidden_states[: draft_hidden_states.size(0)]
|
||||
if pre_hc_hidden_states is not None:
|
||||
spec_hidden_states = pre_hc_hidden_states[
|
||||
: draft_hidden_states.size(0)
|
||||
]
|
||||
if isinstance(self.sampler, GPUWatermarkSampler):
|
||||
self.speculator.prepare_watermarking(
|
||||
self.sampler._get_contexts(input_batch.idx_mapping),
|
||||
@@ -2147,8 +2193,16 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
dp_sync=dp_sync,
|
||||
mm_inputs=mm_inputs,
|
||||
)
|
||||
self.req_states.draft_tokens[input_batch.idx_mapping] = draft_tokens
|
||||
if self.adaptive_verification is not None:
|
||||
if draft_tokens is not None:
|
||||
self.req_states.draft_tokens[input_batch.idx_mapping] = draft_tokens
|
||||
if self.pp_handler is not None:
|
||||
# Earlier stages never run the speculator; ship the
|
||||
# drafts so their next verification step embeds the real
|
||||
# draft tokens instead of stale buffer contents.
|
||||
self.pp_handler.broadcast_drafts(
|
||||
self.req_states.draft_tokens, input_batch
|
||||
)
|
||||
if draft_tokens is not None and self.adaptive_verification is not None:
|
||||
self.adaptive_verification.record_confidences(
|
||||
self.speculator.draft_token_confidence_probs, input_batch
|
||||
)
|
||||
@@ -2160,7 +2214,11 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
input_batch,
|
||||
self.req_states.draft_tokens[input_batch.idx_mapping],
|
||||
)
|
||||
if self.pp_handler is not None:
|
||||
if self.pp_handler is not None and self.speculator is None:
|
||||
# When a speculator ran, the propose() path above already
|
||||
# broadcast the fresh drafts. Broadcasting here as well would
|
||||
# double-post on the pp_broadcast group and misalign the
|
||||
# recv FIFO on earlier stages, hanging the pipeline.
|
||||
self.pp_handler.broadcast_drafts(
|
||||
self.req_states.draft_tokens, input_batch
|
||||
)
|
||||
|
||||
@@ -46,6 +46,18 @@ def compute_need_sampled_mask(input_batch: InputBatch) -> np.ndarray | None:
|
||||
return produces_sample if produces_sample.any() else None
|
||||
|
||||
|
||||
def _alloc_combined(num_reqs: int, device: torch.device) -> torch.Tensor:
|
||||
"""Allocate the (2, N) int32 buffer broadcast alongside sampled tokens.
|
||||
|
||||
The inner dim is padded to a multiple of 4 so that both `unbind` views
|
||||
stay 16-byte aligned for any `num_reqs`: triton specializes on pointer
|
||||
alignment, and a misaligned `num_rejected` would JIT-compile a second
|
||||
`_post_update_kernel` variant at serving time. The padding is broadcast
|
||||
but never read. Sender and receiver must both use this allocation.
|
||||
"""
|
||||
return torch.empty(2, -(-num_reqs // 4) * 4, dtype=torch.int32, device=device)
|
||||
|
||||
|
||||
class PPHandler:
|
||||
"""Runs the PP sampled-token broadcast/recv on a side stream so the
|
||||
default stream isn't gated by the matching peer call. Step T's recv is
|
||||
@@ -86,6 +98,14 @@ class PPHandler:
|
||||
)
|
||||
self.aux_hidden_state_relay_keys: tuple[str, ...] = ()
|
||||
|
||||
# Warmup steps run the pipeline with synthetic batches whose outputs are
|
||||
# discarded; the sampled-token broadcast is disabled there so its
|
||||
# side-stream NCCL ops cannot overlap the next step's activation p2p.
|
||||
self.disabled = False
|
||||
|
||||
def set_disabled(self, disabled: bool) -> None:
|
||||
self.disabled = disabled
|
||||
|
||||
def on_req_idx_freed(self, req_idx: int) -> None:
|
||||
self.req_idx_gen_np[req_idx] += 1
|
||||
|
||||
@@ -157,29 +177,15 @@ class PPHandler:
|
||||
num_sampled=slot.num_sampled,
|
||||
num_rejected=slot.num_rejected,
|
||||
idx_mapping=idx_mapping,
|
||||
broadcast_drafts=slot.draft_tokens,
|
||||
)
|
||||
|
||||
def broadcast_drafts(
|
||||
self, draft_tokens: torch.Tensor, input_batch: InputBatch
|
||||
) -> None:
|
||||
"""Broadcast draft proposals so non-last ranks can embed real token ids."""
|
||||
assert self.is_last_rank
|
||||
if compute_need_sampled_mask(input_batch) is None:
|
||||
return
|
||||
with torch.cuda.stream(self.broadcast_stream):
|
||||
self.broadcast_stream.wait_stream(self.main_stream)
|
||||
send = draft_tokens[input_batch.idx_mapping].contiguous()
|
||||
# Must record the idx_mapping tensor since it was allocated
|
||||
# on the main stream.
|
||||
input_batch.idx_mapping.record_stream(self.broadcast_stream)
|
||||
torch.distributed.broadcast(
|
||||
send, src=self.last_rank, group=self.broadcast_group
|
||||
)
|
||||
|
||||
def receive(self, input_batch: InputBatch) -> bool:
|
||||
"""Returns True iff sampled tokens need to be gathered from *all*
|
||||
requests in the batch."""
|
||||
assert not self.is_last_rank
|
||||
if self.disabled:
|
||||
return False
|
||||
need_sampled_mask = compute_need_sampled_mask(input_batch)
|
||||
if need_sampled_mask is None:
|
||||
# Leave this step's reserved slot as None.
|
||||
@@ -195,7 +201,7 @@ class PPHandler:
|
||||
sampled_tokens = torch.empty(
|
||||
num_reqs, self.max_sample_len, dtype=torch.int64, device=self.device
|
||||
)
|
||||
combined = torch.empty(2, num_reqs, dtype=torch.int32, device=self.device)
|
||||
combined = _alloc_combined(num_reqs, self.device)
|
||||
torch.distributed.broadcast(
|
||||
sampled_tokens, src=self.last_rank, group=self.broadcast_group
|
||||
)
|
||||
@@ -213,6 +219,7 @@ class PPHandler:
|
||||
torch.distributed.broadcast(
|
||||
draft_tokens, src=self.last_rank, group=self.broadcast_group
|
||||
)
|
||||
|
||||
event = self.broadcast_stream.record_event()
|
||||
num_sampled, num_rejected = combined.unbind(dim=0)
|
||||
# Must record_stream since these were allocated on broadcast stream but
|
||||
@@ -242,11 +249,16 @@ class PPHandler:
|
||||
input_batch: InputBatch,
|
||||
) -> None:
|
||||
assert self.is_last_rank
|
||||
if compute_need_sampled_mask(input_batch) is None:
|
||||
if self.disabled:
|
||||
return
|
||||
mask = compute_need_sampled_mask(input_batch)
|
||||
if mask is None:
|
||||
# No request needs sampled outputs for a subsequent decode step.
|
||||
return
|
||||
|
||||
assert sampled_token_ids.dtype == torch.int64
|
||||
assert num_sampled.dtype == torch.int32
|
||||
assert num_rejected.dtype == torch.int32
|
||||
|
||||
if current_platform.is_xpu():
|
||||
self.main_stream.synchronize()
|
||||
@@ -262,9 +274,42 @@ class PPHandler:
|
||||
src=self.last_rank,
|
||||
group=self.broadcast_group,
|
||||
)
|
||||
combined = torch.stack((num_sampled, num_rejected), dim=0)
|
||||
combined = _alloc_combined(num_sampled.shape[0], self.device)
|
||||
combined[0, : num_sampled.shape[0]] = num_sampled
|
||||
combined[1, : num_sampled.shape[0]] = num_rejected
|
||||
torch.distributed.broadcast(
|
||||
combined, src=self.last_rank, group=self.broadcast_group
|
||||
)
|
||||
for tensor in (sampled_token_ids, num_sampled, num_rejected):
|
||||
tensor.record_stream(self.broadcast_stream)
|
||||
|
||||
def broadcast_drafts(
|
||||
self, draft_token_table: torch.Tensor, input_batch: InputBatch
|
||||
) -> None:
|
||||
"""Broadcast the speculator's freshly proposed draft tokens.
|
||||
|
||||
Runs after propose() on the last rank; the send is stream-ordered
|
||||
after broadcast()'s sends, matching receive()'s enqueue order. The
|
||||
payload is gathered from the runner's draft table (just updated from
|
||||
propose()'s output) into a fresh compact tensor: the speculator
|
||||
overwrites its own persistent buffer on the next step, possibly
|
||||
before this async send completes.
|
||||
"""
|
||||
assert self.is_last_rank
|
||||
if self.disabled or self.max_sample_len == 1:
|
||||
return
|
||||
if compute_need_sampled_mask(input_batch) is None:
|
||||
return
|
||||
assert draft_token_table.dtype == torch.int64
|
||||
assert draft_token_table.shape[1] == self.max_sample_len - 1
|
||||
|
||||
# Gather on the main stream so the payload is ordered after this
|
||||
# step's table update and before any later one.
|
||||
drafts = draft_token_table[input_batch.idx_mapping]
|
||||
assert drafts.shape[0] == input_batch.num_reqs
|
||||
with torch.cuda.stream(self.broadcast_stream):
|
||||
self.broadcast_stream.wait_stream(self.main_stream)
|
||||
torch.distributed.broadcast(
|
||||
drafts, src=self.last_rank, group=self.broadcast_group
|
||||
)
|
||||
drafts.record_stream(self.broadcast_stream)
|
||||
|
||||
@@ -256,6 +256,7 @@ class DFlashSpeculator(DraftModelSpeculator):
|
||||
num_tokens_across_dp=num_tokens_across_dp,
|
||||
slot_mapping=slot_mappings,
|
||||
batch_descriptor=batch_descriptor,
|
||||
is_padding=self.input_buffers.is_padding[:num_tokens],
|
||||
):
|
||||
last_hidden_states = self.model(
|
||||
input_ids=self.input_buffers.input_ids[:num_tokens],
|
||||
@@ -487,6 +488,7 @@ def _prepare_dflash_inputs_kernel(
|
||||
# Outputs
|
||||
out_input_ids_ptr,
|
||||
out_query_positions_ptr,
|
||||
out_is_padding_ptr,
|
||||
out_query_start_loc_ptr,
|
||||
out_seq_lens_ptr,
|
||||
out_query_slot_mapping_ptr,
|
||||
@@ -617,6 +619,7 @@ def _prepare_dflash_inputs_kernel(
|
||||
)
|
||||
|
||||
tl.store(out_input_ids_ptr + query_idx, input_id, mask=is_query)
|
||||
tl.store(out_is_padding_ptr + query_idx, False, mask=is_query)
|
||||
clamped_query_pos = tl.minimum(query_pos, max_model_len - 1)
|
||||
tl.store(out_query_positions_ptr + query_idx, clamped_query_pos, mask=is_query)
|
||||
tl.store(out_query_slot_mapping_ptr + query_idx, q_slot, mask=is_query)
|
||||
@@ -679,6 +682,9 @@ def _prepare_dflash_inputs_kernel(
|
||||
for i in range(q_pad_start, max_num_tokens, BLOCK_SIZE):
|
||||
block = i + tl.arange(0, BLOCK_SIZE)
|
||||
mask = block < max_num_tokens
|
||||
tl.store(out_input_ids_ptr + block, 0, mask=mask)
|
||||
tl.store(out_query_positions_ptr + block, 0, mask=mask)
|
||||
tl.store(out_is_padding_ptr + block, True, mask=mask)
|
||||
tl.store(out_query_slot_mapping_ptr + block, PAD_SLOT_ID, mask=mask)
|
||||
|
||||
|
||||
@@ -730,6 +736,7 @@ def prepare_dflash_inputs(
|
||||
_prepare_dflash_inputs_kernel[(num_reqs, num_blocks)](
|
||||
input_buffers.input_ids,
|
||||
input_buffers.positions,
|
||||
input_buffers.is_padding,
|
||||
input_buffers.query_start_loc,
|
||||
input_buffers.seq_lens,
|
||||
query_slot_mapping,
|
||||
|
||||
@@ -445,6 +445,11 @@ def _warmup_kernels(
|
||||
for step_indices, step_spec_flags in decode_steps:
|
||||
_run_decode_step(step_indices, step_spec_flags)
|
||||
|
||||
# The deferred PP post-update path only runs on real steps, so the steps
|
||||
# above never JIT-compile its kernel on non-last ranks.
|
||||
if not model_runner.is_last_pp_rank and model_runner.pp_handler is not None:
|
||||
model_runner.warmup_pp_decode_update()
|
||||
|
||||
# Clean up - process finish_req_ids.
|
||||
cleanup_output = SchedulerOutput.make_empty()
|
||||
cleanup_output.finished_req_ids = set(req_ids)
|
||||
|
||||
@@ -805,6 +805,15 @@ class Worker(WorkerBase):
|
||||
|
||||
@instrument(span_name="Warmup (GPU)")
|
||||
def compile_or_warm_up_model(self) -> CompilationTimes:
|
||||
# All warmup phases below run synthetic steps whose sampled outputs are
|
||||
# discarded. The PP sampled-token broadcast would carry no payload, and
|
||||
# its side-stream NCCL ops can overlap the next step's activation p2p
|
||||
# and deadlock the pipeline, so keep it disabled for the whole warmup
|
||||
# window and restore it before serving.
|
||||
pp_handler = getattr(self.model_runner, "pp_handler", None)
|
||||
if pp_handler is not None:
|
||||
pp_handler.set_disabled(True)
|
||||
|
||||
warmup_sizes: list[int] = []
|
||||
|
||||
if (
|
||||
@@ -983,6 +992,9 @@ class Worker(WorkerBase):
|
||||
# gate so subsequent `execute_model` / `sample_tokens` calls enforce it.
|
||||
enable_gpu_sync_check()
|
||||
|
||||
if pp_handler is not None:
|
||||
pp_handler.set_disabled(False)
|
||||
|
||||
return CompilationTimes(
|
||||
language_model=self.compilation_config.compilation_time,
|
||||
encoder=self.compilation_config.encoder_compilation_time,
|
||||
|
||||
Reference in New Issue
Block a user