A modular, block-by-block LLM building library
Project description
llm.py
Build LLMs block by block.
llm.py is a modular, educational, and practical library for building Large Language Models (LLMs) from scratch. It provides implementations of modern components like Rotary Positional Embeddings (RoPE), SwiGLU, RMSNorm, and various attention mechanisms.
Features
- Modular Design: Plug-and-play components (
Componentbased architecture). - Modern Components:
- Positional Embeddings: Rotary (RoPE), Alibi, Sinusoidal, Learned.
- Attention: Multi-Head, Multi-Query (MQA), Grouped-Query (GQA).
- Activations & Norms: SwiGLU, RMSNorm, LayerNorm.
- Configurable: Easy-to-use configuration system for different model sizes.
Installation
pip install llm-dot-py
Note: You may need to install PyTorch separately depending on your CUDA version.
Usage
Here is a simple example of how to build a model:
from llm_py import (
Model, small_config,
Embedding, RotaryPE, SelfAttention, FeedForward, LMHead
)
# Initialize configuration
cfg = small_config(vocab_size=10000)
# Build the model block by block
model = (
Model(cfg)
.add(Embedding())
.add(RotaryPE())
.repeat(SelfAttention, 4, dropout=0.1)
.add(FeedForward())
.add(LMHead(tie_weights=True))
)
# Validate and print summary
model.validate()
model.summary()
# Run a forward pass
import torch
x = torch.randint(0, cfg.vocab_size, (2, 32))
output = model(x)
print(f"Output shape: {output.shape}")
Architecture
The library revolves around the Model class, which acts as a container for sequential Components.
Component: Base class for all layers. Implementation of specific logic (e.g.,RotaryPE) resides here.Config: Dataclass holding hyperparameters (dimension, heads, layers, etc.).
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llm_dot_py-0.1.1.tar.gz.
File metadata
- Download URL: llm_dot_py-0.1.1.tar.gz
- Upload date:
- Size: 16.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
76cd80ee7442220be2cb98a419540713a41da1e3784d4ce1ea7ad609157bc6b0
|
|
| MD5 |
d35802a519130c9fbe30da130570b44a
|
|
| BLAKE2b-256 |
b2874e1b21363ac0935fd11b13a05e590c9c23bc4f441cabf81aba537a2eba3b
|
File details
Details for the file llm_dot_py-0.1.1-py3-none-any.whl.
File metadata
- Download URL: llm_dot_py-0.1.1-py3-none-any.whl
- Upload date:
- Size: 26.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89b8d12032a5523837d5b7af65697546285a2ed3e927f86769f804adb99ab593
|
|
| MD5 |
1f118c1f3fe29e8eef3f8097aa1cd365
|
|
| BLAKE2b-256 |
2bf54906c511b526db13e770b4460769001abf9d4d417ea477896b27cfea4237
|