# Synchronization for sharing/updating shared model state dict across multi-process

**URL:** <https://discuss.pytorch.org/t/synchronization-for-sharing-updating-shared-model-state-dict-across-multi-process/50102>\
**Category:** reinforcement-learning\
**Created:** [July 9, 2019, 9:59am UTC](https://discuss.pytorch.org/t/synchronization-for-sharing-updating-shared-model-state-dict-across-multi-process/50102 "2019-07-09T09:59:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wwiiiii](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/wwiiiii/32/6613_2.png) [@wwiiiii](https://discuss.pytorch.org/u/wwiiiii)\
**Post date:** [July 9, 2019, 9:59am UTC](https://discuss.pytorch.org/t/synchronization-for-sharing-updating-shared-model-state-dict-across-multi-process/50102/1 "2019-07-09T09:59:35Z")

</div>

Hello, while I’m doing a RL project, I need some way to share model state dict across multi-processes.

I found the [A3C implementation](https://github.com/dgriff777/rl_a3c_pytorch) which has this feature, but I have some questions about it.

1. In the above repo, the author defined model instance on CPU shared memory, and then all the sub processes also share that. In each subprocess, it has its own model instance on GPU, and it loads the state from globally shared CPU model instance without any lock.  
So my question is, Is it guranteed that it’s safe to access parameters of the model on CPU shared memory simultaneously in multiple processes without any lock or synchronization?  
I’m pretty sure it’s fine since model parameters are treated as read-only variable here, but I just want to double check it.

2. The next question is my main interest. [At here](https://github.com/dgriff777/rl_a3c_pytorch/blob/master/train.py#L104), the author simply copies the gradient of model in sub-process to shared model without any lock mechanism, and also optimization is done without synchronization. How come is it possible? Why isn’t everything messed up when multiple processes overwrite the gradient of shared model without any lock mechanism?

If I missed out some points, please let me know ☹

---

<div class="post-metadata">

**Author:** ![wwiiiii](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/wwiiiii/32/6613_2.png) [@wwiiiii](https://discuss.pytorch.org/u/wwiiiii)\
**Post date:** [July 10, 2019, 7:56am UTC](https://discuss.pytorch.org/t/synchronization-for-sharing-updating-shared-model-state-dict-across-multi-process/50102/2 "2019-07-10T07:56:49Z")

</div>

Okay, I found there’s already well documented answer at [https://pytorch.org/docs/stable/notes/multiprocessing.html](https://pytorch.org/docs/stable/notes/multiprocessing.html).  
And also with hogwild!-spirit, it’s fine to overwrite gradient from other process.

So the only thing I wonder now is that how the [updating shared model parameter](https://github.com/dgriff777/rl_a3c_pytorch/blob/master/shared_optim.py#L184) could be done without any lock mechanism. Are the `add_` or `addcdiv_` atomic operation?

---

<div class="post-metadata">

**Author:** ![Yanzzzzz](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/y/e9bcb4/32.png) [@Yanzzzzz](https://discuss.pytorch.org/u/Yanzzzzz)\
**Post date:** [January 10, 2020, 7:43am UTC](https://discuss.pytorch.org/t/synchronization-for-sharing-updating-shared-model-state-dict-across-multi-process/50102/3 "2020-01-10T07:43:08Z")

</div>

I have the same question, and I think it’s not atomic operatino.

---

<div class="post-metadata">

**Author:** ![neoql](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@neoql](https://discuss.pytorch.org/u/neoql)\
**Post date:** [October 6, 2020, 2:09pm UTC](https://discuss.pytorch.org/t/synchronization-for-sharing-updating-shared-model-state-dict-across-multi-process/50102/4 "2020-10-06T14:09:57Z")

</div>

I found a possible answer. @Yanzzzzz @wwiiiii

> [@Grad sharing problem in a3c](https://discuss.pytorch.org/t/grad-sharing-problem-in-a3c/10635/2):
>
> yeah it really there just to make sure initializing goes smoothly. async updates without locks mean they updates from the parallel processes are done asynchronously which means they are run on their own clock and the different process threads can actually make updates at the same time which can lead to bad updates. The reason they do this even though there is risk of bad updates being made is that updates are done much faster which speeds up training as the process of acquiring and releasing lo…

And I found another related answer

> [@Model update with "share\_memory" need lock protection](https://discuss.pytorch.org/t/model-update-with-share-memory-need-lock-protection/72857):
>
> TL;DR. Maybe a lock is needed for optimizer.step() while using multiprocessing. The example provided in [MULTIPROCESSING BEST PRACTICES] ([https://pytorch.org/docs/stable/notes/multiprocessing.html](https://pytorch.org/docs/stable/notes/multiprocessing.html)) is to update parameters without lock protection. import torch.multiprocessing as mp from model import MyModel def train(model): # Construct data\_loader, optimizer, etc. for data, labels in data\_loader: optimizer.zero\_grad() loss\_fn(model(data), labels).backward() o…
