|
1 | 1 | # Copyright (c) ModelScope Contributors. All rights reserved. |
| 2 | +import megatron.core |
2 | 3 | import torch |
3 | 4 | from contextlib import contextmanager |
| 5 | +from packaging import version |
4 | 6 | from typing import Optional |
5 | 7 |
|
6 | 8 | from ..constant import ModelType |
|
15 | 17 | except ImportError: |
16 | 18 | DSAttention = object |
17 | 19 |
|
| 20 | +mcore_019 = version.parse(megatron.core.__version__) >= version.parse('0.19.0rc0') |
| 21 | + |
18 | 22 |
|
19 | 23 | class GlmMoeDsaDSAttention(DSAttention): |
20 | 24 | """DSAttention with shared indexer support for GLM 5.2. |
@@ -86,12 +90,13 @@ def forward( |
86 | 90 | if self.training and torch.is_grad_enabled(): |
87 | 91 | q, k, weights = self.indexer.forward_before_topk(x, qr, packed_seq_params) |
88 | 92 | indexer_loss_coeff = getattr(self.config, 'dsa_indexer_loss_coeff', 0.0) |
89 | | - |
| 93 | + kwargs = {} |
| 94 | + if mcore_019: |
| 95 | + kwargs['calculate_per_token_loss'] = self.config.calculate_per_token_loss |
90 | 96 | topk_indices, indexer_loss = FusedDSAIndexerLoss.apply( |
91 | 97 | q, weights, k, query.detach(), key.detach(), self.softmax_scale, |
92 | 98 | self.indexer.index_topk, indexer_loss_coeff, float_mask, |
93 | | - getattr(self.config, 'dsa_indexer_use_sparse_loss', |
94 | | - False), self.indexer.pg_collection, self.config.calculate_per_token_loss) |
| 99 | + getattr(self.config, 'dsa_indexer_use_sparse_loss', False), self.indexer.pg_collection, **kwargs) |
95 | 100 | if indexer_loss_coeff > 0: |
96 | 101 | DSAIndexerLossLoggingHelper.save_loss_to_tracker( |
97 | 102 | loss=indexer_loss, |
|
0 commit comments