Skip to main content

Minimal data loader for Flax

Project description

loaderx

Minimal data loader for Flax

Rationale for Creating loaderx

While Flax supports various data loading backends—such as PyTorch, TensorFlow, Grain, and jax_dataloader.

  1. Installing heavy frameworks like PyTorch or TensorFlow solely for data loading is undesirable.
  2. Grain offers a clean API but suffers from suboptimal performance in practice.
  3. jax_dataloader leverages GPU memory by default, which may lead to inefficient memory usage in certain scenarios.

Design Goals of loaderx

loaderx is designed with simplicity and efficiency in mind. It follows a pragmatic approach—favoring low memory overhead and minimal dependencies. The implementation targets common use cases, with a particular focus on single-host training pipelines.

Current Limitations

At present, loaderx only supports single-host scenarios and does not yet address multi-host training setups.

How to integrate it with Flax.

The loaderx is mainly inspired by the design of Grain, so avoid using patterns like for epoch in num_epochs.

The following is a Flax code for train and valid.

def loss_fn(model: CNN, batch):
  logits = model(batch['data'])
  loss = optax.softmax_cross_entropy_with_integer_labels(logits=logits, labels=batch['label']).mean()
  return loss, logits

@nnx.jit
def train_step(model: CNN, optimizer: nnx.Optimizer, metrics: nnx.MultiMetric, batch):
  """Train for a single step."""
  grad_fn = nnx.value_and_grad(loss_fn, has_aux=True)
  (loss, logits), grads = grad_fn(model, batch)
  metrics.update(loss=loss, logits=logits, labels=batch['label'])  # In-place updates.
  optimizer.update(grads)  # In-place updates.

@nnx.jit
def eval_step(model: CNN, metrics: nnx.MultiMetric, batch):
  loss, logits = loss_fn(model, batch)
  metrics.update(loss=loss, logits=logits, labels=batch['label'])  # In-place updates.

@nnx.jit
def pred_step(model: CNN, batch):
  logits = model(batch['data'])
  return logits.argmax(axis=1)

train_loader = loader(npz_path='data/mnist.npz', num_epoch=10)
for step, batch in enumerate(train_loader):
    train_step(model, optimizer, metrics, batch)
    if step > 0 and step % 500 == 0:
        train_metrics = metrics.compute()
        print("Step:{}_Train Acc@1: {} loss: {} ".format(step,train_metrics['accuracy'],train_metrics['loss']))
        metrics.reset()  # Reset the metrics for the train set.

        # Compute the metrics on the test set after each training epoch.
        val_loader = loader(npz_path='data/mnist.npz', num_epoch=1)
        for val_batch in val_loader:
            eval_step(model, metrics, val_batch)
        val_metrics = metrics.compute()
        print("Step:{}_Val Acc@1: {} loss: {} ".format(step,val_metrics['accuracy'],val_metrics['loss']))
        metrics.reset()  # Reset the metrics for the val set.

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

loaderx-0.0.3.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

loaderx-0.0.3-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file loaderx-0.0.3.tar.gz.

File metadata

  • Download URL: loaderx-0.0.3.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for loaderx-0.0.3.tar.gz
Algorithm Hash digest
SHA256 c015ba274549e0853909b92c8533dfb06c9abddd680e0114ca8f58e7faad6ba9
MD5 e17c9d9dc6bdbbc99a1a3f7c0fb65bfd
BLAKE2b-256 1bdbe7a3008cf4fea9ffe540c568cc1a075d5e9b4d7b90cc0fc7b4bfb1b0197e

See more details on using hashes here.

File details

Details for the file loaderx-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: loaderx-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for loaderx-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 bfdf21b615d1d28d4cd6762849afa392286f20f9f01f47f33ca227328b404417
MD5 cac1cafe454cf56505587cd43b01a91b
BLAKE2b-256 4d0e4d2b4416f83057b518889870f4b0dd0c495f9b15a4b1481c20f6a6b3336f

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