Independent CPU decode/prefill regression evidence for the index_k fix in PR #12

#41
by malaiwah - opened

Independent reproduction supporting PR #12

This reports additional end-to-end CPU evidence for the cache-ownership fix already submitted by @ZenAlexa in PR #12. Please use that PR for the code change; I am not proposing a duplicate.

Cause and scope

In inference/model.py, Indexer.forward publishes shared_attn.index_k inside if self.owns_k and latent is not None. On an incomplete compression group the source does not republish its cache, but the same forward still reads shared_attn.index_k. That slot can contain a later source's cache left over from the previous forward.

This breaks SharedAttentionRuntime's documented invariant that every source writes before its consumers read. Attention._compress_kv already republishes shared_attn.compress_kv on every source call, independently of whether a group completes.

The released configuration has KV sources [2, 8, 14, 20]: layers 2/8/14 have compression ratio 2; layer 20 has ratio 1 and publishes last. The ownership hazard therefore exists in the released schedule too. The numerical measurements below use reduced random-weight models, not the released 510 GB checkpoint; they do not quantify real-checkpoint quality loss.

Measurement: reference model versus itself

For each absolute position 200 through 223, compare:

  • Prefill 200 tokens, then incremental one-token decode.
  • A one-shot prefill of the identical token prefix ending at that position.

Both paths use the same reference-model source and identically initialized weights. This is CPU with local PyTorch replacements for the TileLang kernels, not a run of the official GPU kernels. The supplied CPU shim is used on both sides; activation fake quantization remains enabled. No Transformers model implementation or downloaded model weights participate.

Configuration: five layers, FP32 parameters, batch 2, max_seq_len=256, compress_ratios=(0,2,2,1,1), KV/index sources (1,3), index_topk=64, no Engram, vision, MTP or candidate prefilter. Sources are pinned to dba1be0a40aa45a94ad051997016db3960a90277.

Reference code Max absolute logit difference, all 24 decode positions Argmax agreement (48 batch-position comparisons)
Unmodified 1.991 70.83% (34/48)
With the same cache-publication fix as PR #12 4.029e-5 100% (48/48)

For example, unmodified differences at positions 200–207 were 1.2, 0.021, 0.39, 0.020, 0.31, 0.031, 0.53, 0.044. The direct ownership violation occurs at incomplete-group steps (even positions for ratio 2). Its downstream effects can persist into subsequent positions; divergence is not confined exclusively to those steps.

The 128-token onset in this reduced setup should not be treated as a universal sliding-window threshold: index_topk=64 and ratio 2 also make 128 tokens the point after which selecting among more than 64 completed groups becomes relevant. The released model uses different indexer budgets.

Reproduction

Runnable scripts and observed output: https://gist.github.com/malaiwah/502ae094bd6e942720744d8683393f22

The gist contains the two Python files and observed output. Place check_decode.py and reference_cpu.py together. With PyTorch supporting float8_e4m3fn and float8_e8m0fnu, plus NumPy, Pillow and huggingface_hub installed:

hf download deepseek-ai/DeepSeek-V4.1-Flash --revision dba1be0a40aa45a94ad051997016db3960a90277 --include 'inference/*.py' --local-dir ./reference
python check_decode.py unpatched ./reference/inference
python check_decode.py patched ./reference/inference

The patched mode changes only the cache-publication guard in memory; it does not edit the downloaded sources. Exact small residuals can depend on the PyTorch version and CPU backend.

This complements the official-kernel H100 checks already described in PR #12. Discovered while validating Transformers PR #48721.

Sign up or log in to comment