jverma-quic commented on code in PR #17214: URL: https://github.com/apache/tvm/pull/17214#discussion_r1699130275
########## python/tvm/contrib/hexagon/generate_take_op.py: ########## @@ -0,0 +1,86 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# pylint: disable=missing-docstring, invalid-name, unnecessary-comprehension, unused-argument + +import tvm +import tvm.testing +from tvm import relax +from tvm.contrib.hexagon import hexagon_unary_ops + + +def op_replace(call_node): + def is_op(op_name: str, call_node: relax.Call) -> bool: + if not isinstance(call_node, relax.Call): + return False + call_tir_op = tvm.ir.Op.get("relax.call_tir") + if call_node.op != call_tir_op: + return False + global_var = call_node.args[0] + return op_name in global_var.name_hint + + ops = ["tanh", "sqrt", "rsqrt", "exp", "erf", "sigmoid", "hardswish", "log", "abs"] + for op in ops: + if is_op(op, call_node): + return True + return False + + [email protected]_functor.mutator +class Tanh2TakeReplace(tvm.relax.PyExprMutator): + def __init__(self, mod: tvm.IRModule) -> None: + super().__init__(mod) + self.mod_ = mod + + def transform(self) -> tvm.IRModule: + # Iterate over all the nodes to check for the node replaceable + for global_var, func in self.mod_.functions.items(): + # Skip non-relax functions + if not isinstance(func, relax.Function): + continue + updated_func = self.visit_expr(func) + self.builder_.normalize(updated_func) + self.builder_.update_func(global_var, updated_func) + # At the end of the transformation we return the updated IRModule from the BlockBuilder. + return self.builder_.get() + + def visit_call_(self, call_node: relax.Call) -> relax.Call: + if call_node.args[1][0].struct_info.dtype == "uint8": Review Comment: > Should we verify whether the call_node is a `relax.call_tir` op before accessing the args? @quic-sanirudh: wouldn't it be guaranteed since we're only visiting the call nodes? ########## python/tvm/contrib/hexagon/generate_take_op.py: ########## @@ -0,0 +1,86 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# pylint: disable=missing-docstring, invalid-name, unnecessary-comprehension, unused-argument + +import tvm +import tvm.testing +from tvm import relax +from tvm.contrib.hexagon import hexagon_unary_ops + + +def op_replace(call_node): + def is_op(op_name: str, call_node: relax.Call) -> bool: + if not isinstance(call_node, relax.Call): + return False + call_tir_op = tvm.ir.Op.get("relax.call_tir") + if call_node.op != call_tir_op: + return False + global_var = call_node.args[0] + return op_name in global_var.name_hint Review Comment: I agree with you that relying on the global_var is not the best way to identify the operators for this transformation. However, I don't really think that operator_name will be much better. The problem here is that we lower the graph to Relay first and then during translation to Relax, the operator knowledge is lost. @Lunderberg's suggestion would have worked very well if we could have imported the graph directly to Relax and then before legalizing it, we could have replaced R.tanh with R.take(..). -- 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]
