spartan-torch
Custom layers and building blocks for PyTorch — assemble and fine-tune models fast.
Quickstart
import torch
from spartan_torch import TransformerBlock
block = TransformerBlock(
in_size=128, head_size=32, num_heads=4,
out_size=128, ff_hidden_size=512,
)
x = torch.randn(2, 16, 128)
y, _ = block(x) # y: (2, 16, 128), cache kept for KV decoding
What's inside
Drop-in primitives missing from torch.nn, one brick at a time:
spartan_torch.transformers— attentions (MHA/GQA, linear, Linformer, Performer, Reformer), SSM mixers (Mamba / Mamba-2 / Mamba-3), pre/post/hybrid-norm blocks, norms (RMSNorm, …), positionals (RoPE, ALiBi, learned/sinusoidal), chunked and reversible variants;spartan_torch.cnn— residual, bottleneck, depthwise-separable and inverted-residual blocks;spartan_torch.vit— patch embeddings, class tokens, position embeddings, MAE decoder head;spartan_torch.masking/spartan_torch.selfsup— patch masking, DINO heads/loss, momentum encoder;spartan_torch.utils— small training helpers (e.g. warmup scheduler).
Two things this library deliberately does not do:
- No full models. Stock architectures (ResNet, ViT, LLaMA, …) come from
torchvision/timm/ 🤗transformerswith pretrained weights.spartan_torch.compatships key-mappings that load those weights straight into our primitives. - No heavy training frameworks. The core depends only on
torch/torchvision— no Lightning, no HFTrainer. Experiment harnesses live inexperiments/as optional extras.
Reproducibility
RESULTS.md— weight parity of our primitives against timm/torchvision references, plus a latency/memory sweep of sub-quadratic attentions with a Pareto plot. Tables are generated by scripts (scripts/verify_pretrained.py,scripts/bench_attention.py), never edited by hand.
Installation
Requires Python 3.11+ (and a CUDA-capable Torch — see below).
pip install spartan-torch
For contributors, editable install with experiment and dev extras:
pip install -e ".[experiments,dev]"
CUDA wheels (uv): in
pyproject.toml([tool.uv.sources]/[[tool.uv.index]]) torch is pinned to thecu132index (download.pytorch.org/whl/cu132).pipfrom PyPI installs the stock torch build; for a specific CUDA release install with an explicit--index-url https://download.pytorch.org/whl/<cuda-version>.
Development
Dependencies and tests via just + uv:
just sync # install/sync the environment (uv sync --extra dev --extra experiments)
just test # run the test suite (venv python -m pytest)
just parity # reference weight-parity tests (timm/torchvision; pretrained pulls weights over the network)
just verify # refresh the parity table in RESULTS.md
just bench # GPU attention sweep + Pareto plot (requires CUDA)
just clean # delete .venv
Don't run
just sync/just cleanwhile Jupyter kernels are alive — they lock.pyd/.sofiles and uv can't finish the sync.Development happens in a devcontainer:
.devcontainer/(CUDA image, GPU passthrough, host-mounted HuggingFace/W&B caches).
License
MIT — see LICENSE.
Metadata
Release files for spartan-torch 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| spartan_torch-0.0.1.tar.gz | 71.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| spartan_torch-0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 179.2 kB
Release files / spartan_torch-0.0.1.tar.gz
| Download URL | spartan_torch-0.0.1.tar.gz |
|---|---|
| Size | 71.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
16ec18737f71dd27fa934aff988f03c12b9251f37aab24d8f469a1742e8a4baf
|
|
BLAKE2b-256 checksum How to use checksums |
a6c2ee9ab92368a682acba8d0781d5fc4c13c94ccc701cd59b72b6e8f1901393
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / spartan_torch-0.0.1-py3-none-any.whl
| Download URL | spartan_torch-0.0.1-py3-none-any.whl |
|---|---|
| Size | 107.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6f95ea585c762b6f4867d66799cd9f50779215d005eb20c502746047f8f23a5f
|
|
BLAKE2b-256 checksum How to use checksums |
3ca4ac05f1c30bff402896b0f9bb894a4fb36aadec2b7887223cce9fc49e053b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|