Skip to content

Raise clear errors when validation yields zero batches or zero valid tokens - #4301

Merged
tianyu-l merged 1 commit into
pytorch:mainfrom
shoemoney:fix/validator-zero-batches-error
Sep 19, 2026
Merged

tianyu-l merged 1 commit into
pytorch:mainfrom
shoemoney:fix/validator-zero-batches-error

Conversation

@shoemoney

Copy link
Copy Markdown
Contributor

With the default validation dataloader (concat-then-split packing, validate.py Config), a per-rank validation split holding fewer than num_tokens_per_batch tokens yields zero packed rows, because _packing_output_is_full drops partially filled rows. The validate loop then breaks on the first StopIteration with accumulated_loss still None, and the bare assert accumulated_loss is not None kills the whole training run with a message-less AssertionError at the first validation step. Without the assert, global_loss_sum / num_global_valid_tokens raises ZeroDivisionError instead.

This PR replaces the assert with a ValueError naming the cause (validation dataset supplied fewer than num_tokens_per_batch tokens on this rank; concat-then-split packing drops partial batches) and the remedy (decrease training.num_tokens_per_microbatch_per_dp_rank or use a larger validation set), and guards the zero-valid-token division the same way. Same fail-fast-with-a-clear-message class as the merged #4287 and #4291.

No numerical path changes: this is validation-layer error handling only, the loss computation is untouched, so no numerics proof applies. No new CLI flags.

Tests: two unit tests in tests/unit_tests/test_validate.py construct both zero-batch and zero-valid-token conditions using the file's existing validator harness. On main they fail with the bare AssertionError and ZeroDivisionError; with this change they assert the ValueError messages. pre-commit passes on both touched files.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 24, 2026
@shoemoney
shoemoney force-pushed the fix/validator-zero-batches-error branch from 405a53c to 59be178 Compare August 25, 2026 16:43
…ches

With the default concat-then-split packing, a per-rank validation split
holding fewer than num_tokens_per_batch tokens produces zero packed rows,
because partially filled rows are dropped. The validate loop then breaks on
the first StopIteration with accumulated_loss still None, and the bare
assert kills the run with a message-less AssertionError. If the assert were
removed, the token-count division below would raise ZeroDivisionError
instead.

Raise ValueError with the cause and remedy for both conditions: zero
validation batches on a rank, and zero valid (unmasked) tokens overall.
No numerical path changes; the loss computation is untouched.
@shoemoney
shoemoney force-pushed the fix/validator-zero-batches-error branch from 59be178 to 6c8a6f8 Compare September 18, 2026 13:56
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Sep 18, 2026
@pytorch-bot

pytorch-bot Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@tianyu-l
tianyu-l merged commit 6c2dadb into pytorch:main Sep 19, 2026
14 checks passed
tianyu-l pushed a commit that referenced this pull request Sep 22, 2026
)

## Summary

`validation.steps=-1` consumes a finite dataset once. Ranks then stop
independently, so a DP degree > 1 can hang on validation collectives
(`dist_sum` of token counts / loss). GrainDataLoader already rejects
`repeat=False` with `dp_world_size > 1`; that error names dataloader
`repeat`, not `validation.steps`.

This PR:

1. Replaces the `assert` on `validation.steps` with a `ValueError`,
matching `validation.freq`. `assert` disappears under `python -O`.
2. Raises the same constraint in `Validator.__init__` once the real DP
degree is known, naming `validation.steps`. Config `__post_init__`
cannot do this because `data_parallel_shard_degree` defaults to leftover
`-1`.

The existing dataloader `repeat` guard is left in place. `#4301`
zero-batch / zero-token errors are unchanged. The Flux validator warning
is unchanged.

## Test plan

- [ ] `pytest tests/unit_tests/cpu/test_validate.py
tests/unit_tests/cpu/test_validator_frequency.py` (13 passed locally,
including 8 subtests)
- [ ] Lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests 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