-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[Contrib] Support fp16 input in cpu sort #8672
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,24 +16,23 @@ | |
| # under the License. | ||
| """ Support level6 operator test cases. | ||
| """ | ||
| import pytest | ||
| import numpy as np | ||
| import tvm | ||
| from tvm import te | ||
| from tvm import relay | ||
| import tvm.testing | ||
|
|
||
|
|
||
| @tvm.testing.uses_gpu | ||
| def test_sort(): | ||
| def verify_sort(shape, axis, is_ascend, is_dyn=False): | ||
|
|
||
| def verify_sort(shape, axis, is_ascend, is_dyn=False, in_dtype="float32"): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could also do a small refactoring for this file:
if __name__ == "__main__":
pytest.main([__file__])
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't that make the testing time double? This file is fairly slow to test (more than 3 min according to https://ci.tlcpack.ai/job/tvm/job/main/1384/testReport/ctypes.tests.python.relay/test_op_level6/) and I don't think fp16 tests need to run as often as fp32.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm that's a fair concern. |
||
| if is_dyn: | ||
| x = relay.var("x", relay.TensorType([relay.Any()] * len(shape), "float32")) | ||
| x = relay.var("x", relay.TensorType([relay.Any()] * len(shape), in_dtype)) | ||
| else: | ||
| x = relay.var("x", relay.TensorType(shape, "float32")) | ||
| x = relay.var("x", relay.TensorType(shape, in_dtype)) | ||
| z = relay.sort(x, axis=axis, is_ascend=is_ascend) | ||
| func = relay.Function([x], z) | ||
| x_data = np.random.uniform(size=shape).astype("float32") | ||
| x_data = np.random.uniform(size=shape).astype(in_dtype) | ||
| if is_ascend: | ||
| ref_res = np.sort(x_data, axis=axis) | ||
| else: | ||
|
|
@@ -56,18 +55,19 @@ def verify_sort(shape, axis, is_ascend, is_dyn=False): | |
| verify_sort((3, 5, 6), axis=-1, is_ascend=False, is_dyn=is_dyn) | ||
| verify_sort((3, 2000, 6), axis=1, is_ascend=False, is_dyn=is_dyn) | ||
| verify_sort((1, 122640), axis=1, is_ascend=False, is_dyn=is_dyn) | ||
| verify_sort((1, 122640), axis=1, is_ascend=False, is_dyn=is_dyn, in_dtype="float16") | ||
|
|
||
|
|
||
| @tvm.testing.uses_gpu | ||
| def test_argsort(): | ||
| def verify_argsort(shape, axis, is_ascend, dtype, is_dyn=False): | ||
| def verify_argsort(shape, axis, is_ascend, dtype, is_dyn=False, in_dtype="float32"): | ||
| if is_dyn: | ||
| x = relay.var("x", relay.TensorType([relay.Any()] * len(shape), "float32")) | ||
| x = relay.var("x", relay.TensorType([relay.Any()] * len(shape), in_dtype)) | ||
| else: | ||
| x = relay.var("x", relay.TensorType(shape, "float32")) | ||
| x = relay.var("x", relay.TensorType(shape, in_dtype)) | ||
| z = relay.argsort(x, axis=axis, is_ascend=is_ascend, dtype=dtype) | ||
| func = relay.Function([x], z) | ||
| x_data = np.random.uniform(size=shape).astype("float32") | ||
| x_data = np.random.uniform(size=shape).astype(in_dtype) | ||
| if is_ascend: | ||
| ref_res = np.argsort(x_data, axis=axis, kind="stable") | ||
| else: | ||
|
|
@@ -93,31 +93,34 @@ def verify_argsort(shape, axis, is_ascend, dtype, is_dyn=False): | |
| verify_argsort((3, 6000, 6), axis=1, is_ascend=False, dtype=dtype, is_dyn=is_dyn) | ||
| verify_argsort((1000, 1, 1), axis=0, is_ascend=False, dtype=dtype, is_dyn=is_dyn) | ||
| verify_argsort((1, 122640), axis=1, is_ascend=False, dtype=dtype, is_dyn=is_dyn) | ||
| verify_argsort( | ||
| (1, 122640), axis=1, is_ascend=False, dtype=dtype, is_dyn=is_dyn, in_dtype="float16" | ||
| ) | ||
|
|
||
|
|
||
| @tvm.testing.uses_gpu | ||
| def test_topk(): | ||
| def verify_topk(k, axis, ret_type, is_ascend, dtype): | ||
| def verify_topk(k, axis, ret_type, is_ascend, dtype, in_dtype="float32"): | ||
| shape = (20, 100) | ||
| x = relay.var("x", relay.TensorType(shape, "float32")) | ||
| x = relay.var("x", relay.TensorType(shape, in_dtype)) | ||
| out = relay.topk(x, k, axis, ret_type, is_ascend, dtype) | ||
| if isinstance(out, relay.expr.TupleWrapper): | ||
| out = out.astuple() | ||
| func = relay.Function([x], out) | ||
| np_data = np.random.uniform(size=shape).astype("float32") | ||
| np_data = np.random.uniform(size=shape).astype(in_dtype) | ||
| if is_ascend: | ||
| np_indices = np.argsort(np_data, axis=axis) | ||
| np_indices = np.argsort(np_data, axis=axis, kind="stable") | ||
| else: | ||
| np_indices = np.argsort(-np_data, axis=axis) | ||
| np_indices = np.argsort(-np_data, axis=axis, kind="stable") | ||
| kk = k if k >= 1 else shape[axis] | ||
| if axis == 0: | ||
| np_indices = np_indices[:kk, :] | ||
| np_values = np.zeros(np_indices.shape).astype("float32") | ||
| np_values = np.zeros(np_indices.shape).astype(in_dtype) | ||
| for i in range(shape[1]): | ||
| np_values[:, i] = np_data[np_indices[:, i], i] | ||
| else: | ||
| np_indices = np_indices[:, :kk] | ||
| np_values = np.zeros(np_indices.shape).astype("float32") | ||
| np_values = np.zeros(np_indices.shape).astype(in_dtype) | ||
| for i in range(shape[0]): | ||
| np_values[i, :] = np_data[i, np_indices[i, :]] | ||
| np_indices = np_indices.astype(dtype) | ||
|
|
@@ -140,9 +143,8 @@ def verify_topk(k, axis, ret_type, is_ascend, dtype): | |
| for ret_type in ["both", "values", "indices"]: | ||
| verify_topk(k, axis, ret_type, True, "int64") | ||
| verify_topk(k, axis, ret_type, False, "float32") | ||
| verify_topk(k, axis, ret_type, False, "int64", "float16") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| test_sort() | ||
| test_argsort() | ||
| test_topk() | ||
| pytest.main([__file__]) | ||
Uh oh!
There was an error while loading. Please reload this page.