|
| 1 | +# Copyright (c) OpenMMLab. All rights reserved. |
| 2 | +from torch.distributed.device_mesh import DeviceMesh |
| 3 | + |
| 4 | +from xtuner.v1.loss.ce_loss import CELossConfig, CELossKwargs, LMHeadLossContext |
| 5 | +from xtuner.v1.module.mtp.utils import roll_packed_tensor |
| 6 | +from xtuner.v1.utils.device import get_device |
| 7 | + |
| 8 | + |
| 9 | +DEVICE = get_device() |
| 10 | + |
| 11 | + |
| 12 | +class MTPLossKwargs(CELossKwargs): |
| 13 | + """Keyword arguments for MTP loss computation. |
| 14 | +
|
| 15 | + Inherits all fields from CELossKwargs. The ``shifted_labels`` field is |
| 16 | + expected to be pre-rolled by ``MTPLossConfig.build()`` before this object |
| 17 | + is constructed, so no additional fields are required. |
| 18 | +
|
| 19 | + Args: |
| 20 | + shifted_labels (torch.Tensor): The shifted and rolled labels for MTP |
| 21 | + loss computation. |
| 22 | + loss_weight (torch.Tensor | None): Per-token loss weight. |
| 23 | + """ |
| 24 | + |
| 25 | + |
| 26 | +class MTPLossConfig(CELossConfig): |
| 27 | + """Loss configuration for Multi-Token Prediction (MTP). |
| 28 | +
|
| 29 | + Extends ``CELossConfig`` with a ``mtp_depth`` field that controls how many |
| 30 | + additional positions the labels are rolled during ``build()``. This class |
| 31 | + is intended for internal use by the model and is not exposed to users. |
| 32 | +
|
| 33 | + Args: |
| 34 | + mtp_depth (int): 1-indexed MTP layer depth. The first MTP layer uses |
| 35 | + ``mtp_depth=1`` (shift=-1 on top of the existing label shift). |
| 36 | + """ |
| 37 | + |
| 38 | + mtp_depth: int |
| 39 | + |
| 40 | + @property |
| 41 | + def loss_ctx_cls(self) -> type["MTPLossContext"]: |
| 42 | + return MTPLossContext |
| 43 | + |
| 44 | + @property |
| 45 | + def _loss_kwargs_cls(self) -> type["MTPLossKwargs"]: |
| 46 | + return MTPLossKwargs |
| 47 | + |
| 48 | + def build(self, data: dict, sp_mesh: DeviceMesh | None = None) -> "MTPLossContext | None": |
| 49 | + """Build MTPLossContext from data dict. |
| 50 | +
|
| 51 | + Rolls ``shifted_labels`` by ``-mtp_depth`` positions (per-sequence, |
| 52 | + respecting packed-sequence boundaries) before constructing the loss |
| 53 | + context. The roll is performed on the full sequence prior to any |
| 54 | + sequence-parallel split so that boundary positions and ``cu_seq_lens`` |
| 55 | + are always consistent. |
| 56 | +
|
| 57 | + Args: |
| 58 | + data (dict): Data dict containing loss-related fields. |
| 59 | + Required keys: ``shifted_labels``, ``seq_ctx``. |
| 60 | + sp_mesh (DeviceMesh | None): Sequence parallel mesh. |
| 61 | +
|
| 62 | + Returns: |
| 63 | + MTPLossContext | None: Built loss context, or ``None`` if |
| 64 | + ``shifted_labels`` is not present in ``data``. |
| 65 | + """ |
| 66 | + if "shifted_labels" not in data: |
| 67 | + return None |
| 68 | + |
| 69 | + shifted_labels = data["shifted_labels"] |
| 70 | + cu_seq_lens = data["seq_ctx"].cu_seq_lens_k |
| 71 | + |
| 72 | + rolled = roll_packed_tensor(shifted_labels, cu_seq_lens, shifts=-self.mtp_depth, dim=-1, fill_value=-100) |
| 73 | + |
| 74 | + loss_kwargs = MTPLossKwargs(shifted_labels=rolled).to(DEVICE) |
| 75 | + if sp_mesh is not None and sp_mesh.size() > 1: |
| 76 | + loss_kwargs = loss_kwargs.sp_split(sp_mesh) |
| 77 | + |
| 78 | + return MTPLossContext(self, loss_kwargs) |
| 79 | + |
| 80 | + |
| 81 | +class MTPLossContext(LMHeadLossContext): |
| 82 | + """Loss context for Multi-Token Prediction (MTP). |
| 83 | +
|
| 84 | + Inherits all computation logic from ``LMHeadLossContext``. The label |
| 85 | + rolling is handled upstream in ``MTPLossConfig.build()``, so no override |
| 86 | + is needed here. |
| 87 | +
|
| 88 | + Args: |
| 89 | + loss_cfg (MTPLossConfig): The MTP loss configuration. |
| 90 | + loss_kwargs (MTPLossKwargs): Pre-rolled keyword arguments for loss |
| 91 | + computation. |
| 92 | + """ |
0 commit comments