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.5.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.5-py3-none-any.whl (31.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sarasa-0.0.5.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.5.tar.gz
Algorithm Hash digest
SHA256 7d0d8c0296b634a9a29cdb6b41ddd852721733a8ce1e420eb273219fe07344c2
MD5 9dbed24941d8cd332578847bddcadd1b
BLAKE2b-256 b917118c278a449872d8231a343c505b3a94d9b8bf0b1884dd409b1e26e80e40

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sarasa-0.0.5-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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 bd45258430e082ea849b095b8ef77348b83afa073994e2136b75fc65b700e294
MD5 fadd8d468963d3b177debd05ff27fcf6
BLAKE2b-256 b2b9926bb9692965c082bd52d226c0e7d8b8ebfea6bf06827b7282ad5d30bdac

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