Merged
fix: 量化 LI 适配 TND 格式 #1169
zx1029Add on Nov 3
fix: 量化 LI 适配 TND 格式 #1169
Merged
Total: 3 files changed+37-26
| @@ -3,7 +3,7 @@ | |||
| 3 | # This source code is licensed under the BSD-style license found in the | 3 | # This source code is licensed under the BSD-style license found in the |
| 4 | # LICENSE file in the root directory of this source tree. | 4 | # LICENSE file in the root directory of this source tree. |
| 5 | 5 | ||
| 6 | -"""The MXFP4 selector's forward and backward against the metadata the attention entry carries. | 6 | +"""The MXFP4 selector's TND forward and backward against attention metadata. |
| 7 | 7 | ||
| 8 | The launches are all this module does: the one QLI schedule (a layer that scores every visible | 8 | The launches are all this module does: the one QLI schedule (a layer that scores every visible |
| 9 | entry and the pool source, which are different schedules because the capacity sizes the candidate | 9 | entry and the pool source, which are different schedules because the capacity sizes the candidate |
| @@ -69,17 +69,28 @@ def _selector(*, mode="full", candidate_topk_blocks=_POOL): | |||
| 69 | 69 | ||
| 70 | @pytest.fixture | 70 | @pytest.fixture |
| 71 | def bypass_mxfp4_pack(monkeypatch): | 71 | def bypass_mxfp4_pack(monkeypatch): |
| 72 | - monkeypatch.setattr(lightning_indexer, "_pack_mxfp4", lambda value: (value, value.new_zeros(1))) | 72 | + def pack_tnd(value): |
| 73 | + assert value.ndim == 3 | ||
| 74 | + return value, value.new_zeros(1) | ||
| 75 | + | ||
| 76 | + monkeypatch.setattr(lightning_indexer, "_pack_mxfp4", pack_tnd) | ||
| 73 | 77 | ||
| 74 | 78 | ||
| 75 | def _inputs(): | 79 | def _inputs(): |
| 76 | return ( | 80 | return ( |
| 77 | - torch.zeros(1, _TOKENS, 2, 64, dtype=torch.bfloat16, requires_grad=True), | 81 | + torch.zeros(_TOKENS, 2, 64, dtype=torch.bfloat16, requires_grad=True), |
| 78 | - torch.zeros(1, 2, 64, dtype=torch.bfloat16, requires_grad=True), | 82 | + torch.zeros(2, 64, dtype=torch.bfloat16, requires_grad=True), |
| 79 | - torch.zeros(1, _TOKENS, 2, dtype=torch.float32, requires_grad=True), | 83 | + torch.zeros(_TOKENS, 2, dtype=torch.float32, requires_grad=True), |
| 80 | ) | 84 | ) |
| 81 | 85 | ||
| 82 | 86 | ||
| 87 | +def _assert_tnd_kernel_inputs(args): | ||
| 88 | + assert args[0].shape == (_TOKENS, 2, 64) | ||
| 89 | + assert args[1].shape == (2, 1, 64) | ||
| 90 | + assert args[2].shape == (_TOKENS, 2) | ||
| 91 | + assert args[0].ndim == args[1].ndim == 3 | ||
| 92 | + | ||
| 93 | + | ||
| 83 | @pytest.mark.parametrize( | 94 | @pytest.mark.parametrize( |
| 84 | "query_count, expected_teacher", | 95 | "query_count, expected_teacher", |
| 85 | [(8.0, 1.0), (32.0, 0.25), (None, None)], | 96 | [(8.0, 1.0), (32.0, 0.25), (None, None)], |
| @@ -103,6 +114,7 @@ def test_qli_source_sorts_selection_publishes_pool_and_runs_slikg( | |||
| 103 | 114 | ||
| 104 | def qli(*args, **kwargs): | 115 | def qli(*args, **kwargs): |
| 105 | seen["qli"] = (args, kwargs) | 116 | seen["qli"] = (args, kwargs) |
| 117 | + _assert_tnd_kernel_inputs(args) | ||
| 106 | return unsorted, torch.empty(0), candidate_blocks, torch.empty(0) | 118 | return unsorted, torch.empty(0), candidate_blocks, torch.empty(0) |
| 107 | 119 | ||
| 108 | def slikg(**kwargs): | 120 | def slikg(**kwargs): |
| @@ -138,7 +150,7 @@ def test_qli_source_sorts_selection_publishes_pool_and_runs_slikg( | |||
| 138 | scores.backward(torch.full_like(scores, 8.0)) | 150 | scores.backward(torch.full_like(scores, 8.0)) |
| 139 | 151 | ||
| 140 | indexer = metadata.aicpu_metadata.lightning_indexer | 152 | indexer = metadata.aicpu_metadata.lightning_indexer |
| 141 | - assert torch.equal(indices[0, 0], torch.tensor([3, 2, 1, -1], dtype=torch.int32)) | 153 | + assert torch.equal(indices[0], torch.tensor([3, 2, 1, -1], dtype=torch.int32)) |
| 142 | assert scores.shape == indices.shape and scores.requires_grad | 154 | assert scores.shape == indices.shape and scores.requires_grad |
| 143 | assert torch.equal(candidate, candidate_blocks.unsqueeze(0)) | 155 | assert torch.equal(candidate, candidate_blocks.unsqueeze(0)) |
| 144 | # The pool source launches the schedule that was sized by the capacity, with the same capacity: | 156 | # The pool source launches the schedule that was sized by the capacity, with the same capacity: |
| @@ -172,6 +184,7 @@ def test_qsli_searcher_searches_the_precomputed_pool_and_passes_it_through(monke | |||
| 172 | 184 | ||
| 173 | def qsli(*args, **kwargs): | 185 | def qsli(*args, **kwargs): |
| 174 | seen["qsli"] = (args, kwargs) | 186 | seen["qsli"] = (args, kwargs) |
| 187 | + _assert_tnd_kernel_inputs(args) | ||
| 175 | return topk_indices, torch.empty(0) | 188 | return topk_indices, torch.empty(0) |
| 176 | 189 | ||
| 177 | monkeypatch.setattr( | 190 | monkeypatch.setattr( |
| @@ -184,7 +197,7 @@ def test_qsli_searcher_searches_the_precomputed_pool_and_passes_it_through(monke | |||
| 184 | indices, scores, candidate_out = selector(q, k, w, metadata, candidates_BL1C=candidate) | 197 | indices, scores, candidate_out = selector(q, k, w, metadata, candidates_BL1C=candidate) |
| 185 | 198 | ||
| 186 | indexer = metadata.aicpu_metadata.lightning_indexer | 199 | indexer = metadata.aicpu_metadata.lightning_indexer |
| 187 | - assert indices.shape == scores.shape == (1, 4, 4) | 200 | + assert indices.shape == scores.shape == (4, 4) |
| 188 | torch.testing.assert_close(candidate_out, candidate, rtol=0, atol=0) | 201 | torch.testing.assert_close(candidate_out, candidate, rtol=0, atol=0) |
| 189 | # The search is bound by the count the port derived for these frames, and by the schedule that | 202 | # The search is bound by the count the port derived for these frames, and by the schedule that |
| 190 | # count sized: neither is rebuilt here, and neither depends on QLI having run. | 203 | # count sized: neither is rebuilt here, and neither depends on QLI having run. |
| @@ -207,6 +220,7 @@ def test_qli_without_candidate_pool_runs_the_pool_free_schedule(monkeypatch, byp | |||
| 207 | 220 | ||
| 208 | def qli(*args, **kwargs): | 221 | def qli(*args, **kwargs): |
| 209 | seen["qli"] = (args, kwargs) | 222 | seen["qli"] = (args, kwargs) |
| 223 | + _assert_tnd_kernel_inputs(args) | ||
| 210 | return topk_indices, torch.empty(0), candidate_blocks, torch.ones_like(candidate_blocks) | 224 | return topk_indices, torch.empty(0), candidate_blocks, torch.ones_like(candidate_blocks) |
| 211 | 225 | ||
| 212 | monkeypatch.setattr( | 226 | monkeypatch.setattr( |
| @@ -219,7 +233,7 @@ def test_qli_without_candidate_pool_runs_the_pool_free_schedule(monkeypatch, byp | |||
| 219 | indices, scores, candidate = selector(q, k, w, metadata, candidates_BL1C=None) | 233 | indices, scores, candidate = selector(q, k, w, metadata, candidates_BL1C=None) |
| 220 | 234 | ||
| 221 | indexer = metadata.aicpu_metadata.lightning_indexer | 235 | indexer = metadata.aicpu_metadata.lightning_indexer |
| 222 | - assert indices.shape == scores.shape == (1, 4, 4) | 236 | + assert indices.shape == scores.shape == (4, 4) |
| 223 | assert candidate is None | 237 | assert candidate is None |
| 224 | # A selector that scores every visible entry launches the pool-free schedule, and the kernels' | 238 | # A selector that scores every visible entry launches the pool-free schedule, and the kernels' |
| 225 | # own "no pool" sentinel with it. | 239 | # own "no pool" sentinel with it. |
| @@ -256,7 +270,7 @@ def test_a_pool_free_layer_inside_a_pool_geometry_scores_every_entry(monkeypatch | |||
| 256 | indices, scores, candidate = selector(q, k, w, metadata, candidates_BL1C=None) | 270 | indices, scores, candidate = selector(q, k, w, metadata, candidates_BL1C=None) |
| 257 | 271 | ||
| 258 | indexer = metadata.aicpu_metadata.lightning_indexer | 272 | indexer = metadata.aicpu_metadata.lightning_indexer |
| 259 | - assert indices.shape == scores.shape == (1, 4, 4) | 273 | + assert indices.shape == scores.shape == (4, 4) |
| 260 | assert candidate is None | 274 | assert candidate is None |
| 261 | assert seen["qli"][1]["metadata"] is indexer.fwd | 275 | assert seen["qli"][1]["metadata"] is indexer.fwd |
| 262 | assert seen["qli"][1]["candidate_topk_blocks"] == -1 | 276 | assert seen["qli"][1]["candidate_topk_blocks"] == -1 |
| @@ -23,9 +23,9 @@ class DeepSeekV41QuantLightningIndexerConfig(ModuleSwapConfig): | |||
| 23 | 23 | ||
| 24 | def _quantized_forward( | 24 | def _quantized_forward( |
| 25 | self, | 25 | self, |
| 26 | - idx_q_BLHiDi, | 26 | + idx_q_THDi, |
| 27 | - idx_k_BNDi, | 27 | + idx_k_NDi, |
| 28 | - weights_BLHi, | 28 | + weights_TH, |
| 29 | attention_masks, | 29 | attention_masks, |
| 30 | *, | 30 | *, |
| 31 | candidates_BL1C, | 31 | candidates_BL1C, |
| @@ -34,9 +34,9 @@ def _quantized_forward( | |||
| 34 | 34 | ||
| 35 | return QuantV41LightningIndexer.forward( | 35 | return QuantV41LightningIndexer.forward( |
| 36 | self, | 36 | self, |
| 37 | - idx_q_BLHiDi, | 37 | + idx_q_THDi, |
| 38 | - idx_k_BNDi, | 38 | + idx_k_NDi, |
| 39 | - weights_BLHi, | 39 | + weights_TH, |
| 40 | attention_masks, | 40 | attention_masks, |
| 41 | candidates_BL1C=candidates_BL1C, | 41 | candidates_BL1C=candidates_BL1C, |
| 42 | ) | 42 | ) |
| @@ -251,24 +251,21 @@ class QuantV41LightningIndexer(torch.nn.Module): | |||
| 251 | 251 | ||
| 252 | def forward( | 252 | def forward( |
| 253 | self, | 253 | self, |
| 254 | - idx_q_BLHiDi: torch.Tensor, | 254 | + idx_q_THDi: torch.Tensor, |
| 255 | - idx_k_BNDi: torch.Tensor, | 255 | + idx_k_NDi: torch.Tensor, |
| 256 | - weights_BLHi: torch.Tensor, | 256 | + weights_TH: torch.Tensor, |
| 257 | attention_metadata, | 257 | attention_metadata, |
| 258 | *, | 258 | *, |
| 259 | candidates_BL1C: torch.Tensor | None, | 259 | candidates_BL1C: torch.Tensor | None, |
| 260 | ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor | None]: | 260 | ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor | None]: |
| 261 | - if idx_q_BLHiDi.shape[0] != 1 or idx_k_BNDi.shape[0] != 1 or weights_BLHi.shape[0] != 1: | ||
| 262 | - raise ValueError("V4.1 QLI/QSLI requires one packed TND row (model batch size 1)") | ||
| 263 | - | ||
| 264 | # Which schedule this layer launches, and whether it builds the pool, follow from its own | 261 | # Which schedule this layer launches, and whether it builds the pool, follow from its own |
| 265 | # role -- which the model states and the entry's schedules were sized for. | 262 | # role -- which the model states and the entry's schedules were sized for. |
| 266 | is_source = self.has_candidate_pool and self.mode == "full" | 263 | is_source = self.has_candidate_pool and self.mode == "full" |
| 267 | topk = self.index_topk | 264 | topk = self.index_topk |
| 268 | topk_indices, topk_scores, candidate = _QuantV41LightningIndexerTND.apply( | 265 | topk_indices, topk_scores, candidate = _QuantV41LightningIndexerTND.apply( |
| 269 | - idx_q_BLHiDi.squeeze(0), | 266 | + idx_q_THDi, |
| 270 | - idx_k_BNDi.squeeze(0).unsqueeze(1), | 267 | + idx_k_NDi.unsqueeze(1), |
| 271 | - weights_BLHi.squeeze(0).float(), | 268 | + weights_TH.float(), |
| 272 | topk, | 269 | topk, |
| 273 | self.compress_ratio, | 270 | self.compress_ratio, |
| 274 | attention_metadata, | 271 | attention_metadata, |
| @@ -281,7 +278,7 @@ class QuantV41LightningIndexer(torch.nn.Module): | |||
| 281 | self.candidate_block_size, | 278 | self.candidate_block_size, |
| 282 | ) | 279 | ) |
| 283 | return ( | 280 | return ( |
| 284 | - topk_indices.reshape(1, -1, topk), | 281 | + topk_indices.reshape(-1, topk), |
| 285 | - topk_scores.reshape(1, -1, topk), | 282 | + topk_scores.reshape(-1, topk), |
| 286 | candidate, | 283 | candidate, |
| 287 | ) | 284 | ) |