Coloring¶
Sparsity Detection and Coloring¶
asdex.jacobian_coloring(f, *args, argnums=_DEFAULT_ARGNUMS, has_aux=_DEFAULT_HAS_AUX, mode=_DEFAULT_MODE, symmetric=_DEFAULT_SYMMETRIC_JACOBIAN, postprocess=_DEFAULT_POSTPROCESS, **kwargs)
¶
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.
Defaults to |
_DEFAULT_ARGNUMS
|
has_aux
|
bool
|
Whether |
_DEFAULT_HAS_AUX
|
mode
|
JacobianMode | None
|
AD mode.
|
_DEFAULT_MODE
|
symmetric
|
bool
|
Whether to use symmetric coloring.
Requires a square Jacobian.
Defaults to |
_DEFAULT_SYMMETRIC_JACOBIAN
|
postprocess
|
bool
|
Only read when |
_DEFAULT_POSTPROCESS
|
**kwargs
|
Any
|
Sample keyword arguments of |
{}
|
Returns:
| Type | Description |
|---|---|
ColoredPattern
|
A |
asdex.hessian_coloring(f, *args, argnums=_DEFAULT_ARGNUMS, has_aux=_DEFAULT_HAS_AUX, mode=_DEFAULT_MODE, symmetric=_DEFAULT_SYMMETRIC_HESSIAN, postprocess=_DEFAULT_POSTPROCESS, **kwargs)
¶
Detect Hessian sparsity and color in one step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable
|
Scalar-valued function whose Hessian is to be computed. |
required |
*args
|
Any
|
Sample arguments of |
()
|
argnums
|
int | Sequence[int]
|
Specifies which positional argument(s) to differentiate
with respect to.
Defaults to |
_DEFAULT_ARGNUMS
|
has_aux
|
bool
|
Whether |
_DEFAULT_HAS_AUX
|
mode
|
HessianMode | None
|
AD composition strategy for Hessian-vector products.
|
_DEFAULT_MODE
|
symmetric
|
bool
|
Whether to use symmetric coloring.
Defaults to |
_DEFAULT_SYMMETRIC_HESSIAN
|
postprocess
|
bool
|
Only read when |
_DEFAULT_POSTPROCESS
|
**kwargs
|
Any
|
Sample keyword arguments of |
{}
|
Returns:
| Type | Description |
|---|---|
ColoredPattern
|
A |
Coloring Known Sparsity Patterns¶
asdex.jacobian_coloring_from_sparsity(sparsity, *, mode=_DEFAULT_MODE, symmetric=_DEFAULT_SYMMETRIC_JACOBIAN, postprocess=_DEFAULT_POSTPROCESS)
¶
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.
|
_DEFAULT_MODE
|
symmetric
|
bool
|
Whether to use symmetric coloring.
Requires a square Jacobian.
Defaults to |
_DEFAULT_SYMMETRIC_JACOBIAN
|
postprocess
|
bool
|
Only read when |
_DEFAULT_POSTPROCESS
|
Returns:
| Type | Description |
|---|---|
ColoredPattern
|
A |
asdex.hessian_coloring_from_sparsity(sparsity, *, mode=_DEFAULT_MODE, symmetric=_DEFAULT_SYMMETRIC_HESSIAN, postprocess=_DEFAULT_POSTPROCESS)
¶
Color a sparsity pattern for sparse Hessian computation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sparsity
|
SparsityPattern | NDArray | BCOO
|
A |
required |
mode
|
HessianMode | None
|
AD composition strategy for Hessian-vector products.
|
_DEFAULT_MODE
|
symmetric
|
bool
|
Whether to use symmetric coloring.
Defaults to |
_DEFAULT_SYMMETRIC_HESSIAN
|
postprocess
|
bool
|
Only read when |
_DEFAULT_POSTPROCESS
|
Returns:
| Type | Description |
|---|---|
ColoredPattern
|
A |
Low-Level Algorithms¶
asdex.color_rows(sparsity)
¶
Greedy row-wise coloring for sparse Jacobian computation.
Assigns colors to rows such that no two rows sharing a non-zero column have the same color. This enables computing multiple Jacobian rows in a single VJP by using a combined seed vector.
Uses LargestFirst vertex ordering for fewer colors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sparsity
|
SparsityPattern
|
Jacobian sparsity pattern of shape |
required |
Returns:
| Type | Description |
|---|---|
tuple[NDArray[int32], int]
|
Tuple of (colors, num_colors) where:
|
asdex.color_cols(sparsity)
¶
Greedy column-wise coloring for sparse Jacobian computation.
Assigns colors to columns such that no two columns sharing a non-zero row have the same color. This enables computing multiple Jacobian columns in a single JVP by using a combined tangent vector.
Uses LargestFirst vertex ordering for fewer colors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sparsity
|
SparsityPattern
|
Jacobian sparsity pattern of shape |
required |
Returns:
| Type | Description |
|---|---|
tuple[NDArray[int32], int]
|
Tuple of (colors, num_colors) where:
|
asdex.color_symmetric(sparsity, *, postprocess=_DEFAULT_POSTPROCESS, forced_colors=None)
¶
Greedy symmetric coloring for sparse Hessian computation.
Implements Algorithm 4.1 from Gebremedhin et al. (2007).
A star coloring is a distance-1 coloring with the additional constraint
that every path on 4 vertices uses at least 3 colors.
Returns a :class:StarSet alongside the colors so that
Hessian decompression can use hub-based extraction.
Uses LargestFirst vertex ordering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sparsity
|
SparsityPattern
|
Hessian sparsity pattern of shape |
required |
postprocess
|
bool
|
If |
_DEFAULT_POSTPROCESS
|
forced_colors
|
NDArray[int32] | list[int] | None
|
Optional pre-computed color assignment of shape |
None
|
Returns:
| Type | Description |
|---|---|
tuple[NDArray[int32], int, StarSet]
|
Tuple
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If pattern is not square. |
InvalidColoringError
|
If |
Validation¶
asdex.check_coloring_rows(sparsity, colors)
¶
Check a row coloring: no column contains two rows with the same color.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sparsity
|
SparsityPattern
|
Jacobian sparsity pattern of shape |
required |
colors
|
NDArray[int32]
|
Row color assignment, shape |
required |
Raises:
| Type | Description |
|---|---|
InvalidColoringError
|
If the coloring is invalid. |
asdex.check_coloring_cols(sparsity, colors)
¶
Check a column coloring: no row contains two columns with the same color.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sparsity
|
SparsityPattern
|
Jacobian sparsity pattern of shape |
required |
colors
|
NDArray[int32]
|
Column color assignment, shape |
required |
Raises:
| Type | Description |
|---|---|
InvalidColoringError
|
If the coloring is invalid. |
asdex.check_coloring_symmetric(sparsity, colors)
¶
Check a star coloring: distance-1 coloring + no 2-colored path of 4 vertices.
A star coloring satisfies:
- Adjacent vertices have different colors (distance-1).
- Every path on 4 vertices uses at least 3 distinct colors (no 2-colored P4).
Vertices with color -1 (neutral, from postprocessing) are excluded
from the P4 check as they represent the absence of a color.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sparsity
|
SparsityPattern
|
Hessian sparsity pattern of shape |
required |
colors
|
NDArray[int32]
|
Vertex color assignment, shape |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If pattern is not square. |
InvalidColoringError
|
If the coloring is invalid. |
asdex.InvalidColoringError
¶
Bases: ValueError
Raised when a user-supplied coloring violates a star-coloring constraint.
See color_symmetric with forced_colors.