x-transformers is a Python library offering a compact but full-featured transformer with optional experimental attention and training features from recent research papers.
Overview
x-transformers packages encoder, decoder, and encoder–decoder transformer stacks for use in PyTorch projects. You compose layers through a wrapper API and enable individual research-backed options as flags on the attention blocks.
Capabilities include Flash Attention integration, memory and register tokens, rotary and relative position schemes, sparse and grouped-query attention, GLU feedforward variants, and long-context patterns such as memory tokens between segments.
Examples show decoder-only GPT-style models, BERT-like encoders, cross-attention setups, and autoregressive wrappers with returned memory states for chunked generation.
Focus is on implementing published techniques as switchable options rather than shipping a standalone training or serving product.
Key features
- Encoder, decoder, and joint stacks
- Flash Attention toggle
- Memory and register tokens
- Rotary and relative position biases
- Sparse and hard attention modes
- Grouped-query and one KV head modes
- GLU and macaron feedforward layouts
- Long-context memory between segments
Use cases
- Prototyping custom transformer architectures
- Trying published attention mechanisms
- Building GPT-style language models
- Training BERT-like encoders
- Long-context autoregressive experiments