ᛏ tolk
GPU tensor compiler for OCaml
Tolk is a port of tinygrad in OCaml — a minimal compiler for GPU tensor computation. It takes tensor-level computation graphs, optimizes them, and emits efficient kernels for CPU (via Clang), Metal, CUDA, and OpenCL backends.
Features
- Three-level IR — tensor graphs, kernel DAGs, and linear programs with shared conventions (sub-axes, tagging, map_children)
- Symbolic simplification — three-phase algebraic pipeline for index expressions with div/mod folding
- Hardware decompositions — transcendentals, int64 emulation, float type promotion, and late op rewrites
- Codegen pipeline — range simplification, GPU dimension mapping, beam search optimization, and linearization
- Schedule pipeline — tensor-to-kernel graph transformation with range analysis and multi-device sharding
- JIT integration — used by Rune's
jittransformation to compile and dispatch kernels at runtime
Architecture
Tolk follows a layered compilation pipeline:
- Tensor IR — high-level operation graph (reductions, reshapes, movement ops)
- Schedule — transforms tensor graphs into kernel graphs via rangeify and indexing
- Codegen — optimizes kernel structure (range simplification, GPU dims, beam search)
- Lowering — lowers to linear program IR (devectorization, expansion, decompositions)
- Renderer — emits backend-specific source code (C, Metal, CUDA, OpenCL)
- Runtime — compiles and dispatches kernels on target devices
Libraries
tolk— codegen pipeline, renderer, device abstraction, and runtimetolk.ir— IR definitions (tensor, kernel, program), symbolic simplification, decompositionstolk.cpu— CPU backend (Clang compilation, ELF loading)tolk.metal— Metal backend (macOS GPU)