Skip to content

Add support for 16-bit floats in HIP backend #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
27 changes: 15 additions & 12 deletions kernel_tuner/backends/hip.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"bool": ctypes.c_bool,
"int8": ctypes.c_int8,
"int16": ctypes.c_int16,
"float16": ctypes.c_int16,
"int32": ctypes.c_int32,
"int64": ctypes.c_int64,
"uint8": ctypes.c_uint8,
Expand Down Expand Up @@ -120,25 +119,29 @@ def ready_argument_list(self, arguments):

# Handle numpy arrays
if isinstance(arg, np.ndarray):
if dtype_str in dtype_map.keys():
# Allocate device memory
device_ptr = hip_check(hip.hipMalloc(arg.nbytes))
# Allocate device memory
device_ptr = hip_check(hip.hipMalloc(arg.nbytes))

# Copy data to device using hipMemcpy
hip_check(hip.hipMemcpy(device_ptr, arg, arg.nbytes, hip.hipMemcpyKind.hipMemcpyHostToDevice))
# Copy data to device using hipMemcpy
hip_check(hip.hipMemcpy(device_ptr, arg, arg.nbytes, hip.hipMemcpyKind.hipMemcpyHostToDevice))

prepared_args.append(device_ptr)
else:
raise TypeError(f"Unknown dtype {dtype_str} for ndarray")
prepared_args.append(device_ptr)

# Handle numpy scalar types
elif isinstance(arg, np.generic):
# Convert numpy scalar to corresponding ctypes
ctype_arg = dtype_map[dtype_str](arg)
prepared_args.append(ctype_arg)
if dtype_str in dtype_map:
ctype_arg = dtype_map[dtype_str](arg)
prepared_args.append(ctype_arg)
# 16-bit float is not supported, view it as uint16
elif dtype_str in ("float16", "bfloat16"):
ctype_arg = ctypes.c_uint16(arg.view(np.uint16))
prepared_args.append(ctype_arg)
else:
raise ValueError(f"Invalid argument type {dtype_str}: {arg}")

else:
raise ValueError(f"Invalid argument type {type(arg)}, {arg}")
raise ValueError(f"Invalid argument type {type(arg)}: {arg}")

return prepared_args

Expand Down