grain
Grain: A library for loading and transforming data for ML training.
What it is and what it does
Grain is a data loading and transformation library designed for machine learning workflows. It provides a declarative API to define data processing pipelines—shuffling, mapping, batching, and other transformations—in a composable, deterministic way. While built with JAX models in mind, it does not require JAX and can work with other frameworks.
The library depends on absl-py, array-record, cloudpickle, etils, numpy, portpicker, and protobuf. It is actively maintained by Google, used in projects like MaxText and Gemma, and supports modern Python versions (3.11–3.14) across Linux, macOS, and Windows platforms.
Use it for:
- Define reproducible data pipelines for training JAX models with shuffling, mapping, and batching in a single declarative chain.
- Prepare and transform large datasets for machine learning experiments with deterministic, composable operations.
- Load and preprocess data for multi-framework ML workflows without being tied to a specific training framework.
- Build data augmentation and transformation steps that integrate seamlessly into ML training loops.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Grain is a Python library for reading, transforming, and batching data for training and evaluating machine learning models, with support for declarative data processing pipelines.
Yes. Grain is production-stable (Development Status 5), actively maintained, permissively licensed, and has no known vulnerabilities. Install friction is moderate but manageable. It is a good fit if you need declarative, deterministic data pipelines for ML training, especially with JAX, but also works with other frameworks.
Install
grain on PyPI
pip
pip install grainuv
uv add grainpoetry
poetry add grainInstalling grain
Before you install
Medium install friction due to compiled wheels for multiple Python versions (3.11–3.14) and platforms. Active maintenance with recent release (58 days ago) and ongoing repository activity.
License in practice
Apache License 2.0 is permissive; you may use, modify, and distribute grain freely in commercial and private projects, provided you include license notices and document changes.
Quickstart
pip install grain
import grain
dataset = (
grain.MapDataset.source([0, 1, 2, 3, 4, 5])
.shuffle(seed=42)
.map(lambda x: x + 1)
.batch(batch_size=2)
)
for batch in dataset:
print(batch)
Requires Python 3.11 or later; grain does not use GPU/TPU directly and runs transformations on CPU by default.
Verify before relying
- Whether grain's determinism guarantees hold across all transformation types and edge cases.
- Performance characteristics and scalability limits for very large datasets or complex pipelines.
- Compatibility with frameworks other than JAX beyond basic iteration.
Package facts
| License | Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for… (full text in the JSON record) (permissive) |
| Python support | supports the current Python release (>=3.11) |
| Install friction | medium — platform-specific wheel |
| Runtime dependencies | 7 — absl-py, array-record, cloudpickle, etils, numpy, portpicker, protobuf |
| Maintenance | actively maintained — 58 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 2,127,414/month — #3,270 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: grain-0.2.18-cp311-cp311-macosx_11_0_arm64.whl; grain-0.2.18-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl; grain-0.2.18-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl; grain-0.2.18-cp311-cp311-win_amd64.whl; grain-0.2.18-cp312-cp312-macosx_11_0_arm64.whl; grain-0.2.18-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl; grain-0.2.18-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl; grain-0.2.18-cp312-cp312-win_amd64.whl; grain-0.2.18-cp313-cp313-macosx_11_0_arm64.whl; grain-0.2.18-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl; grain-0.2.18-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl; grain-0.2.18-cp313-cp313-win_amd64.whl; grain-0.2.18-cp314-cp314-macosx_11_0_arm64.whl; grain-0.2.18-cp314-cp314-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl; grain-0.2.18-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl; grain-0.2.18-cp314-cp314-win_amd64.whl
Tags
More Artificial Intelligence packages
LiteLLM provides a unified Python interface to…
permissive · top 100 on PyPI
huggingface-hubClient library and CLI tool for downloading,…
permissive · top 100 on PyPI
langchainLangChain provides a framework for building…
permissive · top 1,000 on PyPI
hf-xethf-xet provides chunk-based deduplication and…
permissive · top 1,000 on PyPI
tokenizersTokenizers converts raw text into token…
permissive · top 1,000 on PyPI
transformersTransformers provides a unified framework for…
permissive · top 1,000 on PyPI
jaxJAX is a Python library for automatic…
permissive · top 1,000 on PyPI
jaxlibjaxlib is the compiled XLA backend that enables…
permissive · top 5,000 on PyPI
flaxFlax is a neural network library for JAX that…
permissive · top 5,000 on PyPI
litdataLitData optimizes and streams large datasets…
permissive · top 15,000 on PyPI
feastFeast is an open-source feature store that…
permissive · top 15,000 on PyPI
webdatasetWebDataset reads and streams large-scale…
permissive · top 5,000 on PyPI
datasetsLoads and preprocesses datasets from the…
permissive · top 1,000 on PyPI
spark-nlpSpark NLP provides distributed natural language…
permissive · top 5,000 on PyPI
mmengineMMEngine is a foundational PyTorch training…
permissive · top 5,000 on PyPI
cluCLU provides utilities and abstractions for…
permissive · top 15,000 on PyPI