Custom Metrics
About
You can implement a custom metric by implementing the Metric interface. The d9d.metric.component package provides helpers for the distributed state.
Design Guidelines
Metric implementations usually follow these rules:
- GPU residency: Accumulate data in GPU tensors, so updates need no host sync.
- Additive states: Do not store averages such as "current accuracy". Store raw counts such as "total correct" and "total samples". Counts stay correct when summed with
all_reduce.
Helper Components
MetricAccumulator keeps a local and a synchronized copy of a metric state tensor. It supports the sum, max and min reduction operations (MetricReduceOp). There is no average operation, because averages are not additive.
Usage
This MaxMetric tracks the maximum value seen across all ranks with a MetricAccumulator.
API Reference
d9d.metric.component
Reusable components for building distributed metrics.
MetricAccumulator
Bases: Stateful
Accumulator of a distributed metric state.
It keeps two copies of the state: a local copy, updated on every step, and a synchronized copy, filled by an
all-reduce in sync().
value
property
The synchronized value if sync() was called after the last update, otherwise the local value.
__init__(initial_value, reduce_op=MetricReduceOp.sum)
Constructs the MetricAccumulator object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial_value
|
Tensor
|
The starting value, e.g. 0 for sum or -inf for max. It sets the device and dtype of the accumulator. |
required |
reduce_op
|
MetricReduceOp
|
The reduction operation for updates and synchronization. |
sum
|
load_state_dict(state_dict)
reset()
Resets the accumulator to its initial state.
state_dict()
sync()
Synchronizes the accumulator across the default process group.
Every rank must call it. The local state stays unchanged.
to(device)
update(value)
Accumulates a value into the local state with the configured reduction operation.
After an update, value returns the local state until the next sync().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Tensor | float | bool
|
The value to accumulate. The |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
MetricReduceOp
Bases: StrEnum
Reduction operation of a MetricAccumulator, used both for updates and for synchronization.
Attributes:
| Name | Type | Description |
|---|---|---|
sum |
Adds values. |
|
max |
Keeps the maximum value. |
|
min |
Keeps the minimum value. |