tensai¶
tensai is a small machine-learning framework for learning and experiments, written in pure Go. It implements forward passes, backpropagation, and optimization with no external dependencies in the default build — no cgo, no assembly files, no C compiler.
net := model.NewSequential()
net.Add(layer.NewDense(8))
net.Add(&layer.Tanh{})
net.Add(layer.NewDense(1))
net.Add(&layer.Sigmoid{})
net.Compile(2, loss.MeanSquaredError{}, optim.NewAdam(0.05))
net.Fit(inputs, targets, 5000)
pred, _ := net.Predict(inputs)
Despite its size, tensai reaches surprisingly far: the same kernels that train a XOR network run the published GPT-2 checkpoint, chat with Qwen2.5 and Gemma 3, decode llama.cpp GGUF quantizations block-exactly, and serve an OpenAI-compatible API — all in pure Go.
Highlights¶
- Matrices and N-d tensors — float32
Matrixand rank-NTensorwith NumPy-style broadcasting, batchedMatMul, zero-copyReshapeand views - Layers —
Embedding,Dense,Conv2D,MaxPool2D,BatchNorm,LayerNorm,Dropout, plusReLU,LeakyReLU,GELU,Sigmoid,Tanh, andSoftmax - Training —
Sequentialmodels withCompile→Fit/FitStep→Predict, three loss functions, momentumSGD/Adam/AdamW, and dataset utilities; a full MLP step runs in ~29 allocations - Autograd — a micrograd-style reverse-mode engine over n-dimensional tensors: broadcasting arithmetic, batched
MatMul,LayerNorm,Embed, andCrossEntropyall differentiate, so an attention block over (batch, seq, model) is written directly. ATaperecycles a step's buffers, which takes the charrnn example from 22MB of allocation per step to 0.75MB.rnn.Cell,rnn.LSTMCell, andrnn.SelfAttentionare built on top; backpropagation through time is a plain Go loop - SIMD acceleration — AVX2 kernels written with Go's experimental
simd/archsimdpackage; build withGOEXPERIMENT=simd, and every other build uses the portable fallbacks automatically - WebGPU backend —
-tags wgpuruns batchedMatMul, attention, and a full quantized transformer decode step on any GPU wgpu-native reaches, throughpuregowith no cgo - int8 / int4 quantization — weight-only quantized matmuls that reach memory bandwidth, plus MXFP4 for gpt-oss
- Model formats — TFLite and ONNX export, safetensors read/write, and a GGUF reader covering the K-quants — all with in-tree encoders, still no dependencies
- Tokenizers — Hugging Face
tokenizer.jsonbyte-level BPE (GPT-2, cl100k, o200k families) and SentencePiece, verified to match the reference implementations exactly - LLM inference —
_example/gpt2, and thetensaicommand — ten model families withrun,chat, andservesubcommands
Where to go next¶
- Getting Started — install and train your first model
- Guide — tensors, layers, training, autograd, quantization, SIMD, GPU
- Model Formats — TFLite, ONNX, safetensors, GGUF
- LLM Inference — run real language models in pure Go
- Examples — thirteen runnable examples, from hello-world to GPT-2 in pure Go
Design notes¶
- All operations are batched: inputs are
MxNmatrices, whereMis the batch size andNis the feature dimension - The
Layerinterface standardizesForward,Backward,Params, andGrads, which keeps new layers straightforward to add Denseweights use Glorot/He-style initialization to keep early training stableSoftmaxCrossEntropysubtracts the row maximum before softmax for numerical stability