# Buggy code
def _forward(self, x):
    # type: (Tensor) -> Tuple[Tensor, Optional[Tensor], Optional[Tensor]]
    # N x 3 x 224 x 224
    x = self.conv1(x)
    # N x 64 x 112 x 112
    x = self.maxpool1(x)
    # N x 64 x 56 x 56
    x = self.conv2(x)
    # N x 64 x 56 x 56
    x = self.conv3(x)
    # N x 192 x 56 x 56
    x = self.maxpool2(x)

    # N x 192 x 28 x 28
    x = self.inception3a(x)
    # N x 256 x 28 x 28
    x = self.inception3b(x)
    # N x 480 x 28 x 28
    x = self.maxpool3(x)
    # N x 480 x 14 x 14
    x = self.inception4a(x)
    # N x 512 x 14 x 14
    aux_defined = self.training and self.aux_logits
    if aux_defined:
        aux1 = self.aux1(x)
    else:
        aux1 = None

    x = self.inception4b(x)
    # N x 512 x 14 x 14
    x = self.inception4c(x)
    # N x 512 x 14 x 14
    x = self.inception4d(x)
    # N x 528 x 14 x 14
    if aux_defined:
        aux2 = self.aux2(x)
    else:
        aux2 = None

    x = self.inception4e(x)
    # N x 832 x 14 x 14
    x = self.maxpool4(x)
    # N x 832 x 7 x 7
    x = self.inception5a(x)
    # N x 832 x 7 x 7
    x = self.inception5b(x)
    # N x 1024 x 7 x 7

    x = self.avgpool(x)
    # N x 1024 x 1 x 1
    x = torch.flatten(x, 1)
    # N x 1024
    x = self.dropout(x)
    x = self.fc(x)
    # N x 1000 (num_classes)
    return x, aux2, aux1


# Traceback (RuntimeError)
Traceback (most recent call last):
File "quantize_resnet.py", line 181, in <module>
main()
File "quantize_resnet.py", line 138, in main
m = torch.jit.script(model.float().eval())
File "/data/users/jerryzh/anaconda3/envs/py3/lib/python3.7/site-packages/torch/jit/__init__.py", line 1267, in script
return torch.jit._recursive.create_script_module(obj, torch.jit._recursive.infer_methods_to_compile)
File "/data/users/jerryzh/anaconda3/envs/py3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 305, in create_script_module
return create_script_module_impl(nn_module, concrete_type, stubs_fn)
File "/data/users/jerryzh/anaconda3/envs/py3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 352, in create_script_module_impl
create_methods_from_stubs(concrete_type, stubs)
File "/data/users/jerryzh/anaconda3/envs/py3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 279, in create_methods_from_stubs
concrete_type._create_methods(defs, rcbs, defaults)
File "/data/users/jerryzh/anaconda3/envs/py3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 578, in compile_unbound_method
create_methods_from_stubs(concrete_type, (stub,))
File "/data/users/jerryzh/anaconda3/envs/py3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 279, in create_methods_from_stubs
concrete_type._create_methods(defs, rcbs, defaults)
RuntimeError:
Module 'GoogLeNet' has no attribute 'aux1' :
File "/data/users/jerryzh/anaconda3/envs/py3/lib/python3.7/site-packages/torchvision-0.6.0a0+b2e9565-py3.7-linux-x86_64.egg/torchvision/models/googlenet.py", line 156
aux_defined = self.training and self.aux_logits
if aux_defined:
aux1 = self.aux1(x)
~~~~~~~~~ <--- HERE
else:
aux1 = None
'GoogLeNet._forward' is being compiled since it was called from 'GoogLeNet.forward'
File "/data/users/jerryzh/anaconda3/envs/py3/lib/python3.7/site-packages/torchvision-0.6.0a0+b2e9565-py3.7-linux-x86_64.egg/torchvision/models/googlenet.py", line 200
# type: (Tensor) -> GoogLeNetOutputs
x = self._transform_input(x)
x, aux1, aux2 = self._forward(x)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
aux_defined = self.training and self.aux_logits
if torch.jit.is_scripting():

# Fixed code
def _forward(self, x):
    # type: (Tensor) -> Tuple[Tensor, Optional[Tensor], Optional[Tensor]]
    # N x 3 x 224 x 224
    x = self.conv1(x)
    # N x 64 x 112 x 112
    x = self.maxpool1(x)
    # N x 64 x 56 x 56
    x = self.conv2(x)
    # N x 64 x 56 x 56
    x = self.conv3(x)
    # N x 192 x 56 x 56
    x = self.maxpool2(x)

    # N x 192 x 28 x 28
    x = self.inception3a(x)
    # N x 256 x 28 x 28
    x = self.inception3b(x)
    # N x 480 x 28 x 28
    x = self.maxpool3(x)
    # N x 480 x 14 x 14
    x = self.inception4a(x)
    # N x 512 x 14 x 14
    aux1 = torch.jit.annotate(Optional[Tensor], None)
    if self.aux1 is not None:
        if self.training:
            aux1 = self.aux1(x)

    x = self.inception4b(x)
    # N x 512 x 14 x 14
    x = self.inception4c(x)
    # N x 512 x 14 x 14
    x = self.inception4d(x)
    # N x 528 x 14 x 14
    aux2 = torch.jit.annotate(Optional[Tensor], None)
    if self.aux2 is not None:
        if self.training:
            aux2 = self.aux2(x)

    x = self.inception4e(x)
    # N x 832 x 14 x 14
    x = self.maxpool4(x)
    # N x 832 x 7 x 7
    x = self.inception5a(x)
    # N x 832 x 7 x 7
    x = self.inception5b(x)
    # N x 1024 x 7 x 7

    x = self.avgpool(x)
    # N x 1024 x 1 x 1
    x = torch.flatten(x, 1)
    # N x 1024
    x = self.dropout(x)
    x = self.fc(x)
    # N x 1000 (num_classes)
    return x, aux2, aux1