Skip to content

Fix negative indexing and multi-dimensional slicing in Helion #438

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

Open
wants to merge 1 commit into
base: yf225/stack/53
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions helion/_compiler/indexing_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,36 @@
ShapeLike = Sequence[SymIntLike]


def _normalize_negative_index(
k: int,
dim_idx: int,
fake_value: torch.Tensor,
state: CodegenState,
) -> str:
"""Normalize negative indices to positive ones.

Args:
k: The negative index value
dim_idx: The dimension index
fake_value: The fake tensor to get dimension size from
state: The codegen state

Returns:
String representation of the normalized index
"""
assert k < 0, "This function should only be called for negative indices"

dim_size = fake_value.size(dim_idx)
# Handle both concrete and symbolic dimension sizes
if isinstance(dim_size, int):
normalized_k = k + dim_size
return repr(normalized_k)
# For symbolic dimensions, we need to generate the proper expression
# The state.codegen is a GenerateAST instance which has device_function
sympy_expr = dim_size._sympy_() + k
return f"({state.codegen.device_function.user_sympy_expr(sympy_expr)})"


class IndexingStrategy:
def codegen_load(
self,
Expand Down Expand Up @@ -553,7 +583,14 @@ def create(
index_values.append(f"tl.zeros([1], {dtype}){expand}")
output_idx += 1
elif isinstance(k, int):
index_values.append(repr(k))
# Normalize negative indices
if k < 0:
dim_idx = len(index_values)
index_values.append(
_normalize_negative_index(k, dim_idx, fake_value, state)
)
else:
index_values.append(repr(k))
elif isinstance(k, torch.SymInt):
symbol = k._sympy_()
origin = None
Expand Down Expand Up @@ -839,7 +876,14 @@ def create(
res.offsets.append("0")
res.block_shape.append(1)
elif isinstance(k, int):
res.offsets.append(repr(k))
# Normalize negative indices
if k < 0:
dim_idx = len(res.offsets)
res.offsets.append(
_normalize_negative_index(k, dim_idx, fake_value, state)
)
else:
res.offsets.append(repr(k))
Comment on lines +880 to +886
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the conditional into the helper to reduce duplicate code.

res.block_shape.append(1)
elif isinstance(k, torch.SymInt):
symbol = k._sympy_()
Expand Down
4 changes: 0 additions & 4 deletions test/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,6 @@ def kernel(
torch.testing.assert_close(src2_result, expected_src2)
torch.testing.assert_close(dst2_result, expected_dst2)

@skipIfNormalMode("InternalError: Negative indexes")
def test_negative_indexing(self):
"""Test both setter from scalar and getter for [-1]"""

Expand Down Expand Up @@ -784,9 +783,6 @@ def kernel(
torch.testing.assert_close(src_result, expected_src)
torch.testing.assert_close(dst_result, expected_dst)

@skipIfNormalMode(
"RankMismatch: Cannot assign a tensor of rank 2 to a buffer of rank 3"
)
def test_multi_dim_slice(self):
"""Test both setter from scalar and getter for [:, :, i]"""

Expand Down
Loading