Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/reference/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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://<profile>` 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 |

Expand Down
10 changes: 8 additions & 2 deletions examples/image_understanding/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions scripts/slurm/13b_validation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions scripts/slurm/7b_requeue.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions scripts/slurm/_run_training.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions scripts/slurm/interactive.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions scripts/slurm/multinode.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions scripts/slurm/singlenode.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/test_slurm_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading