builder: narrow an f32 source to a bf16 declaration at load
The builder accepted one conversion direction beyond the exact widening it already did: a single-plane RAD_F32 source declared RAD_BF16 is rounded to nearest even at load (WeightInfo::narrow, beside widen). It is staged like a widened weight, never read in place from the container, and the planner and oracle treat it as 'not the container's stored bytes'. build_test covers the declaration, the stored dtype and size, and that it is not file-direct.
This commit is contained in:
1 parent
fd40bb430c
commit
6c33ee4e12
6 files changed
+79
-32
No files matched your search
+17
-12
@@ -206,21 +206,24 @@ rad_weight Builder::decl_weight(const char* name, const RadWeightDecl* d) {
|
||||
|
||||
/* Check the declaration against the planes. One plane is declared at its own dtype and extents
|
||||
* (this rank's share under a shard); several at the codes' dtype and the logical extents. A
|
||||
* different dtype must be an exact widening the loader performs; a quantisation belongs to
|
||||
* different dtype must be an exact conversion the loader performs; a quantisation belongs to
|
||||
* rad-convert and is refused. */
|
||||
const RadEncPlane& p0 = w.enc.plane[w.sel[0]];
|
||||
if (p0.dtype != d->dtype) {
|
||||
const bool widen = w.n_sel == 1 && d->dtype == RAD_F32 &&
|
||||
(p0.dtype == RAD_BF16 || p0.dtype == RAD_F16);
|
||||
/* A bf16 readout head over an f32 checkpoint tensor: the loader rounds it to bf16. */
|
||||
const bool narrow = w.n_sel == 1 && d->dtype == RAD_BF16 && p0.dtype == RAD_F32;
|
||||
char e[256];
|
||||
rad_enc_format(&w.enc, e, sizeof e);
|
||||
if (!widen)
|
||||
if (!widen && !narrow)
|
||||
return err(RAD_E_DTYPE, "weight '%s' is declared %s, and '%s' holds %s as %s: only an "
|
||||
"exact widening is done at load -- anything else is a "
|
||||
"quantisation, which a recipe does before the container is "
|
||||
"written (spec §4.4)", n.c_str(), rad_dtype_name(d->dtype),
|
||||
"exact widening or narrowing is done at load; anything else "
|
||||
"is a quantisation, which a recipe does before the container "
|
||||
"is written (spec §4.4)", n.c_str(), rad_dtype_name(d->dtype),
|
||||
w.source.c_str(), p0.role, e);
|
||||
w.widen = true;
|
||||
w.widen = widen;
|
||||
w.narrow = narrow;
|
||||
}
|
||||
int64_t sr = 0, sc = 0; /* the source's logical [rows, cols] */
|
||||
if (w.enc_known) rad_enc_view(ws.rank, ws.shape, &sr, &sc);
|
||||
@@ -257,9 +260,10 @@ rad_weight Builder::decl_weight(const char* name, const RadWeightDecl* d) {
|
||||
* the resolved kernel's layout hook asks for another (spec §4.3); no hook, or
|
||||
* RAD_E_UNSUPPORTED, keeps this. */
|
||||
w.identity = true;
|
||||
w.stored_dtype = w.widen ? d->dtype : p0.dtype;
|
||||
w.stored_bytes = w.widen ? rad_dtype_bytes(d->dtype, vr * vc)
|
||||
: vr * rad_enc_row_bytes(p0.dtype, vc);
|
||||
const bool converted = w.widen || w.narrow;
|
||||
w.stored_dtype = converted ? d->dtype : p0.dtype;
|
||||
w.stored_bytes = converted ? rad_dtype_bytes(d->dtype, vr * vc)
|
||||
: vr * rad_enc_row_bytes(p0.dtype, vc);
|
||||
w.stored_rank = d->rank;
|
||||
for (uint32_t i = 0; i < RAD_MAX_RANK; ++i)
|
||||
w.stored_shape[i] = i < d->rank ? d->shape[i] : 0;
|
||||
@@ -2210,12 +2214,13 @@ void Builder::finish_layouts() {
|
||||
w.source.c_str());
|
||||
continue; /* the stored defaults decl_weight set stand */
|
||||
}
|
||||
if (w.widen) {
|
||||
if (w.widen || w.narrow) {
|
||||
err(RAD_E_DTYPE, "'%s' is declared %s over a %s plane and kernel %s "
|
||||
"relayouts it; a widening and a relayout are not both "
|
||||
"relayouts it; a %s and a relayout are not both "
|
||||
"done at load", w.name.c_str(),
|
||||
rad_dtype_name(w.decl.dtype),
|
||||
rad_dtype_name(w.enc.plane[w.sel[0]].dtype), ki->name);
|
||||
rad_dtype_name(w.enc.plane[w.sel[0]].dtype), ki->name,
|
||||
w.widen ? "widening" : "narrowing");
|
||||
continue;
|
||||
}
|
||||
w.layout_tag = tag;
|
||||
|
||||
+21
-14
@@ -1908,10 +1908,10 @@ struct LoadRun {
|
||||
/* ================================================================== the relayout pool
|
||||
*
|
||||
* Weights the loader must rearrange (spec §4.3): the resolved kernel stores something other than
|
||||
* the canonical planes (a fragment order, a per-row scale tail), or the declaration widens bf16 to
|
||||
* f32. The sweep copies the weight's planes into a host input; once all bytes arrive, a worker
|
||||
* produces the stored form, directly into its host pool slot or, for VRAM, through the worker's
|
||||
* pinned bounce buffers.
|
||||
* the canonical planes (a fragment order, a per-row scale tail), or the declaration converts bf16
|
||||
* to f32, or f32 to bf16. The sweep copies the weight's planes into a host input; once all bytes
|
||||
* arrive, a worker produces the stored form, directly into its host pool slot or, for VRAM,
|
||||
* through the worker's pinned bounce buffers.
|
||||
*
|
||||
* It must keep pace with the disk (Qwen3.8-27B-FP8 rearranges 24 GiB of 29 per load; one thread
|
||||
* runs at a fraction of 7 GB/s), so it is a thread pool and the sweep only reads. The copies out
|
||||
@@ -2248,8 +2248,8 @@ private:
|
||||
const int fs = s.fill(s.in);
|
||||
if (fs < 0) return fs;
|
||||
}
|
||||
/* Stored as read (only reached by a weight with no sweep). */
|
||||
if (w.identity && !w.widen) {
|
||||
/* Stored as read (only reached by a weight with no sweep, no dtype conversion). */
|
||||
if (w.identity && !w.widen && !w.narrow) {
|
||||
const int64_t n = w.stored_bytes;
|
||||
if (!s.dev) { std::memcpy(s.dst, s.in, (size_t)n); return RAD_OK; }
|
||||
const int us = b.upload(s.dst, s.in, n, s.device);
|
||||
@@ -2277,11 +2277,15 @@ private:
|
||||
|
||||
int st = RAD_OK;
|
||||
const char* who = "the loader";
|
||||
if (w.widen) {
|
||||
/* Exact: every bf16 and f16 value is representable in f32. */
|
||||
if (w.widen || w.narrow) {
|
||||
/* Exact: every bf16 and f16 value is representable in f32, and f32 rounds to bf16 to
|
||||
* nearest even. */
|
||||
const int64_t cnt = w.sel_rows[0] * w.sel_cols[0];
|
||||
if (cnt * 4 != n) st = RAD_E_SHAPE;
|
||||
else for (int64_t e = 0; e < cnt; ++e) ((float*)out)[e] = rad_load_f32(pt[0].data, pt[0].dtype, e);
|
||||
const int64_t esz = (int64_t)rad_dtype_bytes(w.stored_dtype, 1);
|
||||
if (cnt * esz != n) st = RAD_E_SHAPE;
|
||||
else for (int64_t e = 0; e < cnt; ++e)
|
||||
rad_store_f32(out, w.stored_dtype, e,
|
||||
rad_load_f32(pt[0].data, pt[0].dtype, e));
|
||||
} else {
|
||||
const Resolved& r = s.prog->ops[(size_t)w.lay_op].bands[(size_t)w.lay_band].dom[w.lay_dom];
|
||||
const RadKernelInfo* ki = r.row->info;
|
||||
@@ -2744,8 +2748,10 @@ int Engine::load_weights() {
|
||||
RAD_ERR("rank %d: '%s' was placed on the %s tier, which reads the container where "
|
||||
"it lies, and its stored form is not the container's (%s)", r->index,
|
||||
w.name.c_str(), tier_name(w.tier),
|
||||
w.widen ? "widened at load" : w.identity ? "a shard of it"
|
||||
: w.layout_tag.c_str());
|
||||
w.widen ? "widened at load"
|
||||
: w.narrow ? "narrowed at load"
|
||||
: w.identity ? "a shard of it"
|
||||
: w.layout_tag.c_str());
|
||||
return RAD_E_STATE;
|
||||
}
|
||||
|
||||
@@ -2862,7 +2868,8 @@ int Engine::load_weights() {
|
||||
total += sh[k].dense_bytes();
|
||||
}
|
||||
sw.in_bytes = in_bytes;
|
||||
if (w.identity && !w.widen && sh[0].dense_bytes() != w.stored_bytes) {
|
||||
if (w.identity && !w.widen && !w.narrow &&
|
||||
sh[0].dense_bytes() != w.stored_bytes) {
|
||||
RAD_ERR("rank %d: '%s': this rank's share is %lld bytes and the slot %lld",
|
||||
r->index, w.name.c_str(), (long long)sh[0].dense_bytes(),
|
||||
(long long)w.stored_bytes);
|
||||
@@ -2884,7 +2891,7 @@ int Engine::load_weights() {
|
||||
* by plane for the relayout pool, which writes the slot. */
|
||||
int32_t stage = -1;
|
||||
int64_t in_bytes = 0;
|
||||
if (!w.identity || w.widen) {
|
||||
if (!w.identity || w.widen || w.narrow) {
|
||||
stage = (int32_t)staged.size();
|
||||
staged.emplace_back();
|
||||
StagedWeight& sw = staged.back();
|
||||
|
||||
@@ -313,7 +313,7 @@ void Planner::pin_mapped(const Program& prog, Plan* p) {
|
||||
"directly has none; convert it to a container");
|
||||
else if (!stored)
|
||||
note_unfit(p, mu, "it must be read from the container's mapping, and its stored form "
|
||||
"is not the container's (a relayout, a widening or a shard)");
|
||||
"is not the container's (a relayout, a dtype conversion or a shard)");
|
||||
if (host_only)
|
||||
for (rad_weight w : mu.weights)
|
||||
if (w < device_ok_.size() && device_ok_[w])
|
||||
@@ -698,8 +698,8 @@ int Planner::assign_expert_tiered(Plan* p) {
|
||||
/* With the file tier on, only a non-file-direct unit gets here (HostFall::place). */
|
||||
note_unfit(p, mu, ssd_ ? "neither VRAM nor the host pool has room, and its "
|
||||
"stored form is not the container's (a relayout, a "
|
||||
"widening or a shard), so the file tier cannot read it "
|
||||
"in place"
|
||||
"dtype conversion or a shard), so the file tier cannot "
|
||||
"read it in place"
|
||||
: "the host pool is full and no --weights-disk-tier was "
|
||||
"given");
|
||||
rc = RAD_E_NOMEM;
|
||||
|
||||
+5
-2
@@ -189,11 +189,13 @@ struct WeightInfo {
|
||||
/* Filled by the layout pass: what the resolved kernel stores. `identity` is the selected
|
||||
* plane's bytes unchanged, the only form the file and mapped tiers can serve since they read
|
||||
* the container in place; `widen` is identity plus an exact widening (a bf16 norm read as
|
||||
* f32) done by the loader. Otherwise the kernel at (lay_op, lay_band, lay_dom) relayouts it at
|
||||
* f32) done by the loader, and `narrow` an exact narrowing (an f32 readout head served as
|
||||
* bf16). Otherwise the kernel at (lay_op, lay_band, lay_dom) relayouts it at
|
||||
* load, as operand `lay_operand`. */
|
||||
std::string layout_tag;
|
||||
bool identity = true;
|
||||
bool widen = false;
|
||||
bool narrow = false;
|
||||
int32_t lay_op = -1, lay_band = -1, lay_dom = -1, lay_operand = -1;
|
||||
uint32_t stored_dtype = RAD_DT_INVALID;
|
||||
uint32_t stored_rank = 0;
|
||||
@@ -238,7 +240,8 @@ inline void weight_planes(const WeightInfo& w, RadTensor* t) {
|
||||
* the mapped tier's pointer): only when they are the selected plane unchanged and this rank holds
|
||||
* all of it. A relayout is not in the file, and a column shard is not one contiguous range. */
|
||||
inline bool weight_file_direct(const WeightInfo& w, int world_size) {
|
||||
return w.identity && !w.widen && (w.decl.shard == RAD_SHARD_NONE || world_size <= 1);
|
||||
return w.identity && !w.widen && !w.narrow &&
|
||||
(w.decl.shard == RAD_SHARD_NONE || world_size <= 1);
|
||||
}
|
||||
|
||||
struct BufferInfo {
|
||||
|
||||
@@ -385,7 +385,7 @@ bool Ctx::oracle_unrelayout(rad_op h, const IssuePlan& hip, const IssuePlan& dip
|
||||
w.layout_tag.c_str(), ki->unrelayout ? "RadShapeFn" : "RadUnrelayoutFn");
|
||||
return false;
|
||||
}
|
||||
if (!o.tab[i] && hk && !w.widen) {
|
||||
if (!o.tab[i] && hk && !w.widen && !w.narrow) {
|
||||
RadTensor pt[RAD_ENC_MAX_PLANES];
|
||||
weight_planes(w, pt);
|
||||
RadLayout hl{};
|
||||
|
||||
@@ -1826,6 +1826,38 @@ TEST(a_widened_weight_cannot_also_be_relayouted) {
|
||||
CHECK(any_error(b, "a widening and a relayout"));
|
||||
}
|
||||
|
||||
/* The other direction, the d3 readout: an f32 checkpoint tensor declared bf16 (what the device
|
||||
* logits kernel reads) is narrowed by the loader, and the stored form is one plain bf16 plane. */
|
||||
TEST(an_f32_source_declared_bf16_is_narrowed_at_load) {
|
||||
NEED_EXTRA(reg)
|
||||
Builder b(reg, make_meta(), make_ctx());
|
||||
b.set_sources([](const std::string& name, const Program&, WeightSource* out) -> int {
|
||||
if (name != "w.readout") return RAD_E_NOTFOUND;
|
||||
out->enc = rad_enc_plain(RAD_F32);
|
||||
out->rank = 2;
|
||||
out->shape[0] = 32;
|
||||
out->shape[1] = 64;
|
||||
return RAD_OK;
|
||||
});
|
||||
RadWeightDecl d{};
|
||||
d.dtype = RAD_BF16;
|
||||
d.rank = 2;
|
||||
d.shape[0] = 32;
|
||||
d.shape[1] = 64;
|
||||
d.access = RAD_ACCESS_PER_TOKEN;
|
||||
d.group = rad_group_layer(0);
|
||||
const rad_weight w = b.decl_weight("w.readout", &d);
|
||||
REQUIRE(w != 0);
|
||||
CHECK_OK(b.finish());
|
||||
CHECK(b.errors().empty());
|
||||
const WeightInfo* wi = b.program().weight(w);
|
||||
CHECK(wi->narrow);
|
||||
CHECK(!wi->widen);
|
||||
CHECK_EQ(wi->stored_dtype, (uint32_t)RAD_BF16);
|
||||
CHECK_EQ((long long)wi->stored_bytes, 32LL * 64LL * 2);
|
||||
CHECK(!weight_file_direct(*wi, 1));
|
||||
}
|
||||
|
||||
/* ================================================================== the sizing declare */
|
||||
|
||||
/* A smaller arena level is declared again at a smaller max_tok and takes the real declare's bands,
|
||||
|
||||
Reference in new issue
Block a user