GGUF Quantization and Dequantization #
Implements unpacking of quantized tensor formats, with focus on ternary quantization for BitNet.
Ternary Quantization Formats #
TQ2_0 (2-bit packing) #
- Block size: 256 elements
- Packing: 4 ternary values per byte (2 bits each)
- Scale: 1 FP16 scale per block
- Encoding: {-1, 0, 1} → {0b11, 0b00, 0b01}
- 0b00 → 0
- 0b01 → +1
- 0b11 → -1 (two's complement)
- Layout: [packed_bytes (64)] [scale (FP16=2 bytes)]
TQ1_0 (base-3 encoding) #
- Block size: 256 elements
- Packing: 5 ternary values per byte (3^5 = 243 < 256)
- More compact but more complex to unpack
- Layout: [packed_bytes (51)] [extra (4)] [scale (FP16=2 bytes)]
References #
- llama.cpp/ggml/src/ggml-quants.c (lines 2103-2271) - TQ2_0 implementation
- llama.cpp/convert_hf_to_gguf.py (lines 3152-3190) - Quantization logic
Ternary Value Helpers #
Unpack a single 2-bit ternary value from packed byte @param packed The packed byte containing 4 values @param idx The index (0-3) of the value to extract @return Ternary value: -1, 0, or 1
Equations
- One or more equations did not get rendered due to their size.
Instances For
Pack 4 ternary values into a single byte (inverse operation) Used for testing and verification
Equations
- One or more equations did not get rendered due to their size.
Instances For
TQ2_0 Block Structure #
TQ2_0 block: 256 elements packed into 64 bytes + 1 FP16 scale Total: 66 bytes per block
Instances For
Block size in elements
Equations
Instances For
Block size in bytes (64 packed + 2 scale)
Equations
Instances For
Simplified FP16 to Float32 conversion Handles normalized values only (sufficient for most scales)
Equations
- One or more equations did not get rendered due to their size.
Instances For
Parse TQ2_0 block from ByteArray
Equations
- One or more equations did not get rendered due to their size.
Instances For
Unpack TQ2_0 block to Float32 array
Equations
- One or more equations did not get rendered due to their size.
Instances For
TQ1_0 Block Structure (TODO: Lower priority) #
High-Level Dequantization API #
Equations
- One or more equations did not get rendered due to their size.
- Hesper.GGUF.Quantization.dequantizeTQ2_0.processBlocks data numElements numBlocks 0 blockIdx acc = Except.error "dequantizeTQ2_0: fuel exhausted"
Instances For
Dequantize tensor based on GGMLType
Equations
- One or more equations did not get rendered due to their size.
- Hesper.GGUF.Quantization.dequantize data Hesper.GGUF.GGMLType.TQ2_0 numElements = Hesper.GGUF.Quantization.dequantizeTQ2_0 data numElements
- Hesper.GGUF.Quantization.dequantize data Hesper.GGUF.GGMLType.TQ1_0 numElements = Except.error "TQ1_0 dequantization not yet implemented"
- Hesper.GGUF.Quantization.dequantize data Hesper.GGUF.GGMLType.F16 numElements = Except.error "F16 dequantization not yet implemented"
- Hesper.GGUF.Quantization.dequantize data ggmlType numElements = Except.error (toString "Dequantization not implemented for " ++ toString ggmlType)
Instances For
Quantization (for testing) #
Helper: Simplified Float32 to FP16 conversion
Equations
- One or more equations did not get rendered due to their size.
Instances For
Quantize Float32 array to TQ2_0 format Used for testing and validation (simplified version)
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
- Hesper.GGUF.Quantization.quantizeTQ2_0.processBlock values numBlocks 0 blockIdx acc = acc