Merge upstream main (v1.15.1 -> v1.17.0, 152 commits) into the SYCL port

Conflicts, both sides kept:
- abi: RAD_ABI_VERSION 37 (upstream); the fork's RadBuildCtx.plain_norm after upstream's
  expert_file_tier, widened to int64_t so it starts past ABI 37's 208-byte struct instead of in
  its tail padding (abi_layout_test); RadSyclStream rows after RadShapeGen*.
- arch/common/rad_block_gdn_fp8.h: upstream's q8_rot guard and the fork's o_plain guard
  combined; the decode-row q_o.step only where q_o is declared (!o_plain).
- docs/OPS.md: upstream's optional params on sample_logprobs, the fork's q_temp on
  mtp_couple_propose.

Also ignores /baseline-models/ (local model copies).

Host tests: arch_test's MTP lint/graph-pin failures are unchanged from before the merge (the
fork's mtp.fc LinearFP8, 10a99a5); avx_check's hand-built moe_gemm_q w4a8 case also fails on
upstream's own files; images/videos need FFmpeg; three binaries over their CPU budget on this VM.
SYCL card tests 3/3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
claudeandClaude Opus 5.5 committed 2026-11-07 22:13:08 +01:00
commit 45f3b510c7
365 files changed
+56942 -6331

No files matched your search

+56 -14
View File
@@ -31,11 +31,15 @@ int Ctx::init(const CtxDesc& d) {
registry_ = d.registry;
kv_ = d.kv;
arch_step_ = d.arch_step;
arch_pass_key_ = d.arch_pass_key;
profiling_ = d.profile_ops;
/* The rank owns one device. Set it here so allocation lands on the right card, and again from
* the rank thread in bind_thread() because the backend's current device is thread-local. */
RAD_TRY(rad_dev_set(device_));
RadDeviceProps props{};
if (rad_dev_props(device_, &props) >= 0 && !props.is_host_backend && props.n_cu > 0)
wgp_count_ = props.n_cu;
if (d.stream) {
stream_ = d.stream;
@@ -367,6 +371,7 @@ int Ctx::bind_ops() {
ip.args.instance = r.instance;
ip.args.rank = rank_;
ip.args.world_size = world_size_;
ip.args.wgp_count = wgp_count_;
/* A launch that ends its own link hold gets the hold word as an extra
* `link_gate` parameter after the geometry's. */
if (r.row->releases_link)
@@ -777,13 +782,30 @@ const int32_t* Ctx::route_counts_device(int layer, int64_t* n_expert) const {
if (n_expert) *n_expert = 0;
if (layer < 0 || (size_t)layer >= route_.size()) return nullptr;
const RouteSlot& s = route_[(size_t)layer];
/* No step gate: route_report lands at the END of the block's passes (heat consumes it a
* step later), but the reported buffer is the block's own ecnt, whose CONTENT the router
* kernel rewrote this pass -- and the file stager's trigger runs after that router on the
* same stream, so a stream-ordered copy reads this pass's counts. The counts are indexed
* by the rank's local expert slot (the kernel subtracts expert_base), which is also what
* the mover's units carry. */
/* No step gate: route_report lands at the END of the block's passes, so at the file stager's
* trigger (the layer's first expert op) the pointer is the one the last pass reported. That
* is right only for a buffer whose address is the same in this pass: the block's own counts
* buffer, which its router rewrites every pass -- and the trigger runs after that router on
* the same stream, so a stream-ordered copy reads the counts of the rows about to run. The
* counts are indexed by the rank's local expert slot (the kernel subtracts expert_base),
* which is also what the mover's units carry. */
if (!s.routing.expert_count || s.reported < 0) return nullptr;
/* A transient buffer is at another offset at another arena level, and liveness lets a
* later buffer share its bytes, since no declared op reads the counts (the report is a
* handoff, not an op): the bytes under the last pass's pointer may be anything. A whole read
* is slow but right; counts read there could name a routed expert zero and leave it
* unread. Every architecture here declares the counts RAD_BUF_PERSIST. */
const char* p = (const char*)s.routing.expert_count;
const char* base = (const char*)arena_.base();
if (base && program_ && p >= base + program_->arena_fixed_bytes && p < base + arena_.bytes()) {
if (!route_transient_noted_) {
route_transient_noted_ = true;
RAD_WARN("rank %d: layer %d reports its routing counts in a transient buffer, so the "
"file stager reads its routed experts whole; declare the counts "
"RAD_BUF_PERSIST", rank_, layer);
}
return nullptr;
}
if (n_expert) *n_expert = s.routing.n_expert ? s.routing.n_expert : route_n_expert_;
return s.routing.expert_count;
}
@@ -942,23 +964,36 @@ int Ctx::lane_join(int from, int to) {
/* The key below reads every RadBatch field a pass's shape or arguments can depend on, by hand. A
* field added to RadBatch changes its size: decide whether the key must read it, then update the
* size here. `agen_b` and `vgen_b` are set only on generator passes, which are never recorded
* (below). */
static_assert(sizeof(void*) != 8 || sizeof(RadBatch) == 408,
* size here. The ABI-35 host mirrors are used by issue-time split selection; unsupported dynamic
* geometries fail closed via the pass-key contract, while Qwen keys its full-group maximum in the
* architecture callback. `agen_b`, `vgen_b` and `sgen_b` are set only on generator passes, which are
* never recorded (below). */
static_assert(sizeof(void*) != 8 || sizeof(RadBatch) == 480,
"RadBatch changed: check that Ctx::pass_key reads every new field a pass depends on");
static_assert(sizeof(void*) != 8 || sizeof(RadKVGroupBatch) == 80,
static_assert(sizeof(void*) != 8 || sizeof(RadKVGroupBatch) == 88,
"RadKVGroupBatch changed: check that Ctx::pass_key reads every new field");
uint64_t Ctx::pass_key(const RadBatch* b) const {
/* Not recordable: encoder passes are shaped by their media, and passes carrying encoder rows
* issue the scatter only when they have some. */
if (b->enc || b->n_mm_rows > 0) return 0;
* issue the scatter only when they have some (and the second attention over a run read both
* ways, n_bidi, only on such a pass). */
if (b->enc || b->n_mm_rows > 0 || b->n_bidi > 0) return 0;
/* A plugin with no pass-key callback cannot key a launch that splits its KV by the live
* context, so a prefill or mixed pass of a program holding such a launcher is not recorded.
* Its decode, verify and draft passes are: their launches get the keyed context bound in
* place of the live maximum (Ctx::issue_body). */
if (!arch_pass_key_ && (b->phase == RAD_PHASE_PREFILL || b->phase == RAD_PHASE_MIXED)) {
for (const IssuePlan& ip : issue_table_)
if (ip.row && ip.row->info &&
(ip.row->info->capabilities & RAD_KERNEL_CAP_DYNAMIC_PREFILL_SPLIT))
return 0;
}
/* A pass carrying a request's words for the plugin is shaped by them (a head issues per
* sequence from their counts), and such a step samples a prompt's first token, which is not
* the repeating decode pass. */
if (b->n_plugin_words > 0) return 0;
/* A generator pass is shaped by what RadBatch::gen_b, agen_b or vgen_b carries, which no key
* below reads. */
/* A generator pass is shaped by what RadBatch::gen_b, agen_b, vgen_b or sgen_b carries, which
* no key below reads. */
if (b->gen) return 0;
uint64_t h = 0x9e3779b97f4a7c15ull;
auto mix = [&](uint64_t v) {
@@ -976,6 +1011,8 @@ uint64_t Ctx::pass_key(const RadBatch* b) const {
ptr(b->token_ids); ptr(b->positions); ptr(b->cu_seqlens); ptr(b->seq_ids);
ptr(b->out_ids); ptr(b->q_lens); ptr(b->ctx_lens); ptr(b->num_accepted);
ptr(b->spec_parent); ptr(b->checkpoint_tok); ptr(b->checkpoint_slot); ptr(b->draft_out_ids);
/* A block drafter's query pass reads it (null on every other pass). */
ptr(b->anchor_rows);
/* Null on a step with no adapter row, which then issues no adapter op (Ctx::issue_lora): the
* two are different passes. The slots themselves are read on the card. */
ptr(b->lora_slot);
@@ -989,6 +1026,11 @@ uint64_t Ctx::pass_key(const RadBatch* b) const {
/* A streamed group's view by the memory it names, never its address (kvstream_view_key). */
mix(kvstream_view_key(k.stream));
}
if (arch_pass_key_) {
const uint64_t geometry = arch_pass_key_(const_cast<Ctx*>(this), b, b->kv_used_max_h);
if (!geometry) return 0;
mix(geometry);
}
mix((uint64_t)level_);
mix(dense_epoch_);
mix(domain_epoch_.load(std::memory_order_relaxed));
+11 -8
View File
@@ -40,10 +40,6 @@ class Registry;
struct XpState;
struct XpLayer;
/* The widest conventional op is gdn_conv_prep at sixteen operands (docs/OPS.md). Thirty-two leaves
* room for an architecture plugin's private ops without a variable-length array on the hot path. */
constexpr int MAX_OPERANDS = 32;
typedef void (*ArchStepFn)(RadCtx*, const RadBatch*);
/* "[shape]/[stride]" of a tensor's live dimensions, for diagnostics. */
@@ -196,6 +192,7 @@ struct CtxDesc {
RadStream stream = nullptr; /* null: the Ctx creates its own compute stream */
ResidencyTable* residency = nullptr; /* null in the all-in-VRAM case */
ArchStepFn arch_step = nullptr; /* the architecture plugin's rad_arch_step */
uint64_t (*arch_pass_key)(RadCtx*, const RadBatch*, const int32_t*) = nullptr;
bool profile_ops = false; /* spec §16, and it changes what you measure */
/* Whether a repeated pass may be recorded and played back. False for a caller whose pass
* issues work the batch does not describe (e.g. a test driving another component through
@@ -260,6 +257,7 @@ public:
}
/* This rank's device ordinal, for the calls that name a card rather than a stream. */
int device() const { return device_; }
int wgp_count() const { return wgp_count_; }
int world_size() const { return world_size_; }
RadStream stream() const { return stream_; }
Program* program() const { return program_; }
@@ -334,10 +332,11 @@ public:
/* Called by the drain, so the ring knows which banks are still unread. */
void mark_credited(int layer, int bank);
const RadRouting* last_routing(int layer) const;
/* The device pointer of `layer`'s expert counts for the current step, with `n_expert` set,
* or null when the layer has not reported this step (a routing-aware file stager then
* reads the layer whole). The pointer is stream-ordered behind the router: a copy on the
* compute stream at the stager's trigger sees this pass's counts. */
/* The device pointer of the buffer `layer`'s router writes its expert counts to, as the
* layer last reported it, with `n_expert` set; null when the layer has not reported yet, or
* when the buffer is a transient one (a routing-aware file stager then reads the layer
* whole). The router rewrites it every pass before the layer's first expert op, so a copy on
* the compute stream at the stager's trigger sees the counts of the rows about to run. */
const int32_t* route_counts_device(int layer, int64_t* n_expert) const;
/* ---------------------------------------------------------- the placement seam */
@@ -657,6 +656,8 @@ private:
bool staging_ = false; /* the stager armed for the pass being issued */
int issue_body(rad_op h, const RadOperand* opd, int n_opd, int64_t n);
ArchStepFn arch_step_ = nullptr;
uint64_t (*arch_pass_key_)(RadCtx*, const RadBatch*, const int32_t*) = nullptr;
int wgp_count_ = 32;
/* The expert prefetch's state (xp_state.h), null while it is off. */
std::unique_ptr<XpState> xp_;
@@ -778,6 +779,8 @@ private:
std::vector<RouteSlot> route_;
int64_t route_n_expert_ = 0;
/* route_counts_device has said once that a layer's counts are a transient buffer. */
mutable bool route_transient_noted_ = false;
/* A bank is one step's histograms for every layer: [kRouteBanks][n_layer][n_expert] in pinned
* host memory. A report copies a layer's counts into its row of the card-side
* [n_layer][n_expert] (an on-card copy that orders nothing else); the end of the pass copies
+75
View File
@@ -10,6 +10,7 @@
* the host. See ctx.h for the cost budget (spec §3.2).
*/
#include <algorithm>
#include <cstdint>
#include <chrono>
#include <functional>
#include <unordered_map>
@@ -62,6 +63,66 @@ std::string tensor_shape_str(const RadTensor& t) {
return s + "]";
}
/* A device launch may use a host-side KV bound only when its exact operand maps to the host mirror
* of seqused (including a sequence slice), or to the position just after a one-sequence prefill
* piece. Never copy seqused back from a device queue here: the AQL upload may still be pending on
* another queue. */
static bool issue_vector_operand(const RadTensor& t) {
return (t.rank == 1 && t.stride[0] == 1) ||
(t.rank == 2 && t.shape[1] == 1 && t.stride[0] == 1 && t.stride[1] == 1);
}
static bool issue_host_span_max(const RadTensor& t, const int32_t* device, const int32_t* host,
int64_t total, int32_t* out) {
if (!t.data || !issue_vector_operand(t) || t.shape[0] <= 0 || !device || !host || total <= 0 || !out)
return false;
const std::uintptr_t p = reinterpret_cast<std::uintptr_t>(t.data);
const std::uintptr_t base = reinterpret_cast<std::uintptr_t>(device);
if (p < base) return false;
const std::uintptr_t delta = p - base;
if (delta % sizeof(int32_t)) return false;
const std::uint64_t first = delta / sizeof(int32_t);
const std::uint64_t count = (std::uint64_t)t.shape[0];
if (first > (std::uint64_t)total || count > (std::uint64_t)total - first) return false;
int32_t maximum = host[first];
for (std::uint64_t i = 1; i < count; ++i) maximum = std::max(maximum, host[first + i]);
*out = maximum;
return true;
}
static int32_t issue_kv_used_max_h(const RadBatch* b, const RadTensor* t, int n) {
if (!b || !t || b->n_seq <= 0) return -1;
bool have = false;
int32_t found = -1;
auto add = [&](int32_t value) {
if (!have) { found = value; have = true; }
else if (found != value) return false;
return true;
};
for (int i = 0; i < n; ++i) {
if (!t[i].data || !issue_vector_operand(t[i]) || t[i].shape[0] <= 0) continue;
if (b->kv) for (int g = 0; g < b->n_kv_groups; ++g) {
if (b->kv_used_max_h && t[i].data == b->kv[g].seqused &&
t[i].shape[0] == b->n_seq) {
if (!add(b->kv_used_max_h[g])) return -1;
continue;
}
int32_t used = -1;
if (issue_host_span_max(t[i], b->kv[g].seqused, b->kv[g].seqused_h,
b->n_seq, &used) && !add(used)) return -1;
}
/* ArPieces' first attention uses positions[R] as its exact KV end: it is the position after
* the first query piece. This special operand is one length for one ordinary prefill seq. */
if (b->phase == RAD_PHASE_PREFILL && b->n_seq == 1 && b->n_spec == 0 &&
b->draft_pass == 0 && t[i].shape[0] == 1) {
int32_t used = -1;
if (issue_host_span_max(t[i], b->positions, b->positions_h, b->n_tok, &used) &&
!add(used)) return -1;
}
}
return have ? found : -1;
}
/* Element offset to byte offset. False when it cannot be expressed: a mid-byte offset into a
* packed sub-byte weight (e.g. an odd W4 element), or a plugin-private dtype with no core-known
* size. */
@@ -854,6 +915,17 @@ int Ctx::issue_body(rad_op h, const RadOperand* opd, int n_opd, int64_t n) {
/* The rest of RadArgs was filled in at prepare; args.t already points at tbuf_. There is one
* entry per schema operand, the trailing optional ones the issue left out null. */
ip.args.n_t = std::max(n_opd, (int)p.n_opd_schema);
ip.args.kv_used_max_h = issue_kv_used_max_h(batch_, t, ip.args.n_t);
/* A plugin with no pass-key callback has its decode, verify and draft passes recorded
* (pass_key), and a played pass keeps the arguments it was recorded with, so a launch there
* must not read a bound the key does not: it gets the step's context bound plus its longest
* query, both keyed, which crosses a bucket every 1024 tokens or more (batch.cpp) instead of
* moving with every token. Its prefill and mixed passes are never recorded and keep the
* exact maximum. */
if (ip.args.kv_used_max_h > 0 && !arch_pass_key_ && batch_ &&
batch_->phase != RAD_PHASE_PREFILL && batch_->phase != RAD_PHASE_MIXED)
ip.args.kv_used_max_h = (int32_t)std::min<int64_t>(
INT32_MAX, (int64_t)batch_->max_ctx_len + batch_->max_q_len);
p.issued = true;
++n_issues_;
@@ -1280,6 +1352,9 @@ int rad_kv_layer_index(RadCtx* c, rad_kvgroup group, int layer) {
}
int rad_rank(RadCtx* c) { return c ? static_cast<rad::Ctx*>(c)->rank() : 0; }
int rad_world_size(RadCtx* c) { return c ? static_cast<rad::Ctx*>(c)->world_size() : 1; }
int rad_ctx_wgp_count(RadCtx* c) {
return c ? static_cast<rad::Ctx*>(c)->wgp_count() : 32;
}
RadStream rad_stream(RadCtx* c) { return c ? static_cast<rad::Ctx*>(c)->stream() : nullptr; }
/* The second lane. `rad_lane` selects the lane subsequent issues launch on; `rad_lane_join`