Jacobian¶
Jacobian Computation¶
asdex.jacobian(f, *args, argnums=0, has_aux=False, holomorphic=False, allow_int=False, mode=None, symmetric=False, output_format='bcoo')
¶
Detect sparsity, color, and return a function computing sparse Jacobians.
Combines jacobian_coloring
and jacobian_from_coloring
in one call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[..., Any]
|
Function whose Jacobian is to be computed. |
required |
*args
|
Any
|
Sample arguments of |
()
|
argnums
|
int | Sequence[int]
|
Specifies which positional argument(s) to differentiate
with respect to (default |
0
|
has_aux
|
bool
|
Whether |
False
|
holomorphic
|
bool
|
Whether |
False
|
allow_int
|
bool
|
Whether to allow differentiating with respect to
integer-valued inputs, mirroring |
False
|
mode
|
JacobianMode | None
|
AD mode.
|
None
|
symmetric
|
bool
|
Whether to use symmetric (star) coloring. Requires a square Jacobian. |
False
|
output_format
|
OutputFormat
|
Type of the output matrix.
|
'bcoo'
|
Returns:
| Type | Description |
|---|---|
Callable[..., Any]
|
A function that takes the same positional args as |
asdex.value_and_jacobian(f, *args, argnums=0, has_aux=False, holomorphic=False, allow_int=False, mode=None, symmetric=False, output_format='bcoo')
¶
Detect sparsity, color, and return a function computing value and sparse Jacobian.
Like jacobian,
but also returns the primal value f(*args)
without an extra forward pass.
Returns:
| Type | Description |
|---|---|
Callable[..., Any]
|
A function that takes the same positional args as |
asdex.jacobian_from_coloring(f, coloring, output_format='bcoo', *, has_aux=False, holomorphic=False, allow_int=False)
¶
Build a sparse Jacobian function from a pre-computed coloring.
Uses row coloring + VJPs or column coloring + JVPs, depending on which needs fewer colors.
The returned callable accepts *args, **kwargs; kwargs are forwarded
to f at call time (matching jax.jacfwd / jax.jacrev).
asdex.value_and_jacobian_from_coloring(f, coloring, output_format='bcoo', *, has_aux=False, holomorphic=False, allow_int=False)
¶
Build a function computing value and sparse Jacobian from a pre-computed coloring.
Coloring¶
asdex.jacobian_coloring(f, *args, argnums=0, has_aux=False, mode=None, symmetric=False, postprocess=False)
¶
Detect Jacobian sparsity and color in one step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable
|
Function whose Jacobian is to be computed. |
required |
*args
|
Any
|
Sample arguments of |
()
|
argnums
|
int | Sequence[int]
|
Specifies which positional argument(s) to differentiate
with respect to (default |
0
|
has_aux
|
bool
|
If |
False
|
mode
|
JacobianMode | None
|
AD mode.
|
None
|
symmetric
|
bool
|
Whether to use symmetric (star) coloring. Requires a square Jacobian. |
False
|
postprocess
|
bool
|
Only read when |
False
|
Returns:
| Type | Description |
|---|---|
ColoredPattern
|
A |
asdex.jacobian_coloring_from_sparsity(sparsity, *, mode=None, symmetric=False, postprocess=False)
¶
Color a sparsity pattern for sparse Jacobian computation.
Assigns colors so that same-colored rows (or columns) can be computed together in a single VJP (or JVP).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sparsity
|
SparsityPattern | NDArray | BCOO
|
A |
required |
mode
|
JacobianMode | None
|
AD mode.
|
None
|
symmetric
|
bool
|
Whether to use symmetric (star) coloring. Requires a square pattern. |
False
|
postprocess
|
bool
|
Only read when |
False
|
Returns:
| Type | Description |
|---|---|
ColoredPattern
|
A |
Sparsity Detection¶
asdex.jacobian_sparsity(f, *args, argnums=0, has_aux=False)
¶
Detect global Jacobian sparsity pattern for f.
Analyzes the computation graph structure directly, without evaluating any derivatives. The result is valid for all inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable
|
Function whose Jacobian sparsity pattern is to be detected. |
required |
*args
|
Any
|
Sample arguments of |
()
|
argnums
|
int | Sequence[int]
|
Specifies which positional argument(s) to differentiate
with respect to (default |
0
|
has_aux
|
bool
|
Whether |
False
|
Returns:
| Type | Description |
|---|---|
SparsityPattern
|
SparsityPattern of shape |
Verification¶
asdex.check_jacobian_correctness(f, x, coloring, *, method='matvec', num_probes=25, seed=0, rtol=None, atol=None)
¶
Verify asdex's sparse Jacobian against a JAX reference at a given input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[..., Any]
|
Function whose Jacobian is to be verified. |
required |
x
|
Any
|
Input at which to evaluate the Jacobian.
For multi-input functions (where |
required |
coloring
|
ColoredPattern
|
Pre-computed colored pattern from
:func: |
required |
method
|
Literal['matvec', 'dense']
|
Verification method.
|
'matvec'
|
num_probes
|
int
|
Number of random probe vectors (only used by |
25
|
seed
|
int
|
PRNG seed for reproducibility (only used by |
0
|
rtol
|
float | None
|
Relative tolerance for comparison.
Defaults to 1e-5 for |
None
|
atol
|
float | None
|
Absolute tolerance for comparison.
Defaults to 1e-5 for |
None
|
Raises:
| Type | Description |
|---|---|
VerificationError
|
If the sparse and reference Jacobians disagree. |
Configuration¶
asdex.JacobianMode = Literal['fwd', 'rev']
module-attribute
¶
AD mode for Jacobian computation.
"fwd" uses JVPs (forward-mode AD),
"rev" uses VJPs (reverse-mode AD).