Skip to content

fix(dataset): size default num_workers from available CPUs (cgroup-aware), not os.cpu_count() - #114

Merged
meilame-tayebjee merged 1 commit into
mainfrom
fix/num-workers-cpu-quota
Nov 24, 2026
Merged

meilame-tayebjee merged 1 commit into
mainfrom
fix/num-workers-cpu-quota

Conversation

@meilame-tayebjee

Copy link
Copy Markdown
Member

Problem

create_dataloader() defaulted to num_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.py triggered this, because it relied on the default).

The TrainingConfig default was also a hardcoded 12. With persistent workers on both the train and val dataloaders, that is 24 processes no matter how many CPUs are available.

Fix

  • New torchTextClassifiers/utilities/resources.py:
    • available_cpu_count() returns the minimum of os.cpu_count(), os.sched_getaffinity(0) and the cgroup CPU quota (v2 cpu.max, or v1 cpu.cfs_quota_us / cpu.cfs_period_us). It is always at least 1.
    • default_num_workers() returns available_cpu_count() - 1, capped at 8.
  • TextClassificationDataset.create_dataloader(num_workers=None) and TrainingConfig.num_workers = None both resolve the value when they are called. Before, the dataset default was evaluated once, at import. Explicit values are unchanged.
  • tests/test_pipeline.py now uses num_workers=0.
  • Updated the TrainingConfig.num_workers docs.

On the 15-CPU-quota / 128-core pod: before, 127 workers per dataloader; now, 8.

Behaviour change

Users who relied on the implicit TrainingConfig default of 12 now get min(available CPUs - 1, 8) instead. Passing num_workers explicitly 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 that create_dataloader and TrainingConfig use the new default. These tests never start worker processes.
  • Full suite: 50 passed. The only failures are in tests that need the optional huggingface extra, which is not installed in my environment (test_tokenizer.py, plus the WordPiece/HF/label-attention pipeline tests: NameError on the tokenizer import). The NGram pipeline test, which trains a model and runs Captum, passes.

🤖 Generated with Claude Code

…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>
@meilame-tayebjee
meilame-tayebjee merged commit bc8e1a5 into main Nov 24, 2026
5 checks passed
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.

1 participant