Is PyTorch code blocking?

Does a call to something like loss.backwards() cause the CPU code to block?


Does 5 get printed concurrently with or after loss.backwards() is finished executing?

I tried running the following example to test it myself:

import torch
from torch import nn
net = nn.Linear(100000, 1)
loss = net(torch.ones(10000, 100000)).mean()

It’s hard to tell for me. Seems like at least the net creation is blocking.

Edit: I would also like to know the logic of this when the model is on GPU.

loss ='cuda')(torch.ones(10000, 100000).to('cuda')).mean()

Since your code is purely performed on the CPU each call would be “blocking”.
Onc you move the model to the GPU its execution will be asynchronous and the CPU will only block if you are explicitly synchronizing the code or if data is moved explicitly or implicitly to the CPU.