Skip to content

fix: clamp start/end in slice_backward to prevent out-of-bounds kernel write#2187

Open
physics31415926 wants to merge 4 commits intoflagos-ai:masterfrom
physics31415926:fix/slice-backward-oob-write
Open

fix: clamp start/end in slice_backward to prevent out-of-bounds kernel write#2187
physics31415926 wants to merge 4 commits intoflagos-ai:masterfrom
physics31415926:fix/slice-backward-oob-write

Conversation

@physics31415926
Copy link
Copy Markdown
Contributor

Problem

When end > dim_size, slice_len was computed without clamping, causing
dim_index = start + slice_idx * step to exceed dim_size. The kernel then
wrote to an out-of-bounds address, triggering cudaErrorIllegalAddress.

PyTorch allows end to exceed the dimension size (it clamps silently), but
the FlagGems kernel did not handle this case.

Fix

Clamp start and end to [0, dim_size] before computing slice_len,
matching PyTorch's behavior.

Test

Added test_slice_backward_oob_end to reproduce the out-of-bounds case.

@physics31415926 physics31415926 marked this pull request as draft March 30, 2026 09:44
@physics31415926 physics31415926 marked this pull request as ready for review March 30, 2026 09:44
@physics31415926 physics31415926 force-pushed the fix/slice-backward-oob-write branch from 28228db to 2ccbd7f Compare March 30, 2026 09:50
@0x45f 0x45f self-assigned this Mar 31, 2026
Copy link
Copy Markdown
Contributor

@tengqm tengqm left a comment

Choose a reason for hiding this comment

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

picky nits.
otherwise lgtm.

delete repeative assignment

Co-authored-by: Qiming Teng <[email protected]>
Signed-off-by: Physics <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants