Documentation

Hesper.GGUF.Quantization

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) #

TQ1_0 (base-3 encoding) #

References #

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

        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) #

              TQ1_0 block: 256 elements packed via base-3 encoding More compact but more complex unpacking

              Instances For

                High-Level Dequantization API #

                Dequantize TQ2_0 tensor to Float32 array

                Equations
                • One or more equations did not get rendered due to their size.
                Instances For
                  def Hesper.GGUF.Quantization.dequantizeTQ2_0.processBlocks (data : ByteArray) (numElements numBlocks fuel blockIdx : Nat) (acc : Array Float) :
                  Equations
                  Instances For
                    def Hesper.GGUF.Quantization.dequantize (data : ByteArray) (ggmlType : GGMLType) (numElements : Nat) :

                    Dequantize tensor based on GGMLType

                    Equations
                    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
                          def Hesper.GGUF.Quantization.quantizeTQ2_0.processBlock (values : Array Float) (numBlocks fuel blockIdx : Nat) (acc : ByteArray) :
                          Equations
                          Instances For