Skip to main content

Small utilities to simplify trainining of PyTorch models.

Project description

PyPI version License: MIT GitHub Repo stars PyPI - Python Version

Trainer Tools

A lightweight, hook-based training loop for PyTorch. trainer-tools abstracts away the boilerplate of training loops while remaining fully customizable via a powerful flexible hook system.

Full Documentation

📘 Read the complete documentation here!

There you will find in-depth guides and API references on:

  • Customizing train_step and eval_step validation runs
  • Powerful robust metric tracking
  • Writing your own custom hooks
  • All built-in hooks (AMP, Checkpointing, EMA, Gradient Accumulation, HF Accelerate, etc.)

Features

  • Hook System: Customize every step of the training lifecycle (before/after batch, step, epoch, fit).
  • Built-in Integrations: Comes with hooks for wandb or trackio, Progress Bar, and Checkpointing.
  • Optimization: Easy Automatic Mixed Precision (AMP), Gradient Accumulation, and Gradient Clipping.
  • Metrics: robust metric tracking and logging to JSONL or external trackers.
  • Memory Profiling: Built-in tools to debug CUDA memory leaks.

Installation

pip install trainer-tools

# With optional integrations
pip install trainer-tools[wandb]      # Weights & Biases logging
pip install trainer-tools[trackio]    # Trackio logging
pip install trainer-tools[hydra]      # Hydra config management
pip install trainer-tools[all]        # All optional dependencies

Quick Start

Here is a minimal example of training a simple model:

import torch
import torch.nn as nn
from torch.utils.data import DataLoader, TensorDataset
from trainer_tools.trainer import Trainer
from trainer_tools.hooks import MetricsHook, Accuracy, Loss, ProgressBarHook

# 1. Prepare Data
x = torch.randn(100, 10)
y = torch.randint(0, 2, (100,))
ds = TensorDataset(x, y)
dl = DataLoader(ds, batch_size=32)

# 2. Define Model and Optimizer
model = nn.Sequential(nn.Linear(10, 2))
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)

# 3. Define the Training Step
def train_step(batch, trainer):
    inputs, labels = batch
    logits = trainer.model(inputs)
    loss = nn.CrossEntropyLoss()(logits, labels)
    
    # Must return a dictionary containing at least the "loss" key!
    return {
        "loss": loss,
        "logits": logits,
        "labels": labels
    }

# 4. Setup Hooks
metrics = MetricsHook(metrics=[Accuracy(pred_key="logits", target_key="labels"), Loss()])
pbar = ProgressBarHook()

# 5. Train
trainer = Trainer(
    model=model,
    train_dl=dl,
    valid_dl=dl,
    optim=optimizer,
    train_step=train_step,
    epochs=5,
    hooks=[metrics, pbar],
    device="cuda" if torch.cuda.is_available() else "cpu"
)

trainer.fit()

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

trainer_tools-0.4.1.tar.gz (138.2 kB view details)

Uploaded Source

Built Distribution

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

trainer_tools-0.4.1-py3-none-any.whl (27.9 kB view details)

Uploaded Python 3

File details

Details for the file trainer_tools-0.4.1.tar.gz.

File metadata

  • Download URL: trainer_tools-0.4.1.tar.gz
  • Upload date:
  • Size: 138.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for trainer_tools-0.4.1.tar.gz
Algorithm Hash digest
SHA256 ec418e234fb3cdaf5852e3e4127ff4c9a6411ec2969a90222ac78b5b485092e0
MD5 93d8a4a2d6c1d305644f9d01cefa3c28
BLAKE2b-256 5de5b3f61b779bf02fad1789bf125af647f942dc52a5cfe85ebd47e2b253a4fd

See more details on using hashes here.

Provenance

The following attestation bundles were made for trainer_tools-0.4.1.tar.gz:

Publisher: publish.yml on ssslakter/trainer-tools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file trainer_tools-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: trainer_tools-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 27.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for trainer_tools-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5bb5d2fa77d27eaf4d9c2c1d69988570f0d67c82271e25c73548f86b82f258bc
MD5 6327ad82b4b6d8dee5942b84bdcae2ff
BLAKE2b-256 dc9749fd772f691fa68512b7ddb149d97a7b1fc0a5640246c15540b395104b6d

See more details on using hashes here.

Provenance

The following attestation bundles were made for trainer_tools-0.4.1-py3-none-any.whl:

Publisher: publish.yml on ssslakter/trainer-tools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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