brainstate 0.2.1__py2.py3-none-any.whl → 0.2.2__py2.py3-none-any.whl
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.
- brainstate/__init__.py +167 -169
- brainstate/_compatible_import.py +340 -340
- brainstate/_compatible_import_test.py +681 -681
- brainstate/_deprecation.py +210 -210
- brainstate/_deprecation_test.py +2297 -2319
- brainstate/_error.py +45 -45
- brainstate/_state.py +2157 -1652
- brainstate/_state_test.py +1129 -52
- brainstate/_utils.py +47 -47
- brainstate/environ.py +1495 -1495
- brainstate/environ_test.py +1223 -1223
- brainstate/graph/__init__.py +22 -22
- brainstate/graph/_node.py +240 -240
- brainstate/graph/_node_test.py +589 -589
- brainstate/graph/_operation.py +1620 -1624
- brainstate/graph/_operation_test.py +1147 -1147
- brainstate/mixin.py +1447 -1433
- brainstate/mixin_test.py +1017 -1017
- brainstate/nn/__init__.py +146 -137
- brainstate/nn/_activations.py +1100 -1100
- brainstate/nn/_activations_test.py +354 -354
- brainstate/nn/_collective_ops.py +635 -633
- brainstate/nn/_collective_ops_test.py +774 -774
- brainstate/nn/_common.py +226 -226
- brainstate/nn/_common_test.py +134 -154
- brainstate/nn/_conv.py +2010 -2010
- brainstate/nn/_conv_test.py +849 -849
- brainstate/nn/_delay.py +575 -575
- brainstate/nn/_delay_test.py +243 -243
- brainstate/nn/_dropout.py +618 -618
- brainstate/nn/_dropout_test.py +480 -477
- brainstate/nn/_dynamics.py +870 -1267
- brainstate/nn/_dynamics_test.py +53 -67
- brainstate/nn/_elementwise.py +1298 -1298
- brainstate/nn/_elementwise_test.py +829 -829
- brainstate/nn/_embedding.py +408 -408
- brainstate/nn/_embedding_test.py +156 -156
- brainstate/nn/_event_fixedprob.py +233 -233
- brainstate/nn/_event_fixedprob_test.py +115 -115
- brainstate/nn/_event_linear.py +83 -83
- brainstate/nn/_event_linear_test.py +121 -121
- brainstate/nn/_exp_euler.py +254 -254
- brainstate/nn/_exp_euler_test.py +377 -377
- brainstate/nn/_linear.py +744 -744
- brainstate/nn/_linear_test.py +475 -475
- brainstate/nn/_metrics.py +1070 -1070
- brainstate/nn/_metrics_test.py +611 -611
- brainstate/nn/_module.py +391 -384
- brainstate/nn/_module_test.py +40 -40
- brainstate/nn/_normalizations.py +1334 -1334
- brainstate/nn/_normalizations_test.py +699 -699
- brainstate/nn/_paddings.py +1020 -1020
- brainstate/nn/_paddings_test.py +722 -722
- brainstate/nn/_poolings.py +2239 -2239
- brainstate/nn/_poolings_test.py +952 -952
- brainstate/nn/_rnns.py +946 -946
- brainstate/nn/_rnns_test.py +592 -592
- brainstate/nn/_utils.py +216 -216
- brainstate/nn/_utils_test.py +401 -401
- brainstate/nn/init.py +809 -809
- brainstate/nn/init_test.py +180 -180
- brainstate/random/__init__.py +270 -270
- brainstate/random/{_rand_funs.py → _fun.py} +3938 -3938
- brainstate/random/{_rand_funs_test.py → _fun_test.py} +638 -640
- brainstate/random/_impl.py +672 -0
- brainstate/random/{_rand_seed.py → _seed.py} +675 -675
- brainstate/random/{_rand_seed_test.py → _seed_test.py} +48 -48
- brainstate/random/{_rand_state.py → _state.py} +1320 -1617
- brainstate/random/{_rand_state_test.py → _state_test.py} +551 -551
- brainstate/transform/__init__.py +56 -59
- brainstate/transform/_ad_checkpoint.py +176 -176
- brainstate/transform/_ad_checkpoint_test.py +49 -49
- brainstate/transform/_autograd.py +1025 -1025
- brainstate/transform/_autograd_test.py +1289 -1289
- brainstate/transform/_conditions.py +316 -316
- brainstate/transform/_conditions_test.py +220 -220
- brainstate/transform/_error_if.py +94 -94
- brainstate/transform/_error_if_test.py +52 -52
- brainstate/transform/_find_state.py +200 -0
- brainstate/transform/_find_state_test.py +84 -0
- brainstate/transform/_jit.py +399 -399
- brainstate/transform/_jit_test.py +143 -143
- brainstate/transform/_loop_collect_return.py +675 -675
- brainstate/transform/_loop_collect_return_test.py +58 -58
- brainstate/transform/_loop_no_collection.py +283 -283
- brainstate/transform/_loop_no_collection_test.py +50 -50
- brainstate/transform/_make_jaxpr.py +2176 -2016
- brainstate/transform/_make_jaxpr_test.py +1634 -1510
- brainstate/transform/_mapping.py +607 -529
- brainstate/transform/_mapping_test.py +104 -194
- brainstate/transform/_progress_bar.py +255 -255
- brainstate/transform/_unvmap.py +256 -256
- brainstate/transform/_util.py +286 -286
- brainstate/typing.py +837 -837
- brainstate/typing_test.py +780 -780
- brainstate/util/__init__.py +27 -27
- brainstate/util/_others.py +1024 -1024
- brainstate/util/_others_test.py +962 -962
- brainstate/util/_pretty_pytree.py +1301 -1301
- brainstate/util/_pretty_pytree_test.py +675 -675
- brainstate/util/_pretty_repr.py +462 -462
- brainstate/util/_pretty_repr_test.py +696 -696
- brainstate/util/filter.py +945 -945
- brainstate/util/filter_test.py +911 -911
- brainstate/util/struct.py +910 -910
- brainstate/util/struct_test.py +602 -602
- {brainstate-0.2.1.dist-info → brainstate-0.2.2.dist-info}/METADATA +108 -108
- brainstate-0.2.2.dist-info/RECORD +111 -0
- {brainstate-0.2.1.dist-info → brainstate-0.2.2.dist-info}/licenses/LICENSE +202 -202
- brainstate/transform/_eval_shape.py +0 -145
- brainstate/transform/_eval_shape_test.py +0 -38
- brainstate/transform/_random.py +0 -171
- brainstate-0.2.1.dist-info/RECORD +0 -111
- {brainstate-0.2.1.dist-info → brainstate-0.2.2.dist-info}/WHEEL +0 -0
- {brainstate-0.2.1.dist-info → brainstate-0.2.2.dist-info}/top_level.txt +0 -0
brainstate/nn/_common.py
CHANGED
@@ -1,226 +1,226 @@
|
|
1
|
-
# Copyright 2025 BrainX Ecosystem Limited. All Rights Reserved.
|
2
|
-
#
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
-
# you may not use this file except in compliance with the License.
|
5
|
-
# You may obtain a copy of the License at
|
6
|
-
#
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
-
#
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
-
# See the License for the specific language governing permissions and
|
13
|
-
# limitations under the License.
|
14
|
-
# ==============================================================================
|
15
|
-
|
16
|
-
# -*- coding: utf-8 -*-
|
17
|
-
|
18
|
-
from collections import defaultdict
|
19
|
-
from typing import Any, Sequence, Hashable, Dict
|
20
|
-
|
21
|
-
from brainstate import environ
|
22
|
-
from brainstate.transform._mapping import vmap
|
23
|
-
from brainstate.typing import Filter
|
24
|
-
from ._module import Module
|
25
|
-
|
26
|
-
AxisName = Hashable
|
27
|
-
|
28
|
-
__all__ = [
|
29
|
-
'EnvironContext',
|
30
|
-
'Vmap',
|
31
|
-
]
|
32
|
-
|
33
|
-
|
34
|
-
class EnvironContext(Module):
|
35
|
-
"""Wrap a module so it executes inside a brainstate environment context.
|
36
|
-
|
37
|
-
Parameters
|
38
|
-
----------
|
39
|
-
layer : Module
|
40
|
-
Module executed within the environment context.
|
41
|
-
**context
|
42
|
-
Keyword arguments forwarded to ``brainstate.environ.context``.
|
43
|
-
|
44
|
-
Attributes
|
45
|
-
----------
|
46
|
-
layer : Module
|
47
|
-
Wrapped module executed inside the context.
|
48
|
-
context : dict
|
49
|
-
Environment arguments applied to the wrapped module.
|
50
|
-
|
51
|
-
Examples
|
52
|
-
--------
|
53
|
-
.. code-block:: python
|
54
|
-
|
55
|
-
>>> import brainstate
|
56
|
-
>>> from brainstate.nn import EnvironContext
|
57
|
-
>>> wrapped = EnvironContext(layer, fit=True)
|
58
|
-
>>> result = wrapped.update(inputs)
|
59
|
-
"""
|
60
|
-
|
61
|
-
def __init__(self, layer: Module, **context):
|
62
|
-
"""Initialize the wrapper with a module and environment arguments.
|
63
|
-
|
64
|
-
Parameters
|
65
|
-
----------
|
66
|
-
layer : Module
|
67
|
-
Module executed inside the environment context.
|
68
|
-
**context
|
69
|
-
Keyword arguments forwarded to ``brainstate.environ.context``.
|
70
|
-
"""
|
71
|
-
super().__init__()
|
72
|
-
|
73
|
-
assert isinstance(layer, Module), 'The layer must be an instance of Module.'
|
74
|
-
self.layer = layer
|
75
|
-
self.context = context
|
76
|
-
|
77
|
-
def update(self, *args, context: Dict = None, **kwargs):
|
78
|
-
"""Execute the wrapped module inside the environment context.
|
79
|
-
|
80
|
-
Parameters
|
81
|
-
----------
|
82
|
-
*args
|
83
|
-
Positional arguments forwarded to the wrapped module.
|
84
|
-
**kwargs
|
85
|
-
Keyword arguments forwarded to the wrapped module.
|
86
|
-
context: dict, optional
|
87
|
-
Additional environment settings for this call. Merged with the
|
88
|
-
stored context.
|
89
|
-
|
90
|
-
Returns
|
91
|
-
-------
|
92
|
-
Any
|
93
|
-
Result returned by the wrapped module.
|
94
|
-
"""
|
95
|
-
if context is None:
|
96
|
-
context = dict()
|
97
|
-
with environ.context(**self.context, **context):
|
98
|
-
return self.layer(*args, **kwargs)
|
99
|
-
|
100
|
-
def add_context(self, **context):
|
101
|
-
"""Add more environment settings to the wrapped module.
|
102
|
-
|
103
|
-
Parameters
|
104
|
-
----------
|
105
|
-
**context
|
106
|
-
Keyword arguments merged into the stored environment context.
|
107
|
-
"""
|
108
|
-
self.context.update(context)
|
109
|
-
|
110
|
-
|
111
|
-
def _filter_states(
|
112
|
-
module: Module,
|
113
|
-
filters: Filter | Dict[Filter, int],
|
114
|
-
) -> Dict:
|
115
|
-
"""Normalize state filter specifications for ``Module.states``.
|
116
|
-
|
117
|
-
Parameters
|
118
|
-
----------
|
119
|
-
module : Module
|
120
|
-
Module providing the states interface.
|
121
|
-
filters : Filter or dict[Filter, int]
|
122
|
-
Filters passed by the caller. Dictionary keys are filters and values
|
123
|
-
are the axes they should map over.
|
124
|
-
|
125
|
-
Returns
|
126
|
-
-------
|
127
|
-
dict[int, Any] or Any or None
|
128
|
-
Structured filters to pass to ``Module.states``. Returns ``None`` when
|
129
|
-
no filtering is requested.
|
130
|
-
"""
|
131
|
-
if filters is None:
|
132
|
-
filtered_states = None
|
133
|
-
elif isinstance(filters, dict):
|
134
|
-
in_states_filter = defaultdict(list)
|
135
|
-
for filter_, axis in filters:
|
136
|
-
assert isinstance(axis, int), 'The value of in_states must be the map axis, which should be an integer.'
|
137
|
-
in_states_filter[axis].append(filter_)
|
138
|
-
filtered_states = module.states(*in_states_filter.values())
|
139
|
-
in_states_axis = tuple(in_states_filter.keys())
|
140
|
-
filtered_states = {axis: states for axis, states in zip(in_states_axis, filtered_states)}
|
141
|
-
else:
|
142
|
-
filtered_states = module.states(filters)
|
143
|
-
return filtered_states
|
144
|
-
|
145
|
-
|
146
|
-
class Vmap(Module):
|
147
|
-
"""Vectorize a module with ``brainstate.transform.vmap``.
|
148
|
-
|
149
|
-
Parameters
|
150
|
-
----------
|
151
|
-
module : Module
|
152
|
-
Module to wrap with vectorized mapping.
|
153
|
-
in_axes : int or None or Sequence[Any], optional
|
154
|
-
Specification for mapping over inputs. Defaults to ``0``.
|
155
|
-
out_axes : Any, optional
|
156
|
-
Specification for mapping over outputs. Defaults to ``0``.
|
157
|
-
vmap_states : Filter or dict[Filter, int], optional
|
158
|
-
State filters to vectorize as inputs. Defaults to ``None``.
|
159
|
-
vmap_out_states : Filter or dict[Filter, int], optional
|
160
|
-
State filters to vectorize as outputs. Defaults to ``None``.
|
161
|
-
axis_name : AxisName or None, optional
|
162
|
-
Name of the axis being mapped. Defaults to ``None``.
|
163
|
-
axis_size : int or None, optional
|
164
|
-
Size of the mapped axis. Defaults to ``None``.
|
165
|
-
|
166
|
-
Examples
|
167
|
-
--------
|
168
|
-
.. code-block:: python
|
169
|
-
|
170
|
-
>>> from brainstate.nn import Vmap
|
171
|
-
>>> vmapped = Vmap(module, in_axes=0, axis_name="batch")
|
172
|
-
>>> outputs = vmapped.update(inputs)
|
173
|
-
"""
|
174
|
-
|
175
|
-
def __init__(
|
176
|
-
self,
|
177
|
-
module: Module,
|
178
|
-
in_axes: int | None | Sequence[Any] = 0,
|
179
|
-
out_axes: Any = 0,
|
180
|
-
vmap_states: Filter | Dict[Filter, int] = None,
|
181
|
-
vmap_out_states: Filter | Dict[Filter, int] = None,
|
182
|
-
axis_name: AxisName | None = None,
|
183
|
-
axis_size: int | None = None,
|
184
|
-
):
|
185
|
-
super().__init__()
|
186
|
-
|
187
|
-
# parameters
|
188
|
-
self.in_axes = in_axes
|
189
|
-
self.out_axes = out_axes
|
190
|
-
self.axis_name = axis_name
|
191
|
-
self.axis_size = axis_size
|
192
|
-
assert isinstance(module, Module), 'The module must be an instance of Module.'
|
193
|
-
self.module = module
|
194
|
-
vmap_states = _filter_states(module, vmap_states)
|
195
|
-
vmap_out_states = _filter_states(module, vmap_out_states)
|
196
|
-
|
197
|
-
@vmap(
|
198
|
-
in_axes=in_axes,
|
199
|
-
out_axes=out_axes,
|
200
|
-
in_states=vmap_states,
|
201
|
-
out_states=vmap_out_states,
|
202
|
-
axis_name=axis_name,
|
203
|
-
axis_size=axis_size,
|
204
|
-
)
|
205
|
-
def vmap_run(*args, **kwargs):
|
206
|
-
return module(*args, **kwargs)
|
207
|
-
|
208
|
-
# vmapped module
|
209
|
-
self.vmapped_fn = vmap_run
|
210
|
-
|
211
|
-
def update(self, *args, **kwargs):
|
212
|
-
"""Execute the vmapped module with the given arguments.
|
213
|
-
|
214
|
-
Parameters
|
215
|
-
----------
|
216
|
-
*args
|
217
|
-
Positional arguments forwarded to the vmapped module.
|
218
|
-
**kwargs
|
219
|
-
Keyword arguments forwarded to the vmapped module.
|
220
|
-
|
221
|
-
Returns
|
222
|
-
-------
|
223
|
-
Any
|
224
|
-
Result of executing the vmapped module.
|
225
|
-
"""
|
226
|
-
return self.vmapped_fn(*args, **kwargs)
|
1
|
+
# Copyright 2025 BrainX Ecosystem Limited. All Rights Reserved.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
# ==============================================================================
|
15
|
+
|
16
|
+
# -*- coding: utf-8 -*-
|
17
|
+
|
18
|
+
from collections import defaultdict
|
19
|
+
from typing import Any, Sequence, Hashable, Dict
|
20
|
+
|
21
|
+
from brainstate import environ
|
22
|
+
from brainstate.transform._mapping import vmap
|
23
|
+
from brainstate.typing import Filter
|
24
|
+
from ._module import Module
|
25
|
+
|
26
|
+
AxisName = Hashable
|
27
|
+
|
28
|
+
__all__ = [
|
29
|
+
'EnvironContext',
|
30
|
+
'Vmap',
|
31
|
+
]
|
32
|
+
|
33
|
+
|
34
|
+
class EnvironContext(Module):
|
35
|
+
"""Wrap a module so it executes inside a brainstate environment context.
|
36
|
+
|
37
|
+
Parameters
|
38
|
+
----------
|
39
|
+
layer : Module
|
40
|
+
Module executed within the environment context.
|
41
|
+
**context
|
42
|
+
Keyword arguments forwarded to ``brainstate.environ.context``.
|
43
|
+
|
44
|
+
Attributes
|
45
|
+
----------
|
46
|
+
layer : Module
|
47
|
+
Wrapped module executed inside the context.
|
48
|
+
context : dict
|
49
|
+
Environment arguments applied to the wrapped module.
|
50
|
+
|
51
|
+
Examples
|
52
|
+
--------
|
53
|
+
.. code-block:: python
|
54
|
+
|
55
|
+
>>> import brainstate
|
56
|
+
>>> from brainstate.nn import EnvironContext
|
57
|
+
>>> wrapped = EnvironContext(layer, fit=True)
|
58
|
+
>>> result = wrapped.update(inputs)
|
59
|
+
"""
|
60
|
+
|
61
|
+
def __init__(self, layer: Module, **context):
|
62
|
+
"""Initialize the wrapper with a module and environment arguments.
|
63
|
+
|
64
|
+
Parameters
|
65
|
+
----------
|
66
|
+
layer : Module
|
67
|
+
Module executed inside the environment context.
|
68
|
+
**context
|
69
|
+
Keyword arguments forwarded to ``brainstate.environ.context``.
|
70
|
+
"""
|
71
|
+
super().__init__()
|
72
|
+
|
73
|
+
assert isinstance(layer, Module), 'The layer must be an instance of Module.'
|
74
|
+
self.layer = layer
|
75
|
+
self.context = context
|
76
|
+
|
77
|
+
def update(self, *args, context: Dict = None, **kwargs):
|
78
|
+
"""Execute the wrapped module inside the environment context.
|
79
|
+
|
80
|
+
Parameters
|
81
|
+
----------
|
82
|
+
*args
|
83
|
+
Positional arguments forwarded to the wrapped module.
|
84
|
+
**kwargs
|
85
|
+
Keyword arguments forwarded to the wrapped module.
|
86
|
+
context: dict, optional
|
87
|
+
Additional environment settings for this call. Merged with the
|
88
|
+
stored context.
|
89
|
+
|
90
|
+
Returns
|
91
|
+
-------
|
92
|
+
Any
|
93
|
+
Result returned by the wrapped module.
|
94
|
+
"""
|
95
|
+
if context is None:
|
96
|
+
context = dict()
|
97
|
+
with environ.context(**self.context, **context):
|
98
|
+
return self.layer(*args, **kwargs)
|
99
|
+
|
100
|
+
def add_context(self, **context):
|
101
|
+
"""Add more environment settings to the wrapped module.
|
102
|
+
|
103
|
+
Parameters
|
104
|
+
----------
|
105
|
+
**context
|
106
|
+
Keyword arguments merged into the stored environment context.
|
107
|
+
"""
|
108
|
+
self.context.update(context)
|
109
|
+
|
110
|
+
|
111
|
+
def _filter_states(
|
112
|
+
module: Module,
|
113
|
+
filters: Filter | Dict[Filter, int],
|
114
|
+
) -> Dict:
|
115
|
+
"""Normalize state filter specifications for ``Module.states``.
|
116
|
+
|
117
|
+
Parameters
|
118
|
+
----------
|
119
|
+
module : Module
|
120
|
+
Module providing the states interface.
|
121
|
+
filters : Filter or dict[Filter, int]
|
122
|
+
Filters passed by the caller. Dictionary keys are filters and values
|
123
|
+
are the axes they should map over.
|
124
|
+
|
125
|
+
Returns
|
126
|
+
-------
|
127
|
+
dict[int, Any] or Any or None
|
128
|
+
Structured filters to pass to ``Module.states``. Returns ``None`` when
|
129
|
+
no filtering is requested.
|
130
|
+
"""
|
131
|
+
if filters is None:
|
132
|
+
filtered_states = None
|
133
|
+
elif isinstance(filters, dict):
|
134
|
+
in_states_filter = defaultdict(list)
|
135
|
+
for filter_, axis in filters:
|
136
|
+
assert isinstance(axis, int), 'The value of in_states must be the map axis, which should be an integer.'
|
137
|
+
in_states_filter[axis].append(filter_)
|
138
|
+
filtered_states = module.states(*in_states_filter.values())
|
139
|
+
in_states_axis = tuple(in_states_filter.keys())
|
140
|
+
filtered_states = {axis: states for axis, states in zip(in_states_axis, filtered_states)}
|
141
|
+
else:
|
142
|
+
filtered_states = module.states(filters)
|
143
|
+
return filtered_states
|
144
|
+
|
145
|
+
|
146
|
+
class Vmap(Module):
|
147
|
+
"""Vectorize a module with ``brainstate.transform.vmap``.
|
148
|
+
|
149
|
+
Parameters
|
150
|
+
----------
|
151
|
+
module : Module
|
152
|
+
Module to wrap with vectorized mapping.
|
153
|
+
in_axes : int or None or Sequence[Any], optional
|
154
|
+
Specification for mapping over inputs. Defaults to ``0``.
|
155
|
+
out_axes : Any, optional
|
156
|
+
Specification for mapping over outputs. Defaults to ``0``.
|
157
|
+
vmap_states : Filter or dict[Filter, int], optional
|
158
|
+
State filters to vectorize as inputs. Defaults to ``None``.
|
159
|
+
vmap_out_states : Filter or dict[Filter, int], optional
|
160
|
+
State filters to vectorize as outputs. Defaults to ``None``.
|
161
|
+
axis_name : AxisName or None, optional
|
162
|
+
Name of the axis being mapped. Defaults to ``None``.
|
163
|
+
axis_size : int or None, optional
|
164
|
+
Size of the mapped axis. Defaults to ``None``.
|
165
|
+
|
166
|
+
Examples
|
167
|
+
--------
|
168
|
+
.. code-block:: python
|
169
|
+
|
170
|
+
>>> from brainstate.nn import Vmap
|
171
|
+
>>> vmapped = Vmap(module, in_axes=0, axis_name="batch")
|
172
|
+
>>> outputs = vmapped.update(inputs)
|
173
|
+
"""
|
174
|
+
|
175
|
+
def __init__(
|
176
|
+
self,
|
177
|
+
module: Module,
|
178
|
+
in_axes: int | None | Sequence[Any] = 0,
|
179
|
+
out_axes: Any = 0,
|
180
|
+
vmap_states: Filter | Dict[Filter, int] = None,
|
181
|
+
vmap_out_states: Filter | Dict[Filter, int] = None,
|
182
|
+
axis_name: AxisName | None = None,
|
183
|
+
axis_size: int | None = None,
|
184
|
+
):
|
185
|
+
super().__init__()
|
186
|
+
|
187
|
+
# parameters
|
188
|
+
self.in_axes = in_axes
|
189
|
+
self.out_axes = out_axes
|
190
|
+
self.axis_name = axis_name
|
191
|
+
self.axis_size = axis_size
|
192
|
+
assert isinstance(module, Module), 'The module must be an instance of Module.'
|
193
|
+
self.module = module
|
194
|
+
vmap_states = _filter_states(module, vmap_states)
|
195
|
+
vmap_out_states = _filter_states(module, vmap_out_states)
|
196
|
+
|
197
|
+
@vmap(
|
198
|
+
in_axes=in_axes,
|
199
|
+
out_axes=out_axes,
|
200
|
+
in_states=vmap_states,
|
201
|
+
out_states=vmap_out_states,
|
202
|
+
axis_name=axis_name,
|
203
|
+
axis_size=axis_size,
|
204
|
+
)
|
205
|
+
def vmap_run(*args, **kwargs):
|
206
|
+
return module(*args, **kwargs)
|
207
|
+
|
208
|
+
# vmapped module
|
209
|
+
self.vmapped_fn = vmap_run
|
210
|
+
|
211
|
+
def update(self, *args, **kwargs):
|
212
|
+
"""Execute the vmapped module with the given arguments.
|
213
|
+
|
214
|
+
Parameters
|
215
|
+
----------
|
216
|
+
*args
|
217
|
+
Positional arguments forwarded to the vmapped module.
|
218
|
+
**kwargs
|
219
|
+
Keyword arguments forwarded to the vmapped module.
|
220
|
+
|
221
|
+
Returns
|
222
|
+
-------
|
223
|
+
Any
|
224
|
+
Result of executing the vmapped module.
|
225
|
+
"""
|
226
|
+
return self.vmapped_fn(*args, **kwargs)
|