Skip to content

Set glibc malloc thresholds in the training launch scripts - #794

Open
timothyngo wants to merge 1 commit into
mainfrom
fix/malloc-env-for-dataloader-workers
Open

timothyngo wants to merge 1 commit into
mainfrom
fix/malloc-env-for-dataloader-workers

Conversation

@timothyngo

Copy link
Copy Markdown
Collaborator

Summary

The training launch scripts under scripts/slurm/ (multinode.sh, singlenode.sh, 7b_requeue.sh, 13b_validation.sh, interactive.sh, _run_training.sh) now export two glibc malloc settings next to their NCCL/Gloo env:

export MALLOC_MMAP_THRESHOLD_=1048576
export MALLOC_TRIM_THRESHOLD_=4194304

so DataLoader workers stop accumulating host memory. docs/reference/environment-variables.md gets a short section on them, and the image-understanding README says to keep them in the stage-1/2 launch environment.

Why

In the image-understanding stage-2 run (3 × 3 tiling at 512 px), tile_image converts the whole 1536² canvas to float32 before cropping (kempnerforge/data/vlm_dataset.py, pil_to_tensor), so each image makes ~28 MB temporaries. glibc's dynamic mmap threshold rises to 32 MB after the first large free, so those blocks come from the worker heap, fragment, and are never returned: each DataLoader worker grew ~7.5 MB per document without bound (48 workers × ~30 GB per node), and the node ran out of host memory after ~10 h on a 1400 GB node.

A 50-minute CPU probe of the real stage-2 loader (12 workers, prefetch 16):

Worker heap Job MaxRSS
Baseline 2.6 → 18.2 GB, linear 241 GB
With the two variables flat at ~0.5 GB 16 GB

Throughput was within noise. Disabling THP or setting NUMPY_MADVISE_HUGEPAGE=0 made no difference.

glibc reads these variables at process start, so they have to be in the launch environment (workers are forked and inherit them); setting them from Python would be too late. No change to numerics or data order.

Tests

  • New test_training_launchers_export_the_malloc_thresholds in tests/unit/test_slurm_scripts.py checks each training launcher exports both.
  • bash -n on every script in scripts/slurm/, ruff check / ruff format --check, tests/unit/test_slurm_scripts.py, and sphinx-build -W on the docs.

The benchmark launchers under benchmarks/ are left as they are.

Loader workers that preprocess large images (3x3 tiling at 512 px makes
~28 MB float32 temporaries per image) grow their heap without bound once
glibc's dynamic mmap threshold rises to 32 MB: the frees fragment the
heap and are never returned. MALLOC_MMAP_THRESHOLD_=1048576 and
MALLOC_TRIM_THRESHOLD_=4194304 keep such blocks on mmap and trim the
heap, so worker memory stays flat. glibc reads them at process start, so
they belong in the launch environment; forked workers inherit them.
@codecov

codecov Bot commented Dec 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

🚀 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 11:38

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant