Skip to content

[Bug] Misuse of reduce_axis #12373

@cxx122

Description

@cxx122
REDUCE_0 = te.reduce_axis((5, 0), name="REDUCE_0")
REDUCE_1 = te.reduce_axis((7, 0), name="REDUCE_1")
TENSOR_0 = te.placeholder([10,10,10,10], dtype="int8", name="TENSOR_0")
TENSOR_1 = te.compute([10,10], lambda mcq,uch:te.sum(expr=TENSOR_0[REDUCE_0,mcq,uch,REDUCE_1], axis=[REDUCE_0,REDUCE_1]), name ="TENSOR_1")

The args in the reduce_axis should be like (0,5), the args like (5,0) will cause a wrong result.

Expected behavior

An error message to point out the misuse.

Actual behavior

AssertionError: 
Not equal to tolerance rtol=1e-05, atol=1e-07

Mismatched elements: 100 / 100 (100%)
Max absolute difference: 92
Max relative difference: 1.
 x: array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],...
 y: array([[75, 68, 41, 69, 74, 68, 66, 63, 65, 72],
       [65, 67, 58, 88, 82, 74, 63, 87, 66, 66],
       [71, 69, 80, 69, 52, 71, 75, 51, 69, 72],...

Environment

Operating System: Ubuntu 18.04 TVM version: tag0.9.0 [d361585]

Steps to reproduce

import os
import numpy as np
import tvm
from tvm import te, auto_scheduler, topi
import tvm.testing

REDUCE_0 = te.reduce_axis((0, 5), name="REDUCE_0")
REDUCE_1 = te.reduce_axis((7, 0), name="REDUCE_1")
TENSOR_0 = te.placeholder([10,10,10,10], dtype="int8", name="TENSOR_0")
TENSOR_1 = te.compute([10,10], lambda mcq,uch:te.sum(expr=TENSOR_0[REDUCE_0,mcq,uch,REDUCE_1], axis=[REDUCE_0,REDUCE_1]), name ="TENSOR_1")
s = te.create_schedule(TENSOR_1.op)
tensor_list = [TENSOR_0,TENSOR_1]

dev = tvm.cpu(0)
pre_list = []
after_list = []
for tensor in tensor_list:
    shape = [x.value if 'value' in dir(x) and isinstance(x.value, int) else 1 for x in tensor.shape]
    params = (5*np.random.uniform(size=shape)).astype(tensor.dtype)
    pre_list.append(tvm.nd.array(params.copy(), dev))
    after_list.append(tvm.nd.array(params.copy(), dev))

pre_mod = tvm.lower(s, tensor_list, simple_mode=True)
with tvm.transform.PassContext(opt_level=4):
    f = tvm.build(pre_mod)
f(*pre_list)

# Schedule
TENSOR_1_mcq, TENSOR_1_uch, TENSOR_1_REDUCE_0, TENSOR_1_REDUCE_1 = tuple(TENSOR_1.op.axis) + tuple(TENSOR_1.op.reduce_axis)
TENSOR_1_REDUCE_0_REDUCE_1_fused = s[TENSOR_1].fuse(TENSOR_1_REDUCE_0, TENSOR_1_REDUCE_1)

now_mod = tvm.lower(s, tensor_list, simple_mode=True)
with tvm.transform.PassContext(opt_level=4):
    f = tvm.build(now_mod)
f(*after_list)

tvm.testing.assert_allclose(pre_list[1].numpy(), after_list[1].numpy(),rtol=1e-5)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions