Skip to main content

vibhu-llm

vibhu-llm is a PyTorch-based library providing a from-scratch implementation of a Large Language Model (LLM). The primary goal of this library is to offer a clear and understandable decoder-only transformer model that can be trained on custom datasets and used for text generation.

Installation

You can install vibhu-llm directly from PyPI:

pip install vibhu-llm

Usage

Here's a quick example of how to use vibhu-llm to train a model and generate text:

import torch
from llm.config import Config
from llm.data import get_train_and_val_dataloaders, get_tokenizer, DATA_FETCHERS
from llm.model import LLMModel
from llm.model_run import get_optimizer, train_model
from llm.utils import get_device
from llm.inference import generate_and_print_sample

# 1. Setup device and tokenizer
device = get_device()
print(f"Using device: {device}")
tokenizer = get_tokenizer()

# 2. Configure the model
config = Config(
    context_length=256,
    vocab_size=tokenizer.n_vocab,
    emb_dim=128,
    n_blocks=2,
    n_heads=8,
    dropout=0.1,
)
model = LLMModel(config).to(device)
print(f"Total model parameters: {sum(p.numel() for p in model.parameters()):,}")

# 3. Load data
# You can choose from available data fetchers
train_loader, val_loader = get_train_and_val_dataloaders(
    DATA_FETCHERS["harry_potter"],
    tokenizer,
    batch_size=32,
    max_length=config.context_length,
    stride=128,
    train_ratio=0.9,
)

# 4. Setup optimizer
optimizer = get_optimizer(model, lr=0.001)

# 5. Train the model
train_model(
    train_loader,
    val_loader,
    model,
    optimizer,
    num_epochs=10,
    config_dict=config.dict(),
    experiment_name="runs/my_experiment",
    eval_interval=100,
    save_interval=100,
)

# 6. Generate text
generate_and_print_sample(
    model,
    tokenizer,
    device,
    start_context="The magic of this world",
    context_size=config.context_length,
    max_new_tokens=50,
)

Configuration

The llm.config.Config class allows you to customize the model architecture. Here are the available parameters:

Parameter Type Description Default
vocab_size int Vocabulary size 50257
context_length int Context length for the model 1024
emb_dim int Embedding dimension 768
n_blocks int Number of transformer blocks 12
n_heads int Number of attention heads 12
dropout float Dropout rate 0.1

Contributing

If you're interested in improving the library, here's how you can get started:

  1. Clone the repository:

    git clone https://github.com/Vibhu-Agarwal/LLM
    cd LLM
    
  2. Install dependencies: It is recommended to use Poetry for managing dependencies.

    poetry install
    
  3. Project Structure:

File Description
src/llm/config.py Contains the configuration class for the model architecture.
src/llm/data.py Handles data loading, tokenization, and creating DataLoader instances for training and validation.
src/llm/model.py Defines the LLM architecture.
src/llm/model_run.py Contains the main training loop and optimizer setup.
src/llm/model_run_utils.py Provides utility functions for the training loop, such as loss calculation and TensorBoard logging.
src/llm/inference.py Includes functions for generating text with a trained model.
src/llm/utils.py Contains utility functions for saving/loading checkpoints and selecting the correct device for training.
src/main.py An example of how to use the library. This file is not part of the packaged library.

Metadata

Release files for vibhu-llm 0.1.10

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

Source distribution (sdist)

Source distribution for vibhu-llm 0.1.10
File Size Uploaded
vibhu_llm-0.1.10.tar.gz 937.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for vibhu-llm 0.1.10
File Interpreter ABI Platform
vibhu_llm-0.1.10-py3-none-any.whl Python 3 none any Details

Total release size: 1.9 MB

Release files / vibhu_llm-0.1.10.tar.gz

Download URL vibhu_llm-0.1.10.tar.gz
Size 937.9 kB
Tags Source
SHA-256 checksum
How to use checksums
26a603022a17a15f90e15845b73a8a39570c7655abae82003c25f48094d095ae
BLAKE2b-256 checksum
How to use checksums
dd2c9e0334f01f34cedd68a89792be312f2886206a207b91759ce44eec16beed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.12.8 Darwin/24.6.0

Release files / vibhu_llm-0.1.10-py3-none-any.whl

Download URL vibhu_llm-0.1.10-py3-none-any.whl
Size 949.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
efc9639480695f2487a38f41082ebe9ebce8ebc04c3c6e0041a4d0c2afc6ee56
BLAKE2b-256 checksum
How to use checksums
c072f4482ef500f65ab925f4d0adf25fb78a0c9a902864c9b6cfb6854ab0d5ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.12.8 Darwin/24.6.0

Release history Release notifications | RSS feed

This release

0.1.10 This release

2 release files

0.1.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