[Qwen3.8-Flash-Next] Support FP8 indexer cache for QSA (#54890)
Signed-off-by: Thien Tran <gau.nernst@yahoo.com.sg>
This commit is contained in:
@@ -725,10 +725,16 @@ class QSAMetadataBuilder(AttentionMetadataBuilder[QSAForwardMetadata]):
|
||||
|
||||
|
||||
class QSAStateBackend(AttentionBackend):
|
||||
"""Key-only dummy backend for out-of-band BF16 QSA side-cache operations."""
|
||||
"""Key-only dummy backend for out-of-band QSA side-cache operations."""
|
||||
|
||||
supported_dtypes: ClassVar[list[torch.dtype]] = [torch.bfloat16]
|
||||
supported_kv_cache_dtypes: ClassVar[list[CacheDType]] = ["auto", "bfloat16"]
|
||||
# fp8 entries allow the optional e4m3 compressed indexer cache.
|
||||
supported_kv_cache_dtypes: ClassVar[list[CacheDType]] = [
|
||||
"auto",
|
||||
"bfloat16",
|
||||
"fp8",
|
||||
"fp8_e4m3",
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def get_name() -> str:
|
||||
@@ -790,8 +796,12 @@ class _QSAStateCache(nn.Module, AttentionLayerBase):
|
||||
"""Adapt the unified [B, H, N, C] view to QSA's [B, N, H, C]."""
|
||||
if kv_cache.ndim != 4 or kv_cache.shape[1] != 1:
|
||||
raise ValueError("QSA state cache must be [blocks, 1, states, width]")
|
||||
if kv_cache.dtype != torch.bfloat16 or kv_cache.shape[3] != self.head_size:
|
||||
raise ValueError("QSA state cache does not match its packed BF16 spec")
|
||||
if kv_cache.dtype != self.dtype or kv_cache.shape[3] != self.head_size:
|
||||
raise ValueError(
|
||||
f"QSA state cache does not match its spec "
|
||||
f"(dtype {kv_cache.dtype} != {self.dtype} or width "
|
||||
f"{kv_cache.shape[3]} != {self.head_size})"
|
||||
)
|
||||
super().bind_kv_cache(kv_cache.transpose(1, 2))
|
||||
|
||||
def get_attn_backend(self) -> type[AttentionBackend]:
|
||||
@@ -850,7 +860,7 @@ class QSAKeyStateCache(_QSAStateCache):
|
||||
|
||||
|
||||
class QSACompressedKeyCache(_QSAStateCache):
|
||||
"""Normalized, group-first-RoPE BF16 key at one row per complete group."""
|
||||
"""Normed, group-first-RoPE key at one row per complete group."""
|
||||
|
||||
def get_kv_cache_spec(self, vllm_config: VllmConfig) -> KVCacheSpec:
|
||||
del vllm_config
|
||||
|
||||
@@ -147,6 +147,21 @@ class QSAIndexer(nn.Module):
|
||||
|
||||
cache_config = vllm_config.cache_config
|
||||
cache_prefix = f"{prefix}." if prefix else ""
|
||||
# Plain e4m3 without scales: Q and the compressed K are RMSNormed
|
||||
# before quantization, and the logits kernels dot fp8 x fp8 directly.
|
||||
self.indexer_kv_dtype = vllm_config.attention_config.resolve_indexer_kv_dtype(
|
||||
"bf16"
|
||||
)
|
||||
if self.indexer_kv_dtype == "fp8":
|
||||
indexer_dtype = torch.float8_e4m3fn
|
||||
elif self.indexer_kv_dtype == "bf16":
|
||||
indexer_dtype = torch.bfloat16
|
||||
else:
|
||||
raise NotImplementedError(
|
||||
f"indexer_kv_dtype={self.indexer_kv_dtype!r} is not supported "
|
||||
"by the Qwen4Exp QSA indexer (only 'bf16' or 'fp8')."
|
||||
)
|
||||
self.indexer_dtype = indexer_dtype
|
||||
self.raw_key_cache = QSAKeyStateCache(
|
||||
head_size=self.index_head_dim,
|
||||
dtype=torch.bfloat16,
|
||||
@@ -158,7 +173,7 @@ class QSAIndexer(nn.Module):
|
||||
)
|
||||
self.compressed_key_cache = QSACompressedKeyCache(
|
||||
head_size=self.index_head_dim,
|
||||
dtype=torch.bfloat16,
|
||||
dtype=indexer_dtype,
|
||||
compress_ratio=self.compress_ratio,
|
||||
prefix=f"{cache_prefix}compressed_key_cache",
|
||||
cache_config=cache_config,
|
||||
@@ -276,6 +291,7 @@ class QSAIndexer(nn.Module):
|
||||
num_tokens,
|
||||
self.index_n_heads,
|
||||
self.index_head_dim,
|
||||
dtype=self.indexer_dtype,
|
||||
)
|
||||
qsa_pre_indexer(
|
||||
projected_q,
|
||||
@@ -313,6 +329,7 @@ class QSAIndexer(nn.Module):
|
||||
self.q_layernorm.variance_epsilon,
|
||||
).reshape_as(q)
|
||||
q = apply_qsa_rope(self.rotary_emb, positions, q)
|
||||
q = q.to(self.indexer_dtype)
|
||||
|
||||
raw_key_cache = raw_key_state_cache.key_cache
|
||||
rope_position_cache = raw_key_state_cache.rope_position_cache
|
||||
|
||||
@@ -200,6 +200,9 @@ def _qsa_mqa_paged_prefill_kernel(
|
||||
other=0.0,
|
||||
eviction_policy="evict_first",
|
||||
)
|
||||
# With an fp8 cache, SM90 lowers this dot to reduced-precision wgmma
|
||||
# accumulation; SM100 tcgen05 accumulates in fp32. The SM90 error
|
||||
# (~1e-4 relative) is far below the e4m3 quantization noise floor.
|
||||
scores = tl.dot(keys, query, out_dtype=tl.float32)
|
||||
scores = tl.reshape(scores, (BLOCK_N, TILE_R, NUM_HEADS_PADDED))
|
||||
score = tl.sum(tl.maximum(scores, 0.0), axis=2)
|
||||
@@ -337,7 +340,7 @@ def warmup_qsa_mqa_paged_decode(
|
||||
for decode_query_len, num_requests in profiles:
|
||||
num_rows = decode_query_len * num_requests
|
||||
q_ptr = TritonWarmupTensor(
|
||||
torch.bfloat16,
|
||||
k_cache.dtype,
|
||||
shape=(num_rows, num_heads, head_dim),
|
||||
)
|
||||
logits_ptr = TritonWarmupTensor(
|
||||
@@ -365,7 +368,8 @@ def warmup_qsa_mqa_paged_decode(
|
||||
BLOCK_N=_DECODE_BLOCK_N,
|
||||
TILES_PER_PROG=tiles_per_program,
|
||||
STAGES=2,
|
||||
num_warps=2,
|
||||
# tuned on GB300
|
||||
num_warps=1 if k_cache.dtype == torch.float8_e4m3fn else 2,
|
||||
grid=(
|
||||
num_requests,
|
||||
triton.cdiv(columns, _DECODE_BLOCK_N * tiles_per_program),
|
||||
@@ -393,7 +397,11 @@ def _prefill_logits(
|
||||
logits = torch.empty(
|
||||
(num_queries, logits_width), dtype=torch.float32, device=q.device
|
||||
)
|
||||
TILE_R = 64
|
||||
# tuned on GB300
|
||||
if k_cache.dtype == torch.float8_e4m3fn:
|
||||
TILE_R, STAGES, num_warps = 32, 2, 8
|
||||
else:
|
||||
TILE_R, STAGES, num_warps = 64, 2, 4
|
||||
BLOCK_N = 64
|
||||
K_TILES = 16
|
||||
grid = (
|
||||
@@ -421,8 +429,8 @@ def _prefill_logits(
|
||||
TILE_R=TILE_R,
|
||||
BLOCK_N=BLOCK_N,
|
||||
K_TILES=K_TILES,
|
||||
STAGES=2,
|
||||
num_warps=4,
|
||||
STAGES=STAGES,
|
||||
num_warps=num_warps,
|
||||
)
|
||||
return logits
|
||||
|
||||
@@ -521,6 +529,7 @@ def qsa_select_paged_decode(
|
||||
assert token_topk % compress_ratio == 0
|
||||
assert block_indices.shape == (q.shape[0], token_topk // compress_ratio)
|
||||
assert decode_query_len > 0 and q.shape[0] % decode_query_len == 0
|
||||
assert q.dtype == k_cache.dtype, "Q and the compressed K cache must match"
|
||||
num_requests = q.shape[0] // decode_query_len
|
||||
assert page_table.shape[0] == num_requests
|
||||
assert visible_blocks.shape == (q.shape[0],)
|
||||
@@ -550,7 +559,8 @@ def qsa_select_paged_decode(
|
||||
BLOCK_N=_DECODE_BLOCK_N,
|
||||
TILES_PER_PROG=tiles_per_program,
|
||||
STAGES=2,
|
||||
num_warps=2,
|
||||
# tuned on GB300
|
||||
num_warps=1 if k_cache.dtype == torch.float8_e4m3fn else 2,
|
||||
)
|
||||
_topk(
|
||||
logits,
|
||||
@@ -593,6 +603,7 @@ def qsa_select_paged_prefill(
|
||||
|
||||
assert token_topk % compress_ratio == 0
|
||||
assert block_indices.shape == (q.shape[0], token_topk // compress_ratio)
|
||||
assert q.dtype == k_cache.dtype, "Q and the compressed K cache must match"
|
||||
rows = q.shape[0]
|
||||
# No row scores beyond cdiv(max_seq_len, compress_ratio) compressed
|
||||
# columns. Round up to 64 to keep the logits row stride
|
||||
|
||||
Reference in New Issue
Block a user