Repository navigation
Conversation
When the training rows left a remainder of 1 for batch_size, the last training batch had one row and every BatchNorm1d layer failed in training mode. Unless dataloader_kwargs sets drop_last, drop that batch from the training loader (a different row with shuffling each epoch); document it. Fixes #577
Lightning's distributed sampler gives each process ceil(n / world_size) rows, so the global row count could drop a process's only batch (no training at all) or miss a per-process one-row tail. Refs #577
This was referenced Oct 21, 2026
This branch has not been deployed
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.
Fixes #577
Problem
The training loader keeps a trailing partial batch. When the number of training rows leaves a remainder of 1 for
batch_size, that batch has a single row, and everynn.BatchNorm1dlayer fails in training mode withValueError: Expected more than 1 value per channel when training. With the defaults that is about one dataset size in 128 (e.g. 162 rows → 129 training rows), for models that usetorch.nn.BatchNorm1d, e.g.MLPConfig(batch_norm=True)orhead_use_batch_norm=True.Fix
TabularDataModule.train_dataloader()(deeptab/data/datamodule.py) setsdrop_last=Truefor the training loader only when:drop_lastindataloader_kwargs, andUnder distributed training the check uses the rows each process receives: Lightning's distributed sampler gives every process
ceil(n / world_size)rows, and the global count could otherwise drop a process's only batch (so it trains nothing) or miss a per-process one-row tail.With shuffling, a different row is left out in each epoch. An explicit
dataloader_kwargs={"drop_last": ...}always wins,dataloader_kwargsitself is not modified, and validation, test and prediction loaders are unchanged. Invalid batch sizes (e.g.0) are not inspected here, soDataLoaderstill rejects them with its ownValueError(covered bytests/lifecycle/test_continuation.py).Contrastive pretraining trains on the same loader, so this also removes the training-side one-row batch reported in #529. #530 handles batches that are too small inside the pretraining steps, which also covers validation batches; the two changes are independent.
Tests
tests/data/test_datamodule.py::test_single_row_trailing_training_batch_is_dropped[...]covers a one-row tail (dropped), a two-row tail and no tail (kept), a single-row dataset (kept), and an explicitdrop_last=False(kept).tests/data/test_datamodule.py::test_single_row_tail_check_uses_rows_per_process[...]stubs a two-process trainer: 66 training rows (33 per process, one-row tail) are dropped, 33 rows (17 per process) are kept.tests/integration/test_partial_training_batches.py(new) checks thatMLPRegressor(MLPConfig(batch_norm=True))and a Mambular head withhead_use_batch_norm=Truefit and predict with 129 training rows.The one-row-tail and both estimator cases fail on
mainand pass with this change. I also ran a real two-process CPU DDP fit: with 162 rows each process trains one 65-row batch, as onmain, and with 323 rows and BatchNorm each process trains one 128-row batch, wheremaincrashes.Docs
docs/core_concepts/training_and_evaluation.mddescribes the automatic drop and how to keep the batch.TabularDataModuleandtrain_dataloaderdocstrings mention it.Checks
just test: 3245 passed, 46 skipped, 28 xfailed; coverage 91.32%just docs: build succeeded (warnings treated as errors)just check: all hooks passed (ruff, prettier, pyright)