Skip to main content

Language Modeling using Transformers (LMT)

CI Python PyTorch License Code style: ruff

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.

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)

Source distribution for pylmt 0.7.0
File Size Uploaded
pylmt-0.7.0.tar.gz 468.6 kB Details

Built distribution (wheel)

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

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

Release history Release notifications | RSS feed

This release

0.7.0 This release

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.10

2 release files

0.2.9

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