From b6cf2b357429c2a6a0d12b8440b2b4bc5d123bff Mon Sep 17 00:00:00 2001 From: Brandon Bertelsen Date: Thu, 29 Oct 2026 00:28:47 -0400 Subject: [PATCH] laya: rad-convert rounds Laya's f16 to bf16 itself; the tiny test is f16 --- data/recipes/laya-bf16.recipe | 21 --------------------- docs/GUIDE.md | 8 ++++---- tests/data/golden/laya_tiny.json | 4 ++-- tests/laya_decision_test.cpp | 9 +++++++++ tools/rad_convert.cpp | 26 ++++++++++++++++++++++++++ 5 files changed, 41 insertions(+), 27 deletions(-) delete mode 100644 data/recipes/laya-bf16.recipe diff --git a/data/recipes/laya-bf16.recipe b/data/recipes/laya-bf16.recipe deleted file mode 100644 index dd90af3a..00000000 --- a/data/recipes/laya-bf16.recipe +++ /dev/null @@ -1,21 +0,0 @@ -# laya-bf16.recipe -- Convai Innovations' Laya (laya, laya-typed-decisions, laya-multilingual) for the -# laya plugin (arch/laya). -# -# rad-convert convaiinnovations/laya-typed-decisions --recipe data/recipes/laya-bf16.recipe \ -# -o laya-typed-decisions.rad -# -# The release ships f16. The plugin reads its linears and tables in bf16, as the release computes them -# on a card under bf16 autocast, and f16 to bf16 rounds, so it is a rule here. Norms and biases are -# declared f32, an exact widening of f16, and no rule names them. -token_embd.weight cast dtype=bf16 -type_emb.weight cast dtype=bf16 -scorer.fc1.weight cast dtype=bf16 -scorer.fc2.weight cast dtype=bf16 -blk.*.attn_qkv.weight cast dtype=bf16 -blk.*.attn_out.weight cast dtype=bf16 -blk.*.ffn_in_gate.weight cast dtype=bf16 -blk.*.ffn_down.weight cast dtype=bf16 -head.*.attn_qkv.weight cast dtype=bf16 -head.*.attn_out.weight cast dtype=bf16 -head.*.ffn_up.weight cast dtype=bf16 -head.*.ffn_down.weight cast dtype=bf16 diff --git a/docs/GUIDE.md b/docs/GUIDE.md index c087d885..55c362b0 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -2851,8 +2851,7 @@ runs the encoder and head in the `laya` plugin (`arch/laya`), on the embedding p every question of a request in one step, each answered in its pooled row. ```sh -rad-convert /srv/models/laya-typed-decisions -o laya-typed-decisions.rad --set decision.type=laya \ - --recipe data/recipes/laya-bf16.recipe +rad-convert /srv/models/laya-typed-decisions -o laya-typed-decisions.rad --set decision.type=laya radiance --model laya-typed-decisions.rad --port 8000 ``` @@ -2860,8 +2859,9 @@ rad-convert reads the release's folders: `encoder/config.json` for the geometry, `rl_agent_config.json` for the sequence budget and the fitted temperatures, and `tokenizer/`. The checkpoint's `config.json` already says `model_type: laya`, so `--set decision.type=laya` is optional. A checkpoint as the `laya` package downloads it, with no `config.json` of its own, -converts the same way. The release ships f16 weights, and the recipe rounds the linears and tables -to bf16, which is how the release computes them on a card. +converts the same way. The release ships f16 weights. rad-convert rounds the linears and tables to +bf16 itself, which is how the release computes them on a card, and records the rule in the +container's recipe. The request and the response are the same as d1's above. What differs: diff --git a/tests/data/golden/laya_tiny.json b/tests/data/golden/laya_tiny.json index 22f858e8..3a611ffc 100644 --- a/tests/data/golden/laya_tiny.json +++ b/tests/data/golden/laya_tiny.json @@ -1,5 +1,5 @@ { - "weights_fnv": "230e9131e87efe05", + "weights_fnv": "e1a5d79faebbea22", "laya_commit": "fe9a6c3502", "requests": [ { @@ -120,7 +120,7 @@ "confidence": 0.0081, "answer_confidence": 0.3787, "action": { - "act_probability": 0.6045 + "act_probability": 0.6046 } }, "tone": { diff --git a/tests/laya_decision_test.cpp b/tests/laya_decision_test.cpp index 14c07015..5db76f66 100644 --- a/tests/laya_decision_test.cpp +++ b/tests/laya_decision_test.cpp @@ -82,6 +82,15 @@ bool write_tiny_laya(const std::string& dir, uint64_t seed) { lin("act_head.2.weight", 2, A); bias("act_head.2.bias", 2); ts.push_back(tiny::noise("temperature", { 3 }, r, 0.0f, 1.0f, "F32")); + /* f16, as the release and laya's trainer save it, so the convert's rounding to bf16 is tested. */ + for (tiny::Tensor& t : ts) { + if (t.dtype != "BF16") continue; + const int64_t n = tiny::numel(t.shape); + std::vector h((size_t)n * 2); + for (int64_t i = 0; i < n; ++i) rad_store_f32(h.data(), RAD_F16, i, rad_load_f32(t.bytes.data(), RAD_BF16, i)); + t.bytes = std::move(h); + t.dtype = "F16"; + } if (!tiny::write_safetensors(dir + "/model.safetensors", ts)) return false; nlohmann::ordered_json root; diff --git a/tools/rad_convert.cpp b/tools/rad_convert.cpp index ed71cb45..f5e1dd8a 100644 --- a/tools/rad_convert.cpp +++ b/tools/rad_convert.cpp @@ -123,6 +123,23 @@ int clef_metadata(const std::string& in, const std::string& config_dir, const Ch return 0; } +/* The rounding every Laya checkpoint needs: the release ships f16 (laya.train saves .half()), and + * the laya plugin reads its linears and tables in bf16, as the release computes them on a card. + * Norms and biases are declared f32, an exact widening of f16, so no rule names them. */ +const char* const kLayaBf16 = + "token_embd.weight cast dtype=bf16\n" + "type_emb.weight cast dtype=bf16\n" + "scorer.fc1.weight cast dtype=bf16\n" + "scorer.fc2.weight cast dtype=bf16\n" + "blk.*.attn_qkv.weight cast dtype=bf16\n" + "blk.*.attn_out.weight cast dtype=bf16\n" + "blk.*.ffn_in_gate.weight cast dtype=bf16\n" + "blk.*.ffn_down.weight cast dtype=bf16\n" + "head.*.attn_qkv.weight cast dtype=bf16\n" + "head.*.attn_out.weight cast dtype=bf16\n" + "head.*.ffn_up.weight cast dtype=bf16\n" + "head.*.ffn_down.weight cast dtype=bf16\n"; + /* Laya (a checkpoint whose config.json says model_type laya, or --set decision.type=laya): the * release keeps its parts in folders, so the encoder's geometry comes from encoder/config.json, * the head's settings from rl_agent_config.json and the vocabulary from tokenizer/. The plugin @@ -605,6 +622,15 @@ int main(int argc, char** argv) { (meta.arch == "laya" || (dt && !std::strcmp(dt, "laya"))) && laya_metadata(in, config_dir, meta, &tokjson) < 0) return 1; + /* f16 to bf16 rounds, so it is a recipe's rule (spec ยง4.4). Every Laya checkpoint needs the same + * rules, so they follow any the caller gave, and the container records them with the rest. */ + if (meta.arch == "laya") { + std::string why; + if (recipe.add_text(kLayaBf16, "laya's f16 to bf16 (built in)", &why) < 0) { + RAD_ERR("%s", why.c_str()); + return 1; + } + } /* The quantisation descriptor (which selects the plugin pair) is derived from * quantization_config, so a --set of it re-derives the descriptor. E.g. a bf16 checkpoint * whose recipe writes Qwen's block fp8 targets qwen35_fp8 by setting what that release