Repository navigation
fix(dataset): size default num_workers from available CPUs (cgroup-aware), not os.cpu_count() - #114
Merged
Merged
Conversation
…u_count() os.cpu_count() reports every CPU of the host, ignoring cgroup quotas and CPU affinity. In containers (Kubernetes/Onyxia, Docker, SLURM) the old default `os.cpu_count() - 1` could spawn >100 DataLoader worker processes and exhaust the container memory. - add utilities/resources.py: available_cpu_count() (min of host count, sched_getaffinity, cgroup v2 cpu.max / v1 cfs quota) and default_num_workers() = available - 1, capped at 8 - create_dataloader() and TrainingConfig now default to num_workers=None, resolved at call time (previously evaluated at import / hardcoded 12) - test_pipeline uses num_workers=0 - unit tests fake cgroup files; they never start worker processes Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Problem
create_dataloader()defaulted tonum_workers=os.cpu_count() - 1.os.cpu_count()returns all the CPUs of the host and ignores container limits. On an Onyxia/Kubernetes pod with a 15-CPU quota running on a 128-core node, that meant 127 DataLoader worker processes, each one a full Python process loading torch. This exhausted the pod's memory and crashed it (tests/test_pipeline.pytriggered this, because it relied on the default).The
TrainingConfigdefault was also a hardcoded12. With persistent workers on both the train and val dataloaders, that is 24 processes no matter how many CPUs are available.Fix
torchTextClassifiers/utilities/resources.py:available_cpu_count()returns the minimum ofos.cpu_count(),os.sched_getaffinity(0)and the cgroup CPU quota (v2cpu.max, or v1cpu.cfs_quota_us / cpu.cfs_period_us). It is always at least 1.default_num_workers()returnsavailable_cpu_count() - 1, capped at 8.TextClassificationDataset.create_dataloader(num_workers=None)andTrainingConfig.num_workers = Noneboth resolve the value when they are called. Before, the dataset default was evaluated once, at import. Explicit values are unchanged.tests/test_pipeline.pynow usesnum_workers=0.TrainingConfig.num_workersdocs.On the 15-CPU-quota / 128-core pod: before, 127 workers per dataloader; now, 8.
Behaviour change
Users who relied on the implicit
TrainingConfigdefault of 12 now getmin(available CPUs - 1, 8)instead. Passingnum_workersexplicitly works exactly as before.Tests
tests/test_resources.py(16 tests): fake cgroup v1/v2 files (quota, fractional quota, unlimited, missing), affinity, fallback, and the default sizing. It also checks thatcreate_dataloaderandTrainingConfiguse the new default. These tests never start worker processes.huggingfaceextra, which is not installed in my environment (test_tokenizer.py, plus the WordPiece/HF/label-attention pipeline tests:NameErroron the tokenizer import). The NGram pipeline test, which trains a model and runs Captum, passes.🤖 Generated with Claude Code