[Kernel] Fuse FP8 output quantization into merge_attn_states (#36518)

Signed-off-by: Carl You <4531192+carlyou@users.noreply.github.com>
This commit is contained in:
Carl Y
2026-04-03 01:47:04 +00:00
committed by GitHub
parent 1f5ec2889c
commit 3bc2734dd0
8 changed files with 516 additions and 70 deletions
+2
View File
@@ -265,6 +265,7 @@ def merge_attn_states(
suffix_lse: torch.Tensor,
output_lse: torch.Tensor | None = None,
prefill_tokens_with_context: int | None = None,
output_scale: torch.Tensor | None = None,
) -> None:
torch.ops._C.merge_attn_states(
output,
@@ -274,6 +275,7 @@ def merge_attn_states(
suffix_output,
suffix_lse,
prefill_tokens_with_context,
output_scale,
)
+25 -10
View File
@@ -14,6 +14,7 @@ def merge_attn_states(
suffix_lse: torch.Tensor,
output_lse: torch.Tensor | None = None,
prefill_tokens_with_context: int | None = None,
output_scale: torch.Tensor | None = None,
) -> None:
"""Merge partial attention outputs from prefix (KV cache) and suffix
(new tokens) into a single output tensor using the log-sum-exp (LSE)
@@ -41,27 +42,37 @@ def merge_attn_states(
>= this value are decode or context-free prefill tokens whose
output is taken directly from suffix_output. If None, all tokens
are treated as having context.
output_scale: Optional scalar tensor for FP8 static quantization.
When provided, output must be FP8 dtype.
"""
# NOTE(DefTruth): Currently, custom merge_attn_states CUDA kernel
# does not support FP8 dtype, fallback to use Triton kernel.
def supported_dtypes(o: torch.Tensor) -> bool:
return o.dtype in [torch.float32, torch.half, torch.bfloat16]
# does not support FP8 dtype for inputs, fallback to use Triton kernel.
# However, when output_scale is provided, the inputs are still BF16/FP16
# and the output is FP8 — both CUDA and Triton support this.
# FP8 output requires output_scale to be set.
if output.dtype not in (torch.float32, torch.half, torch.bfloat16):
assert output_scale is not None, (
f"output_scale is required when output is {output.dtype}"
)
def supported_dtypes(prefix: torch.Tensor) -> bool:
return prefix.dtype in [torch.float32, torch.half, torch.bfloat16]
# NOTE(DefTruth): Currently, custom merge_attn_states CUDA
# kernel load/store 128b(16 bytes) per memory issue within
# thread. Namely, the headsize(headdim) must be multiple of
# pack_size (float32 -> 4, half/bfloat16 -> 8).
def supported_headdim(o: torch.Tensor) -> bool:
headdim = o.shape[2] # [NUM_TOKENS, NUM_HEADS, HEAD_SIZE]
if o.dtype == torch.float32:
# pack_size based on input dtype (float32 -> 4, half/bfloat16 -> 8).
def supported_headdim(prefix: torch.Tensor) -> bool:
headdim = prefix.shape[2] # [NUM_TOKENS, NUM_HEADS, HEAD_SIZE]
if prefix.dtype == torch.float32:
return headdim % 4 == 0
return headdim % 8 == 0
if (
current_platform.is_cuda()
and supported_dtypes(output)
and supported_headdim(output)
and supported_dtypes(prefix_output)
and supported_headdim(prefix_output)
):
from vllm._custom_ops import merge_attn_states
@@ -73,9 +84,12 @@ def merge_attn_states(
suffix_lse,
output_lse,
prefill_tokens_with_context,
output_scale,
)
else:
from vllm.v1.attention.ops.triton_merge_attn_states import merge_attn_states
from vllm.v1.attention.ops.triton_merge_attn_states import (
merge_attn_states,
)
return merge_attn_states(
output,
@@ -85,4 +99,5 @@ def merge_attn_states(
suffix_lse,
output_lse,
prefill_tokens_with_context,
output_scale,
)
@@ -3,8 +3,11 @@
import torch
from vllm.platforms import current_platform
from vllm.triton_utils import tl, triton
float8_info = torch.finfo(current_platform.fp8_dtype())
# Implements section 2.2 of https://www.arxiv.org/pdf/2501.01005
# can be used to combine partial attention results (in the split-KV case)
@@ -16,14 +19,15 @@ def merge_attn_states(
suffix_lse: torch.Tensor,
output_lse: torch.Tensor | None = None,
prefill_tokens_with_context: int | None = None,
output_scale: torch.Tensor | None = None,
) -> None:
num_tokens = output.shape[0]
num_query_heads = output.shape[1]
head_size = output.shape[2]
padded_head_size = triton.next_power_of_2(head_size)
# We assume the output stride on num_head is not always as same as the
# `suffix_output` and `prefix_output`, as them might be padded by the attention
# backend.
# `suffix_output` and `prefix_output`, as them might be padded by the
# attention backend.
prefix_head_stride = prefix_output.stride(1)
output_head_stride = output.stride(1)
@@ -41,10 +45,12 @@ def merge_attn_states(
suffix_lse,
prefix_head_stride,
output_head_stride,
output_scale,
head_size,
padded_head_size,
output_lse is not None,
prefill_tokens_with_context,
output_scale is not None,
)
@@ -58,10 +64,14 @@ def merge_attn_states_kernel(
suffix_lse, # [NUM_HEADS, NUM_TOKENS]
prefix_head_stride,
output_head_stride,
output_scale, # scale tensor or None
HEAD_SIZE: tl.constexpr,
PADDED_HEAD_SIZE: tl.constexpr,
OUTPUT_LSE: tl.constexpr,
prefill_tokens_with_context: tl.constexpr,
USE_FP8: tl.constexpr,
FP8_MIN: tl.constexpr = float8_info.min,
FP8_MAX: tl.constexpr = float8_info.max,
):
token_idx = tl.program_id(0)
num_tokens = tl.num_programs(0)
@@ -87,6 +97,12 @@ def merge_attn_states_kernel(
+ head_arange,
mask=head_mask,
)
if USE_FP8:
s_out = s_out * (1.0 / tl.load(output_scale))
s_out = tl.clamp(s_out, FP8_MIN, FP8_MAX)
s_out = s_out.to(output.dtype.element_ty)
tl.store(
output
+ token_idx * num_heads * output_head_stride
@@ -143,6 +159,12 @@ def merge_attn_states_kernel(
p_scale = p_se / out_se
s_scale = s_se / out_se
out = p_out * p_scale + s_out * s_scale
if USE_FP8:
out = out * (1.0 / tl.load(output_scale))
out = tl.clamp(out, FP8_MIN, FP8_MAX)
out = out.to(output.dtype.element_ty)
tl.store(
output
+ token_idx * num_heads * output_head_stride