Data API¶
The public data contracts are also exported from torch_batteries.
DatasetBundle accepts singular or named datasets in every phase.
BatchScheduleConfig controls the batch order shared by train and validation.
Resolved loader fields use DataLoaderCollection.
torch_batteries.data
¶
Event-driven dataset and DataLoader construction.
Public API¶
DataPack— base contract for charged data lifecycle methods.DataPackHandler— discovers and dispatches charged DataPack methods.DatasetBundleandDataLoaderBundle— resolved data containers.DataLoaderConfig— validated DataLoader construction options.DataContextandResolvedData— workflow context and resolution result.
DataPack
Source
¶
Base class for charged dataset and DataLoader configuration.
Subclasses define data lifecycle methods with :func:torch_batteries.charge.
The default checkpoint contract is stateless; subclasses may override
:meth:state_dict and :meth:load_state_dict when dataset construction relies
on persistent values such as split indices or streaming positions.
resolve(stage, *, device='cpu') Source
¶
Resolve datasets and DataLoaders without constructing a Battery.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stage
|
DataStage
|
Complete workflow stage to resolve: |
required |
device
|
str | device
|
PyTorch device used for device-aware loader defaults. Standalone
resolution defaults to CPU and also accepts |
'cpu'
|
Returns:
| Type | Description |
|---|---|
AbstractContextManager[ResolvedData]
|
A context manager yielding |
Note
Preparation runs once per standalone call. Keep preparation idempotent.
DataPackHandler
Source
¶
Bases: _ChargedHandlerBase
Discover and dispatch lifecycle methods charged on one DataPack.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_pack
|
DataPack
|
DataPack whose charged lifecycle methods are discovered. |
required |
has_handler(event) Source
¶
Return whether the DataPack handles an event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
Event
|
Data lifecycle event to inspect. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when at least one charged handler is registered. |
call(event, context) Source
¶
Call all ordered handlers for a side-effect data event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
Event
|
Side-effect event to dispatch. |
required |
context
|
DataContext
|
Data lifecycle context passed to handlers. |
required |
provide(event, context, *, default) Source
¶
Return a provider result or the supplied default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
Event
|
Provider event to dispatch. |
required |
context
|
DataContext
|
Data lifecycle context passed to the provider. |
required |
default
|
Any
|
Value returned when no provider is registered. |
required |
setup(context) Source
¶
Construct and validate datasets for one workflow invocation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
DataContext
|
Setup context passed to the dataset provider. |
required |
build_loader(context, dataset) Source
¶
Resolve a custom loader or materialize a DataLoaderConfig.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
DataContext
|
Loader configuration context. |
required |
dataset
|
DatasetType
|
Dataset for which a loader is required. |
required |
resolve(stage, *, device='cpu', battery=None, dataset_name=None) Source
¶
Resolve one DataPack stage and guarantee workflow teardown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stage
|
DataStage
|
|
required |
device
|
str | device
|
Device used for loader configuration. |
'cpu'
|
battery
|
Battery | None
|
Optional owning Battery included in event contexts. |
None
|
dataset_name
|
str | None
|
Optional named test or prediction dataset selection. |
None
|
Yields:
| Type | Description |
|---|---|
Generator[ResolvedData]
|
Resolved datasets and loaders for the requested stage. |
BatchScheduleConfig
Source
dataclass
¶
Select the batch order shared by named train and validation datasets.
interleave samples a loader in proportion to its remaining batches.
seed makes its order reproducible for each absolute epoch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
BatchScheduleMode
|
|
'round_robin'
|
seed
|
int
|
Seed used for deterministic interleaving at each absolute epoch. |
0
|
DataContext
Source
¶
Bases: TypedDict
Context passed to methods charged for DataPack lifecycle events.
Every event receives data_pack, stage, and device. Battery-managed
workflows additionally receive battery. stage identifies the workflow
as "fit", "test", or "predict". A configured DataPack seed adds
seed and a fresh generator initialized with that seed. Loader configuration
additionally receives phase, datasets, dataset, and dataset_name.
Teardown receives datasets only when setup succeeded.
DataLoaderBundle
Source
dataclass
¶
DataLoaders resolved for one DataPack stage.
Each phase retains whether its datasets were configured as a bare value or a named mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train
|
DataLoaderCollection | None
|
Training loader or mapping of named training loaders. |
None
|
validation
|
DataLoaderCollection | None
|
Validation loader or mapping of named validation loaders. |
None
|
test
|
DataLoaderCollection | None
|
Test loader or mapping of named test loaders. |
None
|
predict
|
DataLoaderCollection | None
|
Prediction loader or mapping of named prediction loaders. |
None
|
DataLoaderConfig
Source
dataclass
¶
Validated high-level configuration used to construct a DataLoader.
shuffle=None selects the phase default and pin_memory="auto" lets the
runtime select pinning from the Battery device. Setting batch_sampler requires
batch_size=None and conflicts with shuffle, sampler, and drop-last options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int | None
|
Samples per batch, or |
1
|
shuffle
|
bool | None
|
Whether to shuffle map-style data; |
None
|
sampler
|
Sampler[Any] | Iterable[Any] | None
|
Optional sampler or iterable of dataset indices. |
None
|
batch_sampler
|
Sampler[list[int]] | Iterable[list[int]] | None
|
Optional sampler or iterable yielding batches of indices. |
None
|
num_workers
|
int
|
Number of DataLoader worker processes; zero runs in the caller. |
0
|
collate_fn
|
Callable[[list[Any]], Any] | None
|
Optional function that combines samples into a batch. |
None
|
pin_memory
|
bool | Literal['auto']
|
Whether to pin host memory; |
'auto'
|
drop_last
|
bool
|
Whether to omit an incomplete final batch. |
False
|
timeout
|
float
|
Seconds to wait for a worker batch before timing out. |
0
|
worker_init_fn
|
Callable[[int], None] | None
|
Optional initializer called for each worker process. |
None
|
generator
|
Generator | None
|
Optional PyTorch generator overriding the DataPack generator. |
None
|
prefetch_factor
|
int | None
|
Batches prefetched per worker when workers are enabled. |
None
|
persistent_workers
|
bool
|
Keep worker processes alive between loader iterations. |
False
|
DatasetBundle
Source
dataclass
¶
Datasets made available by a charged SETUP_DATA provider.
Each phase accepts one PyTorch dataset or a non-empty mapping of dataset names to PyTorch datasets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train
|
DatasetCollection | None
|
Training dataset or named training datasets. |
None
|
validation
|
DatasetCollection | None
|
Validation dataset or named validation datasets. |
None
|
test
|
DatasetCollection | None
|
Test dataset or named test datasets. |
None
|
predict
|
DatasetCollection | None
|
Prediction dataset or named prediction datasets. |
None
|
batch_schedule
|
BatchScheduleConfig
|
Batch order shared by named training and validation datasets. |
BatchScheduleConfig()
|
ResolvedData
Source
dataclass
¶
Datasets and DataLoaders materialized for one DataPack stage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stage
|
DataStage
|
Workflow stage resolved by the DataPack. |
required |
device
|
device
|
Device used when configuring loaders. |
required |
datasets
|
DatasetBundle
|
Datasets returned by the DataPack's setup handler. |
required |
loaders
|
DataLoaderBundle
|
DataLoaders built for the selected stage. |
required |