-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
149 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,5 @@ dependencies: | |
- pdbpp | ||
- numpy | ||
- pandas | ||
- jax | ||
- jaxlib |
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
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,45 @@ | ||
import pytest | ||
from pybaum.config import IS_JAX_INSTALLED | ||
from pybaum.registry import get_registry | ||
from pybaum.tree_util import leaf_names | ||
from pybaum.tree_util import tree_equal | ||
from pybaum.tree_util import tree_flatten | ||
from pybaum.tree_util import tree_just_flatten | ||
|
||
if IS_JAX_INSTALLED: | ||
import jax.numpy as jnp | ||
else: | ||
# run the tests with normal numpy instead | ||
import numpy as jnp | ||
|
||
|
||
@pytest.fixture | ||
def tree(): | ||
return {"a": {"b": jnp.arange(4).reshape(2, 2)}, "c": jnp.ones(2)} | ||
|
||
|
||
@pytest.fixture | ||
def flat(): | ||
return [0, 1, 2, 3, 1, 1] | ||
|
||
|
||
@pytest.fixture | ||
def registry(): | ||
return get_registry(types=["jax.numpy.ndarray", "numpy.ndarray"]) | ||
|
||
|
||
def test_tree_just_flatten_with_jax(tree, registry, flat): | ||
got = tree_just_flatten(tree, registry=registry) | ||
assert got == flat | ||
|
||
|
||
def test_tree_flatten_with_jax(tree, registry, flat): | ||
got_flat, got_treedef = tree_flatten(tree, registry=registry) | ||
assert got_flat == flat | ||
assert tree_equal(got_treedef, tree) | ||
|
||
|
||
def test_leaf_names_with_jax(tree, registry): | ||
got = leaf_names(tree, registry=registry) | ||
expected = ["a_b_0_0", "a_b_0_1", "a_b_1_0", "a_b_1_1", "c_0", "c_1"] | ||
assert got == expected |
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