Differentiation¶
These functions construct derivatives directly from symbolic expressions. For the usual modelling
interface, including derivatives of a named Function, see Building functions.
The derivatives guide explains gradients, Jacobians, and Hessians.
The result can be any Expr. Each wrt must be an independent input created by sc.sym or a
Function declaration. Computed expressions, including slices and reshapes, and constants raise
ValueError. See Differentiation inputs for
examples and for declaring the inputs of a partial derivative.
Modes¶
A Jacobian-vector product, jvp, computes a directional derivative without constructing the full
Jacobian. A vector-Jacobian product, vjp, propagates output weights back to the inputs.
jvp_many handles several directions together.
jvp ¶
Forward-mode derivative: J(expr, wrt) @ seed, with seed shaped like wrt.
wrt must be an independent input from sc.sym or a Function declaration.
jvp_many ¶
Forward mode over seeds shaped (nseed, *wrt.shape), returning (nseed, *expr.shape).
wrt must be an independent input from sc.sym or a Function declaration. One traversal
shares primal work across all seeds. A missing derivative rule raises.
vjp ¶
Reverse-mode derivative: one adjoint per entry of wrts, seeded by cotangents.
Each cotangent is shaped like its output. One sweep computes the derivative of a scalar with
respect to every requested input at once, which is why gradients go through here.
Every entry of wrts must be an independent input from sc.sym or a Function declaration.
Results follow wrts order.
Holding a value fixed in derivatives¶
stop_gradient ¶
Return value unchanged in evaluation and treat it as a constant in derivatives.
Forward and reverse derivatives through this expression are zero, as is its Jacobian sparsity.
Other uses of value still contribute their derivatives. Its generated evaluation C is unchanged.
Source code in src/scaly/ir/expr.py
Whole derivatives¶
jacobian ¶
Dense Jacobian d expr / d wrt, shape (expr.size, wrt.size).
wrt must be an independent input from sc.sym or a Function declaration.
Column j is the derivative with respect to wrt[j]. Computed by pushing the whole
identity through forward mode in one batched pass, then simplifying.
gradient ¶
Gradient of a scalar expr with respect to wrt, as one reverse sweep.
wrt must be an independent input from sc.sym or a Function declaration.
Source code in src/scaly/ad/derivatives.py
hessian ¶
Second derivatives of a scalar expr: the Jacobian of its gradient.
wrt must be an independent input from sc.sym or a Function declaration.
finite_difference ¶
Approximate the Jacobian of the numerical fun at x by central differences.
Returns shape (output size, x.size). Useful as an independent check of a symbolic derivative.
Source code in src/scaly/ad/derivatives.py
Sparsity¶
A sparsity pattern identifies entries that may be nonzero. Coloring groups derivative directions that can be evaluated together without mixing their results. See the sparsity guide for the storage format and examples.
jacobian_sparsity ¶
Estimate structural sparsity of d vec(expr) / d vec(wrt).
wrt must be an independent input from sc.sym or a Function declaration.
This is purely symbolic: it tracks element dependencies through the graph without using
numerical values. It is conservative for nonsmooth elementwise ops and block ops, but exact
for the structural/arithmetic subset currently implemented here.
Source code in src/scaly/ad/sparsity.py
column_coloring ¶
Assign each column a color, greedily, so no two columns sharing a row get the same one.
Columns of one color can be recovered from a single forward pass, so the number of colors is the number of passes a compact Jacobian costs.
Source code in src/scaly/ad/sparsity.py
star_coloring ¶
Greedily star-color a square sparsity pattern in column order.
The pattern is treated as an undirected graph. A valid coloring is proper, and no simple path of three edges has only two colors. This is the coloring needed to recover a symmetric Hessian from compressed forward products. It is deliberately not distance-2 coloring.
Source code in src/scaly/ad/sparsity.py
color_groups ¶
Invert a coloring into the column indices belonging to each color.
Source code in src/scaly/ad/sparsity.py
Sparse derivatives¶
sparse_jacobian ¶
Sparse Jacobian: a sparse tensor of shape (expr.size, wrt.size) storing only its structural nonzeros.
wrt must be an independent input from sc.sym or a Function declaration.
Takes the structured path when expr is a mapped loop (or a concatenation of them) over
wrt or a constant gather of it. It colors the callee's small local pattern instead of the
whole matrix, so the cost tracks the callee rather than the iteration count. Otherwise falls back
to coloring the global pattern.
Source code in src/scaly/ad/sparse.py
sparse_hessian ¶
Sparse Hessian of a scalar expression, a sparse tensor of shape (wrt.size, wrt.size).
wrt must be an independent input from sc.sym or a Function declaration. triangle
selects the entries stored: "full" keeps the whole symmetric pattern, "lower" and
"upper" keep one triangle with the diagonal. All of them come from one star-colored JVP batch.
sparse_jacobian_colored ¶
Sparse Jacobian from graph-colored compressed JVPs over the whole pattern.
wrt must be an independent input from sc.sym or a Function declaration.
sparse_jacobian_reference ¶
Reference sparse Jacobian path: build the dense Jacobian and keep its structural nonzeros.
wrt must be an independent input from sc.sym or a Function declaration.