How to investigate RuntimeError: bad optional access

Julien_Despois: <p>Hi,</p>
<p>I am trying to convert a torch model to torchscript. I have removed all non supported operations from my model (the ones raised by <code>torch.jit.script(model)</code> ) but now the conversion fails with the following error:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-13-d068ba517b85&gt; in &lt;module&gt;
      1 import torch
----&gt; 2 torchscript_model = torch.jit.script(fa)

~/.conda/envs/pytorch/lib/python3.6/site-packages/torch/jit/__init__.py in script(obj, optimize, _frames_up, _rcb)
   1201 
   1202     if isinstance(obj, torch.nn.Module):
-&gt; 1203         return torch.jit.torch.jit._recursive.recursive_script(obj)
   1204 
   1205     qualified_name = _qualified_name(obj)

~/.conda/envs/pytorch/lib/python3.6/site-packages/torch/jit/_recursive.py in recursive_script(mod, exclude_methods)
    171     filtered_methods = filter(ignore_overloaded, methods)
    172     stubs = list(map(make_stub, filtered_methods))
--&gt; 173     return copy_to_script_module(mod, overload_stubs + stubs)
    174 
    175 

~/.conda/envs/pytorch/lib/python3.6/site-packages/torch/jit/_recursive.py in copy_to_script_module(original, stubs)
     93             setattr(script_module, name, item)
     94 
---&gt; 95     torch.jit._create_methods_from_stubs(script_module, stubs)
     96 
     97     # Now that methods have been compiled, take methods that have been compiled

~/.conda/envs/pytorch/lib/python3.6/site-packages/torch/jit/__init__.py in _create_methods_from_stubs(self, stubs)
   1421     rcbs = [m.resolution_callback for m in stubs]
   1422     defaults = [get_default_args(m.original_method) for m in stubs]
-&gt; 1423     self._c._create_methods(self, defs, rcbs, defaults)
   1424 
   1425 # For each user-defined class that subclasses ScriptModule this meta-class,

~/.conda/envs/pytorch/lib/python3.6/site-packages/torch/jit/_recursive.py in create_method_from_fn(module, fn)
    183         # We don't want to call the hooks here since the graph that is calling
    184         # this function is not yet complete
--&gt; 185         torch.jit._create_methods_from_stubs(module, (stub,))
    186     return stub
    187 

~/.conda/envs/pytorch/lib/python3.6/site-packages/torch/jit/__init__.py in _create_methods_from_stubs(self, stubs)
   1421     rcbs = [m.resolution_callback for m in stubs]
   1422     defaults = [get_default_args(m.original_method) for m in stubs]
-&gt; 1423     self._c._create_methods(self, defs, rcbs, defaults)
   1424 
   1425 # For each user-defined class that subclasses ScriptModule this meta-class,

~/.conda/envs/pytorch/lib/python3.6/site-packages/torch/jit/_recursive.py in make_strong_submodule(field, module, parent)
    193 
    194     # Convert the module to a ScriptModule
--&gt; 195     new_strong_submodule = recursive_script(module)
    196 
    197     # Install the ScriptModule on the python side

~/.conda/envs/pytorch/lib/python3.6/site-packages/torch/jit/_recursive.py in recursive_script(mod, exclude_methods)
    171     filtered_methods = filter(ignore_overloaded, methods)
    172     stubs = list(map(make_stub, filtered_methods))
--&gt; 173     return copy_to_script_module(mod, overload_stubs + stubs)
    174 
    175 

~/.conda/envs/pytorch/lib/python3.6/site-packages/torch/jit/_recursive.py in copy_to_script_module(original, stubs)
     93             setattr(script_module, name, item)
     94 
---&gt; 95     torch.jit._create_methods_from_stubs(script_module, stubs)
     96 
     97     # Now that methods have been compiled, take methods that have been compiled

~/.conda/envs/pytorch/lib/python3.6/site-packages/torch/jit/__init__.py in _create_methods_from_stubs(self, stubs)
   1421     rcbs = [m.resolution_callback for m in stubs]
   1422     defaults = [get_default_args(m.original_method) for m in stubs]
-&gt; 1423     self._c._create_methods(self, defs, rcbs, defaults)
   1424 
   1425 # For each user-defined class that subclasses ScriptModule this meta-class,

~/.conda/envs/pytorch/lib/python3.6/site-packages/torch/jit/_recursive.py in create_method_from_fn(module, fn)
    183         # We don't want to call the hooks here since the graph that is calling
    184         # this function is not yet complete
--&gt; 185         torch.jit._create_methods_from_stubs(module, (stub,))
    186     return stub
    187 

~/.conda/envs/pytorch/lib/python3.6/site-packages/torch/jit/__init__.py in _create_methods_from_stubs(self, stubs)
   1421     rcbs = [m.resolution_callback for m in stubs]
   1422     defaults = [get_default_args(m.original_method) for m in stubs]
-&gt; 1423     self._c._create_methods(self, defs, rcbs, defaults)
   1424 
   1425 # For each user-defined class that subclasses ScriptModule this meta-class,

~/.conda/envs/pytorch/lib/python3.6/site-packages/torch/jit/_recursive.py in create_method_from_fn(module, fn)
    183         # We don't want to call the hooks here since the graph that is calling
    184         # this function is not yet complete
--&gt; 185         torch.jit._create_methods_from_stubs(module, (stub,))
    186     return stub
    187 

~/.conda/envs/pytorch/lib/python3.6/site-packages/torch/jit/__init__.py in _create_methods_from_stubs(self, stubs)
   1421     rcbs = [m.resolution_callback for m in stubs]
   1422     defaults = [get_default_args(m.original_method) for m in stubs]
-&gt; 1423     self._c._create_methods(self, defs, rcbs, defaults)
   1424 
   1425 # For each user-defined class that subclasses ScriptModule this meta-class,

RuntimeError: bad optional access
</code></pre>
<p>I have no idea how to investigate or solve this problem. Is there any way to get to the root of the issue?</p>
<p>Thanks,</p>
<p>Julien</p>

driazati: <p>This looks like a bug, would you mind filing an issue on GitHub with a repro so we can work on a fix?</p>

Julien_Despois: <p>Thanks for your answer.<br>
After investigating the model (commenting blocks of code until the error is gone), I have traced back to the error to creating a tensor manually from a blend of 0-dim tensor and floats.</p>
<p>E.g. <code>torch.tensor([[my_tensor, my_tensor]])</code> works <img src="https://discuss.pytorch.org/images/emoji/apple/white_check_mark.png?v=9" title=":white_check_mark:" class="emoji" alt=":white_check_mark:"><br>
E.g. <code>torch.tensor([[my_tensor, 0.0]])</code> raises the error <img src="https://discuss.pytorch.org/images/emoji/apple/x.png?v=9" title=":x:" class="emoji" alt=":x:"></p>
<p>I have solved it in my case by using:<br>
<code>torch.cat([my_tensor.view(1), torch.tensor([0.0])]).unsqueeze</code><br>
or something similar.</p>
<p>Julien</p>

ptrblck: <p>That’s great debugging! <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"><br>
An issue would still be good, as the error message wasn’t really helpful.</p>