Documentation

Hesper.GGUF.Loader

GGUF Tensor Loader #

Loads tensor data from parsed GGUF files and uploads to GPU buffers.

Architecture #

GGUF File (on disk)
  │
  ├─► Parser → GGUFFile structure (in memory)
  │            ├─ Header
  │            ├─ Metadata
  │            ├─ Tensor Info (names, shapes, offsets)
  │            └─ Data Blob (raw bytes)
  │
  ├─► Loader → Extract specific tensors by name
  │            ├─ Lookup tensor info
  │            ├─ Extract ByteArray slice
  │            └─ Return (data, shape, quantization type)
  │
  └─► Upload → GPU Buffer
               ├─ If quantized (TQ2_0): Upload packed + scales
               └─ If Float32: Upload directly

Tensor Naming Conventions #

GGUF files use specific naming patterns (from llama.cpp):

Embedding #

token_embd.weight → [vocab_size, dim]

Transformer Blocks (per layer N) #

blk.N.attn_norm.weight → [dim]           (RMSNorm scales)
blk.N.attn_q.weight → [dim, dim]         (Q projection)
blk.N.attn_k.weight → [dim, kv_dim]      (K projection)
blk.N.attn_v.weight → [dim, kv_dim]      (V projection)
blk.N.attn_output.weight → [dim, dim]    (Output projection)

blk.N.ffn_norm.weight → [dim]            (RMSNorm scales)
blk.N.ffn_gate.weight → [dim, ffn_dim]   (Gate projection)
blk.N.ffn_up.weight → [dim, ffn_dim]     (Up projection)
blk.N.ffn_down.weight → [ffn_dim, dim]   (Down projection)

Output #

output_norm.weight → [dim]               (Final RMSNorm)
output.weight → [dim, vocab_size]        (LM head)

Quantization Handling #

Different tensors use different quantization:

We handle this by:

  1. Check tensor's ggml_type field
  2. Extract appropriate data format
  3. Upload to GPU with correct interpretation

Performance #

Zero-copy extraction: Tensor data is a ByteArray.extract slice - no memory copying.

References #

Tensor Data Types #

GGML quantization type IDs (from ggml-quants.h)

Instances For
    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.

      Tensor Info #

      Tensor metadata extracted from GGUF

      Instances For
        Equations
        • One or more equations did not get rendered due to their size.
        Instances For

          Tensor Extraction #

          Find tensor by name in GGUF file

          @param gguf Parsed GGUF file @param name Tensor name (e.g., "blk.0.attn_q.weight") @return Tensor info if found

          Equations
          Instances For
            Equations
            • One or more equations did not get rendered due to their size.
            Instances For

              Extract tensor data by name

              Returns raw bytes for the tensor. Caller must interpret based on ggmlType.

              @param gguf Parsed GGUF file @param name Tensor name @return (TensorInfo, ByteArray) - metadata and data

              Equations
              • One or more equations did not get rendered due to their size.
              Instances For

                mmap-aware tensor data getter. When the file was loaded via loadGGUFMmap and dataBlob is empty (zero-copy mode), copies the bytes from the mmap region instead. Otherwise falls back to the pure-byteArray path.

                Equations
                • One or more equations did not get rendered due to their size.
                Instances For

                  Extract Float32 tensor data

                  Unpacks F32 data into Float array.

                  @param gguf Parsed GGUF file @param name Tensor name @return Array of Float32 values

                  Equations
                  Instances For
                    Equations
                    • One or more equations did not get rendered due to their size.
                    Instances For

                      Extract F16 (Float16) tensor data as ByteArray

                      Returns raw F16 data as ByteArray (ready for GPU upload).

                      @param gguf Parsed GGUF file @param name Tensor name @return ByteArray of F16 data

                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For

                        Extract I2_S (BitNet ternary) tensor data

                        Returns packed 2-bit ternary data and scale factor. I2_S format: 2 bits per weight {-1, 0, +1} Encoding: 00 → -1, 01 → 0, 10 → +1

                        @param gguf Parsed GGUF file @param name Tensor name @return (packed_data: ByteArray, scale: Float, num_elements: Nat)

                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For

                          Extract TQ2_0 quantized tensor data

                          Returns packed ternary data and FP16 scales for TQ2_0 tensors.

                          @param gguf Parsed GGUF file @param name Tensor name @return (packed_data, scales_data, num_blocks)

                          Equations
                          • One or more equations did not get rendered due to their size.
                          Instances For

                            GPU Upload #

                            Extract Float32 tensor as ByteArray

                            @param gguf Parsed GGUF file @param name Tensor name @return ByteArray of Float32 data (ready for GPU upload)

                            Equations
                            • One or more equations did not get rendered due to their size.
                            Instances For

                              Extract F16 tensor as ByteArray

                              @param gguf Parsed GGUF file @param name Tensor name @return ByteArray of F16 data (ready for GPU upload)

                              Equations
                              • One or more equations did not get rendered due to their size.
                              Instances For

                                Extract I2_S tensor and dequantize to Float32

                                Dequantizes BitNet ternary weights to F32 for GPU computation.

                                @param gguf Parsed GGUF file @param name Tensor name @return ByteArray of F32 dequantized data

                                Equations
                                • One or more equations did not get rendered due to their size.
                                Instances For

                                  Extract TQ2_0 quantized tensor as ByteArrays

                                  Returns two ByteArrays: packed data and scales.

                                  @param gguf Parsed GGUF file @param name Tensor name @return (packed_data, scales_data)

                                  Equations
                                  • One or more equations did not get rendered due to their size.
                                  Instances For

                                    Utilities #

                                    List all tensor names in GGUF file

                                    Useful for debugging and validation.

                                    @param gguf Parsed GGUF file

                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    Instances For
                                      def Hesper.GGUF.Loader.validateTensor (gguf : GGUFFile) (name : String) (expectedShape : Array Nat) :

                                      Validate tensor exists and has expected shape

                                      @param gguf Parsed GGUF file @param name Tensor name @param expectedShape Expected dimensions @return true if tensor exists with correct shape

                                      Equations
                                      • One or more equations did not get rendered due to their size.
                                      Instances For

                                        Extract Q4_K tensor data as raw ByteArray

                                        Returns raw Q4_K block data ready for GPU upload. Q4_K blocks: 144 bytes per 256 elements.

                                        @param gguf Parsed GGUF file @param name Tensor name @return (raw_data: ByteArray, num_elements: Nat)

                                        Equations
                                        • One or more equations did not get rendered due to their size.
                                        Instances For

                                          Extract Q4_K tensor and upload raw block data to GPU

                                          @param gguf Parsed GGUF file @param name Tensor name @return (ByteArray of raw Q4_K block data, num_elements)

                                          Equations
                                          • One or more equations did not get rendered due to their size.
                                          Instances For