(actual, desired, atol=1e-6, rtol=1e-3, err_msg="")
| 69 | |
| 70 | |
| 71 | def assert_allclose(actual, desired, atol=1e-6, rtol=1e-3, err_msg=""): |
| 72 | # jax.numpy.asarray no longer accepts None as an input as of Jax >= 0.6.0 |
| 73 | # Adding a manual check and exception to prevent test failure |
| 74 | if actual is None and desired is None: |
| 75 | return |
| 76 | if actual is None or desired is None: |
| 77 | raise ValueError( |
| 78 | f"Actual={actual} and desired={desired}. Either actual and desired must be None" |
| 79 | "or neither should be None" |
| 80 | ) |
| 81 | |
| 82 | actual = jnp.asarray(actual).astype(np.float32) |
| 83 | desired = jnp.asarray(desired).astype(np.float32) |
| 84 | # Checks if 'actual' and 'desired' are within (atol + rtol * abs(desired)). |
| 85 | diff: np.ndarray = np.abs(actual - desired) |
| 86 | if diff.size > 0: |
| 87 | diff = diff.max() |
| 88 | np.testing.assert_allclose( |
| 89 | actual, |
| 90 | desired, |
| 91 | atol=atol, |
| 92 | rtol=rtol, |
| 93 | err_msg=f"{err_msg}: {diff}.\nactual={actual}\ndesired={desired}", |
| 94 | ) |
| 95 | |
| 96 | |
| 97 | def assert_not_allclose(actual, desired, atol=1e-6, rtol=1e-3): |
no test coverage detected