|
| 1 | +# Copyright 2024 The JAX Authors. |
| 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 | +# https://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 | +from absl.testing import absltest |
| 16 | + |
| 17 | +import jax |
| 18 | +from jax._src import test_util as jtu |
| 19 | + |
| 20 | +from jax_ffi_example import counter |
| 21 | + |
| 22 | +jax.config.parse_flags_with_absl() |
| 23 | + |
| 24 | + |
| 25 | +class CounterTests(jtu.JaxTestCase): |
| 26 | + def setUp(self): |
| 27 | + super().setUp() |
| 28 | + if not jtu.test_device_matches(["cpu"]): |
| 29 | + self.skipTest("Unsupported platform") |
| 30 | + |
| 31 | + def test_basic(self): |
| 32 | + self.assertEqual(counter.counter(0), 0) |
| 33 | + self.assertEqual(counter.counter(0), 1) |
| 34 | + self.assertEqual(counter.counter(0), 2) |
| 35 | + self.assertEqual(counter.counter(1), 0) |
| 36 | + self.assertEqual(counter.counter(0), 3) |
| 37 | + |
| 38 | + def test_jit(self): |
| 39 | + @jax.jit |
| 40 | + def counter_fun(x): |
| 41 | + return x, counter.counter(2) |
| 42 | + |
| 43 | + self.assertEqual(counter_fun(0)[1], 0) |
| 44 | + self.assertEqual(counter_fun(0)[1], 1) |
| 45 | + |
| 46 | + # Persists across different cache hits |
| 47 | + self.assertEqual(counter_fun(1)[1], 2) |
| 48 | + |
| 49 | + # Persists after the cache is cleared |
| 50 | + counter_fun.clear_cache() |
| 51 | + self.assertEqual(counter_fun(0)[1], 3) |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + absltest.main(testLoader=jtu.JaxTestLoader()) |
0 commit comments