fn
jvp
→tuplejvp(func: Callable[..., Tensor | tuple[Tensor, ...]], primals: tuple[Tensor, ...], tangents: tuple[Tensor, ...], strict: bool = False)Compute the Jacobian-vector product of func at primals.
Returns both the primal output func(*primals) and the directional
derivative — the
forward-mode contraction of the Jacobian against a tangent vector.
Forward-mode is preferred over reverse-mode when the input dimension
is small relative to the output dimension.
Parameters
funcCallableDifferentiable function returning a Tensor or tuple of Tensors.
primalstuple of TensorPoints at which to evaluate
func and its Jacobian.tangentstuple of TensorTangent vectors, each matching the shape of the corresponding
primal.
strictbool= FalseIf
True, raise when an output is independent of any input.
Default False.Returns
tuple(primals_out, tangents_out) with the same nested structure as
func(*primals).
Notes
Implemented via the exact -perturbation trick: introduce
a scalar with requires_grad=True, substitute
x + alpha * t for each primal, and read
at from
a single backward pass. No finite differences are used, so the result
is exact up to floating-point rounding.
Examples
>>> import lucid
>>> from lucid.func import jvp
>>> f = lambda x: x ** 2
>>> x = lucid.tensor([1.0, 2.0, 3.0])
>>> t = lucid.ones_like(x)
>>> y, dy = jvp(f, (x,), (t,)) # dy = 2 * x