v0.5.1 PyTorch 2.0+ MIT Built with PCG
Attention, solved—not softmaxed.
xaker fuses Exclusive Self Attention with kernel ridge regression and solves the regularised system with Preconditioned Conjugate Gradient. One PyTorch module. Deterministic benches, typed API, paper-grade rubrics.
# A mathematically grounded attention block.
from xaker import Config, Model
import torch
cfg = Config(dim=512, heads=8, kernel="exp", precond="fast")
m = Model(cfg, num_layers=6, vocab_size=32000,
max_seq_len=512, attention_type="fused")
x = torch.randint(0, 32000, (2, 128))
logits = m(x)
print(f"parameters: {sum(p.numel() for p in m.parameters()):,}")
print(f"logits shape: {logits.shape}")
01 / Problem framing
Softmax made attention easy to write. It also constrains the math.
Scaled dot-product attention scales scores by softmax and produces a positive, doubly-stochastic interaction. The construction is clean, but the matrix it operates on is dense, the kernel it builds is not positive semidefinite, and the spectral structure can deteriorate on long sequences.
The empirical consequence
On a sequence of length 128 with dim=64, the condition number of the score matrix reaches ~7.5 × 104. A regularised formulation working in the same basis reaches the high single digits. The ratio is not a constant; it widens with length.
xaker takes the second path. Attention is rewritten as a kernel ridge regression: (K + λI) α = v, solved iteratively. The diagonal of K is removed (Exclusive Self Attention) so each token's self-aligned component no longer dominates its own output. A preconditioner chosen from a fixed strategy set accelerates the iterate.
02 / The system
One package, six registries, one typed driver.
xaker is built around polymorphism, not mode flags. Four attention variants, four kernels, four preconditioners, three XSA modes, two iterative solvers, and a typed benchmark driver — all on the same nn.Module surface.
BLOCK
Polymorphic registry for attention variants: standard, xsa, fused, linear. Adding a variant is one class plus one entry.
Kernels
exp, rbf, linear, cosine. Stateless ops plus a learnable stateful wrapper for Fused.
Preconditioners
Identity, Diagonal, Fast, Cccp. One factory, Make(config), dispatches by string.
XSA modes
Projection, Zero, Mask. Strategy triple driven by XsaStrategy(config, scale).
PCG solver
pcg and richardson over the regularised operator (K + λI). Returns a Solve dataclass with full history.
Bench driver
Typed Spec → run → write producing schema- stable JSON, environment block, per-seed statistics, git_sha.
Four CLIs
xaker-train, xaker-eval, xaker-bench, xaker-validate. Save and load a checkpoint; the train→eval round-trip is on by default.
Rubric gate
Six dimensions enforce paper-worthiness in CI: novelty, repro, correctness, efficiency, stability, usability.
03 / How xaker works
The pipeline, end to end.
One pass through Fused.attend per head. The tokens become a kernel; the kernel becomes a regularised system; the system is solved.
Source: xaker/attention/fused.py. Math derivation: Mathematical foundations.
04 / Evidence
One number, then a chart, then a derived claim.
The headline condition-number ratio is reproducible from a single command. The chart on the right shows how the gap widens with sequence length. Every table on this page is regenerated from paper_runs/ JSONs on every commit.
Condition number vs sequence length
log-scale. Lower is better. dim=64, lam=10.0, CPU.
Reproduce with:
python -m xaker.bench.condition --lam 10.0 --lengths 16 32 64 128 --out paper_runs/condition.json.
What changes when you switch
Side-by-side, in the same backbone, on the same hardware. Numbers below come from paper_runs/.
05 / What ships
The package surface, as code.
Every public symbol is a single word. No _private names, no shim modules, no aliases. The polymorphic registry is the design.
Attention variants
- StandardVaswani-style scaled dot-product attention.
- XsaExclusive Self Attention with strategy dispatch.
- FusedFlagship: XSA + kernel ridge regression via PCG.
- LinearLinear-complexity baseline (Katharopoulos et al., 2020).
Kernel functions
- expcosine-sim exponential, default; stateful + learnable.
- rbfclassical Gaussian kernel σ = 1.
- linearraw inner product; ridge compensates.
- cosineL2-normalised inner product, range [−1, 1].
Preconditioners
- IdentityP(r) = r; baseline.
- Diagonallearned softplus-positive Jacobi.
- Fastlearned low-rank + diagonal; cache-aware.
- CccpTyler M-estimator; eigh-based inverse half-power.
Solvers and dispatch
- pcgPreconditioned Conjugate Gradient; returns Solve.
- richardsonfixed-iteration preconditioned Richardson.
- BLOCKattention dispatch: standard · xsa · fused · linear.
- Makepreconditioner dispatch by config.precond.
06 / Documentation
Pick a track. The doc system meets you where you are.
Four entry points. Each track tells you which docs to read first and which to skip.
First-time user
I want to install xaker and run a forward pass.
Researcher
I want to understand the math, the pipeline, the numbers.
Contributor
I want to add a kernel, a preconditioner, a variant.
Benchmark / validation
I want to evaluate xaker against my own baseline.
07 / Quickstart
Two commands to a benchmark. Three to a Transformer.
The working install is the source install (PyPI mirroring is planned). The headline benchmark is one reproducible command.
git clone https://github.com/sachncs/xaker.git
cd xaker
python3 -m venv .venv
source .venv/bin/activate
pip install -e '.[dev]'
The .[dev] is intentional; it means "this package, plus dev extras."
xaker-validate
# novelty 2/3 novel=2/2 (fused.py,linear.py)
# repro 3/3 seeds=True bench=True paper_runs=12 cudnn=True
# correctness 3/3 fused=True cg=True dispatch=True
# efficiency 3/3 bench=True specs=True runs=12
# stability 3/3 seeds=True runs=True dtype=True
# usability 3/3 cli=True readme=True rubric=True
# Total 17/18 — PASS
from xaker import Config, Model
import torch
cfg = Config(dim=512, heads=8,
kernel="exp", precond="fast")
m = Model(cfg, num_layers=6, vocab_size=32000,
max_seq_len=512, attention_type="fused")
logits = m(torch.randint(0, 32000, (2, 128)))
print(logits.shape) # torch.Size([2, 128, 32000])
The same Config drives Standard, Xsa, Fused, and Linear through the BLOCK registry — no if mode == "fused" chains.
python -m xaker.bench.condition \
--lam 10.0 --lengths 16 32 64 128 \
--out paper_runs/condition.json
# wrote paper_runs/condition.json
# ✓ headline condition numbers match the four metric cards above
08 / Design principles
Why the code looks the way it does.
A short manifesto on the choices behind xaker. These are defended in design_decisions.md; here are the four anchors.
Polymorphism, not flags.
The four attention variants, four kernels, four preconditioners, and three XSA modes are all selected through a single dispatch. Adding a variant is one class plus one registry entry. Adding a fifth is the same amount of work.
The math is the spec.
docs/math.md and xaker/solver/precond.py agree byte for byte. If a derivation disagrees with the code, the code wins until a paper revision moves the goalposts.
Solver-backed, not softmax-backed.
Attention as a regularised system gives you a preconditioner knob. Identity, Diagonal, Fast, and Cccp cover the spectrum from debug-only to best-converges-on-ill-conditioned-kernels. Preconditioner choice is a config field, not a fork.
Numerical stability is a contract.
BOUND clamps. BOUND does not vary with the kernel. λ is guaranteed positive via softplus + ε. The dtype frontier is documented, not hidden.
One public surface. One word per symbol.
_private prefixes are gone. Multi-word snake-case is gone. Aliases are gone. CI enforces this. The single-word rule keeps the API coherent across versions.
What we did not build, deliberately.
No FlashAttention. No sparse / Nyström. No AMP. No transformers-hub integration. Each of these is documented as absent and survives a paper revision. Roadmap, not TODO.
09 / Limitations
Where the math behaves, and where it doesn't.
Read this section before using xaker in production. The dtype frontier is a contract; the solver fallbacks are explicit; the unsupported environments are listed.
10 / Quality bar
This library is evaluated, not just presented.
The paper-worthiness rubric runs on every push to master and is enforced by CI. Each dimension below maps to a real grader that inspects the repo and emits an evidence string. The current run is presented here; it moves with the codebase.
11 / Cite
Cite xaker.
The paper is in preparation. Until the arXiv identifier is assigned, cite the GitHub release matching the version you used. A canonical CITATION.cff ships at the repository root and is what GitHub's "Cite this repository" button reads.
@software{xaker,
title = {xaker: A Mathematically Grounded Attention Framework},
author = {sachin},
year = {2026},
url = {https://github.com/sachncs/xaker},
note = {Paper in preparation; arXiv ID will replace this entry on submission.}
}