Skip to main content

rawformer

Transformers from scratch, in pure PyTorch. Not production-ready — this is for my own use and understanding.

Architectures: ViT Positional encodings: Learned, AxialRoPE

Installation

pip install rawformer

Two env vars control runtime checking:

  • BEARTYPE=1 — enable runtime type checking (default off, needs pip install rawformer[typecheck])
  • PYTHONOPTIMIZE=1 — strip assert statements (default on)

Most errors will be caught by ValueError, it is pretty safe to strip asserts.

Usage notes

Exclude position parameters from weight decay

ViT.no_weight_decay() returns the parameter names that must not be decayed — the class token and everything in the position scheme:

skip = model.no_weight_decay()
params = list(model.named_parameters())
optim = torch.optim.AdamW(
    [
        {"params": [p for n, p in params if n not in skip], "weight_decay": 0.05},
        {"params": [p for n, p in params if n in skip], "weight_decay": 0.0},
    ],
    lr=1e-3,
)

AdamW(model.parameters(), weight_decay=...) decays the learned position table and the class token, and the common p.ndim >= 2 grouping rule doesn't save them — both are ndim-3.

RoPE base is a buffer by default

AxialRoPE's rotary base is a non-trainable buffer. Its gradient is structurally attenuated (the i=0 channel contributes nothing, and the channels that do contribute are weighted by their own small θ), while decoupled weight decay acts on it regardless — so as a parameter it drifts toward 0, collapsing every rotary frequency to ~1.0 and destroying positional resolution over a long run. Pass learnable=True to opt in, and exclude it from decay via no_weight_decay().

Choosing rotary_dim

RoPE is applied per attention head, so rotary_dim must be <= head_dim (not embed_dim). Values below head_dim are valid and leave the remaining dimensions unrotated. Must be divisible by 2 * n_axes.

Axis order

AxialRoPE assigns axes[k] to spatial_shape[k], and flattens the position grid row-major to match the tokenizer's output order. Per-axis init_theta must be given in the same order.

Changing input resolution

AxialRoPE handles new input resolutions unchanged. LearnedPositionEmbeddings does not — it raises above max_len, with no position-embedding interpolation.

Developer install

uv sync --extra cpu    # or --extra cuda
uv pip install -e .

pre-commit install
pre-commit run --all-files

Metadata

Release files for rawformer 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rawformer 0.5.0
File Size Uploaded Provenance Actions
rawformer-0.5.0.tar.gz 11.0 kB Without provenance Details for rawformer-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for rawformer 0.5.0
File Interpreter ABI Platform Actions
rawformer-0.5.0-py3-none-any.whl Python 3 none any Details for rawformer-0.5.0-py3-none-any.whl

Total release size: 24.3 kB

Release files / rawformer-0.5.0.tar.gz

Download URL rawformer-0.5.0.tar.gz
Size 11.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d280e7a556b2f2056993c556eb1c1e3f8151c6187ef90a8b84a657d6e8da1e83
BLAKE2b-256 checksum
How to use checksums
fb16c6a3a40d5bd7d0cb8a975aa1219134fd4a3b9422044fcfb349724e308387
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"44","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / rawformer-0.5.0-py3-none-any.whl

Download URL rawformer-0.5.0-py3-none-any.whl
Size 13.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b796551e6a6c7d37af18b354a3abeb4981138ec8c4bb6c1172a33a94ffb63678
BLAKE2b-256 checksum
How to use checksums
e38cccd04fb42b472ddc3ae56fb7c442937815ebfbbcd4f90e711b2416f08995
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"44","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for rawformer 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page