Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions megatron/core/transformer/multi_token_prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ def save_metrics_to_tracker(
"""Save normalized MTP loss and acceptance counts for logging.

This compatibility path is used by tests and callers that already
computed a normalized per-layer loss. Dynamic-CP code should use
``save_loss_to_tracker`` so loss is weighted by token counts.
computed a normalized per-layer loss. Dynamic-CP code uses
``save_loss_to_tracker`` to normalize each local contribution safely.
"""
if layer_number is None:
return
Expand Down Expand Up @@ -402,11 +402,12 @@ def save_loss_to_tracker(
reduce_group: Optional[torch.distributed.ProcessGroup] = None,
avg_group: Optional[torch.distributed.ProcessGroup] = None,
):
"""Save the mtp loss sum and token count for logging.
"""Normalize and accumulate a local MTP loss for logging.

Stores raw sums so that the global per-token loss can be computed
correctly after all-reduce, even when token counts differ across
ranks (e.g. Dynamic CP) or microbatches.
MTP is normalized independently for each microbatch. The tracker
accumulates those normalized losses, then reduction combines the sums
across ranks. This intentionally preserves sequence-packing semantics
instead of changing the metric to global ``sum(loss) / sum(tokens)``.

Args:
loss_sum (torch.Tensor): Sum of per-element losses on this rank.
Expand All @@ -415,18 +416,17 @@ def save_loss_to_tracker(
num_layers (int): The number of total layers.
correct (Optional[torch.Tensor]): Number of correct MTP predictions.
total (Optional[torch.Tensor]): Total number of MTP predictions.
reduce_group (torch.distributed.ProcessGroup): The group for sum-reducing losses.
avg_group (torch.distributed.ProcessGroup): The group for sum-reducing before averaging.
reduce_group (torch.distributed.ProcessGroup): Group for summing losses.
avg_group (torch.distributed.ProcessGroup): Group for averaging losses.
"""
if layer_number is None:
return

tracker = MTPLossLoggingHelper.tracker
if "loss_sums" not in tracker:
tracker["loss_sums"] = torch.zeros(num_layers, device=torch.cuda.current_device())
tracker["num_tokens"] = torch.zeros(num_layers, device=torch.cuda.current_device())
loss_sum = (loss_sum * (num_tokens > 0).to(loss_sum.dtype)) / num_tokens.clamp(min=1)
tracker["loss_sums"][layer_number] += loss_sum.detach()
tracker["num_tokens"][layer_number] += num_tokens.detach()
if correct is not None and total is not None:
if "correct_values" not in tracker:
tracker["correct_values"] = torch.zeros(
Expand Down Expand Up @@ -485,7 +485,6 @@ def clean_loss_in_tracker():
tracker = MTPLossLoggingHelper.tracker
if "loss_sums" in tracker:
tracker["loss_sums"].zero_()
tracker["num_tokens"].zero_()
if "values" in tracker:
tracker["values"].zero_()
if "correct_values" in tracker:
Expand All @@ -499,21 +498,21 @@ def clean_loss_in_tracker():
def reduce_loss_in_tracker():
"""Collect and reduce the mtp losses across ranks.

Packs loss sums and token counts into a single tensor for one
all-reduce, then computes per-token loss. This produces correct
weighted-average results even when ranks hold different numbers
of tokens (e.g. Dynamic CP with variable CP sizes).
Each element is already a sum of normalized microbatch losses. Sum
reductions preserve additive groups, while the DP+CP average keeps the
legacy logging contract.
"""
tracker = MTPLossLoggingHelper.tracker
if "loss_sums" not in tracker:
return
packed = torch.cat([tracker["loss_sums"], tracker["num_tokens"]])
for group_key in ('reduce_group', 'avg_group'):
group = tracker.get(group_key)
if group is not None:
torch.distributed.all_reduce(packed, group=group)
loss_sums, num_tokens = packed.chunk(2)
tracker["values"] = loss_sums / num_tokens.clamp(min=1)
values = tracker["loss_sums"]
if tracker.get('reduce_group') is not None:
torch.distributed.all_reduce(values, group=tracker['reduce_group'])
if tracker.get('avg_group') is not None:
torch.distributed.all_reduce(
values, group=tracker['avg_group'], op=torch.distributed.ReduceOp.AVG
)
tracker["values"] = values

@staticmethod
def track_mtp_metrics(loss_scale, iteration, writer, wandb_writer=None, total_loss_dict=None):
Expand Down
8 changes: 4 additions & 4 deletions megatron/training/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -3130,10 +3130,10 @@ def training_log(

# Log MTP metrics.
if args.mtp_num_layers is not None:
# MTP tracker stores raw loss sums and token counts, so after reduction
# tracker["values"] already equals the per-token loss (loss_sum / num_tokens)
# aggregated across all ranks and microbatches. No further scaling needed.
mtp_loss_scale = 1.0
# The tracker stores a sum of normalized microbatch losses.
# Sequence-packing schedulers may change the number of microbatches for
# this step, so use the scheduled count passed to training_log.
mtp_loss_scale = 1 / (num_microbatches or get_num_microbatches())
MTPLossLoggingHelper.track_mtp_metrics(
mtp_loss_scale, iteration, writer, wandb_writer, total_loss_dict
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@
"step_interval": 1,
"values": {
"1": 10.91979,
"2": 10.92265,
"2": 10.92264,
"3": 10.92219,
"4": 10.91957,
"5": 10.90846,
Expand All @@ -442,7 +442,7 @@
"8": 10.92446,
"9": 10.91998,
"10": 10.92341,
"11": 10.91589,
"11": 10.91588,
"12": 10.90792,
"13": 10.92213,
"14": 10.91569,
Expand All @@ -455,8 +455,8 @@
"21": 10.90603,
"22": 10.90322,
"23": 10.90169,
"24": 10.89089,
"25": 10.88251,
"24": 10.8909,
"25": 10.8825,
"26": 10.89359,
"27": 10.8887,
"28": 10.87619,
Expand All @@ -477,7 +477,7 @@
"43": 10.84328,
"44": 10.82898,
"45": 10.84347,
"46": 10.83297,
"46": 10.83298,
"47": 10.83911,
"48": 10.82542,
"49": 10.83132,
Expand All @@ -494,7 +494,7 @@
"60": 10.77895,
"61": 10.77556,
"62": 10.76277,
"63": 10.77595,
"63": 10.77594,
"64": 10.76136,
"65": 10.7585,
"66": 10.75798,
Expand All @@ -514,17 +514,17 @@
"80": 10.67858,
"81": 10.67147,
"82": 10.65165,
"83": 10.63056,
"83": 10.63057,
"84": 10.61714,
"85": 10.60392,
"86": 10.63183,
"87": 10.62791,
"88": 10.62832,
"88": 10.62833,
"89": 10.59789,
"90": 10.59506,
"91": 10.60606,
"92": 10.58205,
"93": 10.55313,
"93": 10.55314,
"94": 10.58516,
"95": 10.57313,
"96": 10.56963,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@
"60": 10.77895,
"61": 10.77556,
"62": 10.76277,
"63": 10.77595,
"63": 10.77594,
"64": 10.76136,
"65": 10.7585,
"66": 10.75798,
Expand All @@ -514,17 +514,17 @@
"80": 10.67858,
"81": 10.67147,
"82": 10.65165,
"83": 10.63056,
"83": 10.63057,
"84": 10.61714,
"85": 10.60392,
"86": 10.63183,
"87": 10.62791,
"88": 10.62832,
"88": 10.62833,
"89": 10.59789,
"90": 10.59506,
"91": 10.60606,
"92": 10.58205,
"93": 10.55313,
"93": 10.55314,
"94": 10.58516,
"95": 10.57313,
"96": 10.56963,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,25 +437,25 @@
"3": 10.93384,
"4": 10.92739,
"5": 10.90724,
"6": 10.91816,
"6": 10.91817,
"7": 10.92486,
"8": 10.92528,
"9": 10.93457,
"10": 10.9265,
"11": 10.91896,
"12": 10.91863,
"13": 10.92814,
"14": 10.91204,
"14": 10.91203,
"15": 10.92041,
"16": 10.92467,
"17": 10.92235,
"18": 10.9072,
"18": 10.90719,
"19": 10.91438,
"20": 10.90506,
"21": 10.91161,
"22": 10.89778,
"23": 10.90483,
"24": 10.88965,
"24": 10.88964,
"25": 10.89765,
"26": 10.88453,
"27": 10.89849,
Expand Down Expand Up @@ -491,7 +491,7 @@
"57": 10.78961,
"58": 10.79824,
"59": 10.78095,
"60": 10.77504,
"60": 10.77503,
"61": 10.77627,
"62": 10.7614,
"63": 10.78392,
Expand All @@ -511,7 +511,7 @@
"77": 10.69055,
"78": 10.68188,
"79": 10.66968,
"80": 10.67687,
"80": 10.67688,
"81": 10.66904,
"82": 10.65016,
"83": 10.6267,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@
"57": 10.78961,
"58": 10.79824,
"59": 10.78095,
"60": 10.77504,
"60": 10.77503,
"61": 10.77627,
"62": 10.7614,
"63": 10.78392,
Expand All @@ -511,7 +511,7 @@
"77": 10.69055,
"78": 10.68188,
"79": 10.66968,
"80": 10.67687,
"80": 10.67688,
"81": 10.66904,
"82": 10.65016,
"83": 10.6267,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@
"30": 10.93961,
"31": 10.91806,
"32": 10.92899,
"33": 10.92158,
"33": 10.92159,
"34": 10.92688,
"35": 10.91862,
"36": 10.917,
Expand All @@ -475,13 +475,13 @@
"41": 10.90546,
"42": 10.88722,
"43": 10.89763,
"44": 10.87485,
"44": 10.87484,
"45": 10.88603,
"46": 10.87926,
"47": 10.87569,
"48": 10.86052,
"49": 10.86102,
"50": 10.84774,
"50": 10.84773,
"51": 10.86037,
"52": 10.84549,
"53": 10.85137,
Expand All @@ -497,7 +497,7 @@
"63": 10.80803,
"64": 10.80094,
"65": 10.78782,
"66": 10.78838,
"66": 10.78839,
"67": 10.78222,
"68": 10.76003,
"69": 10.78043,
Expand Down
Loading
Loading