Linux / keyless-cuda-gate (push) Skipped
Rust port / port-typecheck (push) Failing after 8s
Desktop foundation / core (ubuntu-latest) (push) Failing after 11s
Rust port / port-oracles (push) Failing after 11s
Rust port / port-linux-tests (push) Failing after 14s
Rust port / port-fast (push) Failing after 9s
Linux / verify (push) Failing after 17s
Linux / keyless-cuda (push) Skipped
Linux / static-cuda-package (push) Skipped
Linux / hosted-cuda-package (push) Skipped
Linux / linux-static-cli (push) Skipped
Linux / linux-cli-container (push) Skipped
Linux / release-cli (push) Skipped
Brings the Rust/CUDA port of the Strata mode17 serve path together with the CPU/iGPU/NPU gpt-oss runtime that main gained meanwhile. Control plane: the Qwen tokenizer is the implementation behind the Qwen35 variant of codec::TextCodec (qwen(), encode_prompt() widen the trait; QwenCodec derefs to the tokenizer), so control-token marks, images, chat template, the runtime backend and vision keep their behavior, while the gpt-oss/Harmony path and the worker pool keep main's. Config carries the union of fields; extra_workers is rejected together with runtime, lazy_load, idle_unload_s and vision; the gpt-oss family refuses top_k/min_p/penalties (its GEN line has no such keys). leleka-kar-codec takes f16_bits_to_f32 from leleka-quant-core (identical code). Workspace members and default-members are the union of both sides. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
134 lines
4.9 KiB
Rust
134 lines
4.9 KiB
Rust
//! The text side of a model family: how prompts become token ids and generated ids become text.
|
|
//!
|
|
//! The Responses code was written for Qwen3.5 (ChatML prompt, `<think>` reasoning, `<tool_call>` XML).
|
|
//! A family supplies its tokenizer and a stateful decoder that turns the generated token stream into
|
|
//! that same markup, so the existing reasoning and tool-call parsing, validation, history and streaming
|
|
//! events work unchanged. For gpt-oss the decoder translates Harmony channels and tool calls
|
|
//! (see `harmony_family`).
|
|
|
|
use std::sync::Arc;
|
|
|
|
/// Which prompt format and output grammar a served model uses.
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum Family {
|
|
/// Qwen3.5: ChatML prompt, `<think>` reasoning that the prompt opens, XML tool calls.
|
|
Qwen35,
|
|
/// gpt-oss: Harmony prompt, reasoning in the analysis channel, tool calls in the commentary channel.
|
|
GptOss,
|
|
}
|
|
|
|
/// Turns generated token ids into text bytes, keeping whatever state the family's grammar needs.
|
|
pub trait TokenDecoder: Send {
|
|
/// The bytes for one more generated token.
|
|
fn push(&mut self, id: u32) -> Result<Vec<u8>, String>;
|
|
/// The generation ended: any bytes the decoder was holding back.
|
|
fn finish(&mut self) -> Vec<u8>;
|
|
}
|
|
|
|
/// Tokenizer and output decoding of one model family.
|
|
pub trait TextCodec: Send + Sync {
|
|
fn family(&self) -> Family;
|
|
/// Token ids of `text`; `parse_special` turns special-token text (the template markers) into tokens.
|
|
fn encode(&self, text: &str, parse_special: bool) -> Result<Vec<u32>, String>;
|
|
/// A fresh decoder for one generation.
|
|
fn decoder(&self) -> Box<dyn TokenDecoder>;
|
|
/// Tokens that end the visible output of a generation (they are not part of the text).
|
|
fn terminal_ids(&self) -> Vec<u32>;
|
|
/// Token ids of a rendered prompt, the one tokenization used for generation and for counting. The template's own
|
|
/// markers are special tokens; the default reads every special-token text as a token (Harmony).
|
|
fn encode_prompt(&self, prompt: &str) -> Result<Vec<u32>, String> {
|
|
self.encode(prompt, true)
|
|
}
|
|
/// The Qwen tokenizer behind this codec, for what only that family has (control-token marks, pictures, the
|
|
/// reasoning budget, the runtime backend); `None` for the other families.
|
|
fn qwen(&self) -> Option<&Arc<crate::tokenizer::Tokenizer>> {
|
|
None
|
|
}
|
|
}
|
|
|
|
/// Decode a whole id sequence at once.
|
|
pub fn decode_all(codec: &dyn TextCodec, ids: &[u32]) -> Result<Vec<u8>, String> {
|
|
let mut decoder = codec.decoder();
|
|
let mut bytes = Vec::new();
|
|
for id in ids {
|
|
bytes.extend(decoder.push(*id)?);
|
|
}
|
|
bytes.extend(decoder.finish());
|
|
Ok(bytes)
|
|
}
|
|
|
|
/// Qwen3.5 decodes token by token with no state.
|
|
struct QwenDecoder(Arc<crate::tokenizer::Tokenizer>);
|
|
|
|
impl TokenDecoder for QwenDecoder {
|
|
fn push(&mut self, id: u32) -> Result<Vec<u8>, String> {
|
|
self.0.decode_bytes(&[id]).map_err(|e| e.to_string())
|
|
}
|
|
|
|
fn finish(&mut self) -> Vec<u8> {
|
|
Vec::new()
|
|
}
|
|
}
|
|
|
|
/// The Qwen tokenizer as a codec; hold it in an `Arc` and call [`qwen_codec`].
|
|
pub struct QwenCodec(Arc<crate::tokenizer::Tokenizer>);
|
|
|
|
pub fn qwen_codec(tokenizer: crate::tokenizer::Tokenizer) -> QwenCodec {
|
|
QwenCodec(Arc::new(tokenizer))
|
|
}
|
|
|
|
/// The codec is the tokenizer plus the family's grammar; the Qwen-only code (control-token marks, pictures) reaches the
|
|
/// tokenizer through it.
|
|
impl std::ops::Deref for QwenCodec {
|
|
type Target = crate::tokenizer::Tokenizer;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
/// The codec over a tokenizer that is shared with other code (the runtime backend counts with the same one).
|
|
pub fn qwen_codec_shared(tokenizer: Arc<crate::tokenizer::Tokenizer>) -> QwenCodec {
|
|
QwenCodec(tokenizer)
|
|
}
|
|
|
|
impl QwenCodec {
|
|
/// The text of `ids` (tests and tools; the Responses code decodes through [`TextCodec::decoder`]).
|
|
pub fn decode(&self, ids: &[u32]) -> Result<String, String> {
|
|
self.0.decode(ids).map_err(|e| e.to_string())
|
|
}
|
|
}
|
|
|
|
impl TextCodec for QwenCodec {
|
|
fn family(&self) -> Family {
|
|
Family::Qwen35
|
|
}
|
|
|
|
fn encode(&self, text: &str, parse_special: bool) -> Result<Vec<u32>, String> {
|
|
self.0
|
|
.encode(text, parse_special)
|
|
.map_err(|e| e.to_string())
|
|
}
|
|
|
|
fn decoder(&self) -> Box<dyn TokenDecoder> {
|
|
Box::new(QwenDecoder(self.0.clone()))
|
|
}
|
|
|
|
/// Control-token text inside the messages is marked by the renderer and encoded as ordinary text
|
|
/// ([`crate::tokenizer::Tokenizer::encode_marked`]).
|
|
fn encode_prompt(&self, prompt: &str) -> Result<Vec<u32>, String> {
|
|
self.0.encode_marked(prompt).map_err(|e| e.to_string())
|
|
}
|
|
|
|
fn qwen(&self) -> Option<&Arc<crate::tokenizer::Tokenizer>> {
|
|
Some(&self.0)
|
|
}
|
|
|
|
fn terminal_ids(&self) -> Vec<u32> {
|
|
["<|im_end|>", "<|endoftext|>"]
|
|
.iter()
|
|
.filter_map(|marker| self.0.special_id(marker))
|
|
.collect()
|
|
}
|
|
}
|