[TensorIR] Error, object of type 'Var' has no len?

Hello everyone, I’m going through tvm tutorial Blitz Course to TensorIR ,
The code is here:

import tvm
from tvm.ir.module import IRModule
from tvm.script import tir as T
import numpy as np


@tvm.script.ir_module
class MyModule:
    @T.prim_func
    def main(a: T.handle, b: T.handle):
        # We exchange data between function by handles, which are similar to pointer.        T.func_attr({"global_symbol": "main", "tir.noalias": True})
        # Create buffer from handles.
        A = T.match_buffer(a, (8,), dtype="float32")
        B = T.match_buffer(b, (8,), dtype="float32")
        for i in range(8):
            # A block is an abstraction for computation.
            with T.block("B"):
                # Define a spatial block iterator and bind it to value i.
                vi = T.axis.spatial(8, i)
                B[vi] = A[vi] + 1.0


ir_module = MyModule
print(type(ir_module))
print(ir_module.script())

However, when I ran it, I got an error object of type ‘Var’ has no len()

what is the problem? Thanks.

This seems to work for me on the latest main branch of TVM. Could you tell me which version/commit of TVM you’re using?

I’m using the latest released version tvm-v0.10.0

Then I run with TVM_BACKTRACE=1 environment variable to display a backtrace,

Traceback (most recent call last):
  File "/root/tvm-v0.10.0/python/tvm/script/utils.py", line 99, in call_with_error_reporting
    return func(*args, **kwargs)
  File "/root/tvm-v0.10.0/python/tvm/script/tir/special_stmt.py", line 136, in match_buffer
    if not isinstance(self.node, ast.Assign) or not len(self.node.lhs) == 1:
TypeError: object of type 'Var' has no len()

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "tir_ast.py", line 8, in <module>
    class MyModule:
  File "tir_ast.py", line 10, in MyModule
    def main(a: T.handle, b: T.handle):
  File "/root/tvm-v0.10.0/python/tvm/script/tir/prim_func.py", line 40, in prim_func
    result = from_source(input_func)
  File "/root/tvm-v0.10.0/python/tvm/script/parser.py", line 1368, in from_source
    result = to_ast(input_func, TVMDiagnosticCtx(), parser)
  File "/root/anaconda3/envs/tvm/lib/python3.8/site-packages/synr/compiler.py", line 719, in to_ast
    transformed = transformer.do_transform(prog, diagnostic_ctx)
  File "/root/anaconda3/envs/tvm/lib/python3.8/site-packages/synr/transformer.py", line 30, in do_transform
    return self.transform(node)
  File "/root/tvm-v0.10.0/python/tvm/script/parser.py", line 195, in transform
    transform_res = visitor(node)
  File "/root/tvm-v0.10.0/python/tvm/script/parser.py", line 363, in transform_Module
    return self.transform(next(iter(node.funcs.values())))
  File "/root/tvm-v0.10.0/python/tvm/script/parser.py", line 195, in transform
    transform_res = visitor(node)
  File "/root/tvm-v0.10.0/python/tvm/script/parser.py", line 494, in transform_Function
    body = self.parse_body(node.body)
  File "/root/tvm-v0.10.0/python/tvm/script/parser.py", line 235, in parse_body
    res = self.transform(stmt)
  File "/root/tvm-v0.10.0/python/tvm/script/parser.py", line 195, in transform
    transform_res = visitor(node)
  File "/root/tvm-v0.10.0/python/tvm/script/parser.py", line 598, in transform_Assign
    func.handle(node, self.context, arg_list, node.rhs.func_name.span)
  File "/root/tvm-v0.10.0/python/tvm/script/tir/special_stmt.py", line 94, in handle
    return call_with_error_reporting(
  File "/root/tvm-v0.10.0/python/tvm/script/utils.py", line 105, in call_with_error_reporting
    report_error(error_msg, node_span)
  File "/root/tvm-v0.10.0/python/tvm/script/context_maintainer.py", line 246, in report_error
    self._report_error(message, span)
  File "/root/tvm-v0.10.0/python/tvm/script/parser.py", line 219, in report_error
    self.error(message, span)
  File "/root/anaconda3/envs/tvm/lib/python3.8/site-packages/synr/transformer.py", line 34, in error
    self._diagnostic_context.emit("error", message, span)
  File "/root/tvm-v0.10.0/python/tvm/script/diagnostics.py", line 52, in emit
    self.diag_ctx.render()  # Raise exception on the first error we hit. TODO remove
  File "/root/tvm-v0.10.0/python/tvm/ir/diagnostics/__init__.py", line 119, in render
    _ffi_api.DiagnosticContextRender(self)
  File "/root/tvm-v0.10.0/python/tvm/_ffi/_ctypes/packed_func.py", line 237, in __call__
    raise get_last_ffi_error()
tvm.error.DiagnosticError: Traceback (most recent call last):
  2: TVMFuncCall
  1: tvm::runtime::PackedFuncObj::Extractor<tvm::runtime::PackedFuncSubObj<tvm::runtime::TypedPackedFunc<void (tvm::DiagnosticContext)>::AssignTypedLambda<tvm::{lambda(tvm::DiagnosticContext)#9}>(tvm::{lambda(tvm::DiagnosticContext)#9}, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)::{lambda(tvm::runtime::TVMArgs const&, tvm::runtime::TVMRetValue*)#1}> >::Call(tvm::runtime::PackedFuncObj const*, tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*)
  0: tvm::DiagnosticContext::Render()
  File "/root/tvm-v0.10.0/src/ir/diagnostic.cc", line 105
DiagnosticError: one or more error diagnostics were emitted, please check diagnostic render for output.

I don’t know what to do. :cold_sweat:

Thanks for your information. I’m not sure what happens either. Since every commit merged into the codebase passes the CI, which includes the tutorial, it should work.

BTW, we’ve refactored the TVMScript parser recently. Could you please upgrade your TVM version to v0.11 or latest main branch and try again?

1 Like

Thanks for your reply. I switched my work to a Linux machine and passed the case above.
I guess there are something wrong with my current work platform(WSL1 Ubuntu18.04).
I will consider upgrading my TVM version to v0.11 in the next few days.

Seems a bug in the old parser that depends on synr, which is probably a bit outdated. Please try building from source using the latest apache/tvm:main

1 Like

Just as you mentioned, my current tvm version is 0.10.0, and I upgrade my python module synr from 0.4 to 0.6 (0.5 also works), using command

pip install synr==0.6

the problem is solved.

Now I’m upgrading my TVM version from source and trying to build it.

The latest TVM in mainline does not depend on synr and is adopting a more generalizable approach, so I would expect those parsing issues to be going away

@junrushao Exactly, you are right!
I upgraded my TVM to the latest version in mainline(v0.11.dev0), and passed the case above with synr uninstalled.