GoMLX
An accelerated Machine Learning framework for Go
About GoMLX
This large README is shown as a plain-text preview. Read the full README at the source
# **_GoMLX_**, an Accelerated ML and Math Framework
[](https://gomlx.github.io/)
[](https://pkg.go.dev/github.com/gomlx/gomlx?tab=doc)
[](https://hub.docker.com/r/janpfeifer/gomlx_jupyterlab)
[](https://github.com/gomlx/gomlx/blob/main/LICENSE)
[](https://gophers.slack.com/archives/gomlx)
[](https://github.com/avelino/awesome-go#machine-learning)
[](https://github.com/gomlx/gomlx/blob/main/README.md#-support-the-project)
<br/>
[](https://github.com/gomlx/gomlx/actions/workflows/linux_amd64_tests.yaml)
[](https://github.com/gomlx/gomlx/actions/workflows/linux_arm64_tests.yaml)
[](https://github.com/gomlx/gomlx/actions/workflows/darwin_tests.yaml)
[](https://github.com/gomlx/gomlx/actions/workflows/windows_amd64_tests.yaml)

## π About **_GoMLX_** - [gomlx.github.io](https://gomlx.github.io/)
<img align="right" src="docs/gomlx_gopher2.png" alt="GoMLX Gopher" width="220px"/>
**GoMLX** is an easy-to-use set of Machine Learning and generic math libraries and tools.
It can be seen as a **PyTorch/Jax/TensorFlow for Go**.
It can be used to train, fine-tune, modify, and combine machine learning models (it reads models from HuggingFace,
with a growing list of model support).
It provides all the tools to make that work easy: from a complete set of differentiable operators,
all the way to UI tools to plot metrics while training in a notebook.
It defines a common "backend" API to run models. It includes a pure Go backend that is portable and also runs in WASM (in a browser), [see demo created with GoMLX](https://janpfeifer.github.io/hiveGo/www/hive/).
The _"xla"_ backend is an optimized engine based on [OpenXLA](https://github.com/openxla/xla)
that uses just-in-time compilation to CPU, GPUs (Nvidia, and likely AMD ROCm, Intel, Macs) and Google's TPUs.
It also supports modern distributed execution (**new, still being actively improved**) for multi-TPU or multi-GPU
using XLA Shardy, an evolution of the [GSPMD distribution](https://arxiv.org/abs/2105.04663)).
It's the same engine that powers Google's [Jax](https://github.com/google/jax),
[TensorFlow](https://tensorflow.org/) and [Pytorch/XLA](https://docs.pytorch.org/xla/master/learn/xla-overview.html),
and it has the same speed in many cases (*).
More recently, it added the _"onnx"_ backend, which uses [ONNX Runtime](https://onnxruntime.ai/) to run GoMLX computations. It can also save models to `.onnx` file format.
> [!Tip]
> * **Documentation at [gomlx.github.io](https://gomlx.github.io/)**.
> * See our π [**tutorial**](https://gomlx.github.io/gomlx/notebooks/tutorial.html) π
> * A [guided example for Kaggle Dogs Vs Cats](https://gomlx.github.io/gomlx/notebooks/dogsvscats.html).
<div>
<p>It was developed to be a full-featured ML platform for Go, productionizable and easy to experiment with ML ideas
βsee Long-Term Goals below.</p>
It strives to be **simple to read and reason about**, leading the user to a correct and transparent mental model
of what is going on (no surprises)βaligned with Go philosophy.
At the cost of more typing (more verbose) at times.
It is also incredibly flexible and easy to extend and try non-conventional ideas: use it to experiment with new
optimizer ideas, complex regularizers, funky multitasking, etc.
Documentation is kept up to date (if it is not well-documented, it is as if the code is not there),
and error messages are useful (always with a stack-trace) and try to make it easy to solve issues.
</div>
## News
- **π NEW π** **Apple Silicon `"mps"` & `"mlx"` GPU Backends** ([`github.com/gomlx/go-darwinml`](https://github.com/gomlx/go-darwinml)):
- **`"mps"`**: Native macOS GPU acceleration using Apple's Metal Performance Shaders Graph (`MPSGraph`), with zero-copy unified memory shared buffers (`MTLBuffer`) and mixed-scope ping-pong buffer pooling for training.
- **`"mlx"`**: Native macOS GPU acceleration using Apple's [MLX](https://github.com/ml-explore/mlx) framework (`mlx::core`), supporting both compiled static execution (`GOMLX_BACKEND=mlx`) and native dynamic-shape execution without recompilation (`GOMLX_BACKEND=mlx:dynamic`), zero-copy unified memory `mlx::core::array` buffers, and native in-place buffer donation (requires `brew install mlx`).
- Automatically included in `github.com/gomlx/gomlx/backends/default` on Darwin builds (select with `GOMLX_BACKEND=mps` or `GOMLX_BACKEND=mlx`, or exclude at build time with `-tags=nomps` / `-tags=nomlx`).
- *Note*: With the new ARM64 NEON and Apple `SME` SIMD microkernels in the `"go"` backend, `"go"` is often faster than `"mps"`/`"mlx"` on Apple Silicon for many small-to-medium workloads.
- **π NEW π** **Parameter-Efficient Fine-Tuning (PEFT / LoRA)** ([`ml/layers/peft`](ml/layers/peft)):
- **Zero code changes**: Simply `import _ "github.com/gomlx/gomlx/ml/layers/peft"` and set the hyperparameter (e.g. `-set=peft_adapter=my_lora;peft_rank=16`).
- **Automatic adapter injection**: Hooks into `FNN`, `Dense`, and `MultiHeadAttention` linear layers, automatically freezing base weights and training only low-rank adapters.
- **Selective targeting & multi-adapter**: Target specific projections (`-set=peft_target_modules=query,value`), freeze layers (`peft_adapter=off`), and store or switch multiple named adapters in the same checkpoint. See the [PEFT guide](docs/peft.md).
- **π NEW π** New `onnx` backend, based on ONNX Runtime. It has support for "onnx:cpu", "onnx:cuda" (CUDA) and "onnx:rocm" versions support. For WebAssembly (WASM) it supports "onnx:wasm" (CPU), "webgpu" (GPU) and "webnn" (experimental).
- Includes saving a model to a `.onnx` file, that can be served with other inference systems that support ONNX (e.g. [KnightAnalytics Hugot](https://github.com/knights-analytics/hugot)).
- **π NEW π** The "go" backend has been greatly optimized, **matching or exceeding "xla:cpu" speeds on many FNN and Transformer workloads** (try out the [UCI-Adult/Census model](https://gomlx.github.io/gomlx/notebooks/uci-adult.html)):
- **Hierarchical 3-Level Parallel Scheduler**: Replaced `workerspool` with a persistent, deadlock-free scheduler prioritizing `Inter-Call` > `Intra-Op` > `Inter-Op` parallelism with dynamic graph execution, Guided Self-Scheduling (GSS), and $2 \times W$ proportional chunking to minimize synchronization overhead and maximize cache locality.
- **Improved Buffer & Constant Memory Management**: Shared constants across compilations, exact-sized non-pooled constant allocations, and one-time GEMM weight panel pre-packing with automatic raw buffer eviction.
- **Broadened SIMD Support (AMD64 & ARM64)**: Uses Go 1.27 portable `simd` across platforms alongside handwritten assembly microkernels for **AMD64** (`AVX2` and `AVX-512`) and **ARM64** (`NEON` and Apple `SME` on Darwin and Linux)βreaching up to **2.51 TFlops/s** on Apple M6 (**20Γβ25Γ faster** than `nosimd`) and outperforming `xla:cpu` by **1.38Γ** on `all-MiniLM-L6-v2` sentence encoding. Also added SIMD convolution implementations.
## πΊοΈ Overview
**GoMLX** is a full-featured ML framework, supporting various well-known ML components
from the bottom to the top of the stack. But it is still only a slice of what a major ML library/framework should provide
(like TensorFlow, Jax, or PyTorch).
### GoMLX Examples
**Training examples:**
* [UCI-Adult/Census model](https://gomlx.github.io/gomlx/notebooks/uci-adult.html);
* [How do KANs learn ?](https://gomlx.github.io/gomlx/notebooks/kan_shapes.html);
* [Cifar-10 demo](https://gomlx.github.io/gomlx/notebooks/cifar.html);
* [MNIST demo (library and command-line only)](https://github.com/gomlx/gomlx/tree/main/examples/mnist)
* [Dogs & Cats classifier demo](https://gomlx.github.io/gomlx/notebooks/dogsvscats.html);
* [IMDB Movie Review demo](https://gomlx.github.io/gomlx/notebooks/imdb.html);
* [Diffusion model for Oxford Flowers 102 dataset (generates random flowers)](examples/oxfordflowers102/OxfordFlowers102_Diffusion.ipynb);
* [Flow Matching Study Notebook](https://gomlx.github.io/gomlx/notebooks/flow_matching.html) based on Meta's ["Flow Matching Guide and Code"](https://ai.meta.com/research/publications/flow-matching-guide-and-code/).
* [GNN model for OGBN-MAG (experimental)](examples/ogbnmag/ogbn-mag.ipynb).
* Last, a trivial [synthetic linear model](https://github.com/gomlx/gomlx/blob/main/examples/linear/linear.go), for those curious to see a barebones simple model.
* Neural Style Transfer 10-year Celebration: [see a demo written using GoMLX](https://github.com/janpfeifer/styletransfer/blob/main/demo.ipynb) of the [original paper](https://arxiv.org/abs/1508.06576).
* [Triplet Losses](https://github.com/gomlx/gomlx/blob/main/ml/train/losses/triplet.go): various negative sampling strategies as well as various distance metrics.
* [AlphaZero AI for the game of Hive](https://github.com/janpfeifer/hiveGo/): it uses a trivial GNN to evaluate
positions on the board. It includes a [WASM demo (runs GoMLX in the browser!)](https://janpfeifer.github.io/hiveGo/www/hive/) and a command-line UI to test your skills!
**Inference Examples:**
* **π NEW π** [SAM2: Segment Anything Model (Facebook)](https://github.com/gomlx/go-huggingface/blob/main/models/sam2/README.md): model to segment images (videos version not ported yet).
* **π NEW π** [Gemma4-4B-it library and demo](https://github.com/gomlx/go-huggingface/tree/main/examples/gemma4-e4bit):
Google's new free generative LLM, instruction tuned. See also [HuggingFace's "google/gemma-4-E4B-it" model page](https://huggingface.co/google/gemma-4-E4B-it).
* [KaLM-Gema3 12B parameters](https://github.com/gomlx/go-huggingface/tree/main/examples/kalmgemma3):
Tencent's top-ranked sentence encoder for RAGs, using [go-huggingface](https://github.com/gomlx/go-huggingface/) to
load the model and tokenizer, and **GoMLX** to execute it.
* [Gemma 3 270M](https://github.com/gomlx/gomlx/tree/main/examples/gemma3): Demonstrates ONNX-converted
text generation (LLM) using the [onnx-community/gemma-3-270m-it-ONNX](https://huggingface.co/onnx-community/gemma-3-270m-it-ONNX)
model with GoMLX.
It uses the [`gomlx/onnx-gomlx`](https://github.com/gomlx/onnx-gomlx) package to convert the model, and [`gomlx/go-huggingface`](https://github.com/gomlx/go-huggingface) to download the model and run the tokenizer.
* **π NEW π** [GPT-2](https://github.com/gomlx/gomlx/tree/main/examples/gpt2): Demonstrates text generation using
the new (experimental) transformer and generator packages.
* [BERT-base-NER](https://github.com/gomlx/gomlx/tree/main/examples/BERT-base-NER): A BERT-base model fine-tuned
for Named Entity Recognition. It's also an ONNX-converted model from [dslim/bert-base-NER model](https://huggingface.co/dslim/bert-base-NER) from HuggingFace.
* [MixedBread Reranker v1](https://github.com/gomlx/gomlx/tree/main/examples/mxbai-rerank): A cross-encoder reranking
example, see [HuggingFace MixedBread Reranker v1 page](https://huggingface.co/mixedbread-ai/mxbai-rerank-base-v1).
It uses the [`gomlx/onnx-gomlx`](https://github.com/gomlx/onnx-gomlx) package to convert the model, and [`gomlx/go-huggingface`](https://github.com/gomlx/go-huggingface) to download the model and run the tokenizer.
### Backends
GoMLX is a friendly "intermediary ML API", that hosts a common API and a library
of ML layers and such. But per-se it doesn't execute any computation: it relies
on different backends to compile and execute the computation on very different
hardware.
The supporting backend package must always be included in a GoMLX program, and the
default backends (`xla`, `go`, `mps` and `mlx` on Darwin, and optionally `onnx`, see below) are included if you import:
```
import _ "github.com/gomlx/gomlx/backends/default"
```
At runtime, it will pick the fastest backend available to you, but you can specify which backend to use by setting the `GOMLX_BACKEND` environment variable (e.g. `export GOMLX_BACKEND=xla:cuda`, `export GOMLX_BACKEND=xla:cpu`, `export GOMLX_BACKEND=mlx`, `export GOMLX_BACKEND=mps`, or `export GOMLX_BACKEND=go`).
There is a common `compute.Backend` interface defined in
[`github.com/gomlx/compute`](https://github.com/gomlx/compute). And there are 5
different implementations (more planned in the future):
1. **`xla`**: [OpenXLA](https://github.com/openxla/xla) backend for CPUs, GPUs, and TPUs. State-of-the-art as these things go, but only static-shape.
For linux/amd64, linux/arm64 (CPU) and darwin/arm64 (CPU) for now. The Go API is defined under the [go-xla](https://github.com/gomlx/go-xla) project,
in `github.com/gomlx/go-xla/compute/xla/autoinstall`. It is included by default.
2. **`go`**: a pure Go backend (no C/C++ dependencies), implemented in `github.com/gomlx/compute/gobackend`, but included by default.
It is slower than XLA for large training workloads, but very portable (compiles to WASM/Windows/etc.) and fast on CPU.
* **SIMD support**: portable Go 1.27 `simd` vectorization plus handwritten assembly microkernels for **AMD64** (`AVX2`, `AVX-512`) and **ARM64** (`NEON`, Apple `SME` on Darwin and Linux) across `DotGeneral`/`FusedDense`, `FusedScaledDotProductAttention`, reductions, binary broadcasts, and half-precision (`Float16`/`BFloat16`).
* **π NEW π**: hierarchical 3-level parallel scheduler (`Inter-Call` > `Intra-Op` > `Inter-Op`) with asymmetric CPU cluster awareness (Super, Performance, and Efficiency cores), **fused operations**, constant weight panel caching, and quantization support.
* See also [GoMLX compiled to WASM to power the AI for a game of Hive](https://janpfeifer.github.io/hiveGo/www/hive/)
* Supports dynamic shapes (experimental).
3. **[onnx](https://github.com/gomlx/compute-onnx)**: A backend that uses ONNX Runtime to execute GoMLX graphs: for Linux/Windows AMD64 only. Build with tag `--tags=onnx` to get it imported by `github.com/gomlx/gomlx/backends/default`, or import it directly from `github.com/gomlx/compute-onnx`. It supports CPU (`GOMLX_BACKEND="onnx:cpu"`), GPU (`"onnx:cuda"` or `"onnx:gpu"`), WASM CPU (`"onnx:wasm"`), WASM GPU (`"onnx:webgpu"`) and the exeperimental WASM WebNN (`"onnx:webnn"`).
4. **π NEW π** **[`mps`](https://github.com/gomlx/go-darwinml)** and **[`mlx`](https://github.com/gomlx/go-darwinml)**: Apple Silicon GPU backends for Darwin (macOS) in [`github.com/gomlx/go-darwinml`](https://github.com/gomlx/go-darwinml), both included by default on Darwin builds (can be excluded with `-tags=nomps` or `-tags=nomlx`):
* **`mps`** (`github.com/gomlx/go-darwinml/compute/mps`): Uses Apple's system Metal Performance Shaders Graph (`MPSGraph`) framework (no external libraries required).
* **`mlx`** (`github.com/gomlx/go-darwinml/compute/mlx`): Uses Apple's [MLX](https://github.com/ml-explore/mlx) framework (`brew install mlx`), supporting both compiled static execution (`GOMLX_BACKEND=mlx`) and native dynamic shapes (`GOMLX_BACKEND=mlx:dynamic`).
* Note that with the ARM64 `NEON` and Apple `SME` SIMD acceleration in the `"go"` backend, `"go"` is often faster than `"mps"`/`"mlx"` on Macs for small-to-medium workloads.
### Highlights
Some selected highlights:
* **π NEW π**: **ARM64 NEON & Apple SME SIMD Support + Improved Parallel Scheduler** in the pure Go backend (`gobackend`): handwritten NEON and `FEAT_SME` microkernels for Darwin and Linux ARM64 (reaching up to **2.51 TFlops/s** on Apple Silicon), asymmetric CPU cluster scheduling, and a hierarchical 3-level parallel scheduler (`Inter-Call` > `Intra-Op` > `Inter-Op`) with constant weight panel caching.
* **π NEW π**: **Gradient checkpointing**: trade-off memory usage for recomputation when training large models, with a very simple API.
* **π NEW π**: Save models to a `.onnx` file, that can be used with ONNX Runtime. See example in [UCI-Adult demo](https://github.com/gomlx/gomlx/blob/main/examples/adult/demo/save_onnx.go).
* **Parameter-efficient fine-tuning (PEFT / LoRA)**: Zero-code-change fine-tuning via post-linear hooks across `FNN`, `Dense`, and `MultiHeadAttention`. Configure via hyperparameters (`peft_adapter`, `peft_rank`, `peft_target_modules`), with automatic base-weight freezing and multi-adapter support. See the [PEFT guide](docs/peft.md).
* HuggingFace Go compatibility with [go-huggingface](https://github.com/gomlx/go-huggingface):
- Download files from models/datasets sharing the same cache framework as the python version.
- Tokenizers for various classes in pure Go, downloaded directly from HuggingFace.
- Datasets iterators (using Apache's Parquet format), to experiment with standard datasets.
- Model parameters reading from GGUF or `safetensors` format.
- Model conversion to GoMLX (some models at least) with a compatible `transformer` library. Includes support to
sentence embedding (equivalent to `sentence_transformer` Python library).
* Convert ONNX models to GoMLX with [onnx-gomlx](https://github.com/gomlx/onnx-gomlx): both as an alternative for
`onnxruntime` (leveraging XLA), but also to further fine-tune models.
* [Docker "gomlx_jupyterlab"](https://hub.docker.com/r/janpfeifer/gomlx_jupyterlab) with integrated JupyterLab
and [GoNB](https://github.com/janpfeifer/gonb) (a Go kernel for Jupyter notebooks)
* Autodiff: automatic differentiationβonly gradients for now, no jacobian.
* `Store` and `Scope`: simple variable management for ML models.
* ML layers library with the most popular machine learning "layers": FFN layers,
various activation functions, layer and batch normalization, DyT (Dynamic Tanh normalizer, https://arxiv.org/abs/2503.10622), convolutions, pooling, dropout, Multi-Head-Attention
(for transformer layers), LSTM, KAN (B-Splines, [GR-KAN/KAT networks](https://arxiv.org/abs/2409.10594),
Discrete-KAN, PiecewiseLinear KAN), PiecewiseLinear (for calibration and normalization), various regularizations,
FFT (reverse/differentiable), learnable rational functions (both for activations and [GR-KAN/KAT networks](https://arxiv.org/abs/2409.10594)),
VNN (Vector Neural Networks) for SO(3)-Equivariant/Invariant layers, etc.
* Training library, with some pretty-printing:
* Plots for Jupyter notebook, using [GoNB, a Go Kernel](https://github.com/janpfeifer/gonb).
* Various debugging tools: collecting values for particular nodes for plotting, simply logging the value
of nodes during training, stack-trace of the code where nodes are created.
* `gomlx_checkpoints`, the command line tool to inspect checkpoint of train(-ing) models, **generate plots**
with loss and arbitrary evaluation metrics using [Vizb](https://vizb.goptics.org/) (a separate CLI tool --
`--plot` is the only flag that needs it installed; everything else works without it).
See [`cmd/gomlx_checkpoints/README.md`](cmd/gomlx_checkpoints/README.md) for usage details.Frequently Asked Questions
What is GoMLX?
GoMLX is a Machine Learning library for the Go programming language. An accelerated Machine Learning framework for Go
How many GitHub stars does GoMLX have?
GoMLX has 1,693 GitHub stars in the directory's latest synchronization.
How do I install GoMLX?
Install GoMLX with the Go module system using `go get gomlx/gomlx`. Check the repository for the current installation instructions.
What category does GoMLX belong to?
GoMLX is listed under Machine Learning, specifically Machine Learning.