Skip to content

[Bug] Weird behaviors for relay.sum and relay.cast op #13796

Description

@Kevin-XiongC
import tvm
from tvm import relay
from tvm.contrib import graph_executor
import numpy as np
x=relay.var("x",shape=[2,2],dtype="int32")
t1=relay.cast(x,"bool")
y=relay.sum(t1)
f = relay.Function([x],relay.Tuple([t1,y]))
mod = tvm.IRModule({"main":f})
target = "llvm"
with tvm.transform.PassContext(opt_level=3 ):
    lib = relay.build(mod, target=target)
m = graph_executor.GraphModule(lib['default'](tvm.cpu()))
x = np.ones((2,2)).astype("int32")
m.set_input("x",x)
print(m.get_output(0))
print(m.get_output(1))

The code snippets above should be equivalent to below:

x = np.ones((2,2)).astype("int32").astype(bool)
print(x)
print(np.sum(x,dtype=bool))

Expected behavior

The relay program should always produce t1 all true and y true just like the numpy-based code outputs below. In common sense, it is expected to view non-zero elements as True and sum should perform reduction by OR operation for bool data type.

[[ True  True]
 [ True  True]]
True

Actual behavior

However, the relay-based code produce rather random results.

[[ True False]
 [False False]]
True

Or

[[False False]
 [False False]]
False

Even

[[False False]
 [False False]]
True

Environment

commit id: 1258863
OS: ubuntu 20.04 or macos
LLVM 14

Steps to reproduce

Run the code snippets above.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triagePRs or issues that need to be investigated by maintainers to find the right assignees to address ittype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions