Coloring¶
Sparsity Detection and Coloring¶
asdex.jacobian_coloring(f, input_shape, *, mode=None, symmetric=False, postprocess=False)
¶
Detect Jacobian sparsity and color in one step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable
|
Function taking an array and returning an array. |
required |
input_shape
|
int | tuple[int, ...]
|
Shape of the input array. |
required |
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.hessian_coloring(f, input_shape, *, mode=None, symmetric=True, postprocess=False)
¶
Detect Hessian sparsity and color in one step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable
|
Scalar-valued function taking an array. |
required |
input_shape
|
int | tuple[int, ...]
|
Shape of the input array. |
required |
mode
|
HessianMode | None
|
AD composition strategy for Hessian-vector products.
|
None
|
symmetric
|
bool
|
Whether to use symmetric (star) coloring. Defaults to True (exploits H = H^T for fewer colors). |
True
|
postprocess
|
bool
|
Only read when |
False
|
Returns:
| Type | Description |
|---|---|
ColoredPattern
|
A |
Coloring Known Sparsity Patterns¶
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 |
asdex.hessian_coloring_from_sparsity(sparsity, *, mode=None, symmetric=True, postprocess=False)
¶
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.
|
None
|
symmetric
|
bool
|
Whether to use symmetric (star) coloring. Defaults to True (exploits Hessian symmetry for fewer colors). |
True
|
postprocess
|
bool
|
Only read when |
False
|
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
|
SparsityPattern of shape (m, n) representing the Jacobian sparsity pattern |
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
|
SparsityPattern of shape (m, n) representing the Jacobian sparsity pattern |
required |
Returns:
| Type | Description |
|---|---|
tuple[NDArray[int32], int]
|
Tuple of (colors, num_colors) where:
|
asdex.color_symmetric(sparsity, *, postprocess=False, 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
|
SparsityPattern of shape |
required |
postprocess
|
bool
|
If |
False
|
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 |