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 = DataLoader(dataset = Dataset(dataset_path, data, label),batch_size=256,num_epochs=10,transform=transform)
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 = DataLoader(dataset = Dataset(dataset_path, data, label),batch_size=256,num_epochs=1,transform=transform)
        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.5.tar.gz (5.3 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.5-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: loaderx-0.0.5.tar.gz
  • Upload date:
  • Size: 5.3 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.5.tar.gz
Algorithm Hash digest
SHA256 6973d1fa875683d5d87ecb41f027485e839244ec37d05f7bbb71f492c3fde947
MD5 ff3e68af4e41af6c771915f4cc1536d8
BLAKE2b-256 194d936cfaa71c800abad3eec2220087fcf969cdd2ae6d786b55ca6a97e2a70f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: loaderx-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 6.3 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 6e5e58a51373384353ca282c2e2cf58822469c4290294d78ae032cdc111c6985
MD5 9fe0524000c4e6a2e680a6307b75887f
BLAKE2b-256 ac5e544730f997fd367a539ee80d45afdada962c497426ce066a98a92b3332b6

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