Language Modeling using Transformers (LMT)
An educational PyTorch library for understanding how modern transformer architectures work -- from attention mechanisms to full language models. Every component is written to be understood, with clear code, detailed docstrings, and mathematical notation that maps directly to the papers.
Features
Attention Mechanisms
| Component | Description | Paper |
|---|---|---|
| Multi-Head Attention | Standard scaled dot-product attention | Vaswani et al., 2017 |
| Grouped Query Attention | Shared KV heads for efficiency | Ainslie et al., 2023 |
| Sliding Window Attention | Local attention with fixed window | Beltagy et al., 2020 |
| Multi-Head Latent Attention | KV compression + decoupled RoPE | DeepSeek-AI, 2024 |
Feed-Forward Networks
| Component | Description |
|---|---|
| SwiGLU | Gated FFN with Swish activation (LLaMA, Mixtral) |
| Mixture of Experts | Top-k sparse routing with load balancing loss |
Other Components: RMSNorm, Rotary Position Embedding (RoPE)
Model Architectures
| Model | Key Components |
|---|---|
| GPT | Multi-head attention + GELU FFN + learned position embeddings |
| LLaMA | RMSNorm + RoPE + SwiGLU + GQA |
| Mixtral | LLaMA + MoE FFN + sliding window attention |
Installation
pip install pylmt
Or install from source for development:
git clone https://github.com/michaelellis003/LMT.git
cd LMT
pip install uv
uv sync
Quick Start
import torch
from lmt.models.config import ModelConfig
from lmt.models.llama import LLaMA
config = ModelConfig(
vocab_size=32000,
embed_dim=512,
num_heads=8,
num_kv_heads=4, # GQA: 4 KV heads shared across 8 query heads
num_layers=6,
context_length=1024,
dropout=0.0,
)
model = LLaMA(config)
x = torch.randint(0, config.vocab_size, (1, 128))
logits = model(x) # [1, 128, 32000]
Using Individual Layers
from lmt.layers.attention import GroupedQueryAttention
from lmt.layers.ffn import SwiGLU
from lmt.layers.normalization import RMSNorm
norm = RMSNorm(d_model=512)
attn = GroupedQueryAttention(config)
ffn = SwiGLU(d_model=512)
x = torch.randn(1, 64, 512)
x = x + attn(norm(x)) # Pre-norm attention
x = x + ffn(norm(x)) # Pre-norm FFN
Mixture of Experts
from lmt.models.mixtral import Mixtral
config = ModelConfig(
vocab_size=32000, embed_dim=512, num_heads=8,
num_kv_heads=4, num_layers=8,
context_length=2048, window_size=256, dropout=0.0,
)
model = Mixtral(config, num_experts=8, top_k=2)
logits = model(x)
aux_loss = model.aux_loss # load balancing loss for training
Project Structure
src/lmt/
layers/
attention/ # MHA, GQA, Sliding Window, MLA
ffn/ # SwiGLU, MoE (Router + Experts)
normalization/ # RMSNorm
positional/ # RoPE
models/
gpt/ # GPT (original decoder-only transformer)
llama/ # LLaMA (RMSNorm + RoPE + SwiGLU + GQA)
mixtral/ # Mixtral (LLaMA + MoE + sliding window)
training/ # Trainer, configs, dataloaders
tokenizer/ # BPE, naive tokenizers
Development
uv run pytest tests/ -v # Run tests (171 passing)
uv run ruff check src/ tests/ # Lint
uv run ruff format src/ tests/ # Format
uv run pyright src/ # Type check
uv run mkdocs serve # Local docs server
License
Apache License 2.0. See LICENSE for details.
Metadata
Release files for pylmt 0.7.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pylmt-0.7.0.tar.gz | 468.6 kB | Without provenance | Details for pylmt-0.7.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pylmt-0.7.0-py3-none-any.whl | Python 3 | none | any | Details for pylmt-0.7.0-py3-none-any.whl |
Total release size: 555.2 kB
Release files / pylmt-0.7.0.tar.gz
| Download URL | pylmt-0.7.0.tar.gz |
|---|---|
| Size | 468.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b3cf4a8eeb007a4cd3c100bef092c6a390afaf73b37cace04d9b79c65c4daf09
|
|
BLAKE2b-256 checksum How to use checksums |
c3af7a99fc91c439388e8b1f6e11652e7fa6bb948deefffd89255d99a7bbb3d1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / pylmt-0.7.0-py3-none-any.whl
| Download URL | pylmt-0.7.0-py3-none-any.whl |
|---|---|
| Size | 86.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4d38a5f6ae1a66f1aac02ebd61eace810d8b30cc73b67dcf7211cf1529bf62ff
|
|
BLAKE2b-256 checksum How to use checksums |
bb917e70d176d9498abc0c3a4af71b6442a4d877ecae09451d6d3a66b440b7b1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Security for pylmt 0.7.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.