Skip to content

Log the per-step shutdown vote's time as distributed/shutdown_vote_ms - #789

Open
Naeemkh wants to merge 5 commits into
mainfrom
feat/shutdown-vote-latency-metric
Open

Naeemkh wants to merge 5 commits into
mainfrom
feat/shutdown-vote-latency-metric

Conversation

@Naeemkh

@Naeemkh Naeemkh commented Dec 6, 2026

Copy link
Copy Markdown
Member

Summary

Part of #564, which asks for a measurement of the per-step shutdown vote on 8 or more GPU nodes before deciding whether to fold it into the NaN check's all-reduce.

  • Why. At every step, _shutdown_if_requested casts the shutdown vote: one blocking gloo MAX all-reduce over the control group, which carries the shutdown flag and, since Publish an async checkpoint once every rank's write has ended, and fit the shutdown timer to the signal lead #723, CheckpointManager.flushing. It runs after tracker.end_step, so step_time, tok/s and MFU leave it out, and no metric showed what it costs.
  • What. The loop times that all-reduce alone with time.perf_counter, with no synchronization added; the flags are read before the clock starts. On each step whose metrics are logged, it logs the mean over the votes since the last logged step as distributed/shutdown_vote_ms, through tracker.log_eval, so it reaches stdout, W&B, TensorBoard and MLflow like the loop's other metrics. The vote of a step skipped for a NaN loss counts toward the next logged mean, a shutdown at a logged step logs its vote before the emergency checkpoint, and each run_training_loop call starts its own interval. The loop keeps a running sum and count of the votes' times, so its memory stays the same whatever metrics.log_interval is.
  • What a user will notice. A run with several ranks prints one more line on each logged step, after that step's vote, such as [step 10] distributed/shutdown_vote_ms=0.3540, and its backends get the new series. A single-process run is unchanged. Nothing else changes: the vote, its flags, its place in the step and every stop decision stay as they are, and no collective is added.
  • Docs. docs/metrics-and-profiling/index.md and metrics-tracker.md list the key and say what it measures, docs/training/training-loop.md names it beside the checkpoint times, and docs/resilience/slurm-preemption.md says what it is for: deciding Fold the per-step shutdown vote into the NaN check's all-reduce #564 from a multi-node run, where folding would move both of the vote's flags into the NaN check and save only the all-reduce's own latency. codebase-map.json lists the new test classes.

Defaults taken, each yours to reverse

  1. Its own log_eval line after the vote, not a field of the step line. The vote runs after end_step, so a value in the step line would cover the previous interval's votes and be missing at step 1.
  2. Nothing is logged without a control group. One process has no process group, so its vote reduces nothing, and logging about 0 ms would add a line to every single-GPU run.
  3. Rank 0's own time, waits included. Stdout and the backends show rank 0's value, which includes any wait for a rank that reaches the vote later. A maximum over the ranks would need another collective, the very cost being measured. A large value can therefore mean that the ranks reach the vote at different times rather than a slow all-reduce; folding the vote into the NaN check would move such a wait, not remove it.
  4. The mean over the interval. One number per logged step, as for the other series. The loop keeps only a running sum and count, so a maximum would be one more running value, while a percentile would need each vote's time kept.

Testing

  • uv run ruff check . passes
  • uv run ruff format --check . passes
  • uv run pyright kempnerforge/ passes (0 errors)
  • uv run pytest tests/unit/ -v --timeout=60 passes on the first two commits (c1e6f222), apart from 39 failures the CPU test machine causes (6636 passed, 14 skipped): 38 tests in test_vlm_dataset.py and test_video_dataset.py need gpt2 from a Hugging Face cache this offline machine no longer has, and one test_hooks.py case hit Errno 39 while removing its temporary directory on the shared filesystem; test_hooks.py passes when run alone (43 passed). The three later commits were run on the two test files they touch, as listed below.
  • If distributed code changed: uv run torchrun --nproc_per_node=4 -m pytest tests/distributed/ -v (not run: it needs 4 GPUs, and no module under kempnerforge/distributed/ changed; the vote's real gloo path is covered by the two-rank unit test below)
  • If training loop / parallelism / optimizers changed: uv run pytest tests/e2e/ --e2e -v (not run: it needs GPUs; the change only adds a timer and a log call after the existing vote)

New tests:

  • tests/unit/test_train_entry.py::TestShutdownVoteTime runs the loop on a control group with a fake clock, where the k-th vote takes k ms and the shutdown handler's reinstall and check before each vote take 1 s each, which the vote's time must leave out. With a metrics interval of 3 and 7 steps, the backend gets the key at steps 1, 3 and 6 only, the steps that log train/loss, with 1.0, 2.5 and 5.0 ms. A shutdown asked at step 3 logs 2.5 ms there. A NaN-skipped step 2 logs nothing and its vote counts in step 3's mean. A second loop on the same session starts its own interval. A run without a control group logs no vote time.
  • tests/unit/test_shutdown_agreement.py::TestShutdownVoteTime runs the real loop on two gloo ranks, on the control group the loop makes: with a metrics interval of 3, both ranks log a positive mean at steps 1, 3 and 6.

Failing first, against main 3899909 with only the two test files changed:

FAILED test_train_entry.py::TestShutdownVoteTime::test_each_logged_step_logs_the_mean_of_its_votes
  assert [] == [(1, 1.0 ± 1.....0 ± 5.0e-06)]
FAILED test_train_entry.py::TestShutdownVoteTime::test_a_skipped_steps_vote_counts_toward_the_next_logged_mean
  assert [] == [(1, 1.0 ± 1.....5 ± 2.5e-06)]
FAILED test_train_entry.py::TestShutdownVoteTime::test_a_new_loop_starts_its_own_interval
  assert [] == [(1, 1.0 ± 1.....0 ± 3.0e-06)]
FAILED test_train_entry.py::TestShutdownVoteTime::test_a_stop_at_a_logged_step_logs_its_vote
  assert [] == [(1, 1.0 ± 1.....5 ± 2.5e-06)]
FAILED test_shutdown_agreement.py::TestShutdownVoteTime::test_every_rank_times_the_vote_on_the_loops_control_group
  {0: 'step=6 saves=[6] nans=0 vote_ms_at=[] positive=True'} != {0: 'step=6 saves=[6] nans=0 vote_ms_at=[1, 3, 6] positive=True'}
  {1: 'step=6 saves=[6] nans=0 vote_ms_at=[] positive=True'} != {1: 'step=6 saves=[6] nans=0 vote_ms_at=[1, 3, 6] positive=True'}
PASSED test_train_entry.py::TestShutdownVoteTime::test_a_run_without_a_control_group_logs_no_vote_time

The last test keeps main's single-process behavior. Each of these changes to the code fails at least one of the tests: dropping the per-loop reset, dropping the control-group check, logging on every step, starting the clock before the handler's reinstall and check, or recording the vote only when no rank asked to stop. The last two passed the tests as they were before the slow handler and the stop-step test.

Commits after review, and their checks:

  • 0857152c Test that the vote time leaves out the handler and covers a stop step: test_train_entry.py and test_shutdown_agreement.py at this commit, 431 passed.
  • 9ce25bef Keep a running sum and count of the vote times, not a list: the same two files at the head, 431 passed.
  • db64875f Say that folding moves both of the vote's flags and saves only its latency: Sphinx -W --keep-going builds clean at the head.
  • At the head, ruff check and format are clean, pyright reports 0 errors, and codebase-map.json parses with only main's known count mismatches.

Also checked: the new doc anchors resolve in the built HTML, and the branch merges cleanly with main and with every open PR (#648, #709, #770, #775, #776, #777, #781, #782, #783, #784, #787, #788), and the new tests pass on its merges with the three that share code or test files (#775, #776, #782).

Contribution terms

Part of #564. What remains: read distributed/shutdown_vote_ms against train/step_time_sec from a run on 8 or more GPU nodes, then fold the vote into NaNDetector.check_loss's all-reduce (or cast it with async_op=True) only if it costs more than about 1% of a step, and otherwise close #564.

@Naeemkh Naeemkh added the core Affecting core label Dec 6, 2026
@codecov

codecov Bot commented Dec 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
kempnerforge/training/loop.py 99.68% <100.00%> (+<0.01%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@amazloumi
amazloumi self-requested a review December 6, 2026 01:18

@amazloumi amazloumi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approve. The vote timing adds no collective and leaves every stop decision unchanged: in a 4-rank run with a shutdown signal on one rank, head and main stop and save at the same step, the logged mean counts NaN-skipped steps correctly, and on GPU the new metric shows sub-millisecond values with throughput unchanged. The new tests catch every mutation tried, and the gate is green.

Two points that should not hold up the merge:

  1. Merging this will close #564, although #564 still needs the multi-node measurement this PR only makes possible: the body's last paragraph says "otherwise close #564", which GitHub reads as a closing keyword. Rewording that line keeps #564 open.

  2. The paragraph in docs/resilience/slurm-preemption.md about folding the vote into the NaN check describes a decision still open in #564, so it goes stale once that is settled; the docs could keep only what the metric measures and leave the decision to the issue.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Affecting core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fold the per-step shutdown vote into the NaN check's all-reduce

2 participants