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

Fixed left_pad_sequence - correctly flip dims based on batch_first #1523

Merged
merged 2 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions tests/torchtune/data/test_collate.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def test_left_pad_sequence(self):
expected = torch.tensor([[0, 0, 1, 2, 3], [0, 4, 5, 6, 7], [8, 9, 10, 11, 12]])
assert torch.equal(result, expected)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could I be annoying and ask if you could explicitly write out the expected tensor, please? Helps a lot to understand what's going at a glance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! I've added the expected tensor for the batch_first=False case.

result = left_pad_sequence([a, b, c], batch_first=False, padding_value=0)
assert torch.equal(result, expected.transpose(0, 1))


class TestPaddedCollate:
def test_padded_collate_classifier_labels(self):
Expand Down
2 changes: 1 addition & 1 deletion torchtune/data/_collate.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def left_pad_sequence(
map(lambda x: torch.flip(x, dims=[0]), sequences),
batch_first=batch_first,
padding_value=padding_value,
).flip(dims=[1])
).flip(dims=[int(batch_first)])


def padded_collate(
Expand Down
Loading