# Prefix parameter names in saved model if trained by multi-GPU?

**URL:** <https://discuss.pytorch.org/t/prefix-parameter-names-in-saved-model-if-trained-by-multi-gpu/494>\
**Category:** Uncategorized\
**Created:** [February 14, 2017, 7:23pm UTC](https://discuss.pytorch.org/t/prefix-parameter-names-in-saved-model-if-trained-by-multi-gpu/494 "2017-02-14T19:23:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ming](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ming/32/54_2.png) [@ming](https://discuss.pytorch.org/u/ming)\
**Post date:** [February 14, 2017, 7:23pm UTC](https://discuss.pytorch.org/t/prefix-parameter-names-in-saved-model-if-trained-by-multi-gpu/494/1 "2017-02-14T19:23:25Z")

</div>

Hi,

If I train a model with one GPU (without nn.DataParallel), the parameter names in saved models are something like:  
features.0.weight

If the model was initialized with nn.DataParallel, the saved parameter names have a prefix:  
model.features.0.weight

While doing inference I only use one GPU so the model failed to load the latter model file because the parameter names are not matching. I am wondering why parameter names are prepend the prefix? Can I trim the prefix and still use the model?

Thanks.  
Ming

---

<div class="post-metadata">

**Author:** ![apaszke](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/apaszke/32/21_2.png) [@apaszke](https://discuss.pytorch.org/u/apaszke)\
**Post date:** [February 14, 2017, 7:26pm UTC](https://discuss.pytorch.org/t/prefix-parameter-names-in-saved-model-if-trained-by-multi-gpu/494/2 "2017-02-14T19:26:36Z")

</div>

Yes, you can just remove the prefix:

```python
state_dict = {k.partition('model.')[2]: v for k,v in state_dict}

```

---

<div class="post-metadata">

**Author:** ![ming](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ming/32/54_2.png) [@ming](https://discuss.pytorch.org/u/ming)\
**Post date:** [February 14, 2017, 11:16pm UTC](https://discuss.pytorch.org/t/prefix-parameter-names-in-saved-model-if-trained-by-multi-gpu/494/3 "2017-02-14T23:16:36Z")

</div>

Thanks for the reply! Removing prefix works.

Curious why the prefix is needed? It creates inconvenience when we want to resume training a single-GPU-trained-model with multi-GPU, or pass a multi-GPU trained model to inference code that only uses one GPU?

Also it looks the pretrained resnet doesn’t have ‘module.’ prefix, does it mean that they were trained on single GPU?

---

<div class="post-metadata">

**Author:** ![apaszke](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/apaszke/32/21_2.png) [@apaszke](https://discuss.pytorch.org/u/apaszke)\
**Post date:** [February 15, 2017, 3:40pm UTC](https://discuss.pytorch.org/t/prefix-parameter-names-in-saved-model-if-trained-by-multi-gpu/494/4 "2017-02-15T15:40:21Z")

</div>

It’s needed because that’s how state\_dicts work 😉 You recursively go over the network, prepending the names. But maybe it’s a good idea to override that for DataParallel.

No, they probably had the prefixes trimmed before serialization.

---

<div class="post-metadata">

**Author:** ![lunasdejavu](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lunasdejavu/32/12338_2.png) [@lunasdejavu](https://discuss.pytorch.org/u/lunasdejavu)\
**Post date:** [May 24, 2019, 8:00am UTC](https://discuss.pytorch.org/t/prefix-parameter-names-in-saved-model-if-trained-by-multi-gpu/494/5 "2019-05-24T08:00:52Z")

</div>

Hi I met the same problem but  
after I changed the code like this:

```auto
state_dict =checkpoint['model_dict']
state_dict = {k.partition('model.')[2]: v for k,v in state_dict}
model.load_state_dict(state_dict)

```

it showed this error:  
`ValueError: too many values to unpack (expected 2)`  
why it didn’t work?

---

<div class="post-metadata">

**Author:** ![Lynx\_Commando](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lynx_commando/32/14537_2.png) [@Lynx\_Commando](https://discuss.pytorch.org/u/Lynx_Commando)\
**Post date:** [November 11, 2019, 11:47am UTC](https://discuss.pytorch.org/t/prefix-parameter-names-in-saved-model-if-trained-by-multi-gpu/494/6 "2019-11-11T11:47:46Z")

</div>

> [@apaszke](#):
>
> ```auto
> state_dict = {k.partition('model.')[2]: v for k,v in state_dict}
> 
> ```

{k.partition(‘module.’)[2]:state\_dict[k] for k in state\_dict.keys()}

---

<div class="post-metadata">

**Author:** ![pepeportolo](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/p/b5a626/32.png) [@pepeportolo](https://discuss.pytorch.org/u/pepeportolo)\
**Post date:** [May 3, 2022, 11:43am UTC](https://discuss.pytorch.org/t/prefix-parameter-names-in-saved-model-if-trained-by-multi-gpu/494/7 "2022-05-03T11:43:46Z")

</div>

I know this thread is a little bit old, but I just run into the same problem. From [https://pytorch.org/tutorials/beginner/saving\_loading\_models.html:](https://pytorch.org/tutorials/beginner/saving_loading_models.html:)

"`torch.nn.DataParallel` is a model wrapper that enables parallel GPU utilization. To save a `DataParallel` model generically, save the `model.module.state_dict()` . This way, you have the flexibility to load the model any way you want to any device you want."

Regards  
Pepe
