Skip to main content

No project description provided

Project description

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.

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

vibhu_llm-0.1.10.tar.gz (937.9 kB view details)

Uploaded Source

Built Distribution

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

vibhu_llm-0.1.10-py3-none-any.whl (949.2 kB view details)

Uploaded Python 3

File details

Details for the file vibhu_llm-0.1.10.tar.gz.

File metadata

  • Download URL: vibhu_llm-0.1.10.tar.gz
  • Upload date:
  • Size: 937.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.12.8 Darwin/24.6.0

File hashes

Hashes for vibhu_llm-0.1.10.tar.gz
Algorithm Hash digest
SHA256 26a603022a17a15f90e15845b73a8a39570c7655abae82003c25f48094d095ae
MD5 3e13857deec85ddd1b1f955e3fe6d4bb
BLAKE2b-256 dd2c9e0334f01f34cedd68a89792be312f2886206a207b91759ce44eec16beed

See more details on using hashes here.

File details

Details for the file vibhu_llm-0.1.10-py3-none-any.whl.

File metadata

  • Download URL: vibhu_llm-0.1.10-py3-none-any.whl
  • Upload date:
  • Size: 949.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.12.8 Darwin/24.6.0

File hashes

Hashes for vibhu_llm-0.1.10-py3-none-any.whl
Algorithm Hash digest
SHA256 efc9639480695f2487a38f41082ebe9ebce8ebc04c3c6e0041a4d0c2afc6ee56
MD5 0eeeedd230c498438187733a0337a2f7
BLAKE2b-256 c072f4482ef500f65ab925f4d0adf25fb78a0c9a902864c9b6cfb6854ab0d5ea

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