RuntimeError: ENTER is not supported in mobile module

zabir-nabil: <p>I am trying to build the <code>ptl</code> weights for one of the simple SRE model. I tried to simplify the model so that it doesn’t contain any complex operations.</p>
<p>But it’s still throwing this ambiguous error.</p>
<pre><code class="lang-auto">RecursiveScriptModule(original_name=MainModel)
Traceback (most recent call last):
  File "melspec_test.py", line 90, in &lt;module&gt;
    optimized_model._save_for_lite_interpreter("vgg_sre.ptl")
  File "C:\ProgramData\Anaconda3\envs\py38\lib\site-packages\torch\jit\_script.py", line 707, in _save_for_lite_interpreter
    return self._c._save_for_mobile(*args, **kwargs)
RuntimeError: ENTER is not supported in mobile module.
</code></pre>
<p>My code:</p>
<pre><code class="lang-auto">import torch
import torchaudio
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import Parameter
from torch.utils.mobile_optimizer import optimize_for_mobile


class MainModel(nn.Module):
    def __init__(self, nOut = 1024):
        super(MainModel, self).__init__()
        
        self.netcnn = nn.Sequential(
            nn.Conv2d(1, 96, kernel_size=(5,7), stride=(1,2), padding=(2,2)),
            nn.BatchNorm2d(96),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=(1,3), stride=(1,2)),

            nn.Conv2d(96, 256, kernel_size=(5,5), stride=(2,2), padding=(1,1)),
            nn.BatchNorm2d(256),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=(3,3), stride=(2,2)),

            nn.Conv2d(256, 384, kernel_size=(3,3), padding=(1,1)),
            nn.BatchNorm2d(384),
            nn.ReLU(inplace=True),

            nn.Conv2d(384, 256, kernel_size=(3,3), padding=(1,1)),
            nn.BatchNorm2d(256),
            nn.ReLU(inplace=True),

            nn.Conv2d(256, 256, kernel_size=(3,3), padding=(1,1)),
            nn.BatchNorm2d(256),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=(3,3), stride=(2,2)),

            nn.Conv2d(256, 512, kernel_size=(4,1), padding=(0,0)),
            nn.BatchNorm2d(512),
            nn.ReLU(inplace=True),
            
        )

        self.sap_linear = nn.Linear(512, 512)
        self.attention = self.new_parameter(512, 1)
        out_dim = 512


        self.fc = nn.Linear(out_dim, nOut)

    def new_parameter(self, *size):
        out = nn.Parameter(torch.FloatTensor(*size))
        nn.init.xavier_normal_(out)
        return out
        
    def forward(self, x):

        with torch.no_grad():
            x = x.log()
            x = x.unsqueeze(1)

        x = self.netcnn(x)

        x = x.permute(0, 2, 1, 3)
        x = x.squeeze(dim=1).permute(0, 2, 1)  # batch * L * D
        h = torch.tanh(self.sap_linear(x))
        w = torch.matmul(h, self.attention).squeeze(dim=2)
        w = F.softmax(w, dim=1).view(x.size(0), x.size(1), 1)
        x = torch.sum(x * w, dim=1)

        x = self.fc(x)

        return x

model = MainModel()
model.eval()
x = torch.rand(1, 40, 200)
traced_cell = torch.jit.trace(model, x)


quantized_model = torch.quantization.quantize_dynamic(
    model, qconfig_spec={torch.nn.Linear}, dtype=torch.qint8)
scripted_model = torch.jit.script(quantized_model)
optimized_model = optimize_for_mobile(scripted_model)

print(optimized_model)

optimized_model._save_for_lite_interpreter("vgg_sre.ptl")
</code></pre>
<p>I am using pytorch torch-1.11.0.dev20220108+cpu (nightly) on windows.</p>
<p>I am not sure, for which operations it’s failing, there’s no clear error message.</p>

Linbin: <p>it says “ENTER” instruction is not supported in mobile:</p><aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/pytorch/blob/56511f859a32aa4fc9648aa1ca69d0689e09bca3/torch/csrc/jit/runtime/instruction.h#L75">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/blob/56511f859a32aa4fc9648aa1ca69d0689e09bca3/torch/csrc/jit/runtime/instruction.h#L75" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/56511f859a32aa4fc9648aa1ca69d0689e09bca3/torch/csrc/jit/runtime/instruction.h#L75" target="_blank" rel="noopener nofollow ugc">pytorch/pytorch/blob/56511f859a32aa4fc9648aa1ca69d0689e09bca3/torch/csrc/jit/runtime/instruction.h#L75</a></h4>



    <pre class="onebox">      <code class="lang-h">
        <ol class="start lines" start="65" style="counter-reset: li-counter 64 ;">
            <li>  _(DEVICE, "") /* invokes aten::device for a Tensor */                        \</li>
            <li>  _(DTYPE, "") /* invokes aten::dtype for a Tensor */                          \</li>
            <li>  _(DIM, "") /* invokes aten::dim for a Tensor */                              \</li>
            <li>  _(__NOT__, "") /* performs `not` operator from Python  */                    \</li>
            <li>  _(TO_LIST, "") /* convert the input to a list */                             \</li>
            <li>  _(NUM_TO_TENSOR,                                                             \</li>
            <li>    "") /* performs the conversion of a number/scalar to Tensor */             \</li>
            <li>  _(IS_CUDA, "") /* invokes aten::is_cuda for a Tensor */                      \</li>
            <li>  _(FORK, "CN") /* launch a thread to run code entry x with N inputs  */       \</li>
            <li>  _(WARN, "I") /* emit a warning with line information */                      \</li>
            <li class="selected">  _(ENTER, "EN") /* enter scope of a contextmanager */                         \</li>
            <li>  _(EXIT, "EX") /* exit the last entered contextmanager */</li>
            <li>
            </li>
<li>enum OpCode : uint8_t {</li>
            <li>#define DEFINE_OP(op, _) op,</li>
            <li>  FORALL_OPCODES(DEFINE_OP)</li>
            <li>#undef DEFINE_OP</li>
            <li>};</li>
            <li>
            </li>
<li>struct Instruction {</li>
            <li>  OpCode op;</li>
        </ol>
      </code>
    </pre>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>I don’t know which op triggers it (maybe with torch.no_grad?). cc <a class="mention" href="/u/martin_yuan">@Martin_Yuan</a></p>

cccclai: <aside class="quote no-group" data-username="zabir-nabil" data-post="1" data-topic="141118">
<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/zabir-nabil/48/45573_2.png" class="avatar"> zabir-nabil:</div>
<blockquote>
<pre><code class="lang-auto">        with torch.no_grad():
            x = x.log()
            x = x.unsqueeze(1)
</code></pre>
</blockquote>
</aside>
<p>I think this line might be an issue. <code>ENTER</code> is used to resolve context and is not supported with mobile interpreter yet. The <code>with</code> statement basically creates a runtime context that allows you to run a group of statements under the control of a context manager.</p>