This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/docs/IMPLEMENTATION.md
T
Deadcode 5a9ce4912b radiance 1.12.0
New models:
- Fish Audio drama-3: speech with speaker tags and inline direction, voices cloned from reference
  clips (the `references` field of /v1/audio/speech and the streaming session), two cards
- Gemma 4 E2B/E4B (KV-shared layers, per-layer embeddings, clipped vision linears), with
  NVIDIA-Nemotron-3.5-Bolt-E4B in bf16 with DFlash and vision on one card, and E4B recipes

Speed:
- MXFP4 GEMMs for one to 64 rows and a tiled prefill GEMM at TN=4 from 1024 rows: the 27B MXFP4
  with DFlash2 decodes about 1% faster at c1, 5% at c4 and 14% at c16
- The fp8 and int8 LM heads read their weight once for 65 to 128 rows
- A prefill step of one sequence runs in two pieces on a second lane at two ranks (qwen35_fp8,
  gemma3): a 45K-token prompt on the 27B fp8 in 15.1 s instead of 16.2 s
- Gemma 3's fused all-reduce junctions also under a lossy wire
- --draft-short-from N: a block drafter runs its shorter block from N decoding sequences on
  (off by default; with 8 on the 27B MXFP4: c8 +12%, c16 +18%, c64 +32%, c1 unchanged)
- --tp-attn-split rows: GLM-5.3-Flash's MLA attention split by rows over the ranks, 18 to 21%
  faster prefill; lossy, `heads` stays the default
- --draft-coupling on: Qwen3.8-Flash-Next's MTP drafter samples its proposals from its reranked
  top-8 and the target's draw is coupled to it; pays at high temperatures (c1 at T 1.0: 155 ->
  178 tok/s), costs at low ones; off by default

Features:
- Edited prompts on windowed models (Gemma 4) reuse their prefix: periodic window snapshots, and
  snapshot-backed prefix KV kept across recomputes (an edited tail on Gemma 4 31B: 96 s -> 2.6 s)
- DELETE /sessions/<label> and DELETE /sessions?id=<id> forget one stored conversation
- --state-cache-dtype f16: the delta-net state cache at half the memory (qwen35 plugins; lossy)

Fixes:
- A browser's CORS preflight allows DELETE

Changes: Plugin ABI 27: out-of-tree plugins must be rebuilt. The environment switches the PRs
added are flags now (--tp-attn-split, --draft-coupling, --state-cache-dtype).
2026-10-24 13:12:28 +02:00

125 lines
7.9 KiB
Markdown

# Radiance: implementation brief
`spec.md` is the design and it is binding; read it first. This file covers the mechanical part:
where things live, how they build, and the conventions every component follows.
## The host backend is a primary target
Everything except the device kernels builds, tests and runs through the host device backend:
`cmake` with no ROCm must configure, build, test and run, and `RAD_HIP_FOUND` gates only the HIP
backend and the device kernels. A machine with no card is a complete development environment for
the core, the tools, the architecture plugins and `libref`, and the suite runs in full there.
## Layout
```
abi/ the ABI, and all a plugin compiles against. Frozen: change it deliberately
core/rad_internal.h shared internals: logging, Geometry, constraint matching
core/util/ logging, error strings, dtype arithmetic, tensor helpers
core/device/ device backends: host, and aql (HSA queues the engine writes; HIP for memory)
core/plugin/ plugin loader, kernel registry, op schemas, selector, bucket tables
core/build/ the declare phase: builder, weight manifest, buffer plan, graph dump
core/runtime/ the run phase: RadCtx, rad_issue, the arena
core/mem/ pools, slab, KV block manager, prefix cache
core/sched/ scheduler, RadBatch construction, request lifecycle
core/place/ placement planner, the mover, the heat engine
core/sample/ device sampler dispatch, the host reference chain, grammar bitmask
core/text/ tokeniser, chat template, tool parsing, detokenisation
core/mm/ image and video decoding, and the processor that turns frames into patches
core/gen/ image generation: the request and the pass driver
core/format/ the .rad reader and writer, safetensors, recipes, the imatrix, the tune cache
core/server/ HTTP: the OpenAI, Anthropic and llama-server endpoints, /metrics, the dashboard
core/diag/ the startup report
core/kld.* the KL mode: --kld-record / --kld-ref
core/engine* the engine that ties them together; core/main.cpp is the `radiance` binary
libref/ every op, naively and correctly, host domain: the oracle and last resort
libr4d/ the RDNA4 (gfx12xx) kernel library: kernels, rows, layouts, fusion claims
libavx/ the same vocabulary in x86 SIMD, host domain: the production host kernels,
built on every x86-64 build because the engine refuses to serve an op that
resolves only to libref (without --debug-accept-reference-kernels)
libquant/ the built-in quantisers rad-convert's recipes name: grids, transforms, rtn, gptq
arch/<id>/ one architecture plugin per directory
tools/ rad-info, rad-kbench, rad-convert, rad-tune, rad-schemas, rad-tokcheck,
rad-chatparse, rad-gbnf-check
cmake/ rad_add_plugin, and the package config an out-of-tree plugin find_package()s
scripts/ harnesses for serving, tracing and profiling a running engine
data/ kernels.rkb and kernels_moe.rkb: recorded references rad-kbench replays with
no model. Two, because no one container declares the whole vocabulary.
recipes/: the quantisation recipes of the containers this tree serves
docs/KERNELS.md what rad-kbench last measured, generated and checked in
vendor/ llama.cpp lifts tracked to an upstream commit (vendor/UPSTREAM), changed only
through vendor/patches/; nlohmann/json and cpp-httplib
tests/ ctest
```
Each directory has its own `CMakeLists.txt` and builds only what is inside it. A new top-level
directory adds its `add_subdirectory` line to the root `CMakeLists.txt`; `arch/CMakeLists.txt`
adds every architecture directory itself.
## Build
```sh
cmake -S . -B build -G Ninja # host backend; no ROCm required
cmake --build build -j
ctest --test-dir build --output-on-failure
```
A change is done when its directory compiles and its tests pass.
## Conventions
**No Python in the engine, its tools, its build or its test suite.** `spec.md` opens with this
constraint. Tests are C++ and CTest. The Python files in the tree are run by hand and nothing in
the build or the suite calls them: `tests/mkgolden.py` and `tests/mklfm2golden.py` regenerate the
reference numbers in `tests/mm_golden.h` and `tests/lfm2_golden.h` from transformers;
`tools/pad_tp.py` and `tools/pad_verify.py` pad a checkpoint for a tensor-parallel width and check
the result; `scripts/cardclock.py` monitors card clocks.
**C ABI at plugin boundaries, C++20 inside.** `abi/` is C and must stay compilable as C.
Everything under `core/` is C++20 and may use the standard library freely.
**Errors are values.** Return `RAD_OK` or a negative `RAD_E_*`. Do not throw across a function
that a plugin or the run phase can call. `fatal()` is for broken invariants only.
**Nothing unimplemented returns success.** An unwritten function returns `RAD_E_UNSUPPORTED` and
logs once at Warn with its own name. A stub that returns 0 produces fluent wrong output with no way
to bisect it (`spec.md` §17).
**No allocation and no synchronisation on the step path.** The arena is sized at declare. A
`malloc` needed inside `rad_arch_step` or the scheduler's per-step work is a declare-phase bug.
**Kernel scratch is per lane, each region sized by what its lane may issue.** In-order launch lets
every op on one stream share one scratch region; a second lane is a second stream and gets a second
region once the architecture names any buffer to `rad_buf_concurrent` (`Program::scratch_regions`).
Region 0 holds the program's largest device scratch (`Program::scratch_bytes`). Region 1 holds the
largest device scratch of an op the second lane may issue (`Program::scratch_bytes_lane1`, computed
in `Builder::finish_buffer_liveness`): by `Ctx::lane1_`'s rule 3 such an op touches only
concurrent buffers, so an op with any other transient buffer never runs there, and one with no
transient buffer counts. `Arena::init` takes both sizes, `arena_plan_bytes` charges both to the VRAM
budget, and startup prints them (`arena: ... device + ... scratch + ... for the second lane = ...`).
A lane-1 request larger than region 1 comes from an op that breaks rule 3 in its buffers too, so
`Ctx::issue` fails the step naming the kernel (`RAD_E_SCRATCH`) rather than let it overrun the
region. On Qwen3.8-27B with its DFlash2 drafter region 1 is 0 B against region 0's 50 MiB a card
at speculation 7 (17 MiB without speculation).
**`rad_memset_async` refuses `RAD_MEM_HOST` on a card; the host backend cannot reproduce that.**
`RAD_MEM_HOST` is pageable memory the GPU has never been shown, so the device backend returns
`RAD_E_UNSUPPORTED` naming the rule (`core/device/aql.cpp`). On the host backend all memory kinds
are the same memory, so a memset on host memory passes in a no-ROCm build and fails on a card. Use
`RAD_MEM_HOST_PINNED` or `RAD_MEM_HOST_MAPPED`, or memset on the host. `rad_memcpy_async` accepts
pageable memory but bounces it through pinned memory and waits, so a pageable copy on the step path
is a host stall; `aql_pageable_bytes()` counts them.
**Kernel plugins launch with `hipLaunchKernel` and the engine dispatches them.** On a card every
stream is an HSA queue the engine owns and writes AQL packets into (`core/device/aql.cpp`). The
executable defines `hipLaunchKernel` and HIP's kernel registration hooks, so a plugin built by
hipcc needs nothing special. A plugin must not call any other HIP stream API on an engine stream:
`hipEventRecord` and `hipStreamWaitEvent` are refused by name, and anything else would hand HIP a
stream it does not own. Use `rad_event_*` and `rad_memcpy_async`.
**Comments explain the decision, not the syntax.** Say why this and not the obvious alternative,
and name the cost when there is one. `libr4d/r4d_rows.cpp` is the model.
**Naming.** `rad_` prefix on everything with external linkage. Files are named for what they
provide. `snake_case` for functions and variables, `PascalCase` for types.