hub — design
Checkpoints in safetensors files or GGUF files (llama.cpp's format), read in
Agentica, and nn/ layers built from them by tensor name. Local files only.
Depends on tensor/ and nn/.
API #
Hub.open(path) -> Result[Checkpoint] a .safetensors file, a .gguf file, or a sharded .index.json
Hub.names(c) -> List[String] sorted
Hub.has?(c, name) -> Bool
Hub.tensor(c, name, backend, dtype = F32) -> Result[Tensor] F32/F16/BF16 and quantized as F32 (F16 with dtype F16: ggml half weight; a quantized tensor with its own dtype: ggml blocks), else the file's dtype (F64 on ggml: F32)
Hub.tensors(c, backend) -> Result[Map[String, Tensor]] every tensor, read as `tensor` reads one
Hub.shape(c, name) -> Result[List[Int]] from the header
Hub.dtype(c, name) -> Result[String] how the file stores it, from the header
Hub.source(c, name) -> Result[Shards.Source] where an F32/F16/BF16 tensor's values are in its file (for the writer)
Hub.metadata(c) -> Map[file, Map[String, String]] each shard's __metadata__
Load.dense(c, prefix, backend, weights = F32) {prefix}.weight [out, in] kept as stored (Dense.out_in); {prefix}.bias if present
Load.conv1d(c, prefix, stride, padding, backend) {prefix}.weight [out, in, kernel] as stored; bias if present
Load.layer_norm(c, prefix, eps, backend) {prefix}.weight, {prefix}.bias; or {prefix}.gamma, {prefix}.beta when there is no .weight (BERT)
Load.embedding(c, name, backend, weights = F32) [rows, width]
Load.encoder_block(c, BlockNames { norm1, query, key, value, output, norm2, up, down },
heads, activation, eps, backend)
Load.decoder_block(c, DecoderBlockNames { norm1, query, key, value, output,
norm2, cross_query, cross_key, cross_value, cross_output,
norm3, up, down }, heads, activation, eps, backend)Whisper's names for decoder block N (prefix model.decoder.layers.N. in
Hugging Face's WhisperForConditionalGeneration):
norm1 self_attn_layer_norm query..output self_attn.{q,k,v,out}_proj
norm2 encoder_attn_layer_norm cross_* encoder_attn.{q,k,v,out}_proj
norm3 final_layer_norm up, down fc1, fc2The test fixtures drop the model. part of the prefix
(decoder.layers.0.); a real checkpoint keeps it.
Decisions #
- The format is checked whole at
open: the 8-byte header length (at most 100 MB, inside the file), a UTF-8 JSON object starting with{, each entry'sdtype,shapeanddata_offsets, a byte count that matches the shape, and offsets that tile the buffer (sorted, no gap, overlap or tail). An error names the file and the tensor. A repeated key cannot be seen:JSON.parsekeeps the last one. openreads the header and no data. Itstats the file, reads the 8-byte length and checks it against the size, then the header, and keeps where the data starts (Shard.base). A tensor is read when asked for, so the size of the file is no limit. An F32, F16 or BF16 tensor goes toTensor.from_file: the backend reads its byte range itself, with no Agentica list in between, and checks the range against the file again. Any other dtype is read here, from its offset (File.seek, Agentica 0.37.0); those tensors are small (indices, masks). A file replaced betweenopenandtensorgives the weights of the new file at the old offsets (wrong, not a crash: C checks the range against the size of the file at each read). There is no stat or mtime check.- The index's
weight_mapdecides. A sharded checkpoint opens every shard it names; a shard must be a plain file name in the index's folder (no/,\,..or absolute path) and hold the tensors mapped to it. Symlinks are followed on purpose: the Hugging Face cache links its snapshot files to blobs. Shards and a single file are keyed by file name. - Dtypes:
F32,F16andBF16load asF32, the compute dtype, exactly (tensor/DESIGN.md§2).F64,I8,I16,I32,I64,U8andBOOLkeep theirtensor/dtype and decode bit for bit. The wider unsigned types and the 8-bit floats have notensor/dtype: the file opens, and only that tensor is refused. Infinities and NaN decode, but a NaN payload is not kept bit for bit. The reference backend holds doubles, so anI64past 2^53 loses precision there. - The loader converts, nothing else. Float weights become
F32(tensor/DESIGN.md§6); a dense weight stays[out, in]as stored, and itsNn.Densesays so (out_in), because ggml multiplies by that layout with no copy and copies[in, out]on every run; a missing bias isnone(Whisper'sk_proj). Names are the caller's: Whisper's or BERT's live with the model code. weights: F16is opt-in.Load.dense,Load.embeddingand the blocks take it (Asr.openandQwen3.openpass it on): a rank-2 weight with at leastHALF_MIN_ELEMENTS(100000) values is read as a half weight (tensor/DESIGN.md§7), everything else staysF32; a smaller matrix gains nothing by halving. Only the tokens of a model are loaded this way, not its positional tables, whichNn.positionalslices.Nn.embeddingwidens the rows of a half table withto_dtype. Another backend than ggml, or a dtype other thanF32orF16, is an error. A file that is notF32,F16orBF16(anF64one) has no half form and staysF32. ABF16value under about 6e-8 (F16's smallest) becomes zero, and under 6e-5 it loses digits.WEIGHTS=f16(Load.weights_from_env) sets it for the tools.weights: BF16works on ggml, Torch and XLA. On Torch and XLA the same matrices are read asBF16(tensor/DESIGN.md§7): abfloat16checkpoint keeps its words, anF32orF16one rounds once, and a value beyond the largestBF16is an error. On ggml they are read asF16, as underweights: F16(a value beyond 65504 is an error that says so): ggml would multiply aBF16weight through anF32copy of it in every graph, about 1 GB for the head of Llama 3.2 1B, or, with its ownBF16product, round the activations toBF16, which moved the logits of that model by 0.039 (ml#444).WEIGHTS=bf16sets it for the tools.weights: Q8_0is opt-in too, and it keeps what the file holds. A rank-2 weight that the file stores as Q8_0 is read as a quantized weight (Hub.tensorwithdtype: Q8_0: ggml only, about a quarter of the memory ofF32); everything else (norms, a Q8_0 tensor of another rank, a tensor that is not Q8_0 in its file) staysF32, because quantizing is not done here (a file is quantized by llama.cpp'sllama-quantize, from the F16 file thattools/to_gguf.agcwrites). Without the option a Q8_0 file loads asF32: the dequantized values are exact, four times the memory, on any backend. Another backend than ggml withweights: Q8_0is an error, and so isHub.tensorwithdtype: Q8_0on a tensor that the file does not store as Q8_0.WEIGHTS=q8_0sets it for the tools. The product of a quantized weight is good to the half step of ggml's rounding of the activations to Q8_0 blocks, not toF32's exactness (tensor/DESIGN.md§7);Hub.dtypesays how the file stores a tensor.weights: Q4_Kkeeps a bit width per tensor. A Q4_K_M file mixes Q4_K and Q6_K matrices (andF32norms), so one request covers both: underweights: Q4_Ka rank-2 weight stored as Q4_K stays Q4_K and one stored as Q6_K stays Q6_K, each as the file has it (Load.kept);weights: Q8_0keeps no K tensor andweights: Q4_Kno Q8_0 one, and Q6_K is no request of its own (WEIGHTS=q4_ksets it for the tools). A tensor stored as another packed type (Q8_0 underweights: Q4_K) is dequantized toF32.Hub.tensorwithdtype: Q4_KorQ6_Kis that one type, and an error on a tensor stored as another. A K row holds whole blocks of 256 values: llama.cpp stores a matrix whose rows do not in another type (Q5_0 and the like), which is not read, so loading such a file is an error naming the tensor.
GGUF #
gguf.agc reads version 3 files and the qwen3 architecture only
(another one is an error that names it). Little-endian files only, which is
every file in use. Hub.open takes a file for GGUF by its .gguf extension,
not by its magic. It is the same design as safetensors:
- Checked whole at
open: the magic, the version, the counts against the size of the file, every string and array length, the alignment (a power of two, 32 unlessgeneral.alignmentsays), and for each tensor the number of dimensions (at most 4), its type, its size from its shape and type, its offset (aligned, inside the data, clear of the others) and its name (once). The header is read in one piece (8 MiB, then twice that until it fits, at most 256 MiB); an array is skipped, not decoded, so a vocabulary of 150000 strings costs a length read each, and is read only when asked for. - One reader for both formats. A GGUF becomes a
Shards.Shard(shard.agc, which safetensors fills too), with the names of Hugging Face's checkpoint (blk.N.attn_q.weightismodel.layers.N.self_attn.q_proj.weight; the table isBLOCK_NAMESandTOP_NAMES) and the shape the other way round (GGUF lists the first dimension last). SoHub.tensor,Load.*and the half path (weights: F16) work on it unchanged: an F32, F16 or BF16 tensor is oneTensor.from_fileof its byte range. No row permutation applies to Qwen3 (checked against the F32 file of llama.cpp's converter, value for value); a Llama-style rope would need one. - The config is the file's key-values (
Qwen3.gguf_config): the key-values are typed (Gguf.int,Gguf.float,Gguf.string;Gguf.stringsandGguf.intsread an array from the file, whichopenskipped), andHub.Checkpoint.ggufkeeps the header.rope.scaling.*is refused. - A tied model's loaded copy holds its table twice. llama.cpp writes
output.weightfor a tied model too (the file is as large as the safetensors one, which keepslm_head.weightas well), and the loader then reads it as an untied head: the loaded Qwen3-0.6B holds the table twice, 0.3 GB more withweights: F16and 0.6 GB more inF32. A file withoutoutput.weightis read as tied. - Types. F32, F16, BF16 and Q8_0 (read by the backend), and F64 and the
integers (as in safetensors). A quantized type (Q8_0, Q4_K and the others)
is sized and checked in blocks (a tensor may fill the data). A Q8_0, Q4_K or
Q6_K tensor reads as
F32(Tensor.from_filewith its format, dequantized by the backend, as ggml does) or, with the dtype of its type, as a quantized weight that keeps its blocks on ggml (tensor/DESIGN.md§7). The other quantized types (Q4_0, Q5_K...) are refused when read: they have notensor/dtype yet. A type that is not sized here (the IQ types) refuses the whole file.
Writing #
safetensors_write.agc writes one safetensors file from tensors in memory:
write(path, tensors, metadata) takes F32 tensors by name, sorts the names,
builds the header in Agentica (padded with spaces to a multiple of 8, as the
safetensors library does) and appends each tensor from its backend
(Tensor.append_file), so the same tensors give the same bytes and no value
goes through a list. It is for the trained part of a model (train/
Params), which is in memory and not in a source file; Hub.tensors reads a
file back as a map by name.
gguf_write.agc writes version 3 files: write(path, fields, tensors) takes
the key-values (U32, F32, Bool, Text, Texts, Ints) and the
tensors, each as the place of its values in a file (Hub.source) and the
format to write (F32, F16 or BF16). The header is built in Agentica and
written first; each tensor is then converted straight from its source range
into its place by Tensor.convert_file, aligned to 32 as llama.cpp does, so
no value goes through a list. The file is written as path.part and renamed
when whole (and removed when the rename fails). Gguf.gguf_name is the other
way of Gguf.shard's name map; its layer is a number with no sign and no
leading zero.
transformers/src/qwen3_gguf.agc and transformers/tools/to_gguf.agc use it to
write a Qwen3 folder: against llama.cpp's own converter, the F16 and F32 files
of Qwen3-0.6B have every shared key-value and all 310 tensors byte for byte
(llama.cpp also writes output.weight, a copy of the table, and a few
general.* keys; a file without output.weight is read as tied, llama.cpp
falling back to token_embd.weight), and llama.cpp loads the F16 file.
Cost #
Measured on the machine of benchmarks/README.md (Ryzen 5 3550H), F32 weights:
openreads the header only.- A float tensor on libtorch or ggml costs one
preadand one copy into the tensor's memory, and no Agentica value. Whisper tiny (151 MB) loads in 0.3-0.45 s and the process peaks at about 0.5 GB while it loads. Whisper medium (3 GB of F32) loads in about 8 s on the CPU and 24 s on Vulkan, and the process peaks at 4 GB on the CPU (the weights of Vulkan are outside the process). - On the reference backend a float still costs about 0.65 µs and 110 bytes of
peak memory per element (a list of
Float), and a read drops the bytes before the tensor: it is for tests and small files.
The tests use files of a few hundred bytes that test/peer.py (Python,
struct, json and math only) writes at test time into a temporary
folder, with the forward pass of a decoder block in doubles; run them from
hub/.