|
6 | 6 |
|
7 | 7 | # (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary. |
8 | 8 |
|
| 9 | +from unittest import mock |
| 10 | + |
9 | 11 | import pytest |
10 | 12 | import torch |
| 13 | +from tests.test_utils import gpu_test |
11 | 14 | from torchtune.data import ( |
12 | 15 | left_pad_sequence, |
13 | 16 | padded_collate, |
14 | 17 | padded_collate_dpo, |
| 18 | + padded_collate_packed, |
15 | 19 | padded_collate_sft, |
16 | 20 | ) |
| 21 | +from torchtune.modules.attention_utils import _SUPPORTS_FLEX_ATTENTION |
17 | 22 |
|
18 | 23 |
|
19 | 24 | class TestPaddedCollateSFT: |
@@ -47,6 +52,119 @@ def test_batch_pad_sequence(self): |
47 | 52 | padded_label, torch.tensor([10, ignore_idx, ignore_idx]) |
48 | 53 | ) |
49 | 54 |
|
| 55 | + @mock.patch("torchtune.modules.attention_utils._SUPPORTS_FLEX_ATTENTION", False) |
| 56 | + def test_padded_collate_packed_sdpa(self): |
| 57 | + token_pairs = [ |
| 58 | + { |
| 59 | + "tokens": torch.tensor([1, 2, 3, 4, 5, 6]), |
| 60 | + "labels": torch.tensor([7, 8, 9, 10, 11, 12]), |
| 61 | + "input_pos": torch.tensor([0, 1, 2, 0, 1, 0]), |
| 62 | + "seq_lens": torch.tensor([3, 2, 1]), |
| 63 | + }, |
| 64 | + { |
| 65 | + "tokens": torch.tensor([13, 14, 15, 16, 17, 18]), |
| 66 | + "labels": torch.tensor([19, 20, 21, 22, 23, 24]), |
| 67 | + "input_pos": torch.tensor([0, 1, 0, 1, 0, 1]), |
| 68 | + "seq_lens": torch.tensor([2, 2, 2]), |
| 69 | + }, |
| 70 | + ] |
| 71 | + collated = padded_collate_packed( |
| 72 | + batch=token_pairs, |
| 73 | + ) |
| 74 | + torch.testing.assert_close( |
| 75 | + collated["tokens"], |
| 76 | + torch.tensor([[1, 2, 3, 4, 5, 6], [13, 14, 15, 16, 17, 18]]), |
| 77 | + ) |
| 78 | + torch.testing.assert_close( |
| 79 | + collated["labels"], |
| 80 | + torch.tensor([[7, 8, 9, 10, 11, 12], [19, 20, 21, 22, 23, 24]]), |
| 81 | + ) |
| 82 | + torch.testing.assert_close( |
| 83 | + collated["input_pos"], |
| 84 | + torch.tensor([[0, 1, 2, 0, 1, 0], [0, 1, 0, 1, 0, 1]]), |
| 85 | + ) |
| 86 | + torch.testing.assert_close( |
| 87 | + collated["mask"], |
| 88 | + torch.tensor( |
| 89 | + [ |
| 90 | + [ |
| 91 | + [1, 0, 0, 0, 0, 0], |
| 92 | + [1, 1, 0, 0, 0, 0], |
| 93 | + [1, 1, 1, 0, 0, 0], |
| 94 | + [0, 0, 0, 1, 0, 0], |
| 95 | + [0, 0, 0, 1, 1, 0], |
| 96 | + [0, 0, 0, 0, 0, 1], |
| 97 | + ], |
| 98 | + [ |
| 99 | + [1, 0, 0, 0, 0, 0], |
| 100 | + [1, 1, 0, 0, 0, 0], |
| 101 | + [0, 0, 1, 0, 0, 0], |
| 102 | + [0, 0, 1, 1, 0, 0], |
| 103 | + [0, 0, 0, 0, 1, 0], |
| 104 | + [0, 0, 0, 0, 1, 1], |
| 105 | + ], |
| 106 | + ], |
| 107 | + dtype=torch.bool, |
| 108 | + ), |
| 109 | + ) |
| 110 | + |
| 111 | + @pytest.mark.skipif( |
| 112 | + not _SUPPORTS_FLEX_ATTENTION, |
| 113 | + reason="Please install a nightly build of torch to run this test.", |
| 114 | + ) |
| 115 | + @gpu_test(gpu_count=1) |
| 116 | + def test_padded_collate_packed_flex(self): |
| 117 | + # create_block_mask requires that seq_len be divisible by 128, the default block size. |
| 118 | + # see https://github.com/pytorch/pytorch/blob/main/torch/nn/attention/flex_attention.py#L636 |
| 119 | + batch = [ |
| 120 | + { |
| 121 | + "tokens": torch.arange(128, dtype=torch.long), |
| 122 | + "labels": torch.arange(128, dtype=torch.long), |
| 123 | + "input_pos": torch.arange(128, dtype=torch.long), |
| 124 | + "seq_lens": torch.ones(64, dtype=torch.long) * 2, |
| 125 | + }, |
| 126 | + { |
| 127 | + "tokens": torch.arange(128, 256, dtype=torch.long), |
| 128 | + "labels": torch.arange(128, 256, dtype=torch.long), |
| 129 | + "input_pos": torch.arange(128, 256, dtype=torch.long), |
| 130 | + "seq_lens": torch.ones(32, dtype=torch.long) * 4, |
| 131 | + }, |
| 132 | + ] |
| 133 | + collated = padded_collate_packed( |
| 134 | + batch=batch, |
| 135 | + ) |
| 136 | + torch.testing.assert_close( |
| 137 | + collated["tokens"], |
| 138 | + torch.stack( |
| 139 | + [ |
| 140 | + torch.arange(128, dtype=torch.long), |
| 141 | + torch.arange(128, 256, dtype=torch.long), |
| 142 | + ] |
| 143 | + ), |
| 144 | + ) |
| 145 | + torch.testing.assert_close( |
| 146 | + collated["labels"], |
| 147 | + torch.stack( |
| 148 | + [ |
| 149 | + torch.arange(128, dtype=torch.long), |
| 150 | + torch.arange(128, 256, dtype=torch.long), |
| 151 | + ] |
| 152 | + ), |
| 153 | + ) |
| 154 | + torch.testing.assert_close( |
| 155 | + collated["input_pos"], |
| 156 | + torch.stack( |
| 157 | + [ |
| 158 | + torch.arange(128, dtype=torch.long), |
| 159 | + torch.arange(128, 256, dtype=torch.long), |
| 160 | + ] |
| 161 | + ), |
| 162 | + ) |
| 163 | + torch.testing.assert_close( |
| 164 | + collated["mask"].to_dense(), |
| 165 | + torch.tensor([[[[1]]], [[[1]]]], dtype=torch.int32, device="cuda"), |
| 166 | + ) |
| 167 | + |
50 | 168 |
|
51 | 169 | class TestLeftPadSequence: |
52 | 170 | def test_left_pad_sequence(self): |
|
0 commit comments