takara-ai/go-attention: dot-product, multi-head and transformer layers written in pure Go
A full attention mechanism and transformer in pure go.
At a glance
- What is it?
- go-attention is a Go module from takara.ai that implements dot-product attention, multi-head attention and a full transformer layer with no third-party requirements. This article walks through its types, configs and pooling helpers, and where its performance claims stop.
- Who is it for?
- Choose go-attention if you want to read or embed attention and a single transformer layer in plain Go slices with no dependency to vendor. Do not choose it if you need pinned releases, benchmarks beyond one Apple M1 run, or anything other than float64 vectors, and run go test -bench=. ./attention on your own hardware before trusting its latency numbers.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 163 days ago.
- What is it written in?
- Mainly Go, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 25, 2026, and from our analysis. They are not legal advice.
Star history
- takara-ai/go-attention479 ★ · +0
| Repository | 2026-09-11 | 2026-11-25 |
|---|---|---|
| takara-ai/go-attention | 479 | 479 |
0 Stars in the last 75 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
DotProductAttention hands back the attention weights next to the output
The two basic types are plain slices: Vector is a []float64 and Matrix is a []Vector. The simplest entry point, DotProductAttention, takes one query Vector plus a keys Matrix and a values Matrix, and returns three things: output, weights and an error.
output, weights, err := attention.DotProductAttention(query, keys, values)The worked example uses a query of {1.0, 0.0, 1.0, 0.0}, three keys (one similar, one different, one neutral) and three two-number values. Its printed result is attention weights of [0.523 0.174 0.302] and an output of [2.558 3.558], a weighted combination of the values. Because the weights come back, you can inspect which key the query favoured instead of only seeing the blended result. Every number is a float64, so inputs from other precisions need converting before they reach this API.
Multi-head configuration ties the per-head sizes to DModel divided by NumHeads
NewMultiHeadAttention takes a MultiHeadConfig. The example sets NumHeads to 4, DModel to 64, DKey to 16, DValue to 16 and DropoutRate to 0.1. The inline comments say DKey and DValue are the size per head, written as DModel/NumHeads, so 64 divided by 4 gives 16.
config := attention.MultiHeadConfig{
NumHeads: 4, // Number of parallel attention heads
DModel: 64, // Size of input/output embeddings
DKey: 16, // Size per head (DModel/NumHeads)
DValue: 16, // Size per head (DModel/NumHeads)
DropoutRate: 0.1, // For regularization
}The constructor returns an error, and the example handles it with log.Fatal, so a config that does not fit is meant to fail at construction time. The printed output for a batched input of 2 batches by 3 tokens by 64 features is a shape of [6×64]. In that example the batch dimension is not kept separate, so you map rows back to batches yourself.
The transformer layer keeps its input shape from [3×64] to [3×64]
The transformer package builds a layer from a TransformerConfig with DModel set to 64, NumHeads to 4, DHidden to 256 and DropoutRate to 0.1. DHidden is the size of the feed-forward hidden layer, so each layer combines self-attention with a feed-forward network. Creation goes through transformer.NewTransformerLayer(config), which also returns an error.
The example builds an input sequence shaped [seq_len × d_model] with a seqLen of 3. The printed result is an input shape of [3×64] and an output shape of [3×64], which means layers can be stacked by feeding one layer's output into the next. The example shows one layer only. There is no embedding step, tokenizer or output head in what you are shown, so a language model would need those parts from somewhere else. The transformer package imports the attention package, and both live in this module.
projectVector reads weight rows in order after a column-major cache-miss fix
One performance note is specific enough to be checked in the source. projectVector uses a row-major loop order so that weight rows are read sequentially. The README says this fixed a column-major stride that caused cache misses on the Q, K and V projections and on the feed-forward matmuls.
The other built-in optimizations are an 8x unrolled dot product and object pools that cut allocation overhead in hot paths. The module also has a go.mod that declares go 1.23.4 and has no require lines, so building it does not fetch other modules. If your toolchain is older than that directive, expect the Go command to complain before it builds anything. The examples live under examples/basic, examples/advanced, examples/data and examples/performance, and the quick start runs api_examples.go with go run.
Pools and monitoring are package-level switches you must drive correctly
Performance control happens through package-level functions, not through the layer configs. You fetch a default config, flip EnableMonitoring and EnableAutoTuning, and install it globally:
config := attention.DefaultPerformanceConfig()
config.EnableMonitoring = true
config.EnableAutoTuning = true
attention.SetPerformanceConfig(config)Vector pooling is manual. GetVectorFromPool(size) hands you a vector and PutVectorToPool returns it, with the example deferring the put so the vector goes back when the function exits. A vector you forget to return is simply not reused, and one you keep using after returning it is shared memory, so treat the pool as a loan. Statistics come from GetAllPerformanceStats(). Parallel processing has its own DefaultParallelConfig(), described as goroutine-based parallelization for multi-core systems, so concurrency settings sit apart from the monitoring switch.
The only benchmark figures come from one Apple M1 and cover dot products only
The README gives timings from go test -bench=. ./attention on an Apple M1: about 17 to 62ns per dot product for vectors of 64 to 256, about 250 to 290ns for 512 to 1024, and about 970 to 1230ns for 4096 and above. Those figures are for DotProduct on single vectors, called as attention.DotProduct(v1, v2).
They do not time multi-head attention or the transformer layer, and no other machine is measured. The same section says the performance characteristics are the same on any Go-compatible platform, but that sentence is a claim and not a result, since one chip is the only evidence shown. Words such as production-grade and predictable latency also come without latency percentiles or comparisons against another implementation. If latency matters to you, rerun the benchmark command above on your own hardware and then time a full layer, because the published numbers do not tell you what a layer costs.
There are no releases, so go get follows the default branch
The repository has no GitHub releases, and the README's install step is a plain module fetch followed by running the examples:
# Get the module
go get github.com/takara-ai/go-attention
# Run the examples
go run api_examples.goWithout release tags there is no named version to pin to and no release notes to read before you upgrade. The default branch is main, and the last push landed on 2026-06-15. The repository is not archived and carries an MIT license. The full API reference is in API.md in the repository root, next to cmd/, attention/, transformer/ and examples/. Since the code takes float64 slices and leaves tokenization and training out of view, adopt it as a set of building blocks you review yourself, and record the commit you fetched in your go.sum.
Editorial conclusion
Choose go-attention if you want to read or embed attention and a single transformer layer in plain Go slices with no dependency to vendor. Do not choose it if you need pinned releases, benchmarks beyond one Apple M1 run, or anything other than float64 vectors, and run go test -bench=. ./attention on your own hardware before trusting its latency numbers.
Frequently asked questions
What is the attention mechanism in go-attention?
In the dot-product example a query Vector is compared with a keys Matrix, which produces weights such as [0.523 0.174 0.302] that blend the values Matrix into an output like [2.558 3.558].
What attention mechanisms in LLMs does go-attention cover?
It provides dot-product attention, multi-head attention configured with NumHeads, DModel, DKey and DValue, and a transformer layer with a feed-forward size DHidden, all written in pure Go.
What is the formula for attention as computed by go-attention?
You call attention.DotProductAttention(query, keys, values), which returns the output, the weights and an error; the sample run prints weights [0.523 0.174 0.302] and output [2.558 3.558].
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/takara-ai-go-attention)Star history chart for your README
[](https://hysenlabs.com/projects/takara-ai-go-attention)Was this useful?