Small utilities to simplify trainining of PyTorch models.
Project description
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_stepandeval_stepvalidation 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
# Recommended installation (includes accelerate, trackio, and hydra)
pip install trainer-tools[core]
# Minimal 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file trainer_tools-0.4.3.tar.gz.
File metadata
- Download URL: trainer_tools-0.4.3.tar.gz
- Upload date:
- Size: 130.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
db216d916c6d8320ea9de0965aedd616f12230a064ca078ac86e2c72118796bb
|
|
| MD5 |
b838cd0840acd55dfbb0c6b4cb931224
|
|
| BLAKE2b-256 |
962fb6ef40e99b437cde3aba8aeda1a4fb41f099dcb1f89b9f179f4656ce51a6
|
Provenance
The following attestation bundles were made for trainer_tools-0.4.3.tar.gz:
Publisher:
publish.yml on ssslakter/trainer-tools
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
trainer_tools-0.4.3.tar.gz -
Subject digest:
db216d916c6d8320ea9de0965aedd616f12230a064ca078ac86e2c72118796bb - Sigstore transparency entry: 1389462088
- Sigstore integration time:
-
Permalink:
ssslakter/trainer-tools@159556a874a1e49d5af4c6c7c1423bc9e5253d99 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/ssslakter
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@159556a874a1e49d5af4c6c7c1423bc9e5253d99 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file trainer_tools-0.4.3-py3-none-any.whl.
File metadata
- Download URL: trainer_tools-0.4.3-py3-none-any.whl
- Upload date:
- Size: 28.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f1609f066abe3dd1d6d0bcf5094e19091c1fc9529f2a1a94840fef19ec9c34ec
|
|
| MD5 |
ae2a7f3f41c186e492bf4b29b9c02690
|
|
| BLAKE2b-256 |
56e3a83bff79b8c6c275bd39f8c399c6fdd1a2bbb77803d7fca10178dc9237a7
|
Provenance
The following attestation bundles were made for trainer_tools-0.4.3-py3-none-any.whl:
Publisher:
publish.yml on ssslakter/trainer-tools
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
trainer_tools-0.4.3-py3-none-any.whl -
Subject digest:
f1609f066abe3dd1d6d0bcf5094e19091c1fc9529f2a1a94840fef19ec9c34ec - Sigstore transparency entry: 1389462101
- Sigstore integration time:
-
Permalink:
ssslakter/trainer-tools@159556a874a1e49d5af4c6c7c1423bc9e5253d99 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/ssslakter
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@159556a874a1e49d5af4c6c7c1423bc9e5253d99 -
Trigger Event:
workflow_dispatch
-
Statement type: