Skip to main content

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 (Component based 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

Metadata

Release files for llm-dot-py 0.1.2

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

Source distribution (sdist)

Source distribution for llm-dot-py 0.1.2
File Size Uploaded
llm_dot_py-0.1.2.tar.gz 18.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for llm-dot-py 0.1.2
File Interpreter ABI Platform
llm_dot_py-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 46.4 kB

Release files / llm_dot_py-0.1.2.tar.gz

Download URL llm_dot_py-0.1.2.tar.gz
Size 18.0 kB
Tags Source
SHA-256 checksum
How to use checksums
ad3a5650d9d8ffc5619de03a6ad94de97feaae525a8d763148ccded7322283dc
BLAKE2b-256 checksum
How to use checksums
5340dbdef2c3198304cf70b259e81cf5bfdbaed8c0871da6200ed898612506d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release files / llm_dot_py-0.1.2-py3-none-any.whl

Download URL llm_dot_py-0.1.2-py3-none-any.whl
Size 28.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fce42c1d75fba42c1f2bc0e28edcf7a5afbedb7ed0626bc1e464c79001a86c46
BLAKE2b-256 checksum
How to use checksums
d7c4fce1ae71c56ffbfc2b6a70f0f3c61f0bea16b8f3ecd9d4417bd8a6d94ef6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release history Release notifications | RSS feed

This release

0.1.2 This release

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