How can we pass tuple inputs to a Relay function?


#1

I have this simple code to to a getitem on an input tuple

import tvm
from tvm import relay

x = relay.var('x', type_annotation=relay.ty.TupleType([
    relay.ty.TensorType((), 'int64'),
    relay.ty.TensorType((), 'int64')]))

fn = relay.Function([x], relay.expr.TupleGetItem(x, 0))
mod = relay.Module({})
gv = relay.GlobalVar('fn')
mod[gv] = fn
mod.entry_func = gv
mod[gv] = relay.ir_pass.infer_type(mod[gv], mod=mod)

ctx = tvm.cpu()
target = tvm.target.create('llvm')
exec = relay.build_module.create_executor(mod=mod, ctx=ctx, target=target)
f = exec.evaluate(gv)

The problem is that I can’t figure out how to pass the tuple as input to f

I’ve tried:

f((1, 2)) -> AttributeError: ‘tuple’ object has no attribute ‘dtype’
f(relay.const((1, 2))) -> AttributeError: ‘tuple’ object has no attribute ‘dtype’

I’ve also tried this but I didn’t have much faith:
f(relay.backend.interpreter.TupleValue( relay.backend.interpreter.TensorValue(tvm.ndarray.array(1)), relay.backend.interpreter.TensorValue(tvm.ndarray.array(2)))) -> AttributeError: ‘<class ‘tvm.relay.backend.interpreter.TupleValue’>’ object has no attribute ‘dtype’

There may be something obvious that I’m missing.


#2

You can try passing a numpy.ndarray, they have the attribute dtype.
Hope it helps.


#3

But I want to pass in a tuple. And a tuple can contain things that will not fit in a numpy.ndarray.


#4

See https://github.com/dmlc/tvm/pull/3349, thanks for reporting the issue.


#5

It seems that this has been fixed, how do you pass a tuple as input then?


#6

You can just use a python tuple now. It will be converted properly.


#7

maybe try this:
Tuple_data.fields
to get the data