-
Notifications
You must be signed in to change notification settings - Fork 20
Add support for ellipsis (...) indexing in Helion #437
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
Closed
Closed
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -227,6 +227,9 @@ def valid_block_size( | |
for i, k in enumerate(subscript): | ||
if k is None: | ||
continue | ||
if k is Ellipsis: | ||
# Ellipsis is not supported in tensor descriptor mode | ||
return False | ||
size, stride = size_stride.popleft() | ||
if isinstance(k, slice): | ||
# Slices with steps are not supported in tensor descriptor mode | ||
|
@@ -447,6 +450,14 @@ def codegen_store( | |
) | ||
|
||
|
||
def _calculate_ellipsis_dims( | ||
index: list[object], current_index: int, total_dims: int | ||
) -> int: | ||
"""Calculate how many dimensions an ellipsis should expand to.""" | ||
remaining_indices = len(index) - current_index - 1 | ||
return total_dims - current_index - remaining_indices | ||
Comment on lines
+453
to
+458
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. I don't think this is right when combined with |
||
|
||
|
||
class SubscriptIndexing(NamedTuple): | ||
index_expr: ast.AST | ||
mask_expr: ast.AST | ||
|
@@ -465,9 +476,18 @@ def compute_shape( | |
input_size = collections.deque(tensor.size()) | ||
output_size = [] | ||
env = CompileEnvironment.current() | ||
for k in index: | ||
for i, k in enumerate(index): | ||
if k is None: | ||
output_size.append(1) | ||
elif k is Ellipsis: | ||
ellipsis_dims = _calculate_ellipsis_dims(index, i, len(tensor.size())) | ||
for _ in range(ellipsis_dims): | ||
size = input_size.popleft() | ||
if size != 1: | ||
rdim = env.allocate_reduction_dimension(size) | ||
output_size.append(rdim.var) | ||
else: | ||
output_size.append(1) | ||
elif isinstance(k, int): | ||
input_size.popleft() | ||
elif isinstance(k, torch.SymInt): | ||
|
@@ -517,6 +537,21 @@ def create( | |
for n, k in enumerate(index): | ||
if k is None: | ||
output_idx += 1 | ||
elif k is Ellipsis: | ||
ellipsis_dims = _calculate_ellipsis_dims(index, n, fake_value.ndim) | ||
for _ in range(ellipsis_dims): | ||
expand = tile_strategy.expand_str(output_size, output_idx) | ||
size = fake_value.size(len(index_values)) | ||
if size != 1: | ||
rdim = env.allocate_reduction_dimension(size) | ||
block_idx = rdim.block_id | ||
index_var = state.codegen.index_var(block_idx) | ||
index_values.append(f"({index_var}){expand}") | ||
if mask := state.codegen.mask_var(block_idx): | ||
mask_values.setdefault(f"({mask}){expand}") | ||
else: | ||
index_values.append(f"tl.zeros([1], {dtype}){expand}") | ||
output_idx += 1 | ||
elif isinstance(k, int): | ||
index_values.append(repr(k)) | ||
elif isinstance(k, torch.SymInt): | ||
|
@@ -729,8 +764,16 @@ def is_supported( | |
# TODO(jansel): support block_ptr with extra_mask | ||
return False | ||
input_sizes = collections.deque(fake_tensor.size()) | ||
for k in index: | ||
input_size = 1 if k is None else input_sizes.popleft() | ||
for n, k in enumerate(index): | ||
if k is None: | ||
input_size = 1 | ||
elif k is Ellipsis: | ||
ellipsis_dims = _calculate_ellipsis_dims(index, n, fake_tensor.ndim) | ||
for _ in range(ellipsis_dims): | ||
input_sizes.popleft() | ||
continue | ||
else: | ||
input_size = input_sizes.popleft() | ||
if isinstance(k, torch.SymInt): | ||
symbol = k._sympy_() | ||
origin = None | ||
|
@@ -780,9 +823,21 @@ def create( | |
fake_value, | ||
reshaped_size=SubscriptIndexing.compute_shape(fake_value, index), | ||
) | ||
for k in index: | ||
for n, k in enumerate(index): | ||
if k is None: | ||
pass # handled by reshaped_size | ||
elif k is Ellipsis: | ||
ellipsis_dims = _calculate_ellipsis_dims(index, n, fake_value.ndim) | ||
env = CompileEnvironment.current() | ||
for _ in range(ellipsis_dims): | ||
size = fake_value.size(len(res.offsets)) | ||
if size != 1: | ||
rdim = env.allocate_reduction_dimension(size) | ||
res.offsets.append(state.codegen.offset_var(rdim.block_id)) | ||
res.block_shape.append(rdim.var) | ||
else: | ||
res.offsets.append("0") | ||
res.block_shape.append(1) | ||
elif isinstance(k, int): | ||
res.offsets.append(repr(k)) | ||
res.block_shape.append(1) | ||
|
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
Oops, something went wrong.
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.
Is this hard to support? Maybe we we pre-expand the indexing string the existing code could work unmodified.