Performance issues with nn.ModuleList() and growing architectures

daniel451: <p>PyTorch version: 2.1</p>
<p>I am building growing networks, i.e., models that add new parameters as needed during training. The problem is that the shorter, more elegant solution seems to have a memory leak and also performance issues (much slower).</p>
<p>One of the possible implementations in PyTorch, let’s call it <strong>A</strong>,  is to use nn.ModuleList() and simply add new layers to that list. The entire network architecture is, of course, more complex but a simple example would be:</p>
<pre><code class="lang-auto">class GrowingLayer(nn.Module):
    ...
    self.layer_list = nn.ModuleList()
    ...

    def forward(self, x):
        layer_activations = [layer(x) for layer in self.layer_list]
        return torch.stack(layer_activations, dim=0).sum(dim=0)

    def grow(self):
        self.layer_list.append(nn.Conv2d(...))
</code></pre>
<p>Another possible implementation, let’s refer to that as <strong>B</strong>, is to have just a single Conv2d layer and, for growing, simply creating a new, bigger Conv2d layer and copy over the weights of the previously used layer to not disregard learned information. A simple example might be:</p>
<pre><code class="lang-auto">class GrowingConv2d(nn.Module):
    def __init__(self, ...):
        ...
        self.conv = nn.Conv2d(...)

    def grow(self, new_out_channels):
        new_conv = nn.Conv2d(self.conv.in_channels, new_out_channels, ...)

        # copy weights
        with torch.no_grad():
            new_conv.weight[:self.conv.out_channels] = self.conv.weight
            if self.conv.bias is not None:
                new_conv.bias[:self.conv.out_channels] = self.conv.bias

        # replace old with new layer
        self.conv = new_conv

    def forward(self, x):
        return self.conv(x)
</code></pre>
<p>Approach <strong>A</strong> should be preferred for experiments since in this scenario we are dealing with individual layers packed in module lists, arguably an elegant way of using module lists, and thus we can easily store each one of those in different optimizer groups. For example, when we add new parameters on growing:</p>
<pre><code class="lang-auto">        self.optim.param_groups.append(
            {
                "params": new_params,
                 ...
            }
        )
</code></pre>
<p>This is very handy because with just a few lines of code we can (1) grow different parts of a large architecture (2) keep momentum, learning rate, weight decay, … for “old” weights, and (3) initialize new params in a new optimizer group as we see fit for the current ongoing training.</p>
<p>Achieving the same with indexing every single layer by copying weights like in solution <strong>B</strong> is much more challenging since (1) we would need to store all of that information per indexed weight range and (2) and indexed weight is not the same as a nn.Parameter, thus we lose a lot of autograd features and need to write a lot more code manually.</p>
<p>Moreover, it is intuitive to use container structures to store new layers/parameters and requires significantly less lines of code in an entire training setup. Indexing weights manually is more prone to bugs, less intuitive, and requires a lot of manual implementation at other parts in the code (such as optimizer).</p>
<p>In terms of convergence and behavior during training approach <strong>A</strong> performs very well and as expected during training but w.r.t. performance (speed of forward + backward passes) and especially memory usage it is roughly an order of magnitude worse. Approach <strong>B</strong> performs as you would expect: as any regular Conv2d layer.</p>
<p>Now, especially an aggregation of results in pure Python (list comprehension) like this</p>
<pre><code class="lang-auto">layer_activations = [layer(x) for layer in self.layer_list]
</code></pre>
<p>should be slower but I do not see any reason why this approach would be roughly an order of magnitude slower.</p>
<p>What is even worse: there seems to be a memory leak with approach <strong>A</strong>. In approach <strong>B</strong> the VRAM usage increases as expected (close to linearly to the amount of weights you add). Approach <strong>A</strong>, however, when started with 800K parameters total for an entire model and growing it until 2M parameters goes from 3-4GB VRAM usage up to 40GB VRAM usage. For each call of the <code>grow()</code> method, in addition to the parameters that actually get added, there seems to be a very big overhead with this approach.</p>
<p>Did someone encounter similar issues?</p>
<p>Is there maybe a solution that is similar in simplicity to <strong>A</strong> but faster / more memory efficient?</p>
<p>It would actually be very handy to have a map-like or foreach-like function for nn.ModuleList that can apply some input to all layers in that list.</p>
<p>Apart from all of that, <code>torch.compile</code> also did not help. Performance for <strong>A</strong> actually did not increase at all and memory usage was the same. Although I did not try yet to re-compile after every growing operation, will test that also soon.</p>

eqy: <p>I think the issue is that approach A grows the autograd graph more than approach B because the activations will be saved in between the stack and sum, whereas there is no corresponding intermediate activation that will use memory in approach B.<br>
approach A:<br>
e.g., conv1 → out1, conv2 → out2<br>
<strong>out1 and out2 will be saved for autograd</strong><br>
stack = [out1, out2]<br>
<strong>stack will be saved for autograd</strong><br>
sum = stack …<br>
<strong>sum will be saved for autograd</strong></p>
<p>approach B:<br>
conv → out<br>
<strong>out will be saved for autograd</strong></p>
<p>You may check if you can do a <code>torch.concat</code> in approach A and simply grow the number of output channels, but this will also incur a copy (though perhaps less than that what is currently done).</p>

daniel451: <p>Thank you for the reply and the suggestion <a class="mention" href="/u/eqy">@eqy</a>.</p>
<blockquote>
<p>You may check if you can do a <code>torch.concat</code></p>
</blockquote>
<p>Interestingly, <code>torch.concat</code> actually did not improve the situation (at least not beyond possible measurement errors). I will try to write up a minimal example soon since testing with a big model and framework (1) takes too much time, (2) impact on performance might be concealed by other things happening, and (3) reproducability and testing should be better (especially for other people) with a minimal example.</p>

smth: <p>For Approach A, you can make it much much more memory-efficient if you rewrite it like this:</p>
<pre><code class="lang-auto">def forward(self, x):
	out = None
	for layer in self.layer_list:
		out_layer = layer(x)
		if out is None:
			out = out_layer
		else:
			out.add_(out_layer)
	return out
</code></pre>

daniel451: <p><a class="mention" href="/u/smth">@smth</a> ah yes, of course, making it an in-place operation and adding up the values on the existing Tensor after first iteration instead of computing every single result and storing it separately in memory before adding up. Very good, thank you <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>