moongguf

API reference

moongguf

One package. Files, metadata tables, tensor indexes and the bytes behind them, with a refusal for everything that is not a GGUF file.

Written from pkg.generated.mbti and the source comments on every build: 82 public names, in three groups.

letlimits

pub let limits : Limits

The preset a reader starts from: roomy enough for every file published so far, tight enough that a corrupt count is refused instead of allocated.

letmagic

pub let magic : Bytes

The four bytes every GGUF file starts with.

fnis_gguf

pub fn is_gguf(BytesView) -> Bool

Whether bytes start with the magic, which is all that can be told without reading the rest.

suberrorRefused

pub(all) suberror Refused

Why bytes are not a GGUF file this library can read, or why a file it holds cannot be written. The variants a caller is meant to act on carry the numbers that explain them: a ceiling and what went past it, or the position something was expected at and the position found.

  • Truncated(at~ : Int64, want~ : Int32, got~ : Int32)
  • Magic(got~ : Bytes)
  • Version(got~ : UInt32)
  • ReadOnly(version~ : Version)
  • Untyped(got~ : UInt32)
  • Unencoded(got~ : UInt32)
  • Entries(limit~ : Int32, got~ : Int32)
  • Tensors(limit~ : Int32, got~ : Int32)
  • Text(limit~ : Int32, got~ : Int32)
  • Items(limit~ : Int32, got~ : Int32)
  • Nesting(limit~ : Int32, got~ : Int32)
  • Alignment(got~ : UInt32)
  • Duplicate(name~ : String)
  • Missing(name~ : String)
  • Blocks(name~ : String, block~ : Int32, got~ : UInt64)
  • Dims(limit~ : Int32, got~ : Int32)
  • Volume(name~ : String)
  • Malformed(at~ : Int64)
  • Mismatch(at~ : Int64, want~ : ValueType, got~ : ValueType)
  • Offset(name~ : String, want~ : UInt64, got~ : UInt64)
  • Absent(name~ : String)
  • Short(name~ : String, want~ : Int64, got~ : Int64)
  • Large(limit~ : Int32, got~ : Int64)
  • Empty

The error is a Show: its text is what a caller prints.

enumByteOrder

The byte order a file's numbers are stored in. Files are little-endian; a big-endian file is one converted for a machine that needs it, and reads and writes the same way once this says which it is.

pub(all) enum ByteOrder {
  • Little
  • Big

The type derives Eq, Debug.

structEntry

One metadata pair as it sits in the file: its key, its value, and where the pair starts. The position is what a dump of a file shows beside a pair, and what tells two pairs with the same key apart when reading a file that has them.

pub(all) struct Entry {
  • key : String
  • value : Value
  • at : Int64

The type derives Eq, Debug.

structExtent

Where a tensor's bytes sit: at from the start of the data section, and how many bytes they take. The data section itself starts at File::data_start.

pub(all) struct Extent {
  • at : Int64
  • length : Int64

The type derives Eq, Debug.

structFile

A GGUF file: the header, the metadata table, the tensor index, and — when the bytes of the data section were handed over with the rest — the tensor data itself. Reading never copies the tensor bytes. data hands out a view into the bytes the file was parsed from, so a gigabyte model costs its metadata and no more. A file too large for one Bytes is read in two parts: the index from the head of the file, and each tensor's bytes as they are needed, which shard takes a view of.

pub struct File {
  • // private fields
pub fn File::add(Self, Tensor, BytesView) -> Unit raise Refused

Put a tensor into the file, with its bytes. The bytes stay where the caller put them: a file holds views, and writing it copies them out once. The tensor lands at the end of the data section, where the one before it ends and the alignment is satisfied.

pub fn File::add_index(Self, Tensor) -> Unit raise Refused

Put a tensor into the file's index without its bytes, for a caller that writes the data section itself.

pub fn File::alignment(Self) -> UInt32

The alignment the data section starts on, from general.alignment or the format's default of 32.

pub fn File::data(Self, StringView) -> BytesView?

The bytes of a tensor, as a view into the bytes the file was parsed from. None when those bytes are not in hand — a file parsed from its index alone still answers everything about the tensor except its data.

pub fn File::data_start(Self) -> Int64 raise Refused

Where the data section starts: the end of the index, rounded up to the alignment. A file that was read knows this from its own bytes. A file built here finds it by writing its index, which is what a caller streaming a large file needs before it can place the first tensor.

pub fn File::encode(Self) -> Bytes raise Refused

The whole file: the index, then every tensor's bytes at the offset the index recorded, each followed by the zeroes that reach the alignment. One Bytes holds the result, so a file past what a Bytes can hold is refused rather than truncated: encode_index and add_index are there for writing such a file in pieces.

pub fn File::encode_index(Self) -> Bytes raise Refused

The index as bytes: the header, the metadata table, the tensor index, and the zeroes that bring the length up to the alignment. This is the first part of a file, and all of it a reader needs to answer what a model holds without its weights.

pub fn File::extent(Self, StringView) -> Extent raise Refused

Where a tensor's bytes sit and how far they run.

pub fn File::get(Self, StringView) -> Value?

The value under a metadata key.

pub fn File::header(Self) -> Header

The magic, the version and the two counts.

pub fn File::meta(Self) -> Meta

The metadata table.

pub fn File::new(alignment? : UInt32) -> Self

An empty version 3 file, ready for metadata and tensors to be put into it. An alignment other than the format's 32 is recorded as general.alignment, because a reader would otherwise lay the data section out at 32 and find every tensor's bytes in the wrong place.

pub fn File::order(Self) -> ByteOrder

The byte order the file's numbers are stored in.

pub fn File::parse(BytesView, limits? : Limits) -> Self raise Refused

Read a file from its bytes. The bytes may be the whole file, or just as much of it as holds the index — the header, the metadata table and the tensor index. Tensor bytes are only needed when something asks for them.

pub fn File::remove(Self, StringView) -> Unit

Drop a metadata key. Dropping general.alignment puts the alignment back to the format's 32.

pub fn File::set(Self, String, Value) -> Unit raise Refused

Put a value under a metadata key, replacing one already there. general.alignment is the one key with a meaning beyond its value: setting it moves where the data section starts, so the tensors after it move too. Only a power of two fits.

pub fn File::shard(Self, StringView, BytesView, at? : Int64) -> BytesView raise Refused

A view of a tensor's bytes read from somewhere else — the second half of reading a file too large for one Bytes, where the caller holds the data section in pieces. at is how far into the tensor the piece starts, so a tensor larger than one Bytes can be handed over in pieces of whatever size the caller can hold. The piece is checked against the tensor's extent and the part that belongs to the tensor is returned, padding and all dropped.

pub fn File::tensor(Self, StringView) -> Tensor?

One tensor's index entry.

pub fn File::tensor_count(Self) -> Int32

How many tensors the index holds.

pub fn File::tensors(Self) -> ArrayView[Tensor]

The tensor index, in file order.

pub fn File::to_json(Self) -> @json.Json
pub fn File::version(Self) -> Version

The version the file is written in.

enumGgmlType

The encodings a tensor's blocks can be stored in, numbered as ggml numbers them. The number is what a file stores, so an encoding keeps its number; a file that names a number outside this table is refused rather than guessed at. Reading a tensor's bytes is all this library does. Turning a block of q6_k into floats is arithmetic, and lives elsewhere.

pub(all) enum GgmlType {
  • F32
  • F16
  • Q4_0
  • Q4_1
  • Q5_0
  • Q5_1
  • Q8_0
  • Q8_1
  • Q2_K
  • Q3_K
  • Q4_K
  • Q5_K
  • Q6_K
  • Q8_K
  • IQ2_XXS
  • IQ2_XS
  • IQ3_XXS
  • IQ1_S
  • IQ4_NL
  • IQ3_S
  • IQ2_S
  • IQ4_XS
  • I8
  • I16
  • I32
  • I64
  • F64
  • IQ1_M
  • BF16
  • TQ1_0
  • TQ2_0
  • MXFP4
  • NVFP4
  • Q1_0
  • Q2_0
  • F8_E4M3

The type derives Eq, Debug.

pub fn GgmlType::all() -> Array[Self]

Every encoding the table holds, in ggml's order.

pub fn GgmlType::block_size(Self) -> Int32

How many values one block holds. Plain float and integer encodings hold one value per block; the quantised ones hold a whole row chunk.

pub fn GgmlType::bytes_per_block(Self) -> Int32

How many bytes one block takes.

pub fn GgmlType::code(Self) -> UInt32

The number the file stores for this encoding.

pub fn GgmlType::is_quantized(Self) -> Bool

Whether the encoding packs several values into a block, which is what makes a block stand for scaled integers rather than the values themselves.

pub fn GgmlType::name(Self) -> String

The name this encoding goes by in a dump, such as Q4_K — the spelling the format's own tooling prints, and the one on the variant.

pub fn GgmlType::of(UInt32) -> Self?

The encoding a file's number names, or None for a number this version of the format does not define.

structHeader

The fixed part of a file: the magic, the version, and how many of the two tables follow.

pub(all) struct Header {
  • version : Version
  • tensors : Int32
  • entries : Int32

The type derives Eq, Debug.

structLimits

Ceilings on what a file may ask of a reader, so that a corrupt or hostile length cannot ask for gigabytes before the bytes behind it turn up. Each has a name and a published preset, so a caller reading files it did not make can tighten one without guessing at the rest. Going past one is Refused, carrying the ceiling and the number that went past it.

pub(all) struct Limits {
  • entries : Int32
  • tensors : Int32
  • text : Int32
  • items : Int32
  • depth : Int32

The type derives Eq, Debug.

structMeta

The metadata table: pairs in the order the file holds them, with a lookup by key beside it. Order is kept because writing the table back in another order would change the file, and because the order often says something — a vocabulary and its merges are easier to read in the order they were written.

pub struct Meta {
  • // private fields
pub fn Meta::contains(Self, StringView) -> Bool

Whether the table has that key.

pub fn Meta::count(Self) -> Int32

How many pairs the table holds.

pub fn Meta::entries(Self) -> ArrayView[Entry]

The pairs, in file order.

pub fn Meta::entry(Self, StringView) -> Entry?

The whole pair under a key, with the position it was read from.

pub fn Meta::flag(Self, StringView) -> Bool?

The flag under a key when it is a bool.

pub fn Meta::float(Self, StringView) -> Double?

The number under a key when it is stored as float32 or float64.

pub fn Meta::get(Self, StringView) -> Value?

The value under a key.

pub fn Meta::int(Self, StringView) -> Int64?

The number under a key when it is stored as a signed kind and fits 64 bits.

pub fn Meta::items(Self, StringView) -> Array[Value]?

The entries under a key when it is an array.

pub fn Meta::text(Self, StringView) -> String?

The text under a key, or None if it is absent or holds something else.

pub fn Meta::uint(Self, StringView) -> UInt64?

The number under a key when it is stored as an unsigned kind; None otherwise, a negative signed number included.

structTensor

One entry of a file's tensor index: what it is called, what it is stored as, its dimensions, where its bytes start, and — when the file was read — where the entry itself sits. The dimensions are ggml's order, the one the file stores: the first is the row the innermost loop walks, so a matrix of 4096 rows of 11008 values is [11008, 4096] and takes 11008 * 4096 * 2 bytes as f16.

pub struct Tensor {
  • // private fields

The type derives Eq, Debug.

pub fn Tensor::at(Self) -> Int64

Where the entry sits in the file it was read from, or -1 for one made here.

pub fn Tensor::byte_length(Self) -> Int64 raise Refused

How many bytes the tensor takes, blocks and all. Refused when its row is not a whole number of blocks, which no reader could walk.

pub fn Tensor::data(Self) -> BytesView?

The bytes attached to it, when it carries its own rather than borrowing the file's.

pub fn Tensor::dims(Self) -> ArrayView[UInt64]

The dimensions in ggml's order, the row first.

pub fn Tensor::extent(Self) -> Extent raise Refused

Where the bytes sit and how far they run.

pub fn Tensor::kind(Self) -> GgmlType

The encoding its blocks are stored in.

pub fn Tensor::n_elements(Self) -> UInt64 raise Refused

How many values the tensor holds.

pub fn Tensor::name(Self) -> String

The name the index gives it.

pub fn Tensor::new(name~ : String, kind~ : GgmlType, dims~ : Array[UInt64]) -> Self

A tensor to put into a file, before its bytes are attached.

enumValue

One metadata value, as the file holds it. The integer kinds keep their width: a count stored as uint32 reads back as UInt32 and is written as uint32 again, so a file survives a round trip unchanged even where two widths could hold the same number. Array carries the kind its entries share, which an empty list would otherwise lose, and its entries may themselves be Array.

pub(all) enum Value {
  • UInt8(UInt32)
  • Int8(Int32)
  • UInt16(UInt32)
  • Int16(Int32)
  • UInt32(UInt32)
  • Int32(Int32)
  • Float32(Float)
  • Bool(Bool)
  • String(String)
  • Array(ValueType, Array[Value])
  • UInt64(UInt64)
  • Int64(Int64)
  • Float64(Double)

The type derives Eq, Debug.

pub fn Value::as_bool(Self) -> Bool?

The truth of a bool value.

pub fn Value::as_float(Self) -> Double?

The number as a double, widening whichever width it was stored in. A float64 comes back bit for bit.

pub fn Value::as_int(Self) -> Int64?

The number as a signed 64-bit integer; None for the kinds that are not numbers without a fraction, and for an unsigned one too large to hold.

pub fn Value::as_items(Self) -> Array[Self]?

The entries of an array value, whatever kind they share.

pub fn Value::as_text(Self) -> String?

The text of a string value.

pub fn Value::as_uint(Self) -> UInt64?

The number as an unsigned 64-bit integer, for the kinds that are numbers without a fraction; None for the rest.

pub fn Value::items(ValueType, Array[Self]) -> Self

An array value whose entries are kind; every entry must be of that kind, which the writer checks.

pub fn Value::text(String) -> Self

A string value.

pub fn Value::to_json(Self) -> @json.Json
pub fn Value::value_type(Self) -> ValueType

Which of the thirteen kinds this value is.

enumValueType

The thirteen kinds of value a metadata entry can hold, numbered as the file numbers them. The numbers are the format's, and one of them, Array, says the value is a list whose own entries share a kind.

pub(all) enum ValueType {
  • UInt8
  • Int8
  • UInt16
  • Int16
  • UInt32
  • Int32
  • Float32
  • Bool
  • String
  • Array
  • UInt64
  • Int64
  • Float64

The type derives Eq, Debug.

pub fn ValueType::code(Self) -> UInt32

The number the file stores for this kind of value.

pub fn ValueType::name(Self) -> String

The name the format's own tooling prints for this kind, in the capital letters a metadata dump carries. It is the name a dump compared against another tool's has to agree on.

pub fn ValueType::of(UInt32) -> Self?

The kind a file's number names, or None past the thirteen.

enumVersion

The version of the format a file was written in. Versions 1 and 2 are read; 3 is read and written.

pub(all) enum Version {
  • V1
  • V2
  • V3

The type derives Eq, Debug.

pub fn Version::code(Self) -> UInt32

The number the file stores for this version.

pub fn Version::of(UInt32) -> Self?

The version a number names, or None for a number the format never had.