Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clu metrics + __future__.annotations #295

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions clu/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def evaluate(model, p_variables, test_ds):
"""
from __future__ import annotations
from collections.abc import Mapping, Sequence
import inspect
from typing import Any, TypeVar, Protocol

from absl import logging
Expand Down Expand Up @@ -534,8 +535,11 @@ def empty(cls: type[C]) -> C:
_reduction_counter=_ReductionCounter(jnp.array(1, dtype=jnp.int32)),
**{
metric_name: metric.empty()
for metric_name, metric in cls.__annotations__.items()
})
for metric_name, metric in inspect.get_annotations(
cls, eval_str=True
).items()
},
)

@classmethod
def _from_model_output(cls: type[C], **kwargs) -> C:
Expand All @@ -544,8 +548,11 @@ def _from_model_output(cls: type[C], **kwargs) -> C:
_reduction_counter=_ReductionCounter(jnp.array(1, dtype=jnp.int32)),
**{
metric_name: metric.from_model_output(**kwargs)
for metric_name, metric in cls.__annotations__.items()
})
for metric_name, metric in inspect.get_annotations(
cls, eval_str=True
).items()
},
)

@classmethod
def single_from_model_output(cls: type[C], **kwargs) -> C:
Expand Down
2 changes: 2 additions & 0 deletions clu/metrics_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

"""Tests for clu.metrics."""

from __future__ import annotations

import functools
from unittest import mock

Expand Down