device and plugin boundary: kv_resolve, the streamed store rows, ABI 30

This commit is contained in:
C_Tamas committed 2026-10-24 19:19:19 +07:00
1 parent 5a9ce4912b
commit cce687bf2c
34 files changed
+3643 -29

No files matched your search

+41 -5
View File
@@ -14,11 +14,47 @@ extern "C" {
/* Bumped whenever anything a plugin compiles against changes shape: this header, rad_types.h,
* rad_builder.h, rad_runtime.h, rad_device.h, rad_encoding.h, rad_quant.h, rad_sample.h or
* rad_spechub.h. Every
* kind of plugin reports it, and a plugin that does not report exactly this version is refused at
* load with both versions printed. A new optional export (e.g. rad_plugin_reference) is not a
* change of shape: a plugin without it gets the default. */
#define RAD_ABI_VERSION 27u
* rad_spechub.h. Every kind of plugin reports it, and a plugin that does not report exactly this
* version is refused at load with both versions printed. A new optional export (e.g.
* rad_plugin_reference) is not a
* change of shape: a plugin without it gets the default.
*
* 26: RadKVGroupBatch grows one trailing, zero-defaulted field (`stream`, a RadKVStream) so a
* plugin can see a streamed KV group's ring, residency map and host mirror, and RadBuildCtx grows
* `kv_resident_blocks` and `kv_stream_ok` (--kv-resident and the architecture's capability). Every
* earlier field keeps its offset, but the structs are larger, so out-of-tree plugins must be
* rebuilt against 26.
*
* 27: RadBuildCtx grows `cvec`, a rank's control-vector direction table (control vectors). Trailing
* and zero-defaulted as above, so a plugin built against 26 keeps every offset.
*
* 28: RadArchProbe grows two trailing, zero-defaulted fields that met in the same merge:
* `chain_pays` (the plugin measured its speculative step faster with chained launches) and
* `kv_stream_ok` (the architecture can move a KV group between host memory and a resident ring).
* Both are additions a 27 plugin never reads; a plugin that does set them must be built against
* 28, since 27's struct ends where these begin.
*
* 29: radiance 1.11.0's Unlimited-OCR and control-vector work meets KV streaming. RadBuildCtx
* grows `attn_prefill_fp8` (--attn-prefill-fp8, the prefill attention's products accepted in
* e4m3); it fills the four padding bytes ABI 28 already had after `kv_quant`, so no field moves.
* RadArchProbe grows `control_vector_ok` after ABI 28's `kv_stream_ok`: upstream 1.11.0 had it at
* the offset `kv_stream_ok` took in 28, so it moves to 80 and the struct grows, which is why this
* is a bump. Both trailing fields are zero-defaulted, but a plugin that sets either must be built
* against 29.
*
* 30: radiance 1.12.0 meets KV streaming. Upstream spent 27 on its own additions and this tree had
* already spent it, so the merge takes the next number for both: upstream's `RadBuildCtx` grows
* `tp_attn_rows`, `state_dtype` (--state-cache-dtype f16) and `draft_coupling`, and gains
* `rad_lane1_window_begin/end` and `rad_drafter_short_block`; `RadArchProbe` grows
* `tp_attn_rows_ok` and `state_f16_ok`; `RadAudioGenBatch` grows `prev_slot`/`prev_pos`/
* `prev_used` and the reference prompt's `n_ref`/`ref_samples`/`ref_audio`/`ref_cu`/`ref_cu_h`/
* `ref_frame`/`ref_frame_h`; `RadAudioGenDecl` grows `branches`, `input_special` and the
* `tmpl_ref`/`ref_*` reference template; and the plugin ABI gains `rad_spechub.h` (a whole new
* header) with the per-row `rad_kernel_reference` override. Every upstream field is trailing and
* zero-defaulted, and `RadKVStream` loses our never-read `scratch` (80 -> 72 bytes), so a plugin
* built against 26-29 keeps the offsets it reads only if it is rebuilt; the loss of a middle
* pointer in `RadKVStream` is why this is a bump even before upstream's additions. */
#define RAD_ABI_VERSION 30u
/* ================================================================== plugin identity */
/* One value per plugin, not flags (QUANT is 3). RAD_PLUGIN_QUANT is a quantiser plugin:
+67
View File
@@ -22,6 +22,56 @@ typedef struct RadCtx RadCtx;
* scheduler and the run phase the bounds without a device read. */
enum { RAD_PHASE_PREFILL = 0, RAD_PHASE_DECODE = 1, RAD_PHASE_MIXED = 2 };
/* ================================================================== KV streaming */
/* A streamed KV group keeps its whole context in pinned host memory and only a ring of blocks on
* the card (docs/KV-STREAMING.md §3.2, §3.6, §4.2). The step batch carries this view of that
* group; a group whose KV is fully resident has none.
*
* Why one pointer to a struct rather than fields inside RadKVGroupBatch: the data is per group
* and fixed at bring-up, while the batch is rebuilt per step for every rank, so copying its
* pointers and geometry per group into every step's batch would be work that never changes. One
* trailing pointer also keeps a group's own layout as it was for every plugin that does not
* stream: the field is zero-defaulted, and a null `stream` means the group is fully resident and
* has no ring, no map and no host copy.
*
* The layout mirrors the pool, layer by layer: a layer's mirror is a dense run of `blocks` rows
* of `block_bytes`, and `host_layer_stride` is `blocks * block_bytes`. So layer l's row for block
* b is at `host + l * host_layer_stride + b * block_bytes`, and `block_bytes` is both the row
* pitch `kv_resolve` reads the mirror at and the length of every copy into the ring.
*
* `res`, `slot_block`, `slot_state` and `ctl` are device memory: a resolve launch reads and
* rewrites them every step, so a host round trip per lookup would cost the link latency the ring
* exists to avoid. Nothing here changes address for the engine's life, which is what lets a
* recorded pass replay a resolve. */
typedef struct RadKVStream {
/* The group's mirror, pinned host memory, RAD_MEM_HOST_MAPPED so a device kernel reads it over
* the link in place. `blocks * bytes_per_block` bytes, all layers, zeroed at bring-up: a block
* no store has written yet reads as zeros rather than as whatever the page held. */
uint8_t* host;
/* Residency, both directions, ONE SLICE A LAYER: `res` is block -> ring slot or -1 (device,
* `n_layers * blocks` entries, layer l at `res + l * blocks`), `slot_block` is slot -> block or
* -1 (`n_layers * slots`), `slot_state` is `n_layers * slots * 2`: the epoch of a slot's last
* use and its CLOCK reference bit. A block is resident per layer because the resolve copies one
* layer's bytes; a group-wide map would call a block resident for a layer that was never
* copied. `ctl` is shared: its counters and overflow latch are the group's. */
int32_t* res;
int32_t* slot_block;
int32_t* slot_state;
/* The control block: epoch, clock hand, the last launch's miss count, the overflow latch, and
* the cumulative misses/lookups/calls counters (docs/OPS.md "kv_resolve"). */
int32_t* ctl;
/* Bytes one block of ONE layer occupies: the mirror's row pitch and the ring's slot pitch. */
int64_t block_bytes;
/* Bytes one layer of the mirror occupies, i.e. the distance between layer l and layer l+1. */
int64_t host_layer_stride;
/* Ring depth in blocks: the group's device pool is `slots` blocks deep while a selection names
* block ids over the group's whole context. */
int64_t slots;
/* Bytes the host mirror occupies, all layers (`blocks * block_bytes * n_layers`), so a caller
* that only has this view can report or check the off-card tier. */
int64_t host_bytes;
} RadKVStream;
typedef struct RadKVGroupBatch {
rad_kvgroup group;
int32_t* slot_mapping; /* [n_tok] flat slot index per token, device */
@@ -36,6 +86,10 @@ typedef struct RadKVGroupBatch {
int64_t state_index_pitch;
int64_t block_size;
int64_t max_blocks;
/* Null unless this group's KV streams: the mirror, the residency map and the ring's geometry
* (RadKVStream above). Trailing and zero-defaulted, so a plugin built against ABI 25 reads
* every field it knows at the same offset and simply never sees the ring. */
const RadKVStream* stream;
} RadKVGroupBatch;
typedef struct RadBatch {
@@ -721,6 +775,19 @@ int rad_step_fail(RadCtx* c, const char* what);
/* ================================================================== run-phase queries */
const RadBatch* rad_batch(RadCtx* c);
/* The position of absolute model layer `layer` in the cache `group` holds, or -1 when this rank
* has no pool for it or the group does not hold that layer. It is the same lookup a RAD_KV operand
* makes (KVPoolBinding::layer_slot), and the index a streamed group's host mirror is laid out by:
* layer `i` of the group is at `stream->host + i * stream->host_layer_stride`. A group's bound
* layers need not be contiguous, so this is never `layer` itself. */
int rad_kv_layer_index(RadCtx* c, rad_kvgroup group, int layer);
/* Prefill staging (docs/KV-STREAMING.md §4.1). When the group's pass selected the one-layer stage
* view, copy this layer's whole host-mirror row into it, on the compute stream, after the layer's
* store and before its attention; returns 1 and the caller must not resolve. Returns 0 when the
* group has no stage view (a resident group, or the parity configuration's context-sized ring) and
* the caller proceeds normally. The ordering rule is in core/kvstream/stage.h. */
int rad_kv_stage_layer(RadCtx* c, rad_kvgroup group, const RadKVGroupBatch* kvb,
int layer);
int rad_rank(RadCtx* c);
int rad_world_size(RadCtx* c);
RadStream rad_stream(RadCtx* c);