reax 0.7.0.dev3__tar.gz → 0.7.0.dev4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/PKG-INFO +2 -2
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/pyproject.toml +1 -1
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/__init__.py +1 -1
- reax-0.7.0.dev4/src/reax/_vendor/__init__.py +1 -0
- reax-0.7.0.dev4/src/reax/_vendor/simple_pytree.py +248 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/metrics/_classification.py +13 -14
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/metrics/_evaluators.py +3 -2
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/metrics/_metric.py +3 -2
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/metrics/aggregation.py +16 -19
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/metrics/collections.py +2 -3
- reax-0.7.0.dev4/src/reax/metrics/utils.py +160 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/optimizers.py +8 -5
- reax-0.7.0.dev4/src/reax/pytree.py +87 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/results.py +2 -3
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/strategies/_jax.py +3 -3
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/strategies/_single_device.py +1 -1
- reax-0.7.0.dev4/src/reax/utils/__init__.py +4 -0
- reax-0.7.0.dev4/src/reax/utils/masking.py +97 -0
- reax-0.7.0.dev3/src/reax/metrics/utils.py +0 -252
- reax-0.7.0.dev3/src/reax/utils/__init__.py +0 -4
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/LICENSE.txt +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/README.rst +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/_engine.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/_module_hooks.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/data/__init__.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/data/_datasource_manager.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/data/_datasources.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/data/_loaders.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/data/_types.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/data/collate.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/data/dataloaders.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/data/datamodules.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/data/datasets.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/data/fetchers.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/data/kfold.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/data/samplers.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/data/utils.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/demos/__init__.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/demos/boring_classes.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/demos/mnist.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/exceptions.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/hooks.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/keys.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/lightning/LICENSE +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/lightning/__init__.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/lightning/rank_zero.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/listeners/__init__.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/listeners/checkpointer.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/listeners/early_stopping.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/listeners/model_checkpoint.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/listeners/progress/__init__.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/listeners/progress/progress_bar.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/listeners/progress/rich_progress.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/listeners/progress/tqdm_progress.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/listeners/utils.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/loggers/__init__.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/loggers/_utils.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/loggers/csv_logs.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/loggers/logger.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/loggers/mlflow.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/loggers/pandas.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/loggers/tensorboard.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/metrics/__init__.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/metrics/_globals.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/metrics/_registry.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/metrics/jm/__init__.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/metrics/jm/types.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/metrics/jm/utils.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/metrics/regression.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/modules.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/plugins.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/profilers/__init__.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/profilers/jax_profiler.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/profilers/profiler.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/random.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/saving.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/stages/__init__.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/stages/_loops.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/stages/_timer.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/stages/common.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/stages/fit.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/stages/predict.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/stages/stages.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/stages/stats.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/stages/test.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/stages/train.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/stages/validation.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/strategies/__init__.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/strategies/_factory.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/strategies/_parallel.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/strategies/_strategies.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/strategies/_utils.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/testing.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/training/__init__.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/training/_checkpoint_connector.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/training/_checkpointing.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/training/_hooks_map.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/training/_logger_connector.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/training/_utils.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/training/trainer.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/types.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/utils/arrays.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/utils/containers.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/utils/events.py +0 -0
- {reax-0.7.0.dev3 → reax-0.7.0.dev4}/src/reax/utils/grads.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: reax
|
|
3
|
-
Version: 0.7.0.
|
|
3
|
+
Version: 0.7.0.dev4
|
|
4
4
|
Summary: REAX: A simple training framework for JAX-based projects
|
|
5
5
|
Keywords: machine learning,jax,research
|
|
6
6
|
Author-email: Martin Uhrin <martin.uhrin.10@ucl.ac.uk>
|
|
@@ -17,7 +17,6 @@ Classifier: Programming Language :: Python :: 3.14
|
|
|
17
17
|
License-File: LICENSE.txt
|
|
18
18
|
Requires-Dist: beartype
|
|
19
19
|
Requires-Dist: einops
|
|
20
|
-
Requires-Dist: equinox
|
|
21
20
|
Requires-Dist: flax
|
|
22
21
|
Requires-Dist: fsspec
|
|
23
22
|
Requires-Dist: jax
|
|
@@ -45,6 +44,7 @@ Requires-Dist: pylint ; extra == "dev"
|
|
|
45
44
|
Requires-Dist: ruff ; extra == "dev"
|
|
46
45
|
Requires-Dist: ty==0.0.84 ; extra == "dev"
|
|
47
46
|
Requires-Dist: docutils ; extra == "docs"
|
|
47
|
+
Requires-Dist: equinox ; extra == "docs"
|
|
48
48
|
Requires-Dist: jupyter ; extra == "docs"
|
|
49
49
|
Requires-Dist: myst-nb ; extra == "docs"
|
|
50
50
|
Requires-Dist: pandoc ; extra == "docs"
|
|
@@ -25,7 +25,6 @@ requires-python = '>=3.10'
|
|
|
25
25
|
dependencies = [
|
|
26
26
|
'beartype',
|
|
27
27
|
"einops",
|
|
28
|
-
"equinox",
|
|
29
28
|
"flax",
|
|
30
29
|
'fsspec',
|
|
31
30
|
'jax',
|
|
@@ -47,6 +46,7 @@ Source = 'https://github.com/camml-lab/reax'
|
|
|
47
46
|
[project.optional-dependencies]
|
|
48
47
|
docs = [
|
|
49
48
|
'docutils',
|
|
49
|
+
'equinox', # Used by the example model in the introduction notebook
|
|
50
50
|
'jupyter', # For running doc examples
|
|
51
51
|
'myst-nb', # Jupyter notebooks in docs
|
|
52
52
|
'pandoc',
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Third-party code vendored into reax."""
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
# Vendored from simple-pytree 0.2.3 (https://github.com/cgarciae/simple-pytree,
|
|
2
|
+
# commit 182178837e4e8b75021c6e622ea249564703dae3), which is no longer developed.
|
|
3
|
+
#
|
|
4
|
+
# MIT License
|
|
5
|
+
#
|
|
6
|
+
# Copyright (c) 2023 Cristian Garcia
|
|
7
|
+
#
|
|
8
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
# in the Software without restriction, including without limitation the rights
|
|
11
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
# furnished to do so, subject to the following conditions:
|
|
14
|
+
#
|
|
15
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
16
|
+
# copies or substantial portions of the Software.
|
|
17
|
+
#
|
|
18
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
+
# SOFTWARE.
|
|
25
|
+
#
|
|
26
|
+
# Changes from upstream:
|
|
27
|
+
# * Dropped the fallback for jax < 0.4.7.
|
|
28
|
+
# * A dataclass field counts as static if its metadata has ``pytree_node=False`` *or*
|
|
29
|
+
# ``static=True`` (the latter is what ``equinox.field(static=True)`` sets).
|
|
30
|
+
# * Static data is stored in the treedef as a sorted tuple of ``(name, value)`` pairs
|
|
31
|
+
# instead of an (unhashable) ``MappingProxyType``. Static fields that were never set on
|
|
32
|
+
# the instance are skipped rather than raising a ``KeyError``.
|
|
33
|
+
# * ``replace()`` always copies the instance and swaps the values, it never calls
|
|
34
|
+
# ``__init__`` (``dataclasses.replace`` does, which breaks custom ``__init__`` methods).
|
|
35
|
+
# pylint: skip-file
|
|
36
|
+
from abc import ABCMeta
|
|
37
|
+
import copy as copy_
|
|
38
|
+
import dataclasses
|
|
39
|
+
from functools import partial
|
|
40
|
+
import importlib.util
|
|
41
|
+
import typing as tp
|
|
42
|
+
|
|
43
|
+
import jax
|
|
44
|
+
|
|
45
|
+
__all__ = ("Pytree", "PytreeMeta", "is_static_field")
|
|
46
|
+
|
|
47
|
+
P = tp.TypeVar("P", bound="Pytree")
|
|
48
|
+
|
|
49
|
+
_NODE_FIELDS = "_pytree__node_fields"
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def is_static_field(field: dataclasses.Field) -> bool:
|
|
53
|
+
"""Whether the passed dataclass field is marked as static (i.e. not a pytree node)."""
|
|
54
|
+
return not field.metadata.get("pytree_node", True) or bool(field.metadata.get("static", False))
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class PytreeMeta(ABCMeta):
|
|
58
|
+
def __call__(cls: type[P], *args: tp.Any, **kwargs: tp.Any) -> P:
|
|
59
|
+
obj: P = cls.__new__(cls, *args, **kwargs)
|
|
60
|
+
obj.__dict__["_pytree__initializing"] = True
|
|
61
|
+
try:
|
|
62
|
+
obj.__init__(*args, **kwargs)
|
|
63
|
+
finally:
|
|
64
|
+
del obj.__dict__["_pytree__initializing"]
|
|
65
|
+
|
|
66
|
+
vars_dict = vars(obj)
|
|
67
|
+
vars_dict[_NODE_FIELDS] = tuple(
|
|
68
|
+
sorted(field for field in vars_dict if field not in cls._pytree__static_fields)
|
|
69
|
+
)
|
|
70
|
+
return obj
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class Pytree(metaclass=PytreeMeta):
|
|
74
|
+
_pytree__initializing: bool
|
|
75
|
+
_pytree__class_is_mutable: bool
|
|
76
|
+
_pytree__static_fields: tuple[str, ...]
|
|
77
|
+
_pytree__node_fields: tuple[str, ...]
|
|
78
|
+
_pytree__setter_descriptors: frozenset[str]
|
|
79
|
+
|
|
80
|
+
def __init_subclass__(cls, mutable: bool = False, **kwargs):
|
|
81
|
+
super().__init_subclass__(**kwargs)
|
|
82
|
+
|
|
83
|
+
# gather class info
|
|
84
|
+
class_vars = vars(cls)
|
|
85
|
+
setter_descriptors = set()
|
|
86
|
+
static_fields = _inherited_static_fields(cls)
|
|
87
|
+
|
|
88
|
+
# add special static fields
|
|
89
|
+
static_fields.add(_NODE_FIELDS)
|
|
90
|
+
|
|
91
|
+
for field, value in class_vars.items():
|
|
92
|
+
if isinstance(value, dataclasses.Field) and is_static_field(value):
|
|
93
|
+
static_fields.add(field)
|
|
94
|
+
|
|
95
|
+
# add setter descriptors
|
|
96
|
+
if hasattr(value, "__set__"):
|
|
97
|
+
setter_descriptors.add(field)
|
|
98
|
+
|
|
99
|
+
static_fields = tuple(sorted(static_fields))
|
|
100
|
+
|
|
101
|
+
# init class variables
|
|
102
|
+
cls._pytree__initializing = False
|
|
103
|
+
cls._pytree__class_is_mutable = mutable
|
|
104
|
+
cls._pytree__static_fields = static_fields
|
|
105
|
+
cls._pytree__setter_descriptors = frozenset(setter_descriptors)
|
|
106
|
+
|
|
107
|
+
jax.tree_util.register_pytree_with_keys(
|
|
108
|
+
cls,
|
|
109
|
+
partial(cls._pytree__flatten, with_key_paths=True),
|
|
110
|
+
cls._pytree__unflatten, # ty: ignore[invalid-argument-type]
|
|
111
|
+
flatten_func=partial(cls._pytree__flatten, with_key_paths=False),
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
# flax serialization support
|
|
115
|
+
if importlib.util.find_spec("flax") is not None:
|
|
116
|
+
from flax import serialization
|
|
117
|
+
|
|
118
|
+
serialization.register_serialization_state(
|
|
119
|
+
cls,
|
|
120
|
+
partial(cls._to_flax_state_dict, cls._pytree__static_fields),
|
|
121
|
+
partial(cls._from_flax_state_dict, cls._pytree__static_fields),
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
@classmethod
|
|
125
|
+
def _pytree__flatten(
|
|
126
|
+
cls,
|
|
127
|
+
pytree: "Pytree",
|
|
128
|
+
*,
|
|
129
|
+
with_key_paths: bool,
|
|
130
|
+
) -> tuple[tuple[tp.Any, ...], tuple[tuple[str, tp.Any], ...]]:
|
|
131
|
+
all_vars = vars(pytree).copy()
|
|
132
|
+
static = tuple(
|
|
133
|
+
(name, all_vars.pop(name)) for name in pytree._pytree__static_fields if name in all_vars
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
if with_key_paths:
|
|
137
|
+
node_values = tuple(
|
|
138
|
+
(jax.tree_util.GetAttrKey(field), all_vars.pop(field))
|
|
139
|
+
for field in pytree._pytree__node_fields
|
|
140
|
+
)
|
|
141
|
+
else:
|
|
142
|
+
node_values = tuple(all_vars.pop(field) for field in pytree._pytree__node_fields)
|
|
143
|
+
|
|
144
|
+
if all_vars:
|
|
145
|
+
raise ValueError(
|
|
146
|
+
f"Unexpected fields in {cls.__name__}: {', '.join(all_vars.keys())}."
|
|
147
|
+
"You cannot add new fields to a Pytree after it has been initialized."
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
return node_values, static
|
|
151
|
+
|
|
152
|
+
@classmethod
|
|
153
|
+
def _pytree__unflatten(
|
|
154
|
+
cls: type[P],
|
|
155
|
+
static_fields: tuple[tuple[str, tp.Any], ...],
|
|
156
|
+
node_values: tuple[tp.Any, ...],
|
|
157
|
+
) -> P:
|
|
158
|
+
pytree = object.__new__(cls)
|
|
159
|
+
static = dict(static_fields)
|
|
160
|
+
pytree.__dict__.update(zip(static[_NODE_FIELDS], node_values, strict=True))
|
|
161
|
+
pytree.__dict__.update(static)
|
|
162
|
+
return pytree
|
|
163
|
+
|
|
164
|
+
@classmethod
|
|
165
|
+
def _to_flax_state_dict(
|
|
166
|
+
cls, static_field_names: tuple[str, ...], pytree: "Pytree"
|
|
167
|
+
) -> dict[str, tp.Any]:
|
|
168
|
+
from flax import serialization
|
|
169
|
+
|
|
170
|
+
state_dict = {
|
|
171
|
+
name: serialization.to_state_dict(getattr(pytree, name))
|
|
172
|
+
for name in pytree.__dict__
|
|
173
|
+
if name not in static_field_names
|
|
174
|
+
}
|
|
175
|
+
return state_dict
|
|
176
|
+
|
|
177
|
+
@classmethod
|
|
178
|
+
def _from_flax_state_dict(
|
|
179
|
+
cls,
|
|
180
|
+
static_field_names: tuple[str, ...],
|
|
181
|
+
pytree: P,
|
|
182
|
+
state: dict[str, tp.Any],
|
|
183
|
+
) -> P:
|
|
184
|
+
"""Restore the state of a data class."""
|
|
185
|
+
from flax import serialization
|
|
186
|
+
|
|
187
|
+
state = state.copy() # copy the state so we can pop the restored fields.
|
|
188
|
+
updates = {}
|
|
189
|
+
for name in pytree.__dict__:
|
|
190
|
+
if name in static_field_names:
|
|
191
|
+
continue
|
|
192
|
+
if name not in state:
|
|
193
|
+
raise ValueError(
|
|
194
|
+
f"Missing field {name} in state dict while restoring"
|
|
195
|
+
f" an instance of {type(pytree).__name__},"
|
|
196
|
+
f" at path {serialization.current_path()}"
|
|
197
|
+
)
|
|
198
|
+
value = getattr(pytree, name)
|
|
199
|
+
value_state = state.pop(name)
|
|
200
|
+
updates[name] = serialization.from_state_dict(value, value_state, name=name)
|
|
201
|
+
if state:
|
|
202
|
+
names = ",".join(state.keys())
|
|
203
|
+
raise ValueError(
|
|
204
|
+
f'Unknown field(s) "{names}" in state dict while'
|
|
205
|
+
f" restoring an instance of {type(pytree).__name__}"
|
|
206
|
+
f" at path {serialization.current_path()}"
|
|
207
|
+
)
|
|
208
|
+
return pytree.replace(**updates)
|
|
209
|
+
|
|
210
|
+
def replace(self: P, **kwargs: tp.Any) -> P:
|
|
211
|
+
"""Return a copy of this object with the passed fields replaced by new values.
|
|
212
|
+
|
|
213
|
+
This never calls ``__init__``, the stored values are swapped directly.
|
|
214
|
+
"""
|
|
215
|
+
unknown_keys = set(kwargs) - set(vars(self))
|
|
216
|
+
if unknown_keys:
|
|
217
|
+
raise ValueError(
|
|
218
|
+
f"Trying to replace unknown fields {unknown_keys} for '{type(self).__name__}'"
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
pytree = copy_.copy(self)
|
|
222
|
+
pytree.__dict__.update(kwargs)
|
|
223
|
+
return pytree
|
|
224
|
+
|
|
225
|
+
if not tp.TYPE_CHECKING:
|
|
226
|
+
|
|
227
|
+
def __setattr__(self: P, field: str, value: tp.Any):
|
|
228
|
+
if self._pytree__initializing or field in self._pytree__setter_descriptors:
|
|
229
|
+
pass
|
|
230
|
+
elif not hasattr(self, field) and not self._pytree__initializing:
|
|
231
|
+
raise AttributeError(f"Cannot add new fields to {type(self)} after initialization")
|
|
232
|
+
elif not self._pytree__class_is_mutable:
|
|
233
|
+
raise AttributeError(f"{type(self)} is immutable, trying to update field {field}")
|
|
234
|
+
|
|
235
|
+
object.__setattr__(self, field, value)
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def _inherited_static_fields(cls: type) -> set[str]:
|
|
239
|
+
static_fields = set()
|
|
240
|
+
for parent_class in cls.mro():
|
|
241
|
+
if parent_class is not cls and parent_class is not Pytree:
|
|
242
|
+
if issubclass(parent_class, Pytree):
|
|
243
|
+
static_fields.update(parent_class._pytree__static_fields)
|
|
244
|
+
elif dataclasses.is_dataclass(parent_class):
|
|
245
|
+
for field in dataclasses.fields(parent_class):
|
|
246
|
+
if is_static_field(field):
|
|
247
|
+
static_fields.add(field.name)
|
|
248
|
+
return static_fields
|
|
@@ -30,14 +30,13 @@
|
|
|
30
30
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
31
31
|
# See the License for the specific language governing permissions and
|
|
32
32
|
# limitations under the License.
|
|
33
|
-
import dataclasses
|
|
34
|
-
|
|
35
33
|
import beartype
|
|
36
34
|
import jax.numpy as jnp
|
|
37
35
|
import jax.typing
|
|
38
36
|
import jaxtyping as jt
|
|
39
37
|
from typing_extensions import TypedDict, override
|
|
40
38
|
|
|
39
|
+
from .. import pytree
|
|
41
40
|
from . import _metric, jm
|
|
42
41
|
|
|
43
42
|
__all__ = ("Accuracy", "StatScores")
|
|
@@ -73,15 +72,16 @@ class Accuracy(_metric.Metric):
|
|
|
73
72
|
|
|
74
73
|
stats: StatScores
|
|
75
74
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
75
|
+
# Configuration, kept static so that it is available as Python data under `jax.jit`
|
|
76
|
+
mode: jm.DataType = pytree.field(static=True)
|
|
77
|
+
average: jm.AverageMethod = pytree.field(static=True)
|
|
78
|
+
mdmc_average: jm.MDMCAverageMethod = pytree.field(static=True)
|
|
79
|
+
num_classes: int | None = pytree.field(static=True)
|
|
80
|
+
threshold: float = pytree.field(static=True)
|
|
81
|
+
multiclass: bool | None = pytree.field(static=True)
|
|
82
|
+
ignore_index: int | None = pytree.field(static=True)
|
|
83
|
+
top_k: int | None = pytree.field(static=True)
|
|
84
|
+
subset_accuracy: bool = pytree.field(static=True)
|
|
85
85
|
|
|
86
86
|
@jt.jaxtyped(typechecker=beartype.beartype)
|
|
87
87
|
def __init__(
|
|
@@ -304,13 +304,12 @@ class Accuracy(_metric.Metric):
|
|
|
304
304
|
multiclass=self.multiclass,
|
|
305
305
|
)
|
|
306
306
|
|
|
307
|
-
return
|
|
307
|
+
return self.replace(stats=StatScores(tp=tp, fp=fp, tn=tn, fn=fn))
|
|
308
308
|
|
|
309
309
|
@override
|
|
310
310
|
def merge(self, other: "Accuracy") -> "Accuracy":
|
|
311
311
|
"""Merge function."""
|
|
312
|
-
return
|
|
313
|
-
self,
|
|
312
|
+
return self.replace(
|
|
314
313
|
stats=StatScores(
|
|
315
314
|
tp=self.stats["tp"] + other.stats["tp"],
|
|
316
315
|
fp=self.stats["fp"] + other.stats["fp"],
|
|
@@ -3,10 +3,11 @@
|
|
|
3
3
|
import abc
|
|
4
4
|
from typing import TYPE_CHECKING, TypeVar
|
|
5
5
|
|
|
6
|
-
import equinox as eqx
|
|
7
6
|
import jax
|
|
8
7
|
from typing_extensions import override
|
|
9
8
|
|
|
9
|
+
from .. import pytree
|
|
10
|
+
|
|
10
11
|
if TYPE_CHECKING:
|
|
11
12
|
import reax
|
|
12
13
|
|
|
@@ -15,7 +16,7 @@ __all__ = "MetricEvaluator", "DefaultEvaluator", "VmapEvaluator"
|
|
|
15
16
|
T = TypeVar("T", bound="reax.Metric")
|
|
16
17
|
|
|
17
18
|
|
|
18
|
-
class MetricEvaluator(abc.ABC,
|
|
19
|
+
class MetricEvaluator(abc.ABC, pytree.PyTree):
|
|
19
20
|
@abc.abstractmethod
|
|
20
21
|
def create(self, metric: T, *args, **kwargs) -> T:
|
|
21
22
|
"""Evaluate the metric creating a new instance"""
|
|
@@ -4,11 +4,12 @@ import functools
|
|
|
4
4
|
import types
|
|
5
5
|
from typing import Any, ClassVar, Generic, TypeVar, cast
|
|
6
6
|
|
|
7
|
-
import equinox
|
|
8
7
|
import jax
|
|
9
8
|
import jax.numpy as jnp
|
|
10
9
|
from typing_extensions import Self, override
|
|
11
10
|
|
|
11
|
+
from .. import pytree
|
|
12
|
+
|
|
12
13
|
__all__ = ("Metric", "FromFun")
|
|
13
14
|
|
|
14
15
|
_OutT = TypeVar("_OutT")
|
|
@@ -29,7 +30,7 @@ class hybrid_method(Generic[_R]): # pylint: disable=invalid-name
|
|
|
29
30
|
return types.MethodType(self.__func__, objtype if obj is None else obj)
|
|
30
31
|
|
|
31
32
|
|
|
32
|
-
class Metric(
|
|
33
|
+
class Metric(pytree.PyTree, Generic[_OutT], metaclass=abc.ABCMeta):
|
|
33
34
|
"""The base class for all metrics.
|
|
34
35
|
|
|
35
36
|
To be compatible with JAX Metrics are designed to be immutable meaning that when we 'update'
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import abc
|
|
2
2
|
from typing import Any
|
|
3
3
|
|
|
4
|
-
import equinox as eqx
|
|
5
4
|
import jax
|
|
6
5
|
import jax.numpy as jnp
|
|
7
6
|
from typing_extensions import Self, override
|
|
8
7
|
|
|
9
|
-
from reax
|
|
8
|
+
from reax import pytree
|
|
9
|
+
from reax.utils import arrays, masking
|
|
10
10
|
|
|
11
11
|
from . import utils
|
|
12
12
|
from ._metric import Metric
|
|
@@ -51,8 +51,8 @@ class SetAccumulation(Metric[jax.Array]):
|
|
|
51
51
|
"""Get the set of unique values with a fixed memory footprint."""
|
|
52
52
|
|
|
53
53
|
# We keep these static so JAX knows the shape of the accumulator at compile time
|
|
54
|
-
max_size: int =
|
|
55
|
-
fill_value: Any =
|
|
54
|
+
max_size: int = pytree.field(static=True, default=SET_ACCUMULATION_MAX_SIZE)
|
|
55
|
+
fill_value: Any = pytree.field(static=True, default=-1)
|
|
56
56
|
_accumulator: jax.Array | None = None
|
|
57
57
|
|
|
58
58
|
@property
|
|
@@ -83,7 +83,9 @@ class SetAccumulation(Metric[jax.Array]):
|
|
|
83
83
|
# Use a large finite negative number to avoid NaN gradient poisoning
|
|
84
84
|
fill_value = SET_ACCUMULATION_FILL_VALUE
|
|
85
85
|
else:
|
|
86
|
-
|
|
86
|
+
# Use the dtype JAX will hold this as (int64 -> int32 unless x64 is enabled),
|
|
87
|
+
# otherwise the sentinel overflows when cast back in `compute`
|
|
88
|
+
fill_value = jnp.iinfo(jax.dtypes.canonicalize_dtype(val_arr.dtype)).max
|
|
87
89
|
|
|
88
90
|
acc = cls._get_unique_fixed(np_, val_arr, max_size, fill_value, where=mask)
|
|
89
91
|
return cls(_accumulator=acc, max_size=max_size, fill_value=fill_value)
|
|
@@ -100,18 +102,15 @@ class SetAccumulation(Metric[jax.Array]):
|
|
|
100
102
|
fv_cast = self._cast_sentinel(self.accumulator)
|
|
101
103
|
|
|
102
104
|
if mask is not None:
|
|
103
|
-
values = jnp.where(mask, values, fv_cast)
|
|
105
|
+
values = jnp.where(masking.prepare_mask(values, mask), values, fv_cast)
|
|
104
106
|
|
|
105
|
-
|
|
107
|
+
# The accumulator is flat, so flatten e.g. (N, 1) per-node values before joining
|
|
108
|
+
combined = jnp.concatenate([self.accumulator, values.reshape(-1)])
|
|
106
109
|
|
|
107
110
|
# 3. Re-uniquify the combined buffer
|
|
108
111
|
new_acc = self._get_unique_fixed(jnp, combined, self.max_size, self.fill_value)
|
|
109
112
|
|
|
110
|
-
return
|
|
111
|
-
lambda m: m._accumulator, # pylint: disable=protected-access
|
|
112
|
-
self,
|
|
113
|
-
new_acc,
|
|
114
|
-
)
|
|
113
|
+
return self.replace(_accumulator=new_acc)
|
|
115
114
|
|
|
116
115
|
@override
|
|
117
116
|
def merge(self, other: "SetAccumulation") -> "SetAccumulation":
|
|
@@ -124,7 +123,7 @@ class SetAccumulation(Metric[jax.Array]):
|
|
|
124
123
|
np_ = arrays.infer_backend([self.accumulator, other.accumulator])
|
|
125
124
|
combined = np_.concatenate([self.accumulator, other.accumulator])
|
|
126
125
|
new_acc = self._get_unique_fixed(np_, combined, self.max_size, self.fill_value)
|
|
127
|
-
return
|
|
126
|
+
return self.replace(_accumulator=new_acc)
|
|
128
127
|
|
|
129
128
|
@override
|
|
130
129
|
def compute(self) -> jax.Array:
|
|
@@ -168,7 +167,7 @@ class SetAccumulation(Metric[jax.Array]):
|
|
|
168
167
|
fv_cast = np_.array(fill_value, dtype=values.dtype)
|
|
169
168
|
|
|
170
169
|
if where is not None:
|
|
171
|
-
where =
|
|
170
|
+
where = masking.prepare_mask(values, where)
|
|
172
171
|
values = np_.where(where, values, fv_cast)
|
|
173
172
|
|
|
174
173
|
if np_ is jnp:
|
|
@@ -269,11 +268,9 @@ class Std(Aggregation):
|
|
|
269
268
|
) -> tuple[jax.Array, jax.Array, jax.Array]:
|
|
270
269
|
"""Get the total, sum of squares and count of the (unmasked) values."""
|
|
271
270
|
values = jnp.atleast_1d(values)
|
|
272
|
-
mask
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
return values.sum(), (values**2).sum(), jnp.asarray(count)
|
|
271
|
+
where = None if mask is None else masking.prepare_mask(values, mask)
|
|
272
|
+
count = masking.count_valid(values, where)
|
|
273
|
+
return values.sum(where=where), (values**2).sum(where=where), jnp.asarray(count)
|
|
277
274
|
|
|
278
275
|
def merge(self, other: "Std") -> "Std":
|
|
279
276
|
"""Merge function."""
|
|
@@ -2,11 +2,10 @@ from collections.abc import Sequence
|
|
|
2
2
|
from typing import TYPE_CHECKING, Any
|
|
3
3
|
|
|
4
4
|
import beartype
|
|
5
|
-
import equinox
|
|
6
5
|
import flax.core
|
|
7
6
|
import jaxtyping as jt
|
|
8
7
|
|
|
9
|
-
from .. import exceptions
|
|
8
|
+
from .. import exceptions, pytree
|
|
10
9
|
from . import _evaluators
|
|
11
10
|
from . import _metric as metric_
|
|
12
11
|
|
|
@@ -18,7 +17,7 @@ __all__ = ("MetricCollection", "combine")
|
|
|
18
17
|
MetricType = type[metric_.Metric] | metric_.Metric
|
|
19
18
|
|
|
20
19
|
|
|
21
|
-
class MetricCollection(
|
|
20
|
+
class MetricCollection(pytree.PyTree):
|
|
22
21
|
"""A collection of metrics that can be created/updated/merged using a single call."""
|
|
23
22
|
|
|
24
23
|
_metrics: "flax.core.FrozenDict[str, reax.Metric]"
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
from collections.abc import Callable
|
|
2
|
+
from typing import TYPE_CHECKING, Any, ClassVar, Protocol, TypeVar
|
|
3
|
+
|
|
4
|
+
import beartype
|
|
5
|
+
import jax
|
|
6
|
+
import jax.numpy as jnp
|
|
7
|
+
import jaxtyping as jt
|
|
8
|
+
from jaxtyping import ArrayLike
|
|
9
|
+
from typing_extensions import override
|
|
10
|
+
|
|
11
|
+
from reax.utils import masking
|
|
12
|
+
|
|
13
|
+
from .. import pytree
|
|
14
|
+
from ._metric import Metric
|
|
15
|
+
|
|
16
|
+
if TYPE_CHECKING:
|
|
17
|
+
import reax
|
|
18
|
+
|
|
19
|
+
OptionalMask = reax.types.ArrayMask | None
|
|
20
|
+
else:
|
|
21
|
+
OptionalMask = Any
|
|
22
|
+
|
|
23
|
+
__all__ = tuple()
|
|
24
|
+
|
|
25
|
+
M = TypeVar("M", bound=Metric)
|
|
26
|
+
_WithAccT = TypeVar("_WithAccT", bound="WithAccumulator")
|
|
27
|
+
_WithAccCountT = TypeVar("_WithAccCountT", bound="WithAccumulatorAndCount")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class ReduceFn(Protocol):
|
|
31
|
+
def __call__(
|
|
32
|
+
self, values: ArrayLike, /, axis: Any = None, *, where: ArrayLike | None = None
|
|
33
|
+
) -> jax.Array:
|
|
34
|
+
"""Perform reduction on the passed values (e.g. :func:`jax.numpy.sum`)."""
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def concat(tensors: tuple[jax.Array]) -> jax.Array:
|
|
38
|
+
"""Concat function."""
|
|
39
|
+
return jnp.concatenate(tuple(map(jnp.atleast_1d, tensors)))
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class WithAccumulator(pytree.PyTree):
|
|
43
|
+
"""Abstract aggregation metric."""
|
|
44
|
+
|
|
45
|
+
reduce_fn: ClassVar[ReduceFn]
|
|
46
|
+
merge_fn: ClassVar[Callable] = concat
|
|
47
|
+
|
|
48
|
+
accumulator: jax.Array | None = None
|
|
49
|
+
|
|
50
|
+
@classmethod
|
|
51
|
+
def empty(cls):
|
|
52
|
+
return cls(accumulator=None)
|
|
53
|
+
|
|
54
|
+
@classmethod
|
|
55
|
+
def create(
|
|
56
|
+
cls: type[_WithAccT],
|
|
57
|
+
values: jax.typing.ArrayLike,
|
|
58
|
+
mask: OptionalMask = None,
|
|
59
|
+
) -> _WithAccT:
|
|
60
|
+
"""Create function."""
|
|
61
|
+
where = None if mask is None else masking.prepare_mask(values, mask)
|
|
62
|
+
return cls(accumulator=cls.reduce_fn(values, where=where))
|
|
63
|
+
|
|
64
|
+
@jt.jaxtyped(typechecker=beartype.beartype)
|
|
65
|
+
def merge(self: _WithAccT, other: _WithAccT) -> _WithAccT:
|
|
66
|
+
"""Merge function."""
|
|
67
|
+
if self.accumulator is None:
|
|
68
|
+
return other
|
|
69
|
+
if other.accumulator is None:
|
|
70
|
+
return self
|
|
71
|
+
|
|
72
|
+
cls = type(self)
|
|
73
|
+
return cls(accumulator=cls.reduce_fn(cls.merge_fn((self.accumulator, other.accumulator))))
|
|
74
|
+
|
|
75
|
+
@jt.jaxtyped(typechecker=beartype.beartype)
|
|
76
|
+
def update(
|
|
77
|
+
self: _WithAccT,
|
|
78
|
+
values: jax.typing.ArrayLike,
|
|
79
|
+
mask: jax.typing.ArrayLike | None = None,
|
|
80
|
+
) -> _WithAccT:
|
|
81
|
+
"""Update function."""
|
|
82
|
+
cls = type(self)
|
|
83
|
+
if self.accumulator is None:
|
|
84
|
+
return cls.create(values, mask=mask)
|
|
85
|
+
|
|
86
|
+
where = None if mask is None else masking.prepare_mask(values, mask)
|
|
87
|
+
reduced = cls.reduce_fn(values, where=where)
|
|
88
|
+
return cls(accumulator=cls.reduce_fn(cls.merge_fn((self.accumulator, reduced))))
|
|
89
|
+
|
|
90
|
+
@jt.jaxtyped(typechecker=beartype.beartype)
|
|
91
|
+
def compute(self) -> jax.Array:
|
|
92
|
+
"""Compute function."""
|
|
93
|
+
if self.accumulator is None:
|
|
94
|
+
raise RuntimeError("Nothing to compute, metric is empty!")
|
|
95
|
+
return self.accumulator
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
class WithAccumulatorAndCount(WithAccumulator):
|
|
99
|
+
"""Helper class to group common functionality for metrics that can keep track using a total and
|
|
100
|
+
count accumulators.
|
|
101
|
+
"""
|
|
102
|
+
|
|
103
|
+
count: jax.typing.ArrayLike = 0
|
|
104
|
+
|
|
105
|
+
@override
|
|
106
|
+
@classmethod
|
|
107
|
+
def create(
|
|
108
|
+
cls: type[_WithAccCountT],
|
|
109
|
+
values: jt.ArrayLike,
|
|
110
|
+
mask: OptionalMask = None,
|
|
111
|
+
) -> _WithAccCountT:
|
|
112
|
+
"""Create function."""
|
|
113
|
+
values = jnp.atleast_1d(values)
|
|
114
|
+
where = None if mask is None else masking.prepare_mask(values, mask)
|
|
115
|
+
return cls(
|
|
116
|
+
accumulator=cls.reduce_fn(values, where=where), count=masking.count_valid(values, where)
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
@override
|
|
120
|
+
@jt.jaxtyped(typechecker=beartype.beartype)
|
|
121
|
+
def merge(self: _WithAccCountT, other: _WithAccCountT) -> _WithAccCountT:
|
|
122
|
+
"""Merge function."""
|
|
123
|
+
if self.accumulator is None:
|
|
124
|
+
return other
|
|
125
|
+
if other.accumulator is None:
|
|
126
|
+
return self
|
|
127
|
+
|
|
128
|
+
cls = type(self)
|
|
129
|
+
return cls(
|
|
130
|
+
accumulator=cls.reduce_fn(cls.merge_fn((self.accumulator, other.accumulator))),
|
|
131
|
+
count=self.count + other.count,
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
@override
|
|
135
|
+
@jt.jaxtyped(typechecker=beartype.beartype)
|
|
136
|
+
def update(
|
|
137
|
+
self: _WithAccCountT,
|
|
138
|
+
values: jax.typing.ArrayLike,
|
|
139
|
+
mask: jax.typing.ArrayLike | None = None,
|
|
140
|
+
) -> _WithAccCountT:
|
|
141
|
+
"""Update function."""
|
|
142
|
+
cls = type(self)
|
|
143
|
+
if self.accumulator is None:
|
|
144
|
+
# This metric is empty
|
|
145
|
+
return cls.create(values, mask=mask)
|
|
146
|
+
|
|
147
|
+
where = None if mask is None else masking.prepare_mask(values, mask)
|
|
148
|
+
reduced = cls.reduce_fn(values, where=where)
|
|
149
|
+
return cls(
|
|
150
|
+
accumulator=cls.reduce_fn(cls.merge_fn((self.accumulator, reduced))),
|
|
151
|
+
count=self.count + masking.count_valid(values, where),
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
@override
|
|
155
|
+
@jt.jaxtyped(typechecker=beartype.beartype)
|
|
156
|
+
def compute(self) -> jax.Array:
|
|
157
|
+
"""Compute function."""
|
|
158
|
+
if self.accumulator is None:
|
|
159
|
+
raise RuntimeError("Nothing to compute, metric is empty!")
|
|
160
|
+
return self.accumulator / self.count
|