mshr-h commented on code in PR #17875:
URL: https://github.com/apache/tvm/pull/17875#discussion_r2055168695
##########
python/tvm/relax/frontend/torch/fx_translator.py:
##########
@@ -132,6 +132,54 @@ def convert(node: fx.Node) -> relax.Var:
return convert
+ ########## Binary Ops ##############
+
+ def _binary_op_inplace(self, relax_op: Callable, intrinsic_op: Callable)
-> Callable:
+ from torch import fx
+
+ def convert(node: fx.Node) -> relax.Var:
+ def promote_binary_op_args(lhs, rhs):
+ if isinstance(lhs, relax.Expr) and isinstance(rhs, relax.Expr):
+ return lhs, rhs
+ elif isinstance(lhs, relax.Expr):
+ assert isinstance(lhs.struct_info, relax.TensorStructInfo)
+ return lhs, relax.const(rhs, lhs.struct_info.dtype)
+ elif isinstance(rhs, relax.Expr):
+ assert isinstance(rhs.struct_info, relax.TensorStructInfo)
+ return relax.const(lhs, rhs.struct_info.dtype), rhs
+ else:
+ assert False
+
+ def call_binary_op(op, lhs, rhs):
+ lhs, rhs = promote_binary_op_args(lhs, rhs)
+ return self.block_builder.emit(op(lhs, rhs))
+
+ lhs, rhs = self.retrieve_args(node)
+ if isinstance(lhs, relax.Var) or isinstance(rhs, relax.Var):
Review Comment:
Can we avoid implementing separate converters for in-place and non-in-place
ops? I think the following logic should handle both cases. What do you think?
@kavin-sai-krishna
```python
if isinstance(lhs, relax.Var) or isinstance(rhs, relax.Var):
output = call_binary_op(relax_op, lhs, rhs)
elif isinstance(lhs, relax.expr.Constant):
output = call_binary_op(
relax_op, lhs, relax.const(rhs,
dtype=lhs.struct_info.dtype)
)
elif isinstance(rhs, relax.expr.Constant):
output = call_binary_op(
relax_op, relax.const(lhs, dtype=rhs.struct_info.dtype),
rhs
)
else:
output = intrinsic_op(lhs, rhs)
self.env[node.args[0]] = output
return output
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]