While accessing individual layers using ._modules() in ResNet50: forward() function, the terminal quits on it's own, with an output: '^C'

username: Infinite_Warp
cooked: <p>I was trying the coding implementation of a research paper Attention Aware Polarity Sensitive Embedding for Affective Image Retrieval. I found this code on github:</p>
<p><a href="https://github.com/Xingxu1996/AIR_iccv2019" rel="nofollow noopener">Coding Implementation</a></p>
<pre><code class="lang-auto"> print("check point ---2")
    print(len(self.base._modules))
    for name, module in self.base._modules.items():
      print(name)
      print(x.size())
      if name == 'avgpool':
        break

      if name == 'layer3':
        l2 = x
      x = module(x)
      l4 = x
    print("check point ---x") 
</code></pre>
<p>The terminal on Google Colab quits after printing this:</p>
<p><a href="https://i.stack.imgur.com/vcJxY.png" rel="nofollow noopener">Output with cuda</a></p>
<p>It never reaches “checkpoint —x”, and the program just ends.</p>
<p>I tried to remove the  <code>.cuda()</code>  from the  <code>__init()__</code>  :</p>
<p><a href="https://i.stack.imgur.com/amQIA.png" rel="nofollow noopener">output without cuda</a></p>
<p>I observed that it was able to loop over one more iteration, so I believe that the RAM on Google Colab is filling up as the model is very large. I want to know if there is a workaround for this piece of code.If I can find l2 and l4 some other way?</p>

username: ptrblck
cooked: <p>Could you run the code on a local machine in a terminal to get a proper error message?<br>
I’m not sure, if you can observe the RAM utilization in Colab, but if so you could check if you are really running out of memory.</p>

username: Infinite_Warp
cooked: <p>Thank you for the reply sir. I tried the model on my cpu(8GB RAM, Windows):</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/7/77fd3423239b0f0263f9105fa21ebb3d5b94ddbf.png" data-download-href="https://discuss.pytorch.org/uploads/default/77fd3423239b0f0263f9105fa21ebb3d5b94ddbf" title="Capture1"><img src="https://discuss.pytorch.org/uploads/default/original/3X/7/7/77fd3423239b0f0263f9105fa21ebb3d5b94ddbf.png" alt="Capture1" data-base62-sha1="h7tiVFDA3pkbf3l0seegFsUZkxp" width="690" height="38" data-dominant-color="191919"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Capture1</span><span class="informations">1306×73 5.17 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>It states that it doesn’t have enough memory to allocate(Asks me to buy new RAM <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=12" title=":sweat_smile:" class="emoji" alt=":sweat_smile:" loading="lazy" width="20" height="20"> <img src="https://discuss.pytorch.org/images/emoji/apple/joy.png?v=12" title=":joy:" class="emoji" alt=":joy:" loading="lazy" width="20" height="20">).I apologize for the blurred out image. The error starts in line 137 of new_resnet24.py, <code>c2 = self.inconv2(l2)</code> . I suspect, the same thing might be happening on Google Colab(12GB RAM). I have tried all kinds of methods to obtain  <em>layer2</em>  and  <em>layer4</em> :</p>
<ol>
<li>Forward hooks</li>
<li>Using a list of features of the model and then looping over the children of resnet50</li>
<li>Reducing the size of the dataset to reduce computational cost</li>
</ol>
<p>None of the above methods have worked. Each time the program ended with ‘^C’ . If you can suggest  <strong>some more</strong>  methods to obtain the activations of the intermediate layers, I would be grateful.</p>
<p><del>PS</del><del>:</del> <del>The </del><del>code </del><del>snippet </del><del>in </del><del>the </del><del>original </del><del>question </del><del>is </del><del>a </del><del>part </del><del>of </del><del>the </del> <code>forward()</code>  <del>method </del><del>of </del><del>ResNet </del>in <del>new_resnet24</del><del>.</del><del>py </del><del>in </del><del>this </del><a href="https://github.com/Daksh-404/Attention-Aware-Polarity-Sensitive-Embedding-Model" rel="noopener nofollow ugc"><del>repository</del></a></p>

username: ptrblck
cooked: <p>Forward hooks would work fine and I don’t think the hooks themselves create the increased memory usage.<br>
Based on the code snippet you’ve provided it seems you are executing the forward method sequentially using the child modules of the base model, which could yield the OOM error.<br>
If the model is working fine without your current approach of storing the intermediate tensors, could you check, if you are storing these intermediates without detaching them?</p>

username: Infinite_Warp
cooked: <p>I am not sure what detaching <code>l2</code> and <code>l4</code> actually means. They are sequentially assigned to the new layer output <code>x</code>. I believe, they are being assigned <code>x</code> on the same space allocated to them initially. I don’t think any extra space is being used during reassignment.</p>
<p>In the case of forward hooks as well, I need the previous outputs which induce the need to sequentially find the outputs till the required layer. I might have not understood the concept of forward hooks( I only read the doubts on the discussion forum about forward hooks in pytorch) properly. Can you give a short example on the given snippet of code?</p>

username: Infinite_Warp
cooked: <p>Thank you the problem was solved. You were right, I needed to detach the intermediate tensors. I used the <code>Variable</code> wrapping with <code>l4</code> and <code>x</code>. Thanks for the help</p>