A jax wrapper for autograd-differentiable functions.
This project has been archived.
The maintainers of this project have marked this project as archived. No new releases are expected.
Project description
Agjax -- jax wrapper for autograd-differentiable functions.
v0.3.4
Agjax allows existing code built with autograd to be used with the jax framework.
In particular, agjax.wrap_for_jax allows arbitrary autograd functions ot be differentiated using jax.grad. Several other function transformations (e.g. compilation via jax.jit) are not supported.
Meanwhile, agjax.experimental.wrap_for_jax supports grad, jit, vmap, and jacrev. However, it depends on certain under-the-hood behavior by jax, which is not guaranteed to remain unchanged. It also is more restrictive in terms of the valid function signatures of functions to be wrapped: all arguments and outputs must be convertible to valid jax types. (agjax.wrap_for_jax also supports non-jax inputs and outputs, e.g. strings.)
Installation
pip install agjax
Usage
Basic usage is as follows:
@agjax.wrap_for_jax
def fn(x, y):
return x * npa.cos(y)
jax.grad(fn, argnums=(0, 1))(1.0, 0.0)
# (Array(1., dtype=float32), Array(0., dtype=float32))
The experimental wrapper is similar, but requires that the function outputs and datatypes be specified, simiilar to jax.pure_callback.
wrapped_fn = agjax.experimental.wrap_for_jax(
lambda x, y: x * npa.cos(y),
result_shape_dtypes=jnp.ones((5,)),
)
jax.jacrev(wrapped_fn, argnums=0)(jnp.arange(5, dtype=float), jnp.arange(5, 10, dtype=float))
# [[ 0.28366217 0. 0. 0. 0. ]
# [ 0. 0.96017027 0. 0. 0. ]
# [ 0. 0. 0.75390226 0. 0. ]
# [ 0. 0. 0. -0.14550003 0. ]
# [ 0. 0. 0. 0. -0.91113025]]
Agjax wrappers are intended to be quite general, and can support functions with multiple inputs and outputs as well as functions that have nondifferentiable outputs or arguments that cannot be differentiated with respect to. These should be specified using nondiff_argnums and nondiff_outputnums arguments. In the experimental wrapper, these must still be jax-convertible types, while in the standard wrapper they may have arbitrary types.
@functools.partial(
agjax.wrap_for_jax, nondiff_argnums=(2,), nondiff_outputnums=(1,)
)
def fn(x, y, string_arg):
return x * npa.cos(y), string_arg * 2
(value, aux), grad = jax.value_and_grad(
fn, argnums=(0, 1), has_aux=True
)(1.0, 0.0, "test")
print(f"value = {value}")
print(f" aux = {aux}")
print(f" grad = {grad}")
value = 1.0
aux = testtest
grad = (Array(1., dtype=float32), Array(0., dtype=float32))
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 Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file agjax-0.3.4.tar.gz.
File metadata
- Download URL: agjax-0.3.4.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
114bb85390f64bfb35472176c38b028c5bbe475090382db633d84cb058fce382
|
|
| MD5 |
59cc54fb380c416c9aa273018a0ba380
|
|
| BLAKE2b-256 |
6d70178773d432775fbaef7bbcd8e4bc8db2236feb65d8877f8adcb42dee47b6
|
File details
Details for the file agjax-0.3.4-py3-none-any.whl.
File metadata
- Download URL: agjax-0.3.4-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ec720e48b42b6aedc3a384c9085faf9eb604166115010b4246d16c5925f312e0
|
|
| MD5 |
b090182556ea17bf67aa2cf116519f06
|
|
| BLAKE2b-256 |
10ec014a0737c4f8cfa6850b3c418e8756e6941e083d6c12ffc7f8a9049f21c5
|