Commit Graph
1 Commits
Author SHA1 Message Date
FC.Li 5cbc2329a4 feat(cache): visualize recipe layouts in startup logs (#3571)
Cache startup logs show capacity and packing without exposing field
placement or unused bytes. Add an ASCII layout matrix after capacity
agreement and PP narrowing, with cache group names as columns and
resident planes as rows.

The report includes physical packing and usable block counts, field
offsets, shapes, and dtypes, stride padding, unused-plane holes, and
per-parent payload utilization. Each column describes an alternative
binding of the same parent. Serving builds log at INFO; memory probes
log at DEBUG. The design documentation explains byte accounting,
null-parent reservation, and PP and offload interpretation.

Validation:

- 44 CPU planner and visualization tests passed: `python -m pytest
--noconftest -q test/runtime/test_cache_layout_visualization.py
test/runtime/test_cache_memory_plan.py`.
- Coverage includes shared-stride alignment gaps, trailing padding,
unused planes, PP field and plane filtering, null-parent accounting, and
large capacities.
- `pre-commit run --all-files` passed.
- GPU serving was not exercised.

## Example startup logs

These examples use the cache field declarations with the same `pack()`
and `format_cache_layout()` code as the startup report. Capacity is
illustrative: **one usable parent plus one reserved null parent**, with
`pp_rank=0`. Actual serving capacity follows memory budgeting and
scheduler demand. Logger timestamps and rank prefixes are omitted.

### DeepSeek V4.1 Flash with DSpark

Configuration: native `v41` cache format and **three DSpark stages**.
The single `flatkv` plane is **3,225,600 B per parent**. The groups
reuse that plane with different packing, so their columns describe
alternative parent bindings and must not be added together.

<details>
<summary>Full V4.1 Flash + DSpark layout log</summary>

```text
CacheRecipe DeepseekV41Recipe (deepseek_v41): build=serving, pp_rank=0
prefix_granularity=256 tokens, usable_parents=1, reserved_null_parents=1
logical_parent=3,225,600 B, resident_parent=3,225,600 B, layout_arena=6,451,200 B (0.006 GiB)
Groups are alternative bindings of the same parent; do not sum columns.
Packing and block counts are rank-local physical counts.
Ranges [start, end) are bytes within one child stride, repeated packing times.
Padding is unused space in an occupied plane; a hole is an unused plane.
+--------------------+-----------------------------------+---------------------------------+-----------------------------------+------------------------------------------+
| plane              | v41.compressor_tail_r2            | v41.global_r1                   | v41.global_r2                     | v41.swa                                  |
|                    | packing=126                       | packing=140                     | packing=45                        | packing=2                                |
|                    | usable_blocks=126                 | usable_blocks=140               | usable_blocks=45                  | usable_blocks=2                          |
+--------------------+-----------------------------------+---------------------------------+-----------------------------------+------------------------------------------+
| flatkv             | stride=25,600 B                   | stride=23,040 B                 | stride=71,680 B                   | stride=1,612,800 B                       |
| parent=3,225,600 B | layer.14.compressor_tail          | layer.20.global_kv              | layer.14.global_kv                | layer.0.swa                              |
| arena_offset=0 B   |   [0, 8,192): 8,192 B             |   [0, 18,432): 18,432 B         |   [0, 18,432): 18,432 B           |   [0, 33,792): 33,792 B                  |
|                    |   float32 (2, 2, 512)             |   uint8 (64, 288)               |   uint8 (64, 288)                 |   uint8 (64, 528)                        |
|                    | layer.2.compressor_tail           | layer.20.index_k                | layer.14.index_k                  | layer.1.swa                              |
|                    |   [8,192, 16,384): 8,192 B        |   [18,432, 22,784): 4,352 B     |   [18,432, 22,784): 4,352 B       |   [33,792, 67,584): 33,792 B             |
|                    |   float32 (2, 2, 512)             |   uint8 (64, 68)                |   uint8 (64, 68)                  |   uint8 (64, 528)                        |
|                    | layer.8.compressor_tail           | padding [22,784, 23,040): 256 B | layer.2.global_kv                 | layer.10.swa                             |
|                    |   [16,384, 24,576): 8,192 B       | payload=3,189,760 B/parent      |   [22,784, 41,216): 18,432 B      |   [67,584, 101,376): 33,792 B            |
|                    |   float32 (2, 2, 512)             | padding=35,840 B/parent         |   uint8 (64, 288)                 |   uint8 (64, 528)                        |
|                    | padding [24,576, 25,600): 1,024 B |                                 | layer.2.index_k                   | layer.11.swa                             |
|                    | payload=3,096,576 B/parent        |                                 |   [41,216, 45,568): 4,352 B       |   [101,376, 135,168): 33,792 B           |
|                    | padding=129,024 B/parent          |                                 |   uint8 (64, 68)                  |   uint8 (64, 528)                        |
|                    |                                   |                                 | layer.8.global_kv                 | layer.12.swa                             |
|                    |                                   |                                 |   [45,568, 64,000): 18,432 B      |   [135,168, 168,960): 33,792 B           |
|                    |                                   |                                 |   uint8 (64, 288)                 |   uint8 (64, 528)                        |
|                    |                                   |                                 | layer.8.index_k                   | layer.13.swa                             |
|                    |                                   |                                 |   [64,000, 68,352): 4,352 B       |   [168,960, 202,752): 33,792 B           |
|                    |                                   |                                 |   uint8 (64, 68)                  |   uint8 (64, 528)                        |
|                    |                                   |                                 | padding [68,352, 71,680): 3,328 B | layer.14.swa                             |
|                    |                                   |                                 | payload=3,075,840 B/parent        |   [202,752, 236,544): 33,792 B           |
|                    |                                   |                                 | padding=149,760 B/parent          |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.15.swa                             |
|                    |                                   |                                 |                                   |   [236,544, 270,336): 33,792 B           |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.16.swa                             |
|                    |                                   |                                 |                                   |   [270,336, 304,128): 33,792 B           |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.17.swa                             |
|                    |                                   |                                 |                                   |   [304,128, 337,920): 33,792 B           |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.18.swa                             |
|                    |                                   |                                 |                                   |   [337,920, 371,712): 33,792 B           |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.19.swa                             |
|                    |                                   |                                 |                                   |   [371,712, 405,504): 33,792 B           |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.2.swa                              |
|                    |                                   |                                 |                                   |   [405,504, 439,296): 33,792 B           |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.20.swa                             |
|                    |                                   |                                 |                                   |   [439,296, 473,088): 33,792 B           |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.21.swa                             |
|                    |                                   |                                 |                                   |   [473,088, 506,880): 33,792 B           |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.22.swa                             |
|                    |                                   |                                 |                                   |   [506,880, 540,672): 33,792 B           |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.23.swa                             |
|                    |                                   |                                 |                                   |   [540,672, 574,464): 33,792 B           |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.24.swa                             |
|                    |                                   |                                 |                                   |   [574,464, 608,256): 33,792 B           |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.25.swa                             |
|                    |                                   |                                 |                                   |   [608,256, 642,048): 33,792 B           |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.26.swa                             |
|                    |                                   |                                 |                                   |   [642,048, 675,840): 33,792 B           |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.27.swa                             |
|                    |                                   |                                 |                                   |   [675,840, 709,632): 33,792 B           |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.28.swa                             |
|                    |                                   |                                 |                                   |   [709,632, 743,424): 33,792 B           |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.29.swa                             |
|                    |                                   |                                 |                                   |   [743,424, 777,216): 33,792 B           |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.3.swa                              |
|                    |                                   |                                 |                                   |   [777,216, 811,008): 33,792 B           |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.30.swa                             |
|                    |                                   |                                 |                                   |   [811,008, 844,800): 33,792 B           |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.31.swa                             |
|                    |                                   |                                 |                                   |   [844,800, 878,592): 33,792 B           |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.32.swa                             |
|                    |                                   |                                 |                                   |   [878,592, 912,384): 33,792 B           |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.33.swa                             |
|                    |                                   |                                 |                                   |   [912,384, 946,176): 33,792 B           |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.34.swa                             |
|                    |                                   |                                 |                                   |   [946,176, 979,968): 33,792 B           |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.35.swa                             |
|                    |                                   |                                 |                                   |   [979,968, 1,013,760): 33,792 B         |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.36.swa                             |
|                    |                                   |                                 |                                   |   [1,013,760, 1,047,552): 33,792 B       |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.37.swa                             |
|                    |                                   |                                 |                                   |   [1,047,552, 1,081,344): 33,792 B       |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.38.swa                             |
|                    |                                   |                                 |                                   |   [1,081,344, 1,115,136): 33,792 B       |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.39.dspark_kv0                      |
|                    |                                   |                                 |                                   |   [1,115,136, 1,180,672): 65,536 B       |
|                    |                                   |                                 |                                   |   bfloat16 (64, 512)                     |
|                    |                                   |                                 |                                   | layer.39.dspark_kv1                      |
|                    |                                   |                                 |                                   |   [1,180,672, 1,246,208): 65,536 B       |
|                    |                                   |                                 |                                   |   bfloat16 (64, 512)                     |
|                    |                                   |                                 |                                   | layer.39.dspark_kv2                      |
|                    |                                   |                                 |                                   |   [1,246,208, 1,311,744): 65,536 B       |
|                    |                                   |                                 |                                   |   bfloat16 (64, 512)                     |
|                    |                                   |                                 |                                   | layer.39.swa                             |
|                    |                                   |                                 |                                   |   [1,311,744, 1,345,536): 33,792 B       |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.4.swa                              |
|                    |                                   |                                 |                                   |   [1,345,536, 1,379,328): 33,792 B       |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.5.swa                              |
|                    |                                   |                                 |                                   |   [1,379,328, 1,413,120): 33,792 B       |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.6.swa                              |
|                    |                                   |                                 |                                   |   [1,413,120, 1,446,912): 33,792 B       |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.7.swa                              |
|                    |                                   |                                 |                                   |   [1,446,912, 1,480,704): 33,792 B       |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.8.swa                              |
|                    |                                   |                                 |                                   |   [1,480,704, 1,514,496): 33,792 B       |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | layer.9.swa                              |
|                    |                                   |                                 |                                   |   [1,514,496, 1,548,288): 33,792 B       |
|                    |                                   |                                 |                                   |   uint8 (64, 528)                        |
|                    |                                   |                                 |                                   | padding [1,548,288, 1,612,800): 64,512 B |
|                    |                                   |                                 |                                   | payload=3,096,576 B/parent               |
|                    |                                   |                                 |                                   | padding=129,024 B/parent                 |
+--------------------+-----------------------------------+---------------------------------+-----------------------------------+------------------------------------------+
| TOTAL / parent     | payload=3,096,576 B               | payload=3,189,760 B             | payload=3,075,840 B               | payload=3,096,576 B                      |
| 3,225,600 B        | padding=129,024 B                 | padding=35,840 B                | padding=149,760 B                 | padding=129,024 B                        |
|                    | hole=0 B                          | hole=0 B                        | hole=0 B                          | hole=0 B                                 |
|                    | unused=129,024 B                  | unused=35,840 B                 | unused=149,760 B                  | unused=129,024 B                         |
|                    | utilization=96.00%                | utilization=98.89%              | utilization=95.36%                | utilization=96.00%                       |
+--------------------+-----------------------------------+---------------------------------+-----------------------------------+------------------------------------------+
```

</details>

### Kimi K3 with DSpark

Configuration: **TP8, five MLA draft layers, FP8 KV for both target and
draft, and FP32 KDA recurrent state**. The parent has 24 target MLA
planes and five draft planes, totaling **25,657,344 B**. The reported
`uint8` dtype is the FP8 KV storage view.

Each KDA group has 23 layers and uses `slot.0` through `slot.22`.
`slot.23` and the five draft planes (`slot.24` through `slot.28`) are
unused by a KDA binding: **5,308,416 B of holes**, plus **1,625,088 B of
stride padding**, giving **72.98% payload utilization**.

The excerpt below preserves the original table formatting and includes
`slot.0`, `slot.23`, `slot.24`, `slot.28`, and the totals. Other plane
rows are omitted; totals cover all 29 planes.

<details>
<summary>K3 TP8 + DSpark layout log (selected planes and complete
totals)</summary>

```text
CacheRecipe KimiK3Recipe (kimi_k3): build=serving, pp_rank=0
prefix_granularity=128 tokens, usable_parents=1, reserved_null_parents=1
logical_parent=25,657,344 B, resident_parent=25,657,344 B, layout_arena=51,314,688 B (0.048 GiB)
Groups are alternative bindings of the same parent; do not sum columns.
Packing and block counts are rank-local physical counts.
Ranges [start, end) are bytes within one child stride, repeated packing times.
Padding is unused space in an occupied plane; a hole is an unused plane.
+---------------------------+--------------------------+--------------------------------------+--------------------------------------+--------------------------------------+
| plane                     | full_attention           | linear_attention_0                   | linear_attention_1                   | linear_attention_2                   |
|                           | packing=12               | packing=1                            | packing=1                            | packing=1                            |
|                           | usable_blocks=12         | usable_blocks=1                      | usable_blocks=1                      | usable_blocks=1                      |
+---------------------------+--------------------------+--------------------------------------+--------------------------------------+--------------------------------------+
| slot.0                    | stride=73,728 B          | stride=884,736 B                     | stride=884,736 B                     | stride=884,736 B                     |
| parent=884,736 B          | layer.3.latent_kv        | layer.0.conv_state                   | layer.30.conv_state                  | layer.61.conv_state                  |
| arena_offset=0 B          |   [0, 73,728): 73,728 B  |   [0, 27,648): 27,648 B              |   [0, 27,648): 27,648 B              |   [0, 27,648): 27,648 B              |
|                           |   uint8 (128, 1, 576)    |   bfloat16 (4608, 3)                 |   bfloat16 (4608, 3)                 |   bfloat16 (4608, 3)                 |
|                           | payload=884,736 B/parent | layer.0.recurrent_state              | layer.30.recurrent_state             | layer.61.recurrent_state             |
|                           | padding=0 B/parent       |   [27,648, 814,080): 786,432 B       |   [27,648, 814,080): 786,432 B       |   [27,648, 814,080): 786,432 B       |
|                           |                          |   float32 (12, 128, 128)             |   float32 (12, 128, 128)             |   float32 (12, 128, 128)             |
|                           |                          | padding [814,080, 884,736): 70,656 B | padding [814,080, 884,736): 70,656 B | padding [814,080, 884,736): 70,656 B |
|                           |                          | payload=814,080 B/parent             | payload=814,080 B/parent             | payload=814,080 B/parent             |
|                           |                          | padding=70,656 B/parent              | padding=70,656 B/parent              | padding=70,656 B/parent              |
+---------------------------+--------------------------+--------------------------------------+--------------------------------------+--------------------------------------+
| slot.23                   | stride=73,728 B          | hole=884,736 B/parent                | hole=884,736 B/parent                | hole=884,736 B/parent                |
| parent=884,736 B          | layer.92.latent_kv       |                                      |                                      |                                      |
| arena_offset=28,311,552 B |   [0, 73,728): 73,728 B  |                                      |                                      |                                      |
|                           |   uint8 (128, 1, 576)    |                                      |                                      |                                      |
|                           | payload=884,736 B/parent |                                      |                                      |                                      |
|                           | padding=0 B/parent       |                                      |                                      |                                      |
+---------------------------+--------------------------+--------------------------------------+--------------------------------------+--------------------------------------+
| slot.24                   | stride=73,728 B          | hole=884,736 B/parent                | hole=884,736 B/parent                | hole=884,736 B/parent                |
| parent=884,736 B          | layer.93.latent_kv       |                                      |                                      |                                      |
| arena_offset=30,081,024 B |   [0, 73,728): 73,728 B  |                                      |                                      |                                      |
|                           |   uint8 (128, 1, 576)    |                                      |                                      |                                      |
|                           | payload=884,736 B/parent |                                      |                                      |                                      |
|                           | padding=0 B/parent       |                                      |                                      |                                      |
+---------------------------+--------------------------+--------------------------------------+--------------------------------------+--------------------------------------+
| slot.28                   | stride=73,728 B          | hole=884,736 B/parent                | hole=884,736 B/parent                | hole=884,736 B/parent                |
| parent=884,736 B          | layer.97.latent_kv       |                                      |                                      |                                      |
| arena_offset=37,158,912 B |   [0, 73,728): 73,728 B  |                                      |                                      |                                      |
|                           |   uint8 (128, 1, 576)    |                                      |                                      |                                      |
|                           | payload=884,736 B/parent |                                      |                                      |                                      |
|                           | padding=0 B/parent       |                                      |                                      |                                      |
+---------------------------+--------------------------+--------------------------------------+--------------------------------------+--------------------------------------+
| TOTAL / parent            | payload=25,657,344 B     | payload=18,723,840 B                 | payload=18,723,840 B                 | payload=18,723,840 B                 |
| 25,657,344 B              | padding=0 B              | padding=1,625,088 B                  | padding=1,625,088 B                  | padding=1,625,088 B                  |
|                           | hole=0 B                 | hole=5,308,416 B                     | hole=5,308,416 B                     | hole=5,308,416 B                     |
|                           | unused=0 B               | unused=6,933,504 B                   | unused=6,933,504 B                   | unused=6,933,504 B                   |
|                           | utilization=100.00%      | utilization=72.98%                   | utilization=72.98%                   | utilization=72.98%                   |
+---------------------------+--------------------------+--------------------------------------+--------------------------------------+--------------------------------------+
```

</details>

Signed-off-by: FC-Li <robert_lexis@163.com>
2026-11-18 14:41:55 +08:00