runtime: wait for the Host load in the per-token-head MLA history read

MLATokenToKVPool.get_mla_kv_buffer read the per-token-head planes
straight from kv_buffer, without the layerwise Host-load wait that the
pool's other accessors make (get_key_buffer, get_value_buffer,
kv_write_target, set_mla_kv_buffer). It reads a Host prefix hit's
history: read_prefix_history (the MLA chunked prefix replay) and the
Dots3 Note model call it.

No caller reads stale rows today: the DeepSeek-V3 chunked prefix replay
writes the layer's new latent rows through the prologue
(kv_write_target, which waits for the same layer) before it reads the
history, and the Kimi-K3 and Dots3 Note caches refuse per-token-head KV.
The branch now takes the planes from get_key_buffer, which waits for the
layer's load and returns the same plane triple, so the read no longer
relies on an earlier write of the layer.

The Kimi-K3 pool's layer_accesses_wait_for_load note no longer lists
this branch among the accessors that do not wait.

Test: the host_cache fixture of test_l2_layerwise_fences.py builds a
per-token-head MLA pool, and a new test holds a Host load back while
get_mla_kv_buffer reads the loaded page. Without this change the read
returns the page's old bytes on both copy paths.

Signed-off-by: Jiying Dong <87510204+dongjiyingdjy@users.noreply.github.com>
This commit is contained in:
Jiying Dong
2026-11-02 10:24:33 +00:00
parent b5538e0778
commit b8750bd7f5
3 changed files with 79 additions and 4 deletions
@@ -49,9 +49,8 @@ class HybridKDATokenToKVPool(MLATokenToKVPool):
#: A Kimi-K3 replay reaches the latent rows only through get_key_buffer
#: (the prologue's kv_write_target, the attention break's reads) and the
#: KDA State through get_component, both of which wait for the layer.
#: The accessors that do not wait are not reached: get_state_buffers has
#: callers only in tests, and get_mla_kv_buffer's per-token-head branch
#: needs per-token-head KV, which this class refuses.
#: The accessor that does not wait is not reached: get_state_buffers has
#: callers only in tests.
#: In a mixed round with speculative decoding, the verify rows also reach
#: State through pointer tables built once, with no wait per layer (verify
#: seeding, replay descriptors, the post-acceptance commit). That is safe
@@ -212,7 +212,7 @@ class MLATokenToKVPool(CachePool):
dst_dtype = dst_dtype or self.dtype
if self.quant_method == "per_token_head":
k_lora_cache, k_scale_cache, k_rope_cache = self.kv_buffer[layer_id]
k_lora_cache, k_scale_cache, k_rope_cache = self.get_key_buffer(layer_id)
k_lora = k_lora_cache[loc].view(self.dtype).float()
k_scale = k_scale_cache[loc]
k_rope = k_rope_cache[loc].float()
+76
View File
@@ -74,6 +74,7 @@ def host_cache(request, io_backend):
from tokenspeed.runtime.layers.attention.configs.base import AttnConfig
from tokenspeed.runtime.layers.attention.configs.dsa import DSAConfig
from tokenspeed.runtime.layers.attention.configs.mha import MHAConfig
from tokenspeed.runtime.layers.attention.configs.mla import MLAConfig
from tokenspeed.runtime.layers.attention.kv_cache.factory import (
create_cache_arena,
create_cache_pool,
@@ -190,6 +191,37 @@ def host_cache(request, io_backend):
draft_attn_config=None,
**recipe_args,
)
elif family == "mla_per_token_head":
# DeepSeek-V3 MLA with --kv-cache-quant-method per_token_head: FP8
# latent, FP32 scale and BF16 RoPE planes per layer.
attn_config = AttnConfig(
kv_cache_dtype=torch.float8_e4m3fn,
kernel_page_size=64,
components=(
MLAConfig(
backend_name="mla",
num_attention_heads=64,
num_kv_heads=64,
head_dim=192,
attn_tp_size=1,
kv_lora_rank=512,
qk_nope_head_dim=128,
qk_rope_head_dim=64,
v_head_dim=128,
scaling=1.0,
kv_cache_dim=576,
),
),
**dict(model_wide, kv_cache_quant_method="per_token_head"),
)
recipe = cache_recipe(
"mla",
model_config=SimpleNamespace(num_attention_layers=2),
attn_config=attn_config,
draft_model_config=None,
draft_attn_config=None,
**recipe_args,
)
elif family == "glm53_flash":
from test.runtime.test_glm53_flash_cache_spec import _recipe
@@ -390,6 +422,50 @@ def test_a_draft_context_write_lands_after_its_tail_page_load(host_cache):
assert torch.equal(latent[slots].view(torch.uint8), expected)
@pytest.mark.parametrize("io_backend", ["kernel", "direct"])
@pytest.mark.parametrize(
"host_cache",
[("mla_per_token_head", "full_attention")],
ids=["mla_per_token_head"],
indirect=True,
)
def test_a_per_token_head_history_read_sees_its_load(host_cache):
"""The MLA chunked prefix replay reads a Host hit's history through
``get_mla_kv_buffer`` (``read_prefix_history``). With per-token-head
planes the read waits for the layer's load as the pool's other accessors
do; otherwise it sees what the pages held before."""
from tokenspeed.runtime.layers.paged_attention import PagedAttention
cache = host_cache
pool = cache.pool
page_size = cache.arena.prefix_granularity
page = 2
slots = torch.arange(page * page_size, (page + 1) * page_size, device="cuda")
layer = PagedAttention(
1, pool.kv_cache_dim, 1.0, 1, 0, rotary_emb=None, qk_norm=None
)
host_rows = [
rows.view(torch.uint8)
for rows in pool.get_mla_kv_buffer(layer, slots - page_size, torch.bfloat16)
]
# Load destinations are never zeroed: give the page other bytes.
cache.arena.zero_blocks({"full_attention": np.asarray([page])})
with torch.cuda.stream(cache.executor.load_stream):
torch.cuda._sleep(_HOLD_BACK_CYCLES)
cache.handle.execute(
cache.ts.ExecutionPlan(),
None,
cache_ops=[cache.LoadBackOp(3, [cache.group], 1, page)],
)
seen = pool.get_mla_kv_buffer(layer, slots, torch.bfloat16)
assert not cache.executor.load_stream.query()
torch.cuda.synchronize()
assert [int(ack.op_id) for ack in cache.handle.poll_cache_results()] == [3]
for rows, host in zip(seen, host_rows, strict=True):
assert torch.equal(rows.view(torch.uint8), host)
@pytest.mark.parametrize("io_backend", ["kernel", "direct"])
@pytest.mark.parametrize(
"host_cache",