TECHNOLOGY

JAX custom VJP: the saved residual and backward cotangent are different interfaces

In this article
THE QUICK TAKE

A correct forward value does not validate a custom derivative; the backward rule must return cotangents matching the original argument structure.

Recognize the extra mathematical implementation

A numerical layer replaces automatic reverse differentiation with a hand-written formula to control its backward calculation. The custom_vjp reference describes a decorated function that uses its custom rule under reverse-mode differentiation. Ordinary forward predictions can remain correct while the derivative implementation is wrong. Review the backward formula as another implementation of the model’s mathematics, with its own validation cases, rather than assuming the decorator merely changes execution speed.

Define what the forward rule saves

The defvjp contract separates the primal output from residual values saved for the backward rule. A residual may contain intermediates needed to evaluate the derivative; it is not automatically the same object as the prediction or upstream gradient. Save exactly the state the formula requires, with an intentional structure. Excessively retaining full inputs can enlarge the live state, while omitting a necessary intermediate can force an incorrect reconstruction later.

Match the pullback to its actual inputs

The backward rule receives saved residuals and the output cotangent. Without nondifferentiable argument declarations, its result is a tuple corresponding to the original arguments, with matching nested structures. Establish the meaning of each incoming cotangent before combining it with a derivative factor. Returning the local derivative alone ignores the upstream sensitivity and breaks the chain rule. Structured outputs also require accounting for the corresponding structured cotangent rather than silently using only a convenient component.

Select the rule family for required transformations

The custom derivative guide explains that custom_vjp is reverse-mode only when forward-mode differentiation is applied directly to that function. If the application needs direct JVP sensitivity analysis, review custom_jvp or another appropriate rule mechanism instead. Do not infer support for every transformation from a successful gradient call. Keep the set of required transformations alongside the function’s interface so a later analysis task does not discover the restriction unexpectedly.

Test derivatives independently of predictions

Compare the backward result against an independent reference on smooth representative inputs and include nontrivial upstream sensitivities. Then test the intended compilation and batching wrappers. The guides on autograd chain rules and checkpoint recomputation explain related ideas. Document any deliberately modified derivative as part of the mathematical contract. A finite forward loss proves neither that the saved residual is sufficient nor that the returned input cotangents implement the intended model update.

Earn rental income from GPUs

Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.

Explore GPU hosting
← All articles