[Feature] KV cache per-token-head INT8/FP8 quantization (#38378)
Signed-off-by: JartX <sagformas@epdcenter.es> Signed-off-by: Wentao Ye <44945378+yewentao256@users.noreply.github.com> Co-authored-by: yangyang4991 <yangyang4991@gmail.com> Co-authored-by: Wentao Ye <44945378+yewentao256@users.noreply.github.com> Co-authored-by: Isotr0py <2037008807@qq.com>
This commit is contained in:
co-authored by
yangyang4991
Wentao Ye
Isotr0py
parent
4eefbf9609
commit
2ce3d0ce36
+17
-4
@@ -8,7 +8,10 @@ from pydantic import Field, SkipValidation, field_validator, model_validator
|
||||
|
||||
from vllm.config.utils import config
|
||||
from vllm.logger import init_logger
|
||||
from vllm.utils.torch_utils import is_quantized_kv_cache
|
||||
from vllm.utils.torch_utils import (
|
||||
is_quantized_kv_cache,
|
||||
kv_cache_uses_per_token_head_scales,
|
||||
)
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
@@ -21,6 +24,8 @@ CacheDType = Literal[
|
||||
"fp8_e5m2",
|
||||
"fp8_inc",
|
||||
"fp8_ds_mla",
|
||||
"int8_per_token_head",
|
||||
"fp8_per_token_head",
|
||||
]
|
||||
MambaDType = Literal["auto", "float32", "float16"]
|
||||
MambaCacheMode = Literal["all", "align", "none"]
|
||||
@@ -237,12 +242,20 @@ class CacheConfig:
|
||||
@field_validator("cache_dtype", mode="after")
|
||||
@classmethod
|
||||
def _validate_cache_dtype(cls, cache_dtype: CacheDType) -> CacheDType:
|
||||
if is_quantized_kv_cache(cache_dtype):
|
||||
if kv_cache_uses_per_token_head_scales(cache_dtype):
|
||||
logger.info(
|
||||
"Using fp8 data type to store kv cache. It reduces the GPU "
|
||||
"Using %s data type to store kv cache. It reduces the GPU "
|
||||
"memory footprint and boosts the performance. "
|
||||
"Dynamic per-token-head scales will be computed at runtime.",
|
||||
str(cache_dtype),
|
||||
)
|
||||
elif is_quantized_kv_cache(cache_dtype):
|
||||
logger.info(
|
||||
"Using %s data type to store kv cache. It reduces the GPU "
|
||||
"memory footprint and boosts the performance. "
|
||||
"Meanwhile, it may cause accuracy drop without a proper "
|
||||
"scaling factor."
|
||||
"scaling factor",
|
||||
str(cache_dtype),
|
||||
)
|
||||
return cache_dtype
|
||||
|
||||
|
||||
@@ -38,6 +38,7 @@ from vllm.v1.kv_cache_interface import (
|
||||
FullAttentionSpec,
|
||||
KVCacheSpec,
|
||||
SlidingWindowSpec,
|
||||
get_kv_quant_mode,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -381,8 +382,10 @@ class Attention(nn.Module, AttentionLayerBase):
|
||||
|
||||
# for attn backends supporting query quantization
|
||||
self.query_quant = None
|
||||
if self.impl.supports_quant_query_input and self.kv_cache_dtype.startswith(
|
||||
"fp8"
|
||||
if (
|
||||
self.impl.supports_quant_query_input
|
||||
and self.kv_cache_dtype.startswith("fp8")
|
||||
and not self.kv_cache_dtype.endswith("per_token_head")
|
||||
):
|
||||
is_per_head = (
|
||||
hasattr(self, "q_scale") and self.q_scale.numel() == self.num_kv_heads
|
||||
@@ -539,6 +542,7 @@ class Attention(nn.Module, AttentionLayerBase):
|
||||
block_size = vllm_config.cache_config.block_size
|
||||
# Should not be called for enc-dec or encoder-only attention.
|
||||
assert self.attn_type == AttentionType.DECODER
|
||||
quant_mode = get_kv_quant_mode(self.kv_cache_dtype)
|
||||
if self.sliding_window is not None:
|
||||
assert not vllm_config.model_config.use_mla, (
|
||||
"MLA is not supported for slidingwindow"
|
||||
@@ -548,6 +552,7 @@ class Attention(nn.Module, AttentionLayerBase):
|
||||
num_kv_heads=self.num_kv_heads,
|
||||
head_size=self.head_size,
|
||||
dtype=self.kv_cache_torch_dtype,
|
||||
kv_quant_mode=quant_mode,
|
||||
sliding_window=self.sliding_window,
|
||||
)
|
||||
else:
|
||||
@@ -557,6 +562,7 @@ class Attention(nn.Module, AttentionLayerBase):
|
||||
head_size=self.head_size,
|
||||
head_size_v=self.head_size_v,
|
||||
dtype=self.kv_cache_torch_dtype,
|
||||
kv_quant_mode=quant_mode,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ from vllm.v1.kv_cache_interface import (
|
||||
AttentionSpec,
|
||||
ChunkedLocalAttentionSpec,
|
||||
KVCacheSpec,
|
||||
get_kv_quant_mode,
|
||||
)
|
||||
|
||||
|
||||
@@ -123,5 +124,6 @@ class ChunkedLocalAttention(Attention):
|
||||
num_kv_heads=self.num_kv_heads,
|
||||
head_size=self.head_size,
|
||||
dtype=self.kv_cache_torch_dtype,
|
||||
kv_quant_mode=get_kv_quant_mode(self.kv_cache_dtype),
|
||||
attention_chunk_size=self.attention_chunk_size,
|
||||
)
|
||||
|
||||
@@ -18,7 +18,11 @@ from vllm.v1.attention.backend import (
|
||||
subclass_attention_backend_with_overrides,
|
||||
)
|
||||
from vllm.v1.attention.selector import get_attn_backend
|
||||
from vllm.v1.kv_cache_interface import CrossAttentionSpec, KVCacheSpec
|
||||
from vllm.v1.kv_cache_interface import (
|
||||
CrossAttentionSpec,
|
||||
KVCacheSpec,
|
||||
get_kv_quant_mode,
|
||||
)
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
@@ -220,4 +224,5 @@ class CrossAttention(Attention):
|
||||
num_kv_heads=self.num_kv_heads,
|
||||
head_size=self.head_size,
|
||||
dtype=self.kv_cache_torch_dtype,
|
||||
kv_quant_mode=get_kv_quant_mode(self.kv_cache_dtype),
|
||||
)
|
||||
|
||||
@@ -26,6 +26,7 @@ from vllm.v1.kv_cache_interface import (
|
||||
AttentionSpec,
|
||||
KVCacheSpec,
|
||||
SinkFullAttentionSpec,
|
||||
get_kv_quant_mode,
|
||||
)
|
||||
|
||||
logger = init_logger(__name__)
|
||||
@@ -217,6 +218,7 @@ class StaticSinkAttention(Attention, CustomOp):
|
||||
head_size_v=self.head_size_v,
|
||||
sink_len=self.sink_len,
|
||||
dtype=self.kv_cache_torch_dtype,
|
||||
kv_quant_mode=get_kv_quant_mode(self.kv_cache_dtype),
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ from vllm.model_executor.layers.quantization.base_config import (
|
||||
)
|
||||
from vllm.platforms import current_platform
|
||||
from vllm.utils.torch_utils import is_quantized_kv_cache
|
||||
from vllm.v1.kv_cache_interface import kv_cache_uses_per_token_head_scales
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
@@ -53,6 +54,20 @@ class BaseKVCacheMethod(QuantizeMethodBase):
|
||||
assert not hasattr(layer, "prob_scale")
|
||||
return
|
||||
|
||||
# Per-token-head quantized KV cache: scales are computed dynamically
|
||||
# per (token, head) in the kernel at cache-write time. Checkpoint
|
||||
# scales are never used regardless of calculate_kv_scales.
|
||||
if kv_cache_uses_per_token_head_scales(layer.kv_cache_dtype):
|
||||
layer._k_scale.copy_(1.0)
|
||||
layer._v_scale.copy_(1.0)
|
||||
layer._k_scale_float = 1.0
|
||||
layer._v_scale_float = 1.0
|
||||
del layer.k_scale
|
||||
del layer.v_scale
|
||||
del layer.q_scale
|
||||
del layer.prob_scale
|
||||
return
|
||||
|
||||
# If the kv-cache is not quantized, we enforce the k/v_scale to be 1.0
|
||||
# regardless whether the kv-scale is available in the checkpoint.
|
||||
# No need to process kv scales after loading if we are going to
|
||||
|
||||
@@ -505,6 +505,7 @@ class Platform:
|
||||
FullAttentionSpec,
|
||||
MambaSpec,
|
||||
MLAAttentionSpec,
|
||||
get_kv_quant_mode,
|
||||
)
|
||||
|
||||
cache_config = vllm_config.cache_config
|
||||
@@ -516,6 +517,8 @@ class Platform:
|
||||
else:
|
||||
kv_cache_dtype = STR_DTYPE_TO_TORCH_DTYPE[cache_config.cache_dtype]
|
||||
|
||||
kv_quant_mode = get_kv_quant_mode(cache_config.cache_dtype)
|
||||
|
||||
# Compute attention page size for 1 token
|
||||
if model_config.use_mla:
|
||||
attn_page_size_1_token = MLAAttentionSpec(
|
||||
@@ -523,6 +526,7 @@ class Platform:
|
||||
num_kv_heads=model_config.get_num_kv_heads(parallel_config),
|
||||
head_size=model_config.get_head_size(),
|
||||
dtype=kv_cache_dtype,
|
||||
kv_quant_mode=kv_quant_mode,
|
||||
).page_size_bytes
|
||||
else:
|
||||
attn_page_size_1_token = FullAttentionSpec(
|
||||
@@ -530,6 +534,7 @@ class Platform:
|
||||
num_kv_heads=model_config.get_num_kv_heads(parallel_config),
|
||||
head_size=model_config.get_head_size(),
|
||||
dtype=kv_cache_dtype,
|
||||
kv_quant_mode=kv_quant_mode,
|
||||
).page_size_bytes
|
||||
|
||||
# Compute mamba page size
|
||||
|
||||
@@ -37,6 +37,8 @@ STR_DTYPE_TO_TORCH_DTYPE = {
|
||||
"fp8_e4m3": torch.uint8,
|
||||
"fp8_e5m2": torch.uint8,
|
||||
"int8": torch.int8,
|
||||
"int8_per_token_head": torch.int8,
|
||||
"fp8_per_token_head": torch.uint8,
|
||||
"fp8_inc": torch.float8_e4m3fn,
|
||||
"fp8_ds_mla": torch.uint8,
|
||||
}
|
||||
@@ -62,7 +64,12 @@ T = TypeVar("T")
|
||||
|
||||
|
||||
def is_quantized_kv_cache(kv_cache_dtype: str) -> bool:
|
||||
return kv_cache_dtype.startswith("fp8")
|
||||
return kv_cache_dtype.startswith("fp8") or kv_cache_dtype.endswith("per_token_head")
|
||||
|
||||
|
||||
def kv_cache_uses_per_token_head_scales(kv_cache_dtype: str) -> bool:
|
||||
"""Return True if *kv_cache_dtype* needs per-token-head scales."""
|
||||
return kv_cache_dtype.endswith("per_token_head")
|
||||
|
||||
|
||||
def is_strictly_contiguous(t: torch.Tensor) -> bool:
|
||||
|
||||
@@ -17,7 +17,9 @@ if TYPE_CHECKING:
|
||||
from vllm.model_executor.layers.quantization.utils.quant_utils import QuantKey
|
||||
from vllm.platforms.interface import DeviceCapability
|
||||
from vllm.v1.attention.backends.utils import KVCacheLayoutType
|
||||
from vllm.v1.kv_cache_interface import AttentionSpec
|
||||
from vllm.v1.kv_cache_interface import AttentionSpec, KVQuantMode
|
||||
|
||||
from vllm.v1.kv_cache_interface import get_kv_quant_mode
|
||||
|
||||
|
||||
class AttentionType(str, Enum):
|
||||
@@ -740,6 +742,13 @@ class AttentionImplBase(ABC, Generic[T]):
|
||||
class AttentionImpl(AttentionImplBase[T], Generic[T]):
|
||||
"""Standard attention implementation with forward method."""
|
||||
|
||||
kv_cache_dtype: str
|
||||
|
||||
@property
|
||||
def kv_quant_mode(self) -> "KVQuantMode":
|
||||
"""Return the KV cache quantization mode for this layer."""
|
||||
return get_kv_quant_mode(self.kv_cache_dtype)
|
||||
|
||||
@abstractmethod
|
||||
def __init__(
|
||||
self,
|
||||
|
||||
@@ -33,9 +33,14 @@ from vllm.v1.attention.backends.utils import get_kv_cache_layout
|
||||
from vllm.v1.attention.ops.triton_prefill_attention import context_attention_fwd
|
||||
from vllm.v1.attention.ops.triton_reshape_and_cache_flash import (
|
||||
triton_reshape_and_cache_flash,
|
||||
triton_reshape_and_cache_flash_per_token_head_quant,
|
||||
)
|
||||
from vllm.v1.attention.ops.triton_unified_attention import unified_attention
|
||||
from vllm.v1.kv_cache_interface import AttentionSpec
|
||||
from vllm.v1.kv_cache_interface import (
|
||||
AttentionSpec,
|
||||
get_kv_quant_mode,
|
||||
kv_cache_uses_per_token_head_scales,
|
||||
)
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
@@ -270,6 +275,8 @@ class TritonAttentionBackend(AttentionBackend):
|
||||
"fp8",
|
||||
"fp8_e4m3",
|
||||
"fp8_e5m2",
|
||||
"int8_per_token_head",
|
||||
"fp8_per_token_head",
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
@@ -302,6 +309,18 @@ class TritonAttentionBackend(AttentionBackend):
|
||||
) -> tuple[int, ...]:
|
||||
if block_size % 16 != 0:
|
||||
raise ValueError("Block size must be a multiple of 16.")
|
||||
if kv_cache_uses_per_token_head_scales(cache_dtype_str):
|
||||
# Pad head_size by sizeof(float32)/sizeof(cache_dtype) so
|
||||
# the per-head scale fits inline. The backend extracts
|
||||
# data[:head_size] and scale[head_size:] via typed views.
|
||||
from vllm.utils.torch_utils import (
|
||||
STR_DTYPE_TO_TORCH_DTYPE,
|
||||
get_dtype_size,
|
||||
)
|
||||
|
||||
cache_dtype = STR_DTYPE_TO_TORCH_DTYPE[cache_dtype_str]
|
||||
scale_pad = get_dtype_size(torch.float32) // get_dtype_size(cache_dtype)
|
||||
return (num_blocks, 2, block_size, num_kv_heads, head_size + scale_pad)
|
||||
return (num_blocks, 2, block_size, num_kv_heads, head_size)
|
||||
|
||||
@staticmethod
|
||||
@@ -365,6 +384,62 @@ class TritonAttentionBackend(AttentionBackend):
|
||||
|
||||
|
||||
class TritonAttentionImpl(AttentionImpl):
|
||||
# Per-token-head quant: scale views carved from inline head padding.
|
||||
_k_scale_cache: torch.Tensor | None = None
|
||||
_v_scale_cache: torch.Tensor | None = None
|
||||
|
||||
def _ensure_scale_caches(self, kv_cache: torch.Tensor) -> None:
|
||||
"""Extract per-head scale views from the padded head dimension.
|
||||
|
||||
The KV cache shape is ``(num_blocks, 2, block_size, nkv, hs+pad)``
|
||||
where ``pad = sizeof(float32) / sizeof(cache_dtype)``. The last
|
||||
``pad`` elements of each head hold one float32 scale. We create
|
||||
strided float32 views over those bytes.
|
||||
|
||||
Scale shape: ``(num_blocks, block_size, num_kv_heads)``
|
||||
"""
|
||||
if self._k_scale_cache is not None:
|
||||
return
|
||||
from vllm.utils.torch_utils import get_dtype_size
|
||||
|
||||
num_blocks, _, block_size, nkv, padded_hs = kv_cache.shape
|
||||
dtype_sz = kv_cache.element_size()
|
||||
scale_pad = get_dtype_size(torch.float32) // dtype_sz # e.g. 4
|
||||
hs = padded_hs - scale_pad
|
||||
|
||||
raw = kv_cache.untyped_storage()
|
||||
base_f32 = torch.tensor([], dtype=torch.float32, device=kv_cache.device).set_(
|
||||
raw
|
||||
)
|
||||
|
||||
# In the raw bytes, each (block, kv_half, slot, head) occupies
|
||||
# padded_hs * dtype_sz bytes. The scale float32 sits at byte
|
||||
# offset hs * dtype_sz within that region.
|
||||
kv_half_bytes = block_size * nkv * padded_hs * dtype_sz
|
||||
full_block_f32 = 2 * kv_half_bytes // 4 # stride between blocks
|
||||
slot_f32 = nkv * padded_hs * dtype_sz // 4 # stride between slots
|
||||
head_f32 = padded_hs * dtype_sz // 4 # stride between heads
|
||||
scale_off_f32 = hs * dtype_sz // 4 # offset to scale within head
|
||||
|
||||
# K scales: kv_half=0
|
||||
self._k_scale_cache = torch.as_strided(
|
||||
base_f32,
|
||||
size=(num_blocks, block_size, nkv),
|
||||
stride=(full_block_f32, slot_f32, head_f32),
|
||||
storage_offset=scale_off_f32,
|
||||
)
|
||||
self._k_scale_cache.fill_(1.0)
|
||||
|
||||
# V scales: kv_half=1, offset by kv_half_bytes
|
||||
v_base_f32 = kv_half_bytes // 4
|
||||
self._v_scale_cache = torch.as_strided(
|
||||
base_f32,
|
||||
size=(num_blocks, block_size, nkv),
|
||||
stride=(full_block_f32, slot_f32, head_f32),
|
||||
storage_offset=v_base_f32 + scale_off_f32,
|
||||
)
|
||||
self._v_scale_cache.fill_(1.0)
|
||||
|
||||
def fused_output_quant_supported(self, quant_key: QuantKey):
|
||||
return quant_key == kFp8StaticTensorSym
|
||||
|
||||
@@ -418,6 +493,9 @@ class TritonAttentionImpl(AttentionImpl):
|
||||
self.use_alibi_sqrt = use_alibi_sqrt
|
||||
self.supports_quant_query_input = current_platform.is_cuda()
|
||||
|
||||
self._kv_quant_mode = get_kv_quant_mode(kv_cache_dtype)
|
||||
self._is_per_token_head_quant = self._kv_quant_mode.is_per_token_head
|
||||
|
||||
def forward(
|
||||
self,
|
||||
layer: torch.nn.Module,
|
||||
@@ -480,15 +558,35 @@ class TritonAttentionImpl(AttentionImpl):
|
||||
layer,
|
||||
)
|
||||
|
||||
# For decoder and cross-attention, use KV cache as before
|
||||
key_cache, value_cache = kv_cache.unbind(1)
|
||||
if is_quantized_kv_cache(self.kv_cache_dtype):
|
||||
if key_cache.dtype != self.fp8_dtype:
|
||||
# Per-token-head quantized KV cache: use separate scale caches.
|
||||
if self._is_per_token_head_quant:
|
||||
self._ensure_scale_caches(kv_cache)
|
||||
key_cache, value_cache = kv_cache.unbind(1)
|
||||
if key_cache.dtype == torch.uint8:
|
||||
key_cache = key_cache.view(self.fp8_dtype)
|
||||
value_cache = value_cache.view(self.fp8_dtype)
|
||||
assert layer._q_scale_float == 1.0, (
|
||||
"A non 1.0 q_scale is not currently supported."
|
||||
k_descale = None
|
||||
v_descale = None
|
||||
k_scale_cache = self._k_scale_cache
|
||||
v_scale_cache = self._v_scale_cache
|
||||
# FP8 per-tensor / auto path (original flow).
|
||||
else:
|
||||
key_cache, value_cache = kv_cache.unbind(1)
|
||||
if is_quantized_kv_cache(self.kv_cache_dtype):
|
||||
if key_cache.dtype != self.fp8_dtype:
|
||||
key_cache = key_cache.view(self.fp8_dtype)
|
||||
value_cache = value_cache.view(self.fp8_dtype)
|
||||
assert layer._q_scale_float == 1.0, (
|
||||
"A non 1.0 q_scale is not currently supported."
|
||||
)
|
||||
descale_shape = (
|
||||
attn_metadata.query_start_loc.shape[0] - 1,
|
||||
key_cache.shape[2],
|
||||
)
|
||||
k_descale = layer._k_scale.expand(descale_shape)
|
||||
v_descale = layer._v_scale.expand(descale_shape)
|
||||
k_scale_cache = None
|
||||
v_scale_cache = None
|
||||
|
||||
cu_seqlens_q = attn_metadata.query_start_loc
|
||||
seqused_k = attn_metadata.seq_lens
|
||||
@@ -502,7 +600,6 @@ class TritonAttentionImpl(AttentionImpl):
|
||||
softmax_segm_max = attn_metadata.softmax_segm_max
|
||||
softmax_segm_expsum = attn_metadata.softmax_segm_expsum
|
||||
|
||||
descale_shape = (cu_seqlens_q.shape[0] - 1, key_cache.shape[2])
|
||||
mm_prefix_range_tensor = attn_metadata.mm_prefix_range_tensor
|
||||
|
||||
unified_attention(
|
||||
@@ -522,8 +619,8 @@ class TritonAttentionImpl(AttentionImpl):
|
||||
block_table=block_table,
|
||||
softcap=self.logits_soft_cap,
|
||||
q_descale=None, # Not supported
|
||||
k_descale=layer._k_scale.expand(descale_shape),
|
||||
v_descale=layer._v_scale.expand(descale_shape),
|
||||
k_descale=k_descale,
|
||||
v_descale=v_descale,
|
||||
seq_threshold_3D=seq_threshold_3D,
|
||||
num_par_softmax_segments=num_par_softmax_segments,
|
||||
softmax_segm_output=softmax_segm_output,
|
||||
@@ -532,6 +629,9 @@ class TritonAttentionImpl(AttentionImpl):
|
||||
sinks=self.sinks,
|
||||
output_scale=output_scale,
|
||||
mm_prefix_range=mm_prefix_range_tensor,
|
||||
kv_quant_mode=self._kv_quant_mode,
|
||||
k_scale_cache=k_scale_cache,
|
||||
v_scale_cache=v_scale_cache,
|
||||
)
|
||||
|
||||
return output
|
||||
@@ -555,10 +655,10 @@ class TritonAttentionImpl(AttentionImpl):
|
||||
attn_metadata: Encoder attention metadata
|
||||
layer: The attention layer
|
||||
"""
|
||||
# For encoder attention, process FP8 quantization if needed
|
||||
# Quantized KV cache is not supported for encoder attention.
|
||||
if is_quantized_kv_cache(self.kv_cache_dtype):
|
||||
raise NotImplementedError(
|
||||
"quantization is not supported for encoder attention"
|
||||
"quantized KV cache is not supported for encoder attention"
|
||||
)
|
||||
|
||||
# Use encoder-specific metadata for sequence information
|
||||
@@ -594,16 +694,28 @@ class TritonAttentionImpl(AttentionImpl):
|
||||
# For encoder attention,
|
||||
# we use direct Q, K, V tensors without caching
|
||||
return
|
||||
# For decoder and cross-attention, use KV cache as before
|
||||
key_cache, value_cache = kv_cache.unbind(1)
|
||||
|
||||
# Reshape the input keys and values and store them in the cache.
|
||||
if self._is_per_token_head_quant:
|
||||
self._ensure_scale_caches(kv_cache)
|
||||
key_cache, value_cache = kv_cache.unbind(1)
|
||||
if key_cache.dtype == torch.uint8:
|
||||
key_cache = key_cache.view(self.fp8_dtype)
|
||||
value_cache = value_cache.view(self.fp8_dtype)
|
||||
triton_reshape_and_cache_flash_per_token_head_quant(
|
||||
key,
|
||||
value,
|
||||
key_cache,
|
||||
value_cache,
|
||||
self._k_scale_cache,
|
||||
self._v_scale_cache,
|
||||
slot_mapping,
|
||||
)
|
||||
return
|
||||
# For decoder and cross-attention, use KV cache as before.
|
||||
key_cache, value_cache = kv_cache.unbind(1)
|
||||
if is_quantized_kv_cache(self.kv_cache_dtype):
|
||||
key_cache = key_cache.view(self.fp8_dtype)
|
||||
value_cache = value_cache.view(self.fp8_dtype)
|
||||
# triton kernel does not support uint8 kv_cache
|
||||
# (because some explicit casts (e.g. float8_e4m3fnuz)
|
||||
# are not supported)
|
||||
triton_reshape_and_cache_flash(
|
||||
key,
|
||||
value,
|
||||
@@ -616,6 +728,8 @@ class TritonAttentionImpl(AttentionImpl):
|
||||
)
|
||||
|
||||
def fused_rope_kvcache_supported(self):
|
||||
if self._is_per_token_head_quant:
|
||||
return False
|
||||
return rocm_aiter_ops.is_enabled()
|
||||
|
||||
def do_rope_and_kv_cache_update(
|
||||
|
||||
@@ -3,10 +3,16 @@
|
||||
|
||||
import torch
|
||||
|
||||
from vllm.model_executor.layers.quantization.utils.quant_utils import (
|
||||
FP8_DTYPE,
|
||||
get_fp8_min_max,
|
||||
)
|
||||
from vllm.platforms import current_platform
|
||||
from vllm.triton_utils import tl, triton
|
||||
from vllm.utils.torch_utils import is_quantized_kv_cache
|
||||
|
||||
FP8_MIN, FP8_MAX = get_fp8_min_max()
|
||||
|
||||
|
||||
@triton.jit
|
||||
def reshape_and_cache_kernel_flash(
|
||||
@@ -118,6 +124,198 @@ def reshape_and_cache_kernel_flash(
|
||||
return
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Per-token-head dynamic quantization kernel
|
||||
# Grid: (num_tokens, NUM_KV_HEADS)
|
||||
# Each program handles one (token, head) pair:
|
||||
# 1. Loads K (or V) for that single head
|
||||
# 2. Computes absmax across head_size → scale = absmax / QUANT_MAX
|
||||
# 3. Quantizes and stores the data + per-head scale
|
||||
#
|
||||
# Parametrised by QUANT_MAX / QUANT_MIN so the same code path works
|
||||
# for int8 (±127/128), fp8_e4m3 (±448), and other formats.
|
||||
# ---------------------------------------------------------------------------
|
||||
@triton.jit
|
||||
def _reshape_cache_per_token_head(
|
||||
key_ptr, # [num_tokens, num_kv_heads, head_size]
|
||||
value_ptr, # [num_tokens, num_kv_heads, head_size_v]
|
||||
key_cache_ptr, # [num_blocks, block_size, num_kv_heads, head_size]
|
||||
value_cache_ptr, # [num_blocks, block_size, num_kv_heads, head_size_v]
|
||||
k_scale_cache_ptr, # [num_blocks, block_size, num_kv_heads] float32
|
||||
v_scale_cache_ptr, # [num_blocks, block_size, num_kv_heads] float32
|
||||
slot_mapping_ptr, # [num_tokens]
|
||||
stride_key_tok: tl.int64,
|
||||
stride_key_head: tl.int64,
|
||||
stride_val_tok: tl.int64,
|
||||
stride_val_head: tl.int64,
|
||||
stride_kc_blk: tl.int64, # key_cache stride over blocks
|
||||
stride_kc_slot: tl.int64, # key_cache stride over slots
|
||||
stride_kc_head: tl.int64, # key_cache stride over heads
|
||||
stride_vc_blk: tl.int64,
|
||||
stride_vc_slot: tl.int64,
|
||||
stride_vc_head: tl.int64,
|
||||
stride_ks_blk: tl.int64, # k_scale_cache stride[0] (blocks)
|
||||
stride_ks_slot: tl.int64, # k_scale_cache stride[1] (slots)
|
||||
stride_ks_head: tl.int64, # k_scale_cache stride[2] (heads)
|
||||
stride_vs_blk: tl.int64, # v_scale_cache stride[0] (blocks)
|
||||
stride_vs_slot: tl.int64, # v_scale_cache stride[1] (slots)
|
||||
stride_vs_head: tl.int64, # v_scale_cache stride[2] (heads)
|
||||
block_size: tl.constexpr,
|
||||
head_size: tl.constexpr,
|
||||
head_size_v: tl.constexpr,
|
||||
HEAD_SIZE_PADDED: tl.constexpr, # next_power_of_2(max(head_size, head_size_v))
|
||||
QUANT_MAX: tl.constexpr = 127.0,
|
||||
QUANT_MIN: tl.constexpr = -128.0,
|
||||
):
|
||||
tok = tl.program_id(0)
|
||||
head = tl.program_id(1)
|
||||
|
||||
slot = tl.load(slot_mapping_ptr + tok).to(tl.int64)
|
||||
if slot < 0:
|
||||
return
|
||||
|
||||
blk = slot // block_size
|
||||
slot_in_blk = slot % block_size
|
||||
|
||||
dim_offs = tl.arange(0, HEAD_SIZE_PADDED)
|
||||
|
||||
# ---- Key: load one head → absmax → quantize → store -------------------
|
||||
k_mask = dim_offs < head_size
|
||||
k_h = tl.load(
|
||||
key_ptr + tok * stride_key_tok + head * stride_key_head + dim_offs,
|
||||
mask=k_mask,
|
||||
other=0.0,
|
||||
).to(tl.float32)
|
||||
|
||||
k_scale = tl.maximum(tl.max(tl.abs(k_h)) / QUANT_MAX, 1e-6)
|
||||
tl.store(
|
||||
k_scale_cache_ptr
|
||||
+ blk * stride_ks_blk
|
||||
+ slot_in_blk * stride_ks_slot
|
||||
+ head * stride_ks_head,
|
||||
k_scale,
|
||||
)
|
||||
|
||||
k_q = tl.clamp(k_h * (1.0 / k_scale), QUANT_MIN, QUANT_MAX)
|
||||
tl.store(
|
||||
key_cache_ptr
|
||||
+ blk * stride_kc_blk
|
||||
+ slot_in_blk * stride_kc_slot
|
||||
+ head * stride_kc_head
|
||||
+ dim_offs,
|
||||
k_q,
|
||||
mask=k_mask,
|
||||
)
|
||||
|
||||
# ---- Value: same per-head approach ------------------------------------
|
||||
v_mask = dim_offs < head_size_v
|
||||
v_h = tl.load(
|
||||
value_ptr + tok * stride_val_tok + head * stride_val_head + dim_offs,
|
||||
mask=v_mask,
|
||||
other=0.0,
|
||||
).to(tl.float32)
|
||||
|
||||
v_scale = tl.maximum(tl.max(tl.abs(v_h)) / QUANT_MAX, 1e-6)
|
||||
tl.store(
|
||||
v_scale_cache_ptr
|
||||
+ blk * stride_vs_blk
|
||||
+ slot_in_blk * stride_vs_slot
|
||||
+ head * stride_vs_head,
|
||||
v_scale,
|
||||
)
|
||||
|
||||
v_q = tl.clamp(v_h * (1.0 / v_scale), QUANT_MIN, QUANT_MAX)
|
||||
tl.store(
|
||||
value_cache_ptr
|
||||
+ blk * stride_vc_blk
|
||||
+ slot_in_blk * stride_vc_slot
|
||||
+ head * stride_vc_head
|
||||
+ dim_offs,
|
||||
v_q,
|
||||
mask=v_mask,
|
||||
)
|
||||
|
||||
|
||||
# Mapping from cache torch dtype to (QUANT_MAX, QUANT_MIN) for the
|
||||
# per-token-head quantization kernel.
|
||||
_PER_TOKEN_HEAD_QUANT_PARAMS: dict[torch.dtype, tuple[float, float]] = {
|
||||
torch.int8: (127.0, -128.0),
|
||||
FP8_DTYPE: (FP8_MAX, FP8_MIN),
|
||||
}
|
||||
|
||||
|
||||
def triton_reshape_and_cache_flash_per_token_head_quant(
|
||||
key: torch.Tensor, # [num_tokens, num_kv_heads, head_size]
|
||||
value: torch.Tensor, # [num_tokens, num_kv_heads, head_size_v]
|
||||
key_cache: torch.Tensor, # [num_blocks, block_size, num_kv_heads, head_size]
|
||||
value_cache: torch.Tensor, # [num_blocks, block_size, num_kv_heads, head_size_v]
|
||||
k_scale_cache: torch.Tensor, # [num_blocks, block_size, num_kv_heads] float32
|
||||
v_scale_cache: torch.Tensor, # [num_blocks, block_size, num_kv_heads] float32
|
||||
slot_mapping: torch.Tensor, # [num_tokens]
|
||||
):
|
||||
"""Quantize key/value per (token, head) and write to paged cache.
|
||||
|
||||
Computes one scale = absmax / QUANT_MAX per (token, head), stores
|
||||
quantized data in key_cache/value_cache, and stores the float32
|
||||
scale in k_scale_cache/v_scale_cache.
|
||||
|
||||
The quantization range (QUANT_MAX, QUANT_MIN) is derived from the
|
||||
cache tensor dtype so the same code path works for int8 and fp8.
|
||||
"""
|
||||
cache_dtype = key_cache.dtype
|
||||
quant_params = _PER_TOKEN_HEAD_QUANT_PARAMS.get(cache_dtype)
|
||||
if quant_params is None:
|
||||
raise ValueError(
|
||||
f"Per-token-head quantization not supported for cache dtype "
|
||||
f"{cache_dtype}. Supported: {list(_PER_TOKEN_HEAD_QUANT_PARAMS)}"
|
||||
)
|
||||
quant_max, quant_min = quant_params
|
||||
|
||||
num_tokens, num_kv_heads, head_size = key.shape
|
||||
head_size_v = value.shape[2]
|
||||
head_size_padded = triton.next_power_of_2(max(head_size, head_size_v))
|
||||
|
||||
block_size = key_cache.shape[1]
|
||||
|
||||
if current_platform.is_rocm() or current_platform.is_xpu():
|
||||
num_warps = 4
|
||||
else:
|
||||
num_warps = min(16, max(1, head_size_padded // 32))
|
||||
|
||||
_reshape_cache_per_token_head[(num_tokens, num_kv_heads)](
|
||||
key_ptr=key,
|
||||
value_ptr=value,
|
||||
key_cache_ptr=key_cache,
|
||||
value_cache_ptr=value_cache,
|
||||
k_scale_cache_ptr=k_scale_cache,
|
||||
v_scale_cache_ptr=v_scale_cache,
|
||||
slot_mapping_ptr=slot_mapping,
|
||||
stride_key_tok=key.stride(0),
|
||||
stride_key_head=key.stride(1),
|
||||
stride_val_tok=value.stride(0),
|
||||
stride_val_head=value.stride(1),
|
||||
stride_kc_blk=key_cache.stride(0),
|
||||
stride_kc_slot=key_cache.stride(1),
|
||||
stride_kc_head=key_cache.stride(2),
|
||||
stride_vc_blk=value_cache.stride(0),
|
||||
stride_vc_slot=value_cache.stride(1),
|
||||
stride_vc_head=value_cache.stride(2),
|
||||
stride_ks_blk=k_scale_cache.stride(0),
|
||||
stride_ks_slot=k_scale_cache.stride(1),
|
||||
stride_ks_head=k_scale_cache.stride(2),
|
||||
stride_vs_blk=v_scale_cache.stride(0),
|
||||
stride_vs_slot=v_scale_cache.stride(1),
|
||||
stride_vs_head=v_scale_cache.stride(2),
|
||||
block_size=block_size,
|
||||
head_size=head_size,
|
||||
head_size_v=head_size_v,
|
||||
HEAD_SIZE_PADDED=head_size_padded,
|
||||
QUANT_MAX=quant_max,
|
||||
QUANT_MIN=quant_min,
|
||||
num_warps=num_warps,
|
||||
)
|
||||
|
||||
|
||||
def triton_reshape_and_cache_flash(
|
||||
key: torch.Tensor, # [num_tokens, num_heads, head_size]
|
||||
value: torch.Tensor, # [num_tokens, num_heads, head_size]
|
||||
@@ -224,7 +422,6 @@ def triton_reshape_and_cache_flash(
|
||||
block_size=block_size,
|
||||
x=x,
|
||||
USE_HEAD_MAJOR_LAYOUT=use_head_major_layout,
|
||||
# FP8 flags
|
||||
FP8_KV_CACHE=FP8_KV_CACHE,
|
||||
# autotune parameters
|
||||
TILE_SIZE=TILE_SIZE,
|
||||
|
||||
@@ -13,6 +13,7 @@ import vllm.envs as envs
|
||||
from vllm.logger import init_logger
|
||||
from vllm.platforms import current_platform
|
||||
from vllm.triton_utils import tl, triton
|
||||
from vllm.v1.kv_cache_interface import KVQuantMode
|
||||
|
||||
logger = init_logger(__name__)
|
||||
is_batch_invariant = envs.VLLM_BATCH_INVARIANT
|
||||
@@ -32,6 +33,63 @@ def apply_softcap(S, x):
|
||||
return x * (p1 - p2) / (p1 + p2)
|
||||
|
||||
|
||||
@triton.jit
|
||||
def _prepare_kv_tile(
|
||||
data,
|
||||
Q,
|
||||
tensor_scale,
|
||||
scale_cache_ptr,
|
||||
physical_block_idx,
|
||||
seq_offset,
|
||||
kv_head_idx,
|
||||
stride_s_blk,
|
||||
stride_s_slot,
|
||||
stride_s_head,
|
||||
tile_mask,
|
||||
BLOCK_SIZE: tl.constexpr,
|
||||
KV_QUANT_MODE: tl.constexpr,
|
||||
):
|
||||
"""Prepare a loaded KV tile for attention computation.
|
||||
|
||||
Casts the raw KV data to Q's dtype and loads per-token-head scales
|
||||
when applicable:
|
||||
|
||||
- ``KV_QUANT_MODE == 0``: cast only (no-op for bf16/fp16).
|
||||
- ``KV_QUANT_MODE == 1`` (FP8 per-tensor): dequantize inline
|
||||
using the tensor-wide scale.
|
||||
- ``KV_QUANT_MODE >= 2`` (per-token-head int8/fp8): cast to Q's
|
||||
dtype and return per-head scales separately — the caller applies
|
||||
them after the dot product for better numerical efficiency.
|
||||
|
||||
Returns ``(data, token_head_scales)``. *token_head_scales* is only
|
||||
meaningful when ``KV_QUANT_MODE >= 2``; callers gate its use on
|
||||
the same constexpr so the compiler eliminates dead code.
|
||||
"""
|
||||
# KV_QUANT_MODE values: 0=none, 1=fp8 per-tensor,
|
||||
# 2=int8 per-token-head, 3=fp8 per-token-head
|
||||
|
||||
# Placeholder scales (float32) — never read when KV_QUANT_MODE < 2.
|
||||
unused_scales = tile_mask.to(tl.float32)
|
||||
|
||||
if KV_QUANT_MODE == 1: # FP8 per-tensor
|
||||
if Q.dtype.is_fp8():
|
||||
return data.to(Q.dtype), unused_scales
|
||||
return (data.to(tl.float32) * tl.load(tensor_scale)).to(Q.dtype), unused_scales
|
||||
if KV_QUANT_MODE >= 2: # per-token-head (int8 or fp8)
|
||||
scale_idx = (
|
||||
physical_block_idx * stride_s_blk
|
||||
+ (seq_offset % BLOCK_SIZE) * stride_s_slot
|
||||
+ kv_head_idx * stride_s_head
|
||||
)
|
||||
token_head_scales = tl.load(
|
||||
scale_cache_ptr + scale_idx, mask=tile_mask, other=1.0
|
||||
)
|
||||
return data.to(Q.dtype), token_head_scales
|
||||
# .to(Q.dtype) is a no-op when data is already Q's type (bf16/fp16),
|
||||
# but required so Triton sees consistent return types across branches.
|
||||
return data.to(Q.dtype), unused_scales
|
||||
|
||||
|
||||
@triton.jit
|
||||
def find_seq_idx(
|
||||
query_start_len_ptr,
|
||||
@@ -105,8 +163,20 @@ def kernel_unified_attention_2d(
|
||||
num_seqs: tl.int32,
|
||||
BLOCK_M: tl.constexpr, # int
|
||||
USE_FP8: tl.constexpr, # bool
|
||||
# KV cache quantization: 0=none, 1=fp8, 2=per-token-head
|
||||
KV_QUANT_MODE: tl.constexpr = 0,
|
||||
FP8_MIN: tl.constexpr = float8_info.min,
|
||||
FP8_MAX: tl.constexpr = float8_info.max,
|
||||
# Per-token-head scale caches (KV_QUANT_MODE >= 2)
|
||||
# Shape: [num_blocks, block_size, num_kv_heads]
|
||||
k_scale_cache_ptr=None,
|
||||
v_scale_cache_ptr=None,
|
||||
stride_ks_blk=0,
|
||||
stride_ks_slot=0,
|
||||
stride_ks_head=0,
|
||||
stride_vs_blk=0,
|
||||
stride_vs_slot=0,
|
||||
stride_vs_head=0,
|
||||
):
|
||||
q_block_global_idx = tl.program_id(0)
|
||||
kv_head_idx = tl.program_id(1)
|
||||
@@ -258,14 +328,21 @@ def kernel_unified_attention_2d(
|
||||
mask=dim_mask[:, None] & tile_mask[None, :],
|
||||
other=0.0,
|
||||
)
|
||||
|
||||
if K_load.dtype.is_fp8():
|
||||
if Q.dtype.is_fp8():
|
||||
K = K_load
|
||||
else:
|
||||
K = (K_load.to(tl.float32) * tl.load(k_scale)).to(Q.dtype)
|
||||
else:
|
||||
K = K_load
|
||||
K, k_token_head_scales = _prepare_kv_tile(
|
||||
K_load,
|
||||
Q,
|
||||
k_scale,
|
||||
k_scale_cache_ptr,
|
||||
physical_block_idx,
|
||||
seq_offset,
|
||||
kv_head_idx,
|
||||
stride_ks_blk,
|
||||
stride_ks_slot,
|
||||
stride_ks_head,
|
||||
tile_mask,
|
||||
BLOCK_SIZE,
|
||||
KV_QUANT_MODE,
|
||||
)
|
||||
|
||||
# V : (TILE_SIZE, HEAD_SIZE)
|
||||
V_load = tl.load(
|
||||
@@ -273,14 +350,21 @@ def kernel_unified_attention_2d(
|
||||
mask=dim_mask[None, :] & tile_mask[:, None],
|
||||
other=0.0,
|
||||
)
|
||||
|
||||
if V_load.dtype.is_fp8():
|
||||
if Q.dtype.is_fp8():
|
||||
V = V_load
|
||||
else:
|
||||
V = (V_load.to(tl.float32) * tl.load(v_scale)).to(Q.dtype)
|
||||
else:
|
||||
V = V_load
|
||||
V, v_token_head_scales = _prepare_kv_tile(
|
||||
V_load,
|
||||
Q,
|
||||
v_scale,
|
||||
v_scale_cache_ptr,
|
||||
physical_block_idx,
|
||||
seq_offset,
|
||||
kv_head_idx,
|
||||
stride_vs_blk,
|
||||
stride_vs_slot,
|
||||
stride_vs_head,
|
||||
tile_mask,
|
||||
BLOCK_SIZE,
|
||||
KV_QUANT_MODE,
|
||||
)
|
||||
|
||||
# Compute attention mask: causal by default (key <= query)
|
||||
query_abs_pos = context_len + query_pos[:, None]
|
||||
@@ -318,7 +402,12 @@ def kernel_unified_attention_2d(
|
||||
# S : (BLOCK_M, TILE_SIZE)
|
||||
S = tl.zeros(shape=(BLOCK_M, TILE_SIZE), dtype=tl.float32)
|
||||
|
||||
S += scale * tl.dot(Q, K)
|
||||
# Per-token-head quant: fuse softmax_scale with per-head k_scale
|
||||
# to avoid a separate BLOCK_M × TILE_SIZE multiply on S.
|
||||
if KV_QUANT_MODE >= 2:
|
||||
S += tl.dot(Q, K) * (scale * k_token_head_scales[None, :])
|
||||
else:
|
||||
S += scale * tl.dot(Q, K)
|
||||
|
||||
if USE_SOFTCAP:
|
||||
S = apply_softcap(S, softcap)
|
||||
@@ -382,7 +471,12 @@ def kernel_unified_attention_2d(
|
||||
)
|
||||
|
||||
# acc : (BLOCK_M, HEAD_SIZE_PADDED)
|
||||
acc += tl.dot(P.to(V.dtype), V)
|
||||
# Per-token-head quant: apply v_scale to P instead of V.
|
||||
if KV_QUANT_MODE >= 2:
|
||||
P_v = (P * v_token_head_scales[None, :]).to(V.dtype)
|
||||
acc += tl.dot(P_v, V)
|
||||
else:
|
||||
acc += tl.dot(P.to(V.dtype), V)
|
||||
|
||||
# epilogue
|
||||
acc = acc / L[:, None]
|
||||
@@ -453,6 +547,18 @@ def kernel_unified_attention_3d(
|
||||
USE_MM_PREFIX: tl.constexpr, # bool
|
||||
MAX_MM_RANGES: tl.constexpr, # int
|
||||
mm_prefix_range_ptr, # [num_seqs] - prefix length for each sequence
|
||||
# KV cache quantization: 0=none, 1=fp8, 2=per-token-head
|
||||
KV_QUANT_MODE: tl.constexpr = 0,
|
||||
# Per-token-head scale caches (KV_QUANT_MODE >= 2)
|
||||
# Shape: [num_blocks, block_size, num_kv_heads]
|
||||
k_scale_cache_ptr=None,
|
||||
v_scale_cache_ptr=None,
|
||||
stride_ks_blk=0,
|
||||
stride_ks_slot=0,
|
||||
stride_ks_head=0,
|
||||
stride_vs_blk=0,
|
||||
stride_vs_slot=0,
|
||||
stride_vs_head=0,
|
||||
):
|
||||
q_block_global_idx = tl.program_id(0)
|
||||
kv_head_idx = tl.program_id(1)
|
||||
@@ -613,14 +719,21 @@ def kernel_unified_attention_3d(
|
||||
mask=dim_mask[:, None] & tile_mask[None, :],
|
||||
other=0.0,
|
||||
)
|
||||
|
||||
if K_load.dtype.is_fp8():
|
||||
if Q.dtype.is_fp8():
|
||||
K = K_load
|
||||
else:
|
||||
K = (K_load.to(tl.float32) * tl.load(k_scale)).to(Q.dtype)
|
||||
else:
|
||||
K = K_load
|
||||
K, k_token_head_scales = _prepare_kv_tile(
|
||||
K_load,
|
||||
Q,
|
||||
k_scale,
|
||||
k_scale_cache_ptr,
|
||||
physical_block_idx,
|
||||
seq_offset,
|
||||
kv_head_idx,
|
||||
stride_ks_blk,
|
||||
stride_ks_slot,
|
||||
stride_ks_head,
|
||||
tile_mask,
|
||||
BLOCK_SIZE,
|
||||
KV_QUANT_MODE,
|
||||
)
|
||||
|
||||
# V : (TILE_SIZE, HEAD_SIZE)
|
||||
V_load = tl.load(
|
||||
@@ -628,14 +741,21 @@ def kernel_unified_attention_3d(
|
||||
mask=dim_mask[None, :] & tile_mask[:, None],
|
||||
other=0.0,
|
||||
)
|
||||
|
||||
if V_load.dtype.is_fp8():
|
||||
if Q.dtype.is_fp8():
|
||||
V = V_load
|
||||
else:
|
||||
V = (V_load.to(tl.float32) * tl.load(v_scale)).to(Q.dtype)
|
||||
else:
|
||||
V = V_load
|
||||
V, v_token_head_scales = _prepare_kv_tile(
|
||||
V_load,
|
||||
Q,
|
||||
v_scale,
|
||||
v_scale_cache_ptr,
|
||||
physical_block_idx,
|
||||
seq_offset,
|
||||
kv_head_idx,
|
||||
stride_vs_blk,
|
||||
stride_vs_slot,
|
||||
stride_vs_head,
|
||||
tile_mask,
|
||||
BLOCK_SIZE,
|
||||
KV_QUANT_MODE,
|
||||
)
|
||||
|
||||
# Compute attention mask: causal by default (key <= query)
|
||||
query_abs_pos = context_len + query_pos[:, None]
|
||||
@@ -672,7 +792,13 @@ def kernel_unified_attention_3d(
|
||||
|
||||
# S : (BLOCK_M, TILE_SIZE)
|
||||
S = tl.zeros(shape=(BLOCK_M, TILE_SIZE), dtype=tl.float32)
|
||||
S += scale * tl.dot(Q, K)
|
||||
|
||||
# Per-token-head quant: fuse softmax_scale with per-head k_scale
|
||||
# to avoid a separate BLOCK_M × TILE_SIZE multiply on S.
|
||||
if KV_QUANT_MODE >= 2:
|
||||
S += tl.dot(Q, K) * (scale * k_token_head_scales[None, :])
|
||||
else:
|
||||
S += scale * tl.dot(Q, K)
|
||||
|
||||
if USE_SOFTCAP:
|
||||
S = apply_softcap(S, softcap)
|
||||
@@ -736,7 +862,12 @@ def kernel_unified_attention_3d(
|
||||
)
|
||||
|
||||
# acc : (BLOCK_M, HEAD_SIZE_PADDED)
|
||||
acc += tl.dot(P.to(V.dtype), V)
|
||||
# Per-token-head quant: apply v_scale to P instead of V.
|
||||
if KV_QUANT_MODE >= 2:
|
||||
P_v = (P * v_token_head_scales[None, :]).to(V.dtype)
|
||||
acc += tl.dot(P_v, V)
|
||||
else:
|
||||
acc += tl.dot(P.to(V.dtype), V)
|
||||
|
||||
segm_output_offset = (
|
||||
query_offset_0[:, None].to(tl.int64)
|
||||
@@ -911,6 +1042,10 @@ def unified_attention(
|
||||
# Optional tensor for prefix lengths (PrefixLM support)
|
||||
mm_prefix_range=None,
|
||||
use_alibi_sqrt=False,
|
||||
# KV cache quantization mode and per-token-head scale caches.
|
||||
kv_quant_mode: KVQuantMode = KVQuantMode.NONE,
|
||||
k_scale_cache=None, # [num_blocks, block_size, num_kv_heads] float32
|
||||
v_scale_cache=None, # [num_blocks, block_size, num_kv_heads] float32
|
||||
):
|
||||
assert causal, "Only causal attention is supported"
|
||||
assert q_descale is None, "Q scales not supported"
|
||||
@@ -1040,6 +1175,15 @@ def unified_attention(
|
||||
num_seqs=num_seqs,
|
||||
BLOCK_M=BLOCK_M,
|
||||
USE_FP8=output_scale is not None,
|
||||
KV_QUANT_MODE=kv_quant_mode,
|
||||
k_scale_cache_ptr=k_scale_cache,
|
||||
v_scale_cache_ptr=v_scale_cache,
|
||||
stride_ks_blk=k_scale_cache.stride(0) if k_scale_cache is not None else 0,
|
||||
stride_ks_slot=k_scale_cache.stride(1) if k_scale_cache is not None else 0,
|
||||
stride_ks_head=k_scale_cache.stride(2) if k_scale_cache is not None else 0,
|
||||
stride_vs_blk=v_scale_cache.stride(0) if v_scale_cache is not None else 0,
|
||||
stride_vs_slot=v_scale_cache.stride(1) if v_scale_cache is not None else 0,
|
||||
stride_vs_head=v_scale_cache.stride(2) if v_scale_cache is not None else 0,
|
||||
)
|
||||
else:
|
||||
kernel_unified_attention_3d[
|
||||
@@ -1092,6 +1236,15 @@ def unified_attention(
|
||||
num_seqs=num_seqs,
|
||||
BLOCK_M=BLOCK_M,
|
||||
NUM_SEGMENTS_PER_SEQ=num_par_softmax_segments,
|
||||
KV_QUANT_MODE=kv_quant_mode,
|
||||
k_scale_cache_ptr=k_scale_cache,
|
||||
v_scale_cache_ptr=v_scale_cache,
|
||||
stride_ks_blk=k_scale_cache.stride(0) if k_scale_cache is not None else 0,
|
||||
stride_ks_slot=k_scale_cache.stride(1) if k_scale_cache is not None else 0,
|
||||
stride_ks_head=k_scale_cache.stride(2) if k_scale_cache is not None else 0,
|
||||
stride_vs_blk=v_scale_cache.stride(0) if v_scale_cache is not None else 0,
|
||||
stride_vs_slot=v_scale_cache.stride(1) if v_scale_cache is not None else 0,
|
||||
stride_vs_head=v_scale_cache.stride(2) if v_scale_cache is not None else 0,
|
||||
)
|
||||
reduce_segments[(q.shape[0], num_query_heads)](
|
||||
output_ptr=out,
|
||||
|
||||
@@ -1,21 +1,70 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
from dataclasses import dataclass, fields, replace
|
||||
from enum import IntEnum
|
||||
from math import prod
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import torch
|
||||
from typing_extensions import Self
|
||||
|
||||
from vllm.config import VllmConfig
|
||||
from vllm.logger import init_logger
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm.config import VllmConfig
|
||||
from vllm.utils.math_utils import cdiv
|
||||
from vllm.utils.torch_utils import get_dtype_size
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# KV cache quantization mode
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class KVQuantMode(IntEnum):
|
||||
"""KV cache quantization mode.
|
||||
|
||||
Used by attention backends and kernels to dispatch quantization logic
|
||||
without string matching on ``kv_cache_dtype``.
|
||||
"""
|
||||
|
||||
NONE = 0
|
||||
FP8_PER_TENSOR = 1 # per-tensor scales (current fp8 path)
|
||||
INT8_PER_TOKEN_HEAD = 2 # per-token-head dynamic scales for int8
|
||||
FP8_PER_TOKEN_HEAD = 3 # per-token-head dynamic scales for fp8
|
||||
|
||||
@property
|
||||
def is_per_token_head(self) -> bool:
|
||||
"""True for any per-token-head quantization mode."""
|
||||
return self >= 2
|
||||
|
||||
|
||||
def get_kv_quant_mode(kv_cache_dtype: str) -> KVQuantMode:
|
||||
"""Map a ``kv_cache_dtype`` string to a :class:`KVQuantMode`."""
|
||||
if kv_cache_dtype == "int8_per_token_head":
|
||||
return KVQuantMode.INT8_PER_TOKEN_HEAD
|
||||
if kv_cache_dtype == "fp8_per_token_head":
|
||||
return KVQuantMode.FP8_PER_TOKEN_HEAD
|
||||
if kv_cache_dtype.startswith("fp8"):
|
||||
return KVQuantMode.FP8_PER_TENSOR
|
||||
return KVQuantMode.NONE
|
||||
|
||||
|
||||
def is_quantized_kv_cache(kv_cache_dtype: str) -> bool:
|
||||
return get_kv_quant_mode(kv_cache_dtype) != KVQuantMode.NONE
|
||||
|
||||
|
||||
def kv_cache_uses_per_token_head_scales(kv_cache_dtype: str) -> bool:
|
||||
"""Return True if *kv_cache_dtype* needs per-token-head scales."""
|
||||
return get_kv_quant_mode(kv_cache_dtype).is_per_token_head
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class KVCacheSpec:
|
||||
"""
|
||||
@@ -66,11 +115,19 @@ class AttentionSpec(KVCacheSpec):
|
||||
num_kv_heads: int
|
||||
head_size: int
|
||||
dtype: torch.dtype
|
||||
kv_quant_mode: KVQuantMode = KVQuantMode.NONE
|
||||
page_size_padded: int | None = None
|
||||
|
||||
@property
|
||||
def page_size_bytes(self) -> int:
|
||||
real_page_size = self.real_page_size_bytes
|
||||
# Per-token-head scales are stored in separate tensors managed
|
||||
# by the attention backend, but the memory is carved from the
|
||||
# raw KV cache allocation so it must be budgeted here.
|
||||
if self.kv_quant_mode.is_per_token_head:
|
||||
real_page_size += (
|
||||
2 * self.block_size * self.num_kv_heads * get_dtype_size(torch.float32)
|
||||
)
|
||||
if self.page_size_padded is not None:
|
||||
assert self.page_size_padded >= real_page_size
|
||||
return self.page_size_padded
|
||||
@@ -159,6 +216,7 @@ class FullAttentionSpec(AttentionSpec):
|
||||
head_size=specs[0].head_size,
|
||||
head_size_v=specs[0].head_size_v,
|
||||
dtype=specs[0].dtype,
|
||||
kv_quant_mode=specs[0].kv_quant_mode,
|
||||
page_size_padded=specs[0].page_size_padded,
|
||||
sliding_window=cls.merge_window_sizes(sliding_window),
|
||||
attention_chunk_size=cls.merge_window_sizes(attention_chunk_size),
|
||||
@@ -220,6 +278,7 @@ class MLAAttentionSpec(FullAttentionSpec):
|
||||
num_kv_heads=specs[0].num_kv_heads,
|
||||
head_size=specs[0].head_size,
|
||||
dtype=specs[0].dtype,
|
||||
kv_quant_mode=specs[0].kv_quant_mode,
|
||||
page_size_padded=specs[0].page_size_padded,
|
||||
cache_dtype_str=cache_dtype_str_set.pop(),
|
||||
)
|
||||
@@ -352,6 +411,7 @@ class SinkFullAttentionSpec(FullAttentionSpec):
|
||||
head_size_v=specs[0].head_size_v,
|
||||
sink_len=specs[0].sink_len,
|
||||
dtype=specs[0].dtype,
|
||||
kv_quant_mode=specs[0].kv_quant_mode,
|
||||
page_size_padded=specs[0].page_size_padded,
|
||||
sliding_window=cls.merge_window_sizes(sliding_window),
|
||||
attention_chunk_size=cls.merge_window_sizes(attention_chunk_size),
|
||||
|
||||
Reference in New Issue
Block a user