8

I have some questions about using the torch.multiprocessing module. Let’s say I have a torch.nn.Module called model and I call model.share_memory() on it.

What happens if two threads call the forward(), i.e. model(input) at the same time? Is it safe? Or should I use Lock mechanisms to be sure that model is not accessed at the same time by multiple threads? Similarly, what happens if two or more threads have an optimizer working on model.parameters() and they call optimizer.step() at the same time?

I ask these questions because I often see the optimizer.step() being called on shared models without lock mechanisms (i.e. in RL implementations of A3C or ACER) and I wonder if it is a safe thing to do.

Federico Taschin
  • 2,027
  • 3
  • 15
  • 28

1 Answers1

1

It doesn't have to be safe, since they are running asynchronously not in parallel. Quoting from the docs,

Using torch.multiprocessing, it is possible to train a model asynchronously, with parameters either shared all the time, or being periodically synchronized. In the first case, we recommend sending over the whole model object, while in the latter, we advise to only send the state_dict().

ndrwnaguib
  • 5,623
  • 3
  • 28
  • 51