Differentiation Core
Use this sub-skill when a task is about Autograd's core differentiation operators: scalar gradients, Jacobians, directional derivatives, Hessians, VJP/JVP pullbacks, generalized Gauss-Newton products, complex holomorphic derivatives, checkpointing, or numerical gradient checks.
Trigger examples
- "I need
gradorvalue_and_gradfor a scalar loss." - "I need
jacobianorelementwise_gradfor a vector-valued output." - "I need a Hessian-vector product,
make_vjp, ormake_jvp." - "I want to verify a derivative with
check_grads." - "My function has
if/whilebranches and I want to know whether Autograd can still differentiate it."
Read first
references/api-reference.mdfor operator selection, return conventions, and mode relationships.references/workflows.mdfor scalar-loss, vector-output, higher-order, complex, and gradient-checking recipes.references/troubleshooting.mdfor scalar-output errors, argnum confusion, shape mismatches, holomorphic warnings, and checkpoint tradeoffs.scripts/differentiation_smoke.pyfor a runnable smoke that exercises the core operators without plotting.
Boundary routing
- Wrapper behavior, NumPy/SciPy support,
A.dot(B)/ in-place / container pitfalls, and optional dependency questions belong in../numpy-scipy-primitives/SKILL.md. primitive,defvjp,defjvp, and custom VJP/JVP mechanics belong in../extend-primitives/SKILL.md.- Optimizer wiring, flattening, and
fixed_pointbelong in../optimization-workflows/SKILL.md.
How to choose an operator
- Use reverse mode when the output is a real scalar and the input is large:
grad,value_and_grad, ormake_vjp. - Use forward mode when the input is scalar or you want one directional derivative:
derivormake_jvp. - Use
jacobianwhen you need the full output-to-input sensitivity tensor. - Use
elementwise_gradwhen the function is real-valued and elementwise so the Jacobian is diagonal or column-summed. - Use
hessian,hessian_tensor_product,hessian_vector_product, ormake_ggnvpwhen you need curvature without writing the matrix explicitly. - Use
holomorphic_gradonly for complex-holomorphic functions. - Use
checkpointwhen the function is deep and memory is the bottleneck. - Use
grad_and_auxwhen the function returns a scalar loss plus extra side information.
Core rules
gradandvalue_and_gradrequire a real scalar output.jacobianworks on scalar or array outputs and returns a tensor with output shape first and input shape last.elementwise_gradis not a general Jacobian helper; it returns the column-sum/diagonal behavior used for elementwise real functions.- Control flow is transparent to Autograd: the executed Python path matters, not the unexecuted branches.
make_vjpexposes the reverse-mode pullback;make_jvpexposes the forward-mode pushforward.- Higher-order derivatives are usually formed by composing these operators.
grad_and_auxdifferentiates only the first output and returns the auxiliary payload unchanged.checkpointrecomputes forward intermediates during the backward pass to trade time for memory.
Typical route
- Start with
gradfor a scalar loss. - If the output is vector-valued, switch to
jacobianorelementwise_grad. - If you need both the value and the gradient, use
value_and_grad. - If you need a directional derivative, use
derivormake_jvp. - If you need a pullback from output cotangents, use
make_vjp. - If you need curvature information, use
hessianor a Hessian-vector product helper. - If the function is complex-valued and holomorphic, use
holomorphic_grad. - If the function is deep and memory-bound, wrap it with
checkpoint. - Validate the chosen operator with
check_gradsbefore trusting a new workflow.
Reliability notes
- Use
check_gradsfor a single function andcombo_checkwhen you want the same assertion to cover several positional or keyword combinations. - When a derivative shape looks wrong, inspect the primal input and output shapes before changing the operator.
- When the function is intentionally branchy, choose a test point away from the branch threshold.
- When the failure message names a missing VJP or JVP rule for a primitive, route to
../extend-primitives/SKILL.mdinstead of trying to solve it here.
What this sub-skill does not cover
- Built-in NumPy/SciPy wrapper behavior and compatibility traps live in
../numpy-scipy-primitives/SKILL.md. - Defining new primitives or VJP/JVP rules lives in
../extend-primitives/SKILL.md. - Optimizer flattening and structured parameter updates live in
../optimization-workflows/SKILL.md.
Fastest sanity check
Run scripts/differentiation_smoke.py when you need a quick end-to-end check that the bundled differentiation guidance still matches the installed package.