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-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.0.tar.gz (13.9 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.0-py3-none-any.whl (24.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: llm_dot_py-0.1.0.tar.gz
  • Upload date:
  • Size: 13.9 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.0.tar.gz
Algorithm Hash digest
SHA256 d7d929d87f7846f7a5f16e6fed7d0c6f1bb736fb9dea85502d4a465c0196b1d0
MD5 c44bd236fefad2fe5652028eb8e14f42
BLAKE2b-256 0ad4bad5f198f95a117ee904c00b682fd7b5a383b00904b7758f5fc3c548b022

See more details on using hashes here.

File details

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

File metadata

  • Download URL: llm_dot_py-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 24.1 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ae58c30ee7a59a9bce875f951e164d2ce6fd4236e5835d9927a750d77acc91a0
MD5 cd0f5cf880ec6ebebab364d79a525b94
BLAKE2b-256 bf20ff4a882caba25b818fed8cf0f9f6f2b225875d4312e39ba5dd05a2d2eabd

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