1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//! Cyberbrain embed: static token embeddings in the model2vec format, implemented in-tree
//! (SPEC §6).
//!
//! Original work, copyright 2026 Krynex Labs, licensed FSL-1.1-ALv2. Written from
//! `docs/SPEC.md` alone under the clean-room boundary in §0.
//!
//! # What this crate does
//!
//! A model2vec-format model is two files: a `tokenizer.json` (HuggingFace tokenizers
//! format) and a `model.safetensors` holding one embedding matrix of shape
//! `[vocab, dim]`. The whole forward pass is
//!
//! 1. tokenise the text (no special tokens),
//! 2. look up one row per token id, skipping the unknown token,
//! 3. mean-pool the rows,
//! 4. L2-normalise.
//!
//! There is no transformer. Producing a vector costs a few microseconds.
//!
//! # What this crate never does
//!
//! **No network I/O of any kind.** Model artefacts are loaded from a local path handed in by
//! the caller (the policy crate's registered `ModelDownload` egress path, SPEC §12.1). Every
//! load verifies a blake3 hash of both files against a manifest and refuses on mismatch.
//! There is no "verify later", no "warn and continue" and no default download location.
//!
//! # Degenerate inputs
//!
//! An empty string, or a string whose every token is unknown to the model, has no rows to
//! pool. Dividing by zero there would yield a NaN vector, and a NaN that reaches a cosine
//! comparison silently corrupts every ranking it touches. Instead such inputs yield the
//! **all-zero vector** and [`Embedding::tokens_known`] reports `0`, so a caller can tell the
//! case apart and log it (SPEC §14.5: a branch that declines to act says why). The zero
//! vector has cosine 0 against everything, which ranks it last rather than randomly.
//! [`is_zero`] is the check callers should use before running a semantic search with such a
//! query.
pub use ;
pub use ;
pub use is_zero;
/// The only pooling this crate implements. Part of every profile id, so a future pooling
/// change cannot be confused with vectors produced by this one.
pub const POOLING: &str = "mean";
/// The tensor name a model2vec artefact uses for its embedding matrix.
pub const EMBEDDINGS_TENSOR: &str = "embeddings";