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| Name | Type | Description |
|---|---|---|
kind | int = 0 | The type. |
elem_kind | int = 0 | The type of the elements, for an array. |
i | i64 = 0 | The integer value. |
f | f64 = 0 | The float value. |
s | str = "" | The string. |
ints | Vector | The integer array. |
floats | Vector | The float array. |
strs | Vector | The string array, such as the tokenizer vocabulary. |
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()
endfree
func free()The example for this item is shown with the ones above: gguf-Read.salam
TensorInfo
struct TensorInfo| Name | Type | Description |
|---|---|---|
name | str = "" | The name, such as blk.0.attn_q.weight. |
dims | Vector | The dimensions. |
ggml_type | int = 0 | The weight type, one of the constants above. |
offset | i64 = 0 | Where 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(): intThe example for this item is shown with the ones above: gguf-Read.salam
free
func free()The example for this item is shown with the ones above: gguf-Read.salam
Gguf
struct Gguf| Name | Type | Description |
|---|---|---|
version | int = 0 | The format version. |
keys | Vector | The metadata keys. |
values | Vector | The values, in the same order as the keys. |
tensors | Vector | The tensors. |
alignment | int = 32 | The data alignment. |
data_offset | int = 0 | Where the data section starts in the file. |
raw | compress.ByteBuf | The bytes of the file. |
error | str = "" | The error message. |
ok | bool = false | It was read correctly. |
The example for this item is shown with the ones above: gguf-Read.salam
meta_index
func meta_index(key: str): intThe example for this item is shown with the ones above: gguf-Read.salam
meta_int
func meta_int(key: str, dflt: i64): i64The example for this item is shown with the ones above: gguf-Read.salam
meta_str
func meta_str(key: str): strThe example for this item is shown with the ones above: gguf-Read.salam
tensor_index
func tensor_index(name: str): intThe example for this item is shown with the ones above: gguf-Read.salam
free
func free()The example for this item is shown with the ones above: gguf-Read.salam
Constants and variables
GGML_F32
const GGML_F32: i32 = 0The example for this item is shown with the ones above: gguf-Read.salam
GGML_F16
const GGML_F16: i32 = 1The example for this item is shown with the ones above: gguf-Read.salam
GGML_Q4_0
const GGML_Q4_0: i32 = 2The example for this item is shown with the ones above: gguf-Read.salam
GGML_Q4_1
const GGML_Q4_1: i32 = 3The example for this item is shown with the ones above: gguf-Read.salam
GGML_Q8_0
const GGML_Q8_0: i32 = 8The example for this item is shown with the ones above: gguf-Read.salam
META_STRING
const META_STRING: i32 = 8The example for this item is shown with the ones above: gguf-Read.salam
META_ARRAY
const META_ARRAY: i32 = 9The example for this item is shown with the ones above: gguf-Read.salam
Functions
ParseGguf
func ParseGguf(buf: compress.ByteBuf, ok &: bool): GgufThe example for this item is shown with the ones above: gguf-Read.salam
LoadGguf
func LoadGguf(path: str, ok &: bool): Gguf.gguf file.The example for this item is shown with the ones above: gguf-Read.salam
TensorBytes
func TensorBytes(g &: Gguf, index: int): intThe example for this item is shown with the ones above: gguf-Read.salam
LoadTensorF32
func LoadTensorF32(g &: Gguf, index: int, ok &: bool): tensor.TensorThe example for this item is shown with the ones above: gguf-Read.salam