diff --git a/Cargo.lock b/Cargo.lock index 5fb51353..f8d3729d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1007,6 +1007,16 @@ dependencies = [ name = "leleka-backend-api" version = "0.1.0" +[[package]] +name = "leleka-tokenizer" +version = "0.1.0" +dependencies = [ + "aho-corasick", + "fancy-regex", + "serde", + "serde_json", +] + [[package]] name = "libc" version = "0.2.190" @@ -1988,15 +1998,14 @@ checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" name = "strata-control" version = "0.1.0" dependencies = [ - "aho-corasick", "axum", "cc", - "fancy-regex", "futures-core", "hex", "http-body", "http-body-util", "jsonschema", + "leleka-tokenizer", "libc", "regex", "regex-syntax", diff --git a/Cargo.toml b/Cargo.toml index d73133ff..5a15d9d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [workspace] resolver = "3" -members = ["crates/backend-api", "control-plane", "desktop-core"] -default-members = ["crates/backend-api", "control-plane", "desktop-core"] +members = ["crates/backend-api", "crates/tokenizer", "control-plane", "desktop-core"] +default-members = ["crates/backend-api", "crates/tokenizer", "control-plane", "desktop-core"] [workspace.package] edition = "2024" diff --git a/README.md b/README.md index 518cef37..3e107e42 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,8 @@ performance gain is claimed for Leleka until its real-model gates pass. ## Repository layout - `crates/backend-api/` — Leleka's versioned Rust backend contract. +- `crates/tokenizer/` — the Qwen35 byte-level BPE tokenizer extracted as a + backend-independent Rust crate. - `desktop/`, `desktop-core/` — current desktop application and its Rust core. - `control-plane/` — current Rust API, Responses handling, and worker supervisor. - `engine/strata/` — pinned upstream Strata reference; retained during migration. @@ -30,12 +32,13 @@ artifacts remain outside Git or under ignored paths. ## Current Leleka scope -The Leleka implementation currently defines a backend contract for model -identity, capability declarations, loading, generation, cancellation, unloading, -typed output events, and exactly-one terminal stream behavior. It has no -inference backend or CUDA integration yet. The repository also contains the -existing Strata Next desktop and control-plane implementations as migration -source; their presence does not mean Leleka can serve a model independently. +Leleka currently has a versioned backend contract and a standalone Qwen35 BPE +tokenizer crate. The existing Rust control plane now consumes that shared +tokenizer crate instead of maintaining a second copy; its oracle-parity tests +remain in the workspace. Leleka still has no independent inference backend or +CUDA integration, so this does not yet serve a model independently. The +desktop app and the rest of the control plane remain available as migration +source in this repository. ## Development diff --git a/control-plane/Cargo.toml b/control-plane/Cargo.toml index 3a1da7d8..f293001b 100644 --- a/control-plane/Cargo.toml +++ b/control-plane/Cargo.toml @@ -10,11 +10,10 @@ name = "strata-next" path = "src/main.rs" [dependencies] -aho-corasick = "1.1" axum = "0.8" -fancy-regex = "0.19" futures-core = "0.3" hex = "0.4" +leleka-tokenizer = { path = "../crates/tokenizer" } http-body = "1" jsonschema = { version = "0.58.4", default-features = false } libc = "0.2" diff --git a/control-plane/src/lib.rs b/control-plane/src/lib.rs index b4b6cf33..97965c3c 100644 --- a/control-plane/src/lib.rs +++ b/control-plane/src/lib.rs @@ -43,7 +43,7 @@ pub mod service_install; pub mod service_unit; pub mod stream_parts; pub mod tensor_export; -pub mod tokenizer; +pub use leleka_tokenizer as tokenizer; pub mod tokenizer_assets; pub mod tool_output; pub mod wire; diff --git a/control-plane/src/tokenizer_assets.rs b/control-plane/src/tokenizer_assets.rs index a0cec9a5..324c924e 100644 --- a/control-plane/src/tokenizer_assets.rs +++ b/control-plane/src/tokenizer_assets.rs @@ -14,16 +14,7 @@ use crate::gguf; type Result = std::result::Result>; -// Kept byte-for-byte equivalent to the upstream qwen35 oracle pattern. -pub(crate) const QWEN35_PATTERN: &str = concat!( - r"(?:'[sS]|'[tT]|'[rR][eE]|'[vV][eE]|'[mM]|'[lL][lL]|'[dD])", - r"|[^\r\n\p{L}\p{N}]?[\p{L}\p{M}]+", - r"|\p{N}", - r"| ?[^\s\p{L}\p{M}\p{N}]+[\r\n]*", - r"|\s*[\r\n]+", - r"|\s+(?!\S)", - r"|\s+", -); +pub(crate) use leleka_tokenizer::QWEN35_PATTERN; pub fn extract(gguf_path: &Path, output: &Path) -> Result { let metadata = gguf::tokenizer_metadata(gguf_path)?; diff --git a/crates/tokenizer/Cargo.toml b/crates/tokenizer/Cargo.toml new file mode 100644 index 00000000..ee1884d8 --- /dev/null +++ b/crates/tokenizer/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "leleka-tokenizer" +version = "0.1.0" +description = "Native Qwen35 tokenizer implementation for Leleka" +edition.workspace = true +license.workspace = true +rust-version.workspace = true + +[dependencies] +aho-corasick = "1.1" +fancy-regex = "0.19" +serde = { version = "1", features = ["derive"] } +serde_json = "1" + +[lints] +workspace = true diff --git a/control-plane/src/tokenizer.rs b/crates/tokenizer/src/lib.rs similarity index 97% rename from control-plane/src/tokenizer.rs rename to crates/tokenizer/src/lib.rs index 56dafd2d..2f7ff08c 100644 --- a/control-plane/src/tokenizer.rs +++ b/crates/tokenizer/src/lib.rs @@ -12,7 +12,16 @@ use aho_corasick::{AhoCorasick, AhoCorasickBuilder, MatchKind}; use fancy_regex::Regex; use serde::Deserialize; -use crate::tokenizer_assets::QWEN35_PATTERN; +/// Pre-tokenization expression pinned to the Qwen35 GGUF vocabulary oracle. +pub const QWEN35_PATTERN: &str = concat!( + r"(?:'[sS]|'[tT]|'[rR][eE]|'[vV][eE]|'[mM]|'[lL][lL]|'[dD])", + r"|[^\r\n\p{L}\p{N}]?[\p{L}\p{M}]+", + r"|\p{N}", + r"| ?[^\s\p{L}\p{M}\p{N}]+[\r\n]*", + r"|\s*[\r\n]+", + r"|\s+(?!\S)", + r"|\s+", +); type Result = std::result::Result>; @@ -336,7 +345,8 @@ fn literal_matcher( Ok(Some(LiteralMatcher { search, ids })) } -pub(crate) fn byte_alphabet() -> ([char; 256], HashMap) { +#[doc(hidden)] +pub fn byte_alphabet() -> ([char; 256], HashMap) { let mut chars = ['\0'; 256]; for byte in (0x21..=0x7e).chain(0xa1..=0xac).chain(0xae..=0xff) { chars[byte] = char::from_u32(byte as u32).expect("byte code point");