Skip to main content

A library to train PyTorch models as a stream of events

Project description

FitStream

A tiny library to make PyTorch experiment easy for small models and in-memory datasets.

Getting started

Using uv

uv add fitstream

Using pip:

pip install fitstream

Training a model:

from torch.optim import Adam

from fitstream import epoch_stream, take # epoch_stream is the main entry point

X, y = get_data()
model = get_model()
loss = get_loss()
optimizer = Adam(model.parameters())

# an infinite stream of training epochs (limit it with `take` or `early_stop`)
events = epoch_stream((X, y), model, optimizer, loss, batch_size=32, shuffle=True)
for event in take(10)(events):
    print(f"step={event['step']}, loss={event['train_loss']}")
# epoch=1, loss=...
# epoch=2, loss=...
# ...

Basics

The core idea of the library is "training loop as a stream of events". The epoch_stream is just an iterable over dictionaries comprising of the epoch, the model, and the training loss. Everything we do is transforming or enriching these events. FitStream provides a small pipe(...) helper to compose transformations left-to-right.

Augmentation

The augment function turns an "augmenter" (a function that looks at an event and returns extra keys) into a stream transform stage. We typically compose stages with pipe(...).

Here is an example - we add the norm of the model parameters to each event:

from torch import nn, linalg
from fitstream import epoch_stream, augment, pipe

def model_param_norm(ev: dict) -> dict:
    model_params = nn.utils.parameters_to_vector(ev['model'].parameters())
    return {'model_param_norm': linalg.norm(model_params)}


events = pipe(
    epoch_stream(...),
    augment(model_param_norm),
)
for event in events:
    print(f"step={event['step']}", 
          f"model_param_norm={event['model_param_norm']}"
    )

We also have some built-in augmentation functions. Here is an example of adding validation loss to each event:

from torch import nn
from fitstream import epoch_stream, augment, pipe, validation_loss

validation_set = get_validation_set()
events = pipe(
    epoch_stream(...),
    augment(validation_loss(validation_set, nn.CrossEntropyLoss())),
)
for event in events:
    print(f"step={event['step']}, val_loss={event['val_loss']}")

We can, of course, augment the stream more than once:

events = pipe(
    epoch_stream(...),
    augment(validation_loss(...)),
    augment(model_param_norm),
)
for event in events:
    print(f"step={event['step']}", 
          f"val_loss={event['val_loss']}",
          f"model_param_norm={event['model_param_norm']}"
    )

Selecting events

Since the training loop is a standard Python iterable, you can use any Python selection logic. FitStream includes a small helper, take(...), to limit the number of epochs:

from fitstream import epoch_stream, take

for event in take(100)(epoch_stream(...)):
    print(event)
# {'step': 1, ....}
# {'step': 2, ...}
# ...
# { 'step': 100, ...}

fitstream has some of its own selection primitives, such as early stopping:

from fitstream import augment, early_stop, epoch_stream, pipe, take, validation_loss

events = pipe(
    epoch_stream(...),
    augment(validation_loss(...)),
    take(500),  # safety cap
    early_stop(key="val_loss", patience=10),
)
for event in events:
    print(event)

Side effects

Sometimes you want to log metrics (or write to an external system) without changing the stream. Use tap(fn):

from fitstream import epoch_stream, pipe, tap, take

events = pipe(
    epoch_stream(...),
    tap(lambda ev: print(ev["step"], ev["train_loss"])),
    take(10),
)
list(events)

Sinks

Iterating over events and doing something yourself can be tedious, so we have some utilities to help you process the event stream.

It is typically useful to collect all events into a list, but exclude the model and keep just the metrics. We have the collect sink for that:

from fitstream import collect, epoch_stream, take

# collect 100 epochs to a list
history = collect(take(100)(epoch_stream(...)))

We can also store them to a jsonl file:

from fitstream import collect_jsonl, epoch_stream, take

# collect 100 epochs to json
collect_jsonl(take(100)(epoch_stream(...)), 'runs/my_experiment.jsonl')

Documentation

Full documentation is available at https://fitstream.readthedocs.io/.

Development

  • After cloning this repo, run make setup to create a virtual environment and install all dependencies.
  • Building is done via uv build.
  • Running tests is done via make test
  • Building documentation via make doc
  • Linting via make lint

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

fitstream-0.1.1.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

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

fitstream-0.1.1-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file fitstream-0.1.1.tar.gz.

File metadata

  • Download URL: fitstream-0.1.1.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.6

File hashes

Hashes for fitstream-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ca7764bb2652fe2de0e17a5c73a689f6107d600dc684882ba8e111956c17f98a
MD5 20fe5959306783b25d472d243627d8c0
BLAKE2b-256 43d1eecac5517ec75c59df1e7882d3ebd5f7240c210ecc44f220576b3ee3bff2

See more details on using hashes here.

File details

Details for the file fitstream-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: fitstream-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.6

File hashes

Hashes for fitstream-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eb2860fda3d4517f893cb3d539cf496416bb456ad20eb21b5d537ebcbb5cb042
MD5 17af56efaaf5d897d01c72ef3e1599a9
BLAKE2b-256 be851f515fc78f64fbc5de7d4674c00a9be0b724cd7f82bd97efffd23c6f2fd7

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