<h2 dir="auto"><g-emoji class="g-emoji" alias="bug" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f41b.png">🐛</g-emoji> Bug</h2>
<p dir="auto">Python doesn't care about indentation of comment lines, but the TorchScript parser can get confused by non-indented comments (which is what Jupyter produces when you comment code out with ctrl-/).</p>
<h2 dir="auto">To Reproduce</h2>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import torch
import torch.utils.collect_env
torch.utils.collect_env.main()

class MyModule(torch.nn.Module):
    def forward(self):
#         return do_computation()
# NOTE: This is how Jupyter comments things out: ^^^
        return torch.Tensor([])

m = MyModule()
print(m())
torch.jit.script(m)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">torch</span>
<span class="pl-k">import</span> <span class="pl-s1">torch</span>.<span class="pl-s1">utils</span>.<span class="pl-s1">collect_env</span>
<span class="pl-s1">torch</span>.<span class="pl-s1">utils</span>.<span class="pl-s1">collect_env</span>.<span class="pl-en">main</span>()

<span class="pl-k">class</span> <span class="pl-v">MyModule</span>(<span class="pl-s1">torch</span>.<span class="pl-s1">nn</span>.<span class="pl-v">Module</span>):
    <span class="pl-k">def</span> <span class="pl-en">forward</span>(<span class="pl-s1">self</span>):
<span class="pl-c">#         return do_computation()</span>
<span class="pl-c"># NOTE: This is how Jupyter comments things out: ^^^</span>
        <span class="pl-k">return</span> <span class="pl-s1">torch</span>.<span class="pl-v">Tensor</span>([])

<span class="pl-s1">m</span> <span class="pl-c1">=</span> <span class="pl-v">MyModule</span>()
<span class="pl-en">print</span>(<span class="pl-en">m</span>())
<span class="pl-s1">torch</span>.<span class="pl-s1">jit</span>.<span class="pl-en">script</span>(<span class="pl-s1">m</span>)</pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Collecting environment information...
PyTorch version: 1.5.0a0+5a8562a
Is debug build: Yes
CUDA used to build PyTorch: None

OS: Fedora release 30 (Thirty)
GCC version: (GCC) 9.1.1 20190503 (Red Hat 9.1.1-1)
CMake version: version 3.14.4

Python version: 3.7
Is CUDA available: No
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA

Versions of relevant libraries:
[pip3] numpy==1.17.0
[pip3] pytorch-pretrained-bert==0.6.2
[pip3] torch==1.2.0
[pip3] torchtext==0.4.0
[conda] Could not collect
tensor([])

Traceback (most recent call last):

  File &quot;/home/dreiss/work/venv-pt-local/lib/python3.7/site-packages/IPython/core/interactiveshell.py&quot;, line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File &quot;&lt;ipython-input-176-270f64c79839&gt;&quot;, line 13, in &lt;module&gt;
    torch.jit.script(m)

  File &quot;/home/dreiss/work/pytorch/torch/jit/__init__.py&quot;, line 1267, in script
    return torch.jit._recursive.create_script_module(obj, torch.jit._recursive.infer_methods_to_compile)

  File &quot;/home/dreiss/work/pytorch/torch/jit/_recursive.py&quot;, line 305, in create_script_module
    return create_script_module_impl(nn_module, concrete_type, stubs_fn)

  File &quot;/home/dreiss/work/pytorch/torch/jit/_recursive.py&quot;, line 317, in create_script_module_impl
    stubs = stubs_fn(nn_module)

  File &quot;/home/dreiss/work/pytorch/torch/jit/_recursive.py&quot;, line 511, in infer_methods_to_compile
    stubs.append(make_stub_from_method(nn_module, method))

  File &quot;/home/dreiss/work/pytorch/torch/jit/_recursive.py&quot;, line 41, in make_stub_from_method
    return make_stub(func)

  File &quot;/home/dreiss/work/pytorch/torch/jit/_recursive.py&quot;, line 34, in make_stub
    ast = torch.jit.get_jit_def(func, self_name=&quot;RecursiveScriptModule&quot;)

  File &quot;/home/dreiss/work/pytorch/torch/jit/frontend.py&quot;, line 167, in get_jit_def
    py_ast = ast.parse(dedent_src)

  File &quot;/usr/lib64/python3.7/ast.py&quot;, line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)

  File &quot;&lt;unknown&gt;&quot;, line 1
    def forward(self):
    ^
IndentationError: unexpected indent"><pre class="notranslate"><code class="notranslate">Collecting environment information...
PyTorch version: 1.5.0a0+5a8562a
Is debug build: Yes
CUDA used to build PyTorch: None

OS: Fedora release 30 (Thirty)
GCC version: (GCC) 9.1.1 20190503 (Red Hat 9.1.1-1)
CMake version: version 3.14.4

Python version: 3.7
Is CUDA available: No
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA

Versions of relevant libraries:
[pip3] numpy==1.17.0
[pip3] pytorch-pretrained-bert==0.6.2
[pip3] torch==1.2.0
[pip3] torchtext==0.4.0
[conda] Could not collect
tensor([])

Traceback (most recent call last):

  File "/home/dreiss/work/venv-pt-local/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "&lt;ipython-input-176-270f64c79839&gt;", line 13, in &lt;module&gt;
    torch.jit.script(m)

  File "/home/dreiss/work/pytorch/torch/jit/__init__.py", line 1267, in script
    return torch.jit._recursive.create_script_module(obj, torch.jit._recursive.infer_methods_to_compile)

  File "/home/dreiss/work/pytorch/torch/jit/_recursive.py", line 305, in create_script_module
    return create_script_module_impl(nn_module, concrete_type, stubs_fn)

  File "/home/dreiss/work/pytorch/torch/jit/_recursive.py", line 317, in create_script_module_impl
    stubs = stubs_fn(nn_module)

  File "/home/dreiss/work/pytorch/torch/jit/_recursive.py", line 511, in infer_methods_to_compile
    stubs.append(make_stub_from_method(nn_module, method))

  File "/home/dreiss/work/pytorch/torch/jit/_recursive.py", line 41, in make_stub_from_method
    return make_stub(func)

  File "/home/dreiss/work/pytorch/torch/jit/_recursive.py", line 34, in make_stub
    ast = torch.jit.get_jit_def(func, self_name="RecursiveScriptModule")

  File "/home/dreiss/work/pytorch/torch/jit/frontend.py", line 167, in get_jit_def
    py_ast = ast.parse(dedent_src)

  File "/usr/lib64/python3.7/ast.py", line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)

  File "&lt;unknown&gt;", line 1
    def forward(self):
    ^
IndentationError: unexpected indent
</code></pre></div>
<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/suo/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/suo">@suo</a></p>
<p dir="auto">This can cause issues when you have comments at a lower indentation than the rest of the code:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import torch

class Foo(torch.nn.Module):
    def forward(self, x):
        x = torch.neg(x)
# foo foo foo i have a comment at the wrong indent
        return x

torch.jit.script(Foo())"><pre class="notranslate"><code class="notranslate">import torch

class Foo(torch.nn.Module):
    def forward(self, x):
        x = torch.neg(x)
# foo foo foo i have a comment at the wrong indent
        return x

torch.jit.script(Foo())
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;ought.py&quot;, line 9, in &lt;module&gt;
    torch.jit.script(Foo())
  File &quot;/home/jamesreed/pytorch/torch/jit/__init__.py&quot;, line 1159, in script
    return torch.jit.torch.jit._recursive.recursive_script(obj)
  File &quot;/home/jamesreed/pytorch/torch/jit/_recursive.py&quot;, line 166, in recursive_script
    stubs = list(map(make_stub, filtered_methods))
  File &quot;/home/jamesreed/pytorch/torch/jit/_recursive.py&quot;, line 163, in make_stub
    return torch.jit.script_method(func, _jit_internal.createResolutionCallbackFromClosure(func))
  File &quot;/home/jamesreed/pytorch/torch/jit/__init__.py&quot;, line 1224, in script_method
    ast = get_jit_def(fn, self_name=&quot;ScriptModule&quot;)
  File &quot;/home/jamesreed/pytorch/torch/jit/frontend.py&quot;, line 164, in get_jit_def
    py_ast = ast.parse(dedent_src)
  File &quot;/home/jamesreed/miniconda3/lib/python3.6/ast.py&quot;, line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File &quot;&lt;unknown&gt;&quot;, line 1
    def forward(self, x):
    ^
IndentationError: unexpected indent"><pre class="notranslate"><code class="notranslate">Traceback (most recent call last):
  File "ought.py", line 9, in &lt;module&gt;
    torch.jit.script(Foo())
  File "/home/jamesreed/pytorch/torch/jit/__init__.py", line 1159, in script
    return torch.jit.torch.jit._recursive.recursive_script(obj)
  File "/home/jamesreed/pytorch/torch/jit/_recursive.py", line 166, in recursive_script
    stubs = list(map(make_stub, filtered_methods))
  File "/home/jamesreed/pytorch/torch/jit/_recursive.py", line 163, in make_stub
    return torch.jit.script_method(func, _jit_internal.createResolutionCallbackFromClosure(func))
  File "/home/jamesreed/pytorch/torch/jit/__init__.py", line 1224, in script_method
    ast = get_jit_def(fn, self_name="ScriptModule")
  File "/home/jamesreed/pytorch/torch/jit/frontend.py", line 164, in get_jit_def
    py_ast = ast.parse(dedent_src)
  File "/home/jamesreed/miniconda3/lib/python3.6/ast.py", line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "&lt;unknown&gt;", line 1
    def forward(self, x):
    ^
IndentationError: unexpected indent
</code></pre></div>
<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/suo/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/suo">@suo</a></p>