fix(megatron): cast get_cp_local_num_tokens to torch.int for Megatron int32 token accumulator - #290
Open
ZiyiTsang wants to merge 1 commit into
Open
fix(megatron): cast get_cp_local_num_tokens to torch.int for Megatron int32 token accumulator#290ZiyiTsang wants to merge 1 commit into
ZiyiTsang wants to merge 1 commit into
Conversation
Both branches (cp_size == 1 and cp_size > 1) returned float32 (loss_mask.sum()), crashing every --calculate-per-token-loss training run at step 0: Megatron schedules.py accumulates `total_num_tokens += num_tokens` on an int tensor, so a float input raises "Result type Float can't be cast to the desired output type Int". Match the accumulator dtype, and compare the normalizer dtype-safely in the rloo dispatch test while pinning the int contract.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Why
Megatron pipeline schedules accumulate the per-token loss normalizer on an int tensor:
megatron/core/pipeline_parallel/schedules.py—total_num_tokens = torch.zeros([], dtype=torch.int)(lines 256/652/1144/2363)relax/backends/megatron/streaming_schedules.py:106,295uses the same int accumulatorget_cp_local_num_tokensreturned float32 (aggregatedloss_mask.sum()), so every training run with--calculate-per-token-losscrashes at step 0 on the firsttotal_num_tokens += num_tokens:17/30 example launchers enable
--calculate-per-token-loss(all six sdpo launchers, most OPD/agentic recipes, rloo, mini_swe, nemo_gym).How
Cast the three return paths of
get_cp_local_num_tokenstotorch.intso the value handed toforward_backward_*matches the accumulator dtype. Non-per-token-loss paths are unaffected (their normalizer istorch.tensor(1)), and mixing the int32 scalar intolog_valuesstill produces identical float32 metrics.Testing
pytest tests/backends/megatron/test_rloo_policy_loss_dispatch.py tests/backends/megatron/test_rloo_cp_reduction.pypasses on CPU.test_rloo_policy_loss_dispatchnow compares the normalizer dtype-safely and pinsnormalizer.dtype == torch.intas the regression contract (the dtype-stricttorch.allclosewould otherwise fail after this fix).Relates to #285 (touches the same function cp_size == 1 branch).