[Model][DSv4.1] Store the whole KV in MXFP8 (FlashMLA V4.1 record) (#56893)

Signed-off-by: Yongye Zhu <yongye@inferact.ai>
Signed-off-by: Yongye Zhu <zyy1102000@gmail.com>
Co-authored-by: Yongye Zhu <yongye@inferact.ai>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Yongye Zhu
2026-09-14 20:12:55 -07:00
committed by GitHub
co-authored by Yongye Zhu Claude Opus 5
parent a529c1a748
commit e6eb0d120c
14 changed files with 709 additions and 110 deletions
+130
View File
@@ -215,6 +215,136 @@ def test_v41_fused_save_compress_and_insert(
torch.testing.assert_close(cache_backing, expected_cache, rtol=0, atol=0)
def _mxfp8_record_reference(
latent_row: torch.Tensor, cos_sin_row: torch.Tensor
) -> tuple[torch.Tensor, torch.Tensor]:
"""Reference V4.1 record for one token: 512 fp8 bytes + 16 UE8M0 scales.
Mirrors FlashMLA's ``KVCacheLayout.V41_FP8Sparse`` quantizer: rotate the
RoPE tail first, then scale every 32-dim tile, RoPE tiles included.
"""
row = latent_row.float()
c, s = cos_sin_row.chunk(2)
rotated = row.clone()
even, odd = row[448::2], row[449::2]
rotated[448::2] = even * c - odd * s
rotated[449::2] = odd * c + even * s
quantized, scales = _ue8m0_reference(rotated, 32, 448.0)
return quantized.view(torch.uint8), (scales.log2() + 127).to(torch.uint8)
@pytest.mark.skipif(not current_platform.is_cuda(), reason="CUDA only")
@pytest.mark.parametrize("compress_ratio", [1, 2])
def test_v41_rope_insert_mxfp8_record(compress_ratio: int):
"""The 528-byte V4.1 record quantizes the RoPE dims instead of keeping them
in bf16, so a page is [block_size x 512 fp8][block_size x 16 UE8M0].
Byte-exact against the reference quantizer, including the rows and page
padding the kernel must leave alone.
"""
from vllm.models.deepseek_v41.common.ops.fused_compress_quant_cache import (
rope_quant_insert,
)
torch.manual_seed(7)
device = "cuda"
cache_block = 64
cache_stride = math.ceil(cache_block * 528 / 512) * 512
num_tokens = 12
positions = torch.arange(num_tokens, dtype=torch.int64, device=device)
latent = torch.randn(num_tokens, 512, dtype=torch.bfloat16, device=device)
angles = torch.randn(64, 32, device=device)
cos_sin = torch.cat((angles.cos(), angles.sin()), dim=-1)
cache_backing = torch.full((2, cache_stride), 165, dtype=torch.uint8, device=device)
cache = cache_backing.as_strided((2, cache_block, 528), (cache_stride, 528, 1))
# Leave one token unmapped so the kernel's negative-slot skip is covered.
slots = torch.arange(num_tokens, dtype=torch.int64, device=device)
slots[3] = -1
rope_quant_insert(latent, positions, cos_sin, cache, slots, compress_ratio)
expected = torch.full_like(cache_backing, 165)
for t in range(num_tokens):
slot = slots[t].item()
pos = positions[t].item()
if slot < 0 or (pos + 1) % compress_ratio:
continue
page, row = divmod(slot, cache_block)
values, scales = _mxfp8_record_reference(
latent[t], cos_sin[pos // compress_ratio * compress_ratio]
)
expected[page, row * 512 : (row + 1) * 512] = values
scale_offset = cache_block * 512 + row * 16
expected[page, scale_offset : scale_offset + 16] = scales
torch.testing.assert_close(cache_backing, expected, rtol=0, atol=0)
@pytest.mark.skipif(not current_platform.is_cuda(), reason="CUDA only")
def test_v41_mxfp8_cache_round_trip():
"""quantize_and_insert -> dequantize_and_gather recovers the V4.1 record.
The RoPE dims now go through fp8, so they carry quantization error too --
the check is that every dim survives within one MXFP8 tile step, and that
the bytes on the way match the reference quantizer.
"""
from vllm.models.deepseek_v41.common.ops import (
dequantize_and_gather_k_cache,
quantize_and_insert_k_cache,
)
torch.manual_seed(11)
device = "cuda"
block_size = 64
num_tokens = 70
num_blocks = 4
page_bytes = math.ceil(block_size * 528 / 512) * 512
k = torch.randn(num_tokens, 512, dtype=torch.bfloat16, device=device)
cache = torch.zeros(num_blocks, page_bytes, dtype=torch.uint8, device=device)
slot_mapping = torch.arange(num_tokens, dtype=torch.int64, device=device)
quantize_and_insert_k_cache(
k, cache, slot_mapping, block_size=block_size, bytes_per_token=528
)
for t in (0, 1, block_size, num_tokens - 1):
values, scales = _ue8m0_reference(k[t].float(), 32, 448.0)
page, row = divmod(t, block_size)
torch.testing.assert_close(
cache[page, row * 512 : (row + 1) * 512],
values.view(torch.uint8),
rtol=0,
atol=0,
)
scale_offset = block_size * 512 + row * 16
torch.testing.assert_close(
cache[page, scale_offset : scale_offset + 16],
(scales.log2() + 127).to(torch.uint8),
rtol=0,
atol=0,
)
out = torch.zeros(1, num_tokens, 512, dtype=torch.bfloat16, device=device)
dequantize_and_gather_k_cache(
out,
cache.view(num_blocks, block_size, 528),
seq_lens=torch.tensor([num_tokens], dtype=torch.int32, device=device),
gather_lens=None,
block_table=torch.arange(num_blocks, dtype=torch.int32, device=device).view(
1, -1
),
block_size=block_size,
offset=0,
)
# Half an e4m3 ULP at the top of the range (16 units) bounds the error.
tile_amax = k.float().abs().view(num_tokens, 16, 32).amax(-1).clamp(min=1e-4)
tolerance = 16.0 * (tile_amax / 448.0).log2().ceil().exp2()
error = (out[0].float() - k.float()).abs()
assert (error <= tolerance.repeat_interleave(32, dim=-1)).all()
@pytest.mark.skipif(not current_platform.is_cuda(), reason="CUDA only")
@pytest.mark.parametrize("compress_ratio", [1, 2])
@pytest.mark.parametrize("store_fp8", [False, True])
@@ -424,6 +424,104 @@ def test_kv_path_matches_reference(num_tokens: int, block_size: int):
)
# ── Test 2c: DeepSeek V4.1 MXFP8 record (kv_mxfp8=True) ──────────────────────
V41_HEAD_BYTES = HEAD_DIM + HEAD_DIM // 32 # 512 fp8 + 16 UE8M0 scales = 528
@pytest.mark.parametrize("num_tokens", [1, 17, 64])
@pytest.mark.parametrize("block_size", [32, 64])
def test_kv_path_mxfp8_record_matches_reference(num_tokens: int, block_size: int):
"""``kv_mxfp8=True`` writes DeepSeek's V4.1 record: all 512 dims as fp8 e4m3
with one UE8M0 scale per 32 dims, no bf16 RoPE tail.
Both sides quantize the same bf16-rounded rotated row, so the bytes must
match the Triton encoder exactly apart from the documented 1-ULP bf16
rounding split on the rotation itself.
"""
from vllm.models.deepseek_v41.common.ops import (
dequantize_and_gather_k_cache as v41_dequantize_and_gather_k_cache,
)
from vllm.models.deepseek_v41.common.ops import (
quantize_and_insert_k_cache as v41_quantize_and_insert_k_cache,
)
torch.manual_seed(3)
device = "cuda"
dtype = torch.bfloat16
max_pos = 4096
kv = torch.randn(num_tokens, HEAD_DIM, dtype=dtype, device=device)
positions = torch.arange(num_tokens, dtype=torch.int64, device=device)
cos_sin_cache = make_cos_sin_cache(max_pos, ROPE_DIM, torch.float32, device)
num_blocks = (num_tokens + block_size - 1) // block_size + 1
slot_mapping = torch.arange(num_tokens, dtype=torch.int64, device=device)
k_cache_ref = torch.zeros(
num_blocks, block_size * V41_HEAD_BYTES, dtype=torch.uint8, device=device
)
v41_quantize_and_insert_k_cache(
apply_rope_gptj_last_k(kv, positions, cos_sin_cache),
k_cache_ref,
slot_mapping,
block_size=block_size,
use_fnuz=USE_FNUZ,
bytes_per_token=V41_HEAD_BYTES,
)
k_cache_fused = torch.zeros_like(k_cache_ref)
q_dummy = torch.zeros(num_tokens, 1, HEAD_DIM, dtype=dtype, device=device)
torch.ops._C.fused_deepseek_v4_qnorm_rope_kv_rope_quant_insert(
q_dummy,
kv,
k_cache_fused,
slot_mapping,
positions,
cos_sin_cache,
64,
1e-6,
block_size,
True,
True, # kv_mxfp8
)
# A pre-quantization bf16 tie can land the two encoders on adjacent e4m3
# codes, so compare after dequantizing and allow one step at the tile scale.
def _dequant(cache_2d):
out = torch.zeros(1, num_tokens, HEAD_DIM, dtype=dtype, device=device)
v41_dequantize_and_gather_k_cache(
out,
cache_2d.view(num_blocks, block_size, V41_HEAD_BYTES),
torch.tensor([num_tokens], dtype=torch.int32, device=device),
None,
torch.arange(num_blocks, dtype=torch.int32, device=device).unsqueeze(0),
block_size,
offset=0,
use_fnuz=USE_FNUZ,
)
return out[0]
rec_ref = _dequant(k_cache_ref).float()
rec_fused = _dequant(k_cache_fused).float()
tile_scale = (
(rec_ref.abs().view(num_tokens, 16, 32).amax(-1).clamp(min=1e-4) / FP8_MAX)
.log2()
.ceil()
.exp2()
)
# One e4m3 ULP at the top of the range is 32 quantized units.
step = 32.0 * tile_scale.repeat_interleave(32, dim=-1)
assert ((rec_fused - rec_ref).abs() <= step).all()
# The NoPE dims see no rotation at all, so they must be byte-identical.
nope_bytes = slice(0, block_size * HEAD_DIM)
ref_rows = k_cache_ref[:, nope_bytes].view(num_blocks, block_size, HEAD_DIM)
fused_rows = k_cache_fused[:, nope_bytes].view(num_blocks, block_size, HEAD_DIM)
torch.testing.assert_close(
fused_rows[..., :NOPE_DIM], ref_rows[..., :NOPE_DIM], rtol=0, atol=0
)
# ── Test 2b: DP padding (slot_mapping shorter than q/kv) ─────────────────────