gguf

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

View Source
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

func CheckSHA256(r io.ReaderAt, size int64, want string) error

CheckSHA256 reports whether the size bytes read through r hash to want, which is a lowercase hexadecimal SHA-256 digest.

func TypeName

func TypeName(typeID uint32) string

TypeName returns the ggml name of a tensor type, such as "Q4_K", or "" if this package does not know the type.

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.

func Read

func Read(r io.ReaderAt, size int64) (*File, error)

Read parses the GGUF file of the given size read through r.

type Tensor

type Tensor struct {
	Name       string
	Dimensions []uint64
	TypeID     uint32
	Offset     uint64
}

A Tensor describes one tensor in the tensor directory. Its data lives at [File.DataOffset]+Offset and runs for Tensor.Bytes bytes.

func (Tensor) Bytes

func (t Tensor) Bytes() (uint64, error)

Bytes reports how many bytes t occupies in the tensor data block.

type Value

type Value struct {
	TypeID uint32
	Value  any
}

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.

func (Value) Int

func (v Value) Int() (int64, bool)

Int reports v as a signed integer. It returns false if v is not an integer type, or is an unsigned value too large for int64.

func (Value) Uint

func (v Value) Uint() (uint64, bool)

Uint reports v as an unsigned integer. It returns false if v is not one of the unsigned integer types.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL