From 96d3bc65e8d43e84aac6966d071d9716a3c7437b Mon Sep 17 00:00:00 2001 From: Timothy Ngotiaoco Date: Sun, 6 Dec 2026 05:02:30 -0500 Subject: [PATCH] Set glibc malloc thresholds in the training launch scripts 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. --- docs/reference/environment-variables.md | 19 +++++++++++++++++++ examples/image_understanding/README.md | 10 ++++++++-- scripts/slurm/13b_validation.sh | 2 ++ scripts/slurm/7b_requeue.sh | 2 ++ scripts/slurm/_run_training.sh | 2 ++ scripts/slurm/interactive.sh | 2 ++ scripts/slurm/multinode.sh | 2 ++ scripts/slurm/singlenode.sh | 2 ++ tests/unit/test_slurm_scripts.py | 11 +++++++++++ 9 files changed, 50 insertions(+), 2 deletions(-) diff --git a/docs/reference/environment-variables.md b/docs/reference/environment-variables.md index be7f4dad7..1b3b5ee33 100644 --- a/docs/reference/environment-variables.md +++ b/docs/reference/environment-variables.md @@ -59,6 +59,24 @@ nor PyTorch reads that variable. `distributed.nccl_timeout_sec` sets the timeout of the default process group and of the group of every mesh dimension. +## glibc malloc (loader worker memory) + +The training launch scripts under +[`scripts/slurm/`](https://github.com/KempnerInstitute/KempnerForge/tree/main/scripts/slurm) +export two glibc variables that no Python module reads: + +| Variable | Value | Purpose | +|----------|-------|---------| +| `MALLOC_MMAP_THRESHOLD_` | `1048576` | Serve every allocation of 1 MiB or more with its own `mmap`, returned to the OS when freed | +| `MALLOC_TRIM_THRESHOLD_` | `4194304` | Return free memory at the top of the heap to the OS once it passes 4 MiB | + +Without them, glibc raises its mmap threshold to as much as 32 MiB after the first large free, +so large temporaries such as those of image tiling (a 3 × 3 grid at 512 px makes a float32 +canvas of about 28 MB per image) come from the heap of each loader worker, fragment it and are +never given back: worker memory then grows with every sample until the node runs out. glibc +reads the variables when a process starts, so they must be in the launch environment; loader +workers are forked from the rank and inherit them. Setting them from Python is too late. + ## SLURM metadata (resilience) Read by `get_slurm_info()`, `is_slurm_job()` and `is_slurm_requeue()` in @@ -153,6 +171,7 @@ not by any Python module: | `srun` (SLURM) | `SLURM_*` (incl. `SLURM_PROCID`, `SLURM_LOCALID`, `SLURM_NTASKS`, `SLURM_JOB_*`) | | `scripts/slurm/_run_training.sh` | Exports `RANK`, `LOCAL_RANK`, `WORLD_SIZE`, `MASTER_ADDR`, `MASTER_PORT`, `NCCL_*`, `GLOO_*` before calling the trainer | | `scripts/slurm/multinode.sh`, `7b_requeue.sh`, `13b_validation.sh`, `singlenode.sh` | `KEMPNERFORGE_SHUTDOWN_LEAD_SEC`, the lead their ranks get after the shutdown signal | +| `scripts/slurm/multinode.sh`, `7b_requeue.sh`, `13b_validation.sh`, `singlenode.sh`, `interactive.sh`, `_run_training.sh` | `MALLOC_MMAP_THRESHOLD_`, `MALLOC_TRIM_THRESHOLD_`, so loader workers return large frees to the OS | | `init_distributed()` | Fills missing `RANK`/`LOCAL_RANK`/`WORLD_SIZE` from `SLURM_*`, missing `MASTER_ADDR`/`MASTER_PORT`, and all four `NCCL_*`/`GLOO_*` defaults | | MLflow backend, eval publisher | `DATABRICKS_CONFIG_PROFILE` (from a `databricks://` URI) and `DATABRICKS_TOKEN` (from `DATABRICKS_API_TOKEN`) when unset or empty; the backend also fills the three `MLFLOW_*` HTTP defaults and `MLFLOW_DISABLE_TELEMETRY` above | diff --git a/examples/image_understanding/README.md b/examples/image_understanding/README.md index 84a6adcb4..cc3f155cb 100644 --- a/examples/image_understanding/README.md +++ b/examples/image_understanding/README.md @@ -358,8 +358,8 @@ uv run python examples/vlm/scripts/convert_hf_backbone.py \ ``` The run takes 4 nodes of 4 GPUs. Launch it the way `scripts/slurm/multinode.sh` -does, with one task per GPU and that script's `MASTER_ADDR`, `MASTER_PORT` and -NCCL settings, running the example's entry point: +does, with one task per GPU and that script's `MASTER_ADDR`, `MASTER_PORT`, +NCCL and glibc malloc settings, running the example's entry point: ```bash srun --kill-on-bad-exit=1 uv run python examples/image_understanding/train.py \ @@ -425,6 +425,12 @@ srun --kill-on-bad-exit=1 uv run python examples/image_understanding/train.py \ From a converted `.pt`, pass `--checkpoint.load_path=/your/init/checkpoint` instead. +Keep the launch scripts' malloc settings (`MALLOC_MMAP_THRESHOLD_=1048576`, +`MALLOC_TRIM_THRESHOLD_=4194304`) in this run's environment: without them each loader +worker keeps the heap its tiling temporaries grew, and its memory rises with every sample +until the node runs out of host memory hours into the run +([Environment variables](../../docs/reference/environment-variables.md#glibc-malloc-loader-worker-memory)). + The mixture file and the run's settings for it (`[vlm]`, `data.pack_sequences`, `train.batch_size = 1`) are checked before training starts. The run saves every 1000 steps and keeps the last three checkpoints. diff --git a/scripts/slurm/13b_validation.sh b/scripts/slurm/13b_validation.sh index bba47faef..eb7eb0d8c 100755 --- a/scripts/slurm/13b_validation.sh +++ b/scripts/slurm/13b_validation.sh @@ -32,6 +32,8 @@ export NCCL_IB_DISABLE=0 export NCCL_NET_GDR_LEVEL=2 export NCCL_IB_GID_INDEX=3 export GLOO_SOCKET_IFNAME="${IB_IFNAME}" +export MALLOC_MMAP_THRESHOLD_=1048576 +export MALLOC_TRIM_THRESHOLD_=4194304 export KEMPNERFORGE_SHUTDOWN_LEAD_SEC="${KEMPNERFORGE_SHUTDOWN_LEAD_SEC:-120}" mkdir -p logs diff --git a/scripts/slurm/7b_requeue.sh b/scripts/slurm/7b_requeue.sh index 12ac560f4..71b818a52 100755 --- a/scripts/slurm/7b_requeue.sh +++ b/scripts/slurm/7b_requeue.sh @@ -34,6 +34,8 @@ export NCCL_IB_DISABLE=0 export NCCL_NET_GDR_LEVEL=2 export NCCL_IB_GID_INDEX=3 export GLOO_SOCKET_IFNAME="${IB_IFNAME}" +export MALLOC_MMAP_THRESHOLD_=1048576 +export MALLOC_TRIM_THRESHOLD_=4194304 export KEMPNERFORGE_SHUTDOWN_LEAD_SEC="${KEMPNERFORGE_SHUTDOWN_LEAD_SEC:-120}" mkdir -p logs diff --git a/scripts/slurm/_run_training.sh b/scripts/slurm/_run_training.sh index 296f734bd..a3f27a662 100755 --- a/scripts/slurm/_run_training.sh +++ b/scripts/slurm/_run_training.sh @@ -18,6 +18,8 @@ export NCCL_SOCKET_IFNAME="$IB_IFNAME" export GLOO_SOCKET_IFNAME="$IB_IFNAME" export NCCL_IB_DISABLE=0 export NCCL_IB_GID_INDEX=3 +export MALLOC_MMAP_THRESHOLD_=1048576 +export MALLOC_TRIM_THRESHOLD_=4194304 LOG_DIR="${KEMPNERFORGE_LOG_DIR:-${SLURM_SUBMIT_DIR:-$PWD}/logs/distributed}" mkdir -p "$LOG_DIR" diff --git a/scripts/slurm/interactive.sh b/scripts/slurm/interactive.sh index 19dc371a0..e0453e69f 100755 --- a/scripts/slurm/interactive.sh +++ b/scripts/slurm/interactive.sh @@ -18,6 +18,8 @@ export GLOO_SOCKET_IFNAME="${IB_IFNAME:-ib0}" export NCCL_IB_DISABLE=0 export NCCL_NET_GDR_LEVEL=2 export NCCL_IB_GID_INDEX=3 +export MALLOC_MMAP_THRESHOLD_=1048576 +export MALLOC_TRIM_THRESHOLD_=4194304 export PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True diff --git a/scripts/slurm/multinode.sh b/scripts/slurm/multinode.sh index bf674d9cd..7302de427 100755 --- a/scripts/slurm/multinode.sh +++ b/scripts/slurm/multinode.sh @@ -35,6 +35,8 @@ export NCCL_NET_GDR_LEVEL=2 export NCCL_IB_GID_INDEX=3 export GLOO_SOCKET_IFNAME="${IB_IFNAME}" +export MALLOC_MMAP_THRESHOLD_=1048576 +export MALLOC_TRIM_THRESHOLD_=4194304 export KEMPNERFORGE_SHUTDOWN_LEAD_SEC="${KEMPNERFORGE_SHUTDOWN_LEAD_SEC:-120}" mkdir -p logs diff --git a/scripts/slurm/singlenode.sh b/scripts/slurm/singlenode.sh index 709febe9c..32b35dcec 100755 --- a/scripts/slurm/singlenode.sh +++ b/scripts/slurm/singlenode.sh @@ -29,6 +29,8 @@ export NCCL_NET_GDR_LEVEL=2 export NCCL_IB_GID_INDEX=3 export GLOO_SOCKET_IFNAME="${IB_IFNAME}" +export MALLOC_MMAP_THRESHOLD_=1048576 +export MALLOC_TRIM_THRESHOLD_=4194304 export KEMPNERFORGE_SHUTDOWN_LEAD_SEC=30 mkdir -p logs diff --git a/tests/unit/test_slurm_scripts.py b/tests/unit/test_slurm_scripts.py index ae3371136..423282984 100644 --- a/tests/unit/test_slurm_scripts.py +++ b/tests/unit/test_slurm_scripts.py @@ -83,6 +83,17 @@ def test_no_script_exports_nccl_timeout(path): assert "NCCL_TIMEOUT" not in path.read_text() +TRAINING_SCRIPTS = (*SRUN_SCRIPTS, "singlenode.sh", "interactive.sh", "_run_training.sh") + + +@pytest.mark.parametrize("name", TRAINING_SCRIPTS) +def test_training_launchers_export_the_malloc_thresholds(name): + """glibc reads them at process start, so the forked loader workers return large frees.""" + lines = (SLURM / name).read_text().splitlines() + assert "export MALLOC_MMAP_THRESHOLD_=1048576" in lines + assert "export MALLOC_TRIM_THRESHOLD_=4194304" in lines + + LEAD_ENV = "KEMPNERFORGE_SHUTDOWN_LEAD_SEC" TORCHRUN_CLOSE_TIMEOUT_S = 30 SIGNALED = (*SRUN_SCRIPTS, "singlenode.sh")