Skip to main content

Add your description here

Project description

sarasa

A minimum LLM training framework built on pure PyTorch with simplicity and extensibility.

Installation

uv sync [--extra cpu|cu128|cu130] [--extra flash_attn]

or

uv add sarasa[cpu|cu128|cu130]

Features

  • Pure PyTorch implementation

  • Flexible configuration system with command-line overrides

  • Support from a single GPU to multiple GPUs (simple DDP and FSDP for now)

  • Selective activation checkpointing (SAC) for memory efficiency

  • Async distributed checkpoint saving

  • Checkpoint loading

  • FP8 training

  • Profiling

Usage

It's (almost) ready to use. First, set up tokenizer, e.g.,

mkdir tokenizer
cd tokenizer
uvx hf download --local-dir . --include "tokenizer*" "meta-llama/Llama-3.1-8B"

Then, the following command starts training of a GPT model on FineWeb-edu with a single or multiple GPUs.

uv run torchrun --nproc_per_node="gpu" main.py \
--config-file configs/example.py \
[--train.local-batch-size 8 ...] # override config options as needed

Extending with Custom Components

Extending Sarasa is as simple as defining your own configuration dataclasses with create methods for custom models, optimizers, data loaders, etc. Here's an example of using a custom optimizer:

from sarasa import Trainer, Config

class CustomOptimizer(torch.optim.Optimizer):
    ...

class CustomOptim:
    lr: float = ...

    def create(self,
               model: torch.nn.Module
    ) -> torch.optim.Optimizer:
        return CustomOptimizer(model.parameters(), lr=self.lr, ...)

class CustomOptim2:
    lr: float = ...

    def create(self,
               model: torch.nn.Module
    ) -> torch.optim.Optimizer:
        return CustomOptimizer(model.parameters(), lr=self.lr, ...)


if __name__ == "__main__":
    config = Config.from_cli(optim_type=CustomOptim | CustomOptim2)
    trainer = Trainer(config)
    trainer.train()

Thanks to tyro's type support, Sarasa can automatically recognize multiple custom optimizer types. From the command line, you can specify which custom optimizer to use:

python script.py optim:custom_optim --optim.lr 0.001 ...

(As tyro automatically converts config class names from CamelCase to snake_case, config class names are recommended not to include Config suffixes.)

Config File Example

It's very simple. IDE autocompletion will help you.

from sarasa import Config, Data, LRScheduler, Model, Train, LRScheduler
from custom_optim import CustomOptim

# only one Config instance should be defined in each config file
config = Config.create(
    model=Model(num_layers=12),
    train=Train(
        local_batch_size=16,
        global_batch_size=256,
        dtype="bfloat16",
    ),
    optim=CustomOptim(lr=0.001),
    lr_scheduler=LRScheduler(
        decay_type="linear",
        warmup_steps=1000,
        total_steps=100000,
    ),
    data=Data(tokenizer_path="./tokenizer"),
    seed=12,
)

Acknowledgements

This project is heavily inspired by and borrows code from torchtitan.

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

sarasa-0.0.4.tar.gz (28.8 kB view details)

Uploaded Source

Built Distribution

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

sarasa-0.0.4-py3-none-any.whl (31.8 kB view details)

Uploaded Python 3

File details

Details for the file sarasa-0.0.4.tar.gz.

File metadata

  • Download URL: sarasa-0.0.4.tar.gz
  • Upload date:
  • Size: 28.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.29 {"installer":{"name":"uv","version":"0.9.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for sarasa-0.0.4.tar.gz
Algorithm Hash digest
SHA256 3bf04768a4c16fe434365e383831556b1be7ac22e454514b56351e4a591f5cb3
MD5 09af59422a4e4838f46d877ab0e8c5d6
BLAKE2b-256 51c01a1996c3dfc63bc821ca564f0400a378b84e7442f757a64570d87a20c8fd

See more details on using hashes here.

File details

Details for the file sarasa-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: sarasa-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 31.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.29 {"installer":{"name":"uv","version":"0.9.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for sarasa-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d080ee3e8a7c8771f0b5c265057619ce62add3c4d14e7da86d34caf44bab7ff9
MD5 32dcbcc92aba6cd70b048079ffc90cbd
BLAKE2b-256 73768a2c6fc44477d122609d49cf50e9a2eb96de71d7f587dd7f352977d35039

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