Skip to content

batch_func

Import path: earth2studio.models.batch.batch_func

View source on GitHub

Documentation

Batch utility decorator which can be added to prognostic and diagnostic models to help enable support for automatic batching of data. This class contains a decorator function which should be added to calls where this functionality is desired.

CoordinateSystem inputs remain allocation-free DataArrays paired with separate tensors. Leading dimensions are flattened into batch and restored on each output, including auxiliary coordinates. Dictionary coordinates remain supported for models whose input signatures are dictionaries.

DataArray methods take one DataArray followed by optional pass-through arguments and return or yield a DataArray. The input signature must start with batch; all remaining signature dimensions are fixed trailing dimensions. Leading input dimensions are packed with .e2s.batch() and restored with .e2s.unbatch(). This preserves leading labels and auxiliary coordinates even if the model drops attributes. Output batch size must remain unchanged. Coordinates spanning both packed and fixed dimensions are rejected by the accessor.

Note

A model attributes input_coords and output_coords must have "batch" as the coordinate system of the first dimensions. I.e. first key entry needs to be "batch".

Note

Decorated model methods declare explicit, named parameters. The decorator's variadic forwarding is an internal implementation detail and preserves the wrapped method's signature. For legacy tensor methods, positional arguments are explicit (x, CoordSystem) pairs and the result is a tuple[torch.Tensor, CoordSystem]. Keyword arguments pass through unchanged.

Example
class Model():

    input_coords = OrderedDict([("batch", np.empty(0)), ...])
    output_coords = OrderedDict([("batch", np.empty(0)), ...])

    @batch_func()
    def __call__(
        self,
        x: torch.Tensor,
        coords: CoordSystem,
    ) -> tuple[torch.Tensor, CoordSystem]:
        ```pycon
        ...
        ```