- class torch.autograd.forward_ad.dual_level[source]¶
Context-manager that enables forward AD. All forward AD computation must be performed in a
dual_levelcontext appropriately enters and exit the dual level to controls the current forward AD level, which is used by default by the other functions in this API.
We currently don’t plan to support nested
dual_levelcontexts, however, so only a single forward AD level is supported. To compute higher-order forward grads, one can use
>>> x = torch.tensor() >>> x_t = torch.tensor() >>> 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.