Repository navigation
Adds CUDA FP16 arithmetic operators #7885
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 7 commits
1cd16ff
3efd4fe
6c69b2c
5188b28
5b721ad
87b8c2c
6809886
7b36527
9269ff7
227ca5b
fac30e2
db03e4d
5898b6b
527973c
f10a4e5
882ea3d
0b244ea
a7129d4
e1783fa
7a7ad7d
b2bef05
021503f
f26d289
8932a70
a19874d
fbc183d
cfeb49c
aa4835c
dd8498e
277cf08
224bd48
95670b0
ce23094
8a96ff7
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 | ||||
|---|---|---|---|---|---|---|
| @@ -1,9 +1,29 @@ | ||||||
| import numpy as np | ||||||
| from numba.cuda.testing import unittest, CUDATestCase | ||||||
| from numba.cuda.testing import unittest, CUDATestCase, skip_unless_cc_53 | ||||||
| from numba import cuda | ||||||
| import operator | ||||||
|
|
||||||
|
|
||||||
| def simple_fp16add(ary, a, b): | ||||||
| ary[0] = a + b | ||||||
|
|
||||||
|
|
||||||
| def simple_fp16sub(ary, a, b): | ||||||
| ary[0] = a - b | ||||||
|
|
||||||
|
|
||||||
| def simple_fp16mul(ary, a, b): | ||||||
| ary[0] = a * b | ||||||
|
|
||||||
|
|
||||||
| def simple_fp16neg(ary, a): | ||||||
| ary[0] = -a | ||||||
|
|
||||||
|
|
||||||
| def simple_fp16abs(ary, a): | ||||||
| ary[0] = abs(a) | ||||||
|
|
||||||
|
|
||||||
| class TestOperatorModule(CUDATestCase): | ||||||
| """ | ||||||
| Test if operator module is supported by the CUDA target. | ||||||
|
|
@@ -36,6 +56,41 @@ def test_truediv(self): | |||||
| def test_floordiv(self): | ||||||
| self.operator_template(operator.floordiv) | ||||||
|
|
||||||
| @skip_unless_cc_53 | ||||||
| def test_fp16_binary(self): | ||||||
| functions = (simple_fp16add, simple_fp16sub, simple_fp16mul) | ||||||
| ops = (operator.add, operator.sub, operator.mul) | ||||||
|
|
||||||
| for fn, op in zip(functions, ops): | ||||||
| with self.subTest(op=op): | ||||||
| kernel = cuda.jit("void(f2[:], f2, f2)")(fn) | ||||||
|
|
||||||
| expected = np.zeros(1, dtype=np.float16) | ||||||
|
Member
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. This assignment of
Suggested change
|
||||||
| got = np.zeros(1, dtype=np.float16) | ||||||
| arg1 = np.random.random(1).astype(np.float16) | ||||||
| arg2 = np.random.random(1).astype(np.float16) | ||||||
|
|
||||||
| kernel[1, 1](got, arg1[0], arg2[0]) | ||||||
| expected = op(arg1, arg2) | ||||||
| np.testing.assert_allclose(got[0], expected) | ||||||
|
Member
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. This compares the scalar first element of
Suggested change
|
||||||
|
|
||||||
| @skip_unless_cc_53 | ||||||
| def test_fp16_unary(self): | ||||||
| functions = (simple_fp16neg, simple_fp16abs) | ||||||
| ops = (operator.neg, operator.abs) | ||||||
|
|
||||||
| for fn, op in zip(functions, ops): | ||||||
| with self.subTest(op=op): | ||||||
| kernel = cuda.jit("void(f2[:], f2)")(fn) | ||||||
|
|
||||||
| expected = np.zeros(1, dtype=np.float16) | ||||||
|
Member
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. Superfluous assignment:
Suggested change
|
||||||
| got = np.zeros(1, dtype=np.float16) | ||||||
| arg1 = np.random.random(1).astype(np.float16) | ||||||
|
|
||||||
| kernel[1, 1](got, arg1[0]) | ||||||
| expected = op(arg1) | ||||||
| np.testing.assert_allclose(got[0], expected) | ||||||
|
Member
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 should compare arrays instead of a scalar and an array:
Suggested change
|
||||||
|
|
||||||
|
|
||||||
| if __name__ == '__main__': | ||||||
| unittest.main() | ||||||
Uh oh!
There was an error while loading. Please reload this page.