Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Function

torch.no_grad

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.

How it works

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.

Syntax

torch.no_grad()

Key points

  • It temporarily disables gradient calculation within its scope.
  • Use it for inference when the computation will not need backward().
  • Avoiding autograd tracking can reduce memory use.
  • Results of ordinary operations do not require gradients even if their inputs do.
  • The same behavior can be applied to a function with decorator syntax.

Common mistakes

  • Factory functions that accept requires_grad can still create tensors requiring gradients inside the context.
  • The setting applies only to the current thread, so work executed in another thread is unaffected.
  • no_grad disables reverse-mode gradient tracking, not forward-mode automatic differentiation.

Example: Disable gradient tracking for inference

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)  # True

Use 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.

When to use it

Inference

Run evaluation or prediction without building autograd history.

Gradient-free utility functions

Decorate a helper whose operations should never track gradients.

Memory-conscious forward passes

Limit memory use during forward-only computation.

Interview questions

QWhat does torch.no_grad do, and why use it?

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.

QCan torch.no_grad be used as a decorator?

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.

QDoes torch.no_grad disable forward-mode AD?

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_grad
  • torch.set_grad_enabled
  • torch.inference_mode
  • torch.is_grad_enabled
  • Tensor.backward

Source: no_grad — PyTorch 2.14 documentation. Checked 2026-10-15.