Documentation
¶
Overview ¶
Package gguf reads GGUF model containers.
GGUF is the single-file container used by llama.cpp and compatible runtimes. A file holds a header, a metadata key-value block, a tensor directory, and the tensor data itself. Read parses everything but the tensor data, which callers map or read themselves using File.DataOffset, Tensor.Offset, and Tensor.Bytes.
Read is strict: it accepts only version 3 files and rejects duplicate keys, non-UTF-8 strings, unaligned or out-of-order tensor offsets, and any declared size that does not fit the file. Parsing is bounded, so a truncated or hostile file yields an error rather than a large allocation. Errors from a malformed file wrap ErrInvalid; errors from a well-formed file that uses an encoding this package does not implement wrap ErrUnsupported.
g, err := gguf.Read(f, size)
if err != nil {
return err
}
for _, t := range g.Tensors {
n, err := t.Bytes() // tensor occupies [g.DataOffset+t.Offset, +n)
...
}
Example ¶
b := fixture() // a small in-memory GGUF file
g, err := Read(bytes.NewReader(b), int64(len(b)))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(g.Metadata["general.architecture"].Value)
for _, t := range g.Tensors {
n, err := t.Bytes()
if err != nil {
fmt.Println(err)
return
}
fmt.Println(t.Name, t.Dimensions, g.DataOffset+t.Offset, n)
}
Output: deepseek4 token_embd.weight [4 8] 192 128
Index ¶
Examples ¶
Constants ¶
const ( TypeUint8 = 0 TypeInt8 = 1 TypeUint16 = 2 TypeInt16 = 3 TypeUint32 = 4 TypeInt32 = 5 TypeFloat32 = 6 TypeBool = 7 TypeString = 8 TypeArray = 9 TypeUint64 = 10 TypeInt64 = 11 TypeFloat64 = 12 )
Metadata value types, as stored in Value.TypeID.
Variables ¶
var ( ErrInvalid = errors.New("invalid GGUF") ErrUnsupported = errors.New("unsupported GGUF encoding") // ErrVersion is wrapped by the error Read reports for a file whose // version is not 3, such as a version 2 file. It wraps ErrUnsupported. ErrVersion = fmt.Errorf("%w: version", ErrUnsupported) )
Errors reported by Read. Use errors.Is to distinguish a malformed file from one this package cannot decode.
Functions ¶
func CheckSHA256 ¶
CheckSHA256 reports whether the size bytes read through r hash to want, which is a lowercase hexadecimal SHA-256 digest.
Types ¶
type File ¶
type File struct {
Metadata map[string]Value
Tensors []Tensor
DataOffset uint64 // byte offset of the tensor data block
Size uint64 // size of the whole file
}
A File is the parsed header, metadata, and tensor directory of a GGUF file. The tensor data itself is not read; it begins at DataOffset.
type Tensor ¶
A Tensor describes one tensor in the tensor directory. Its data lives at [File.DataOffset]+Offset and runs for Tensor.Bytes bytes.
type Value ¶
A Value is one metadata value. Value holds the Go value matching TypeID: uint8, int8, uint16, int16, uint32, int32, float32, bool, string, uint64, int64, float64, or []any for an array.