-
Notifications
You must be signed in to change notification settings - Fork 249
Add Op dot #430
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
Merged
Merged
Add Op dot #430
Changes from 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
198880b
dot
wlxjhyf e329cd1
dot kernel
wlxjhyf d0f012b
resolve conflicts of dot kernel
wlxjhyf e026813
Merge branch 'master' into master
wlxjhyf d452bc3
fix_format_error
7d9f603
fix with single kernel in small input
wlxjhyf 803677e
Merge branch 'master' into master
wlxjhyf e2d137c
fix:some code problems
wlxjhyf f536523
Merge branch 'master' into master
StrongSpoon a002037
delete redundant code
StrongSpoon 3458d1c
reformat
StrongSpoon 73527f4
reformat
StrongSpoon 9d8b43c
reformat
StrongSpoon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import logging | ||
| import math | ||
|
|
||
| import torch | ||
| import triton | ||
| import triton.language as tl | ||
|
|
||
| from ..runtime import torch_device_fn | ||
| from ..utils import libentry | ||
| from ..utils import triton_lang_extension as tle | ||
|
|
||
| @libentry() | ||
| @triton.jit | ||
| def dot_kernel(x_ptr, y_ptr, out_ptr, N, BLOCK_SIZE: tl.constexpr): | ||
| pid = tle.program_id(0) | ||
| block_start = pid * BLOCK_SIZE | ||
|
|
||
| offsets = block_start + tl.arange(0, BLOCK_SIZE) | ||
|
|
||
| mask = offsets < N | ||
| x = tl.load(x_ptr + offsets, mask=mask, other=0.0).to(tl.float32) | ||
| y = tl.load(y_ptr + offsets, mask=mask, other=0.0).to(tl.float32) | ||
|
|
||
| sum = tl.sum(x * y) | ||
| tl.store(out_ptr, sum) | ||
|
|
||
|
|
||
| @libentry() | ||
| @triton.jit | ||
| def dot_kernel_1(x_ptr, y_ptr, mid_ptr, N, BLOCK_SIZE: tl.constexpr): | ||
| pid = tle.program_id(0) | ||
| block_start = pid * BLOCK_SIZE | ||
|
|
||
| offsets = block_start + tl.arange(0, BLOCK_SIZE) | ||
|
|
||
| mask = offsets < N | ||
| x = tl.load(x_ptr + offsets, mask=mask, other=0.0).to(tl.float32) | ||
| y = tl.load(y_ptr + offsets, mask=mask, other=0.0).to(tl.float32) | ||
|
|
||
| partial_sum = tl.sum(x * y) | ||
| tl.store(mid_ptr + pid, partial_sum) | ||
|
|
||
|
|
||
| @libentry() | ||
| @triton.jit | ||
| def dot_kernel_2(mid_ptr, out_ptr, M, BLOCK_MID: tl.constexpr): | ||
| offset = tl.arange(0, BLOCK_MID) | ||
| mid = mid_ptr + offset | ||
| mask = offset < M | ||
| mid_val = tl.load(mid, mask=mask, other=0.0) | ||
| out_val = tl.sum(mid_val) | ||
| tl.store(out_ptr, out_val) | ||
|
|
||
|
|
||
| def dot(x, y): | ||
| logging.debug("Triton Dot Product") | ||
|
|
||
| assert x.shape == y.shape, "Input vectors must have the same shape" | ||
| assert x.dim() == 1, "Input must be 1D tensors" | ||
|
|
||
| N = x.shape[0] | ||
|
|
||
| # Only when N is less than TRITON_MAX_TENSOR_NUMEL can it be processed with a single kernel, and performance is better when N < 4096 | ||
| if N >= 4096: | ||
| block_size = triton.next_power_of_2(math.ceil(math.sqrt(N))) | ||
|
|
||
| mid_size = triton.cdiv(N, block_size) | ||
| block_mid = triton.next_power_of_2(mid_size) | ||
|
|
||
| grid_1 = (mid_size, 1, 1) | ||
| grid_2 = (1, 1, 1) | ||
|
|
||
| mid = torch.empty((mid_size,), dtype=torch.float32, device=x.device) | ||
| out = torch.empty([], dtype=x.dtype, device=x.device) | ||
|
|
||
| with torch_device_fn.device(x.device): | ||
| dot_kernel_1[grid_1](x, y, mid, N, block_size) | ||
| dot_kernel_2[grid_2](mid, out, mid_size, block_mid) | ||
|
|
||
| else: | ||
| block_size = triton.next_power_of_2(math.ceil(N)) | ||
|
|
||
| grid = (1, 1, 1) | ||
|
|
||
| out = torch.empty([], dtype=torch.float32, device=x.device) | ||
|
|
||
| with torch_device_fn.device(x.device): | ||
| dot_kernel[grid](x, y, out, N, block_size) | ||
| out = out.to(x.dtype) | ||
|
|
||
| return out | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| REDUCTION_SHAPES, | ||
| REDUCTION_SMALL_SHAPES, | ||
| SHAPE_STRIDES, | ||
| UT_SHAPES_1D, | ||
| SkipVersion, | ||
| gems_assert_close, | ||
| gems_assert_equal, | ||
|
|
@@ -1050,3 +1051,19 @@ def test_accuracy_mse_loss(shape, dtype, reduction): | |
| with flag_gems.use_gems(): | ||
| res_out = torch.nn.functional.mse_loss(inp, target, reduction=reduction) | ||
| gems_assert_close(res_out, ref_out, dtype, equal_nan=True, reduce_dim=shape[dim]) | ||
|
|
||
|
|
||
| @pytest.mark.dot | ||
| @pytest.mark.parametrize("shape", UT_SHAPES_1D) | ||
| @pytest.mark.parametrize("dtype", FLOAT_DTYPES) | ||
| def test_accuracy_dot_tensor_tensor(shape, dtype): | ||
| inp1 = torch.randn(shape, dtype=dtype, device=flag_gems.device) | ||
| inp2 = torch.randn(shape, dtype=dtype, device=flag_gems.device) | ||
| ref_inp1 = to_reference(inp1, False) | ||
| ref_inp2 = to_reference(inp2, False) | ||
|
||
|
|
||
| ref_out = torch.dot(ref_inp1, ref_inp2) | ||
| with flag_gems.use_gems(): | ||
| res_out = torch.dot(inp1, inp2) | ||
|
|
||
| gems_assert_close(res_out, ref_out, dtype, equal_nan=True) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
math.ceil is useless here.