Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/tvm/_ffi/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# ----------------------------
string_types = (str,)
integer_types = (int, np.int32)
numeric_types = integer_types + (float, np.float32)
numeric_types = integer_types + (float, np.float16, np.float32)

# this function is needed for python3
# to convert ctypes.char_p .value back to python str
Expand Down
10 changes: 9 additions & 1 deletion python/tvm/runtime/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@


def _convert(arg, cargs):
def _gettype(arg):
if isinstance(arg, np.float16):
return "float16"
elif isinstance(arg, (_base.integer_types, bool)):
return "int32"
else:
return "float32"

if isinstance(arg, Object):
cargs.append(arg)
elif isinstance(arg, np.ndarray):
Expand All @@ -45,7 +53,7 @@ def _convert(arg, cargs):
_convert(field, field_args)
cargs.append(container.tuple_object(field_args))
elif isinstance(arg, (_base.numeric_types, bool)):
dtype = "int32" if isinstance(arg, (_base.integer_types, bool)) else "float32"
dtype = _gettype(arg)
value = tvm.nd.array(np.array(arg, dtype=dtype), device=tvm.cpu(0))
cargs.append(value)
elif isinstance(arg, str):
Expand Down
20 changes: 20 additions & 0 deletions tests/python/relay/test_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,26 @@ def test_add_op_scalar(target, dev):
check_result(target, dev, [x_data, y_data], x_data + y_data, mod)


def test_add_op_scalar_float16(target, dev):
"""
test_add_op_scalar_float16:
fn (x, y) {
return x + y;
}
"""
mod = tvm.IRModule()
x = relay.var("x", shape=(), dtype="float16") # Default to float16
y = relay.var("y", shape=(), dtype="float16") # Default to float16
func = relay.Function([x, y], relay.op.add(x, y))
x_y_data = [
(np.array(10.0, dtype="float16"), np.array(1.0, dtype="float16")),
(np.float16(10.0), np.float16(1.0)),
]
for (x_data, y_data) in x_y_data:
mod["main"] = func
check_result(target, dev, [x_data, y_data], x_data + y_data, mod)


def test_add_op_scalar_int(target, dev):
"""
test_add_op_scalar_int:
Expand Down