rocm_jax/tests/util_test.py
Peter Hawkins 74384e6a87 Add a C++ safe_zip implementation.
Benchmark results on my workstation:
```
name                                 old cpu/op   new cpu/op   delta
safe_zip/arg_lengths:0/num_args:1    1.22µs ± 1%  0.28µs ± 8%  -77.33%  (p=0.008 n=5+5)
safe_zip/arg_lengths:1/num_args:1    1.28µs ± 1%  0.34µs ± 6%  -73.18%  (p=0.008 n=5+5)
safe_zip/arg_lengths:2/num_args:1    1.28µs ± 1%  0.38µs ± 5%  -70.26%  (p=0.008 n=5+5)
safe_zip/arg_lengths:5/num_args:1    1.38µs ± 1%  0.51µs ± 3%  -63.26%  (p=0.008 n=5+5)
safe_zip/arg_lengths:10/num_args:1   1.61µs ± 1%  0.69µs ± 3%  -56.93%  (p=0.008 n=5+5)
safe_zip/arg_lengths:100/num_args:1  5.39µs ± 1%  3.83µs ± 2%  -29.03%  (p=0.008 n=5+5)
safe_zip/arg_lengths:0/num_args:2    1.46µs ± 1%  0.32µs ± 4%  -78.30%  (p=0.008 n=5+5)
safe_zip/arg_lengths:1/num_args:2    1.52µs ± 1%  0.39µs ± 4%  -74.20%  (p=0.008 n=5+5)
safe_zip/arg_lengths:2/num_args:2    1.53µs ± 1%  0.44µs ± 4%  -71.38%  (p=0.008 n=5+5)
safe_zip/arg_lengths:5/num_args:2    1.66µs ± 2%  0.60µs ± 3%  -63.96%  (p=0.008 n=5+5)
safe_zip/arg_lengths:10/num_args:2   1.90µs ± 1%  0.82µs ± 3%  -56.66%  (p=0.008 n=5+5)
safe_zip/arg_lengths:100/num_args:2  6.51µs ± 1%  4.80µs ± 0%  -26.23%  (p=0.016 n=5+4)
safe_zip/arg_lengths:0/num_args:3    1.62µs ± 1%  0.36µs ± 4%  -77.95%  (p=0.008 n=5+5)
safe_zip/arg_lengths:1/num_args:3    1.68µs ± 1%  0.44µs ± 3%  -73.75%  (p=0.008 n=5+5)
safe_zip/arg_lengths:2/num_args:3    1.69µs ± 1%  0.50µs ± 3%  -70.48%  (p=0.008 n=5+5)
safe_zip/arg_lengths:5/num_args:3    1.83µs ± 1%  0.68µs ± 2%  -62.73%  (p=0.008 n=5+5)
safe_zip/arg_lengths:10/num_args:3   2.12µs ± 1%  0.96µs ± 1%  -54.71%  (p=0.008 n=5+5)
safe_zip/arg_lengths:100/num_args:3  7.34µs ± 2%  5.89µs ± 1%  -19.74%  (p=0.008 n=5+5)
```

In addition, improve the length mismatch error for safe_map and define __module__ on both functions.

PiperOrigin-RevId: 523475834
2023-04-11 12:43:04 -07:00

208 lines
6.4 KiB
Python

# 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.
import operator
import unittest
from absl.testing import absltest
from jax._src import linear_util as lu
from jax._src import test_util as jtu
from jax._src import util
from jax.config import config
from jax._src.util import weakref_lru_cache
config.parse_flags_with_absl()
FLAGS = config.FLAGS
try:
from jax._src.lib import utils as jaxlib_utils
except:
jaxlib_utils = None
class UtilTest(jtu.JaxTestCase):
def test_wrapped_fun_transforms(self):
"""Test a combination of transforms."""
def f(*args, **kwargs):
"""The function to be transformed.
Scales the positional arguments by a factor.
Takes only one keyword argument, the factor to scale by."""
factor = kwargs.pop('factor', 2) # For PY2
assert not kwargs
return tuple(a * factor for a in args)
@lu.transformation_with_aux
def kw_to_positional(factor, *args, **kwargs):
"""A transformation with auxiliary output.
Turns all keyword parameters into positional ones.
On entry, append the values of the keyword arguments to the positional
arguments. On exit, take a list of results and recreate a dictionary
from the tail of the results. The auxiliary output is the list of
keyword keys.
"""
kwargs_keys = kwargs.keys()
new_args = tuple(kwargs[k] for k in kwargs_keys)
new_kwargs = dict(factor=factor)
results = yield args + new_args, new_kwargs # Yield transformed (args, kwargs)
# Assume results correspond 1:1 to the args + new_args
assert len(results) == len(args) + len(new_args)
aux_output = len(new_args)
yield (results[0:len(args)],
dict(zip(kwargs_keys, results[len(args):]))), aux_output
wf = lu.wrap_init(f) # Wraps `f` as a `WrappedFun`.
wf, out_thunk = kw_to_positional(wf, 2)
# Call the transformed function.
scaled_positional, scaled_kwargs = wf.call_wrapped(1, 2, three=3, four=4)
self.assertEqual((2, 4), scaled_positional)
self.assertEqual(dict(three=6, four=8), scaled_kwargs)
self.assertEqual(2, out_thunk())
def test_weakref_lru_cache(self):
@weakref_lru_cache
def example_cached_fn(key):
return object()
class Key:
def __init__(self):
# Make a GC loop.
self.ref_loop = [self]
stable_keys = [Key() for _ in range(2049)]
for i in range(10000):
example_cached_fn(stable_keys[i % len(stable_keys)])
example_cached_fn(Key())
def test_weakref_lru_cache_asan_problem(self):
@weakref_lru_cache
def reference_loop_generator(x):
return x
for _ in range(4097):
reference_loop_generator(lambda x: x)
class SafeMapTest(jtu.JaxTestCase):
def test_safe_map(self):
def unreachable(*args, **kwargs):
raise RuntimeError("unreachable")
self.assertEqual([], util.safe_map(unreachable, []))
self.assertEqual([], util.safe_map(unreachable, (), []))
self.assertEqual([], util.safe_map(unreachable, [], [], []))
self.assertEqual([], util.safe_map(unreachable, [], iter([]), [], []))
def double(x):
return x * 2
self.assertEqual([14], util.safe_map(double, (7,)))
self.assertEqual([0, 2, 4, 6], util.safe_map(double, range(4)))
def make_tuple(*args):
return args
self.assertEqual(
[(0, 4), (1, 5), (2, 6), (3, 7)],
util.safe_map(make_tuple, range(4), range(4, 8)),
)
@unittest.skipIf(not hasattr(jaxlib_utils, 'safe_map'),
"requires jaxlib 0.4.9")
def test_safe_map_errors(self):
with self.assertRaisesRegex(
TypeError, "safe_map requires at least 2 arguments"
):
util.safe_map()
with self.assertRaisesRegex(
TypeError, "safe_map requires at least 2 arguments"
):
util.safe_map(lambda x: x)
with self.assertRaisesRegex(TypeError, "'int' object is not callable"):
util.safe_map(7, range(6))
def error(*args, **kwargs):
raise RuntimeError("hello")
with self.assertRaisesRegex(RuntimeError, "hello"):
util.safe_map(error, range(6))
with self.assertRaisesRegex(
ValueError, r"safe_map\(\) argument 2 is longer than argument 1"
):
util.safe_map(operator.add, range(3), range(4))
with self.assertRaisesRegex(
ValueError, r"safe_map\(\) argument 2 is shorter than argument 1"
):
util.safe_map(operator.add, range(7), range(2))
with self.assertRaisesRegex(
ValueError, r"safe_map\(\) argument 2 is longer than argument 1"
):
util.safe_map(operator.add, (), range(3))
class SafeZipTest(jtu.JaxTestCase):
def test_safe_zip(self):
self.assertEqual([], util.safe_zip([]))
self.assertEqual([], util.safe_zip((), []))
self.assertEqual([], util.safe_zip([], [], []))
self.assertEqual([], util.safe_zip([], iter([]), [], []))
self.assertEqual([(7,)], util.safe_zip((7,)))
self.assertEqual([(0,), (1,), (2,), (3,)], util.safe_zip(range(4)))
self.assertEqual(
[(0, 4), (1, 5), (2, 6), (3, 7)],
util.safe_zip(range(4), range(4, 8)),
)
@unittest.skipIf(not hasattr(jaxlib_utils, 'safe_zip'),
"requires jaxlib 0.4.9")
def test_safe_zip_errors(self):
with self.assertRaisesRegex(
TypeError, "safe_zip requires at least 1 argument"
):
util.safe_zip()
with self.assertRaisesRegex(
TypeError, "'function' object is not iterable"
):
util.safe_zip(lambda x: x)
with self.assertRaisesRegex(
ValueError, r"safe_zip\(\) argument 2 is longer than argument 1"
):
util.safe_zip(range(3), range(4))
with self.assertRaisesRegex(
ValueError, r"safe_zip\(\) argument 2 is shorter than argument 1"
):
util.safe_zip(range(7), range(2))
with self.assertRaisesRegex(
ValueError, r"safe_zip\(\) argument 2 is longer than argument 1"
):
util.safe_zip((), range(3))
if __name__ == "__main__":
absltest.main(testLoader=jtu.JaxTestLoader())