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//! - [`store`] and [`mod@download`]: where model files and LoRA adapters live (list and remove
9//! them too), and a consent-gated download that resumes a `.part` file and checks size and
10//! SHA-256 before the file appears, for single and split models and for adapters.
11//! - [`hf`]: search the Hugging Face Hub, read a repo's GGUF files (split files grouped) and a
12//! file's header, so any repo can become a [`ModelSpec`]; find a repo's LoRA adapters (GGUF
13//! and PEFT), which are never listed as models; [`HfToken`] for gated repos, read on the
14//! downloading node only.
15//! - [`gguf`]: the GGUF header fields that size the KV cache, and whether a file is a model or
16//! a LoRA adapter.
17//! - [`fit`]: pick the largest file whose weights and KV cache fit the memory budget.
18//! - [`prompt`]: render chat turns with the model's own Jinja template, and the `<tool_call>`
19//! tag format for tools (prompt text and a streaming parser).
20//! - [`worker`]: an [`Engine`] on its own thread, streaming text with cancel.
21//! - [`cpu`]: whether this CPU can run the compiled-in llama.cpp (checked before it loads).
22//! - [`hw`]: what accelerators and memory this machine has, the memory budget for models, a
23//! tiered catalog with footprints, and [`hw::plan`] to pick models that fit.
24//! - `llama` (feature): the llama.cpp [`Engine`].
25//! - `agent` (feature): `weirdnet_agent::ModelBackend` over the worker.
26//!
27//! Nothing here knows an app: callers pass paths, consent and callbacks.
28#![forbid(unsafe_code)]
29
30pub mod artifact;
31pub mod catalog;
32pub mod cpu;
33pub mod download;
34pub mod fit;
35pub mod gguf;
36pub mod hf;
37pub mod hw;
38pub mod prompt;
39pub mod store;
40#[cfg(test)]
41mod testing;
42pub mod token;
43pub mod worker;
44
45#[cfg(feature = "agent")]
46pub mod agent;
47#[cfg(feature = "llama")]
48pub mod llama;
49
50pub use artifact::{Artifact, ArtifactFile, ArtifactFormat, ArtifactKind, Provenance, inspect};
51pub use catalog::{Device, ModelSpec, human};
52pub use download::{Consent, Progress, download, download_adapter, download_parts};
53pub use hf::{AdapterFormat, Hub, RepoAdapter};
54pub use prompt::{ChatMessage, ChatRole};
55pub use store::{DiskSpace, Imported, ModelLayout, ModelStore, StoredAdapter, StoredModel, StoredProjector};
56pub use token::HfToken;
57pub use worker::{Chunk, Engine, GenerateRequest, Generation, Stats, Worker};
58
59/// Everything that can go wrong here.
60#[derive(Debug, thiserror::Error)]
61pub enum Error {
62 #[error("{0}")]
63 Io(#[from] std::io::Error),
64 #[error("network: {0}")]
65 Http(String),
66 #[error("{name}: checksum mismatch (got {got}); the file was deleted")]
67 Checksum { name: String, got: String },
68 #[error("download stopped at {done} of {total} (run it again to resume)")]
69 Incomplete { done: String, total: String },
70 #[error("download cancelled at {done} of {total} (run it again to resume)")]
71 Cancelled { done: String, total: String },
72 #[error("consent was given for another model")]
73 Consent,
74 #[error("{0:?} is not a Hugging Face model id (owner/name)")]
75 BadRepo(String),
76 #[error("{0}")]
77 Model(String),
78 /// This CPU lacks instructions the compiled-in llama.cpp uses; see [`cpu`].
79 #[error("local models need CPU instructions this one lacks: {}", missing.join(", "))]
80 UnsupportedCpu { missing: Vec<String> },
81 /// `cpu-dispatch` builds: no llama.cpp CPU backend library could be loaded.
82 #[error("no llama.cpp CPU backend found in {} (ship the ggml-cpu libraries there)", dir.display())]
83 NoCpuBackend { dir: std::path::PathBuf },
84}
85
86pub type Result<T> = std::result::Result<T, Error>;