Repository navigation
Keep global valid token counts on device - #4099
Merged
Merged
Conversation
xmfan
marked this pull request as ready for review
August 10, 2026 18:21
xmfan
requested review from
IvanKobzarev,
SherlockNoMad,
aditvenk,
fegin,
felipemello1,
sanketpurandare,
tianyu-l,
wconstab and
wwwjn
as code owners
August 10, 2026 18:21
tianyu-l
reviewed
Aug 10, 2026
| if global_valid_tokens is not None: | ||
| # TODO(pianpwk): Teach spmd_types that P / scalar preserves P. | ||
| with spmd.no_typecheck(): | ||
| loss = loss / global_valid_tokens |
Contributor
There was a problem hiding this comment.
was this causing D2H sync before this PR?
Contributor
Author
There was a problem hiding this comment.
no, both were scalars, d2h sync was happening earlier when global_valid_tokens is updated with some .item()
Represent global valid token counts consistently as device scalar tensors across training, validation, pipeline parallelism, and graph tracing. Add a tensor-returning distributed sum helper so loss normalization no longer requires an eager CPU sync. Test Plan: - pytest -q tests/unit_tests/test_distributed_utils.py tests/unit_tests/test_loss.py torchtitan/experiments/graph_trainer/tests/test_chunked_loss.py - pyrefly check tests/unit_tests/test_distributed_utils.py torchtitan/distributed/utils.py torchtitan/trainer.py torchtitan/components/loss.py torchtitan/components/validate.py torchtitan/models/flux/trainer.py - 1x H100, Llama 3 debug model, seed 42, 10 deterministic steps: parent and tensor-scalar runs matched raw TensorBoard loss and grad norm bitwise at all 10 steps. Both runs completed and flushed metrics before the existing native process-teardown abort. stack-info: PR: #4099, branch: xmfan/stack/40
tianyu-l
approved these changes
Aug 10, 2026
| ) | ||
| else: | ||
| global_valid_tokens = float(local_valid_tokens.item()) | ||
| global_valid_tokens = local_valid_tokens |
Contributor
There was a problem hiding this comment.
do we not need move this to GPU?
Contributor
Author
There was a problem hiding this comment.
Unlike main trainer, flux always keeps local_valid_tokens as a device tensor. it's used for loss accumulation and logging. The loss will eventually need to be on cpu for logging, but it could be done async, so I'm leaning towards we should have these valid token counts on device
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.
Stacked PRs:
Keep global valid token counts on device
Represent global valid token counts consistently as device scalar tensors across training, validation, pipeline parallelism, and graph tracing. Logging may still add back the sync, but intermediates no longer sync. This is needed for cudagraphs as the value of global valid token counts changes each iteration and needs to be part of the graph.
Test Plan: