Skip to content
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

add stride into KJT pytree #2587

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 11 additions & 5 deletions torchrec/sparse/jagged_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3031,13 +3031,17 @@ def dist_init(

def _kjt_flatten(
t: KeyedJaggedTensor,
) -> Tuple[List[Optional[torch.Tensor]], List[str]]:
return [getattr(t, a) for a in KeyedJaggedTensor._fields], t._keys
) -> Tuple[List[Optional[torch.Tensor]], Tuple[List[str], int]]:
# for variable batch scenario, the stride cannot be computed from lengths/len(keys),
# instead, it should be computed from stride_per_key_per_rank, which is not included
# in the flatten spec. The stride is needed for the EBC output shape, so we need to
# store it in the context.
return [getattr(t, a) for a in KeyedJaggedTensor._fields], (t._keys, t.stride())


def _kjt_flatten_with_keys(
t: KeyedJaggedTensor,
) -> Tuple[List[Tuple[KeyEntry, Optional[torch.Tensor]]], List[str]]:
) -> Tuple[List[Tuple[KeyEntry, Optional[torch.Tensor]]], Tuple[List[str], int]]:
values, context = _kjt_flatten(t)
# pyre can't tell that GetAttrKey implements the KeyEntry protocol
return [ # pyre-ignore[7]
Expand All @@ -3046,9 +3050,11 @@ def _kjt_flatten_with_keys(


def _kjt_unflatten(
values: List[Optional[torch.Tensor]], context: List[str] # context is the _keys
values: List[Optional[torch.Tensor]],
context: List[str], # context is (_keys, _stride)
) -> KeyedJaggedTensor:
return KeyedJaggedTensor(context, *values)
keys, stride = context
return KeyedJaggedTensor(keys, *values, stride=stride)


def _kjt_flatten_spec(
Expand Down
Loading