Skip to content

Update KJT stride calculation logic to be based off of inverse_indices for VBE KJTs. #3119

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: main
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
6 changes: 6 additions & 0 deletions torchrec/sparse/jagged_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,13 +1095,18 @@ def _maybe_compute_stride_kjt(
lengths: Optional[torch.Tensor],
offsets: Optional[torch.Tensor],
stride_per_key_per_rank: Optional[torch.IntTensor],
inverse_indices: Optional[Tuple[List[str], torch.Tensor]] = None,
) -> int:
if stride is None:
if len(keys) == 0:
stride = 0
elif (
stride_per_key_per_rank is not None and stride_per_key_per_rank.numel() > 0
):
# For VBE KJT, batch size should be based on inverse_indices when set.
if inverse_indices is not None and inverse_indices[1].numel() > 0:
return inverse_indices[1].shape[-1]

stride = int(stride_per_key_per_rank.sum(dim=1).max().item())
elif offsets is not None and offsets.numel() > 0:
stride = (offsets.numel() - 1) // len(keys)
Expand Down Expand Up @@ -2138,6 +2143,7 @@ def stride(self) -> int:
self._lengths,
self._offsets,
self._stride_per_key_per_rank,
self._inverse_indices,
)
self._stride = stride
return stride
Expand Down
12 changes: 12 additions & 0 deletions torchrec/sparse/tests/test_keyed_jagged_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,18 @@ def test_meta_device_compatibility(self) -> None:
lengths=torch.tensor([], device=torch.device("meta")),
)

def test_vbe_kjt_stride(self) -> None:
inverse_indices = torch.tensor([[0, 1, 0], [0, 0, 0]])
kjt = KeyedJaggedTensor(
keys=["f1", "f2", "f3"],
values=torch.tensor([5, 6, 7, 1, 2, 3, 0, 1]),
lengths=torch.tensor([3, 3, 2]),
stride_per_key_per_rank=[[2], [1]],
inverse_indices=(["f1", "f2"], inverse_indices),
)

self.assertEqual(kjt.stride(), inverse_indices.shape[-1])


class TestKeyedJaggedTensorScripting(unittest.TestCase):
def test_scriptable_forward(self) -> None:
Expand Down
Loading