torch.autograd.forward_ad.make_dual — PyTorch 2.7 documentation (original) (raw)

Shortcuts

torch.autograd.forward_ad.make_dual(tensor, tangent, *, level=None)[source][source]

Associate a tensor value with its tangent to create a “dual tensor” for forward AD gradient computation.

The result is a new tensor aliased to tensor with tangent embedded as an attribute as-is if it has the same storage layout or copied otherwise. The tangent attribute can be recovered with unpack_dual().

This function is backward differentiable.

Given a function f whose jacobian is J, it allows one to compute the Jacobian-vector product (jvp) between J and a given vector v as follows.

Example:

with dual_level(): ... inp = make_dual(x, v) ... out = f(inp) ... y, jvp = unpack_dual(out)

Please see the forward-mode AD tutorialfor detailed steps on how to use this API.