reax 0.7.0.dev3__tar.gz → 0.7.0.dev5__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.
Files changed (105) hide show
  1. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/PKG-INFO +2 -2
  2. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/pyproject.toml +1 -1
  3. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/__init__.py +1 -1
  4. reax-0.7.0.dev5/src/reax/_vendor/__init__.py +1 -0
  5. reax-0.7.0.dev5/src/reax/_vendor/simple_pytree.py +248 -0
  6. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/listeners/early_stopping.py +17 -3
  7. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/metrics/_classification.py +13 -14
  8. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/metrics/_evaluators.py +3 -2
  9. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/metrics/_metric.py +3 -2
  10. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/metrics/aggregation.py +16 -19
  11. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/metrics/collections.py +2 -3
  12. reax-0.7.0.dev5/src/reax/metrics/utils.py +160 -0
  13. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/modules.py +3 -3
  14. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/optimizers.py +8 -5
  15. reax-0.7.0.dev5/src/reax/pytree.py +87 -0
  16. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/results.py +8 -7
  17. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/stages/_loops.py +1 -1
  18. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/stages/stages.py +1 -1
  19. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/strategies/_jax.py +3 -3
  20. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/strategies/_single_device.py +1 -1
  21. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/training/trainer.py +1 -1
  22. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/types.py +4 -0
  23. reax-0.7.0.dev5/src/reax/utils/__init__.py +4 -0
  24. reax-0.7.0.dev5/src/reax/utils/masking.py +97 -0
  25. reax-0.7.0.dev3/src/reax/metrics/utils.py +0 -252
  26. reax-0.7.0.dev3/src/reax/utils/__init__.py +0 -4
  27. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/LICENSE.txt +0 -0
  28. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/README.rst +0 -0
  29. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/_engine.py +0 -0
  30. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/_module_hooks.py +0 -0
  31. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/data/__init__.py +0 -0
  32. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/data/_datasource_manager.py +0 -0
  33. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/data/_datasources.py +0 -0
  34. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/data/_loaders.py +0 -0
  35. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/data/_types.py +0 -0
  36. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/data/collate.py +0 -0
  37. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/data/dataloaders.py +0 -0
  38. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/data/datamodules.py +0 -0
  39. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/data/datasets.py +0 -0
  40. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/data/fetchers.py +0 -0
  41. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/data/kfold.py +0 -0
  42. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/data/samplers.py +0 -0
  43. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/data/utils.py +0 -0
  44. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/demos/__init__.py +0 -0
  45. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/demos/boring_classes.py +0 -0
  46. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/demos/mnist.py +0 -0
  47. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/exceptions.py +0 -0
  48. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/hooks.py +0 -0
  49. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/keys.py +0 -0
  50. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/lightning/LICENSE +0 -0
  51. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/lightning/__init__.py +0 -0
  52. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/lightning/rank_zero.py +0 -0
  53. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/listeners/__init__.py +0 -0
  54. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/listeners/checkpointer.py +0 -0
  55. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/listeners/model_checkpoint.py +0 -0
  56. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/listeners/progress/__init__.py +0 -0
  57. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/listeners/progress/progress_bar.py +0 -0
  58. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/listeners/progress/rich_progress.py +0 -0
  59. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/listeners/progress/tqdm_progress.py +0 -0
  60. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/listeners/utils.py +0 -0
  61. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/loggers/__init__.py +0 -0
  62. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/loggers/_utils.py +0 -0
  63. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/loggers/csv_logs.py +0 -0
  64. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/loggers/logger.py +0 -0
  65. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/loggers/mlflow.py +0 -0
  66. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/loggers/pandas.py +0 -0
  67. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/loggers/tensorboard.py +0 -0
  68. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/metrics/__init__.py +0 -0
  69. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/metrics/_globals.py +0 -0
  70. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/metrics/_registry.py +0 -0
  71. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/metrics/jm/__init__.py +0 -0
  72. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/metrics/jm/types.py +0 -0
  73. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/metrics/jm/utils.py +0 -0
  74. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/metrics/regression.py +0 -0
  75. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/plugins.py +0 -0
  76. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/profilers/__init__.py +0 -0
  77. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/profilers/jax_profiler.py +0 -0
  78. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/profilers/profiler.py +0 -0
  79. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/random.py +0 -0
  80. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/saving.py +0 -0
  81. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/stages/__init__.py +0 -0
  82. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/stages/_timer.py +0 -0
  83. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/stages/common.py +0 -0
  84. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/stages/fit.py +0 -0
  85. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/stages/predict.py +0 -0
  86. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/stages/stats.py +0 -0
  87. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/stages/test.py +0 -0
  88. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/stages/train.py +0 -0
  89. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/stages/validation.py +0 -0
  90. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/strategies/__init__.py +0 -0
  91. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/strategies/_factory.py +0 -0
  92. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/strategies/_parallel.py +0 -0
  93. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/strategies/_strategies.py +0 -0
  94. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/strategies/_utils.py +0 -0
  95. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/testing.py +0 -0
  96. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/training/__init__.py +0 -0
  97. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/training/_checkpoint_connector.py +0 -0
  98. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/training/_checkpointing.py +0 -0
  99. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/training/_hooks_map.py +0 -0
  100. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/training/_logger_connector.py +0 -0
  101. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/training/_utils.py +0 -0
  102. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/utils/arrays.py +0 -0
  103. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/utils/containers.py +0 -0
  104. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/src/reax/utils/events.py +0 -0
  105. {reax-0.7.0.dev3 → reax-0.7.0.dev5}/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.dev3
3
+ Version: 0.7.0.dev5
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',
@@ -68,4 +68,4 @@ __all__ = (
68
68
  )
69
69
  )
70
70
 
71
- __version__ = "0.7.0.dev3"
71
+ __version__ = "0.7.0.dev5"
@@ -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
@@ -106,6 +106,9 @@ class EarlyStopping(hooks.TrainerListener):
106
106
  log_rank_zero_only (bool, optional): When set ``True``, logs the
107
107
  status of the early stopping listener only for rank 0
108
108
  process, defaults to False.
109
+ start_from_epoch (int, optional): Epoch at which early stopping
110
+ checks should begin. Checks will not run if the current epoch
111
+ is strictly less than ``start_from_epoch``, defaults to 0.
109
112
 
110
113
  Raises:
111
114
  MisconfigurationException: If ``mode`` is none of ``"min"`` or
@@ -138,6 +141,7 @@ class EarlyStopping(hooks.TrainerListener):
138
141
  divergence_threshold: float | None = None,
139
142
  check_on_train_epoch_end: bool | None = None,
140
143
  log_rank_zero_only: bool = False,
144
+ start_from_epoch: int = 0,
141
145
  ):
142
146
  # Params
143
147
  self._monitor: Final[str] = monitor
@@ -152,6 +156,7 @@ class EarlyStopping(hooks.TrainerListener):
152
156
  self.__check_on_train_epoch_end = check_on_train_epoch_end
153
157
  self._log_rank_zero_only = log_rank_zero_only
154
158
  self._min_delta: Final[float] = min_delta if mode == "max" else -min_delta
159
+ self._start_from_epoch: Final[int] = start_from_epoch
155
160
 
156
161
  # State
157
162
  self._check_on_train_epoch_end = check_on_train_epoch_end
@@ -175,7 +180,9 @@ class EarlyStopping(hooks.TrainerListener):
175
180
 
176
181
  def _should_skip_check(self, trainer: "reax.Trainer") -> bool:
177
182
  """Should skip check."""
178
- return not isinstance(trainer.stage, stages.Fit) and not trainer.sanity_checking
183
+ if trainer.current_epoch < self._start_from_epoch:
184
+ return True
185
+ return not isinstance(trainer.stage, stages.Fit) or trainer.sanity_checking
179
186
 
180
187
  @override
181
188
  def on_fit_start(self, _trainer: "reax.Trainer", stage: "reax.stages.Fit", /) -> None:
@@ -197,8 +204,15 @@ class EarlyStopping(hooks.TrainerListener):
197
204
  self._run_early_stopping_check(trainer, stage)
198
205
 
199
206
  @override
200
- def on_validation_end(self, trainer: "reax.Trainer", stage: "reax.stages.Validate", /) -> None:
201
- """On validation end."""
207
+ def on_validation_epoch_end(
208
+ self, trainer: "reax.Trainer", stage: "reax.Stage", _loop: "reax.stages.ValidationEpoch", /
209
+ ) -> None:
210
+ """On validation epoch end.
211
+
212
+ Validation nested inside ``fit`` fires this hook, whereas ``on_validation_end`` only
213
+ fires for a standalone validation stage. This is therefore where the check has to run
214
+ when ``check_on_train_epoch_end`` is false.
215
+ """
202
216
  if self._check_on_train_epoch_end or self._should_skip_check(trainer):
203
217
  return
204
218
  self._run_early_stopping_check(trainer, stage)
@@ -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
- mode: jm.DataType
77
- average: jm.AverageMethod
78
- mdmc_average: jm.MDMCAverageMethod
79
- num_classes: int | None
80
- threshold: float
81
- multiclass: bool | None
82
- ignore_index: int | None
83
- top_k: int | None
84
- subset_accuracy: bool
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 dataclasses.replace(self, stats=StatScores(tp=tp, fp=fp, tn=tn, fn=fn))
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 dataclasses.replace(
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, eqx.Module):
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(equinox.Module, Generic[_OutT], metaclass=abc.ABCMeta):
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.utils import arrays
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 = eqx.field(static=True, default=SET_ACCUMULATION_MAX_SIZE)
55
- fill_value: Any = eqx.field(static=True, default=-1)
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
- fill_value = jnp.iinfo(val_arr.dtype).max
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
- combined = jnp.concatenate([self.accumulator, values])
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 eqx.tree_at(
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 eqx.tree_at(lambda m: m.accumulator, self, new_acc)
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 = utils.prepare_mask(values, 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, count = utils.prepare_mask(values, mask, return_count=True)
273
- if mask is not None:
274
- values = jnp.where(mask, values, jnp.zeros_like(values))
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(equinox.Module):
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]"