Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

Agjax - jax wrapper for autograd-differentiable functions.

Docs Continuous integration PyPI version

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))

Metadata

Release files for agjax 0.3.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for agjax 0.3.7
File Size Uploaded Provenance Actions
agjax-0.3.7.tar.gz 11.3 kB Without provenance Details for agjax-0.3.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for agjax 0.3.7
File Interpreter ABI Platform Actions
agjax-0.3.7-py3-none-any.whl Python 3 none any Details for agjax-0.3.7-py3-none-any.whl

Total release size: 21.1 kB

Release files / agjax-0.3.7.tar.gz

Download URL agjax-0.3.7.tar.gz
Size 11.3 kB
Tags Source
SHA-256 checksum
How to use checksums
89f0060e964896319863bb67deaa34423a2602536b209f110685220c122ffb53
BLAKE2b-256 checksum
How to use checksums
f6c1d18527d79dc55332d0b50c37942453a5ee1ac29bc1256da00aa77a94ce82
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.8

Release files / agjax-0.3.7-py3-none-any.whl

Download URL agjax-0.3.7-py3-none-any.whl
Size 9.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eb6eb0c5222ec3f2210df60f7b806c5222f8ad3f1d4a98c9ccbdfdf3b801f57c
BLAKE2b-256 checksum
How to use checksums
d3657be6f353c714271068f896e68e23940d163eb3f20b423a25fb22075447d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.8

Security for agjax 0.3.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page