# 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:
have `adapter.type` = `lora` and `adapter.lora.alpha`, the keys
[llama.cpp](https://github.com/ggml-org/llama.cpp)'s `convert_lora_to_gguf.py` writes.
`gguf::read_header` reads any GGUF header (models and adapters; `read_info` still fails on
adapters, which have no blocks). A PEFT adapter is an `adapter_config.json` with `peft_type`
LORA next to `adapter_model.safetensors` (or `.bin`), the layout vLLM serves.
Adapters are never models: `Hub::models` reads the header of every file whose name hints at an
adapter (a `lora` or `adapter` word) and leaves real adapters out, so fits never rank one, and
`ModelStore::list` skips `adapters/` and hinted GGUFs whose header says adapter. Hub searches
leave adapter repos out unless `Search::with_adapters(true)`. Adapters download into
`adapters/<id>/` under the store with the same consent, resume and SHA-256 checks as models, plus
an `adapter.json` with the repo, format and base model (no paths).
## Your own model files
For weights a person brings themselves (a fine-tune made on another machine, a trainer's
output), `artifact::inspect` reads and hashes the files and `ModelStore::import` places them in
the store with a provenance record next to them.
```rust
use std::path::PathBuf;
use weirdnet_model::artifact::{self, ORIGIN_UPLOAD, Provenance};
use weirdnet_model::{Imported, ModelStore};
let store = ModelStore::from_env();
// One directory, or files one by one (a GGUF, every part of a split GGUF).
let found = artifact::inspect(&[PathBuf::from("out/qwen3-8b-support-r3.Q4_K_M.gguf")])?;
println!("{:?} {:?} {:?} {}", found.kind, found.format, found.architecture, found.revision);
let record = Provenance::for_artifact(&found, "support-r3", ORIGIN_UPLOAD).with_license("apache-2.0");
// `staged` holds the files under their artifact paths, outside the store (an upload's parts).
let staged = PathBuf::from("/tmp/upload-staging");
match store.import(&staged, &found, &record, "support-r3")? {
Imported::Model(m) => println!("{} in {:?}", m.id, m.files),
other => println!("{other:?}"),
}
println!("{} bytes free, {} bytes uploaded", store.free_bytes(), store.bytes_by_origin(ORIGIN_UPLOAD));
```
What `inspect` accepts: GGUF models (split ones with every part), GGUF LoRA adapters and
multimodal projectors (`GgufKind::Projector`: `general.type` = `mmproj` or the `clip`
architecture), safetensors model directories (`config.json` with `architectures`, an index that
covers every shard), MLX directories (MLX-LM's `format` = `mlx` metadata or a `quantization`
block in `config.json`) and PEFT LoRA directories. Weights are data, never code: pickled weights
(`.bin`, `.pt`, `.pth`, `.ckpt`, `.pkl`), Python files and other code are refused, and so are
symlinks, hidden files given by name and anything that isn't a regular file. Headers are read
with size caps before anything is hashed; `artifact::early_header` checks a header from the
first bytes of a file still being received. The revision is `sha256:` and the first 16 hex
digits of the SHA-256 of the sorted `path:sha256` lines.
`import` checks the staged files against the artifact (size, SHA-256 and headers), sets the
record's id, kind, files and revision from the bytes, moves the files into a work directory
next to the target and renames it into place last. Layout: a GGUF model in `<id>/`, an adapter in
`adapters/<id>/` (with its `adapter.json`), a projector in `projectors/<id>/`, a safetensors
model in `safetensors/<id>/` and an MLX model in `mlx/<id>/`. `list` keeps listing GGUF models
(an imported one under its id, with `provenance`); `dir_models` lists the safetensors and MLX
directories, `projectors` the projectors. An adapter's `size` leaves `adapter.json` and
`provenance.json` out. `free_bytes` and `disk_space` read the store's filesystem.
## CPU support
llama.cpp's build turns on AVX, AVX2, BMI2, FMA and F16C by default, so a plain `llama` build runs
only on x86 CPUs that have them (Intel Haswell and AMD Excavator, 2013 to 2015, and later). Two
things keep older CPUs from crashing with an illegal instruction:
- **The guard, always on.** `build.rs` works out which instructions the linked llama.cpp contains
(from the target features, `target-cpu=native`, `GGML_*` environment variables and
`SOURCE_DATE_EPOCH`, the way `llama-cpp-sys-2` configures it). `LlamaEngine::load` calls
`cpu::check()` before any llama.cpp code runs and returns `Error::UnsupportedCpu { missing }` on a
CPU without them. Apps can call `cpu::check()` themselves to hide local models, or to offer
another machine instead. `examples/model.rs cpu` prints the result.
- **`cpu-dispatch`, for release binaries that should run local models on any CPU.** It enables
llama-cpp-2's `dynamic-backends` (`GGML_BACKEND_DL` plus `GGML_CPU_ALL_VARIANTS`): ggml is built
once per CPU level (x86: baseline x86-64, SSE4.2, Sandy Bridge, Haswell, AVX-512 levels up to
Sapphire Rapids; ARM: armv8.0 to armv9.2, Apple M1 to M4) and asks each library at load time
whether this CPU can run it, keeping the best. Speed on a modern CPU is the same as the static
build, because the same AVX2 code runs.
`cpu-dispatch` makes llama.cpp shared libraries that the app has to ship:
- `libllama`, `libllama-common`, `libggml`, `libggml-base` (`.so`, `.dylib` or `.dll`), which the
executable links against. On Linux and macOS, point the executable's rpath at them (for example
`-C link-arg=-Wl,-rpath,$ORIGIN` on Linux); on Windows put the DLLs next to the `.exe`. Without
them the app does not start at all, not just local models.
- one `libggml-cpu-<level>` library per CPU level (`.so` on Linux and macOS, `.dll` on Windows),
plus `libggml-vulkan` or `libggml-metal` with those features. They load from
`llama::backends_dir()`: the directory given to `llama::set_backends_dir`, else
`WEIRDNET_GGML_BACKENDS_DIR`, else the executable's directory (a development build falls back to
where `llama-cpp-sys-2` built them). The working directory is never searched. If none loads,
loading a model fails with `Error::NoCpuBackend`.
On x86-64 Linux (llama-cpp-sys-2 0.1.161, release, stripped) that is a 4.3 MB executable plus four
shared libraries (11 MB) and 14 CPU libraries (20 MB), 35 MB in all, against 9.4 MB for the static
example. Ship the versioned names the executable asks for (`libllama.so.0` and so on).
`llama-cpp-sys-2` 0.1.161's shared-library build does not work with Rust 1.100's build directory
layout (its build script fails with "No such file or directory"), so build `cpu-dispatch` with Rust
1.99 for now. A second build in the same target directory can also fail with "File exists" on the
library symlinks it left there; deleting `target/<profile>/{,deps/,examples/}lib*.so*` symlinks
between builds avoids it. The static `llama` build has neither problem.
Without `cpu-dispatch`, a static build can still run everywhere at a speed cost by building ggml
for an older level, for example `GGML_AVX=OFF GGML_AVX2=OFF GGML_BMI2=OFF GGML_FMA=OFF
GGML_F16C=OFF` in the build environment (SSE4.2, Nehalem and later). The guard follows those
variables. ARM builds need nothing: `llama-cpp-sys-2` builds for armv8-a on Linux, and macOS uses
the compiler's Apple M1 baseline. Do not use `target-cpu=native` for anything you distribute.
## License
BSD-3-Clause.