net1, net2, net_shared are preinstantiated nn.Sequential modules that will be passed to the constructor of Module1 and Module2. So, does net_shared will be treated as a single net? So that backpropagating through Module2, net_shared weights in Module1 will be updated, or by instantiating two different classes it will be treated as two deep copies instead of a shallow one?
No, nothing is being copied (other than the python references themselves).
The line of code, net_shared = nn.Sequential(), instantiates a single
instance of the class Sequential and sets the python reference net_shared
to refer to it. When, instantiating instances of Module1 and Module2, you
pass in net_shared, module1.net_shared and module2.net_shared get
created as python references that refer to the same object-instance as the net_shared reference that is passed in.
There is only one (not counting net1 and net2) instance of Sequential
and it is indeed shared between module1 and module2 – no copies,
neither deep nor shallow are made.
You can probe this by looking at the object id()'s of your net_shareds and
you will see that they are the same: