Skip to content

Commit 5574def

Browse files
jd7-trfacebook-github-bot
authored andcommitted
Update KJT stride calculation logic to be based off of inverse_indices for VBE KJTs. (pytorch#3119)
Summary: Pull Request resolved: pytorch#3119 For VBE KJTs, ppdate the `_maybe_compute_stride_kjt` logic to calculate stride based off of `inverse_indices` when its set. Currently, stride of VBE KJT with `stride_per_key_per_rank` is calculated as the max "stride per key". This is different from the batch size of the EBC output KeyedTensor which is based off of inverse_indices. This causes issues in IR module serialization. Reviewed By: TroyGarden Differential Revision: D76997485 fbshipit-source-id: 164976668ba569523d90b128c08f3a80414fa6de
1 parent 5553cb0 commit 5574def

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

torchrec/sparse/jagged_tensor.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,13 +1095,18 @@ def _maybe_compute_stride_kjt(
10951095
lengths: Optional[torch.Tensor],
10961096
offsets: Optional[torch.Tensor],
10971097
stride_per_key_per_rank: Optional[torch.IntTensor],
1098+
inverse_indices: Optional[Tuple[List[str], torch.Tensor]] = None,
10981099
) -> int:
10991100
if stride is None:
11001101
if len(keys) == 0:
11011102
stride = 0
11021103
elif (
11031104
stride_per_key_per_rank is not None and stride_per_key_per_rank.numel() > 0
11041105
):
1106+
# For VBE KJT, batch size should be based on inverse_indices when set.
1107+
if inverse_indices is not None:
1108+
return inverse_indices[1].shape[-1]
1109+
11051110
s = stride_per_key_per_rank.sum(dim=1).max().item()
11061111
if not torch.jit.is_scripting() and is_non_strict_exporting():
11071112
stride = torch.sym_int(s)
@@ -2146,6 +2151,7 @@ def stride(self) -> int:
21462151
self._lengths,
21472152
self._offsets,
21482153
self._stride_per_key_per_rank,
2154+
self._inverse_indices,
21492155
)
21502156
self._stride = stride
21512157
return stride

torchrec/sparse/tests/test_keyed_jagged_tensor.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,18 @@ def test_meta_device_compatibility(self) -> None:
10171017
lengths=torch.tensor([], device=torch.device("meta")),
10181018
)
10191019

1020+
def test_vbe_kjt_stride(self) -> None:
1021+
inverse_indices = torch.tensor([[0, 1, 0], [0, 0, 0]])
1022+
kjt = KeyedJaggedTensor(
1023+
keys=["f1", "f2", "f3"],
1024+
values=torch.tensor([5, 6, 7, 1, 2, 3, 0, 1]),
1025+
lengths=torch.tensor([3, 3, 2]),
1026+
stride_per_key_per_rank=[[2], [1]],
1027+
inverse_indices=(["f1", "f2"], inverse_indices),
1028+
)
1029+
1030+
self.assertEqual(kjt.stride(), inverse_indices.shape[-1])
1031+
10201032

10211033
class TestKeyedJaggedTensorScripting(unittest.TestCase):
10221034
def test_scriptable_forward(self) -> None:

0 commit comments

Comments
 (0)