Torchscript results in AttributeError: 'Call' object has no attribute 'id'

username: fermat97
cooked: <p>I try to TorchScript my function with <code>@torch.jit.script</code> decoration, which itself calls other functions from another module. But I ended up with the following error.</p>
<blockquote>
<p>File “/venv/lib/python3.7/site-packages/torch/jit/<strong>init</strong>.py”, line 1551, in script<br>
fn = torch._C._jit_script_compile(qualified_name, ast, _rcb, get_default_args(obj))<br>
File “/venv/lib/python3.7/site-packages/torch/jit/_recursive.py”, line 583, in try_compile_fn<br>
return torch.jit.script(fn, _rcb=rcb)<br>
File “/venv/lib/python3.7/site-packages/torch/jit/<strong>init</strong>.py”, line 1547, in script<br>
ast = get_jit_def(obj, obj.<strong>name</strong>)<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 185, in get_jit_def<br>
return build_def(ctx, fn_def, type_line, def_name, self_name=self_name)<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 219, in build_def<br>
build_stmts(ctx, body))<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 126, in build_stmts<br>
stmts = [build_stmt(ctx, s) for s in stmts]<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 126, in <br>
stmts = [build_stmt(ctx, s) for s in stmts]<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 193, in <strong>call</strong><br>
return method(ctx, node)<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 384, in build_If<br>
build_stmts(ctx, stmt.body),<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 126, in build_stmts<br>
stmts = [build_stmt(ctx, s) for s in stmts]<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 126, in <br>
stmts = [build_stmt(ctx, s) for s in stmts]<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 193, in <strong>call</strong><br>
return method(ctx, node)<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 384, in build_If<br>
build_stmts(ctx, stmt.body),<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 126, in build_stmts<br>
stmts = [build_stmt(ctx, s) for s in stmts]<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 126, in <br>
stmts = [build_stmt(ctx, s) for s in stmts]<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 193, in <strong>call</strong><br>
return method(ctx, node)<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 385, in build_If<br>
build_stmts(ctx, stmt.orelse))<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 126, in build_stmts<br>
stmts = [build_stmt(ctx, s) for s in stmts]<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 126, in <br>
stmts = [build_stmt(ctx, s) for s in stmts]<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 193, in <strong>call</strong><br>
return method(ctx, node)<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 384, in build_If<br>
build_stmts(ctx, stmt.body),<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 126, in build_stmts<br>
stmts = [build_stmt(ctx, s) for s in stmts]<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 126, in <br>
stmts = [build_stmt(ctx, s) for s in stmts]<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 193, in <strong>call</strong><br>
return method(ctx, node)<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 413, in build_With<br>
return With(r, build_withitems(ctx, stmt.items), build_stmts(ctx, stmt.body))<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 121, in build_withitems<br>
items = [build_withitem(ctx, i) for i in items]<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 121, in <br>
items = [build_withitem(ctx, i) for i in items]<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 193, in <strong>call</strong><br>
return method(ctx, node)<br>
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 282, in build_withitem<br>
end = start + len(item.context_expr.id)<br>
AttributeError: ‘Call’ object has no attribute ‘id’</p>
</blockquote>
<p>The <code>qualified_name</code> in <code>torch._C._jit_script_compile(qualified_name, ast, _rcb, get_default_args(obj))</code> is referenced to a simple function which does not call any <code>PyTorch</code> module.<br>
I use <code>PyTorch 1.6.0</code> with <code>Python 3.7</code>. Any idea?</p>

username: fermat97
cooked: <p>It seems changing the <code>WithItemBuilder</code> class from (PyTorch 1.6.0):</p>
<pre><code class="lang-auto">class WithItemBuilder(Builder):
    @staticmethod
    def build_withitem(ctx, item):
        lineno = item.context_expr.lineno
        start = item.context_expr.col_offset
        op_vars = item.optional_vars
        if op_vars:
            end = op_vars.col_offset + len(op_vars.id)
        else:
            end = start + len(item.context_expr.id)

        r = ctx.make_range(lineno, start, end)

        return WithItem(r, build_expr(ctx, item.context_expr), build_expr(ctx, op_vars) if op_vars else None)
</code></pre>
<p>to (PyTorch master branch)</p>
<pre><code class="lang-auto">class WithItemBuilder(Builder):
    @staticmethod
    def build_withitem(ctx, item):
        lineno = item.context_expr.lineno
        start = item.context_expr.col_offset
        end = start + len(pretty_node_names[ast.With])
        op_vars = item.optional_vars
        r = ctx.make_range(lineno, start, end)

        return WithItem(r, build_expr(ctx, item.context_expr), build_expr(ctx, op_vars) if op_vars else None)
</code></pre>
<p>would resolve the issue, however I am not sure if this is really the fix to the problem.</p>

username: SplitInfinity
cooked: <p>The problem is that the first implementation of <code>WithItemBuilder</code> that you linked makes use of features of the <code>ast</code> module that were introduced in <code>python3.8</code>. The second one should be compatible with 3.8 as well as older versions and was introduced because of a previous bug report that <code>WithItemBuilder</code> did not work in 3.6.</p>