Standard library · Machine learning

gguf

Reading GGUF model files, the llama.cpp format.

import gguf

GGUF is the llama.cpp file format for large language models. One file holds the metadata, such as the architecture and the tokenizer vocabulary, and the weights, usually quantised. LoadGguf reads only the header, and LoadTensorF32 turns each weight into a tensor whenever you need it.

The example below builds a small file so that it runs without downloading a model.

Types

MetaValue

struct MetaValue
One metadata value.
NameTypeDescription
kindint = 0The type.
elem_kindint = 0The type of the elements, for an array.
ii64 = 0The integer value.
ff64 = 0The float value.
sstr = ""The string.
intsVectorThe integer array.
floatsVectorThe float array.
strsVectorThe string array, such as the tokenizer vocabulary.
gguf-Read.salam
import gguf
import compress
import encoding.binary
import archive
import os

func string(b &: compress.ByteBuf, text: str):
    binary.WriteU64LE(b, text.len() as i64)
    compress.BufPushBytes(b, text as void*, text.len())
end

func build(b &: compress.ByteBuf):
    compress.BufPushBytes(b, "GGUF" as void*, 4)
    binary.WriteU32LE(b, 3)
    binary.WriteU64LE(b, 1)
    binary.WriteU64LE(b, 3)
    string(b, "general.architecture")
    binary.WriteU32LE(b, gguf.META_STRING)
    string(b, "llama")
    string(b, "llama.context_length")
    binary.WriteU32LE(b, 4)
    binary.WriteU32LE(b, 2048)
    string(b, "tokenizer.tokens")
    binary.WriteU32LE(b, gguf.META_ARRAY)
    binary.WriteU32LE(b, gguf.META_STRING)
    binary.WriteU64LE(b, 2)
    string(b, "<s>")
    string(b, "hello")
    string(b, "w")
    binary.WriteU32LE(b, 2)
    binary.WriteU64LE(b, 3)
    binary.WriteU64LE(b, 2)
    binary.WriteU32LE(b, gguf.GGML_F32)
    binary.WriteU64LE(b, 0)
    until compress.BufLen(b) % 32 != 0:
        binary.WriteU8(b, 0)
    end
    repeat 6 in i:
        binary.WriteF32(b, (i as f64) * 0.5, false)
    end
end

func main:
    mut buffer := compress.NewByteBuf()
    build(buffer)
    path := os.TempDir() + "/salam_tiny_en.gguf"
    _ := archive.WriteBinaryFileBuf(path, buffer)
    mut good := false
    mut in_memory := gguf.ParseGguf(buffer, good)
    println good, in_memory.error == ""
    in_memory.free()

    mut g := gguf.LoadGguf(path, good)
    println good, g.ok, g.version, g.keys.len(), g.tensors.len(), g.alignment, g.data_offset
    println g.meta_str("general.architecture"), g.meta_int("llama.context_length", 0), g.meta_int("llama.block_count", 32)
    tokens := g.values.get(g.meta_index("tokenizer.tokens"))
    println tokens.kind == gguf.META_ARRAY, tokens.elem_kind == gguf.META_STRING, tokens.strs.get(1)
    idx := g.tensor_index("w")
    info := g.tensors.get(idx)
    println info.name, info.dims.len(), info.count(), info.ggml_type, info.offset, gguf.TensorBytes(g, idx)
    mut t := gguf.LoadTensorF32(g, idx, good)
    println good, t.at(0), t.at(5)
    t.free()
    println gguf.GGML_F16, gguf.GGML_Q4_0, gguf.GGML_Q4_1, gguf.GGML_Q8_0
    g.free()
end
Outputtrue true true true 3 3 1 32 224 llama 2048 32 true true hello w 2 6 0 0 24 true 0 2.5 1 2 3 8

free

func free()
Frees the value.

The example for this item is shown with the ones above: gguf-Read.salam

TensorInfo

struct TensorInfo
The information about one tensor in the file.
NameTypeDescription
namestr = ""The name, such as blk.0.attn_q.weight.
dimsVectorThe dimensions.
ggml_typeint = 0The weight type, one of the constants above.
offseti64 = 0Where the data sits, from the start of the data section.

The example for this item is shown with the ones above: gguf-Read.salam

count

func count(): int
The number of elements.

The example for this item is shown with the ones above: gguf-Read.salam

free

func free()
Frees it.

The example for this item is shown with the ones above: gguf-Read.salam

Gguf

struct Gguf
A parsed GGUF file.
NameTypeDescription
versionint = 0The format version.
keysVectorThe metadata keys.
valuesVectorThe values, in the same order as the keys.
tensorsVectorThe tensors.
alignmentint = 32The data alignment.
data_offsetint = 0Where the data section starts in the file.
rawcompress.ByteBufThe bytes of the file.
errorstr = ""The error message.
okbool = falseIt was read correctly.

The example for this item is shown with the ones above: gguf-Read.salam

meta_index

func meta_index(key: str): int
The index of a metadata key; -1 when there is none.

The example for this item is shown with the ones above: gguf-Read.salam

meta_int

func meta_int(key: str, dflt: i64): i64
An integer metadata value, or the default value.

The example for this item is shown with the ones above: gguf-Read.salam

meta_str

func meta_str(key: str): str
A string metadata value.

The example for this item is shown with the ones above: gguf-Read.salam

tensor_index

func tensor_index(name: str): int
The index of the tensor with this name.

The example for this item is shown with the ones above: gguf-Read.salam

free

func free()
Frees all of it.

The example for this item is shown with the ones above: gguf-Read.salam

Constants and variables

GGML_F32

const GGML_F32: i32 = 0
Weight type: 32 bit float.

The example for this item is shown with the ones above: gguf-Read.salam

GGML_F16

const GGML_F16: i32 = 1
Weight type: 16 bit float.

The example for this item is shown with the ones above: gguf-Read.salam

GGML_Q4_0

const GGML_Q4_0: i32 = 2
Weight type: 4 bit quantised with a scale.

The example for this item is shown with the ones above: gguf-Read.salam

GGML_Q4_1

const GGML_Q4_1: i32 = 3
Weight type: 4 bit quantised with a scale and a minimum.

The example for this item is shown with the ones above: gguf-Read.salam

GGML_Q8_0

const GGML_Q8_0: i32 = 8
Weight type: 8 bit quantised.

The example for this item is shown with the ones above: gguf-Read.salam

META_STRING

const META_STRING: i32 = 8
Metadata type: a string.

The example for this item is shown with the ones above: gguf-Read.salam

META_ARRAY

const META_ARRAY: i32 = 9
Metadata type: an array.

The example for this item is shown with the ones above: gguf-Read.salam

Functions

ParseGguf

func ParseGguf(buf: compress.ByteBuf, ok &: bool): Gguf
Reads the file from a buffer; the buffer belongs to the result from then on.

The example for this item is shown with the ones above: gguf-Read.salam

LoadGguf

func LoadGguf(path: str, ok &: bool): Gguf
Reads a .gguf file.

The example for this item is shown with the ones above: gguf-Read.salam

TensorBytes

func TensorBytes(g &: Gguf, index: int): int
The size of the tensor data in bytes.

The example for this item is shown with the ones above: gguf-Read.salam

LoadTensorF32

func LoadTensorF32(g &: Gguf, index: int, ok &: bool): tensor.Tensor
Converts the tensor to float and loads it; the F16 and quantised types are unpacked.

The example for this item is shown with the ones above: gguf-Read.salam