dual_level
- class torch.autograd.forward_ad.dual_level
Context-manager for forward AD, where all forward AD computation must occur within the
dual_levelcontext.
Context-manager for forward AD, where all forward AD computation must occur within the dual_level context.
We currently don’t plan to support nested dual_level contexts, however, so
only a single forward AD level is supported. To compute higher-order
forward grads, one can use torch.func.jvp().
Example:
>>> x = torch.tensor([1])
>>> x_t = torch.tensor([1])
>>> with dual_level():
... inp = make_dual(x, x_t)
... # Do computations with inp
... out = your_fn(inp)
... _, grad = unpack_dual(out)
>>> grad is None
False
>>> # After exiting the level, the grad is deleted
>>> _, grad_after = unpack_dual(out)
>>> grad is None
True
Please see the forward-mode AD tutorial for detailed steps on how to use this API.