Issue 1:
🐛 Bug

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-/).

To Reproduce

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)

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 "<ipython-input-176-270f64c79839>", line 13, in <module>
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 "<unknown>", line 1
def forward(self):
^
IndentationError: unexpected indent

cc @suo

Issue 2:
This can cause issues when you have comments at a lower indentation than the rest of the code:

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())

Traceback (most recent call last):
File "ought.py", line 9, in <module>
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 "<unknown>", line 1
def forward(self, x):
^
IndentationError: unexpected indent

cc @suo