forked from jax-ml/jax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_util_test.py
72 lines (63 loc) · 2.39 KB
/
api_util_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Copyright 2020 The JAX Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for jax.api_util."""
import itertools as it
from absl.testing import absltest
from absl.testing import parameterized
from jax._src import api_util
from jax import numpy as jnp
from jax._src import test_util as jtu
from jax import config
config.parse_flags_with_absl()
class ApiUtilTest(jtu.JaxTestCase):
def test_donation_vector(self):
params = {"a": jnp.ones([]), "b": jnp.ones([])}
state = {"c": jnp.ones([]), "d": jnp.ones([])}
x = jnp.ones([])
args = params, state, x
for size in range(4):
for donate_argnums in it.permutations((0, 1, 2), size):
for kwargs in ({}, {"a": x}):
expected = ()
expected += (True, True) if 0 in donate_argnums else (False, False)
expected += (True, True) if 1 in donate_argnums else (False, False)
expected += (True,) if 2 in donate_argnums else (False,)
if kwargs:
expected += (False,)
self.assertEqual(
expected,
api_util.donation_vector(donate_argnums, (), args, kwargs))
@parameterized.parameters(
((0,), (0,)),
((0, 1), (1, 2)),
((0, 1, 2), (0, 1, 2)),
)
def test_rebase_donate_argnums_rejects_overlapping(self, donate, static):
with self.assertRaisesRegex(ValueError, "cannot intersect"):
api_util.rebase_donate_argnums(donate, static)
@parameterized.parameters(
((), (), ()),
((), (1, 2, 3), ()),
((0,), (2, 3), (0,)),
((0, 1), (2, 3), (0, 1)),
((2, 3), (0, 1), (0, 1)),
((3, 2), (1, 0), (0, 1)),
((3,), (0, 1), (1,)),
((3, 3, 3,), (0, 1), (1,)),
)
def test_rebase_donate_argnums(self, donate, static, expected):
self.assertEqual(expected,
api_util.rebase_donate_argnums(donate, static))
if __name__ == "__main__":
absltest.main(testLoader=jtu.JaxTestLoader())