pkg/gguf¶
GGUF format library for parsing, constructing, and serializing GGUF model files.
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¶
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 |
ValueTypeFloat16 | 4 | 16-bit float (raw bits) |
ValueTypeUint32 | 5 | Unsigned 32-bit integer |
ValueTypeInt32 | 6 | Signed 32-bit integer |
ValueTypeFloat32 | 7 | 32-bit float |
ValueTypeBool | 8 | Boolean |
ValueTypeString | 9 | Length-prefixed string |
ValueTypeArray | 10 | Typed array |
ValueTypeUint64 | 11 | Unsigned 64-bit integer |
ValueTypeInt64 | 12 | Signed 64-bit integer |
ValueTypeFloat64 | 13 | 64-bit float |
Methods¶
Returns the human-readable name of the value type.
Returns the byte size of a scalar value type. Returns -1 for ValueTypeArray and ValueTypeString.
GGMLType¶
Enum for tensor quantization types. See GGML Types for the full reference table.
| Constant | Value | Constant | Value |
|---|---|---|---|
GGMLTypeF32 | 0 | GGMLTypeQ2K | 10 |
GGMLTypeF16 | 1 | GGMLTypeQ3K | 11 |
GGMLTypeQ4_0 | 2 | GGMLTypeQ4K | 12 |
GGMLTypeQ4_1 | 3 | GGMLTypeQ5K | 13 |
GGMLTypeQ5_0 | 6 | GGMLTypeQ6K | 14 |
GGMLTypeQ5_1 | 7 | GGMLTypeIQ2XXS | 16 |
GGMLTypeQ8_0 | 8 | GGMLTypeIQ2XS | 17 |
GGMLTypeQ8_1 | 9 | GGMLTypeIQ3XXS | 18 |
GGMLTypeIQ1S | 19 | ||
GGMLTypeIQ4NL | 20 | ||
GGMLTypeIQ3S | 21 | ||
GGMLTypeIQ2S | 22 | ||
GGMLTypeIQ4XS | 23 | ||
GGMLTypeI8 | 24 | ||
GGMLTypeI16 | 25 | ||
GGMLTypeI32 | 26 | ||
GGMLTypeI64 | 27 | ||
GGMLTypeF64 | 28 | ||
GGMLTypeIQ1M | 29 |
GGMLTypeInfo¶
Describes the quantization block layout for a given GGMLType.
GGMLTypeInfoMap¶
Package-level map from GGMLType to its GGMLTypeInfo. Used for calculating tensor data sizes.
Header¶
The fixed-size header at the start of every GGUF file.
MetadataKV¶
A single key-value pair from the GGUF metadata section. Value holds the decoded value whose concrete type depends on ValueType.
ArrayValue¶
Represents a GGUF array metadata value. All elements share the same ElemType.
TensorInfo¶
Describes a single tensor's layout within the GGUF file.
Methods¶
Returns the product of all dimensions (total element count).
File¶
type File struct {
Header
Metadata []MetadataKV
Tensors []TensorInfo
TensorData []byte
Alignment uint64
}
In-memory representation of a complete GGUF file.
Methods¶
Returns the file's alignment value. Falls back to DefaultAlignment (32) if not set in metadata.
Updates Header.TensorCount and Header.MetadataKVCount to match the actual lengths of Tensors and Metadata slices.
Functions¶
NewFile¶
Returns a minimal valid GGUF file with correct magic, current version, default alignment, and empty metadata/tensor slices.
Marshal¶
Serializes a File to GGUF binary format with proper alignment padding.
MarshalRaw¶
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¶
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.
TensorDataSize¶
Calculates the byte size required for tensor data:
Returns an error if typ is not in GGMLTypeInfoMap.
NewReader¶
Returns a streaming GGUF reader that wraps an io.Reader.
NewWriter¶
Returns a streaming GGUF writer that wraps an io.Writer.