Confused about Torch.Jit.Script error message

haris525: <p>Hello, good morning. I am relatively new to Pytorch but liking it a lot! I have a BERT classification model that I am able to save, load, and run inference on locally, additionally the model is trained using nvidia CUDA GPU. <strong>Torch version is 1.10.0+cu102</strong> on Windows 10 64 bit .Up to this point the model loads fine</p>
<p>model.to(device)<br>
model.load_state_dict(torch.load(‘MyModelPath/MyModel15.model’, map_location=torch.device(‘cpu’)))</p>
<p><strong>Here is where the problem starts for me</strong></p>
<p>scripted_model = Torch.Jit.Script(model)</p>
<p>throws this error message</p>
<p>Traceback (most recent call last):<br>
File “C:\Users\javedh\lib\site-packages\IPython\core\interactiveshell.py”, line 3457, in run_code<br>
exec(code_obj, self.user_global_ns, self.user_ns)<br>
File “”, line 1, in <br>
scripted_model = torch.jit.script(model)<br>
File “C:\Users\javedh\AppData\Roaming\Python\Python39\site-packages\torch\jit_script.py”, line 1257, in script<br>
return torch.jit._recursive.create_script_module(<br>
File “C:\Users\javedh\AppData\Roaming\Python\Python39\site-packages\torch\jit_recursive.py”, line 451, in create_script_module<br>
return create_script_module_impl(nn_module, concrete_type, stubs_fn)<br>
File “C:\Users\javedh\AppData\Roaming\Python\Python39\site-packages\torch\jit_recursive.py”, line 464, in create_script_module_impl<br>
property_stubs = get_property_stubs(nn_module)<br>
File “C:\Users\javedh\AppData\Roaming\Python\Python39\site-packages\torch\jit_recursive.py”, line 778, in get_property_stubs<br>
properties_asts = get_class_properties(module_ty, self_name=“RecursiveScriptModule”)<br>
File “C:\Users\javedh\AppData\Roaming\Python\Python39\site-packages\torch\jit\frontend.py”, line 161, in get_class_properties<br>
getter = get_jit_def(prop[1].fget, f"__{prop[0]}_getter", self_name=self_name)<br>
File “C:\Users\javedh\AppData\Roaming\Python\Python39\site-packages\torch\jit\frontend.py”, line 264, in get_jit_def<br>
return build_def(parsed_def.ctx, fn_def, type_line, def_name, self_name=self_name, pdt_arg_types=pdt_arg_types)<br>
File “C:\Users\javedh\AppData\Roaming\Python\Python39\site-packages\torch\jit\frontend.py”, line 315, in build_def<br>
build_stmts(ctx, body))<br>
File “C:\Users\javedh\AppData\Roaming\Python\Python39\site-packages\torch\jit\frontend.py”, line 137, in build_stmts<br>
stmts = [build_stmt(ctx, s) for s in stmts]<br>
File “C:\Users\javedh\AppData\Roaming\Python\Python39\site-packages\torch\jit\frontend.py”, line 137, in <br>
stmts = [build_stmt(ctx, s) for s in stmts]<br>
File “C:\Users\javedh\AppData\Roaming\Python\Python39\site-packages\torch\jit\frontend.py”, line 287, in <strong>call</strong><br>
return method(ctx, node)<br>
File “C:\Users\javedh\AppData\Roaming\Python\Python39\site-packages\torch\jit\frontend.py”, line 550, in build_Return<br>
return Return(r, None if stmt.value is None else build_expr(ctx, stmt.value))<br>
File “C:\Users\javedh\AppData\Roaming\Python\Python39\site-packages\torch\jit\frontend.py”, line 287, in <strong>call</strong><br>
return method(ctx, node)<br>
File “C:\Users\javedh\AppData\Roaming\Python\Python39\site-packages\torch\jit\frontend.py”, line 702, in build_Call<br>
args = [build_expr(ctx, py_arg) for py_arg in expr.args]<br>
File “C:\Users\javedh\AppData\Roaming\Python\Python39\site-packages\torch\jit\frontend.py”, line 702, in <br>
args = [build_expr(ctx, py_arg) for py_arg in expr.args]<br>
File “C:\Users\javedh\AppData\Roaming\Python\Python39\site-packages\torch\jit\frontend.py”, line 286, in <strong>call</strong><br>
raise UnsupportedNodeError(ctx, node)<br>
torch.jit.frontend.UnsupportedNodeError: GeneratorExp aren’t supported:<br>
File “C:\Users\javedh\lib\site-packages\transformers\modeling_utils.py”, line 987<br>
activations".<br>
“”"<br>
return any(hasattr(m, “gradient_checkpointing”) and m.gradient_checkpointing for m in self.modules())</p>
<p>I am not sure how to make sense of it in a meaningful way, it seems like a package support issue, but I could be wrong. I would appreciate any assistance in understanding this issue.</p>
<p>Thank you very much!</p>

ptrblck: <p>The error points to:</p>
<pre><code class="lang-python">torch.jit.frontend.UnsupportedNodeError: GeneratorExp aren’t supported
</code></pre>
<p>which is raised due to the used generator expression as seen in this minimal code snippet:</p>
<pre><code class="lang-python">class MyModel(nn.Module):
    def __init__(self):
        super().__init__()
        
    def forward(self, x):
        a = any(i for i in range(10))
        return x


model = MyModel()
x = torch.randn(1, 1)
out = model(x)

scripted = torch.jit.script(model)
# &gt; UnsupportedNodeError: GeneratorExp aren't supported
</code></pre>

haris525: <p>thank you so much ptrlck, any recommendations on how I can address this issue?</p>
<p>thanks again<br>
Haris</p>