use rayon::prelude::*;
use serde::Serialize;
use std::path::{Path, PathBuf};
pub use kute_adapters as adapters;
pub use kute_algorithms as algorithms;
pub use kute_bpe as bpe;
pub use kute_core::{
check_sequence, decoded_units, AdapterCapabilities, CorpusSource, FnCorpus, ModalityAdapter, Registry, Representation,
RepresentationBackend, Tokenizer, VecCorpus,
};
pub use kute_types::*;
pub use kute_vocab as vocab;
#[derive(Clone)]
pub struct Engine {
registry: Registry,
}
impl Default for Engine {
fn default() -> Self {
Self::new()
}
}
#[derive(Clone, Debug, Serialize)]
pub struct VerifyReport {
pub vocabulary: String,
pub digest: String,
pub vectors: usize,
pub passed: usize,
pub failures: Vec<String>,
}
impl VerifyReport {
pub fn ok(&self) -> bool {
self.failures.is_empty()
}
}
const SELF_CHECK_RECORDS: usize = 4;
const SELF_CHECK_BYTES: usize = 64 * 1024;
const SELF_CHECK_SYMBOLS: usize = 4096;
impl Engine {
pub fn new() -> Self {
let mut registry = Registry::new();
kute_bpe::register(&mut registry).expect("built-in algorithm names are unique");
kute_algorithms::register(&mut registry).expect("built-in algorithm names are unique");
kute_adapters::register(&mut registry).expect("built-in adapter names are unique");
Self { registry }
}
pub fn with_registry(registry: Registry) -> Self {
Self { registry }
}
pub fn registry(&self) -> &Registry {
&self.registry
}
pub fn registry_mut(&mut self) -> &mut Registry {
&mut self.registry
}
pub fn train(
&self,
algorithm: &str,
adapter: &str,
adapter_config: &AdapterConfig,
documents: &[Vec<u8>],
config: &TrainingConfig,
) -> Result<Vocabulary> {
let mut next = documents.iter();
self.train_stream(algorithm, adapter, adapter_config, config, &mut || Ok(next.next().map(std::borrow::Cow::Borrowed)))
}
pub fn train_stream<'a>(
&self,
algorithm: &str,
adapter: &str,
adapter_config: &AdapterConfig,
config: &TrainingConfig,
documents: &mut dyn FnMut() -> Result<Option<std::borrow::Cow<'a, Vec<u8>>>>,
) -> Result<Vocabulary> {
let adapter_impl = self.registry.adapter(adapter)?;
let mut tokenizer = self.registry.tokenizer(algorithm)?;
if config.resume_from.is_some() && algorithm != "bpe" && algorithm != "sequence-bpe" {
return Err(KuteError::Unsupported(format!("resuming a training run is implemented for bpe and sequence-bpe, not {algorithm:?}")));
}
let mut held: Vec<CanonicalInput> = Vec::new();
let mut corpus = FnCorpus(|| match documents()? {
Some(doc) => {
let input = adapter_impl.ingest(&doc, adapter_config)?;
if held.len() < SELF_CHECK_RECORDS && !input.payload.is_empty() {
held.push(CanonicalInput {
modality: input.modality,
payload: match &input.payload {
Payload::Bytes(b) => Payload::Bytes(b[..b.len().min(SELF_CHECK_BYTES)].to_vec()),
Payload::Symbols(v) => Payload::Symbols(v[..v.len().min(SELF_CHECK_SYMBOLS)].to_vec()),
},
});
}
Ok(Some(input))
}
None => Ok(None),
});
let mut vocabulary = tokenizer.train(&mut corpus, config)?;
for (n, input) in held.iter().enumerate() {
let back = tokenizer.decode(&tokenizer.encode(input)?)?;
if back.payload != input.payload {
return Err(KuteError::Backend(format!("trained vocabulary does not reproduce the start of training record {}", n + 1)));
}
}
vocabulary.adapter = Some(adapter_impl.spec(adapter_config));
vocabulary.manifest.modality = adapter_impl.modality();
vocabulary.seal()?;
let report = self.verify(&vocabulary)?;
if !report.ok() {
return Err(KuteError::Backend(format!("trained vocabulary fails its own test vectors: {}", report.failures.join("; "))));
}
Ok(vocabulary)
}
pub fn train_paths(
&self,
algorithm: &str,
adapter: &str,
adapter_config: &AdapterConfig,
paths: &[PathBuf],
config: &TrainingConfig,
) -> Result<Vocabulary> {
let mut files = Vec::new();
for path in paths {
files.extend(collect_files(path)?);
}
if files.is_empty() {
return Err(KuteError::InvalidInput("no input files found".into()));
}
if config.stream_bytes.is_some() && adapter == kute_adapters::JSON {
return Err(KuteError::InvalidConfig("the json adapter reads whole documents and cannot be streamed in pieces".into()));
}
let line_cut = adapter != kute_adapters::BINARY;
let mut files = files.into_iter();
let mut open: Option<Pieces> = None;
self.train_stream(algorithm, adapter, adapter_config, config, &mut || loop {
if let Some(pieces) = open.as_mut() {
if let Some(piece) = pieces.next_piece()? {
return Ok(Some(std::borrow::Cow::Owned(piece)));
}
open = None;
}
let Some(file) = files.next() else { return Ok(None) };
match config.stream_bytes {
Some(size) => open = Some(Pieces::open(&file, size, line_cut)?),
None => return Ok(Some(std::borrow::Cow::Owned(std::fs::read(&file)?))),
}
})
}
pub fn tokenizer(&self, vocabulary: Vocabulary) -> Result<Box<dyn Tokenizer>> {
self.registry.tokenizer_for(vocabulary)
}
fn adapter_of(&self, vocabulary: &Vocabulary) -> Result<(Box<dyn ModalityAdapter>, AdapterConfig)> {
match &vocabulary.adapter {
Some(spec) => Ok((self.registry.adapter(&spec.name)?, AdapterConfig { options: spec.options.clone() })),
None => {
let name = match vocabulary.manifest.payload_kind {
PayloadKind::Bytes => kute_adapters::BINARY,
PayloadKind::Symbols => kute_adapters::JSONL_EVENTS,
};
Ok((self.registry.adapter(name)?, AdapterConfig::default()))
}
}
}
pub fn encode_raw(&self, tokenizer: &dyn Tokenizer, raw: &[u8]) -> Result<TokenSequence> {
let vocabulary = tokenizer.vocabulary().ok_or_else(|| KuteError::NotTrained("tokenizer has no vocabulary".into()))?;
let (adapter, config) = self.adapter_of(vocabulary)?;
tokenizer.encode(&adapter.ingest(raw, &config)?)
}
pub fn encode_raw_open(&self, tokenizer: &dyn Tokenizer, raw: &[u8], policy: UnknownPolicy) -> Result<TokenSequence> {
let vocabulary = tokenizer.vocabulary().ok_or_else(|| KuteError::NotTrained("tokenizer has no vocabulary".into()))?;
let (adapter, config) = self.adapter_of(vocabulary)?;
tokenizer.encode_open(&adapter.ingest(raw, &config)?, policy)
}
pub fn decode_raw_bounded(&self, tokenizer: &dyn Tokenizer, tokens: &TokenSequence, max_units: u64) -> Result<Vec<u8>> {
let vocabulary = tokenizer.vocabulary().ok_or_else(|| KuteError::NotTrained("tokenizer has no vocabulary".into()))?;
let units = kute_core::decoded_units(vocabulary, tokens)?;
if units > max_units {
return Err(KuteError::LimitExceeded(format!("the sequence decodes to {units} units; the budget is {max_units}")));
}
self.decode_raw(tokenizer, tokens)
}
pub fn decode_raw(&self, tokenizer: &dyn Tokenizer, tokens: &TokenSequence) -> Result<Vec<u8>> {
let vocabulary = tokenizer.vocabulary().ok_or_else(|| KuteError::NotTrained("tokenizer has no vocabulary".into()))?;
let (adapter, config) = self.adapter_of(vocabulary)?;
adapter.reconstruct(&tokenizer.decode(tokens)?, &config)
}
pub fn encode_batch(&self, tokenizer: &dyn Tokenizer, documents: &[Vec<u8>]) -> Result<Vec<TokenSequence>> {
documents.par_iter().map(|d| self.encode_raw(tokenizer, d)).collect()
}
pub fn verify(&self, vocabulary: &Vocabulary) -> Result<VerifyReport> {
vocabulary.validate()?;
let tokenizer = self.tokenizer(vocabulary.clone())?;
let mut failures = Vec::new();
let mut passed = 0;
for (i, vector) in vocabulary.test_vectors.iter().enumerate() {
let payload = match &vector.input {
TokenPayload::Bytes(b) => Payload::Bytes(b.clone()),
TokenPayload::Symbols(s) => Payload::Symbols(s.clone()),
};
let input = CanonicalInput { modality: vocabulary.manifest.modality, payload };
match tokenizer.encode(&input) {
Err(e) => failures.push(format!("vector {i}: encode failed: {e}")),
Ok(sequence) if sequence.ids != vector.ids => failures.push(format!("vector {i}: encoded ids differ from the recorded ids")),
Ok(sequence) => match tokenizer.decode(&sequence) {
Ok(back) if back.payload == input.payload => passed += 1,
Ok(_) => failures.push(format!("vector {i}: decode did not reproduce the input")),
Err(e) => failures.push(format!("vector {i}: decode failed: {e}")),
},
}
}
Ok(VerifyReport {
vocabulary: format!("{} {}", vocabulary.id(), vocabulary.version()),
digest: vocabulary.digest()?,
vectors: vocabulary.test_vectors.len(),
passed,
failures,
})
}
}
struct Pieces {
reader: std::io::BufReader<std::fs::File>,
size: usize,
line_cut: bool,
carry: Vec<u8>,
done: bool,
}
impl Pieces {
fn open(path: &Path, size: usize, line_cut: bool) -> Result<Self> {
Ok(Self { reader: std::io::BufReader::new(std::fs::File::open(path)?), size, line_cut, carry: Vec::new(), done: false })
}
fn next_piece(&mut self) -> Result<Option<Vec<u8>>> {
use std::io::{BufRead, Read};
if self.done && self.carry.is_empty() {
return Ok(None);
}
let mut piece = std::mem::take(&mut self.carry);
if !self.done && piece.len() < self.size {
let want = (self.size - piece.len()) as u64;
if (&mut self.reader).take(want).read_to_end(&mut piece)? < want as usize {
self.done = true;
}
}
if self.line_cut && !self.done {
match piece.iter().rposition(|&b| b == b'\n') {
Some(at) => self.carry = piece.split_off(at + 1),
None => {
if self.reader.read_until(b'\n', &mut piece)? == 0 || piece.last() != Some(&b'\n') {
self.done = true;
}
}
}
}
Ok(if piece.is_empty() { None } else { Some(piece) })
}
}
pub fn collect_files(path: &Path) -> Result<Vec<PathBuf>> {
let meta = std::fs::metadata(path).map_err(|e| KuteError::InvalidInput(format!("{}: {e}", path.display())))?;
if meta.is_file() {
return Ok(vec![path.to_path_buf()]);
}
let mut out = Vec::new();
let mut stack = vec![path.to_path_buf()];
while let Some(dir) = stack.pop() {
for entry in std::fs::read_dir(&dir)? {
let entry = entry?;
if entry.file_name().to_string_lossy().starts_with('.') {
continue;
}
let kind = entry.file_type()?;
if kind.is_dir() {
stack.push(entry.path());
} else if kind.is_file() {
out.push(entry.path());
}
}
}
out.sort();
Ok(out)
}