diff --git a/docs/TOOLS.md b/docs/TOOLS.md index 6ff59b06..8d2d9508 100644 --- a/docs/TOOLS.md +++ b/docs/TOOLS.md @@ -443,8 +443,8 @@ comparison with the recording runs either way. `--no-cache` runs it regardless. Against a container (`-m`), weight operands are read from it: each weight's canonical planes, which the kernel under test gets through its own `layout`/`relayout` hooks and the oracle gets -decoded, and every expert of a weight table. This is the only test of the artifact and of the -layout hooks. +decoded, and every expert of a weight table, laid out expert by expert and stacked. This is the +only test of the artifact and of the layout hooks. ```sh rad-kbench -m flashnext-w4.rad --home $RADIANCE_HOME --bench --report kbench.md diff --git a/tests/fixture_test.cpp b/tests/fixture_test.cpp index 8d63d0f0..58735305 100644 --- a/tests/fixture_test.cpp +++ b/tests/fixture_test.cpp @@ -1240,6 +1240,52 @@ TEST(a_weight_tables_experts_are_drawn_from_its_encoding) { CHECK(via_draw == cb); } +/* A kernel that lays out its experts (libr4d's 4-bit tables) is handed each expert's arrangement of + * the planes kf_draw_table decodes, stacked one laid expert apart; a kernel with no layout reads the + * decoded table, and a layout that changes an expert's size cannot be stacked. */ +TEST(a_weight_tables_experts_are_each_laid_out_by_the_kernel) { + KfOperand codes = fp8_weight(128, 256, { "codes" }); + codes.role = RAD_OPD_WTAB; + codes.shape.insert(codes.shape.begin(), 3); + const KfEntryPlanes drawn = [&](int64_t e, std::vector>* pl) { + kf_draw_entry_planes(codes, 2, e, pl); + return (int)RAD_OK; + }; + RadKernelInfo ki{}; + RadLayout lay{}; + std::vector out; + CHECK_EQ(kf_lay_table(&ki, nullptr, 0, 2, codes, drawn, &lay, &out), RAD_E_UNSUPPORTED); + ki.layout = lay_identity; + CHECK_EQ(kf_lay_table(&ki, nullptr, 0, 2, codes, drawn, &lay, &out), RAD_E_UNSUPPORTED); + ki.layout = lay_reverse; + ki.relayout = relay_reverse; + REQUIRE_EQ(kf_lay_table(&ki, nullptr, 0, 2, codes, drawn, &lay, &out), RAD_OK); + const size_t per = (size_t)128 * 256; + REQUIRE_EQ(out.size(), 3 * per); + CHECK_EQ(lay.bytes, (int64_t)per); + std::vector> pl; + for (int64_t e = 0; e < 3; ++e) { + kf_draw_entry_planes(codes, 2, e, &pl); + CHECK(std::equal(out.begin() + (long)(per * e), out.begin() + (long)(per * (e + 1)), + pl[0].rbegin())); + } + /* The entries the kernel reads are the ones the oracle decodes. */ + std::vector logical; + std::string why; + REQUIRE_EQ(kf_draw_table(codes, 2, &logical, &why), RAD_OK); + kf_draw_entry_planes(codes, 2, 1, &pl); + CHECK(std::equal(logical.begin() + (long)per, logical.begin() + (long)(2 * per), + pl[0].begin())); + /* Laid out to a size the declared expert does not stride by. */ + ki.layout = [](const RadParam*, int, int, const RadEncoding*, const int*, + const RadTensor* t, int, RadLayout* o) { + o->dtype = t[0].dtype; + o->bytes = rad_dtype_bytes(t[0].dtype, t[0].shape[0] * t[0].shape[1]) + 256; + return (int)RAD_OK; + }; + CHECK_EQ(kf_lay_table(&ki, nullptr, 0, 2, codes, drawn, &lay, &out), RAD_E_SHAPE); +} + /* ================================================================== routing */ /* moe_scatter's offsets are a prefix array: non-decreasing from 0 to the row count. A uniform draw diff --git a/tools/kfixture.cpp b/tools/kfixture.cpp index 2cb619e2..a33e9b93 100644 --- a/tools/kfixture.cpp +++ b/tools/kfixture.cpp @@ -678,6 +678,19 @@ uint64_t kf_weight_seed(const KfOperand& d, size_t k) { return rad_splitmix(s); } +KfOperand kf_table_entry(const KfOperand& d) { + KfOperand one = d; + one.role = RAD_OPD_WEIGHT; + if (!d.shape.empty()) one.shape.assign(d.shape.begin() + 1, d.shape.end()); + return one; +} + +void kf_draw_entry_planes(const KfOperand& d, size_t k, int64_t e, + std::vector>* out) { + uint64_t es = kf_weight_seed(d, k) ^ (0xC2B2AE3D27D4EB4Full * (uint64_t)(e + 1)); + kf_draw_planes(kf_table_entry(d), rad_splitmix(es), out); +} + int kf_draw_table(const KfOperand& d, size_t k, std::vector* out, std::string* why) { out->clear(); if (!d.has_enc || d.shape.size() < 2 || d.shape[0] <= 0) { @@ -686,21 +699,17 @@ int kf_draw_table(const KfOperand& d, size_t k, std::vector* out, std:: } const int64_t ne = d.shape[0]; /* One expert: the table's selection over its shape without the leading dimension. */ - KfOperand one = d; - one.role = RAD_OPD_WEIGHT; - one.shape.assign(d.shape.begin() + 1, d.shape.end()); + const KfOperand one = kf_table_entry(d); const int64_t per = rad_dtype_bytes(d.dtype, rad_numel(one.shape)); if (per <= 0) { if (why) *why = "a dtype the core cannot size"; return RAD_E_DTYPE; } out->assign((size_t)(per * ne), 0); - const uint64_t base = kf_weight_seed(d, k); std::vector> pl; std::vector logical; for (int64_t e = 0; e < ne; ++e) { - uint64_t es = base ^ (0xC2B2AE3D27D4EB4Full * (uint64_t)(e + 1)); - kf_draw_planes(one, rad_splitmix(es), &pl); + kf_draw_entry_planes(d, k, e, &pl); std::string w; const int rc = kf_planes_logical(one, pl, &logical, &w); if (rc != RAD_OK || (int64_t)logical.size() != per) { @@ -714,6 +723,38 @@ int kf_draw_table(const KfOperand& d, size_t k, std::vector* out, std:: return RAD_OK; } +int kf_lay_table(const RadKernelInfo* info, const RadParam* p, int n_p, int operand, + const KfOperand& d, const KfEntryPlanes& planes, RadLayout* lay, + std::vector* out) { + out->clear(); + if (!info || !info->layout || !d.has_enc) return RAD_E_UNSUPPORTED; + if (d.shape.size() < 2 || d.shape[0] <= 0) return RAD_E_SHAPE; + const KfOperand one = kf_table_entry(d); + std::vector> pl; + std::vector laid; + for (int64_t e = 0; e < d.shape[0]; ++e) { + int rc = planes(e, &pl); + if (rc != RAD_OK) return rc; + RadLayout le{}; + rc = kf_lay_weight(info, p, n_p, operand, one, pl, &le, &laid); + if (rc != RAD_OK) return rc; + if (e == 0) { + /* The kernel strides a stacked table by the declared entry in the stored dtype, so a + * layout that changes an entry's size (an fp8 lm_head's appended scales) cannot be + * stacked. A plugin-private dtype has no size the core knows; its kernel reads the + * entry it laid out. */ + const int64_t declared = rad_dtype_bytes(le.dtype, rad_numel(one.shape)); + if (declared > 0 && declared != le.bytes) return RAD_E_SHAPE; + *lay = le; + out->reserve((size_t)(le.bytes * d.shape[0])); + } else if (le.bytes != lay->bytes || le.dtype != lay->dtype) { + return RAD_E_SHAPE; + } + out->insert(out->end(), laid.begin(), laid.end()); + } + return RAD_OK; +} + void kf_operand_enc(const WeightInfo& w, KfOperand* d) { d->has_enc = true; d->enc = w.enc; diff --git a/tools/kfixture.h b/tools/kfixture.h index 6d61a657..cac9c746 100644 --- a/tools/kfixture.h +++ b/tools/kfixture.h @@ -23,6 +23,7 @@ #include "rad_core.h" #include +#include #include #include @@ -189,6 +190,27 @@ uint64_t kf_weight_seed(const KfOperand& d, size_t k); * refusal with `why`. */ int kf_draw_table(const KfOperand& d, size_t k, std::vector* out, std::string* why); +/* One entry of a weight table: the table's encoding and selection over its shape without the + * leading expert dimension, as a weight operand. */ +KfOperand kf_table_entry(const KfOperand& d); + +/* Entry e's planes as kf_draw_table draws them. */ +void kf_draw_entry_planes(const KfOperand& d, size_t k, int64_t e, + std::vector>* out); + +/* Entry e's selected planes, from the fixture's draw or a container. RAD_OK, or the refusal. */ +using KfEntryPlanes = std::function>* out)>; + +/* The bytes kernel `info` reads for the weight table `operand`: every entry laid out from its + * planes as kf_lay_weight lays out a weight, stacked one laid entry apart, which is how a kernel + * reads a table it is handed whole rather than through the engine's pointers. `lay` is the first + * entry's. Returns kf_lay_weight's answer for the entries: RAD_E_UNSUPPORTED when the kernel reads + * them as decoded (kf_draw_table's bytes); RAD_E_SHAPE when the entries lay out to different + * sizes, or to a size the declared entry does not stride by. */ +int kf_lay_table(const RadKernelInfo* info, const RadParam* p, int n_p, int operand, + const KfOperand& d, const KfEntryPlanes& planes, RadLayout* lay, + std::vector* out); + /* ================================================================== drawing an input * * One implementation for recorder and reader, which must produce byte-identical inputs. An index diff --git a/tools/rad_kbench.cpp b/tools/rad_kbench.cpp index c177fcdb..25407a8e 100644 --- a/tools/rad_kbench.cpp +++ b/tools/rad_kbench.cpp @@ -983,6 +983,8 @@ private: const std::vector& in, uint64_t input_hash, const Run& run); bool lay_out(const KfCase& c, size_t k, const RadOpSchema* schema, const Run& run, const Drawn& d, Bytes* laid, RadTensor* desc); + bool lay_out_table(const KfCase& c, size_t k, const RadOpSchema* schema, const Run& run, + Bytes* laid, RadTensor* desc); int record_case(const KfCase& base, KfCase* out, std::string* why); int rerecord(); int record_rows(); @@ -1364,7 +1366,8 @@ static bool lays_out_weight(const KernelRow* row, const Geometry& g, const KfCas if (!info || !info->layout) return false; for (size_t k = 0; k < c.opd.size(); ++k) { const KfOperand& d = c.opd[k]; - if (d.absent || d.role != RAD_OPD_WEIGHT || !d.has_enc) continue; + if (d.absent || (d.role != RAD_OPD_WEIGHT && d.role != RAD_OPD_WTAB) || !d.has_enc) + continue; RadTensor pt[RAD_ENC_MAX_PLANES]; const int n = (int)d.sel.size(); if (n < 1 || n > RAD_ENC_MAX_PLANES) continue; @@ -1407,6 +1410,34 @@ bool Replay::lay_out(const KfCase& c, size_t k, const RadOpSchema* schema, const return true; } +/* The same for a weight table whose kernel lays out each expert: the experts' planes as the draw + * made them (kf_draw_entry_planes), each laid out, stacked. `laid` empty: the row reads the table + * as decoded. */ +bool Replay::lay_out_table(const KfCase& c, size_t k, const RadOpSchema* schema, const Run& run, + Bytes* laid, RadTensor* desc) { + PhaseTimer pt(&ph_, kb::PH_LAYOUT); + laid->clear(); + const KfOperand& d = c.opd[k]; + if (!d.has_enc || d.undrawn) return true; + RadLayout lay{}; + const int lrc = kf_lay_table(run.row->info, run.g.params(), run.g.n_params(), (int)k, d, + [&](int64_t e, std::vector* pl) { + kf_draw_entry_planes(d, k, e, pl); + return (int)RAD_OK; + }, + &lay, laid); + if (lrc == RAD_E_UNSUPPORTED) { laid->clear(); return true; } + if (lrc < 0) { + const char* opd_name = schema->operands[k].name ? schema->operands[k].name : "?"; + nm_.skipped[c.op + " (" + run.lib + "/" + run.row->info->name + " cannot lay out the " + + "table '" + opd_name + "' from " + enc_name(d.enc) + ": " + + rad_strerror(lrc) + ")"]++; + return false; + } + kf_describe_laid(d, lay, laid->data(), desc); + return true; +} + void Replay::run_one(size_t fi, const KfCase& c, const RadOpSchema* schema, const std::vector& in, uint64_t input_hash, const Run& run) { const size_t n_opd = c.opd.size(); @@ -1504,11 +1535,18 @@ void Replay::run_one(size_t fi, const KfCase& c, const RadOpSchema* schema, } continue; } - if (!guarded_alloc(dev[k], (int64_t)src.size(), stream)) { + Bytes laid; + if (c.opd[k].role == RAD_OPD_WTAB && + !lay_out_table(c, k, schema, run, &laid, &t[k])) { + ++st_.skipped; release(); return; + } + const Bytes& up = laid.empty() ? src : laid; + if (!guarded_alloc(dev[k], (int64_t)up.size(), stream)) { nm_.skipped[c.op + " (no device memory for this case)"]++; ++st_.skipped; release(); return; } - if (rad_memcpy_async(dev[k].p, src.data(), dev[k].bytes, stream) < 0) { + if (rad_memcpy_async(dev[k].p, up.data(), dev[k].bytes, stream) < 0 || + (!laid.empty() && rad_stream_sync(stream) < 0)) { ++st_.skipped; release(); return; } t[k].data = dev[k].p; @@ -1518,11 +1556,12 @@ void Replay::run_one(size_t fi, const KfCase& c, const RadOpSchema* schema, /* Host: weights laid out in place, written operands copied, inputs shared. */ for (size_t k = 0; k < n_opd; ++k) { if (c.opd[k].absent || !in[k].bytes) continue; - if (c.opd[k].role == RAD_OPD_WEIGHT) { + if (c.opd[k].role == RAD_OPD_WEIGHT || c.opd[k].role == RAD_OPD_WTAB) { Bytes laid; - if (!lay_out(c, k, schema, run, in[k], &laid, &t[k])) { - ++st_.skipped; return; - } + const bool ok = c.opd[k].role == RAD_OPD_WEIGHT + ? lay_out(c, k, schema, run, in[k], &laid, &t[k]) + : lay_out_table(c, k, schema, run, &laid, &t[k]); + if (!ok) { ++st_.skipped; return; } if (!laid.empty()) { own[k].swap(laid); t[k].data = own[k].data(); } continue; } @@ -3020,6 +3059,49 @@ int main(int argc, char** argv) { } if (read) from_container = true; else kf_draw(kop, k, M, kf_vocab_rows(kop), 0, &bufs[k]); + /* A kernel that lays its experts out (libr4d's 4-bit ones) reads each + * expert's arrangement of the same planes, stacked; the oracle reads the + * table they decode to. */ + if (kop[k].has_enc && !kop[k].undrawn) { + const char* opd_name = o.schema->operands[k].name + ? o.schema->operands[k].name : "?"; + auto planes = [&](int64_t e, std::vector>* pl) { + if (!read) { + kf_draw_entry_planes(kop[k], k, e, pl); + return (int)RAD_OK; + } + const WeightInfo& wi = prog.weights[run[(size_t)e]]; + const RadFileEntry* fe = f.find(wi.source); + if (!fe) return (int)RAD_E_NOTFOUND; + pl->clear(); + for (int j = 0; j < wi.n_sel; ++j) { + const RadFilePlane& fp = f.plane(*fe, wi.sel[j]); + const uint8_t* src = (const uint8_t*)f.data(fp); + pl->emplace_back(src, src + fp.bytes); + } + return (int)RAD_OK; + }; + RadLayout lay{}; + std::vector laid; + const int lrc = kf_lay_table(r.row->info, g.params(), g.n_params(), + (int)k, kop[k], planes, &lay, &laid); + if (lrc == RAD_OK) { + ref_out[k] = bufs[k]; + t_ref[k] = mk(ref_out[k]); + bufs[k].swap(laid); + kf_describe_laid(kop[k], lay, bufs[k].data(), &t_test[k]); + continue; + } + if (lrc != RAD_E_UNSUPPORTED) { + skipped_ops[o.op + " (" + std::string(r.row->info->name) + + " cannot lay out the table '" + opd_name + + "' from " + + enc_name(kop[k].enc) + ": " + rad_strerror(lrc) + + ")"]++; + unusable = true; + break; + } + } } else if (role == RAD_OPD_IN || role == RAD_OPD_INOUT) { /* The fixture's draw, the single implementation recorder and replay * share. */