Skip to content

Commit b4414c0

Browse files
colesburysoumith
authored andcommitted
Handle None in modules list.
It's often useful to add None to an nn.ModuleList to keep the indexing of the module list to match some other property.
1 parent 39edc37 commit b4414c0

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

torch/nn/parallel/replicate.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,15 @@ def replicate(network, devices):
3434

3535
for i, module in enumerate(modules):
3636
for key, child in module._modules.items():
37-
module_idx = module_indices[child]
38-
for j in range(num_replicas):
39-
replica = module_copies[j][i]
40-
replica._modules[key] = module_copies[j][module_idx]
37+
if child is None:
38+
for j in range(num_replicas):
39+
replica = module_copies[j][i]
40+
replica._modules[key] = None
41+
else:
42+
module_idx = module_indices[child]
43+
for j in range(num_replicas):
44+
replica = module_copies[j][i]
45+
replica._modules[key] = module_copies[j][module_idx]
4146
for key, param in module._parameters.items():
4247
if param is None:
4348
for j in range(num_replicas):

0 commit comments

Comments
 (0)