-
Notifications
You must be signed in to change notification settings - Fork 496
Update masked_select and nonzero #719
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
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8e95d4d
Update masked_select and nonzero
tongxin 881b94e
add libentry to masked_select
tongxin 965d119
update masked_select.
tongxin 471ebf5
update masked_select
tongxin 5d7f9fc
Merge branch 'master' into masked_select
tongxin aa9b4f3
Update masked_select.py
tongxin 68bda19
Update masked_select.py
tongxin f866b84
Update masked_select.py, adding back device_guard
tongxin 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,12 +8,13 @@ | |
| from ..runtime import torch_device_fn | ||
| from ..utils import broadcastable, libentry | ||
| from ..utils import triton_lang_extension as tle | ||
| from ..utils.shape_utils import bracket_next_power_of_2 | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @libentry() | ||
| @triton.autotune(configs=runtime.get_tuned_config("masked_select"), key=["n_elements"]) | ||
| @triton.heuristics(runtime.get_heuristic_config("elementwise_generic")) | ||
| @triton.jit | ||
| def masked_select_kernel( | ||
| inp_ptr, | ||
|
|
@@ -34,6 +35,124 @@ def masked_select_kernel( | |
| tl.store(out_ptr + out_offset, inp, mask=(select_mask and mask)) | ||
|
|
||
|
|
||
| @triton.jit | ||
| def masked_select_single_pass_kernel( | ||
| inp_ptr, mask_ptr, out_ptr, N, BLOCK_SIZE: tl.constexpr | ||
| ): | ||
| pid = tl.program_id(0) | ||
| offsets = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE) | ||
| inp = tl.load(inp_ptr + offsets, mask=offsets < N) | ||
| mask = tl.load(mask_ptr + offsets, mask=offsets < N).to(tl.int1) | ||
| mask_ints = mask.to(tl.int32) | ||
| out_offsets = tl.cumsum(mask_ints, axis=0) - 1 | ||
|
|
||
| tl.store(out_ptr + out_offsets, inp, mask=offsets < N and mask) | ||
|
|
||
|
|
||
| def masked_select_single_pass(inp, mask, out, N): | ||
| BLOCK_SIZE = triton.next_power_of_2(N) | ||
| if BLOCK_SIZE <= 512: | ||
| num_warps = 4 | ||
| elif BLOCK_SIZE <= 2048: | ||
| num_warps = 8 | ||
| else: | ||
| num_warps = 16 | ||
| masked_select_single_pass_kernel[(1,)]( | ||
| inp, mask, out, N, BLOCK_SIZE=BLOCK_SIZE, num_warps=num_warps | ||
| ) | ||
| return out | ||
|
|
||
|
|
||
| @triton.jit(do_not_specialize=["N", "nr", "row_stride"]) | ||
| def mask_part_sum_kernel( | ||
| inp_ptr, | ||
| mask_ptr, | ||
| part_sums_ptr, | ||
| counter_ptr, | ||
| N, | ||
| num_blocks, | ||
| num_blocks_per_row, | ||
| NP_BLOCK: tl.constexpr, | ||
| BLOCK_SIZE: tl.constexpr, | ||
| ): | ||
| row_id = tl.program_id(0) | ||
| start_block = row_id * num_blocks_per_row | ||
| offset = start_block * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE) | ||
| acc = tl.zeros((BLOCK_SIZE,), dtype=tl.constexpr(part_sums_ptr.dtype.element_ty)) | ||
| if row_id < tl.num_programs(0) - 1: | ||
| for block_id in range(start_block, start_block + num_blocks_per_row): | ||
| select = tl.load(mask_ptr + offset) | ||
| select_ints = select.to(tl.constexpr(part_sums_ptr.dtype.element_ty)) | ||
| acc += select_ints | ||
| offset += BLOCK_SIZE | ||
| else: | ||
| for block_id in range( | ||
| start_block, min(num_blocks, start_block + num_blocks_per_row) | ||
| ): | ||
| select = tl.load(mask_ptr + offset, mask=offset < N, other=0) | ||
| select_ints = select.to(tl.constexpr(part_sums_ptr.dtype.element_ty)) | ||
| acc += select_ints | ||
| offset += BLOCK_SIZE | ||
| part_sum = tl.sum(acc, axis=0) | ||
| tl.store(part_sums_ptr + row_id, part_sum) | ||
| # cumsum the part_sums | ||
| count = tl.atomic_add(counter_ptr, 1, sem="acq_rel") | ||
| np = tl.num_programs(0) | ||
| if count == np - 1: | ||
| mask = tl.arange(0, NP_BLOCK) < np | ||
| part_sums = tl.load(part_sums_ptr + tl.arange(0, NP_BLOCK), mask=mask) | ||
| final_sum = tl.sum(part_sums, axis=0) | ||
| pre_sums = tl.cumsum(part_sums, axis=0) | ||
| tl.store( | ||
| part_sums_ptr + tl.arange(0, NP_BLOCK), pre_sums - part_sums, mask=mask | ||
| ) | ||
| tl.store(part_sums_ptr + np, final_sum) | ||
|
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. So this kernel computes the CTA-level exclusive-prefix-scan. |
||
|
|
||
|
|
||
| @triton.jit(do_not_specialize=["N", "nr", "row_stride"]) | ||
| def write_back_kernel( | ||
| inp_ptr, | ||
| mask_ptr, | ||
| part_sums_ptr, | ||
| out_ptr, | ||
| N, | ||
| num_blocks, | ||
| num_blocks_per_row, | ||
| NP_BLOCK: tl.constexpr, | ||
| BLOCK_SIZE: tl.constexpr, | ||
| ): | ||
| row_id = tl.program_id(0) | ||
|
|
||
| start_block = row_id * num_blocks_per_row | ||
| offset = start_block * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE) | ||
| advance = tl.load(part_sums_ptr + row_id) | ||
|
|
||
| if row_id < tl.num_programs(0) - 1: | ||
| for block_id in range(start_block, start_block + num_blocks_per_row): | ||
| inp = tl.load(inp_ptr + offset) | ||
| select_mask = tl.load(mask_ptr + offset).to(tl.int1) | ||
| select_ints = select_mask.to(tl.constexpr(part_sums_ptr.dtype.element_ty)) | ||
| out_ptr += advance | ||
| advance = tl.sum(select_ints, axis=0) | ||
| pre_sums = tl.cumsum(select_ints, axis=0) - 1 | ||
| tl.store(out_ptr + pre_sums, inp, mask=select_mask) | ||
| offset += BLOCK_SIZE | ||
| else: | ||
| for block_id in range( | ||
| start_block, min(num_blocks, start_block + num_blocks_per_row) | ||
| ): | ||
| inp = tl.load(inp_ptr + offset, mask=offset < N) | ||
| select_mask = tl.load(mask_ptr + offset, mask=offset < N, other=0).to( | ||
| tl.int1 | ||
| ) | ||
| select_ints = select_mask.to(tl.constexpr(part_sums_ptr.dtype.element_ty)) | ||
| out_ptr += advance | ||
| advance = tl.sum(select_ints, axis=0) | ||
| pre_sums = tl.cumsum(select_ints, axis=0) - 1 | ||
| tl.store(out_ptr + pre_sums, inp, mask=offset < N and select_mask) | ||
| offset += BLOCK_SIZE | ||
|
|
||
|
|
||
| def masked_select(inp, mask): | ||
| logger.debug("GEMS MASKED SELECT") | ||
|
|
||
|
|
@@ -46,15 +165,59 @@ def masked_select(inp, mask): | |
| inp, mask = torch.broadcast_tensors(inp, mask) | ||
|
|
||
| inp = inp.contiguous() | ||
| mask = mask.contiguous() | ||
| mask = mask.ravel() | ||
|
iclementine marked this conversation as resolved.
Outdated
|
||
|
|
||
| N = inp.numel() | ||
| if N <= 4096: | ||
| out = torch.empty(mask.sum(), dtype=inp.dtype, device=inp.device) | ||
| return masked_select_single_pass(inp, mask, out, N) | ||
|
|
||
| # return mask_select(inp, mask) | ||
|
|
||
| BLOCK_SIZE = bracket_next_power_of_2(N, 128, 4096) | ||
| num_warps = min(16, BLOCK_SIZE // 32) | ||
|
|
||
| # max degree of parallelism | ||
| np = torch_device_fn.get_device_properties(mask.device).multi_processor_count | ||
|
|
||
| mask_flattened = mask.ravel() | ||
| # arranged as np rows of blocks | ||
| n_blocks = triton.cdiv(N, BLOCK_SIZE) | ||
| np = min(n_blocks, np) | ||
| n_blocks_per_row = triton.cdiv(n_blocks, np) | ||
| np = triton.cdiv(n_blocks, n_blocks_per_row) | ||
| NP_BLOCK = triton.next_power_of_2(np) | ||
|
|
||
| prefix_sum = mask_flattened.cumsum(axis=0) | ||
| out = torch.empty(prefix_sum[-1].item(), dtype=inp.dtype, device=inp.device) | ||
| # Tally partial sums over cols | ||
| dtype = torch.int32 if N < 2**31 else torch.int64 | ||
| part_sums = torch.empty(np + 1, dtype=dtype, device=mask.device) | ||
| barrier = torch.zeros([], dtype=torch.int, device=mask.device) | ||
| mask_part_sum_kernel[(np,)]( | ||
| inp, | ||
| mask, | ||
| part_sums, | ||
| barrier, | ||
| N, | ||
| n_blocks, | ||
| n_blocks_per_row, | ||
| NP_BLOCK=NP_BLOCK, | ||
| BLOCK_SIZE=BLOCK_SIZE, | ||
| num_warps=num_warps, | ||
| ) | ||
|
|
||
| n_elements = inp.numel() | ||
| grid = lambda meta: (triton.cdiv(n_elements, meta["BLOCK_SIZE"]),) | ||
| with torch_device_fn.device(inp.device): | ||
|
iclementine marked this conversation as resolved.
|
||
| masked_select_kernel[grid](inp, mask_flattened, prefix_sum, out, n_elements) | ||
| # Cumsum to obtain nonzero output starting offsets for each col of blocks | ||
| # pre_sums = part_sums.cumsum(axis=0) | ||
| out = torch.empty(part_sums[-1], dtype=inp.dtype, device=mask.device) | ||
| # write_offsets = pre_sums - part_sums | ||
| write_back_kernel[(np,)]( | ||
| inp, | ||
| mask, | ||
| part_sums, | ||
| out, | ||
| N, | ||
| n_blocks, | ||
| n_blocks_per_row, | ||
| NP_BLOCK=triton.next_power_of_2(np), | ||
| BLOCK_SIZE=BLOCK_SIZE, | ||
| num_warps=num_warps, | ||
| ) | ||
|
iclementine marked this conversation as resolved.
Outdated
|
||
| 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
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
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.
Uh oh!
There was an error while loading. Please reload this page.