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

Make padding_mask Optional in SequenceBatch. #106

Merged
merged 2 commits into from
Oct 17, 2023
Merged
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
5 changes: 4 additions & 1 deletion src/fairseq2/models/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SequenceBatch:
size, :math:`S` is the sequence length, and :math:`*` is any number of
sequence-specific dimensions including none."""

padding_mask: PaddingMask
padding_mask: Optional[PaddingMask]
"""The padding mask of ``seqs``. *Shape:* :math:`(N,S)`, where :math:`N` is
the batch size and :math:`S` is the sequence length."""

Expand All @@ -51,6 +51,9 @@ def batch_size(self) -> int:

def compute_num_tokens(self) -> Tensor:
"""Compute the number of tokens in this batch."""
if self.padding_mask is None:
return torch.full((), self.seqs.numel(), device=self.seqs.device)

return self.padding_mask.seq_lens.sum()


Expand Down