[Edge][Mobile] Ring cache keeps the fused softmax so it survives w4a16

The score-space join removed the concat as intended and is numerically
identical (140.3 dB), but it cannot be quantized: replacing torch.softmax
with an explicit max/exp/sum/div made QNN's context-binary converter exit 14
under w4a16, while every fp16 build compiled. Two earlier shapes failed the
same way -- a broadcast multiply plus ReduceSum for the new token's score,
and a two-axis broadcast multiply for its value contribution.

The device profile says the win was never the concat anyway. Of a sliding
layer, Output was 9.9%, its quantise conversion 3.1% and the window Slice
7.4% -- all of it the cost of handing a 512-entry window back across the
graph boundary once per token. So keep roll's attention exactly as it was,
one fused Softmax over concatenated keys, and change only what the layer
returns: the single new K/V entry, which the runtime drops into the ring
slot. Ring order needs no fixing up, because softmax over keys is
order-independent and each cached key already carries its own rotary phase.

Measured on a real Galaxy S25, sliding layer, ms:

  w4a16   0.917 -> 0.838   (-8.6%)   <- now compiles
  fp16    1.689 -> 1.486   (-12.0%)

Full-attention layers keep roll: ring gains them nothing (1.567 vs 1.559 at
fp16, the concat saved being spent on the extra score matmul), so the
default layout is now per layer type.

Parity against the reference is 140.3 dB for the mixed default with
identical top-1/top-5.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L1ax5wYLA6XbqEL1i8L9gZ
This commit is contained in:
tzhouamandClaude Opus 5 committed 2026-09-11 17:41:29 +08:00
1 parent 67bee59fda
commit 9a4f295625
2 files changed
+73 -41

No files matched your search

+27
View File
@@ -218,3 +218,30 @@ def test_unknown_gelu_mode_is_rejected():
def test_unknown_cache_layout_is_rejected():
with pytest.raises(ValueError, match="unknown cache_layout"):
_cfg(cache_layout="linked_list")
@pytest.mark.core_model
@pytest.mark.cpu
def test_auto_layout_picks_ring_for_sliding_and_roll_for_full():
"""Ring pays off only on sliding layers, and only there does it compile at
w4a16, so the default mixes them."""
cfg = _cfg() # default cache_layout="auto"
assert cfg.layout_for(SLIDING) == "ring"
assert cfg.layout_for(FULL) == "roll"
assert cfg.cache_len(SLIDING, 1024) == HF_CONFIG["sliding_window"] - 1
forced = _cfg(cache_layout="roll")
assert forced.layout_for(SLIDING) == forced.layout_for(FULL) == "roll"
@pytest.mark.core_model
@pytest.mark.cpu
def test_auto_layout_returns_one_entry_for_sliding_and_a_window_for_full():
cfg = _cfg()
step = SparkDecodeStep(cfg).eval()
with torch.no_grad():
out = step(*example_inputs(cfg, 1024))
caches = out[1:]
for i, layer_type in enumerate(cfg.layer_types):
# Both layouts hand back exactly one new entry here: ring by design,
# roll because a full layer only ever appends.
assert caches[2 * i].shape[2] == 1, layer_type