clef: the joint schema head on the card, served on /v1/systemone

This commit is contained in:
Brandon Bertelsen committed 2026-10-28 00:33:55 -04:00
1 parent 555fbf62f5
commit a96bfe821f
17 files changed
+1786 -27

No files matched your search

+33 -1
View File
@@ -2039,7 +2039,7 @@ flags are refused on it, and so are the pixel bands.
| `POST /v1/completions` | raw text completion |
| `POST /v1/embeddings` | needs a model with an embedding head, else 501 |
| `POST /v1/images/generations`, `POST /v1/images/edits` | OpenAI's images API; needs an image model (Qwen-Image 2.1), else 501 (§5.6) |
| `POST /v1/systemone` | LiquidAI d1's decisions over a state: named yes/no, choice and score questions answered as probabilities, nothing generated; needs a container converted as d1, else 501 (§5.13) |
| `POST /v1/systemone` | Decisions over a state: named yes/no, choice and score questions answered as probabilities, nothing generated; LiquidAI's d1, or Cloudflare's clef and clef-flash with their joint head on the card; needs a container converted as one, else 501 (§5.13). `POST /v1/decisions` is the same endpoint under LiteLLM's name |
| `POST /v1/audio/transcriptions` | OpenAI's speech to text, for a model whose plugin transcribes (Qwen3-ASR), else 501 (§5.14) |
| `GET /v1/realtime` (WebSocket) | OpenAI's realtime transcription session: audio streamed in, the words back as they are spoken (§5.14) |
| `POST /v1/audio/speech` | OpenAI's speech API, whole or streamed: speech from a speech model (Breeze-TTS-2, `instructions` describing the voice; drama-3, speakers and direction in the input); songs from a music model (MiniMax-Music3), `input` the lyrics and `instructions` the song; else 501 (§5.15) |
@@ -2754,6 +2754,38 @@ together. A single question is one pass of the whole prompt, tokenised as one te
Against d1's own runner in float32, on eight text cases (one and several questions, JSON and long log
states): every choice and every `input_tokens` equal, worst probability difference 0.0044.
#### clef and clef-flash
Cloudflare's clef (27B) and clef-flash (9B) are decision models on Qwen3.5 that answer the same
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
`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 \
--add /srv/models/clef-flash/joint_head.safetensors --set decision.type=clef
radiance --model clef-flash.rad --port 8000
```
The request and the response are the same as d1's above; `/v1/decisions` is the same endpoint
under LiteLLM's name. What differs:
- One request is one prompt, the state and every question together, so questions are not shared
through the prefix cache; a state many requests share (the rules in front of each respondent)
is, with the head's own per-token rows beside the trunk's cache.
- `state` must be a non-empty string or JSON value; pictures answer 501 for now.
- A prompt longer than the context answers 400 with `context_length_exceeded` (clef's own code
cuts it silently). A request has at most 64 questions, 512 options and 8192 option tokens.
- The head runs on one rank, and the lm_head stays bf16 (a recipe that quantises it is refused).
- `GET /v1/models` reports `"decision": "clef"` in the model's capabilities.
Against clef's own head on the CPU reference, on a tiny model with random weights
(`tests/clef_decision_test.cpp`): worst probability difference 0.0004.
### 5.14 Speech to text
A model whose architecture plugin says how it transcribes -- Qwen3-ASR, served as its checkpoint
+24 -22
View File
@@ -1,42 +1,44 @@
<!-- Machine-drafted by Claude (Opus 5.5) on 2026-10-27. Decision by the user. -->
<!-- Machine-drafted by Claude (Opus 5.5, then Fable 5.1) on 2026-10-27 and 2026-10-28. Decision by the user. -->
# ADR 0001: clef's joint head runs on the GPU, inside the qwen35 plugins
Status: accepted, 2026-10-27.
Status: accepted 2026-10-27, built 2026-10-28.
## Context
Issue #166 asks radiance to serve clef and clef-flash on `/v1/systemone` with no Python sidecar. clef's head reads the final hidden state of every prompt token, not one position as d1 does. Two places fit: the GPU, inside `qwen35_bf16` and `qwen35_fp8`, or the CPU, in the server.
On threadripper, fp8 clef-flash serves 1.43 respondents/s on 13k-token guidance prompts and 9.16 on 676-token prompts. The PyTorch head takes 8.6 ms of the 95 ms short-prompt time (`bench/timings.md`). The CPU head is estimated at 60-120 ms on a short prompt and 0.4-0.9 s on a 13k-token cache miss. That figure is inferred, not measured.
On threadripper, fp8 clef-flash with the head in a PyTorch sidecar served 1.43 respondents/s on 13k-token guidance prompts and 9.16 on 676-token prompts. The PyTorch head took 8.6 ms of the 95 ms short-prompt time (`bench/timings.md`). A CPU head was estimated at 60-120 ms on a short prompt and 0.4-0.9 s on a 13k-token cache miss (inferred, not measured).
## Decision
The head runs on the GPU, in the plugin step. The per-token part (LayerNorm, memory projection, and K and V for the 6 cross-attention layers) is written into a paged FULL KV group during the trunk step, so the prefix cache keeps it. A per-request tail pass runs the query side over those planes.
The head runs on the GPU, in the plugin step (`arch/common/rad_block_clef.h`), with these parts:
## Reuse, before any new code
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.
3. The request's spans, option tokens and question types reach the plugin as integer words (`Request::plugin_words`, `RadBatch::plugin_words`, laid out by `abi/rad_clef.h`). This is the one ABI change: version 32, five trailing fields on `RadBatch`, and `rad_declare_plugin_words`. A pass carrying words is never recorded for replay.
4. The server reads the three values through `Request::token_scores`, as d1 does, and applies the head's three learned scalars from the container header (`decision.clef.*`, written by `rad-convert` from `joint_head.safetensors` and `joint_head_config.json`).
## What was reused, and the one new kernel
| Head piece | Existing code |
| --- | --- |
| LayerNorm with bias | `layernorm` op, as in `arch/common/rad_block_vit.h` |
| Linear, with or without bias, fused exact GELU | `gemm_nt_bias` (`act "gelu"`), `gemm_nt` |
| Self-attention over the field rows | `attn_dense`, `causal 0`, head_dim 64 |
| Cross-attention over the memory rows | `attn_cross` or a paged read of the clef KV group |
| Span means, lm_head row means | `seg_mean`, `gather_rows`, `embed_lookup` / `embed_lookup_q` |
| Per-token K/V kept in the prefix cache | a FULL KV group, bound past `n_layers` as `drafter_kv_group` does |
| LayerNorm with bias, linear with bias and fused exact GELU | `layernorm`, `gemm_nt_bias`, as the vision tower uses them |
| Self-attention over the field rows | `attn_dense`, `causal 0` |
| Field and option rows over every prompt token | `attn_cross`, as sdxl uses it |
| Span means, lm_head rows of the option tokens | `seg_mean`, `gather_rows`, `embed_lookup` |
| Per-token rows kept across requests | a FULL KV group, `kv_store`, the prefix cache |
| Rows back out of the group | `gather_rows` over the pool, one block a row |
| Cosine and prior | `rmsnorm_p` on both rows, then the row mean of their product (an identity) |
| Head weights in the container | `rad-convert --add joint_head.safetensors --set decision.type=clef` |
| Weights optional, gated on metadata | `RadWeightDecl::optional`, gated like the vision tower |
| Result back to the server | the `token_scores` path: stage, issue, collect in `core/engine.cpp` |
| Prompt, spans, answer | `core/server/clef.cpp` (commit 82c114e) |
| The answer back to the server | the `token_scores` path, unchanged |
## New code this needs
1. A per-request payload on `Request` and `RadBatch`: question and option spans, type ids, option token ids.
2. A per-request head tail in the plugin step, and a small logits buffer the engine copies out.
3. A GPU `softmax` kernel for the option routing weights. Only CPU kernels exist.
4. The three learned scalars (`prior_logit_scale`, `joint_logit_scale`, `residual_gate`) go through `exp` and `sigmoid`. The plugin returns prior, cosine and residual per option, and the server combines them, so no `exp` kernel is needed.
5. Span rows must be fresh: prefix reuse stops at the start of the schema, so the span hidden states are always computed in the request's own step.
One kernel did not exist: a softmax-weighted sum over each question's options (`seg_softmax_pool`), added to libref, libavx and libr4d with its schema, shape, self-test case and `docs/OPS.md` entry. `softmax` had only host kernels, and a weightless op with no device kernel is a hole the engine refuses.
## Consequences
The clef KV group needs an attention consumer at block size 16, or the prefix cache silently drops it. The startup report names such a group "not cached". Cosine and dot products run in bf16, so answers can differ from PyTorch in the third decimal place. The golden test measures that difference.
- The head runs on one rank: `world > 1` is refused at declare. clef 27B in bf16 needs two cards and so is not served with the head yet.
- The lm_head must stay bf16 (the head averages its rows); a recipe that quantises it is refused at declare.
- A request has at most 64 questions, 512 options and 8192 option tokens (`abi/rad_clef.h`); past that the server answers 400.
- `tests/clef_decision_test.cpp` serves a tiny clef on the CPU and compares every probability with the release's own head in float32 (`tests/mkclefgolden.py`): largest difference 0.0004 on 2026-10-28.
- bf16 through the head moves a probability by up to a few hundredths against PyTorch on the real model; the 100-respondent runs in `bench/timings.md` measure it.