Calling a function in 'with torch.no_grad()' block

Hi,

Suppose I’ve a code block wrapped with torch no grad and I call a function that does inference in it, i.e,


with torch.no_grad():
    my_infer(my_model, my_data)

should I use with

torch.no_grad()

at the beginning of my_infer() too or is the outer no_grad sufficient?

1 Like

Hi,

It is a context manager. So it will enable the no_grad mode when you enter it and disable it when you leave the indentation.
So no need to do it again inside the function, it will still be activated.

3 Likes