[hybrid script] can not output consistent result when executing hybrid script by relay interpreter

Hi all,
I register an operator named “nn.my_op”, its FTVMCompute attribute calls hybrid script compute_my_op function which is annotated by tvm.hybrid.script

@tvm.hybrid.script
def compute_my_op(x, y):
    # x, y both Tensor type
    res = …
    return res

RELAY_REGISTER_OP(“nn.my_op”)
.set_num_inputs(2)
.add_argument(“data1”, “Tensor”, “The input tensor.”)
.add_argument(“data2”, “Tensor”, “The input tensor.”)
.set_support_level(2)
.add_type_rel(" ", MyOpRel)
.set_attr(“FTVMCompute”, MyOpCompute);

when I create a nn.myop function node and execute it by relay interpreter, and when fed with the same input parameters, it output different matrix results which are sometimes correct, and sometimes wrong; moverover, some of the wrong results contain values of “nan” or “inf”

x = relay.var(‘x’, shape=(10, 10))
y = relay.var(‘y’, shape=(10, 10))
func = relay.Function([x, y], relay.nn.my_op(x, y))
intrp = create_executor(mod=mod, ctx=ctx, target=target)
for i in range(10):
    result = intrp.evaluate(expr)(*args)
    print(result, ‘\n’) ## output different result !!!

while i refer to test_hybrid_script.py and write test case with the following way, it works correctly

zz = compute_my_op(x, y)
s = tvm.create_schedule(C.op)
f = tvm.build(s, [x, y, zz], “llvm”)

a = tvm.ones((10, 10)).astype(‘float32’), ctx)
b = tvm.ones((10, 10)).astype(‘float32’), ctx)
c = tvm.nd.array(np.zeros((10, 10), dtype=a.dtype), ctx)
f(a, b, c) # The results c of each execution are the same

Does this mean relay interpreter can’t execute Op function whose computing calls hybrid script funciton?
or it is caused by something i did wrong, any idea how to solve this issue?

@were I found that the [hybrid script] module is developed by you, could you give me some tips? thank you!

Can you also show me the whole code snippet of your hybrid script?

Can you also show me how you get the op of your written hybrid function?

I can support you asap.

@were thanks for your kind reply.
my hybrid script func is as follows

@tvm.hybrid.script
def compute_my_op(x, y):
    #x.shape=(1, 1, 6, 6)
    #y.shape=(1, 1, 2, 2)
    batch = x.shape[0]
    channel = x.shape[1]
    height = y.shape[2]
    width = y.shape[3]
    kernel = (4, 4)
    strides = (2, 2)
    out = output_tensor(x.shape, y.dtype)

    for b in range(batch):
        for c in range(channel):
            for h in range(height):
                for w in range(width):
                    for i in range(h * strides[0], h * strides[0] + kernel[0])):
                        for j in range(w * strides[1], w * strides[1] + kernel[1])):
                            out[(b, c, i, j)] += y[(b, c, h, w)]

    return out

I also comment the statements of 6 for loop, then out is zero matrix, while out result is not when executed by relay interpreter

for you second question, i use tvm::runtime::Registry::Get() to get registered python function (named A) which is annotated with register_func(), and A calls hybrid script listed above.

thanks again for your help : )