Skip to main content

trainutil

Simple, composable PyTorch training utilities.

  • Batch: declare a batch as a dataclass and collate samples into it.
  • EMA: exponential moving average you can swap into the model for evaluation.
  • Metrics: weighted running means reduced across DDP ranks in one call.

Installation

pip install trainutil

Usage

Batch

@dataclass
class Batch(BaseBatch):
    coords: torch.Tensor
    name: list[str]

a = Batch.from_sample(coords=torch.randn(5, 3), name="a")
b = Batch.from_sample(coords=torch.randn(3, 3), name="b")
batch = Batch.collate([a, b])

batch.coords.shape  # (2, 5, 3)
batch.name          # ['a', 'b']
batch[1]            # Batch of size one
batch.to("cuda")    # every tensor field moves

EMA

ema = EMA({n: p for n, p in model.named_parameters() if p.requires_grad}, decay=0.999)

optimizer.step()
ema.update(model.state_dict())

with ema.swap(model.state_dict()):  # model holds the EMA weights inside the block
    validate(model)

With DDP, pass the unwrapped module (model.module). ema.state_dict() uses the same keys as the model, so model.load_state_dict(ckpt["ema"], strict=False) loads the EMA weights for inference.

Metrics

metrics = MetricAccumulator()
metrics.add({"loss": loss, "lddt": lddt}, weight=batch.batch_size)

means = metrics.flush()  # weighted mean over all ranks; call on every rank
if rank == 0:
    wandb.log(means, step=step)

Release files for trainutil 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for trainutil 0.1.0
File Size Uploaded
trainutil-0.1.0.tar.gz 66.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for trainutil 0.1.0
File Interpreter ABI Platform
trainutil-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 75.3 kB

Release files / trainutil-0.1.0.tar.gz

Download URL trainutil-0.1.0.tar.gz
Size 66.9 kB
Tags Source
SHA-256 checksum
How to use checksums
830a4d970f09e5c1c6c1f394f242fbe3add20351c4fb7ef4dd80de3679b0ce43
BLAKE2b-256 checksum
How to use checksums
6c4510b3b56fbd0ae808e89f0099098c7f72198f58f118c989e1e1d0b8b9c2cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.20

Release files / trainutil-0.1.0-py3-none-any.whl

Download URL trainutil-0.1.0-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7f839d84268b196fe9de47323ed50fae5fc1b65cec18c32c5f48f37f47d4e3a8
BLAKE2b-256 checksum
How to use checksums
d1baacc986e66c572764fd82db2141949e792c41e5e50a8fb831cacdaad4bdd4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.20

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page