-
Notifications
You must be signed in to change notification settings - Fork 425
Support MTP loss mask_type v1 and multi mtp_config #1919
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
Open
x54-729
wants to merge
14
commits into
InternLM:main
Choose a base branch
from
x54-729:mtp_260616
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
11f2848
add multi mtp config; add mtp mask type v1
x54-729 b427a7f
Add SciMTPLossContext SciMTPConfig SciMTPLossConfig; Compatible with …
x54-729 7e80e95
remove SciMTPConfig; bind layer_idx before build mtp loss ctx
x54-729 5045ea9
move bind_mtp_Depth to MTPLossContext
x54-729 92f4ff9
record mtp_name when save qwen3_5 to hf
x54-729 077eb54
fix reshard_after_forward judge using global_mtp_idx
x54-729 19cbcca
remove mtp_loss if comment since total loss is calculated in forward
x54-729 be15d56
rename param name from idx to depth in bind_mtp_depth
x54-729 da6293a
remove _mtp_forward in moe
x54-729 5c5bba4
small fix in moe.py
x54-729 be6b91c
fix bind_layer_idx doc
x54-729 6e079d3
indet mtp_loss
x54-729 86971dd
Change mtp_block from ModuleDict to ModuleList
x54-729 0bcbace
remove mtp_loss sum in moe.py; change total_loss sum in train_engine.py
x54-729 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -569,6 +569,9 @@ def _get_total_loss(self, model_outputs: ModelOutputs) -> torch.Tensor: | |||||||||||||||||||
| loss = torch.tensor(0.0, device=DEVICE) | ||||||||||||||||||||
| for key in model_outputs.model_fields: | ||||||||||||||||||||
| value = getattr(model_outputs, key) | ||||||||||||||||||||
| if "loss" in key and isinstance(value, torch.Tensor): | ||||||||||||||||||||
| if key == "mtp_loss" and isinstance(value, dict): | ||||||||||||||||||||
| for mtp_loss_name, mtp_loss in value.items(): | ||||||||||||||||||||
| loss += mtp_loss | ||||||||||||||||||||
| elif "loss" in key and isinstance(value, torch.Tensor): | ||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
| loss += value | ||||||||||||||||||||
| return loss | ||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| # Copyright (c) OpenMMLab. All rights reserved. | ||
| from typing import Any | ||
| from typing import Any, Optional | ||
|
|
||
| import torch | ||
| import torch.nn.functional as F | ||
|
|
@@ -60,6 +60,7 @@ class MTPLossConfig(CELossConfig): | |
|
|
||
| mtp_depth: int | ||
| detach_mtp_lm_head_weight: bool = False | ||
| mask_type: Optional[str] = None | ||
|
|
||
| @property | ||
| def loss_ctx_cls(self) -> type["MTPLossContext"]: | ||
|
|
@@ -167,6 +168,12 @@ def forward( | |
| head_weight = head_weight.detach() | ||
| head_bias = head_bias.detach() if head_bias is not None else None | ||
| # Dispatch to eager_mode/chunk_mode via base class, which calls loss_fn per chunk | ||
|
|
||
| mask_type = self.loss_cfg.mask_type | ||
| if mask_type == "v1": | ||
| self.process_loss_weight_v1() | ||
| elif mask_type is not None: | ||
| raise NotImplementedError(f"Unknown MTP Loss Mask Type: {mask_type}") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The calculation logic of loss should not be hard-coded here; please implement a new loss_context. |
||
| return super().forward(hidden_states, head_weight, head_bias) | ||
|
|
||
| def loss_fn( | ||
|
|
@@ -214,3 +221,34 @@ def _kl_loss_fn( | |
| ) | ||
|
|
||
| return kl_loss, (None, {}) | ||
|
|
||
| def process_loss_weight_v1(self): | ||
|
HAOCHENYE marked this conversation as resolved.
|
||
| layer_idx = self.loss_cfg.mtp_depth - 1 | ||
| shifted_labels = self.loss_kwargs.shifted_labels | ||
| loss_weight = self.loss_kwargs.loss_weight | ||
| sum_loss_weight = loss_weight.sum() | ||
|
|
||
| easy_to_use = torch.cat( | ||
| [ | ||
| shifted_labels, | ||
| torch.zeros((shifted_labels.size(0), 1), dtype=shifted_labels.dtype, device=shifted_labels.device), | ||
| ], | ||
| dim=-1, | ||
| ) | ||
|
|
||
| # TODO: digit and dot token config | ||
| is_digit = torch.where(easy_to_use < 25, easy_to_use > 14, 0) | ||
| is_dot = torch.where(easy_to_use == 13, 1, 0) | ||
| is_digit_or_dot = is_digit | is_dot | ||
|
|
||
| mask = is_digit_or_dot.clone() | ||
| for i in range(layer_idx + 1): | ||
| mask |= torch.roll(is_digit_or_dot, shifts=i + 1, dims=-1) | ||
|
|
||
| mtp_mask = mask.bool()[:, :-1] | ||
|
|
||
| loss_weight[mtp_mask == 0.0] = 0.0 | ||
| if loss_weight.sum().item() != 0: | ||
| loss_weight = loss_weight * sum_loss_weight / loss_weight.sum() | ||
|
|
||
| self.loss_kwargs.loss_weight = loss_weight | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The loss part should be autonomous to the model, rather than being hardcoded here.