S-exp GPU
All pages
Docs · Function transformsMarkdown

Apply a vector-Jacobian product

(vjp function argument...) returns (values output pullback). output is the function's tensor tree. Calling pullback with a matching output-cotangent tree returns a vector of input-cotangent trees, one per argument, in argument order.

sexpgpu probe --expr '(multiple-value-bind (v pb) (vjp (lambda (x) (* x x)) (full [] 3.0)) (values v (first (pb (ones [])))))' --max-elements 100

The results are 9.0 and 6.0. Unlike grad, the function may return nonscalar tensors and structured trees. Differentiated input and output leaves must be floating tensors. Outputs are positions: cotangents match the output's structure, keys, shapes and kinds; output aliasing imposes no cotangent alias requirement. Unused inputs receive zeros. Inputs are variables: input argument positions are independent; shared leaves within one argument accumulate one shared cotangent.

For a repeated output, each position supplies its own cotangent:

(multiple-value-bind (v pb) (vjp (lambda (x) (list x x)) (full [] 3.0))
  (first (pb (list (full [] 2.0) (full [] 5.0))))) ; 7.0

A tied input leaf receives the sum of all its contributions, and the returned input tree retains that alias structure. This differs from outputs, whose positions may carry different cotangents even when they contain the same value.

The pullback retains residuals of the original call, including its random values. Calling it again, or from inside a scan, does not draw new noise or retrace the function. It is itself differentiable. Capture integer inputs outside the differentiated function. Return one list/vector/plist tree for several results; multiple return values are not an output tree.

Reporting and state declarations are refused with E-TRANSFORM-003. Invalid argument/output/cotangent trees use E-TRANSFORM-001, E-TRANSFORM-002 or E-TREE-001; E-TRANSFORM-004 names an unsupported derivative operation. See jvp for the forward-direction product.