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:
```python
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:
```python
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





---
[Visit 
Topic](https://discuss.tvm.apache.org/t/round-tripping-objects-through-the-ffi/8440/1)
 to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click 
here](https://discuss.tvm.apache.org/email/unsubscribe/45bcb3fbc3b7c07a28386db5740a1fbbfda35a6c269e445aa754ff4c7930f78b).

Reply via email to