Skip to main content

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llm_dot_py-0.1.2.tar.gz (18.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm_dot_py-0.1.2-py3-none-any.whl (28.4 kB view details)

Uploaded Python 3

File details

Details for the file llm_dot_py-0.1.2.tar.gz.

File metadata

  • Download URL: llm_dot_py-0.1.2.tar.gz
  • Upload date:
  • Size: 18.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for llm_dot_py-0.1.2.tar.gz
Algorithm Hash digest
SHA256 ad3a5650d9d8ffc5619de03a6ad94de97feaae525a8d763148ccded7322283dc
MD5 39fe5bb50ad80323cb59fc36e5cddd8a
BLAKE2b-256 5340dbdef2c3198304cf70b259e81cf5bfdbaed8c0871da6200ed898612506d8

See more details on using hashes here.

File details

Details for the file llm_dot_py-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: llm_dot_py-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 28.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for llm_dot_py-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fce42c1d75fba42c1f2bc0e28edcf7a5afbedb7ed0626bc1e464c79001a86c46
MD5 b9e487141ec4165865f8635b24e36259
BLAKE2b-256 d7c4fce1ae71c56ffbfc2b6a70f0f3c61f0bea16b8f3ecd9d4417bd8a6d94ef6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page