Skip to main content

weirdnet_model/
lib.rs

1//! Weirdnet local models.
2//!
3//! - [`artifact`]: inspect and hash model files a person brings (GGUF models, split models,
4//!   LoRA adapters and projectors; safetensors and MLX model directories; PEFT adapters),
5//!   refusing pickles, code and symlinks, and the [`Provenance`] record kept next to them;
6//!   [`ModelStore::import`] places them in the store.
7//! - [`catalog`]: pinned Gemma 4 E2B and E4B GGUF files (Hugging Face repo, file, size, SHA-256).
8//! - [`limits`]: per-model media and request limits ([`ModelLimits`]: context in use, images per
9//!   request and their token cost, audio length and cost, video, request bytes), read from a
10//!   model and its projector when it loads ([`limits::measure`]) and kept next to its files
11//!   ([`ModelStore::record_limits`]). Unknown figures stay absent, never defaulted.
12//! - [`store`] and [`mod@download`]: where model files and LoRA adapters live (list and remove
13//!   them too), and a consent-gated download that resumes a `.part` file and checks size and
14//!   SHA-256 before the file appears, for single and split models and for adapters.
15//! - [`hf`]: search the Hugging Face Hub, read a repo's GGUF files (split files grouped) and a
16//!   file's header, so any repo can become a [`ModelSpec`]; find a repo's LoRA adapters (GGUF
17//!   and PEFT), which are never listed as models; [`HfToken`] for gated repos, read on the
18//!   downloading node only.
19//! - [`gguf`]: the GGUF header fields that size the KV cache, and whether a file is a model, a
20//!   LoRA adapter or a projector (with what a projector takes, [`gguf::ProjectorInfo`]).
21//! - [`fit`]: pick the largest file whose weights and KV cache fit the memory budget.
22//! - [`prompt`]: render chat turns with the model's own Jinja template, and the `<tool_call>`
23//!   tag format for tools (prompt text and a streaming parser).
24//! - [`worker`]: an [`Engine`] on its own thread, streaming text with cancel.
25//! - [`cpu`]: whether this CPU can run the compiled-in llama.cpp (checked before it loads).
26//! - [`hw`]: what accelerators and memory this machine has, the memory budget for models, a
27//!   tiered catalog with footprints (chat, embedding and speech-to-text models, GGUF or vLLM
28//!   checkpoints, with the `transcribe` role, [`hw::Purpose::Transcribe`], and text-to-speech
29//!   models with the `speak` role, [`hw::Purpose::Speak`]), [`hw::plan`] to pick models that fit, [`hw::transcriber`]
30//!   for the speech-to-text one and [`hw::speaker`] for a text-to-speech one. Sharded checkpoints
31//!   list their shards with a hash each ([`hw::CatalogEntry::files`]), and an entry carries the
32//!   extra engine arguments it needs ([`hw::CatalogEntry::engine_args`], such as vLLM's
33//!   `--trust-remote-code`).
34//! - [`speech`]: what a speaking model does ([`SpeechProfile`]), from the catalog, from a
35//!   hand-placed GGUF's headers ([`speech::from_gguf`]) or kept with its limits.
36//! - `llama` (feature): the llama.cpp [`Engine`].
37//! - `agent` (feature): `weirdnet_agent::ModelBackend` over the worker.
38//!
39//! Nothing here knows an app: callers pass paths, consent and callbacks.
40#![forbid(unsafe_code)]
41
42pub mod artifact;
43pub mod catalog;
44pub mod cpu;
45pub mod download;
46pub mod fit;
47pub mod gguf;
48pub mod hf;
49pub mod hw;
50pub mod limits;
51pub mod prompt;
52pub mod speech;
53pub mod store;
54#[cfg(test)]
55mod testing;
56pub mod token;
57pub mod worker;
58
59#[cfg(feature = "agent")]
60pub mod agent;
61#[cfg(feature = "llama")]
62pub mod llama;
63
64pub use artifact::{Artifact, ArtifactFile, ArtifactFormat, ArtifactKind, Provenance, inspect};
65pub use catalog::{Device, ModelSpec, human};
66pub use download::{Consent, Progress, download, download_adapter, download_parts};
67pub use hf::{AdapterFormat, Hub, RepoAdapter};
68pub use limits::{LimitSource, LimitsRecord, ModelLimits};
69pub use prompt::{ChatMessage, ChatRole};
70pub use speech::SpeechProfile;
71pub use store::{DiskSpace, Imported, ModelLayout, ModelStore, StoredAdapter, StoredModel, StoredProjector};
72pub use token::HfToken;
73pub use worker::{Chunk, Engine, GenerateRequest, Generation, Stats, Worker};
74
75/// Everything that can go wrong here.
76#[derive(Debug, thiserror::Error)]
77pub enum Error {
78    #[error("{0}")]
79    Io(#[from] std::io::Error),
80    #[error("network: {0}")]
81    Http(String),
82    #[error("{name}: checksum mismatch (got {got}); the file was deleted")]
83    Checksum { name: String, got: String },
84    #[error("download stopped at {done} of {total} (run it again to resume)")]
85    Incomplete { done: String, total: String },
86    #[error("download cancelled at {done} of {total} (run it again to resume)")]
87    Cancelled { done: String, total: String },
88    #[error("consent was given for another model")]
89    Consent,
90    #[error("{0:?} is not a Hugging Face model id (owner/name)")]
91    BadRepo(String),
92    #[error("{0}")]
93    Model(String),
94    /// This CPU lacks instructions the compiled-in llama.cpp uses; see [`cpu`].
95    #[error("local models need CPU instructions this one lacks: {}", missing.join(", "))]
96    UnsupportedCpu { missing: Vec<String> },
97    /// `cpu-dispatch` builds: no llama.cpp CPU backend library could be loaded.
98    #[error("no llama.cpp CPU backend found in {} (ship the ggml-cpu libraries there)", dir.display())]
99    NoCpuBackend { dir: std::path::PathBuf },
100}
101
102pub type Result<T> = std::result::Result<T, Error>;