Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoneType error when run torch.jit.script() #58

Open
YuantianGao opened this issue Aug 8, 2021 · 1 comment
Open

NoneType error when run torch.jit.script() #58

YuantianGao opened this issue Aug 8, 2021 · 1 comment

Comments

@YuantianGao
Copy link

Hello, I want to optimize the OFA subnet model but I meet the error:

Traceback (most recent call last):
  File "mobile_test.py", line 129, in <module>
    data_loader,bn_list[0],net_config))
  File "mobile_test.py", line 62, in prepare_subnet
    script_subnet = torch.jit.script(subnet)
  File "/home/yuantian/.local/lib/python3.6/site-packages/torch/jit/_script.py", line 943, in script
    obj, torch.jit._recursive.infer_methods_to_compile
  File "/home/yuantian/.local/lib/python3.6/site-packages/torch/jit/_recursive.py", line 391, in create_script_module
    return create_script_module_impl(nn_module, concrete_type, stubs_fn)
  File "/home/yuantian/.local/lib/python3.6/site-packages/torch/jit/_recursive.py", line 448, in create_script_module_impl
    script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
  File "/home/yuantian/.local/lib/python3.6/site-packages/torch/jit/_script.py", line 391, in _construct
    init_fn(script_module)
  File "/home/yuantian/.local/lib/python3.6/site-packages/torch/jit/_recursive.py", line 428, in init_fn
    scripted = create_script_module_impl(orig_value, sub_concrete_type, stubs_fn)
  File "/home/yuantian/.local/lib/python3.6/site-packages/torch/jit/_recursive.py", line 404, in create_script_module_impl
    property_stubs = get_property_stubs(nn_module)
  File "/home/yuantian/.local/lib/python3.6/site-packages/torch/jit/_recursive.py", line 697, in get_property_stubs
    properties_asts = get_class_properties(module_ty, self_name="RecursiveScriptModule")
  File "/home/yuantian/.local/lib/python3.6/site-packages/torch/jit/frontend.py", line 153, in get_class_properties
    getter = get_jit_def(prop[1].fget, f"__{prop[0]}_getter", self_name=self_name)
  File "/home/yuantian/.local/lib/python3.6/site-packages/torch/jit/frontend.py", line 271, in get_jit_def
    return build_def(ctx, fn_def, type_line, def_name, self_name=self_name)
  File "/home/yuantian/.local/lib/python3.6/site-packages/torch/jit/frontend.py", line 305, in build_def
    build_stmts(ctx, body))
  File "/home/yuantian/.local/lib/python3.6/site-packages/torch/jit/frontend.py", line 129, in build_stmts
    stmts = [build_stmt(ctx, s) for s in stmts]
  File "/home/yuantian/.local/lib/python3.6/site-packages/torch/jit/frontend.py", line 129, in <listcomp>
    stmts = [build_stmt(ctx, s) for s in stmts]
  File "/home/yuantian/.local/lib/python3.6/site-packages/torch/jit/frontend.py", line 279, in __call__
    return method(ctx, node)
  File "/home/yuantian/.local/lib/python3.6/site-packages/torch/jit/frontend.py", line 445, in build_Return
    return Return(r, None if stmt.value is None else build_expr(ctx, stmt.value))
  File "/home/yuantian/.local/lib/python3.6/site-packages/torch/jit/frontend.py", line 279, in __call__
    return method(ctx, node)
  File "/home/yuantian/.local/lib/python3.6/site-packages/torch/jit/frontend.py", line 781, in build_Dict
    return DictLiteral(range, [build_expr(ctx, e) for e in expr.keys],
  File "/home/yuantian/.local/lib/python3.6/site-packages/torch/jit/frontend.py", line 781, in <listcomp>
    return DictLiteral(range, [build_expr(ctx, e) for e in expr.keys],
  File "/home/yuantian/.local/lib/python3.6/site-packages/torch/jit/frontend.py", line 278, in __call__
    raise UnsupportedNodeError(ctx, node)
  File "/home/yuantian/.local/lib/python3.6/site-packages/torch/jit/frontend.py", line 111, in __init__
    source_range = ctx.make_range(offending_node.lineno,
AttributeError: 'NoneType' object has no attribute 'lineno'

And this is my code:

ofa_net.set_active_subnet(ks=net_config['ks'], d=net_config['d'], e=net_config['e'])
subnet = ofa_net.get_active_subnet()
    
bn_mean = bn_list[0]
bn_var = bn_list[1]
calib_bn(subnet,data_loader,bn_mean,bn_var)
subnet = QuantizedModel(subnet)
#import torchvision
#subnet = torchvision.models.quantization.mobilenet_v3_small(pretrained=True)
#When I run torchvision's model. it can run successfully.
#fuse model
print(subnet)
fuse_model(subnet) 
    
#quantize model
subnet.qconfig = torch.quantization.default_qconfig
subnet.qconfig = torch.quantization.get_default_qconfig('qnnpack')
torch.backends.quantized.engine = 'qnnpack'
torch.quantization.prepare(subnet, inplace=True)
# Calibrate
print('Calibrating........................')
#evaluate_ofa_subnet(subnet,
	#imagenet_data_path, net_config, data_loader, batch_size=16)
    
#Convert model
torch.quantization.convert(subnet, inplace=True)
#optimize
script_subnet = torch.jit.script(subnet)
script_subnet_optimized = optimize_for_mobile(script_subnet)
return script_subnet_optimized

I can make sure there is None value in the model because when I run the torchvision's model, It can run successfully.
How to solve the problem?

And this is my code link:
https://drive.google.com/drive/folders/172QoQxYoB1fUPvwjudHWgBmRv-IfE8Mg?usp=sharing
You can run without the data loader in test1.py and mobile_test.py is the version that with data loader.
The database link:
https://drive.google.com/file/d/1LY0ERp-egZr7NxTw3bApu2H93s9CQuYV/view?usp=sharing.

Kind regards.
Thanks very much.

@Fr1ck
Copy link

Fr1ck commented Jun 9, 2022

Hi, thanks for your question. I meet the same error when using torch.jit.script(). Do you address it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants