Skip to content

API change in Notebook 6 - DynTensorType throwing errors #28

@gregtozzi

Description

@gregtozzi

Thank you for an excellent course!

Running this block from notebook 6:

A = relax.Var("A", (128, 128), relax.DynTensorType(2, "float32"))
B = relax.Var("B", (128, 128), relax.DynTensorType(2, "float32"))

Results in:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[17], line 1
----> 1 A = relax.Var("A", (128, 128), relax.DynTensorType(2, "float32"))
      2 B = relax.Var("B", (128, 128), relax.DynTensorType(2, "float32"))

File ~/opt/anaconda3/envs/mlcai/lib/python3.9/site-packages/tvm/relax/expr.py:416, in Var.__init__(self, name_hint, struct_info, span)
    414     struct_info = tvm.runtime.convert_to_object(struct_info)
    415     if not isinstance(struct_info, StructInfo):
--> 416         raise TypeError(
    417             "struct_info needs to be an instance of StructInfo. "
    418             "If you attempt to pass in shape, "
    419             "use relax.TensorStructInfo(shape, dtype)."
    420         )
    421 self.__init_handle_by_constructor__(
    422     _ffi_api.Var if isinstance(name_hint, str) else _ffi_api.VarFromId,  # type: ignore
    423     name_hint,
    424     struct_info,
    425     span,
    426 )

TypeError: struct_info needs to be an instance of StructInfo. If you attempt to pass in shape, use relax.TensorStructInfo(shape, dtype).

Modifying the original block to the block below works:

A = relax.Var("A", relax.TensorStructInfo([128, 128], "float32"))
B = relax.Var("x", relax.TensorStructInfo([128, 128], "float32"))

But I can't tell from following the code on TVM's Unity branch if this change is consistent with the original intent of using relax.DynTensorType.

Likewise, under Create Map Function, from_fx contains the line:

input_var = relax.Var(
    node.target, shape, relax.DynTensorType(len(shape), "float32")
)

Which throws:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[27], line 10
      7     A = node_map[node.args[0]]
      8     return bb.emit_te(te_relu, A)
---> 10 MyModule = from_fx(
     11     fx_module, 
     12     input_shapes = [(1, 128)], 
     13     call_function_map = {
     14       torch.matmul: map_matmul,
     15       torch.relu: map_relu, 
     16     },
     17     call_module_map={},
     18 )
     20 MyModule.show()

Cell In[26], line 33, in from_fx(fx_mod, input_shapes, call_function_map, call_module_map)
     31 shape = input_shapes[input_index]
     32 input_index += 1 
---> 33 input_var = relax.Var(
     34     node.target, shape, relax.DynTensorType(len(shape), "float32")
     35 )
     36 fn_inputs.append(input_var)
     37 node_map[node] = input_var

File ~/opt/anaconda3/envs/mlcai/lib/python3.9/site-packages/tvm/relax/expr.py:416, in Var.__init__(self, name_hint, struct_info, span)
    414     struct_info = tvm.runtime.convert_to_object(struct_info)
    415     if not isinstance(struct_info, StructInfo):
--> 416         raise TypeError(
    417             "struct_info needs to be an instance of StructInfo. "
    418             "If you attempt to pass in shape, "
    419             "use relax.TensorStructInfo(shape, dtype)."
    420         )
    421 self.__init_handle_by_constructor__(
    422     _ffi_api.Var if isinstance(name_hint, str) else _ffi_api.VarFromId,  # type: ignore
    423     name_hint,
    424     struct_info,
    425     span,
    426 )

TypeError: struct_info needs to be an instance of StructInfo. If you attempt to pass in shape, use relax.TensorStructInfo(shape, dtype).

Here, a similar modification seems to fix the problem (but I'm still concerned that I don't understand if we're losing something by giving up on relax.DynTensorType):

input_var = relax.Var(
    node.target, relax.TensorStructInfo(shape, "float32")
)

Thanks in advance for your assistance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions