Embedding models
models, modules/embeddings, §M14.22 E2b
Ez az oldal a nucleus saját dokumentációjából készül, ezért angol nyelvű.
CREATE MODEL [IF NOT EXISTS] m USING provider [WITH (option = value, …)] defines a model (catalog 00 15 <name>); DROP MODEL [IF EXISTS] m. The provider is registered for the process
(models::ModelProvider, Registry::add_model_provider; modules reach
it as ext::models): it checks its options — refusing any it does not
know, 22023 — and says the model’s dimensions (1 to 16,000) and batch;
it never reaches the network then. Built in: hashing (options
dimensions, default 256, and analyzer, default simple): an
analyzer’s terms hashed into buckets (feature hashing without signs, so
words never cancel), counted and scaled to length 1 — no model, no
network. The embeddings module (in the nucleus tool) adds openai
(any OpenAI-compatible endpoint), cohere and voyage (its README).
Three options are the core’s: secret (a secret, §9, whose value the
provider gets when it calls; naming one needs USAGE on it, and a
secret is not dropped while a model names it, 2BP01), query_prefix
and document_prefix.
Where a model may call. A provider names the addresses a model’s
options point it at beside its own (ModelProvider::urls: the
embeddings module’s url). A model made by an administrator may call
any; anyone else’s only those under the database’s model_urls
(ALTER DATABASE nucleus SET model_urls = 'https://api.mistral.ai/v1, http://localhost:11434/v1', an administrator’s): the same scheme and
host (with port), and a path under the prefix’s at a /
(models::url_under; …/v1 does not admit …/v10, nor
api.example.com api.example.com.evil.net). Checked when the model
is made and at every call by its owner’s standing (an owner without a
role record is the unclaimed database’s administrator), so a URL taken
off the list is not called again (42501, a hint naming the setting).
A provider’s own address (OpenAI’s, Cohere’s, Voyage’s) needs no
listing; plain HTTP still only to loopback (nucleus-http). So a role
that may make models cannot point one at the server’s network — an
internal service, a cloud’s metadata — unless an administrator lists
it.
embed(text, m [, 'query' | 'document']) returns the text’s vector
(dimensions unconstrained; a vector(n) column checks them, 22000);
a document by default. The model’s prefix for the kind is put before
the text, and a provider whose API takes the kind itself (Cohere,
Voyage) is told. Calling one needs USAGE on the model (GRANT USAGE ON MODEL m TO r; its maker owns it; making one needs CREATE on the
database). embed is stable: in a ranking (ORDER BY v <=> embed($1, m, 'query')) the query’s vector is computed once. Vectors are kept in
a cache for the process — by SHA-256 of the model’s definition, the kind
and the text; 32 MB, the older half dropped when full — so a text is not
sent twice while kept, nor twice in one call; the vectorized executor
sends a batch’s texts in calls of the model’s batch (call_batch). A
provider’s answer is checked: as many vectors as texts, each of the
model’s dimensions, finite. Failures: 58030 where trying again may
help (no connection, 429, 5xx — tried once more first), 55000 where it
will not (a refused key, an unknown model). A remote call ends within
the time its statement has left (models::Request::time_left, from
statement_timeout; ext::time_left on the statement’s own thread),
at most five minutes, the retry’s pause included — nucleus-http’s
Request::total bounds a whole exchange, so a server dripping a byte
within each read’s timeout cannot hold a call.
nc_catalog.models (name, owner, provider, dimensions,
max_batch, secret, query_prefix, document_prefix, options, files, bytes,
created, and the process’s calls, texts, cached, errors,
total_ms, tokens, throttled_ms, and the limits of §8.27; the
models the current role may call) and
nc_catalog.model_providers (name, remote). Tests:
tests/models.rs (hashing end to end, privileges, prefixes, the
secret given, one call for a ranking, the cache, a column table’s 2,000
rows in batches), modules/embeddings/tests/providers.rs (servers on
loopback answering as OpenAI, Cohere and Voyage: the requests, the key,
answers out of order, a 429 tried again, a 401 without the key in the
error, a server down, a dripping server cut off at the statement’s
statement_timeout).
Local models (modules/local-models, §M14.22 E3, in the nucleus
tool): CREATE MODEL m USING local WITH (huggingface = 'org/name' [, revision = 'main'] | path = '/dir' [, pooling = 'cls' | 'mean'] [, threads = n] [, batch = n]). The provider fetches the model’s files
when it is made (ModelProvider::fetch, the only time a provider reads
files or the network outside a call): tokenizer.json, config.json,
model.safetensors and 1_Pooling/config.json where there is one —
from Hugging Face (https://huggingface.co/<repo>/resolve/<revision>/…,
redirects followed to https://, a large file checked against the
SHA-256 the hub announces in X-Linked-Etag; another endpoint, and a
path on the server, only for administrators, 42501). A file may be
at most a quarter of the machine’s memory, and 8 GB (it arrives in
memory, and its weights run there); the download, all its files, ends
within the statement’s time, and an hour. The core keeps
the files with the model by content — 00 16 <SHA-256> <chunk u32>, 32
KiB chunks; the record lists their names, digests and lengths — so a
backup or a replica has them, two models from the same files store them
once, and DROP MODEL removes them with the last model that keeps
them. define loads the model (a model that cannot run is refused when
made, 55000) and gives its dimensions; a call reads the files through
models::Files (their digest checked) only when the process has not
loaded them (four models kept, by Files::id), and runs them on
threads (the machine’s cores by default). nc_catalog.models shows
files and bytes; model_providers shows local as not remote.
Its tokenizers read a Hugging Face tokenizer.json — WordPiece behind
BERT’s normalizer and pre-tokenizer, Unigram behind SentencePiece’s
precompiled character map (by grapheme cluster, UAX #29) and Metaspace —
and give the ids tokenizers gives: special tokens in a text are those
tokens, a text is cut with its closing token kept, and anything else a
file may hold is refused by name. BERT’s character classes are Unicode
8’s, as tokenizers’ are (scripts/gen-tokenizer-tables.py).
Checked by bench/embed/tokens.py (corpora, every code point, random
mixtures: all the same) and modules/local-models/tests/.
Its encoder reads config.json (BERT, RoBERTa, XLM-RoBERTa; GELU,
absolute positions, a RoBERTa’s starting after its padding token’s) and
the safetensors weights (F32, F16, BF16; the linear layers packed once
for the matrix product), pools by sentence-transformers’ 1_Pooling
(the first token or the mean) and scales to length 1. A batch’s texts
are one matrix of their tokens, end to end (no padding, no mask), cut to
the model’s 512 positions; threads take whole texts, about equal work
each, so a text’s vector is the same bits alone, in any batch and on any
number of threads. The kernels (ext::kernels, from nucleus-kernels):
matmul over a PackedB (BLIS’s structure: 8 × 12 register tiles on
NEON, 6 × 16 on x86; every output one chain of fused multiply-adds in
order, so the same bits at every CPU level, held to the naive chain by
its test), gelu (exact, by a rational erf), softmax_rows,
layer_norm, add_assign. The x86 Avx2 level now needs FMA too.
Checked by bench/embed/encoder.py against ONNX Runtime on the
publishers’ f32 exports.