Go on accelerators
Research: deep learning in Go on GPUs, TPUs and NPUs, in one binary.
Status: research only, no code. Goal: deep learning training and inference in Go that runs efficiently on GPUs, TPUs, NPUs and other accelerators. It ships as one binary that is easy to distribute.
1. Prior art and what it proves
- MLIR gpu dialect: kernels are gpu.func in a gpu.module. A kernel outlining pass moves a gpu.launch body into its own function. Targets are attached per module: NVVM (PTX through LLVM's NVPTX backend), ROCDL (AMD) and SPIR-V (Vulkan, and Metal through translation). One module can carry several targets. Proves: one compiler can emit kernels for all three vendor families from one IR. marmot already uses this path for M8 (marmot_gpu_for, -gpu, NVPTX).
- MLIR linalg, tensor and sparse_tensor: structured operations (matmul, convolution, generic loops over tensors) with tiling, fusion and vectorization passes. A sparse compiler generates code from sparsity annotations. Proves: a tensor library can be lowered to good kernels without hand-written ones, if the operations are structured.
- IREE: an end-to-end MLIR compiler and runtime for models (from StableHLO, TOSA, ONNX, PyTorch through torch-mlir). Backends: llvm-cpu, CUDA, ROCm (experimental), Vulkan (SPIR-V), Metal (support varies by version). Its runtime is small and embeddable, and compiled modules are a flat file. Proves: compiled models plus a small runtime can ship inside one application.
- XLA and StableHLO, PJRT: XLA compiles whole computation graphs with operation fusion. StableHLO is the portable operation set with a serialized format. The 0.x series has limited compatibility guarantees. 1.0 plans 5 year backward and 2 year forward compatibility. PJRT is the plugin interface to devices, and the only public way to TPUs. Proves: graph level fusion is the main win for training and large models, and TPUs are reached through XLA, not through kernels.
- Triton: a Python embedded kernel language with block level programming. The compiler handles memory coalescing, shared memory and tensor cores. Proves: a small kernel language with tile semantics reaches near vendor library speed on GPUs for attention and matmul style kernels.
- TVM: tensor expressions with schedules, auto-tuning, many targets including NPUs through BYOC. Proves: auto-tuning matters for portable performance.
- Mojo and MAX: a systems language with compile time metaprogramming over MLIR, kernels and a model serving engine. NVIDIA data center GPUs first, AMD Instinct later, Apple silicon GPUs in progress. Proves: a general purpose language can host fast kernels when the compiler owns the whole stack, and that vendor coverage takes years.
- JAX: tracing pure functions to XLA, transformations (grad, vmap, jit). Proves: automatic differentiation by program transformation over a traced graph is the workable model for training.
- ONNX Runtime: inference from a portable model format with execution providers per device (CUDA, TensorRT, DirectML, CoreML, NPUs). Proves: vendor runtimes are the practical route to NPUs.
- tinygrad: a small framework with lazy tensors and a few dozen low level operations. Each backend implements them (CUDA, AMD, Metal, OpenCL, Qualcomm, WebGPU, CPU through C or LLVM). Proves: a small op set is enough to reach many devices, and kernel fusion from lazy graphs gives most of the speed.
- GoMLX and Gorgonia: GoMLX builds graphs in Go and runs them on XLA through PJRT. A StableHLO backend is replacing the older XLA one. A pure Go backend gives portability. Installation (PJRT plugins, shared libraries) is its main friction. Gorgonia is a graph library with CUDA support that is much less active. Proves: Go users want this, and distribution is the weak point of the cgo and plugin approach.
- TinyGo on embedded: Go compiled through LLVM for microcontrollers. Proves: Go semantics can be restricted (no or simple GC, small runtime) for a constrained target, which is the model for a kernel subset of Go.
2. Paths for marmot
| Path | What | Targets it reaches | Fit |
|---|---|---|---|
| A. Kernels from Go loops (SPEC M8) | parallel loops proved safe by the compiler become gpu.launch kernels | CUDA (NVPTX) now, ROCm (ROCDL) and Vulkan or Metal (SPIR-V) with the same MLIR targets | good for data parallel numeric code, weak for matmul and attention without tiling knowledge |
| B. A tensor library lowered to linalg | a Go package (Tensor type, matmul, conv, elementwise, reductions) that marmot recognizes and lowers to linalg and tensor ops, fused and tiled by MLIR | CPU (vectorized), CUDA, ROCm, SPIR-V devices through the same pipelines, TPUs only through StableHLO export | the main path for training and inference written in Go, autodiff at the library level (tape) or by compiler transformation later |
| C. Import StableHLO or ONNX models for inference | a model file is compiled at build time (through IREE style pipelines or StableHLO to linalg) and linked into the binary | CPU, CUDA, ROCm, Vulkan, Metal, NPUs only through vendor runtimes (ONNX execution providers, Core ML) | fastest way to a real model running, no Go training |
TPUs: reachable only through XLA and PJRT. Path B can export StableHLO for them. marmot would then run the graph through PJRT and not compile kernels itself. NPUs: through vendor runtimes (ONNX Runtime providers, Core ML, vendor SDKs) loaded at run time.
3. Go API and semantics questions
- What a kernel may do: no allocation on the device heap, no goroutines, no channels and no maps. No interfaces with dynamic dispatch, and no panics except a trap that the host turns into an error. Slices of numeric types and structs of them. Calls to other kernel-safe functions (inlined). The compiler checks this and reports the first construct that is not allowed, with its position.
- Memory: device buffers as a Go type (a tensor or a device slice) with explicit or lazy transfer. Unified memory where the device has it. The Go GC owns the host handle and frees the device memory through a finalizer or an explicit Close. No Go pointers into device memory.
- Errors: launches and transfers return errors (device lost, out of memory, no device). A kernel fault becomes an error at the next synchronization point, not a Go panic in a random goroutine.
- Concurrency: one stream per goroutine by default, explicit streams for overlap. Synchronization when a buffer is read on the host.
- Determinism: floating point reductions on devices are not ordered as on the CPU. Tests must compare with a tolerance, and a deterministic mode (fixed reduction order) should exist for debugging.
- Autodiff: a tape in the tensor library first (as PyTorch), compiler level differentiation of kernel functions later (as JAX and Enzyme).
4. Distribution
- One static binary: kernels are compiled ahead of time for each target and embedded as data. The binary selects them at run time by the device it finds. The kernels are PTX and cubin for a list of NVIDIA architectures, code objects for AMD, SPIR-V for Vulkan and metallib for Apple.
- A small loader loads the device runtime at run time (libcuda, ROCm, Vulkan loader, Metal framework). It is not linked, so the binary starts on a machine without a GPU.
- CPU fallback: every kernel also exists as vectorized CPU code (the same linalg lowering to LLVM). It runs when no device is found or the device fails. A log line says which path runs.
- PTX for an unknown newer NVIDIA GPU is compiled by the driver at first run (JIT), so binaries keep working on new hardware.
5. Staged plan
- M8 completion (path A): Go loops to CUDA kernels with the CPU fallback, embedded PTX, run time loading of libcuda. Measure: a saxpy and a matmul bench against the CPU build and against a cuBLAS baseline.
- Tensor package and linalg lowering (path B) for the operations of a small transformer: matmul, add, softmax, layer norm, GELU, embedding. CPU and CUDA.
- First milestone: inference of a small real model, written in Go on the tensor package. An example is GPT-2 small, 124M parameters, with weights from a safetensors file embedded or next to the binary. It must run on the test machine's 8 GB NVIDIA GPU faster than the same program on the CPU build. Measure: tokens per second and time to first token, 3 runs. The CPU baseline is the marmot CPU build and, for reference, a C++ inference engine on the same CPU. Output tokens must be identical to the reference within a tolerance on logits. Binary size and start time with and without a GPU.
- ROCm and SPIR-V targets (Vulkan, Metal) from the same lowering.
- StableHLO import and export (path C, and TPUs through PJRT), training with a tape, then compiler level autodiff.
Open decisions: which model for the milestone. Whether the tensor package is a new package or wraps an existing Go API (GoMLX like graphs). How much of IREE's runtime to reuse against writing a small loader.