Model forward fails

Unity05: <p>I’ve been facing this problem since several days now.<br>
I have converted my model just like this:</p>
<pre><code class="lang-auto">model = DenoisingAutoencoderSheetEdges()
model.load_state_dict(torch.load('models/model_3.pth').state_dict())
model.threshold = 0.75
model.hard_mode = True
model.eval()

qmodel = quantization.convert(module=model)
qmodel = torch.jit.script(qmodel)
qmodel.save('mobile/android_edge_model_2.4.pt')
</code></pre>
<p>my model:</p>
<pre><code class="lang-auto">class DenoisingAutoencoderSheetEdges(nn.Module):

    def __init__(self):
        super(DenoisingAutoencoderSheetEdges, self).__init__()
        self.hard_mode = False
        self.threshold = 0.75

        self.encoder = nn.Sequential(
            nn.ConvTranspose2d(1, 6, kernel_size=5),
            nn.ReLU(True),
            nn.ConvTranspose2d(6, 16, kernel_size=5),
            nn.ReLU(True),
            nn.ConvTranspose2d(16, 25, kernel_size=5),
            nn.ReLU(True),
            nn.ConvTranspose2d(25, 50, kernel_size=5),
            nn.ReLU(True)
        )

        self.decoder = nn.Sequential(
            nn.Conv2d(50, 25, kernel_size=5),
            nn.ReLU(True),
            nn.Conv2d(25, 16, kernel_size=5),
            nn.ReLU(True),
            nn.Conv2d(16, 6, kernel_size=5),
            nn.ReLU(True),
            nn.Conv2d(6, 1, kernel_size=5),
            nn.Sigmoid(),
        )

    def forward(self, x):
        x = self.encoder(x)
        x = self.decoder(x)
       
        if self.hard_mode:
            x_tensor = torch.ones(x.shape)
            y_tensor = torch.zeros(x.shape)
            x = torch.where(x &gt; self.threshold, x_tensor, y_tensor)

        return x
</code></pre>
<p>The model is loaded correctly, but the following error occurs during forwarding:</p>
<pre><code class="lang-auto">     Caused by: java.lang.RuntimeException: [enforce fail at CPUAllocator.cpp:72] data. DefaultCPUAllocator: not enough memory: you tried to allocate 642720000 bytes. Buy new RAM!
    
    The above operation failed in interpreter.
    Traceback (most recent call last):
    Serialized   File "code/__torch__/models.py", line 11
      def forward(self: __torch__.models.DenoisingAutoencoderSheetEdges,
        x: Tensor) -&gt; Tensor:
        x0 = (self.encoder).forward(x, )
              ~~~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
        x1 = (self.decoder).forward(x0, )
        if self.hard_mode:
      File "G:\Anaconda3\envs\PyTorch_pip2\lib\site-packages\torch\nn\modules\container.py", line 100
        def forward(self, input):
            for module in self:
                input = module(input)
                        ~~~~~~ &lt;--- HERE
            return input
    Serialized   File "code/__torch__/torch/nn/modules/container.py", line 30, in forward
        input5 = (_5).forward(input4, )
        input6 = (_6).forward(input5, None, )
        return (_7).forward(input6, )
                ~~~~~~~~~~~ &lt;--- HERE
      File "G:\Anaconda3\envs\PyTorch_pip2\lib\site-packages\torch\nn\modules\container.py", line 100
        def forward(self, input):
            for module in self:
                input = module(input)
                        ~~~~~~ &lt;--- HERE
            return input
    Serialized   File "code/__torch__/torch/nn/modules/container.py", line 29, in forward
        input4 = (_4).forward(input3, None, )
        input5 = (_5).forward(input4, )
        input6 = (_6).forward(input5, None, )
                  ~~~~~~~~~~~ &lt;--- HERE
        return (_7).forward(input6, )
      File "G:\Anaconda3\envs\PyTorch_pip2\lib\site-packages\torch\nn\modules\conv.py", line 776
            output_padding = self._output_padding(input, output_size, self.stride, self.padding, self.kernel_size)
    
            return F.conv_transpose2d(
                   ~~~~~~~~~~~~~~~~~~ &lt;--- HERE
                input, self.weight, self.bias, self.stride, self.padding,
                output_padding, self.groups, self.dilation)
    Serialized   File "code/__torch__/torch/nn/modules/conv/___torch_mangle_2.py", line 16, in forward
          pass
        output_padding = (self)._output_padding(input, output_size, [1, 1], [0, 0], [5, 5], )
        _0 = torch.conv_transpose2d(input, self.weight, self.bias, [1, 1], [0, 0], output_padding, 1, [1, 1])
             ~~~~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
        return _0
      def _output_padding(self: __torch__.torch.nn.modules.conv.___torch_mangle_2.ConvTranspose2d,
    
        at org.pytorch.NativePeer.forward(Native Method)
        at org.pytorch.Module.forward(Module.java:37)
        at com.example.scannerapp_0.DenoiseEdge.getEdgeData(DenoiseEdge.java:43)
        at com.example.scannerapp_0.MainActivity.net(MainActivity.java:99)
        	... 13 more
</code></pre>
<p>I do not understand what the error is trying to tell me or what I need to improve.<br>
I would appreciate any hint or explination.</p>
<p>Greetings,<br>
Unity</p>

albanD: <aside class="quote no-group" data-username="Unity05" data-post="1" data-topic="70910">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/unity05/48/20944_2.png" class="avatar"> Unity05:</div>
<blockquote>
<p>Caused by: java.lang.RuntimeException: [enforce fail at CPUAllocator.cpp:72] data. DefaultCPUAllocator: not enough memory: you tried to allocate 642720000 bytes. Buy new RAM!</p>
</blockquote>
</aside>
<p>From the beginning of the stack trace, it seems like you’re trying to make a Tensor which is too large (600MB). Is that expected? How much memory does it use if you run it on your machine?</p>

Unity05: <p>Ye, that actually caused the error.<br>
I was wondering why the error occured on my emulator device with 2gb memory<br>
since the device in idle state uses only about 800mb, that’s why I thought the cause would he more complex.<br>
However with a 4gb memory device it worked fine.</p>