drytorch.lib.aggregators

Module containing generic accumulator-based aggregators.

Classes

AbstractAccumulator()

Stateful aggregation container.

AbstractAggregator(**kwargs)

Aggregate named values using accumulator objects.

Averager(**kwargs)

Aggregator computing mean over floats.

MeanAccumulator(total, count)

Accumulator computing arithmetic mean for floats.

TorchAverager(**kwargs)

Aggregator computing mean over tensors with distributed support.

TorchMeanAccumulator(total, count)

Accumulator computing arithmetic mean for tensors.

class AbstractAccumulator[source]

Bases: Generic[_T, _R], ABC

Stateful aggregation container.

abstractmethod classmethod from_value(value: _T) Self[source]

Create accumulator from raw value.

Parameters:

value (_T)

Return type:

Self

abstractmethod merge(other: Self) None[source]

Merge another accumulator into this one.

Parameters:

other (Self)

Return type:

None

abstractmethod reduce() _R[source]

Return reduced value.

Return type:

_R

abstractmethod sync() None[source]

Synchronize state across distributed processes.

Return type:

None

class AbstractAggregator(**kwargs: _T)[source]

Bases: Generic[_T, _R]

Aggregate named values using accumulator objects.

Initialize.

Parameters:

kwargs (_T) – named values to aggregate.

__add__(other: Self | Mapping[str, _T]) Self[source]

Return new aggregator containing merged data.

Parameters:

other (Self | Mapping[str, _T])

Return type:

Self

__bool__() bool[source]

Return True if any values are stored.

Return type:

bool

__iadd__(other: Self | Mapping[str, _T]) Self[source]

Merge another aggregator or mapping into this one.

Parameters:

other (Self | Mapping[str, _T])

Return type:

Self

__repr__() str[source]

Return representation of the aggregator.

Return type:

str

clear() None[source]

Remove all accumulated data.

Return type:

None

keys() list[str][source]

Return stored metric names.

Return type:

list[str]

reduce() dict[str, _R][source]

Return reduced values for all metrics.

Return type:

dict[str, _R]

all_reduce() dict[str, _R][source]

Synchronize accumulators across processes and reduce.

Return type:

dict[str, _R]

class MeanAccumulator(total: float, count: int)[source]

Bases: AbstractAccumulator[float, float]

Accumulator computing arithmetic mean for floats.

Parameters:
total

sum of all values.

Type:

float

count

number of values.

Type:

int

classmethod from_value(value: float) MeanAccumulator[source]

Create accumulator from raw value.

Parameters:

value (float)

Return type:

MeanAccumulator

merge(other: Self) None[source]

Merge another accumulator into this one.

Parameters:

other (Self)

Return type:

None

reduce() float[source]

Return reduced value.

Return type:

float

sync() None[source]

Synchronize state across distributed processes.

Return type:

None

class TorchMeanAccumulator(total: Tensor, count: int)[source]

Bases: AbstractAccumulator[Tensor, Tensor]

Accumulator computing arithmetic mean for tensors.

Parameters:
total

sum of all values.

Type:

torch.Tensor

count

number of values.

Type:

int

classmethod from_value(value: Tensor) TorchMeanAccumulator[source]

Create accumulator from raw value.

Parameters:

value (Tensor)

Return type:

TorchMeanAccumulator

merge(other: Self) None[source]

Merge another accumulator into this one.

Parameters:

other (Self)

Return type:

None

reduce() Tensor[source]

Return reduced value.

Return type:

Tensor

sync() None[source]

Synchronize state across distributed processes.

Return type:

None

class Averager(**kwargs: _T)[source]

Bases: AbstractAggregator[float, float]

Aggregator computing mean over floats.

Initialize.

Parameters:

kwargs (_T) – named values to aggregate.

accumulator_cls

alias of MeanAccumulator

class TorchAverager(**kwargs: _T)[source]

Bases: AbstractAggregator[Tensor, Tensor]

Aggregator computing mean over tensors with distributed support.

Initialize.

Parameters:

kwargs (_T) – named values to aggregate.

accumulator_cls

alias of TorchMeanAccumulator