Hi,
I’ve been implementing graph transformations in Python and sometimes it is handy to add annotations to nodes. Now, running these through an ExprMutator will give me all new nodes and they’re gone. But actually, they’re gone ealier than that:
x = tvm.relay.var('x', shape=(1,1))
x.my_annotation = 'something'
y = x * tvm.relay.const(2)
assert y.args[0] == x
y.args[0].my_annotation # AttributeError
The underlying reason is that TVM’s custom FFI doesn’t attach the Python object to the C object but just returns a new Python object pointing to the same C object.
I can work around it by keeping a dict originalizer = {o: o for o in all_original_objects}
and then do originalizer[y.args[0]]
, but that seems clumsy.
For reference, the same thing works better in other frameworks:
x = torch.tensor(1.0, requires_grad=True)
x.my_annotation = 'something'
y = x * 2
y.grad_fn.next_functions[0][0].variable.my_annotation # works!
Should TVM do the same?
Best regards
Thomas