Inference
Run evaluation or prediction without building autograd history.
PyTorch · Function
torch.no_grad disables gradient calculation for a scope or decorated function. It is useful for inference because ordinary results stop requiring gradients and autograd can use less memory.
torch.no_grad temporarily turns off gradient calculation for ordinary operations in its scope. It is available both as a context manager and decorator, reduces memory use when gradients are unnecessary, and leaves eligible factory-created tensors with an explicitly requested requires_grad value unchanged.
torch.no_grad()import torch
x = torch.tensor([1.0], requires_grad=True)
with torch.no_grad():
y = x * 2
print(y.requires_grad) # False
@torch.no_grad()
def double(value):
return value * 2
print(double(x).requires_grad) # False
with torch.no_grad():
parameter = torch.nn.Parameter(torch.rand(2))
print(parameter.requires_grad) # TrueUse this context manager around inference or other work that must not build autograd history. It temporarily disables gradient recording for the current thread, so results from ordinary tensor operations do not require gradients; it can also be applied as a decorator.
Run evaluation or prediction without building autograd history.
Decorate a helper whose operations should never track gradients.
Limit memory use during forward-only computation.
torch.no_grad disables gradient calculation within a scope, making ordinary operation results have requires_grad=False even when inputs require gradients. It is useful for inference when backward will not be called and can reduce memory consumption.
Follow-up: How does it differ for factory functions? Factory functions that accept a requires_grad keyword argument are exempt, so an explicitly requested gradient setting remains effective.
The API works as either a context manager or a decorator. As a context manager it limits the enclosed block; as a decorator it applies the same behavior whenever the decorated function runs.
Follow-up: Is the setting process-wide? No; the documentation specifies that it is thread local and does not affect computation in other threads.
No-grad does not apply to forward-mode automatic differentiation. It controls gradient calculation for the documented mode, while forward-mode dual tensors require a different approach.
Follow-up: How can forward-mode tracking be disabled? The documentation says to unpack the dual tensors.
torch.enable_gradtorch.set_grad_enabledtorch.inference_modetorch.is_grad_enabledTensor.backward