Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export OPENBUENOS_DIVE_INTERP_PATH=/data/local/tmp/tpu_open_stack/dive_interp_rv
- [`libopenbuenos` Guide](docs/libopenbuenos/README.md) & [`ggml-edgetpu` Guide](docs/llama-cpp-edgetpu/README.md)
- [Headless Colab TPU Tooling](docs/cloud-tpu/colab-headless-tooling.md) — Track B `v6e-1`/`v5e-1` headless harness (`tools/colab_tpu_bench.py`), Colab MCP/`v1beta` contract & Track C boundaries
- [Cloud TPU Ops: Container CDI & Prometheus Telemetry](docs/cloud-tpu/ops-container-telemetry.md) — Zero-dep `tpu-exporter` (`:9400/metrics`) & `freetpu-cdi` CNCF CDI `v0.6.0` (`freetpu.dev/tpu=all`) Docker/K8s manifests
- [Multi-Generation Serving & SparseCore Planning](docs/cloud-tpu/multigen-serving-sparsecore.md) — Host-side `v5e` / `v5p` / `v6e` / `tpu7x` capability profiles, roofline offload, paged-KV and MoE reference, codebook dequant, and prefix-aware request routing
- [FreeTPU Router v0 Guide](docs/router/README.md) — OpenAI-compatible router across Pixel Tensor G5 clusters, local hosts, and Cloud TPU slices

---
Expand Down
101 changes: 101 additions & 0 deletions docs/cloud-tpu/multigen-serving-sparsecore.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Multi-Generation Cloud TPU Serving & SparseCore Planning

Host-only planner in [`freetile/include/freetile/serving_planner.hpp`](../../freetile/include/freetile/serving_planner.hpp). It describes `v5e`, `v5p`, `v6e` (Trillium), and `tpu7x` (Ironwood) chips, estimates MXU / VPU / HBM roofline knees, plans SparseCore ragged paged-KV gather and MoE scatter/gather with a CPU reference, dequantizes four 4-bit codebooks, and routes prefix-aware prefill and decode across multi-chip slices. The reference runs on the host. It does not open a device, a USB port, or a cloud API.

```bash
make -C freetile test
cmake -S freetile -B freetile/build -DCMAKE_BUILD_TYPE=Release
cmake --build freetile/build
ctest --test-dir freetile/build --output-on-failure -R freetile_serving_planner_tests
```

`ChipTarget` in `tile_ir.hpp` stays `V5E`, `V6E`, `TPU7X`, and `BUENOS_A0`. `v5p` is a serving-planner generation, not a new tile-IR chip target.

## Capability profiles

Peaks below are chip peaks. HBM bandwidth is decimal GBps (`bytes/s = gbps * 1e9`). VMEM is the FreeTile planning budget and matches `target_profile` for `V5E` (16 MiB), `V6E` (32 MiB), and `TPU7X` (64 MiB per chiplet). It is not the libopentpu HAL VMEM field. `smem_bytes` is a 1 MiB planner constant on every generation.

| | `v5e` | `v5p` | `v6e` Trillium | `tpu7x` Ironwood |
| --- | ---: | ---: | ---: | ---: |
| TensorCores / chip | 1 | 2 | 1 | 2 |
| MXU shape | 128x128 | 128x128 | 256x256 | 256x256 |
| MXUs / TensorCore | 4 | 4 | 2 | 2 |
| MXUs / chip | 4 | 8 | 2 | 4 |
| SparseCores / chip | 0 | 4 | 2 | 4 |
| Chiplets / chip | 1 | 1 | 1 | 2 |
| BF16 TFLOP/s | 197 | 459 | 918 | 2307 |
| INT8 TOPS | 393 published | 918 modeled (`2 * BF16`) | 1836 published | 4614 modeled (equal to FP8) |
| FP8 TFLOP/s | none | 459 (equal to BF16) | 918 (equal to BF16) | 4614 (2x BF16) |
| HBM / chip | 16 GiB | 95 GiB | 32 GiB | 192 GiB |
| HBM bandwidth | 819 GBps | 2765 GBps | 1638 GBps | 7380 GBps |
| VMEM / chip | 16 MiB | 32 MiB | 32 MiB | 128 MiB |
| ICI | 2D torus, 400 GBps, 4 ports | 3D torus, 1200 GBps, port count unpublished | 2D torus, 800 GBps, 4 ports | 3D torus, 1200 GBps, port count unpublished |
| `has_sparsecore` | false | true | true | true |
| Native INT8 / BF16 | yes / yes | yes / yes | yes / yes | yes / yes |
| `supports_fp8` | false | true | true | true |
| `supports_native_fp8` | false | false | false | true |
| `supports_native_s4` | true | true | true | false |

`supports_native_fp8` is true only when the published FP8 peak is strictly greater than BF16 (`tpu7x`). `v5p` and `v6e` list an FP8 rate equal to BF16, so the format is recognized and the 2x-FP8 MXU path is not. Stock profiles set `supports_fp4`, `supports_mxfp4`, and `supports_native_fp4` to false: FP4 is not a published MXU peak format. `tpu7x` die-to-die bandwidth is `d2d_link_multiplier = 6` times one 1D ICI link. Each `tpu7x` chiplet is one TensorCore, two SparseCores, 96 GiB HBM, and 64 MiB VMEM.

`parse_generation` accepts case-insensitive names such as `ct5lp-hightpu-1t`, `ct5p-hightpu-4t`, `TPU v6e Trillium`, and `Ironwood tpu7x-4t`. A bare `v5` is rejected. `tpu7x` / `ironwood` win over a substring that also contains `v6` or `v5`.

## Roofline and offload

One MXU cycle is `2 * mxu_m * mxu_n` BF16 FLOPs, so

```text
clock_hz = bf16_flops / (mxus_per_chip * 2 * mxu_m * mxu_n)
```

The planner models one VPU per TensorCore, 128 lanes by 8 sublanes, and one BF16 FMA (2 FLOPs) per lane-sublane per cycle. Knees are peak flops divided by HBM bytes/s. On `v6e` the MXU/HBM knee is `918e12 / 1638e9`, about 560.44 FLOPs/byte. A square BF16 matmul has arithmetic intensity `n / 3`. `n = 1024` is HBM-bound on `v6e` and compute-bound on `v5e`. `n = 4096` is compute-bound on `v6e`.

Dispatch:

- Dense matmul stays on the MXU. `FORCE_VPU` does not move it. `FORCE_SPARSECORE` returns `dense matmul cannot be forced onto SparseCore`.
- Gather and scatter in `AUTO`: a chip with no SparseCore uses the VPU. A working set that fits in chip VMEM stays on the VPU and is marked resident. A larger working set whose intensity is below the VPU/HBM knee goes to SparseCore. Otherwise it stays on the VPU and is compute-bound.
- `FORCE_SPARSECORE` selects SparseCore on `v5p`, `v6e`, and `tpu7x` even when the set fits in VMEM. On `v5e` it fails with `v5e has no SparseCore; refusing forced SparseCore dispatch`. `AUTO` on `v5e` is a successful VPU plan with the same numeric reference.

## Paged KV and MoE

`plan_paged_kv` packs variable-length sequences into fixed pages. Each sequence starts a fresh page. Unused block-table slots are `0xffffffff`. `paged_kv_pack`, `paged_kv_gather`, and `paged_kv_scatter` are inverses of a concatenated sequence-major float store. Partial-page tails are zero. The same plan runs on every generation; only the offload unit changes. Forced SparseCore dispatch on `v5e` is rejected before any page is written.

`plan_moe_route` reads row-major finite scores `[token][expert]`. Top-k is unique, and ties keep the lower expert id. Capacity is `ceil(capacity_factor * tokens * top_k / experts)`. Tokens are assigned in token order, then rank order; overflow drops the later pair. A repeated `(token, expert)` shares one compact slot and does not consume a second capacity entry. `moe_apply_reference` accumulates `activation * (expert_id + 1)` in rank order so a direct loop can check bit-exact parity. `v5e` `AUTO` uses the VPU path and the same compact layout. Forced SparseCore routing on `v5e` fails with the same SparseCore error.

## Codebook dequant

`codebook_descriptor` is one 16-entry f32 table plus a group size. Dequant is one multiply, `out[i] = center[nibble] * scale`, so the LUT, the select tree, and the E2M1 decoder match at 0.0 ULP, including a negative zero times `1.0`. Even indexes occupy the low nibble.

| Format | Group | Centers |
| --- | ---: | --- |
| `IQ4_XS` | 32 | int8 lattice `-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113` stored as exact f32. One scale per group. |
| `NF4` | 64 | exact f32 bits `bf800000 bf3239b1 bf066b30 beca32a0 be91a24d be3d353f bdba7871 00000000 3da2faff 3e24cae3 3e7c04dd 3ead033a 3ee1a4b8 3f1007ab 3f3913b3 3f800000` |
| `BOF4` | 32 | block-offset 4-bit lattice defined here: center = nibble - 8, so offsets `-8 .. 7`. This name is the planner's offset-8 codebook, not a separately published wire format. |
| `FP4` / `MXFP4` | 32 | OCP E2M1. Nibbles `0 .. 7` are `0, 0.5, 1, 1.5, 2, 3, 4, 6` and `8 .. 15` are the signed values, including `-0`. |

`mxfp4_e8m0_scale` is `2^(exponent - 127)`. Exponent `255` is NaN and fails. The select tree is a complete 4-bit trie: 31 nodes, depth-first preorder, MSB (bit 3) first, left child when the bit is clear. `codebook_select_tree_index` returns the nibble.

Lowering is a report, not a different numeric result:

- `FP4` / `MXFP4` with `supports_native_fp4` set on the profile reports `native_fp4`.
- Otherwise `mxu_m >= 256` (`v6e`, `tpu7x`) reports `vperm`.
- Otherwise (`v5e`, `v5p`) reports `vreg_select_tree`.

Stock profiles never report native FP4. Setting `supports_native_fp4` on a copied `tpu7x` profile reports `native_fp4` for `FP4` / `MXFP4` only. `supports_native_s4` does not change codebook lowering.

## Prefix-aware request router

Slice presets are geometry, not a locked chip generation:

| Preset | Devices | Layout |
| --- | ---: | --- |
| `1x1` | 1 | one package |
| `2x2` | 4 | `dim_x = 2`, `dim_y = 2` |
| `2x4` | 8 | `dim_x = 2`, `dim_y = 4` |
| `tpu7x-dual-chiplet` | 2 | one package, two chiplets |

`device = fnv1a64(prefix) % num_devices`. An empty prefix hashes the little-endian request id. `package = device / chiplets`, `chiplet = device % chiplets`, `x = package % dim_x`, `y = package / dim_x`. `d2d_buddy` exists only when `chiplets == 2`: the other chiplet on the same package.

Length buckets are `<=128`, `<=512`, `<=2048`, `<=8192`, then the rest. Decode is `generated_tokens > 0`. Prefill is `generated_tokens == 0` with `prompt_tokens > 0`. Both zero is invalid. Decode length is the saturating sum of prompt and generated tokens. The scheduler orders decode before prefill, then bucket, length, request id, and original index. A request that does not fit the remaining budget is skipped.

Speculative draft/target verification uses `gamma` and the step budget. A decode request charges `1 + verified` tokens, where `verified = min(draft_tokens, gamma, remaining step budget)`. Accepted drafts are `min(draft_accepted, verified)`. The rest of the verified drafts are rejected, and `draft_tokens - verified` stay pending. Prefill charges `prompt_tokens` and verifies nothing. `draft_accepted > draft_tokens` is invalid.
15 changes: 15 additions & 0 deletions freetile/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_library(freetile STATIC
src/buenos_splines.cpp
src/flash_attn_planner.cpp
src/malibu_profile.cpp
src/serving_planner.cpp
)
target_include_directories(freetile PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_compile_options(freetile PRIVATE -Wall -Wextra -Wpedantic -Werror)
Expand All @@ -32,6 +33,20 @@ target_compile_options(test_splines_and_flash_attn PRIVATE
-Wall -Wextra -Wpedantic -Werror -fno-exceptions -fno-rtti)
add_test(NAME freetile_splines_flash_attn_tests COMMAND test_splines_and_flash_attn)

add_executable(test_serving_planner
tests/test_serving_planner.cpp
)
target_link_libraries(test_serving_planner PRIVATE freetile)
target_compile_options(test_serving_planner PRIVATE -Wall -Wextra -Wpedantic -Werror)
add_test(NAME freetile_serving_planner_tests COMMAND test_serving_planner)

add_executable(test_serving_planner_fuzz
tests/test_serving_planner_fuzz.cpp
)
target_link_libraries(test_serving_planner_fuzz PRIVATE freetile)
target_compile_options(test_serving_planner_fuzz PRIVATE -Wall -Wextra -Wpedantic -Werror)
add_test(NAME freetile_serving_planner_fuzz COMMAND test_serving_planner_fuzz)

option(FREETILE_BUILD_BUENOS_PARITY_TEST "Build bit-exact parity test against sibling libopenbuenos" ON)
option(FREETILE_BUILD_DIGITAL_TWIN "Build freetile-twin digital twin library, CLI, and tests" ON)

Expand Down
22 changes: 18 additions & 4 deletions freetile/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ CORE_SRCS := \
$(THIS_DIR)/src/buenos_emitter.cpp \
$(THIS_DIR)/src/buenos_splines.cpp \
$(THIS_DIR)/src/flash_attn_planner.cpp \
$(THIS_DIR)/src/malibu_profile.cpp
$(THIS_DIR)/src/malibu_profile.cpp \
$(THIS_DIR)/src/serving_planner.cpp

CORE_HDRS := \
$(THIS_DIR)/include/freetile/tile_ir.hpp \
$(THIS_DIR)/include/freetile/mosaic_emitter.hpp \
$(THIS_DIR)/include/freetile/buenos_emitter.hpp \
$(THIS_DIR)/include/freetile/buenos_splines.hpp \
$(THIS_DIR)/include/freetile/flash_attn_planner.hpp \
$(THIS_DIR)/include/freetile/malibu_profile.hpp
$(THIS_DIR)/include/freetile/malibu_profile.hpp \
$(THIS_DIR)/include/freetile/serving_planner.hpp

TWIN_HDR := $(THIS_DIR)/include/freetile/twin.h
TWIN_SRC := $(THIS_DIR)/src/twin.cpp
Expand All @@ -53,13 +55,15 @@ TEST_FREETILE := $(BUILD_DIR)/test_freetile
TEST_SPLINES := $(BUILD_DIR)/test_splines_and_flash_attn
TEST_PARITY := $(BUILD_DIR)/test_buenos_parity
TEST_TWIN := $(BUILD_DIR)/test_freetile_twin
TEST_SERVING := $(BUILD_DIR)/test_serving_planner
TEST_FUZZ := $(BUILD_DIR)/test_serving_planner_fuzz

.PHONY: all host twin test verify-header clean openbuenos-host

all: host

host: $(FREETILE_LIB) $(FREETILE_TWIN_LIB) $(TWIN_CLI) $(ROOT_TWIN_CLI) \
$(TEST_FREETILE) $(TEST_SPLINES) $(TEST_PARITY) $(TEST_TWIN)
$(TEST_FREETILE) $(TEST_SPLINES) $(TEST_PARITY) $(TEST_TWIN) $(TEST_SERVING) $(TEST_FUZZ)

twin: $(TWIN_CLI) $(ROOT_TWIN_CLI)

Expand Down Expand Up @@ -97,6 +101,14 @@ $(TEST_FREETILE): $(THIS_DIR)/tests/test_freetile.cpp $(FREETILE_LIB) $(CORE_HDR
@mkdir -p $(dir $@)
$(HOST_CXX) $(CXXFLAGS_BASE) $< $(FREETILE_LIB) -o $@ $(LDFLAGS)

$(TEST_SERVING): $(THIS_DIR)/tests/test_serving_planner.cpp $(FREETILE_LIB) $(CORE_HDRS)
@mkdir -p $(dir $@)
$(HOST_CXX) $(CXXFLAGS_BASE) $< $(FREETILE_LIB) -o $@ $(LDFLAGS)

$(TEST_FUZZ): $(THIS_DIR)/tests/test_serving_planner_fuzz.cpp $(FREETILE_LIB) $(CORE_HDRS)
@mkdir -p $(dir $@)
$(HOST_CXX) $(CXXFLAGS_BASE) $< $(FREETILE_LIB) -o $@ $(LDFLAGS)

$(TEST_SPLINES): $(THIS_DIR)/tests/test_splines_and_flash_attn.cpp $(FREETILE_LIB) $(CORE_HDRS)
@mkdir -p $(dir $@)
$(HOST_CXX) $(CXXFLAGS_NOEXC) $< $(FREETILE_LIB) -o $@ $(LDFLAGS)
Expand All @@ -113,11 +125,13 @@ verify-header: $(TWIN_HDR)
$(HOST_CC) -x c -std=c11 -fsyntax-only -Wall -Wextra -Wpedantic -Werror $<
$(HOST_CXX) -x c++ -std=c++17 -fsyntax-only -Wall -Wextra -Wpedantic -Werror $<

test: verify-header $(TEST_FREETILE) $(TEST_SPLINES) $(TEST_PARITY) $(TEST_TWIN) $(ROOT_TWIN_CLI)
test: verify-header $(TEST_FREETILE) $(TEST_SPLINES) $(TEST_PARITY) $(TEST_TWIN) $(TEST_SERVING) $(TEST_FUZZ) $(ROOT_TWIN_CLI)
$(TEST_FREETILE)
$(TEST_SPLINES)
$(TEST_PARITY)
$(TEST_TWIN)
$(TEST_SERVING)
$(TEST_FUZZ)

clean:
rm -rf $(THIS_DIR)/build $(ROOT_TWIN_CLI)
1 change: 1 addition & 0 deletions freetile/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- Emits **11-chunk zero-DRAM in-SRAM chained SwiGLU** (`try_emit_buenos_swiglu` / `emit_buenos_swiglu`: `gate -> 0x640 SiLU * up [0x100 MUL] -> down`) with 100% bit-exact parity against `libopenbuenos` across all 6 silicon-proven shapes (`M=1`: `(576,512,576)`, `(576,1536,576)`, `(1536,512,1536)`, `(1536,1024,1536)`; `M=64`: `(576,512,576)`, `(1536,512,1536)`), plus an exact IEEE-754 RNE Horner simulator (`buenos_simulate_swiglu_exact`).
- Extends 7-chunk per-tensor `W8A16(FP16)` (`[N/32, K/2, 32, 2]` `w^0x80` layout, `FP32` accumulator, zero intermediate `FP16` overflow hazard) to `K=576`/`N=576` (`SmolLM2-135M`, `M=1`) and batched `M=64` prefill (`W8A16_PT_64X`, `K,N in {512,576,1024,1536}` within the `768 KiB` Core-0 narrow SRAM ceiling), tagged `silicon_verified = false` until live `rango` verification; includes `buenos_should_prefer_w8a16(...)` encoding the selective mixed-precision policy (`lm_head`, `down_proj`, Layer 0–1, `Q6_K`, outlier crest guard, `>32768` FP16 prescale advisory).
- Pure C++17 standard library only (zero dependency on `libopenbuenos`, LLVM, MLIR, or Python; `-fno-exceptions -fno-rtti` safe). Optional `test_buenos_parity` verifies bit-exact byte-for-byte parity against `libopenbuenos` when the sibling directory is present.
- **Multi-generation serving planner (`include/freetile/serving_planner.hpp`):** host-only `v5e` / `v5p` / `v6e` / `tpu7x` capability profiles, MXU/VPU/HBM roofline offload, SparseCore-gated paged-KV and MoE reference, 4-bit codebook dequant, and prefix-aware request routing. No device, USB, or cloud access. Covered by `make -C freetile test` and `ctest` name `freetile_serving_planner_tests`. See [multi-generation serving](../docs/cloud-tpu/multigen-serving-sparsecore.md).

## Build & Test

Expand Down
Loading
Loading