Grain¶
tinax.grain provides deterministic process-aware training and evaluation pipelines, random-access sources, and explicit multiprocessing worker lifetime.
Grain multiprocessing requires caller-parsed Abseil flags. Tinax validates this requirement but never mutates global flags.
InMemorySource
¶
Snapshot an outer iterable while preserving references to its possibly mutable records.
A deterministic random-access source supporting len() and integer indexing.
The outer iterable is copied into a tuple at construction, but individual records
are held by reference and are not deep copied.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
records
|
Iterable[T]
|
Iterable of records to snapshot into the source. |
required |
source_id
|
str
|
Stable, non-empty identifier used in source representations. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
source_id
property
¶
Return the caller-assigned stable identity used in source representations.
training_batches
¶
training_batches(
dataset: MapDataset[T],
*,
shard_options: ShardOptions,
seed: int,
shuffle: bool,
num_epochs: int | None,
batch_size: int,
batch_fn: BatchTransform[T, BatchT],
) -> MapDataset[BatchT]
Build deterministic full local batches with equal process step counts in every training epoch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
MapDataset[T]
|
Finite |
required |
shard_options
|
ShardOptions
|
Per-process shard selection. |
required |
seed
|
int
|
Integer seed for deterministic ordering and shuffling. |
required |
shuffle
|
bool
|
Whether to shuffle records within each epoch. |
required |
num_epochs
|
int | None
|
Number of epochs to repeat, or |
required |
batch_size
|
int
|
Local (per-process) batch size. Must be positive. |
required |
batch_fn
|
BatchTransform[T, BatchT]
|
Callable collating a sequence of records into one batch. |
required |
Returns:
| Type | Description |
|---|---|
MapDataset[BatchT]
|
A |
MapDataset[BatchT]
|
repeated for |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
evaluation_batches
¶
evaluation_batches(
dataset: MapDataset[T],
*,
shard_options: ShardOptions,
batch_size: int,
batch_fn: BatchTransform[T, BatchT],
) -> MapDataset[BatchT]
Build one ordered finite pass that keeps remainder records and may have unequal process step counts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
MapDataset[T]
|
Finite |
required |
shard_options
|
ShardOptions
|
Per-process shard selection. |
required |
batch_size
|
int
|
Local (per-process) batch size. Must be positive. |
required |
batch_fn
|
BatchTransform[T, BatchT]
|
Callable collating a sequence of records into one batch. |
required |
Returns:
| Type | Description |
|---|---|
MapDataset[BatchT]
|
A |
MapDataset[BatchT]
|
and without dropping the final partial batch. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
open_multiprocessing_iterator
¶
open_multiprocessing_iterator(
dataset: IterDataset[T],
*,
num_workers: int,
per_worker_buffer_size: int = 1,
worker_init_fn: Callable[[int, int], None]
| None = None,
sequential_slice: bool = False,
) -> Iterator[DatasetIterator[T]]
Open a Grain multiprocessing iterator and close every worker when the context exits.
A context manager yielding a dataset iterator backed by worker processes; on exit the iterator and all workers are closed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
IterDataset[T]
|
|
required |
num_workers
|
int
|
Number of worker processes. |
required |
per_worker_buffer_size
|
int
|
Prefetch buffer size per worker. Must be positive. |
1
|
worker_init_fn
|
Callable[[int, int], None] | None
|
Optional callable run in each worker as
|
None
|
sequential_slice
|
bool
|
Whether workers slice the dataset sequentially. |
False
|
Yields:
| Type | Description |
|---|---|
DatasetIterator[T]
|
A |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
RuntimeError
|
If |