# Checkpoint in Multi GPU

**URL:** <https://discuss.pytorch.org/t/checkpoint-in-multi-gpu/97852>\
**Category:** Uncategorized\
**Created:** [September 30, 2020, 8:15pm UTC](https://discuss.pytorch.org/t/checkpoint-in-multi-gpu/97852 "2020-09-30T20:15:50Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![gnadaf](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/cdc98d/32.png) [@gnadaf](https://discuss.pytorch.org/u/gnadaf)\
**Post date:** [September 30, 2020, 8:15pm UTC](https://discuss.pytorch.org/t/checkpoint-in-multi-gpu/97852/1 "2020-09-30T20:15:50Z")

</div>

Is there any difference between, saving checkpoint when training with a single GPU and saving checkpoint with 2 GPUs?

Example: If I use DataParallel to train on 2 GPUs, if I save checkpoint after each epoch, which parameters will be saved? GPU1 info saved or GPU-2 info saved in checkpoint ?

How to check while tranining

---

<div class="post-metadata">

**Author:** ![Janine](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/janine/32/12526_2.png) [@Janine](https://discuss.pytorch.org/u/Janine)\
**Post date:** [October 1, 2020, 2:31am UTC](https://discuss.pytorch.org/t/checkpoint-in-multi-gpu/97852/2 "2020-10-01T02:31:45Z")

</div>

Having same doubts here, any guidance would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [October 2, 2020, 7:11am UTC](https://discuss.pytorch.org/t/checkpoint-in-multi-gpu/97852/3 "2020-10-02T07:11:28Z")

</div>

`nn.DataParallel` will reduce all parameters to the model on the default device, so you could directly store the `model.module.state_dict()`.  
If you are using `DistributedDataParallel`, you would have to make sure that only one rank is storing the checkpoint as otherwise multiple process might be writing to the same file and thus corrupt it.  
[Here](https://github.com/pytorch/examples/blob/a5de3ce4da81bacc21864ace40dbc8a2e810df5a/imagenet/main.py#L254-L262) is an example how to do so.

CC @Janine

---

<div class="post-metadata">

**Author:** ![gnadaf](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/cdc98d/32.png) [@gnadaf](https://discuss.pytorch.org/u/gnadaf)\
**Post date:** [October 2, 2020, 12:24pm UTC](https://discuss.pytorch.org/t/checkpoint-in-multi-gpu/97852/4 "2020-10-02T12:24:30Z")

</div>

> [@ptrblck](#):
>
> f you are using `DistributedDataParallel` , you would have to make su

Thanks @ptrblck, in nn.DataParallel if we use 2 GPUs, checkpoint saves the data from default device (i.e GPU-0), right? GPU-2 data is not important?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [October 2, 2020, 9:25pm UTC](https://discuss.pytorch.org/t/checkpoint-in-multi-gpu/97852/5 "2020-10-02T21:25:54Z")

</div>

Yes, that’s the case since `nn.DataParallel` will create new model replicas in each iteration and thus when you store the `state_dict` outside of the forward or backward pass, there should only be a single model on the initially specified device.

---

<div class="post-metadata">

**Author:** ![gnadaf](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/cdc98d/32.png) [@gnadaf](https://discuss.pytorch.org/u/gnadaf)\
**Post date:** [October 2, 2020, 11:20pm UTC](https://discuss.pytorch.org/t/checkpoint-in-multi-gpu/97852/6 "2020-10-02T23:20:08Z")

</div>

yes, I verified with the experimen.

If we use two GPUs to train model in DataParallel(), Which portion of the saved data is identical between GPU 0 and GPU 1? weights, gradients, LR, etc the same across 2 GPUs?

I think, saved data from only GPU-0 (not from GPU-2), is it correct?

---

<div class="post-metadata">

**Author:** ![gnadaf](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/cdc98d/32.png) [@gnadaf](https://discuss.pytorch.org/u/gnadaf)\
**Post date:** [October 4, 2020, 12:28am UTC](https://discuss.pytorch.org/t/checkpoint-in-multi-gpu/97852/7 "2020-10-04T00:28:41Z")

</div>

@ptrblck,

In case of DataParallel().  
If we use two GPUs, initially model is replicated on both GPUs, weights and bias are same on both GPUs and after calculation, weights are updated GPU-0 (default), but GPU-1 holds the same updated value?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [October 4, 2020, 3:22am UTC](https://discuss.pytorch.org/t/checkpoint-in-multi-gpu/97852/8 "2020-10-04T03:22:34Z")

</div>

The replicas will be recreated in each forward pass using `nn.DataParallel` (which is also why it’s slower than `DistributedDataParallel`).  
Your code will most likely just use the single model, as seen here:

```python
model = MyModel()
model = nn.DataParallel(model)
model.to('cuda:0') # push to default device

output = model(data) # DataParallel will automatically create the replicas
loss = ...
loss.backward() # DataParallel will automatically call the backward pass on all models and reduce the gradients
optimizer.step()

torch.save(model.module.state_dict(), path) # use the default model

```

You can find more information about the underlying workflow in `DataParallel` in [this blog post](https://medium.com/huggingface/training-larger-batches-practical-tips-on-1-gpu-multi-gpu-distributed-setups-ec88c3e51255).

---

<div class="post-metadata">

**Author:** ![gnadaf](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/cdc98d/32.png) [@gnadaf](https://discuss.pytorch.org/u/gnadaf)\
**Post date:** [October 4, 2020, 3:09pm UTC](https://discuss.pytorch.org/t/checkpoint-in-multi-gpu/97852/10 "2020-10-04T15:09:20Z")

</div>

DataParallel().  
In case of 2 GPUs, both GPUs hold the same weights. is it correct?

---

<div class="post-metadata">

**Author:** ![seungjun](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@seungjun](https://discuss.pytorch.org/u/seungjun)\
**Post date:** [October 5, 2020, 2:20am UTC](https://discuss.pytorch.org/t/checkpoint-in-multi-gpu/97852/11 "2020-10-05T02:20:56Z")

</div>

Model parameters on multiple GPUs by DataParallel and DistributedDataParallel are the same. (unless GPU communication glitch happens)

You can just save parameters on GPU 0 and load them later.

- saving

```python
if isinstance(model, (DataParallel, DistributedDataParallel)):
    torch.save(model.module.state_dict(), model_save_name)
else:
    torch.save(model.state_dict(), model_save_name)

```

- loading

```python
state_dict = torch.load(model_name, map_location=current_gpu_device)
if isinstance(model, (DataParallel, DistributedDataParallel)):
    model.module.load_state_dict(state_dict)
else:
    model.load_state_dict(state_dict)

```

---

<div class="post-metadata">

**Author:** ![gnadaf](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/cdc98d/32.png) [@gnadaf](https://discuss.pytorch.org/u/gnadaf)\
**Post date:** [October 5, 2020, 2:35am UTC](https://discuss.pytorch.org/t/checkpoint-in-multi-gpu/97852/12 "2020-10-05T02:35:55Z")

</div>

DataParallel().  
If we use two GPUs, we know model will replicate on both GPUs, but I would like to know while saving model in checkpoint ---- Does checkpoint save the both replicated model?

I believe, the model which is present on default device (GPU-0) will be saved in the checkpoint, Right?

---

<div class="post-metadata">

**Author:** ![seungjun](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@seungjun](https://discuss.pytorch.org/u/seungjun)\
**Post date:** [October 5, 2020, 3:09am UTC](https://discuss.pytorch.org/t/checkpoint-in-multi-gpu/97852/13 "2020-10-05T03:09:36Z")

</div>

Yes, for `DataParallel`, if you save by `torch.save(model.state_dict())`, it will save parameters on GPU 0.

But the parameters will be saved under `model.module` which cannot be loaded to non-DataParallel formats. That’s why I suggest the above code that makes saving/loading compatible with `nn.Module` format and `nn.DataParallel` format.

If you use `DistributedDataParallel`, you should only save parameters from local rank 0. Otherwise, each DDP process will try to save model on each GPU and overwrite each other.

---

<div class="post-metadata">

**Author:** ![gnadaf](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/cdc98d/32.png) [@gnadaf](https://discuss.pytorch.org/u/gnadaf)\
**Post date:** [October 5, 2020, 3:33am UTC](https://discuss.pytorch.org/t/checkpoint-in-multi-gpu/97852/14 "2020-10-05T03:33:14Z")

</div>

Thanks for detailed info.

Having deep insight, we know models are replicated in DataParallel(), and the weights of both models are the same.

Where or in which code part, model weights are updated? ?(in parallel\_apply() models are replicated)

Which model’s weight is saved in checkpoint? (GPU-0 model weights and GPU-1 model weights)

model present on GPU-0 weights saved in checkpoint OR model present on GPU-1 weights are saved in checkpoint?

---

<div class="post-metadata">

**Author:** ![gnadaf](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/cdc98d/32.png) [@gnadaf](https://discuss.pytorch.org/u/gnadaf)\
**Post date:** [October 5, 2020, 3:33am UTC](https://discuss.pytorch.org/t/checkpoint-in-multi-gpu/97852/15 "2020-10-05T03:33:28Z")

</div>

Thanks for detailed info.

Having deep insight, we know models are replicated in DataParallel(), and the weights of both models are the same.

Where or in which code part, model weights are updated? ?(in parallel\_apply() models are replicated)

Which model’s weight is saved in checkpoint? (GPU-0 model weights and GPU-1 model weights)

model present on GPU-0 weights saved in checkpoint OR model present on GPU-1 weights are saved in checkpoint?

---

<div class="post-metadata">

**Author:** ![seungjun](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@seungjun](https://discuss.pytorch.org/u/seungjun)\
**Post date:** [October 6, 2020, 3:03am UTC](https://discuss.pytorch.org/t/checkpoint-in-multi-gpu/97852/16 "2020-10-06T03:03:24Z")

</div>

It would be a good idea to review the blog post suggested by ptrblck above.

In DataParallel, `parallel_apply` does not perform parameter update or synchronization.  
`replicate` function synchronizes model parameters across GPUs.  
[dataparallel forward](http://https://github.com/pytorch/pytorch/blob/master/torch/nn/parallel/data_parallel.py#L160)

As I noted, weights on GPU 0 is saved.  
You can almost consider weights on other GPUs to be not really there constantly. Parameters on other GPUS are only created just before their forward computation by the above `replicate` function. And the `replicate` function is called at every forward call.

---

<div class="post-metadata">

**Author:** ![gnadaf](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/cdc98d/32.png) [@gnadaf](https://discuss.pytorch.org/u/gnadaf)\
**Post date:** [October 22, 2020, 2:15pm UTC](https://discuss.pytorch.org/t/checkpoint-in-multi-gpu/97852/17 "2020-10-22T14:15:15Z")

</div>

> [@seungjun](#):
>
> `istribut`

Can’t we load `nn.DataParallel` format checkpoint to `nn.Module` format model?  
Is it an issue present in Pytorch checkpoint?

---

<div class="post-metadata">

**Author:** ![seungjun](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@seungjun](https://discuss.pytorch.org/u/seungjun)\
**Post date:** [October 23, 2020, 1:32am UTC](https://discuss.pytorch.org/t/checkpoint-in-multi-gpu/97852/18 "2020-10-23T01:32:22Z")

</div>

No. It is not an issue.  
`nn.DataParallel` saves the parameters under `self.module`.  
For example, let’s assume your original single-gpu model had `self.conv` layer.  
In your `DataParallel` model, it will move to `self.module.conv`.  
That’s why I recommend saving `self.module.state_dict()` as in the above example code.

---

<div class="post-metadata">

**Author:** ![gnadaf](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/cdc98d/32.png) [@gnadaf](https://discuss.pytorch.org/u/gnadaf)\
**Post date:** [October 26, 2020, 2:32pm UTC](https://discuss.pytorch.org/t/checkpoint-in-multi-gpu/97852/19 "2020-10-26T14:32:32Z")

</div>

Thanks @seungjun for the detailed Info
