# Model declaration distributed data parallel

**URL:** <https://discuss.pytorch.org/t/model-declaration-distributed-data-parallel/52958>\
**Category:** distributed\
**Created:** [August 9, 2019, 9:37am UTC](https://discuss.pytorch.org/t/model-declaration-distributed-data-parallel/52958 "2019-08-09T09:37:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![haowxu](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/h/e56c9b/32.png) [@haowxu](https://discuss.pytorch.org/u/haowxu)\
**Post date:** [August 9, 2019, 9:37am UTC](https://discuss.pytorch.org/t/model-declaration-distributed-data-parallel/52958/1 "2019-08-09T09:37:32Z")

</div>

Hi, guys. I’m trying to build a simple distributed data parallel training program with 1 GPU per process. Firstly I followed [https://pytorch.org/tutorials/intermediate/dist\_tuto.html](https://pytorch.org/tutorials/intermediate/dist_tuto.html) and added some modification.

```auto
def run(rank, size):
    ...
    model = Net()
    ...

if __name__ == ' __main__':
    ...
    for rank in range(size):
        p = Process(target=init_process, args=(rank, size, run))
        ...

```

However, after reading the example [https://github.com/pytorch/examples/tree/master/mnist\_hogwild](https://github.com/pytorch/examples/tree/master/mnist_hogwild), I found model is one of the arguments of a process:

```auto
if __name__ == ' __main__':
    ...
    model = Net().to(device)
    ...
    for rank in range(args.num_process):
        p = mp.Process(target=train, args=(rank, args, model, device, dataloader_kwargs))
        ...

```

So I just wonder: where should the model be declared, inside or outside these processes? When and how do the gradients get synchronized?

---

<div class="post-metadata">

**Author:** ![pietern](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pietern/32/4981_2.png) [@pietern](https://discuss.pytorch.org/u/pietern)\
**Post date:** [August 12, 2019, 11:25am UTC](https://discuss.pytorch.org/t/model-declaration-distributed-data-parallel/52958/2 "2019-08-12T11:25:40Z")

</div>

If you don’t care for doing hogwild, the second example you list is not applicable to you. Everything is declared in the primary process because the model weights are to be shared (physically shared through shared memory) between the worker processes.

If you want to simply use a single process per GPU and don’t care for physical weight sharing, then you should declare everything in the subprocesses. Think of it as if you were running this on different machines instead of multiple processes on a single machine. Then you’d also have to declare everything in the processes that you launch on those machine instead of a single launcher processes.

---

<div class="post-metadata">

**Author:** ![haowxu](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/h/e56c9b/32.png) [@haowxu](https://discuss.pytorch.org/u/haowxu)\
**Post date:** [August 13, 2019, 6:44am UTC](https://discuss.pytorch.org/t/model-declaration-distributed-data-parallel/52958/3 "2019-08-13T06:44:39Z")

</div>

Big thanks to your reply! So hogwild is not a must for distributed data parallel, even if the models are declared in different processes, they can synchronize their parameters and gradients in some way. Is it right?  
I tried saving the models at the end of these processes using the following code, but I found the parameters of the models are different. Does this mean that I made something wrong?

```auto
def run(rank, size):
    ...
    model = Net()
    ...
    for epoch in range(args.epochs):
        for batch_idx, (input, target) in enumerate(train_loader):
            output = model(input)
            loss = criterion(output, target)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

    ...
    model.save({...}, f'checkpoint-process-{rank}.pth')

```

---

<div class="post-metadata">

**Author:** ![pietern](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pietern/32/4981_2.png) [@pietern](https://discuss.pytorch.org/u/pietern)\
**Post date:** [August 13, 2019, 7:08am UTC](https://discuss.pytorch.org/t/model-declaration-distributed-data-parallel/52958/4 "2019-08-13T07:08:28Z")

</div>

You need to wrap your model with `nn.DistributedDataParallel`, see [https://pytorch.org/tutorials/intermediate/ddp\_tutorial.html](https://pytorch.org/tutorials/intermediate/ddp_tutorial.html) for a tutorial.

---

<div class="post-metadata">

**Author:** ![haowxu](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/h/e56c9b/32.png) [@haowxu](https://discuss.pytorch.org/u/haowxu)\
**Post date:** [August 13, 2019, 9:28am UTC](https://discuss.pytorch.org/t/model-declaration-distributed-data-parallel/52958/5 "2019-08-13T09:28:26Z")

</div>

Surely I did this. Sorry for omitting this in the code above. It’s like:

```auto
...
torch.cuda.set_device(rank)
...
model = Net().cuda()
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[rank], output_device=rank)
...

```

However, the parameters I got still differ.

---

<div class="post-metadata">

**Author:** ![haowxu](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/h/e56c9b/32.png) [@haowxu](https://discuss.pytorch.org/u/haowxu)\
**Post date:** [August 14, 2019, 2:24am UTC](https://discuss.pytorch.org/t/model-declaration-distributed-data-parallel/52958/6 "2019-08-14T02:24:49Z")

</div>

I tried saving the model just after the forward, the parameters of the saved models are the same.

```auto
def run(rank, size):
    ...
    for epoch in range(args.epochs):
        output = model(input)
        loss = criterion(output, target)

        torch.save({...}, f'checkpoint-process-{rank}-epoch-{epoch}.pth')

        ...

    ...

```

Still checking the sync of parameters and gradients in the source code.

---

<div class="post-metadata">

**Author:** ![haowxu](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/h/e56c9b/32.png) [@haowxu](https://discuss.pytorch.org/u/haowxu)\
**Post date:** [August 16, 2019, 10:19am UTC](https://discuss.pytorch.org/t/model-declaration-distributed-data-parallel/52958/7 "2019-08-16T10:19:35Z")

</div>

Update: Just refering the ImageNet example can help sync the parameters. I was using the wrong way to check the parameters of the model.
