Skip to content

pkg/gguf

GGUF format library for parsing, constructing, and serializing GGUF model files.

import "github.com/professor-moody/crucible/pkg/gguf"

Constants

Constant Value Description
Magic [4]byte{0x47, 0x47, 0x55, 0x46} GGUF magic bytes ("GGUF")
CurrentVersion 3 Latest GGUF spec version
DefaultAlignment 32 Default tensor data alignment in bytes

Types

ValueType

type ValueType uint32

Enum for metadata value types in GGUF key-value pairs.

Constant Value Description
ValueTypeUint8 0 Unsigned 8-bit integer
ValueTypeInt8 1 Signed 8-bit integer
ValueTypeUint16 2 Unsigned 16-bit integer
ValueTypeInt16 3 Signed 16-bit integer
ValueTypeUint32 4 Unsigned 32-bit integer
ValueTypeInt32 5 Signed 32-bit integer
ValueTypeFloat32 6 32-bit float
ValueTypeBool 7 Boolean
ValueTypeString 8 Length-prefixed string
ValueTypeArray 9 Typed array
ValueTypeUint64 10 Unsigned 64-bit integer
ValueTypeInt64 11 Signed 64-bit integer
ValueTypeFloat64 12 64-bit float

Methods

func (vt ValueType) String() string

Returns the human-readable name of the value type.

func ValueTypeSize(vt ValueType) int

Returns the byte size of a scalar value type. Returns 0 for ValueTypeArray, ValueTypeString, and unknown values.


GGMLType

type GGMLType uint32

Enum for tensor quantization types. See GGML Types for the full reference table.

Constant Value Constant Value
GGMLTypeF32 0 GGMLTypeIQ3XXS 18
GGMLTypeF16 1 GGMLTypeIQ1S 19
GGMLTypeQ4_0 2 GGMLTypeIQ4NL 20
GGMLTypeQ4_1 3 GGMLTypeIQ3S 21
GGMLTypeQ5_0 6 GGMLTypeIQ2S 22
GGMLTypeQ5_1 7 GGMLTypeIQ4XS 23
GGMLTypeQ8_0 8 GGMLTypeI8 24
GGMLTypeQ8_1 9 GGMLTypeI16 25
GGMLTypeQ2K 10 GGMLTypeI32 26
GGMLTypeQ3K 11 GGMLTypeI64 27
GGMLTypeQ4K 12 GGMLTypeF64 28
GGMLTypeQ5K 13 GGMLTypeIQ1M 29
GGMLTypeQ6K 14 GGMLTypeBF16 30
GGMLTypeQ8_K 15 GGMLTypeTQ1_0 34
GGMLTypeIQ2XXS 16 GGMLTypeTQ2_0 35
GGMLTypeIQ2XS 17 GGMLTypeMXFP4 39
GGMLTypeNVFP4 40 GGMLTypeQ1_0 41
GGMLTypeQ2_0 42

GGMLTypeInfo

type GGMLTypeInfo struct {
    BlockSize int
    TypeSize  int
}

Describes the quantization block layout for a given GGMLType.

GGMLTypeInfoMap

var GGMLTypeInfoMap map[GGMLType]GGMLTypeInfo

Package-level map from GGMLType to its GGMLTypeInfo. Used for calculating tensor data sizes.


type Header struct {
    Magic          [4]byte
    Version        uint32
    TensorCount    uint64
    MetadataKVCount uint64
}

The fixed-size header at the start of every GGUF file.


MetadataKV

type MetadataKV struct {
    Key       string
    ValueType ValueType
    Value     interface{}
    KeyLenOverride *uint64
    ValLenOverride *uint64
}

A single key-value pair from the GGUF metadata section. Value holds the decoded value whose concrete type depends on ValueType.


ArrayValue

type ArrayValue struct {
    ElemType      ValueType
    Data          []interface{}
    CountOverride *uint64
}

Represents a GGUF array metadata value. All elements share the same ElemType.


TensorInfo

type TensorInfo struct {
    Name   string
    NDims  uint32
    Dims   []uint64
    Type   GGMLType
    Offset uint64
}

Describes a single tensor's layout within the GGUF file.

Methods

func (t *TensorInfo) NElements() uint64

Returns the product of all dimensions (total element count).


File

type File struct {
    Header            Header
    Metadata          []MetadataKV
    Tensors           []TensorInfo
    TensorData        []byte
    Alignment         uint64
    TruncationWarning string
}

In-memory representation of a complete GGUF file.

Methods

func (f *File) GetAlignment() uint64

Returns the file's alignment value. Falls back to DefaultAlignment (32) if not set in metadata.

func (f *File) SyncCounts()

Updates Header.TensorCount and Header.MetadataKVCount to match the actual lengths of Tensors and Metadata slices.

func (f *File) Clone() *File

Returns a deep copy suitable for isolated mutation.


Functions

NewFile

func NewFile() *File

Returns a minimal valid GGUF file with correct magic, current version, default alignment, and empty metadata/tensor slices.

Marshal

func Marshal(f *File) ([]byte, error)

Serializes a File to GGUF binary format with proper alignment padding.

MarshalRaw

func MarshalRaw(f *File, skipPadding bool) ([]byte, error)

Serializes a File with explicit control over alignment padding. When skipPadding is true, no padding bytes are inserted between the metadata section and tensor data.

Unmarshal

func Unmarshal(data []byte) (*File, error)

Parses a byte slice into a File. Returns an error if the magic bytes are invalid, the version is unsupported, or the data is truncated.

Tensor-data truncation that can be bounded safely may instead be surfaced through File.TruncationWarning; callers that need complete payloads must check it.

TensorDataSize

func TensorDataSize(nElements uint64, typ GGMLType) (uint64, error)

Calculates the byte size required for tensor data:

size = ceil(nElements / blockSize) * typeSize

Returns an error if typ is not in GGMLTypeInfoMap.

NewReader

func NewReader(r io.Reader) *Reader

Returns a streaming GGUF reader that wraps an io.Reader.

NewWriter

func NewWriter(w io.Writer) *Writer

Returns a streaming GGUF writer that wraps an io.Writer.