Skip to content

Base Metric class

Metric

Bases: SynalinksSaveable

Per-sample metric base; subclass it, or BatchMetric if it needs the batch.

During fit() / evaluate(), an update_state() that also declares an x argument receives the program's inputs (per sample, or the batch's list for a BatchMetric), e.g. to grade with a judge that needs the question.

Example:

program.compile(
    metrics=[
        synalinks.metrics.Metric(),
    ],
)

Parameters:

Name Type Description Default
name str

(Optional) string name of the metric instance.

None
in_mask list

(Optional) list of keys to keep to compute the metric.

None
out_mask list

(Optional) list of keys to remove to compute the metric.

None
in_mask_pattern str

(Optional) Regex pattern; fields whose names match are kept (combined with in_mask via OR).

None
out_mask_pattern str

(Optional) Regex pattern; fields whose names match are dropped (combined with out_mask via OR).

None

Attributes:

Name Type Description
direction str | None

Class-level optimization direction. "up" if higher values are better (accuracy, F1, reward), "down" if lower is better (cost, latency, token usage, errors). Defaults to "up"; operational (LM, embedding model, program) metrics default to "down". Consumers (EarlyStopping, ProgramCheckpoint, Keras-Tuner inference) use it to resolve mode="auto". Setting it to None forces an explicit mode= / direction=.

Source code in synalinks/src/metrics/metric.py
@synalinks_export(["synalinks.Metric", "synalinks.metrics.Metric"])
class Metric(SynalinksSaveable):
    """Per-sample metric base; subclass it, or BatchMetric if it needs the batch.

    During `fit()` / `evaluate()`, an `update_state()` that also declares an `x`
    argument receives the program's inputs (per sample, or the batch's list for
    a `BatchMetric`), e.g. to grade with a judge that needs the question.

    Example:

    ```python
    program.compile(
        metrics=[
            synalinks.metrics.Metric(),
        ],
    )
    ```

    Args:
        name (str): (Optional) string name of the metric instance.
        in_mask (list): (Optional) list of keys to keep to compute the metric.
        out_mask (list): (Optional) list of keys to remove to compute the metric.
        in_mask_pattern (str): (Optional) Regex pattern; fields whose names match
            are kept (combined with ``in_mask`` via OR).
        out_mask_pattern (str): (Optional) Regex pattern; fields whose names match
            are dropped (combined with ``out_mask`` via OR).

    Attributes:
        direction (str | None): Class-level optimization direction. ``"up"`` if
            higher values are better (accuracy, F1, reward), ``"down"`` if
            lower is better (cost, latency, token usage, errors). Defaults to
            ``"up"``; operational (LM, embedding model, program) metrics
            default to ``"down"``. Consumers (EarlyStopping, ProgramCheckpoint,
            Keras-Tuner inference) use it to resolve ``mode="auto"``. Setting
            it to ``None`` forces an explicit `mode=` / `direction=`.
    """

    direction = "up"

    def __init__(
        self,
        name=None,
        description=None,
        in_mask=None,
        out_mask=None,
        in_mask_pattern=None,
        out_mask_pattern=None,
    ):
        self.name = name or auto_name(self.__class__.__name__)
        self.description = docstring_summary(self) if description is None else description
        self._metrics = []
        self._variables = []
        self.in_mask = in_mask
        self.out_mask = out_mask
        self.in_mask_pattern = in_mask_pattern
        self.out_mask_pattern = out_mask_pattern
        self._tracker = Tracker(
            {
                "variables": (
                    lambda x: isinstance(x, backend.Variable),
                    self._variables,
                ),
                "metrics": (lambda x: isinstance(x, Metric), self._metrics),
            }
        )

    async def close(self):
        """Release what this metric holds. Idempotent.

        Nothing, for most metrics; a judge's program, for one wrapping a judge.
        """

    def reset_state(self):
        """Reset all of the metric state variables.

        This function is called between epochs/steps,
        when a metric is evaluated during training.
        """
        for v in self.variables:
            initializer = initializers.Empty(data_model=v._data_model)
            v.assign(initializer.get_json())

    async def update_state(self, *args, **kwargs):
        """Accumulate statistics for the metric."""
        raise NotImplementedError

    def result(self):
        """Compute the current metric value.

        Returns:
            (float | dict): A scalar, or a dictionary of scalars.
        """
        raise NotImplementedError

    def add_variable(self, initializer=None, data_model=None, name=None):
        if initializer is None:
            initializer = initializers.Empty(data_model=data_model)
        self._check_super_called()
        with backend.name_scope(self.name.replace("/", ">"), caller=self):
            initializer = initializer
            variable = backend.Variable(
                initializer=initializer,
                data_model=data_model,
                trainable=False,
                name=name,
            )
        # Prevent double-tracking
        self._tracker.add_to_store("variables", variable)
        return variable

    @property
    def variables(self):
        variables = list(self._variables)
        for metric in self._metrics:
            variables.extend(metric.variables)
        return variables

    async def __call__(self, *args, **kwargs):
        self._check_super_called()
        await self.update_state(*args, **kwargs)
        return self.result()

    def get_config(self):
        """Return the serializable config of the metric.

        Returns:
            (dict): The config dict.
        """
        return {
            "in_mask": self.in_mask,
            "out_mask": self.out_mask,
            "in_mask_pattern": self.in_mask_pattern,
            "out_mask_pattern": self.out_mask_pattern,
            "name": self.name,
            "description": self.description,
        }

    @classmethod
    def from_config(cls, config):
        """Returns a metric instance from config.

        Args:
            config (dict): The config dict.

        Returns:
            (Metric): The metric instance.
        """
        return cls(**config)

    def __setattr__(self, name, value):
        # Track Variables, Layers, Metrics
        if hasattr(self, "_tracker"):
            value = self._tracker.track(value)
        return super().__setattr__(name, value)

    def _check_super_called(self):
        if not hasattr(self, "_tracker"):
            raise RuntimeError(
                "You forgot to call `super().__init__()` "
                "in the `__init__()` method. Go add it!"
            )

    def __repr__(self):
        return f"<{self.__class__.__name__} name={self.name}>"

    def __str__(self):
        return self.__repr__()

close() async

Release what this metric holds. Idempotent.

Nothing, for most metrics; a judge's program, for one wrapping a judge.

Source code in synalinks/src/metrics/metric.py
async def close(self):
    """Release what this metric holds. Idempotent.

    Nothing, for most metrics; a judge's program, for one wrapping a judge.
    """

from_config(config) classmethod

Returns a metric instance from config.

Parameters:

Name Type Description Default
config dict

The config dict.

required

Returns:

Type Description
Metric

The metric instance.

Source code in synalinks/src/metrics/metric.py
@classmethod
def from_config(cls, config):
    """Returns a metric instance from config.

    Args:
        config (dict): The config dict.

    Returns:
        (Metric): The metric instance.
    """
    return cls(**config)

get_config()

Return the serializable config of the metric.

Returns:

Type Description
dict

The config dict.

Source code in synalinks/src/metrics/metric.py
def get_config(self):
    """Return the serializable config of the metric.

    Returns:
        (dict): The config dict.
    """
    return {
        "in_mask": self.in_mask,
        "out_mask": self.out_mask,
        "in_mask_pattern": self.in_mask_pattern,
        "out_mask_pattern": self.out_mask_pattern,
        "name": self.name,
        "description": self.description,
    }

reset_state()

Reset all of the metric state variables.

This function is called between epochs/steps, when a metric is evaluated during training.

Source code in synalinks/src/metrics/metric.py
def reset_state(self):
    """Reset all of the metric state variables.

    This function is called between epochs/steps,
    when a metric is evaluated during training.
    """
    for v in self.variables:
        initializer = initializers.Empty(data_model=v._data_model)
        v.assign(initializer.get_json())

result()

Compute the current metric value.

Returns:

Type Description
float | dict

A scalar, or a dictionary of scalars.

Source code in synalinks/src/metrics/metric.py
def result(self):
    """Compute the current metric value.

    Returns:
        (float | dict): A scalar, or a dictionary of scalars.
    """
    raise NotImplementedError

update_state(*args, **kwargs) async

Accumulate statistics for the metric.

Source code in synalinks/src/metrics/metric.py
async def update_state(self, *args, **kwargs):
    """Accumulate statistics for the metric."""
    raise NotImplementedError

accumulate(state, fields, **values)

Add per-field counts into a metric state, matching fields by name.

The state keeps the list of fields seen so far under "fields", and each count list in the same order, so samples with different structures (e.g. lists of different lengths) never mix the counts of different fields.

Parameters:

Name Type Description Default
state Variable

The metric state.

required
fields list

The name of each count, e.g. a leaf path or a label.

required
**values list

The new counts, one list per state key, aligned with fields.

{}
Source code in synalinks/src/metrics/metric.py
def accumulate(state, fields, **values):
    """Add per-field counts into a metric state, matching fields by name.

    The state keeps the list of fields seen so far under `"fields"`, and each
    count list in the same order, so samples with different structures (e.g.
    lists of different lengths) never mix the counts of different fields.

    Args:
        state (Variable): The metric state.
        fields (list): The name of each count, e.g. a leaf path or a label.
        **values (list): The new counts, one list per state key, aligned with
            `fields`.
    """
    known = list(state.get("fields") or [])
    known += [field for field in dict.fromkeys(fields) if field not in known]
    update = {"fields": known}
    for name, new in values.items():
        current = list(state.get(name) or [])
        current += [0.0] * (len(known) - len(current))
        for field, value in zip(fields, new):
            current[known.index(field)] += float(value)
        update[name] = current
    state.update(update)

align_leaves(y_true, y_pred, fill=None)

Pair the leaves of two data models by their path in the JSON.

Pairing by position would shift every field after a list whose length differs between the two. A failed prediction (y_pred is None) has no leaves.

Parameters:

Name Type Description Default
y_true JsonDataModel

The ground truth.

required
y_pred JsonDataModel

The prediction, or None.

required
fill any

The value of a leaf missing on one side.

None

Returns:

Type Description
tuple

The paths (e.g. "sources/1"), the y_true leaves and the y_pred leaves, aligned.

Source code in synalinks/src/metrics/metric.py
def align_leaves(y_true, y_pred, fill=None):
    """Pair the leaves of two data models by their path in the JSON.

    Pairing by position would shift every field after a list whose length
    differs between the two. A failed prediction (`y_pred is None`) has no
    leaves.

    Args:
        y_true (JsonDataModel): The ground truth.
        y_pred (JsonDataModel): The prediction, or `None`.
        fill (any): The value of a leaf missing on one side.

    Returns:
        (tuple): The paths (e.g. `"sources/1"`), the `y_true` leaves and the
            `y_pred` leaves, aligned.
    """

    def leaves(value, path=""):
        if isinstance(value, dict):
            items = value.items()
        elif isinstance(value, list):
            items = enumerate(value)
        else:
            return {path: value}
        result = {}
        for key, item in items:
            result.update(leaves(item, f"{path}/{key}" if path else str(key)))
        return result

    true = leaves(y_true.get_json())
    pred = {} if y_pred is None else leaves(y_pred.get_json())
    paths = list(true) + [path for path in pred if path not in true]
    return paths, [true.get(p, fill) for p in paths], [pred.get(p, fill) for p in paths]

apply_masks(y_true, y_pred, in_mask=None, in_mask_pattern=None, out_mask=None, out_mask_pattern=None)

Convert y_true/y_pred to JsonDataModel and apply field masks.

Works on both a single sample (a leaf JsonDataModel) and a list/tree of samples; tree.map_structure handles either uniformly.

Raises:

Type Description
ValueError

If the masks drop every field of a non-empty y_true (e.g. a typo in a field name), which would score every sample as a match.

Source code in synalinks/src/metrics/metric.py
def apply_masks(
    y_true,
    y_pred,
    in_mask=None,
    in_mask_pattern=None,
    out_mask=None,
    out_mask_pattern=None,
):
    """Convert ``y_true``/``y_pred`` to ``JsonDataModel`` and apply field masks.

    Works on both a single sample (a leaf ``JsonDataModel``) and a list/tree
    of samples; ``tree.map_structure`` handles either uniformly.

    Raises:
        ValueError: If the masks drop every field of a non-empty `y_true`
            (e.g. a typo in a field name), which would score every sample
            as a match.
    """
    masks = {
        "in_mask": in_mask,
        "in_mask_pattern": in_mask_pattern,
        "out_mask": out_mask,
        "out_mask_pattern": out_mask_pattern,
    }
    masked_true = mask_data_models(y_true, **masks)
    if any(masks.values()):
        unmasked = tree.flatten(mask_data_models(y_true))
        for before, after in zip(unmasked, tree.flatten(masked_true)):
            if before is not None and before.get_json() and not after.get_json():
                raise ValueError(
                    f"The masks {({k: v for k, v in masks.items() if v})} match "
                    f"none of the fields {list(before.get_json())}: check the "
                    "field names."
                )
    return masked_true, mask_data_models(y_pred, **masks)

mask_data_models(value, in_mask=None, in_mask_pattern=None, out_mask=None, out_mask_pattern=None)

Convert value to JsonDataModel and apply field masks.

Works on a single data model or a list/tree of them (None leaves are kept as is).

Source code in synalinks/src/metrics/metric.py
def mask_data_models(
    value,
    in_mask=None,
    in_mask_pattern=None,
    out_mask=None,
    out_mask_pattern=None,
):
    """Convert `value` to `JsonDataModel` and apply field masks.

    Works on a single data model or a list/tree of them (`None` leaves are
    kept as is).
    """

    def apply(x):
        x = ops.convert_to_json_data_model(x)
        if x is None:
            return x
        if in_mask or in_mask_pattern:
            x = x.in_mask(mask=in_mask, pattern=in_mask_pattern)
        if out_mask or out_mask_pattern:
            x = x.out_mask(mask=out_mask, pattern=out_mask_pattern)
        return x

    return tree.map_structure(apply, value)