I want to add a relay operator and package this operator as a relay pass, but when executing, it prompts segmentation fault Error, unable to perform pass operation, can someone help me? Thanks
when i use
custom_op0 = relay.op.get("add")
and it can run normally as i expected.
import numpy as np
import tvm
import tvm.relay as relay
def example():
shape = (1, 64, 54, 54)
c_data = np.empty(shape).astype("float32")
c = relay.const(c_data)
weight = relay.var("weight", shape=(64, 64, 3, 3))
x = relay.var("x", relay.TensorType((1, 64, 56, 56), "float32"))
conv = relay.nn.conv2d(x, weight)
y = relay.add(c, c)
y = relay.multiply(y, relay.const(2, "float32"))
y = relay.add(conv, y)
z = relay.add(y, c)
z1 = relay.add(y, c)
z2 = relay.add(z, z1)
return relay.Function([x, weight], z2)
@relay.op.op.register_compute("new_add")
def new_add_compute(data1,data2):
return relay.add(data1,data2)
@relay.transform.function_pass(opt_level=3)
class CustomPipeline:
"""Simple test function to replace one argument to another."""
def __init__(self):
pass
def transform_function(self, func, mod, ctx):
class ConvAdd(tvm.relay.ExprMutator):
def visit_call(self, callnode):
if (callnode.op.name == "add"):
custom_op0 = relay.op.get("new_add")
#custom_op0 = relay.op.get("add")
new_call = relay.Call(custom_op0, [callnode.args[1], callnode.args[0]])
return super().visit_call(new_call)
else:
return super().visit_call(callnode)
return ConvAdd().visit(func)
if __name__ == '__main__':
f = example()
mod = tvm.IRModule.from_expr(f)
custom_pass = CustomPipeline()
print(mod)
mod3 = custom_pass(mod)
print(mod3)