Skip to content

Add sync_dist_op to LightningModule logging - #21892

Open
zupengwang wants to merge 1 commit into
Lightning-AI:masterfrom
zupengwang:feature/17831-sync-dist-op
Open

Add sync_dist_op to LightningModule logging#21892
zupengwang wants to merge 1 commit into
Lightning-AI:masterfrom
zupengwang:feature/17831-sync-dist-op

Conversation

@zupengwang

Copy link
Copy Markdown

What does this PR do?

Adds a sync_dist_op argument to LightningModule.log and LightningModule.log_dict so the reduction across distributed ranks can be configured independently from the reduction across epoch steps.

When sync_dist_op is omitted, the distributed operation still falls back to reduce_fx, preserving existing behavior. The argument is appended to the public signatures to avoid shifting existing positional arguments. The API docstrings and PyTorch changelog are updated.

The regression test covers CPU, single-GPU, two-process CPU DDP, and two-GPU DDP. It logs rank-dependent values with sync_dist_op="sum" and reduce_fx="max", verifying the two reduction stages produce the expected metric.

Validation:

  • python -m pytest -q tests/tests_pytorch/trainer/logging_/test_train_loop_logging.py::test_logging_sync_dist_true (4 passed)
  • CUDA_VISIBLE_DEVICES= python -m pytest -q tests/tests_pytorch/core/test_results.py tests/tests_pytorch/trainer/logging_/test_train_loop_logging.py (31 passed, 4 skipped)
  • PL_RUN_STANDALONE_TESTS=1 python -m pytest -q tests/tests_pytorch/strategies/test_ddp_integration.py::test_ddp_gradients_synced[...] (all four automatic/manual and static/non-static variants passed when run individually)
  • ruff check and ruff format --check on the changed Python files
  • mypy on the changed source files
  • uv build --offline --wheel for the pytorch package

AI assistance disclosure: AI tools assisted with implementation and test preparation. The contributor reviewed the complete diff and validated the change on CPU and GPU.

Fixes #17831

No breaking changes are intended. The default distributed reduction remains unchanged.

Before submitting
  • Was this discussed/agreed via a GitHub issue? (not for typos and docs) Yes, in self.log: strategy.reduce vs reduce_fx #17831.
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary) Yes, the public API docstrings are updated.
  • Did you write any new necessary tests? (not for typos and docs) Yes.
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request? None.
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or minor internal changes/refactors) Yes.

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the review guidelines. In short, see the following bullet-list:

Reviewer checklist
  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

Signed-off-by: Zupeng Wang <71580390+zupengwang@users.noreply.github.com>
@zupengwang
zupengwang marked this pull request as ready for review August 9, 2026 13:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

self.log: strategy.reduce vs reduce_fx

1 participant