|
| 1 | +--- |
| 2 | +title: "Day 25: the fused read dispatches to the kernel backend" |
| 3 | +parent: Daily log |
| 4 | +nav_order: 25 |
| 5 | +--- |
| 6 | + |
| 7 | +# Day 25: the fused read dispatches to the kernel backend |
| 8 | + |
| 9 | +Date: 2026-07-16 · Week 6 · Phase 2 Paged memory |
| 10 | + |
| 11 | +## What I added today |
| 12 | +One line moved in `src/nanoserve/cache.py`, and it is the line the whole of Week 6 |
| 13 | +was building toward. `PagedKVCache.paged_attention`, the cache's fused read, called |
| 14 | +`paged_attention_reference` directly through Day 24: the byte-exact torch oracle, |
| 15 | +the same SDPA over the assembled slots the tests compare against. Day 25 routes it |
| 16 | +through `paged_attention_dispatch`, the Day-23 entry point that asks |
| 17 | +`select_backend(q.device)` which read this device can actually run. On a CUDA tensor |
| 18 | +with Triton installed that is the `@triton.jit` kernel; on a CPU it is the Day-22 |
| 19 | +tlsim model, the same streaming loop written in torch. The engine gets the kernel on |
| 20 | +a card and the correct-and-slow model on a laptop, and the cache never learns which. |
| 21 | + |
| 22 | +Two new tests in `tests/test_cache.py` pin the wiring. One spies on the dispatcher |
| 23 | +symbol the cache imports and asserts the read calls it exactly once, handed this |
| 24 | +layer's own K/V pools and the slot mapping for the whole history, then hands its |
| 25 | +result straight back. The other feeds the cache's pool through both the dispatched |
| 26 | +read and `paged_attention_reference` and pins them to `atol=1e-5`, the same tolerance |
| 27 | +the kernel itself is held to. The consequence rippled into three older equality tests |
| 28 | +that had pinned the cache read `torch.equal` against a contiguous SDPA: they now carry |
| 29 | +the flash tolerance, because the model no longer runs the byte-exact reference. Suite |
| 30 | +**186 green** (5 GPU-gated skips), ruff clean. |
| 31 | + |
| 32 | +## Why it matters |
| 33 | +Until today Week 6 was a kernel in a file. Day 22 wrote the fused CPU model, Day 23 |
| 34 | +wrote the real Triton kernel, Day 24 built the instrument to read its scaling, and |
| 35 | +every one of them was reachable only from a test. The model's actual attention still |
| 36 | +ran `paged_attention_reference`, the oracle. This is the wiring step that turns the |
| 37 | +kernel into the engine's real attention: the paged forward now dispatches, so the |
| 38 | +same code path picks up the GPU kernel when a card is present and falls back to the |
| 39 | +model that runs anywhere when it is not. The reference did not disappear, it changed |
| 40 | +jobs. It is the oracle both backends are graded against, not the path either one runs. |
| 41 | + |
| 42 | +The honest cost is that paged attention is no longer bit-identical to the naive |
| 43 | +contiguous path. Both backends stream the online softmax, which reassociates the |
| 44 | +exponent sums, so the output lands a few ulps off a plain SDPA (about 7e-6 on the |
| 45 | +two-layer test model) rather than exactly on it. That is not a regression, it is the |
| 46 | +accuracy trade every flash-attention kernel makes, and it belongs on the model's path |
| 47 | +precisely because that is where the kernel now lives. The token-level tests still hold |
| 48 | +because argmax over these logits does not flip at 1e-5; the logit-level test that |
| 49 | +asked for 1e-6 now asks for 1e-5, the tolerance the streaming softmax actually |
| 50 | +delivers. |
| 51 | + |
| 52 | +## What I learned |
| 53 | +1. **Byte-identity was a property of the oracle, not of paging.** Every "paging |
| 54 | + changes nothing" test through Day 24 passed `torch.equal` because the cache read |
| 55 | + *was* the reference, running the identical causal fp32 softmax by exact index. The |
| 56 | + moment the model runs the streaming kernel instead, the scatter and gather stay |
| 57 | + exact (same K/V, same slots) but the softmax summation order moves, and equality |
| 58 | + becomes closeness. The tests that flipped from `equal` to `allclose` were not |
| 59 | + loosened to hide a bug; they caught exactly the numerics change the dispatch |
| 60 | + introduced, which is what a good test does. |
| 61 | +2. **The right tolerance is the one the backend is already held to.** The kernel |
| 62 | + tests pin tlsim and Triton to `paged_attention_reference` at `atol=1e-5, rtol=1e-4`. |
| 63 | + When the cache read started running that same backend, the correct new tolerance |
| 64 | + for the cache tests was not a number I picked to make green, it was that same |
| 65 | + 1e-5, propagated up one level. A tolerance that means something is a tolerance |
| 66 | + borrowed from where the approximation is defined. |
| 67 | +3. **A dispatcher makes the fallback the default and the kernel the exception.** The |
| 68 | + branch reads `"triton" if device.type == "cuda" and has_triton() else "tlsim"`, so |
| 69 | + the CPU model is what runs unless both a card and the package are present. That is |
| 70 | + the right default for a repo that must run on a laptop: the slow-and-correct path |
| 71 | + is the one you get for free, and the fast path is the one that has to prove its |
| 72 | + preconditions. The engine is portable first and fast where it can be, not the |
| 73 | + other way around. |
| 74 | + |
| 75 | +## Diagram |
| 76 | +[paged-read-dispatch.png](../diagrams/paged-read-dispatch.png). The cache read at the |
| 77 | +top, the `select_backend` pill below it, and the two branches: the Triton kernel on |
| 78 | +`cuda & has_triton()`, the tlsim model on `else`. The reference sits dashed in the |
| 79 | +middle, with both backends held to it at `atol=1e-5`, no longer on the path. Both |
| 80 | +converge to the one attention output that flows back to `gqa_attention`. The banner |
| 81 | +carries the trade: Day 24 ran the byte-exact reference, Day 25 runs the streaming |
| 82 | +softmax, so paged is flash-close to naive, not bit-identical. |
| 83 | + |
| 84 | +## Tomorrow |
| 85 | +Measure the dispatch where it matters. The read benchmark and the scaling fit |
| 86 | +(Days 20 and 24) time `gather` and `fused` as standalone functions; wire the runner |
| 87 | +to time the cache's *dispatched* read end to end, so the sweep reports the path the |
| 88 | +engine actually takes rather than a function called in isolation. On CPU that pins the |
| 89 | +tlsim constant against the gather constant under the real slot mapping; on a card it is |
| 90 | +the first number that shows the kernel bending the constant down, which is the whole |
| 91 | +claim Week 6 has been setting up to make. Keep the reference as the correctness gate |
| 92 | +the timed path is checked against before any number is trusted. |
| 93 | + |
| 94 | +## Post angle |
| 95 | +Day 25 of building an LLM inference engine from scratch. Today was one moved line and |
| 96 | +it is the one the whole kernel week was for. My paged cache's attention read was still |
| 97 | +calling the byte-exact torch reference, the oracle. The real Triton kernel and its CPU |
| 98 | +model existed but only the tests ever touched them. Today the cache read dispatches: |
| 99 | +`select_backend(q.device)` picks the Triton kernel on a card and the streaming CPU |
| 100 | +model on a laptop, and the model's actual attention runs whichever the box can run. |
| 101 | +The reference did not go away, it changed jobs, from the path the model runs to the |
| 102 | +oracle both backends are graded against. The honest part: paged attention is no longer |
| 103 | +bit-identical to the naive path. Both backends stream the online softmax, which |
| 104 | +reassociates the exponent sums, so the output lands about 7e-6 off a plain SDPA rather |
| 105 | +than exactly on it. That is not a bug, it is the accuracy trade every flash-attention |
| 106 | +kernel makes, and three of my "paging changes nothing" tests flipped from `torch.equal` |
| 107 | +to `allclose` to carry it. The tolerance I gave them was not invented, it is the same |
| 108 | +1e-5 the kernel is already held to against the reference, propagated up one level. This |
| 109 | +is the shape vLLM and SGLang ship: a dispatcher that runs the fast kernel where the |
| 110 | +hardware allows and a correct fallback everywhere else, with the reference kept as the |
| 111 | +gate, not the path. 186 green. |
0 commit comments