The scope of `torch.no_grad` and `torch.inference_mode`

How deep is the scope of torch.no_grad and torch.inference_mode?

At the following code:

with torch.no_grad():
    y = my_fun(my_tensor)

Will it affect my_fun? Or must I add @torch.no_grad in its definition?
What if tensors are generated within my_fun?

I assume the answer torch.inference_mode will hold for as well.

The context is applied to all methods invoked inside it unless a new context manager is used (at a nested level) changing the behavior.

1 Like