Instructions to use inclusionAI/Ling-3.1-flash with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Inference
- HuggingChat
- Notebooks
- Google Colab
- Kaggle
fix(mtp): skip disabled loss and handle empty targets
#7
by iCavan - opened
- modeling_bailing_moe_v3.py +17 -5
modeling_bailing_moe_v3.py
CHANGED
|
@@ -1552,7 +1552,9 @@ class BailingMoeV3ForCausalLM(BailingMoeV3PreTrainedModel, GenerationMixin):
|
|
| 1552 |
output_router_logits if output_router_logits is not None else self.config.output_router_logits
|
| 1553 |
)
|
| 1554 |
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 1555 |
-
compute_mtp_loss =
|
|
|
|
|
|
|
| 1556 |
# MTP hidden states are kept outside the tuple output, so retain the structured output while
|
| 1557 |
# computing its loss.
|
| 1558 |
model_return_dict = return_dict or compute_mtp_loss
|
|
@@ -1584,10 +1586,8 @@ class BailingMoeV3ForCausalLM(BailingMoeV3PreTrainedModel, GenerationMixin):
|
|
| 1584 |
|
| 1585 |
all_mtp_logits = None
|
| 1586 |
if compute_mtp_loss and outputs.mtp_hidden_states is not None:
|
| 1587 |
-
mtp_hidden_states = outputs.mtp_hidden_states
|
| 1588 |
shift_labels_mtp = None
|
| 1589 |
-
for
|
| 1590 |
-
mtp_hidden_states = mtp_hidden_states[i]
|
| 1591 |
mtp_logits = self.lm_head(mtp_hidden_states).float()
|
| 1592 |
if all_mtp_logits is None:
|
| 1593 |
all_mtp_logits = []
|
|
@@ -1597,8 +1597,20 @@ class BailingMoeV3ForCausalLM(BailingMoeV3PreTrainedModel, GenerationMixin):
|
|
| 1597 |
shift_labels_mtp = labels.clone()
|
| 1598 |
shift_labels_mtp, _ = roll_tensor(shift_labels_mtp, shifts=-1, dims=-1, fill_value=-100)
|
| 1599 |
mtp_logits_ = mtp_logits.view(-1, self.config.vocab_size)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1600 |
mtp_loss = self.loss_function(
|
| 1601 |
-
mtp_logits_,
|
|
|
|
|
|
|
|
|
|
| 1602 |
)
|
| 1603 |
if loss is not None:
|
| 1604 |
loss += self.mtp_loss_scaling_factor * mtp_loss
|
|
|
|
| 1552 |
output_router_logits if output_router_logits is not None else self.config.output_router_logits
|
| 1553 |
)
|
| 1554 |
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 1555 |
+
compute_mtp_loss = (
|
| 1556 |
+
labels is not None and self.num_nextn_predict_layers > 0 and self.mtp_loss_scaling_factor > 0
|
| 1557 |
+
)
|
| 1558 |
# MTP hidden states are kept outside the tuple output, so retain the structured output while
|
| 1559 |
# computing its loss.
|
| 1560 |
model_return_dict = return_dict or compute_mtp_loss
|
|
|
|
| 1586 |
|
| 1587 |
all_mtp_logits = None
|
| 1588 |
if compute_mtp_loss and outputs.mtp_hidden_states is not None:
|
|
|
|
| 1589 |
shift_labels_mtp = None
|
| 1590 |
+
for mtp_hidden_states in outputs.mtp_hidden_states:
|
|
|
|
| 1591 |
mtp_logits = self.lm_head(mtp_hidden_states).float()
|
| 1592 |
if all_mtp_logits is None:
|
| 1593 |
all_mtp_logits = []
|
|
|
|
| 1597 |
shift_labels_mtp = labels.clone()
|
| 1598 |
shift_labels_mtp, _ = roll_tensor(shift_labels_mtp, shifts=-1, dims=-1, fill_value=-100)
|
| 1599 |
mtp_logits_ = mtp_logits.view(-1, self.config.vocab_size)
|
| 1600 |
+
mtp_loss_kwargs = dict(kwargs)
|
| 1601 |
+
if mtp_loss_kwargs.get("num_items_in_batch") is None:
|
| 1602 |
+
loss_labels = mtp_loss_kwargs.get("shift_labels")
|
| 1603 |
+
if loss_labels is None:
|
| 1604 |
+
loss_labels = shift_labels_mtp.view(-1)[1:]
|
| 1605 |
+
# Avoid a mean over zero targets after the causal shift.
|
| 1606 |
+
mtp_loss_kwargs["num_items_in_batch"] = (
|
| 1607 |
+
(loss_labels != mtp_loss_kwargs.get("ignore_index", -100)).sum().clamp_min(1)
|
| 1608 |
+
)
|
| 1609 |
mtp_loss = self.loss_function(
|
| 1610 |
+
mtp_logits_,
|
| 1611 |
+
shift_labels_mtp.to(mtp_logits_.device).view(-1),
|
| 1612 |
+
self.config.vocab_size,
|
| 1613 |
+
**mtp_loss_kwargs,
|
| 1614 |
)
|
| 1615 |
if loss is not None:
|
| 1616 |
loss += self.mtp_loss_scaling_factor * mtp_loss
|