format: a span per stacked part, and a share that is zeros

Two additive builder exports, RAD_ABI_VERSION untouched.

rad_weight_shard_span_parts: a row-sharded weight whose stacked parts
are not the same width -- the delta net's [q|k|v|z], 2048, 2048, 6144,
6144 rows on Flash-Next -- cannot take rad_weight_shard_span's one
[lo, hi), which is applied to every part and assumes them equal. This
takes a span per part together with each part's whole extent in the
container; share.cpp cuts every part by its own rows and refuses totals
that do not sum to the entry. It is how a rank declares an UNEVEN share
of the heads -- 6 of 16 K heads where three ranks take 6, 5 and 5 --
with nothing padded.

rad_weight_shard_zero: this rank's share of a row- or column-sharded
weight is zeros. weight_share returns the declaration's extents with no
rectangles; the loader stages the weight with a worker that writes
zeros (the route a checkpoint weight already takes from a container)
and reads nothing. It is for a rank the geometry gives no real heads
(next commit), which then declares the same program as its peers.

share_test holds the per-part cut and the zero share.
This commit is contained in:
baKon authored and StillDeadcode committed 2026-10-06 11:23:06 +02:00
1 parent 1936bc881f
commit 82445ca71f
9 files changed
+241 -8

No files matched your search

+17
View File
@@ -364,6 +364,23 @@ int rad_buf_concurrent(RadBuilder* b, rad_buf buf);
* groups the span would cut. */
int rad_weight_shard_span(RadBuilder* b, rad_weight w, int64_t lo, int64_t hi);
/* A STACKED weight's span, ONE [lo, hi) PER ROW PART, with each part's whole extent in the
* container: the delta net's [q|k|v|z] is 2048, 2048, 6144, 6144 rows on Flash-Next, and parts
* that are not the same width cannot take rad_weight_shard_span's one span. `n` is the declared
* part count; part g of the container is `total[g]` rows, of which this rank takes [lo[g], hi[g])
* and declared `hi[g] - lo[g]`; the totals must sum to the entry's rows. A rank's UNEVEN share of
* the heads -- 6 of 16 K heads where three ranks take 6, 5 and 5 -- is declared this way, and
* nothing is padded. Additive export: a plugin that does not call it declares as before. */
int rad_weight_shard_span_parts(RadBuilder* b, rad_weight w, const int64_t* lo, const int64_t* hi,
const int64_t* total, int n);
/* THIS RANK'S SHARE IS ZEROS. The weight is declared with its row or column shard and extent as
* on every other rank, and nothing is read for it: the loader writes zeros. It is for a rank the
* geometry gives no real heads (rad_arch.h: a world size the KV head count does not divide), so
* that rank runs the same program as its peers -- the scheduler sees one program -- and its
* projection contributes exactly nothing to the reduce. Additive export. */
int rad_weight_shard_zero(RadBuilder* b, rad_weight w);
#define RAD_BUFS(...) ((const rad_buf[]){ __VA_ARGS__ }), \
(int)(sizeof((const rad_buf[]){ __VA_ARGS__ }) / sizeof(rad_buf))
+3
View File
@@ -149,6 +149,9 @@ public:
int op_writes(rad_op h, const rad_buf* b, int n);
int buf_concurrent(rad_buf b);
int weight_shard_span(rad_weight w, int64_t lo, int64_t hi);
int weight_shard_span_parts(rad_weight w, const int64_t* lo, const int64_t* hi,
const int64_t* total, int n);
int weight_shard_zero(rad_weight w);
/* Finalise: weight first/last use, KV group geometry, buffer liveness and packing. Returns
* RAD_OK, or the first structural error class. Resolution MISSES are not an error here -- an
+58
View File
@@ -892,6 +892,53 @@ int Builder::weight_shard_span(rad_weight h, int64_t lo, int64_t hi) {
return RAD_OK;
}
int Builder::weight_shard_span_parts(rad_weight h, const int64_t* lo, const int64_t* hi,
const int64_t* total, int n) {
if (finished_) { err(RAD_E_STATE, "weight_shard_span_parts after declare finished"); return RAD_E_STATE; }
if (h == 0 || h >= prog_.weights.size() || !lo || !hi || !total) return RAD_E_INVAL;
WeightInfo& w = prog_.weights[h];
const RadWeightDecl& d = w.decl;
if (d.shard != RAD_SHARD_ROW || d.n_row_parts < 2 || n != d.n_row_parts) {
err(RAD_E_INVAL, "weight '%s': per-part spans need a row-sharded weight of %d stacked "
"parts, and %d span(s) were given", w.name.c_str(), d.n_row_parts, n);
return RAD_E_INVAL;
}
for (int g = 0; g < n; ++g) {
if (lo[g] < 0 || hi[g] <= lo[g] || hi[g] > total[g]) {
err(RAD_E_INVAL, "weight '%s': part %d span [%lld, %lld) is empty or outside its "
"%lld rows", w.name.c_str(), g, (long long)lo[g], (long long)hi[g],
(long long)total[g]);
return RAD_E_INVAL;
}
if (d.row_parts[g] != hi[g] - lo[g]) {
err(RAD_E_SHAPE, "weight '%s': part %d span [%lld, %lld) is %lld wide and the "
"declared part is %lld", w.name.c_str(), g, (long long)lo[g],
(long long)hi[g], (long long)(hi[g] - lo[g]), (long long)d.row_parts[g]);
return RAD_E_SHAPE;
}
w.part_lo[g] = lo[g];
w.part_hi[g] = hi[g];
w.part_total[g] = total[g];
}
w.n_part_spans = n;
w.shard_lo = 0;
w.shard_hi = 0;
return RAD_OK;
}
int Builder::weight_shard_zero(rad_weight h) {
if (finished_) { err(RAD_E_STATE, "weight_shard_zero after declare finished"); return RAD_E_STATE; }
if (h == 0 || h >= prog_.weights.size()) return RAD_E_INVAL;
WeightInfo& w = prog_.weights[h];
if (w.decl.shard != RAD_SHARD_ROW && w.decl.shard != RAD_SHARD_COL) {
err(RAD_E_INVAL, "weight '%s': a zero share on a weight that is not row- or "
"column-sharded", w.name.c_str());
return RAD_E_INVAL;
}
w.shard_zero = true;
return RAD_OK;
}
/* ================================================================== name map */
int Builder::decl_name_map(const RadNameMap* m) {
if (finished_) { err(RAD_E_STATE, "decl_name_map after declare finished"); return RAD_E_STATE; }
@@ -1615,6 +1662,17 @@ int rad_weight_shard_span(RadBuilder* b, rad_weight w, int64_t lo, int64_t hi) {
return B(b)->weight_shard_span(w, lo, hi);
}
int rad_weight_shard_span_parts(RadBuilder* b, rad_weight w, const int64_t* lo, const int64_t* hi,
const int64_t* total, int n) {
if (!b) return RAD_E_INVAL;
return B(b)->weight_shard_span_parts(w, lo, hi, total, n);
}
int rad_weight_shard_zero(RadBuilder* b, rad_weight w) {
if (!b) return RAD_E_INVAL;
return B(b)->weight_shard_zero(w);
}
int rad_decl_name_map(RadBuilder* b, const RadNameMap* m) {
if (!b) return RAD_E_INVAL;
return B(b)->decl_name_map(m);
+21 -6
View File
@@ -1979,11 +1979,15 @@ static int fill_from_checkpoint(const CkptWeight& cw, const std::vector<PlaneSha
std::string why;
for (size_t k = 0; k < shares.size(); ++k) {
const PlaneShare& sh = shares[k];
uint8_t* at = in + offs[k];
if (sh.zero) {
std::memset(at, 0, (size_t)sh.dense_bytes());
continue;
}
const RadEncPlane& p = cw.enc.plane[sel[k]];
int64_t pr = 0, pc = 0;
rad_enc_plane_dims(&p, cw.rows, cw.cols, &pr, &pc);
const int64_t crb = rad_enc_row_bytes(p.dtype, pc);
uint8_t* at = in + offs[k];
for (const PlaneShare::Rect& q : sh.rects) {
if (q.byte0 == 0 && q.bytes == sh.row_bytes) {
const int64_t off = q.row0 * sh.row_bytes, n = q.rows * sh.row_bytes;
@@ -2296,8 +2300,11 @@ int Engine::load_weights() {
/* FROM A CHECKPOINT, every weight is staged and its worker reads it: there is no
* sweep to deliver it, and the stored form is whatever the pool makes of the share --
* as it is, widened, or relaid. */
if (!e) {
* as it is, widened, or relaid. A ZERO SHARE (rad_weight_shard_zero) takes the same
* route from a container: nothing in the file is its source, so no sweep run can
* deliver it; its worker writes zeros and the pool stores them as it would any share. */
const bool zero_share = sh[0].zero;
if (!e || zero_share) {
StagedWeight sw;
sw.prog = &r->program;
sw.wi = i;
@@ -2322,9 +2329,17 @@ int Engine::load_weights() {
std::vector<int> sel(w.sel, w.sel + w.n_sel);
std::vector<int64_t> offs(sw.plane_off, sw.plane_off + w.n_sel);
const std::string name = w.name;
sw.fill = [cw, shares, sel, offs, name](uint8_t* in) -> int {
return fill_from_checkpoint(*cw, shares, sel, offs, name, in);
};
if (zero_share) {
const int64_t bytes = in_bytes;
sw.fill = [bytes](uint8_t* in) -> int {
std::memset(in, 0, (size_t)bytes);
return RAD_OK;
};
} else {
sw.fill = [cw, shares, sel, offs, name](uint8_t* in) -> int {
return fill_from_checkpoint(*cw, shares, sel, offs, name, in);
};
}
staged.push_back(std::move(sw));
++n_loaded;
continue;
+33 -2
View File
@@ -13,6 +13,23 @@ int weight_share(const WeightInfo& w, const RadEncoding& enc, uint32_t rank, con
return RAD_E_SHAPE;
};
if (w.n_sel < 1 || w.n_sel > RAD_ENC_MAX_PLANES) return refuse("no planes selected");
/* A ZERO SHARE: the declaration's own extents of every selected plane, and nothing to read.
* At one rank there is no share to zero -- the weight is whole and real. */
if (w.shard_zero && world > 1) {
for (int k = 0; k < w.n_sel; ++k) {
const RadEncPlane& p = enc.plane[w.sel[k]];
PlaneShare& s = out[k];
s = PlaneShare{};
s.dtype = p.dtype;
s.rows = w.sel_rows[k];
s.cols = w.sel_cols[k];
s.row_bytes = rad_enc_row_bytes(p.dtype, s.cols);
s.zero = true;
if (s.rows <= 0 || s.cols <= 0)
return refuse(fmt("a zero share of the '%s' plane has no extent", p.role));
}
return RAD_OK;
}
int64_t R = 0, C = 0;
rad_enc_view(rank, shape, &R, &C);
@@ -38,7 +55,7 @@ int weight_share(const WeightInfo& w, const RadEncoding& enc, uint32_t rank, con
struct VRect { int64_t r0, r1, c0, c1; };
std::vector<VRect> view;
const bool span = w.shard_hi > 0;
const bool span = w.shard_hi > 0 || w.n_part_spans > 0;
if (whole) {
view.push_back({ 0, VR, 0, VC });
} else {
@@ -74,7 +91,21 @@ int weight_share(const WeightInfo& w, const RadEncoding& enc, uint32_t rank, con
* a slice of EACH of them, not a contiguous slice of the stack. The parts are THIS
* RANK'S rows, so a part is `world` times as many rows in the entry. */
const int np = w.decl.n_row_parts > 1 ? w.decl.n_row_parts : 1;
if (span) {
if (w.n_part_spans > 0) {
/* PER-PART SPANS: the plugin stated each stacked part's rows in the container
* and this rank's [lo, hi) of it, so the parts need not be equal. */
if (w.n_part_spans != np)
return refuse(fmt("%d per-part spans for %d stacked parts", w.n_part_spans, np));
int64_t sum = 0, soff = 0;
for (int g = 0; g < np; ++g) sum += w.part_total[g];
if (sum != VR)
return refuse(fmt("the %d stacked parts' stated rows sum to %lld and the entry "
"has %lld", np, (long long)sum, (long long)VR));
for (int g = 0; g < np; ++g) {
view.push_back({ soff + w.part_lo[g], soff + w.part_hi[g], 0, VC });
soff += w.part_total[g];
}
} else if (span) {
if (VR % np) return refuse(fmt("%lld rows do not divide into %d parts",
(long long)VR, np));
const int64_t cpart = VR / np;
+3
View File
@@ -27,6 +27,9 @@ struct PlaneShare {
uint32_t dtype = RAD_DT_INVALID;
int64_t rows = 0, cols = 0; /* the share's extents, in the plane's elements */
int64_t row_bytes = 0;
/* rad_weight_shard_zero: the share has its extents and no rectangles; the loader writes
* dense_bytes() of zeros and reads nothing. */
bool zero = false;
int64_t dense_bytes() const; /* the share laid out dense: rows * a row of `cols` */
};
+12
View File
@@ -146,6 +146,13 @@ struct WeightInfo {
/* An uneven shard's slice of the split dimension (rad_weight_shard_span); hi 0 is the even
* split. */
int64_t shard_lo = 0, shard_hi = 0;
/* rad_weight_shard_span_parts: a span per stacked part and each part's rows in the
* container; n_part_spans 0 is shard_lo/hi (one span for every part) or the even share. */
int n_part_spans = 0;
int64_t part_lo[RAD_MAX_ROW_PARTS] = {0}, part_hi[RAD_MAX_ROW_PARTS] = {0};
int64_t part_total[RAD_MAX_ROW_PARTS] = {0};
/* rad_weight_shard_zero: this rank reads nothing for it and the loader writes zeros. */
bool shard_zero = false;
/* WHAT IT IS A VIEW OF (RadWeightDecl::source and ::planes, spec §4.3). `enc` is the source's
* encoding as the model holds it -- or, with nothing behind the builder, plain in the declared
@@ -201,6 +208,11 @@ struct WeightInfo {
WeightInfo& operator=(const WeightInfo& o) {
name = o.name; decl = o.decl; logical_bytes = o.logical_bytes;
shard_lo = o.shard_lo; shard_hi = o.shard_hi;
n_part_spans = o.n_part_spans;
for (int i = 0; i < RAD_MAX_ROW_PARTS; ++i) {
part_lo[i] = o.part_lo[i]; part_hi[i] = o.part_hi[i]; part_total[i] = o.part_total[i];
}
shard_zero = o.shard_zero;
source = o.source; enc = o.enc; enc_known = o.enc_known; n_sel = o.n_sel;
for (int i = 0; i < RAD_ENC_MAX_PLANES; ++i) {
sel[i] = o.sel[i]; sel_rows[i] = o.sel_rows[i]; sel_cols[i] = o.sel_cols[i];
+43
View File
@@ -1349,4 +1349,47 @@ TEST(the_gate_names_its_caller) {
CHECK(has(msg, "the container's layout"));
}
/* A span, per-part spans and a zero share have to SURVIVE THE WEIGHT LIST GROWING: WeightInfo's
* copy is hand-written (the atomic), so a field the copy leaves out is reset on every weight
* declared before the vector's last reallocation -- which is what the loader saw on rank 0 of a
* three-rank Flash-Next: layer 0's [a|b] with its per-part spans gone, refused as an even share. */
TEST(shard_spans_and_zero_survive_the_weight_list_reallocating) {
NEED_PLUGIN(reg)
Builder b(reg, make_meta(), make_ctx());
/* Three weights declared first and marked, then enough declared after them to grow the
* list several times over. */
RadWeightDecl d{};
d.dtype = RAD_BF16; d.rank = 2; d.shape[0] = 36; d.shape[1] = 64;
d.access = RAD_ACCESS_PER_TOKEN; d.shard = RAD_SHARD_ROW; d.group = rad_group_layer(0);
d.n_row_parts = 2; d.row_parts[0] = 18; d.row_parts[1] = 18;
const rad_weight ab = b.decl_weight("ab", &d);
REQUIRE(ab != 0);
const int64_t lo[2] = { 0, 0 }, hi[2] = { 18, 18 }, tot[2] = { 48, 48 };
CHECK_EQ(b.weight_shard_span_parts(ab, lo, hi, tot, 2), RAD_OK);
const rad_weight sp = decl_w(b, "span", 18, 64);
CHECK_EQ(b.weight_shard_span(sp, 6, 24), RAD_OK);
const rad_weight z = decl_w(b, "zero", 18, 64);
CHECK_EQ(b.weight_shard_zero(z), RAD_OK);
for (int i = 0; i < 300; ++i) {
char n[32]; snprintf(n, sizeof n, "w%d", i);
REQUIRE(decl_w(b, n, 64, 64) != 0);
}
const WeightInfo* w = b.program().weight(ab);
REQUIRE(w != nullptr);
CHECK_EQ(w->n_part_spans, 2);
CHECK_EQ((long long)w->part_lo[1], 0LL);
CHECK_EQ((long long)w->part_hi[1], 18LL);
CHECK_EQ((long long)w->part_total[1], 48LL);
w = b.program().weight(sp);
CHECK_EQ((long long)w->shard_lo, 6LL);
CHECK_EQ((long long)w->shard_hi, 24LL);
w = b.program().weight(z);
CHECK(w->shard_zero);
CHECK_EQ((int)b.errors().size(), 0);
}
RAD_TEST_MAIN()
+51
View File
@@ -163,6 +163,57 @@ TEST(an_uneven_span_is_the_rows_the_plugin_names) {
CHECK_EQ(s[1].rows, (int64_t)3);
}
/* PER-PART SPANS: a stacked weight whose parts are not the same width -- the delta net's
* [q|k|v|z] -- takes a span per part, each in its own part's rows. Three ranks over 16 K heads of
* 128: rank 1 takes K heads [6, 11) of q and of k, and V heads [18, 33) of 48 in v. */
TEST(per_part_spans_cut_each_stacked_part_by_its_own_rows) {
const RadEncoding e = rad_enc_plain(RAD_BF16);
/* entry: q 2048 | k 2048 | v 6144 rows; rank 1 of 3 declares 640 + 640 + 1920 */
WeightInfo w = decl(e, { 0 }, 2048 + 2048 + 6144, 64, RAD_SHARD_ROW, 3, { 640, 640, 1920 });
w.decl.shape[0] = 640 + 640 + 1920;
rad_enc_plane_dims(&e.plane[0], w.decl.shape[0], 64, &w.sel_rows[0], &w.sel_cols[0]);
w.n_part_spans = 3;
const int64_t lo[3] = { 768, 768, 2304 }, hi[3] = { 1408, 1408, 4224 };
const int64_t tot[3] = { 2048, 2048, 6144 };
for (int g = 0; g < 3; ++g) { w.part_lo[g] = lo[g]; w.part_hi[g] = hi[g]; w.part_total[g] = tot[g]; }
PlaneShare s[RAD_ENC_MAX_PLANES];
REQUIRE_EQ(share(w, 2048 + 2048 + 6144, 64, 3, 1, s), RAD_OK);
REQUIRE_EQ(s[0].rects.size(), (size_t)3);
CHECK_EQ(s[0].rects[0].row0, (int64_t)768);
CHECK_EQ(s[0].rects[0].rows, (int64_t)640);
CHECK_EQ(s[0].rects[1].row0, (int64_t)(2048 + 768));
CHECK_EQ(s[0].rects[2].row0, (int64_t)(4096 + 2304));
CHECK_EQ(s[0].rects[2].rows, (int64_t)1920);
CHECK_EQ(s[0].rows, (int64_t)3200);
/* totals that do not sum to the entry are refused */
w.part_total[2] = 6000;
std::string why;
CHECK_EQ(share(w, 2048 + 2048 + 6144, 64, 3, 1, s, &why), RAD_E_SHAPE);
CHECK(why.find("stated rows") != std::string::npos);
}
/* A ZERO SHARE has the declaration's extents and no rectangles: the loader writes zeros and reads
* nothing. At one rank there is nothing to zero. */
TEST(a_zero_share_is_the_declared_extent_and_no_rectangles) {
const RadEncoding e = fp8_block();
WeightInfo w = decl(e, { 0, 1 }, 1536, 128, RAD_SHARD_ROW, 3);
w.shard_zero = true;
PlaneShare s[RAD_ENC_MAX_PLANES];
REQUIRE_EQ(share(w, 1536, 128, 3, 2, s), RAD_OK);
CHECK(s[0].zero);
CHECK_EQ(s[0].rects.size(), (size_t)0);
CHECK_EQ(s[0].rows, (int64_t)512);
CHECK_EQ(s[0].cols, (int64_t)128);
CHECK_EQ(s[0].dense_bytes(), (int64_t)(512 * 128));
CHECK(s[1].zero);
CHECK_EQ(s[1].rows, (int64_t)4);
WeightInfo one = decl(e, { 0, 1 }, 1536, 128, RAD_SHARD_ROW, 1);
one.shard_zero = true;
REQUIRE_EQ(share(one, 1536, 128, 1, 0, s), RAD_OK);
CHECK(!s[0].zero);
CHECK_EQ(s[0].rows, (int64_t)1536);
}
/* A per-tensor scale is one value every rank carries whole, once, whatever the codes' cut. */
TEST(a_per_tensor_scale_is_carried_whole_by_every_rank) {
RadEncoding e;