diff --git a/arch/clef_bf16/CMakeLists.txt b/arch/clef_bf16/CMakeLists.txt new file mode 100644 index 00000000..ed193daa --- /dev/null +++ b/arch/clef_bf16/CMakeLists.txt @@ -0,0 +1,10 @@ +# Built in radiance's tree, or standalone against an installed radiance +# (`cmake -S arch/clef_bf16 -B build -DCMAKE_PREFIX_PATH=`); see cmake/RadAddPlugin.cmake. +if(NOT COMMAND rad_add_plugin) + cmake_minimum_required(VERSION 3.21) + project(clef_bf16 LANGUAGES CXX) + find_package(radiance REQUIRED) + rad_plugin_defaults() +endif() + +rad_add_plugin(clef_bf16 architectures SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/clef_bf16.cpp) diff --git a/arch/clef_bf16/clef_bf16.cpp b/arch/clef_bf16/clef_bf16.cpp new file mode 100644 index 00000000..0562d6dc --- /dev/null +++ b/arch/clef_bf16/clef_bf16.cpp @@ -0,0 +1,62 @@ +/* clef_bf16 -- Cloudflare's clef and clef-flash decision models, bf16: the qwen35_bf16 trunk with + * the joint schema head on the card (arch/common/rad_block_clef.h), served on /v1/systemone. + * + * This is arch/qwen35_bf16/qwen35_bf16.cpp built again under its own architecture id, as + * qwen35_w8a8 is qwen35_fp8 built again: the trunk's declare and step are that file's, untouched, + * and this file adds the head after them. A container selects it with `rad-convert --arch clef`, + * so a plain qwen35 container never pays for the head and the qwen35 plugins carry no decision + * code. The head needs `decision.type=clef` and the head's geometry in the container header, + * which rad-convert writes from joint_head_config.json and joint_head.safetensors (--add). + */ +#define RAD_ARCH_NO_EXPORTS 1 +#include "../qwen35_bf16/qwen35_bf16.cpp" +#undef RAD_ARCH_NO_EXPORTS + +#include + +#include + +namespace clef_bf16 { + +using namespace rad::arch; + +/* One head per rank beside the trunk's own per-rank state (qwen35_bf16::g_model). */ +static ClefHead g_head[MAX_RANKS]; + +static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* ctx) { + if (!b || !meta || !ctx) return RAD_E_INVAL; + if (ctx->rank < 0 || ctx->rank >= MAX_RANKS) return RAD_E_INVAL; + if (!ClefHead::present(meta)) { + std::fprintf(stderr, "radiance: clef_bf16 serves a container converted with " + "--set decision.type=clef and the head added (--add " + "joint_head.safetensors); this one declares no decision model\n"); + return RAD_E_INVAL; + } + RAD_ARCH_TRY(qwen35_bf16::declare(b, meta, ctx)); + /* The trunk's declare does not take part in sizing declares (no probe), so its state and the + * head's are the real ones. The head goes after the vision tower: its buffers join the same + * arena, and it reads the final norm's rows, the lm_head and the trunk's cache block size. */ + qwen35_bf16::Model& m = qwen35_bf16::g_model[ctx->rank]; + ClefHead& h = g_head[ctx->rank]; + h = ClefHead{}; + return h.declare(b, meta, ctx, m.nm, m.g, m.b_h, m.b_logits, m.w_lm_head, m.kv_full, + (int)m.g.n_layer + 256); +} + +static void step(RadCtx* c, const RadBatch* batch) { + qwen35_bf16::step(c, batch); + /* An encoder pass runs the tower alone and leaves no rows for the head. */ + if (batch->enc) return; + qwen35_bf16::Model& m = qwen35_bf16::g_model[rad_rank(c)]; + ClefHead& h = g_head[rad_rank(c)]; + /* The final norm's rows are still in `h` after the trunk's step; the logits row of each + * sampled sequence has been written, and the head adds its values beside it. */ + h.step_tokens(c, batch, m.b_h); + if (batch->n_out > 0) h.step_heads(c, batch, m.b_logits, m.w_lm_head); +} + +} /* namespace clef_bf16 */ + +RAD_ARCH_PLUGIN(clef_bf16, "clef", "", "0.1.0", + "Cloudflare's clef / clef-flash (9B, 27B): the Qwen3.5 trunk of qwen35_bf16, bf16, " + "with the joint schema head on the card for /v1/systemone") diff --git a/arch/clef_fp8/CMakeLists.txt b/arch/clef_fp8/CMakeLists.txt new file mode 100644 index 00000000..01c8f3f0 --- /dev/null +++ b/arch/clef_fp8/CMakeLists.txt @@ -0,0 +1,10 @@ +# Built in radiance's tree, or standalone against an installed radiance +# (`cmake -S arch/clef_fp8 -B build -DCMAKE_PREFIX_PATH=`); see cmake/RadAddPlugin.cmake. +if(NOT COMMAND rad_add_plugin) + cmake_minimum_required(VERSION 3.21) + project(clef_fp8 LANGUAGES CXX) + find_package(radiance REQUIRED) + rad_plugin_defaults() +endif() + +rad_add_plugin(clef_fp8 architectures SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/clef_fp8.cpp) diff --git a/arch/clef_fp8/clef_fp8.cpp b/arch/clef_fp8/clef_fp8.cpp new file mode 100644 index 00000000..b813af72 --- /dev/null +++ b/arch/clef_fp8/clef_fp8.cpp @@ -0,0 +1,59 @@ +/* clef_fp8 -- Cloudflare's clef and clef-flash decision models, block fp8: the qwen35_fp8 trunk + * with the joint schema head on the card (arch/common/rad_block_clef.h), served on + * /v1/systemone. + * + * This is arch/qwen35_fp8/qwen35_fp8.cpp built again under its own namespace and architecture + * id, as qwen35_w8a8 and qwen35moe_fp8 are: the trunk's declare, step and probe are that file's, + * untouched, and this file adds the head after them. A container selects it with `rad-convert + * --arch clef` and the fp8 recipe; the lm_head stays bf16, which the head reads. + */ +#define QWEN35_NS qwen35_clef_fp8 +#define QWEN35_ARCH_ID "clef" +#define RAD_ARCH_NO_EXPORTS 1 +#include "../qwen35_fp8/qwen35_fp8.cpp" +#undef RAD_ARCH_NO_EXPORTS + +#include + +#include + +namespace clef_fp8 { + +using namespace rad::arch; + +static ClefHead g_head[MAX_RANKS]; + +static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* ctx) { + if (!b || !meta || !ctx) return RAD_E_INVAL; + if (ctx->rank < 0 || ctx->rank >= MAX_RANKS) return RAD_E_INVAL; + if (!ClefHead::present(meta)) { + std::fprintf(stderr, "radiance: clef_fp8 serves a container converted with " + "--set decision.type=clef and the head added (--add " + "joint_head.safetensors); this one declares no decision model\n"); + return RAD_E_INVAL; + } + RAD_ARCH_TRY(qwen35_clef_fp8::declare(b, meta, ctx)); + qwen35_clef_fp8::Model& m = qwen35_clef_fp8::g_model[ctx->rank]; + ClefHead& h = g_head[ctx->rank]; + h = ClefHead{}; + return h.declare(b, meta, ctx, m.nm, m.g, m.a_h.x, m.b_logits, m.w_lm_head, m.kv_full, + (int)m.g.n_layer + 256); +} + +static void step(RadCtx* c, const RadBatch* batch) { + qwen35_clef_fp8::step(c, batch); + /* An encoder pass leaves no rows for the head, and a drafter's pass is not a trunk step. */ + if (batch->enc || batch->draft_pass != 0) return; + qwen35_clef_fp8::Model& m = qwen35_clef_fp8::g_model[rad_rank(c)]; + ClefHead& h = g_head[rad_rank(c)]; + /* The final norm's rows are in `a_h.x` after either of the trunk's step paths. */ + h.step_tokens(c, batch, m.a_h.x); + if (batch->n_out > 0) h.step_heads(c, batch, m.b_logits, m.w_lm_head); +} + +} /* namespace clef_fp8 */ + +RAD_ARCH_PROBE(qwen35_clef_fp8) +RAD_ARCH_PLUGIN(clef_fp8, "clef", "fp8_e4m3", "0.1.0", + "Cloudflare's clef / clef-flash (9B, 27B): the Qwen3.5 trunk of qwen35_fp8, " + "block-scaled fp8, with the joint schema head on the card for /v1/systemone") diff --git a/arch/qwen35_bf16/qwen35_bf16.cpp b/arch/qwen35_bf16/qwen35_bf16.cpp index e4d1418c..b23b9472 100644 --- a/arch/qwen35_bf16/qwen35_bf16.cpp +++ b/arch/qwen35_bf16/qwen35_bf16.cpp @@ -38,7 +38,6 @@ */ #include #include -#include #include #include #include @@ -86,11 +85,6 @@ struct Model { VisionTower vit{}; MediaRows mrows{}; - /* ---- clef's joint schema head (arch/common/rad_block_clef.h), declared only when the - * container says `decision.type=clef`; `clef.on` is false otherwise and every step below is - * the one it was. */ - ClefHead clef{}; - std::vector layers; }; @@ -430,12 +424,6 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c rad_note(b, "the %lld MTP/NextN layer(s) under `mtp.` are not declared: this plugin " "serves the trunk only", (long long)nextn); - /* clef's head reads the final norm's rows and the lm_head, and keeps its own cache group, - * bound well past the trunk's layers. */ - if (ClefHead::present(meta)) - RAD_ARCH_TRY(m.clef.declare(b, meta, ctx, nm, g, m.b_h, m.b_logits, m.w_lm_head, - m.kv_full, (int)g.n_layer + 256)); - /* The vision tower, last, so its activations share the arena with every buffer above. */ if (m.media) RAD_ARCH_TRY(m.vit.declare(b, meta, ctx, nm, g.n_embd, (int)g.n_layer + 64)); return RAD_OK; @@ -467,8 +455,6 @@ static void step(RadCtx* c, const RadBatch* batch) { RAD_ISSUE(c, m.op_final_norm, brows(m.b_x, batch->n_tok), RAD_W(m.w_out_norm), brows(m.b_h, batch->n_tok)); - /* clef: every token's final hidden state into the head's cache (a no-op without the head). */ - m.clef.step_tokens(c, batch, m.b_h); /* The lm_head runs only on the rows the sampler asked for; a mid-prompt prefill chunk has * n_out zero and skips it. */ @@ -480,9 +466,6 @@ static void step(RadCtx* c, const RadBatch* batch) { RAD_ISSUE_N(c, m.op_logits, batch->n_out, brows(m.b_hout, batch->n_out), RAD_W(m.w_lm_head), brows(m.b_logits, batch->n_out)); - /* clef: the head over each sequence that carries a request's words, after the lm_head - * has written the row it adds to. */ - m.clef.step_heads(c, batch, m.b_logits, m.w_lm_head); } } diff --git a/arch/qwen35_fp8/qwen35_fp8.cpp b/arch/qwen35_fp8/qwen35_fp8.cpp index 370c6103..88aac2eb 100644 --- a/arch/qwen35_fp8/qwen35_fp8.cpp +++ b/arch/qwen35_fp8/qwen35_fp8.cpp @@ -86,7 +86,6 @@ #endif #include #include -#include #include #include @@ -206,10 +205,6 @@ struct Model { VisionTower vit{}; MediaRows mrows{}; - /* ---- clef's joint schema head (arch/common/rad_block_clef.h), declared only when the - * container says `decision.type=clef`; off, every step below is the one it was. */ - ClefHead clef{}; - std::vector layers; /* The pipelined prefill (step_pipelined): on where declare allowed it; the bounds buffer it @@ -1274,12 +1269,6 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c rad_note(b, "the %lld MTP/NextN layer(s) under `mtp.` are not loaded: this deployment " "does not speculate", (long long)nextn); - /* clef's head (arch/common/rad_block_clef.h) reads the final norm's rows and the lm_head, - * and keeps its own cache group, bound well past the trunk's and a drafter's layers. */ - if (ClefHead::present(meta)) - RAD_ARCH_TRY(m.clef.declare(b, meta, ctx, nm, g, m.a_h.x, m.b_logits, m.w_lm_head, - m.kv_full, (int)g.n_layer + 256)); - /* The vision tower, last: an encoder pass issues nothing else, so its activations share the * arena with every buffer above. Its weight groups start past every text layer, the MTP head * and a drafter's. */ @@ -1504,8 +1493,6 @@ static void step_pipelined(RadCtx* c, const RadBatch* batch, Model& m, const int } pipe_wait1(c); if (L > 0) pipe_taps(c, m, L - 1, R, T - R); - /* clef: both pieces' final norm is done by here; the head's cache takes every row. */ - m.clef.step_tokens(c, batch, m.a_h.x); if (batch->n_out > 0) { RAD_ISSUE_N(c, m.op_gather, batch->n_out, @@ -1514,7 +1501,6 @@ static void step_pipelined(RadCtx* c, const RadBatch* batch, Model& m, const int RAD_ISSUE_N(c, m.op_logits, batch->n_out, brows(m.b_hout, batch->n_out), RAD_W(m.w_lm_head), brows(m.b_logits, batch->n_out)); - m.clef.step_heads(c, batch, m.b_logits, m.w_lm_head); } } #endif @@ -1610,8 +1596,6 @@ static void step(RadCtx* c, const RadBatch* batch) { RAD_ISSUE(c, m.op_final_norm, brows(m.b_x, batch->n_tok), RAD_W(m.w_out_norm), brows(m.a_h.x, batch->n_tok)); - /* clef: every token's final hidden state into the head's cache (a no-op without the head). */ - m.clef.step_tokens(c, batch, m.a_h.x); if (batch->n_out > 0) { RAD_ISSUE_N(c, m.op_gather, batch->n_out, @@ -1621,7 +1605,6 @@ static void step(RadCtx* c, const RadBatch* batch) { RAD_ISSUE_N(c, m.op_logits, batch->n_out, brows(m.b_hout, batch->n_out), RAD_W(m.w_lm_head), brows(m.b_logits, batch->n_out)); - m.clef.step_heads(c, batch, m.b_logits, m.w_lm_head); } } diff --git a/docs/GUIDE.md b/docs/GUIDE.md index f0fe29f7..f7eeb1a9 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -2760,17 +2760,22 @@ Cloudflare's clef (27B) and clef-flash (9B) are decision models on Qwen3.5 that request, every question of it jointly: a joint schema head reads the backbone's final hidden state at every prompt position, the mean over each question's instructions and each option's text, and the lm_head rows of each option's tokens, and gives one logit an option. radiance runs that head -on the card, after the trunk, inside the `qwen35_bf16` and `qwen35_fp8` plugins -(`arch/common/rad_block_clef.h`): there is no second process. The head's own tensors ship as +on the card, after the trunk, in the `clef_bf16` and `clef_fp8` plugins (`arch/clef_bf16`, +`arch/clef_fp8`: the qwen35 plugins' own code with `arch/common/rad_block_clef.h` after it): there +is no second process. The head's own tensors ship as `joint_head.safetensors` beside the checkpoint and are added at convert; `joint_head_config.json` beside them gives the head's geometry. ```sh -rad-convert /srv/models/clef-flash -o clef-flash.rad \ +rad-convert /srv/models/clef-flash -o clef-flash.rad --arch clef \ --add /srv/models/clef-flash/joint_head.safetensors --set decision.type=clef radiance --model clef-flash.rad --port 8000 ``` +`--arch clef` selects the `clef_bf16` and `clef_fp8` plugins, the qwen35 trunk built again with +the head after it; a container left as `qwen35` serves generation and answers `/v1/systemone` +with 501. + The request and the response are the same as d1's above; `/v1/decisions` is the same endpoint under LiteLLM's name. What differs: diff --git a/docs/adr/0001-clef-head-on-the-gpu.md b/docs/adr/0001-clef-head-on-the-gpu.md index e9dcd7e8..556e5d0b 100644 --- a/docs/adr/0001-clef-head-on-the-gpu.md +++ b/docs/adr/0001-clef-head-on-the-gpu.md @@ -12,7 +12,12 @@ On threadripper, fp8 clef-flash with the head in a PyTorch sidecar served 1.43 r ## Decision -The head runs on the GPU, in the plugin step (`arch/common/rad_block_clef.h`), with these parts: +The head runs on the GPU, in the plugin step (`arch/common/rad_block_clef.h`). On this branch +(`clef-flash-arch-plugin`) it is wired in two plugins of its own, `arch/clef_bf16` and +`arch/clef_fp8`, which build the qwen35 plugins' source again under architecture id `clef` and +add the head after the trunk; the qwen35 plugins are untouched. The sibling branch +(`clef-flash-support`) wires the same block into the qwen35 plugins behind `decision.type=clef`. +The parts: 1. Every step, the final norm's rows go through the head's `hidden_norm` and into a paged FULL KV group of the head's own (`clef.kv`, one layer bound past the trunk's). Its block size is the trunk's, so the prefix cache keeps those rows with the trunk's cache. 8 KB a token on clef-flash. 2. On the step that samples a prompt's first token, the head gathers every row of its group back out (`gather_rows` over the sequence's block table, the way the KV tier packs blocks), runs the memory projection and the six cross-attention layers over all of them, averages the question and option spans, routes, runs the decoder layers and the scorer, and writes three values an option (lexical prior, cosine, residual) into the request's logits row. diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ba3c3311..d0d967c0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -310,7 +310,7 @@ foreach(t engine_e2e_test convert_test clef_decision_test) # The tiny clef's golden (tests/mkclefgolden.py) and the one plugin a clef container selects. target_compile_definitions(${t} PRIVATE RAD_CLEF_GOLDEN="${CMAKE_CURRENT_SOURCE_DIR}/data/golden/clef_tiny.json") - add_dependencies(${t} radiance rad-convert libref libquant qwen35_bf16) + add_dependencies(${t} radiance rad-convert libref libquant clef_bf16) else() add_dependencies(${t} ${RAD_E2E_DEPS}) endif() diff --git a/tests/clef_decision_test.cpp b/tests/clef_decision_test.cpp index 5ce28d1c..9a6f42e0 100644 --- a/tests/clef_decision_test.cpp +++ b/tests/clef_decision_test.cpp @@ -1,7 +1,8 @@ /* clef_decision_test.cpp -- /v1/systemone on a clef container, end to end on the CPU. * * A tiny qwen35 (tests/tiny_model.h) with a tiny joint schema head of its own, converted with the - * head added and `decision.type=clef`, served by the radiance binary on the host backend. The + * head added, `--arch clef` (the clef plugins) and `decision.type=clef`, served by the radiance + * binary on the host backend. The * answers are checked against tests/data/golden/clef_tiny.json, the release's own head * (joint_schema_model.py) over transformers' forward pass in float32, made by * tests/mkclefgolden.py from the checkpoint this test writes (RAD_E2E_KEEP=1 keeps it). The @@ -288,7 +289,7 @@ TEST(a_clef_container_decides_every_question_on_the_card_and_matches_the_release /* Converted with the head added; the head's geometry and scalars land in the header. */ const std::string rad = dir / "clef-tiny.rad"; std::string log; - const int st = tiny::run({ RAD_E2E_CONVERT, ck, "-o", rad, "--add", + const int st = tiny::run({ RAD_E2E_CONVERT, ck, "-o", rad, "--arch", "clef", "--add", ck + "/joint_head.safetensors", "--set", "decision.type=clef" }, engine_env(), &log, 300); if (st != 0) fprintf(stderr, " rad-convert exited %d:\n%s\n", st, tail(log).c_str()); @@ -388,8 +389,8 @@ TEST(a_clef_convert_without_the_head_is_refused) { REQUIRE_EQ(mkdir(ck.c_str(), 0755), 0); REQUIRE(write_tiny_clef(ck, 7)); std::string log; - const int st = tiny::run({ RAD_E2E_CONVERT, ck, "-o", dir / "no-head.rad", "--set", - "decision.type=clef" }, engine_env(), &log, 300); + const int st = tiny::run({ RAD_E2E_CONVERT, ck, "-o", dir / "no-head.rad", "--arch", "clef", + "--set", "decision.type=clef" }, engine_env(), &log, 300); CHECK(st != 0); CHECK(log.find("joint_head.safetensors") != std::string::npos); } diff --git a/tests/shipped_plugins_test.cpp b/tests/shipped_plugins_test.cpp index 8f2a59fa..fc2e78cf 100644 --- a/tests/shipped_plugins_test.cpp +++ b/tests/shipped_plugins_test.cpp @@ -90,7 +90,8 @@ TEST(the_shipped_architectures_load_with_distinct_selection_keys) { CHECK(keys.emplace(std::make_pair(p.arch_id, p.arch_quant), p.name).second); } const std::set> want = { - { "anima", "" }, { "breeze", "" }, { "embeddinggemma2", "" }, + { "anima", "" }, { "breeze", "" }, { "clef", "" }, { "clef", "fp8_e4m3" }, + { "embeddinggemma2", "" }, { "fishomni", "bsgemm-moe" }, { "flux3", "" }, { "flux3distilled", "" }, { "gemma3", "" }, { "gemma4", "" }, { "gemma4", "compressed-tensors" }, { "glm5next", "" },