Skip to main content

Transformer Tricks

PyPI PyPI Downloads

A collection of tricks to simplify and speed up transformer models:

These transformer tricks extend a recent trend in neural network design toward architectural parsimony, in which unnecessary components are removed to create more efficient models. Notable examples include RMSNorm’s simplification of LayerNorm by removing mean centering, PaLM's elimination of bias parameters, and decoder-only transformer's omission of the encoder stack. This trend began with the original transformer model's removal of recurrence and convolutions.

For example, our FlashNorm removes the weights from RMSNorm and merges them with the next linear layer. And slim attention removes the entire V-cache from the context memory for MHA transformers.

Transformer tricks GitHub repo: here


Explainer videos

hey hey hey hey


Installation

Install the transformer tricks package:

pip install transformer-tricks

Alternatively, to run from latest repo:

git clone https://github.com/OpenMachine-ai/transformer-tricks.git
python3 -m venv .venv
source .venv/bin/activate
pip3 install --quiet -r requirements.txt

Flashify your own model

Convert any RMSNorm-based HuggingFace checkpoint to a -FlashNorm variant and publish it under your own account. The recipe below does the full round-trip in a dozen lines; it works for Llama, Mistral, Gemma, Qwen, SmolLM, and any other transformer that uses RMSNorm followed by a linear layer.

# pip install transformer-tricks huggingface-hub
import transformer_tricks as tt
from huggingface_hub import HfApi, login

login()                                            # paste your HF write token when prompted

SRC = 'meta-llama/Llama-3.2-1B'                    # source model on HF
OUT = 'YOUR_USERNAME/Llama-3.2-1B-FlashNorm'       # destination (under your account)
LOCAL = './Llama-3.2-1B_flashNorm'                 # local workdir

tt.flashify_repo(SRC, dir=LOCAL, strict=True)      # fold g into W*, remove norm tensors

api = HfApi()
api.create_repo(OUT, exist_ok=True)
api.upload_folder(repo_id=OUT, folder_path=LOCAL)
print(f'Published https://huggingface.co/{OUT}')

The strict=True flag folds the per-channel norm weights g into the following linear layer and removes the now-redundant norm tensors from the state dict entirely. The resulting checkpoint is mathematically equivalent to the source (Proposition 1 of the FlashNorm paper). Framework support status (HuggingFace Transformers, vLLM, llama.cpp) is tracked on the canonical FlashNorm checkpoint: open-machine/SmolLM2-135M-FlashNorm.

A runnable notebook version of this recipe is at notebooks/flashify_and_publish.ipynb.


Documentation

Follow the links below for documentation of the python code in this directory:


Notebooks

The papers are accompanied by the following Jupyter notebooks:

  • Slim attention: Colab
  • Flash normalization: Colab Colab Colab
  • Flashify your own model and publish to HuggingFace: Colab
  • Removing weights from skipless transformers: Colab

Newsletter

Please subscribe to our newsletter on substack to get the latest news about this project. We will never send you more than one email per month.

Substack


Contributing

We pay cash for high-impact contributions. Please check out CONTRIBUTING for how to get involved.


Sponsors

The Transformer Tricks project is currently sponsored by OpenMachine. We'd love to hear from you if you'd like to join us in supporting this project.


Please give us a ⭐ if you like this repo, and check out TinyFive


Star History Chart

Metadata

Release files for transformer-tricks 0.4.1

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

Source distribution (sdist)

Source distribution for transformer-tricks 0.4.1
File Size Uploaded
transformer_tricks-0.4.1.tar.gz 9.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for transformer-tricks 0.4.1
File Interpreter ABI Platform
transformer_tricks-0.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 18.9 kB

Release files / transformer_tricks-0.4.1.tar.gz

Download URL transformer_tricks-0.4.1.tar.gz
Size 9.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c3c442d2e33d45c28b9c0fada22ab2b97219dd1e251b38b6926f7c12af636fd9
BLAKE2b-256 checksum
How to use checksums
50363985686f5fa9894615528198714d93439cae9270880eb3524f55e3d6bfd4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.9

Release files / transformer_tricks-0.4.1-py3-none-any.whl

Download URL transformer_tricks-0.4.1-py3-none-any.whl
Size 9.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b8aa9c3b70a05e1f22b6cf1a9d2dfd247891fcb5b926ed4abf991a09fc617292
BLAKE2b-256 checksum
How to use checksums
8ae4f52b2d1877d266d8e2572e7857009b1681f17d848133ad65da7ff54b3a17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.9

Release history Release notifications | RSS feed

This release

0.4.1 This release

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.1.17

2 release files

0.1.13

2 release files

0.1.12

2 release files

0.1.11

1 release file

0.1.10

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

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