SPB Git

spb/forge

Public MIT

Forge — LLM training from scratch in pure C++20 + Metal on Apple Silicon.

History of src/core Clear filter
page 1
  1. Wed, Aug 5, 2026 1

    1. Add .forge — Apple-native, git-style weight format with zero-copy loading
      …
      A .forge is a model repository: tiny JSON manifests (one commit per save,
      with parent links) over content-addressed shards. Tensors are 16KB-page-
      aligned inside shards padded to page multiples, so loading is mmap +
      newBuffer(bytesNoCopy) — on unified memory the file-cache pages ARE the
      GPU memory. Saves are deltas: only tensors whose FNV-1a hash changed since
      the parent manifest are written. Shards cap at 95MB (GitHub-pushable).
      Store f32 (zero-copy alias at load) or f16/bf16 (half size).
      
      - src/core/fmodel.{h,cpp}: save() + Snapshot zero-copy reader
      - Tensor::from_buffer: views over externally-owned MTLBuffers
      - forge export CLI; generate/eval accept .forge repos directly
      - trainer commits weights natively to <out>/model.forge at each checkpoint
        (forge_save/forge_dtype config keys); .bin keeps optimizer state for resume
      - tools/fmodel.py: inspect, log (history), to-safetensors (pure numpy)
      
      Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
      simon-pierre boucher · 4 files +468 −2
  2. Fri, Jul 31, 2026 1

    1. Forge: LLM training from scratch in C++20 + Metal on Apple Silicon
      …
      A complete transformer training stack with no ML dependencies: tensors,
      autograd, hand-written Metal kernels, flash attention (forward and backward),
      AdamW, BPE tokenizer, checkpointing and generation. Architecture is fully
      config-driven — the same binary trains 12M to 205M parameter models.
      
      Every Metal kernel is validated against a CPU reference (85 parity checks,
      <=1e-4, most bit-exact), gradients against central finite differences, and
      each optimization was accepted only after the training loss trajectory stayed
      numerically unchanged.
      
      Measured findings (M5 Max, documented in RESEARCH.md and paper/forge.tex):
      
      - `constant constexpr` for MSL tile constants declares an address-space
        variable, not a compile-time constant. Loops stop unrolling and every
        matrix accumulator spills: 0.82 -> 10.21 TFLOPS once switched to enums.
      - That defect is invisible in the AIR at every -O level, because unrolling
        happens in the driver back end. Benchmark; do not read the IR.
      - Register pressure, not bandwidth, dominates attention backward. Guided by
        measured spill counts, three restructurings took it 107 -> 7.05 ms (15.2x).
      - On M5, mpp::tensor_ops::matmul2d reaches 51.5 TFLOPS with f16 operands vs
        10.6 for a tuned simdgroup_matrix kernel (4.9x), verified numerically.
        f16 on the simdgroup path alone is worth only +18-22%.
      - Concurrent dispatch for the optimizer sweep: +22% on the 100M config.
      
      Trained the 12.2M config for one epoch over 19.14M TinyStories tokens:
      loss 8.40 -> 2.99, validation 3.009, perplexity 20.27, ~38.2k tokens/sec.
      Simon-Pierre Boucher · 10 files +784
Menu

Theme