# Clone a Module?

**URL:** <https://discuss.pytorch.org/t/clone-a-module/76474>\
**Category:** Uncategorized\
**Created:** [April 13, 2020, 12:04am UTC](https://discuss.pytorch.org/t/clone-a-module/76474 "2020-04-13T00:04:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![zacharynew](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zacharynew/32/19474_2.png) [@zacharynew](https://discuss.pytorch.org/u/zacharynew)\
**Post date:** [April 13, 2020, 12:04am UTC](https://discuss.pytorch.org/t/clone-a-module/76474/1 "2020-04-13T00:04:54Z")

</div>

Hi all,

I’m working on a implementation of MAML ([paper link](https://arxiv.org/pdf/1703.03400.pdf)). There’s a few implementations out there but from what can see they all rely on the functional form of a model. I have a requirement to make this as general as possible so it can be used with a variety of underlying models. To that end I would like to be able to copy a nn.Module instance in the same way one can copy a variable or parameter. The key is that the gradients need to be able to get back to the copied nn.Module.

I’ve tried to hack something together via state-dicts and hooks but I keep running into cases where I need to copy a nn.Module without removing it from the computational graph.

Any thoughts on the problem would be appreciated!

Thanks!

---

<div class="post-metadata">

**Author:** ![zacharynew](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zacharynew/32/19474_2.png) [@zacharynew](https://discuss.pytorch.org/u/zacharynew)\
**Post date:** [April 13, 2020, 12:05am UTC](https://discuss.pytorch.org/t/clone-a-module/76474/2 "2020-04-13T00:05:32Z")

</div>

And just after I post it I find a similar discussion: [Are there any recommended methods to clone a model?](https://discuss.pytorch.org/t/are-there-any-recommended-methods-to-clone-a-model/483/16)

Edit: after reading through the other discussion I think its not what I am after. It focuses on how to properly copy a model and continue training with it. My use case is a little different as I want to properly clone the model and leave that cloning process in the computational graph so that I can differentiate all the way back to the original model.

---

<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:** [April 13, 2020, 4:21am UTC](https://discuss.pytorch.org/t/clone-a-module/76474/3 "2020-04-13T04:21:26Z")

</div>

`tensor.clone()` is a differentiable operation, which could be used on your parameters.  
However, I’m not sure, if that would be suitable for your approach or not.

---

<div class="post-metadata">

**Author:** ![zacharynew](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zacharynew/32/19474_2.png) [@zacharynew](https://discuss.pytorch.org/u/zacharynew)\
**Post date:** [April 13, 2020, 3:28pm UTC](https://discuss.pytorch.org/t/clone-a-module/76474/4 "2020-04-13T15:28:04Z")

</div>

Hey @ptrblck,

I’ve been trying to use the tensor.clone() operation but am not sure how to load the cloned tensors into a new nn.Module without using load\_state\_dict() which appears to disconnect parameters from the computation graph. I think you’re right that individually cloning the parameters is the way to go but I’m not sure how to pull this off and connect them to a new nn.Module without this detach from the computation graph occurring.

Thoughts?

---

<div class="post-metadata">

**Author:** ![zacharynew](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zacharynew/32/19474_2.png) [@zacharynew](https://discuss.pytorch.org/u/zacharynew)\
**Post date:** [April 13, 2020, 3:41pm UTC](https://discuss.pytorch.org/t/clone-a-module/76474/5 "2020-04-13T15:41:18Z")

</div>

After playing around a little more I think it comes down to being able to do the following:

Let `f` and `g` both be instances of the same `nn.Module`. Throw some input values through `g` when `g` is in training mode. Now the parameters in `g` have gradients and are part of a computation graph. Let `A` be a parameter in `f` and `B` be the corresponding parameter in `g`. Now set `C=B.clone()`. Here’s the bit I don’t know how to do, reassign `A` to be `C`.

This seems like a use-case that should be possible with PyTorch without being too hacky. Anyone have a path forward?

Thanks!

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [April 13, 2020, 6:18pm UTC](https://discuss.pytorch.org/t/clone-a-module/76474/6 "2020-04-13T18:18:05Z")

</div>

Hi,

Yes this is quite far from what nn.Modules were built for so it’s not super easy 😕

The thing to keep in mind here is that the original Module and the cloned one should NOT be the same.  
In particular, the cloned one cannot use `nn.Parameter()` because these can only contain leaf Tensors (that don’t have history).  
So you most likely want to make sure that you delete each parameter and re-assign the field with a clone of the value in the other module.

---

<div class="post-metadata">

**Author:** ![Dieblitzen](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/e47c2d/32.png) [@Dieblitzen](https://discuss.pytorch.org/u/Dieblitzen)\
**Post date:** [November 10, 2020, 3:25am UTC](https://discuss.pytorch.org/t/clone-a-module/76474/7 "2020-11-10T03:25:14Z")

</div>

Hi, did you manage to solve this? I am running into the same issues as you

---

<div class="post-metadata">

**Author:** ![Pieterkii](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pieterkii/32/41725_2.png) [@Pieterkii](https://discuss.pytorch.org/u/Pieterkii)\
**Post date:** [August 29, 2021, 12:35am UTC](https://discuss.pytorch.org/t/clone-a-module/76474/8 "2021-08-29T00:35:12Z")

</div>

I’m using the deepcopy operation, works just fine

---

<div class="post-metadata">

**Author:** ![seba-1511](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/seba-1511/32/13883_2.png) [@seba-1511](https://discuss.pytorch.org/u/seba-1511)\
**Post date:** [August 29, 2021, 9:30am UTC](https://discuss.pytorch.org/t/clone-a-module/76474/9 "2021-08-29T09:30:58Z")

</div>

We have an implementation of clone\_module in learn2learn (and used it for our MAML wrappers):

[https://github.com/learnables/learn2learn/blob/752200384c3ca8caeb8487b5dd1afd6568e8ec01/learn2learn/utils/ **init**.py#L51](https://github.com/learnables/learn2learn/blob/752200384c3ca8caeb8487b5dd1afd6568e8ec01/learn2learn/utils/ __init__.py#L51)
