|
| 1 | +"""Tests for global_circulation.structs.""" |
| 2 | + |
| 3 | +from typing import Union |
| 4 | + |
| 5 | +from absl.testing import absltest |
| 6 | +from absl.testing import parameterized |
| 7 | + |
| 8 | +import jax |
| 9 | +import jax.numpy as jnp |
| 10 | +import numpy as np |
| 11 | +import tree_math |
| 12 | + |
| 13 | +ArrayLike = Union[jnp.ndarray, np.ndarray, float] |
| 14 | + |
| 15 | + |
| 16 | +@tree_math.struct |
| 17 | +class TestStruct: |
| 18 | + a: ArrayLike |
| 19 | + b: ArrayLike |
| 20 | + |
| 21 | + |
| 22 | +class StructsTest(parameterized.TestCase): |
| 23 | + |
| 24 | + @parameterized.named_parameters( |
| 25 | + dict(testcase_name='Scalars', x=TestStruct(1., 2.)), |
| 26 | + dict(testcase_name='Arrays', x=TestStruct(np.eye(10), np.ones([3, 4, 5]))) |
| 27 | + ) |
| 28 | + def testFlattenUnflatten(self, x): |
| 29 | + leaves, structure = jax.tree_flatten(x) |
| 30 | + y = jax.tree_unflatten(structure, leaves) |
| 31 | + np.testing.assert_allclose(x.a, y.a) |
| 32 | + np.testing.assert_allclose(x.b, y.b) |
| 33 | + |
| 34 | + @parameterized.named_parameters( |
| 35 | + dict(testcase_name='Addition', |
| 36 | + x=TestStruct(1., 2.), |
| 37 | + y=TestStruct(3., 4.), |
| 38 | + operation=lambda x, y: x + y, |
| 39 | + expected=TestStruct(4., 6.)), |
| 40 | + dict(testcase_name='Subtraction', |
| 41 | + x=TestStruct(1., 2.), |
| 42 | + y=TestStruct(3., 4.), |
| 43 | + operation=lambda x, y: x - y, |
| 44 | + expected=TestStruct(-2., -2.)), |
| 45 | + dict(testcase_name='Multiplication', |
| 46 | + x=TestStruct(1., 2.), |
| 47 | + y=TestStruct(3., 4.), |
| 48 | + operation=lambda x, y: x * y, |
| 49 | + expected=TestStruct(3., 8.)), |
| 50 | + dict(testcase_name='Division', |
| 51 | + x=TestStruct(1., 2.), |
| 52 | + y=TestStruct(3., 4.), |
| 53 | + operation=lambda x, y: x / y, |
| 54 | + expected=TestStruct(1 / 3, 2 / 4)), |
| 55 | + ) |
| 56 | + def testInfixOperator(self, x, y, operation, expected): |
| 57 | + actual = operation(x, y) |
| 58 | + np.testing.assert_allclose(expected.a, actual.a) |
| 59 | + np.testing.assert_allclose(expected.b, actual.b) |
| 60 | + |
| 61 | + @parameterized.named_parameters( |
| 62 | + dict(testcase_name='Product', |
| 63 | + x=TestStruct(1., 2.), |
| 64 | + operation=lambda x: x.a * x.b, |
| 65 | + expected=TestStruct(2., 1.)), |
| 66 | + dict(testcase_name='SquaredNorm', |
| 67 | + x=TestStruct(1., 2.), |
| 68 | + operation=lambda x: x.a**2 + x.b**2, |
| 69 | + expected=TestStruct(2., 4.)), |
| 70 | + ) |
| 71 | + def testGrad(self, x, operation, expected): |
| 72 | + actual = jax.grad(operation)(x) |
| 73 | + np.testing.assert_allclose(expected.a, actual.a) |
| 74 | + np.testing.assert_allclose(expected.b, actual.b) |
| 75 | + |
| 76 | + @parameterized.named_parameters( |
| 77 | + dict(testcase_name='Sum', |
| 78 | + x=TestStruct(1., 2.), |
| 79 | + y=TestStruct(3., 4.), |
| 80 | + operation=lambda x, y: 3 * x + 2 * y), |
| 81 | + dict(testcase_name='Product', |
| 82 | + x=TestStruct(1., 2.), |
| 83 | + y=TestStruct(3., 4.), |
| 84 | + operation=lambda x, y: x * y), |
| 85 | + ) |
| 86 | + def testJit(self, x, y, operation): |
| 87 | + jitted = jax.jit(operation)(x, y) |
| 88 | + unjitted = operation(x, y) |
| 89 | + np.testing.assert_allclose(jitted.a, unjitted.a) |
| 90 | + np.testing.assert_allclose(jitted.b, unjitted.b) |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == '__main__': |
| 94 | + absltest.main() |
0 commit comments