-
Notifications
You must be signed in to change notification settings - Fork 145
Add bitwise_shift_left and bitwise_shift_right #990
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import logging | ||
|
||
import triton | ||
|
||
from flag_gems.utils import pointwise_dynamic | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@pointwise_dynamic(promotion_methods=[(0, 1, "DEFAULT")]) | ||
@triton.jit | ||
def bitwise_left_shift_kernel(a, b): | ||
return a << b | ||
|
||
|
||
def bitwise_left_shift(self, other, *, out=None): | ||
logger.debug("GEMS BITWISE_LEFT_SHIFT") | ||
return bitwise_left_shift_kernel(self, other, out=out) |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,17 @@ | ||||||||||||
import logging | ||||||||||||
|
||||||||||||
import triton | ||||||||||||
|
||||||||||||
from flag_gems.utils import pointwise_dynamic | ||||||||||||
|
||||||||||||
logger = logging.getLogger(__name__) | ||||||||||||
|
||||||||||||
|
||||||||||||
@pointwise_dynamic(promotion_methods=[(0, 1, "DEFAULT")]) | ||||||||||||
@triton.jit | ||||||||||||
def bitwise_right_shift_kernel(a, b): | ||||||||||||
return a >> b | ||||||||||||
|
||||||||||||
|
||||||||||||
def bitwise_right_shift(self, other, *, out=None): | ||||||||||||
return bitwise_right_shift_kernel(self, other, out=out) | ||||||||||||
Comment on lines
+16
to
+17
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. For consistency with
Suggested change
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -85,6 +85,89 @@ def test_accuracy_angle(shape, dtype): | |||||
gems_assert_close(res_out, ref_out, dtype_out) | ||||||
|
||||||
|
||||||
BITWISE_SHAPES = [ | ||||||
((512, 1024), (512, 1024)), | ||||||
((256, 512), (1, 512)), | ||||||
((256, 512), (256, 1)), | ||||||
((1, 512), (256, 512)), | ||||||
((256, 1), (256, 512)), | ||||||
((1024,), ()), | ||||||
((), (1024,)), | ||||||
] | ||||||
|
||||||
|
||||||
@pytest.mark.bitwise_left_shift | ||||||
@pytest.mark.parametrize("shapes", BITWISE_SHAPES) | ||||||
@pytest.mark.parametrize("dtype", ALL_INT_DTYPES + [torch.uint8]) | ||||||
def test_accuracy_bitwise_left_shift(shapes, dtype): | ||||||
shape_a, shape_b = shapes | ||||||
res_a = torch.randint(0, 100, shape_a, dtype=dtype, device=flag_gems.device) | ||||||
res_b = torch.randint(0, 8, shape_b, dtype=dtype, device=flag_gems.device) | ||||||
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. The range of shift amounts
Suggested change
|
||||||
ref_a = to_reference(res_a) | ||||||
ref_b = to_reference(res_b) | ||||||
|
||||||
ref_out = torch.bitwise_left_shift(ref_a, ref_b) | ||||||
with flag_gems.use_gems(): | ||||||
res_out = torch.bitwise_left_shift(res_a, res_b) | ||||||
gems_assert_close(res_out, ref_out, dtype) | ||||||
|
||||||
|
||||||
@pytest.mark.bitwise_right_shift | ||||||
@pytest.mark.parametrize("shapes", BITWISE_SHAPES) | ||||||
@pytest.mark.parametrize("dtype", ALL_INT_DTYPES + [torch.uint8]) | ||||||
def test_accuracy_bitwise_right_shift(shapes, dtype): | ||||||
shape_a, shape_b = shapes | ||||||
res_a = torch.randint(0, 100, shape_a, dtype=dtype, device=flag_gems.device) | ||||||
res_b = torch.randint(0, 8, shape_b, dtype=dtype, device=flag_gems.device) | ||||||
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. The range of shift amounts
Suggested change
|
||||||
ref_a = to_reference(res_a) | ||||||
ref_b = to_reference(res_b) | ||||||
|
||||||
ref_out = torch.bitwise_right_shift(ref_a, ref_b) | ||||||
with flag_gems.use_gems(): | ||||||
res_out = torch.bitwise_right_shift(res_a, res_b) | ||||||
gems_assert_close(res_out, ref_out, dtype) | ||||||
|
||||||
|
||||||
INPLACE_BITWISE_SHAPES = [ | ||||||
((512, 1024), (512, 1024)), | ||||||
((256, 512), (1, 512)), | ||||||
((256, 512), (256, 1)), | ||||||
((1024,), ()), | ||||||
] | ||||||
|
||||||
|
||||||
@pytest.mark.bitwise_left_shift | ||||||
@pytest.mark.parametrize("shapes", INPLACE_BITWISE_SHAPES) | ||||||
@pytest.mark.parametrize("dtype", ALL_INT_DTYPES + [torch.uint8]) | ||||||
def test_accuracy_bitwise_left_shift_(shapes, dtype): | ||||||
shape_a, shape_b = shapes | ||||||
res_a = torch.randint(0, 100, shape_a, dtype=dtype, device=flag_gems.device) | ||||||
res_b = torch.randint(0, 8, shape_b, dtype=dtype, device=flag_gems.device) | ||||||
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. The range of shift amounts
Suggested change
|
||||||
ref_a = to_reference(res_a.clone()) | ||||||
ref_b = to_reference(res_b) | ||||||
|
||||||
ref_a.bitwise_left_shift_(ref_b) | ||||||
with flag_gems.use_gems(): | ||||||
res_a.bitwise_left_shift_(res_b) | ||||||
gems_assert_close(res_a, ref_a, dtype) | ||||||
|
||||||
|
||||||
@pytest.mark.bitwise_right_shift | ||||||
@pytest.mark.parametrize("shapes", INPLACE_BITWISE_SHAPES) | ||||||
@pytest.mark.parametrize("dtype", ALL_INT_DTYPES + [torch.uint8]) | ||||||
def test_accuracy_bitwise_right_shift_(shapes, dtype): | ||||||
shape_a, shape_b = shapes | ||||||
res_a = torch.randint(0, 100, shape_a, dtype=dtype, device=flag_gems.device) | ||||||
res_b = torch.randint(0, 8, shape_b, dtype=dtype, device=flag_gems.device) | ||||||
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. The range of shift amounts
Suggested change
|
||||||
ref_a = to_reference(res_a.clone()) | ||||||
ref_b = to_reference(res_b) | ||||||
|
||||||
ref_a.bitwise_right_shift_(ref_b) | ||||||
with flag_gems.use_gems(): | ||||||
res_a.bitwise_right_shift_(res_b) | ||||||
gems_assert_close(res_a, ref_a, dtype) | ||||||
|
||||||
|
||||||
@pytest.mark.bitwise_not | ||||||
@pytest.mark.parametrize("shape", POINTWISE_SHAPES) | ||||||
@pytest.mark.parametrize("dtype", INT_DTYPES + BOOL_TYPES) | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The range of shift amounts
[0, 8)
is a bit narrow for integer types larger than 8 bits. To ensure the performance benchmark covers a more realistic range of inputs, consider using a range for the shift amount that is dependent on thecur_dtype
, up to the number of bits in the type.