Skip to content

Commit

Permalink
[BugFix] Use shape dtype on ArgReduce to determine return type (#12083)
Browse files Browse the repository at this point in the history
Fix ArgReduce automatic return type inference by forcing it to use the
datatype of the shape of the Tensor instead of the fixed Int32.

Including additional tests.
  • Loading branch information
everton1984 authored Jul 14, 2022
1 parent de3c0f4 commit b9fa576
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/relay/op/tensor/reduce.cc
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ bool GenericReduceRel(const Array<Type>& types, int num_inputs, const Attrs& att

// assign output type and shape
auto oshape = ReduceShapeImpl(in_shape, param, reporter);
reporter->Assign(types[1], TensorType(oshape, DataType::Int(32)));
reporter->Assign(types[1], TensorType(oshape, data->shape[0].dtype()));
return true;
}
/*!
Expand Down
38 changes: 38 additions & 0 deletions tests/python/relay/test_type_infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from tvm.relay import analysis, op, transform
from tvm.relay.op import op as _op

import numpy as np


def infer_mod(mod, annotate_spans=True):
if annotate_spans:
Expand Down Expand Up @@ -544,6 +546,42 @@ def test_repeat_register():
assert "Operator custom_log3 is registered before" in str(cm.execption)


def test_argreduce_infer_return_type():
x_shape = (1, 1)
broadcast_shape = [1, 1]
shape_dtypes = [("int32", lambda x: np.int32(x)), ("int64", lambda x: np.int64(x))]

# Testing with argmax
for (sdtype, conv) in shape_dtypes:
x = relay.var("data", relay.TensorType(x_shape, "float32"))
broadcast_to = relay.op.broadcast_to(x, relay.const(broadcast_shape, dtype=sdtype))
argmax = relay.op.argmax(broadcast_to, axis=[1])

f = relay.Function([x], argmax)
assert_has_type(
f,
relay.FuncType(
[relay.TensorType(broadcast_shape, "float32")],
relay.TensorType([conv(1)], dtype=sdtype),
),
)

# Testing with argmin
for (sdtype, conv) in shape_dtypes:
x = relay.var("data", relay.TensorType(x_shape, "float32"))
broadcast_to = relay.op.broadcast_to(x, relay.const(broadcast_shape, dtype=sdtype))
argmin = relay.op.argmin(broadcast_to, axis=[1])

f = relay.Function([x], argmin)
assert_has_type(
f,
relay.FuncType(
[relay.TensorType(broadcast_shape, "float32")],
relay.TensorType([conv(1)], dtype=sdtype),
),
)


if __name__ == "__main__":
import sys

Expand Down

0 comments on commit b9fa576

Please sign in to comment.