Skip to content

Fix empty indices for gather op#4477

Open
BruceXinXin wants to merge 3 commits intollvm:mainfrom
BruceXinXin:bruce_fix_gather_zero_bug
Open

Fix empty indices for gather op#4477
BruceXinXin wants to merge 3 commits intollvm:mainfrom
BruceXinXin:bruce_fix_gather_zero_bug

Conversation

@BruceXinXin
Copy link

@BruceXinXin BruceXinXin commented Feb 28, 2026

Fix empty indices for gather op (torch.embedding zero-dimension crash)

Problem

torch.embedding(weight, indices) should support indices tensors of any shape, including those with a zero-sized dimension. Given:

  • weight.shape = [vocab_size, embedding_dim]
  • indices.shape = [d1, d2, ..., dn]

The expected output shape is [d1, d2, ..., dn, embedding_dim], and this should hold even when any di = 0 (producing an empty output tensor).

Previously, the lowering of torch.embedding to the gather op would fail or produce incorrect results when any dimension of indices was zero. This is a valid and common case in practice — for example, when batching sequences of variable length that are filtered down to an empty set.

Fix

Handle the empty-indices edge case in the gather op lowering so that when indices contains a zero-sized dimension, the op correctly produces an output tensor of the expected shape with zero elements, rather than crashing or emitting incorrect IR.

Testing

Added a test case covering torch.embedding with zero-sized indices to verify correct shape inference and lowering behavior.

@BruceXinXin BruceXinXin marked this pull request as ready for review March 3, 2026 01:35
@BruceXinXin
Copy link
Author

@sjarus @Lallapallooza

@sjarus sjarus requested a review from tanyokwok March 10, 2026 15:44
@sjarus
Copy link
Collaborator

sjarus commented Mar 10, 2026

I've added @tanyokwok - the author of the code - as reviewer. I'm not personally familiar with the StableHLO pathway code.

@BruceXinXin
Copy link
Author

Seems @tanyokwok is no longer active:) Do you know any one I can ask for review? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants