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/core/sched/geometry.h
T
baKon 662b7720b5 core/sched: a prefix hit resumes on the cold run's chunk grid, so it recomputes the cold run's own chunks
A hit recomputes the tokens past where it resumes. Kernels may round a row differently at another
chunk length (spec §17: the fp8 GEMM's split-K count is chosen from M), so a hit that recomputes
its tail in other chunks than a cold prefill of the same prompt can flip a near-tie in greedy text.
On the DeepSeek-V4.1 daily (TP4) a 24k-token prompt that hit a 16k one's tail snapshot resumed at
15856, ran a 528-token chunk to the next boundary and answered differently from its cold run.

--prefix-resume exact, the default:

- ChunkGeometry carries the grid an idle server's cold prefill chunks on: --max-num-batched-tokens
  rounded down to the quantum, or the checkpoint interval where chunks split at it and it is the
  smaller, times the span from the second interval on.
- plan_chunk writes checkpoints only on that grid. A prompt's tail snapshot is taken where its last
  chunk starts (sliding-window snapshots keep their one-block margin) instead of at its last
  quantum boundary. The chunk already ends there, so a long cold prompt no longer runs a short
  extra step at its end.
- A checkpoint interval past the chunk must be a multiple of it; otherwise it is rounded up, and
  startup says so.
- admit_one steps a hit with no snapshot to resume from (full attention) back to the grid. A
  request's own checkpoint (Request::checkpoint_at, /v1/systemone's shared head) is honored where
  it falls.

--prefix-resume nearest keeps the previous rule.

The cost is re-prefill: an exact repeat runs the cold run's last chunk again, and an agent's next
turn resumes where the previous prompt's last chunk started, about half a chunk more than nearest
(2048 tokens at a 4096-token budget). The property holds on an idle server; a prefill that shares
its steps with decode rows or other requests (--busy-prefill-chunk, --prefill-interleave) is cut
differently, as batching already changes greedy text.

Tests: sched_test covers the grid for six geometries (GDN hybrid, interval past the budget, span,
periodic and tip-only window snapshots, full attention), the interval rounding, the tail snapshot
at the last chunk start, admission stepping back to the grid, and a property over prompt lengths:
every checkpoint is a chunk start of the cold run of its prompt and of any longer prompt it
prefixes, and a hit from it cuts the cold run's chunks. config_test covers the flag.
2026-10-28 12:50:54 +01:00

102 lines
5.2 KiB
C++

/* geometry.h -- the prefill chunk geometry, computed from the resolved kernels.
*
* The attention block size comes from the resolved attention kernel (§7.2) and the linear-state
* chunk length from every resolved kernel that marks a parameter RAD_PROLE_SEQ_CHUNK, so the
* operator never has to supply a minimum --max-num-batched-tokens by hand (as vLLM requires for
* hybrid models). If the budget is below what the geometry needs, startup prints the number and
* refuses: a smaller budget means chunks never land on a checkpoint boundary and the linear
* prefix hit rate is zero.
*/
#pragma once
#include "rad_core.h"
namespace rad {
struct ChunkGeometry {
/* Attention block size, the lcm across every paged KV group. libr4d's paged attention is
* compiled for 16 only. */
int64_t attn_block = 1;
/* Tile length of a linear-state scan (64 for libr4d's gated delta net). Taken from the
* declared geometry and kernel constraints of every op whose schema marks a parameter
* RAD_PROLE_SEQ_CHUNK. */
int64_t state_chunk = 1;
/* lcm(attn_block, state_chunk); every non-final prefill chunk is a multiple of it, so no
* kernel is handed a partial tile. */
int64_t quantum = 1;
/* Linear-state checkpoint interval (§7.3): the operator's request rounded up to a multiple
* of `quantum`, so a split at the interval is a legal chunk split. Zero without linear state
* (see window_ckpt for the exception). */
int64_t checkpoint_interval = 0;
/* How many intervals one prefill chunk may carry from an interval boundary past the first
* (Config::prefill_span); 1 without an interval grid. plan_chunk is where it acts. */
int64_t span = 1;
/* The budget of every step a span does not take (prefill_shared_tok). A span is for a prefill
* alone in its step (Scheduler::step); a step carrying anything else is planned at this
* budget as a deployment without a span plans it, so the span's extra rows never lengthen a
* step other requests wait on. */
int64_t shared_tok = 0;
/* The smallest --max-num-batched-tokens this geometry can be served with. */
int64_t min_max_tok = 1;
/* Smallest upper bound a kernel places on the chunk length, or 0. Used only to report a
* contradiction with `quantum`, never to clamp. */
int64_t max_chunk = 0;
/* Every contributor by name, for the startup line and the refusal. */
std::string why;
/* No linear state but a sliding window: checkpoints snapshot the window behind an
* interval boundary or the prompt's tail (KVManager::has_window_snapshot). Intermediate
* snapshots let an edited prompt resume below its divergence instead of from zero.
* tip-only skips interval snapshots and keeps the original tail-only chunking. */
bool window_ckpt = false;
/* Window checkpoints at interval boundaries too, not only at the tail: every policy but
* tip-only, which keeps the tail alone and so splits no chunk at the interval. */
bool window_periodic = false;
/* --prefix-resume exact (Config::prefix_exact): a prefix hit resumes only where a cold prefill
* of the same prompt starts a chunk, so the tokens a hit recomputes are cut into the chunks a
* cold run cuts them into. A kernel may round a row differently at another chunk length (a
* split-K count chosen from M, a small-M path); computed in the same chunks, the hit's output
* is the cold run's, bit for bit, on an idle server. The grid is where an idle server's prefill
* chunks start: 0, then grid_origin + k * grid. `grid` is the checkpoint interval where chunks
* split at it and it fits the budget, times the span from the second interval on (origin = the
* interval), and otherwise --max-num-batched-tokens rounded down to the quantum (the interval
* is then rounded up to a multiple of it). Zero when not exact. */
bool exact = false;
int64_t grid = 0;
int64_t grid_origin = 0;
bool checkpoints() const { return checkpoint_interval > 0; }
/* The deepest grid position at or below `pos`; `pos` itself when not exact. */
int64_t grid_floor(int64_t pos) const {
if (pos <= 0) return 0;
if (!exact || grid <= 0) return pos;
if (pos < grid_origin) return 0;
return grid_origin + (pos - grid_origin) / grid * grid;
}
bool on_grid(int64_t pos) const { return grid_floor(pos) == pos; }
};
/* The budget of a step a span does not take (ChunkGeometry::shared_tok), from the configuration
* alone: --max-num-batched-tokens less the span's extra intervals, at least one interval, and the
* whole budget without a span. The engine states it to the plugin before declare
* (RadBuildCtx::max_mixed_tok) and the scheduler plans by it, so the two agree. */
int64_t prefill_shared_tok(const Config& cfg);
/* Resolve the geometry from the declared program and the operator's budgets.
*
* Returns RAD_E_INVAL when cfg.max_tok is too small (the required number is in
* out->min_max_tok, the reason in out->why; the caller stops), and RAD_E_SHAPE when two kernels'
* constraints contradict each other, which is a kernel-set bug. */
int chunk_geometry_resolve(const Program& prog, const Config& cfg, ChunkGeometry* out);
} /* namespace rad */