Skip to content

Keep global valid token counts on device - #4099

Merged
xmfan merged 1 commit into
mainfrom
xmfan/stack/40
Aug 12, 2026
Merged

xmfan merged 1 commit into
mainfrom
xmfan/stack/40

Conversation

@xmfan

@xmfan xmfan commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • 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.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 10, 2026
@xmfan xmfan changed the title Keep global valid token counts on device Keep global valid token counts as device tensors Aug 10, 2026
@xmfan xmfan changed the title Keep global valid token counts as device tensors Keep global valid token counts on device Aug 10, 2026
@xmfan
xmfan marked this pull request as ready for review August 10, 2026 18:21
Comment thread torchtitan/components/validate.py Resolved
Comment thread torchtitan/trainer.py Resolved
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was this causing D2H sync before this PR?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
)
else:
global_valid_tokens = float(local_valid_tokens.item())
global_valid_tokens = local_valid_tokens

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we not need move this to GPU?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@xmfan
xmfan merged commit 1f3ae09 into main Aug 12, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants