Describe the bug
When using an nn.ModuleList inside a "main" module, the output from summary seems to be incorrect if the submodules of the modules used in the ModuleList are not declared in the same order they are called in forward.
To Reproduce
To get incorrect output, one can run
import torchinfo
from torch import nn
class MainModule(nn.Module):
def __init__(self):
super().__init__()
self.ml = nn.ModuleList([CNN() for i in range(5)]) # ModuleList of CNNs
def forward(self, x):
for l in self.ml:
x = l(x)
return x
class CNN(nn.Module):
def __init__(self):
super().__init__()
self.relu = nn.ReLU()
self.conv = nn.Conv1d(1, 1, 1) # CNN submodules - note that ReLU is instantiated before Conv1d
self.pool = nn.MaxPool1d(1)
def forward(self, x): # The forward function of the modules in the `ModuleList`
out = x
out = self.conv(out)
out = self.relu(out)
out = self.pool(out)
return out
def main():
model = MainModule()
torchinfo.summary(model, input_size=[1, 10])
if __name__ == "__main__":
main()
The output is as follows. Note that the numbering for the CNNs is inconsistent, some items are out of order, etc.
Desktop (please complete the following information):
OS: macOS 12.6
Browser N/A
Version 1.7.0
Additional context
For my case, there is a workaround, but I wanted to make the issue known in case there doesn't exist a workaround for a particular usage. Also, I wanted to inform other users of the problem. Thank you for taking the time to read this issue!
Describe the bug When using an
nn.ModuleList
inside a "main" module, the output fromsummary
seems to be incorrect if the submodules of the modules used in the ModuleList are not declared in the same order they are called inforward
.To Reproduce To get incorrect output, one can run
The output is as follows. Note that the numbering for the CNNs is inconsistent, some items are out of order, etc.
Expected behavior Swapping
with
seems to resolve the issue, and outputs
Screenshots N/A
Desktop (please complete the following information):
Additional context For my case, there is a workaround, but I wanted to make the issue known in case there doesn't exist a workaround for a particular usage. Also, I wanted to inform other users of the problem. Thank you for taking the time to read this issue!