-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [running] fix multiprocessing bugs * fix tests * [doc] update doc * update * [math] add `brainpy.math.gpu_memory_preallocation()` for controlling GPU memory preallocation * [math] `clear_buffer_memory` support to clear array and compilation both * [dyn] compatible old version of `.reset_state()` function * [setup] update installation info * [install] upgrade dependency * updates * [math] add `brainpy.math.defjvp`, support to define jvp rules for Primitive with multiple results. See examples in `test_ad_support.py` * [math] add `brainpy.math.XLACustomOp.defjvp` * [doc] upgrade `brainpy.math.defjvp` docstring
- Loading branch information
1 parent
6c599a7
commit 8c28685
Showing
10 changed files
with
423 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import functools | ||
from functools import partial | ||
|
||
from jax import tree_util | ||
from jax.core import Primitive | ||
from jax.interpreters import ad | ||
|
||
__all__ = [ | ||
'defjvp', | ||
] | ||
|
||
|
||
def defjvp(primitive, *jvp_rules): | ||
"""Define JVP rules for any JAX primitive. | ||
This function is similar to ``jax.interpreters.ad.defjvp``. | ||
However, the JAX one only supports primitive with ``multiple_results=False``. | ||
``brainpy.math.defjvp`` enables to define the independent JVP rule for | ||
each input parameter no matter ``multiple_results=False/True``. | ||
For examples, please see ``test_ad_support.py``. | ||
Args: | ||
primitive: Primitive, XLACustomOp. | ||
*jvp_rules: The JVP translation rule for each primal. | ||
""" | ||
assert isinstance(primitive, Primitive) | ||
if primitive.multiple_results: | ||
ad.primitive_jvps[primitive] = partial(_standard_jvp, jvp_rules, primitive) | ||
else: | ||
ad.primitive_jvps[primitive] = partial(ad.standard_jvp, jvp_rules, primitive) | ||
|
||
|
||
def _standard_jvp(jvp_rules, primitive: Primitive, primals, tangents, **params): | ||
assert primitive.multiple_results | ||
val_out = tuple(primitive.bind(*primals, **params)) | ||
tree = tree_util.tree_structure(val_out) | ||
tangents_out = [] | ||
for rule, t in zip(jvp_rules, tangents): | ||
if rule is not None and type(t) is not ad.Zero: | ||
r = tuple(rule(t, *primals, **params)) | ||
tangents_out.append(r) | ||
assert tree_util.tree_structure(r) == tree | ||
return val_out, functools.reduce(_add_tangents, | ||
tangents_out, | ||
tree_util.tree_map(lambda a: ad.Zero.from_value(a), val_out)) | ||
|
||
|
||
def _add_tangents(xs, ys): | ||
return tree_util.tree_map(ad.add_tangents, xs, ys, is_leaf=lambda a: isinstance(a, ad.Zero)) | ||
|
Oops, something went wrong.