Skip to main content

A Flax trainer

Project description

XTRAIN: a tiny library for training Flax models.

Design goals:

  • Help avoiding boiler-plate code
  • Minimal functionality and dependency
  • Agnostic to hardware configuration (e.g. GPU->TPU)

General workflow

Step 1: define your model

class MyFlaxModule(nn.Module):
  @nn.compact
  def __call__(self, x):
    ...

Step 2: define loss function

def my_loss_func(batch, prediction):
    x, y_true = batch
    loss = ....
    return loss

Step 3: create an iterator that supplies training data

my_data = zip(sequence_of_inputs, sequence_of_labels)

Step 4: train

# create and initialize a Trainer object
trainer = xtrain.Trainer(
  model = MyFlaxModule(),
  losses = my_loss_func,
  optimizer = optax.adam(1e-4),
)

train_iter = trainer.train(my_data) # returns a iterable object

# iterate the train_iter trains the model
for epoch in range(3):
  for model_out in train_iter:
    pass
  print(train_iter.loss_logs)
  train_iter.reset_loss_logs()

Training data format

  • tensowflow Dataset
  • torch dataloader
  • generator function
  • other python iterable that produce numpy data

Checkpointing

train_iter is orbax compatible.

import orbax.checkpoint as ocp
ocp.StandardCheckpointer().save(cp_path, args=ocp.args.StandardSave(train_iter))

Freeze submodule

train_iter.freeze("submodule/Dense_0/kernel")

Simple batch parallelism on multiple device

# Add a new batch dim to you dataset
ds = ds.batch(8)
# create trainer with the Distributed strategy
trainer_iter = xtrain.Trainer(model, losses, optimizer, strategy=xtrain.Distributed).train(ds)

API documentation

https://jiyuuchc.github.io/xtrain/

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

xtrain-0.3.9.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

xtrain-0.3.9-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file xtrain-0.3.9.tar.gz.

File metadata

  • Download URL: xtrain-0.3.9.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.15 Linux/6.8.0-1014-azure

File hashes

Hashes for xtrain-0.3.9.tar.gz
Algorithm Hash digest
SHA256 a042c0c097b4f215a3f47dbf81bdb8bc080e39f343dab12815b5af80daa97569
MD5 ee150eb7cacdb648b11739944e0ba471
BLAKE2b-256 246043742f8b233d4e2af3b1788b0fb64f366de70da6da2dfe6dd718a45747b7

See more details on using hashes here.

File details

Details for the file xtrain-0.3.9-py3-none-any.whl.

File metadata

  • Download URL: xtrain-0.3.9-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.15 Linux/6.8.0-1014-azure

File hashes

Hashes for xtrain-0.3.9-py3-none-any.whl
Algorithm Hash digest
SHA256 a4df26df2c6f5d3f5489011f8f16ebf655a3d21bc181fbb5f985cd2527955495
MD5 f2b8a4b694f30cf36ba3d9bdb331aef7
BLAKE2b-256 9b16308b2098fc668326b79347324a321b93c644c2673d6b673f4bb723fd346d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page