# weirdnet-model
Local models for [WeirdBrains](https://weirdbrains.com) apps: download a GGUF from Hugging Face (only after consent, with
resume and a SHA-256 check), load it with llama.cpp, and stream tokens from a worker thread you can
cancel. App-agnostic; the model is optional at build time.
- `catalog`: Gemma 4 E2B (phones, small laptops) and E4B (desktop) GGUF defaults, with repo, file,
size and checksum. `catalog::default_for(Device)` picks one.
- `ModelStore`, `download`, `download_parts`: a cache directory (`WEIRDNET_MODELS_DIR` overrides
it), a `Consent` the user must give for exactly that model, `Progress` callbacks, `.part` resume
and verification, split models part by part; `ModelStore::list` and `remove_local` for what is
on disk.
- `hf`: search the Hub for GGUF repos, list a repo's models (split GGUFs grouped into one model,
quantization read from the file name) and read a file's header for KV sizing. `HfToken` for
gated and private repos, read on the downloading node only.
- `fit`: pick the largest file whose weights plus KV cache fit the node's memory budget.
- `prompt`: chat turns rendered through the model's own chat template, and a streaming parser that
turns `<tool_call>` tags into tool calls.
- `Worker`: one thread owns the engine; `Worker::generate` returns a `Generation` that streams
`Chunk`s and can be cancelled from any thread. The `Engine` trait lets tests use a mock.
- `llama` feature: `llama::LlamaEngine` (llama.cpp, CPU). `vulkan` and `metal` add GPU offload.
Building needs cmake and a C++ compiler.
- `cpu-dispatch` feature: llama.cpp built for every CPU level and picked at run time (see below).
- `cpu`: whether this CPU can run the compiled-in llama.cpp. Loading a model checks it first.
- `agent` feature: `agent::LocalModel` implements `weirdnet_agent::ModelBackend`, so the agent
loop can run on a local model with streamed text and parsed tool calls (`weirdnet-agent` 0.5;
text only: messages with image or audio parts are refused).
- `hw`: hardware detection and model sizing (see below).
```toml
weirdnet-model = { version = "0.5", features = ["llama", "agent"] }
```
Tests never download a model. `WEIRDNET_TEST_GGUF=/path/model.gguf cargo test --features llama -- --ignored`
runs the real-model check.
## Hardware and sizing
`hw::detect()` reports the OS, CPU threads, system memory and GPUs: NVIDIA through `nvidia-smi`,
AMD through sysfs, and Apple silicon. Devices that share system memory (Apple, GB10, AMD APUs) are
marked unified. It never fails; a missing tool means fewer devices.
`hw::budget(&hw, memory_budget, reserve)` is the memory models may use: 85% of VRAM, or of
unified memory after an OS reserve; both can be overridden. `hw::plan(&hw, budget)` picks a main
chat model, a smaller stand-in and an embedding model from `hw::catalog()`, a tiered catalog with
each model's footprint; the Gemma entries are this crate's pinned `ModelSpec`s, so they download
through the consent gate.
```rust
let hw = weirdnet_model::hw::detect();
let plan = weirdnet_model::hw::plan(&hw, weirdnet_model::hw::budget(&hw, None, None));
```
## Hub search, fit and downloads
```rust
use weirdnet_model::{ModelStore, Consent, download_parts, fit, hf::Search, hw};
let store = ModelStore::from_env();
let hub = store.hub_client();
let hits = hub.search(&Search::new("qwen3 8b").with_limit(10))?;
let repo = &hits[0].id;
let models = hub.models(repo)?; // split GGUFs come back as one model with all its parts
let info = hub.gguf_info(repo, &models[0].first().name)?; // header only, through a Range request
let hw = hw::detect();
let opts = fit::FitOptions::default().with_slots(2).with_info(&info);
let best = fit::choose(&models, &hw::budget(&hw, None, None), &opts).expect("something fits");
let parts = best.model.specs(repo)?;
// Show the user what will be fetched, then: