Datasets¶
samudra.datasets
¶
InferenceDataset(source, prognostic_var_names, boundary_var_names, input_steps, output_steps, normalize_before_mask, masked_fill_value, long_rollout)
¶
Bases: Dataset
Dataset of overlapping windows used for autoregressive inference.
Each window contains input_steps historical states followed by
output_steps target states. Consecutive windows advance by
output_steps so that every model output becomes the newest part of the
next input history. For example::
input_steps=1, output_steps=1: [0 | 1], [1 | 2], [2 | 3]
input_steps=2, output_steps=2: [0, 1 | 2, 3], [2, 3 | 4, 5]
input_steps=2, output_steps=1: [0, 1 | 2], [1, 2 | 3], [2, 3 | 4]
The values before | are inputs and those after it are targets.
Source code in src/samudra/datasets.py
to(device)
¶
Move the dataset's context tensors to the specified device.
Call this before using the dataset for inference to ensure tensors are on the correct device (GPU).
Source code in src/samudra/datasets.py
HostBatch(dataset_id)
¶
Source code in src/samudra/datasets.py
append(input_, boundary, label)
¶
Add a prognostic input, boundary, and prognostic label as the last step.
ModelBatch(ctx)
¶
A single batch of training data.
A single batch contains multiple steps worth of RolloutStep entries, each
of which is a (prognostic_input, boundary_input, label) triple. The
prognostic and boundary tensors are carried separately because the
samudra-multi model encodes them separately (Samudra just concatenates them later).
Source code in src/samudra/datasets.py
append(prognostic_input, boundary_input, label)
¶
Add another RolloutStep as a new step.
__getitem__(step)
¶
record_stream(stream)
¶
Keep batch storage alive until work queued on stream completes.
Source code in src/samudra/datasets.py
TrainBatchLoader
¶
Bases: Protocol
Training-loop contract shared by Torch and native batch loaders.
BatchReadUse(source, request, mask, role)
dataclass
¶
One canonical source read and its model-facing preparation policy.
BatchReadPlan(dataset_id, steps)
dataclass
¶
Storage-independent reads for a homogeneous batch and full rollout.
TrainingWindows(input_source, label_source, prognostic_var_names, boundary_var_names, input_steps, output_steps, steps, normalize_before_mask, masked_fill_value, stride=1, windows_id=None)
¶
Training-window semantics shared by sample and native batch readers.
Source code in src/samudra/datasets.py
batch_compatibility_key
property
¶
Preserve the current homogeneous dataset-ID batching contract.
window_plan(indices)
¶
Plan all canonical reads for a batch and full rollout.
Source code in src/samudra/datasets.py
BatchPreparer(windows)
¶
Rank-local normalization, masking, shaping, and device-static caches.
Source code in src/samudra/datasets.py
normalize_and_mask_device_planes(use, data, device)
¶
Prepare unique (time, variable, lat, lon) planes on device.
Source code in src/samudra/datasets.py
TorchTrainDataset(windows, *, concurrent_compute_=False)
¶
Bases: Dataset[HostBatch]
Adapt training windows to PyTorch's per-sample Dataset interface.
Source code in src/samudra/datasets.py
to_model_batch(host_batch, device)
¶
Convert HostBatch to ModelBatch, moving tensors to the specified device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host_batch
|
HostBatch
|
CPU data from worker process |
required |
device
|
device
|
Target device (typically GPU) to move tensors to |
required |
Returns:
| Type | Description |
|---|---|
ModelBatch
|
ModelBatch with tensors on the target device |
Source code in src/samudra/datasets.py
TorchBatchLoader(host_loader, datasets, device)
¶
Wrapper around a torch DataLoader that handles GPU post-processing.
This class wraps a DataLoader[HostBatch] and converts the raw data to ModelBatch by applying GPU-based normalization and masking. This allows the data loading process to handle I/O while the main process handles GPU operations.
Since the data samples flow from one process to the other, we want to tie
them back to the dataset they came from which knows how to do that second
half once they're in the main process which has GPU access set up. To do that,
each data sample (which could come from a different dataset) has a dataset ID
-- datasets maps from those IDs to the original datasets.
Source code in src/samudra/datasets.py
__iter__()
¶
Iterate over the dataloader, converting HostBatch to ModelBatch.
Source code in src/samudra/datasets.py
close()
¶
__getitem__(index)
¶
Access a single item by index, converting HostBatch to ModelBatch.
Note: This bypasses the DataLoader's sampling/batching and directly accesses the underlying dataset for test purposes.
Source code in src/samudra/datasets.py
close_pytorch_dataloader(dataloader)
¶
Deterministically stop persistent workers created by a PyTorch loader.