Jit-able runtime assertions for jax in numpy style.
Project description
testax provides runtime assertions for JAX through the testing interface familiar to NumPy users.
>>> import jax >>> from jax import numpy as jnp >>> import testax >>> >>> def safe_log(x): ... testax.assert_array_less(0, x) ... return jnp.log(x) >>> >>> safe_log(jnp.arange(2)) Traceback (most recent call last): ... jax._src.checkify.JaxRuntimeError: Arrays are not less-ordered <BLANKLINE> Mismatched elements: 1 / 2 (50%) Max absolute difference: 1 Max relative difference: 1 x: Array(0, dtype=int32, weak_type=True) y: Array([0, 1], dtype=int32)
testax assertions are jit
-able, although errors need to be functionalized to conform to JAX’s requirement that functions are pure and do not have side effects (see the checkify
guide for details). In short, a checkify
-d function returns a tuple (error, value)
. The first element is an error that may have occurred, and the second is the return value of the original function.
>>> jitted = jax.jit(safe_log) >>> checkified = testax.checkify(jitted) >>> error, y = checkified(jnp.arange(2)) >>> error.throw() Traceback (most recent call last): ... jax._src.checkify.JaxRuntimeError: Arrays are not less-ordered <BLANKLINE> Mismatched elements: 1 / 2 (50%) Max absolute difference: 1 Max relative difference: 1 x: Array(0, dtype=int32, weak_type=True) y: Array([0, 1], dtype=int32) >>> y Array([-inf, 0.], dtype=float32)
Installation
testax is pip-installable and can be installed by running
pip install testax
Interface
testax mirrors the testing interface familiar to NumPy users, such as assert_allclose
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file testax-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: testax-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb12effbaf68a5b650e8d832bda3606dbe18c4e63aa67e2948ef4e6ea5167990 |
|
MD5 | 8001d9db06375a0aa5d6342719e799b1 |
|
BLAKE2b-256 | 7f3226900c5e740fb82564716b82d589ffb43803be2d5058f2902f26ff924281 |