MetaValue
#struct MetaValueOne metadata value.
| 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<i64> | The integer array. |
floats | Vector<f64> | The float array. |
strs | Vector<str> | The string array, such as the tokenizer vocabulary. |
import ai.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 throw:
mut buffer := compress.NewByteBuf()
build(buffer)
path := os.TempDir() + "/salam_tiny_en.gguf"
_ := archive.WriteBinaryFileBuf(path, buffer)
mut in_memory := try gguf.ParseGguf(buffer)
println in_memory.tensors.Len()
in_memory.Free()
mut g := try gguf.LoadGguf(path)
println g.version, g.keys.Len(), g.tensors.Len(), g.alignment, g.data_offset
println g.MetaStr("general.architecture"),
g.MetaInt("llama.context_length", 0),
g.MetaInt("llama.block_count", 32)
tokens := g.values.Get(g.MetaIndex("tokenizer.tokens"))
println tokens.kind == gguf.META_ARRAY, tokens.elem_kind == gguf.META_STRING, tokens.strs.Get(1)
idx := g.TensorIndex("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 := try gguf.LoadTensorF32(g, idx)
println t.At(0), t.At(5)
try:
gguf.LoadTensorF32(g, 7)
catch e:
println e.code
end
t.Free()
println gguf.GGML_F16, gguf.GGML_Q4_0, gguf.GGML_Q4_1, gguf.GGML_Q8_0
g.Free()
end1 3 3 1 32 224 llama 2048 32 true true hello w 2 6 0 0 24 0 2.5 gguf.index 1 2 3 8