Cap CPU accelerator memory at the cgroup limit of the process #8930
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #8930"
general
weight: 0.35
delock
Issue
CPU_Accelerator.total_memory()andavailable_memory()return host RAM frompsutil.virtual_memory(). In a container or a Kubernetes pod, the process can use only its cgroup limit. Callers that plan memory from these values on the CPU accelerator include ZeRO 1/2 flattening (stage_1_and_2.py:451) and the autotuner. Fixes #8929.Related: #1891 asks for cgroup-aware CPU memory limits. #3458 (Docker), #2632 and #2788 (Slurm) report processes killed by a cgroup limit without a message.
Fix
Only the CPU accelerator, its tests and its docs change.
total_memory()returns the smallest memory limit from the process cgroup up to the root, capped at host RAM.available_memory()returns the smallest free headroom over every limited cgroup on that path. A parent limit also counts the usage of its other children, so each limited level is checked. Usage excludes the inactive page cache, which the kernel can reclaim./proc/self/mountinfo. When several mounts match, the one whose root contains the process cgroup is used. This supports mounts that expose a subtree, cgroup v1 co-mounts such ascpu,memory, and delegated subtrees mounted before the main mount.RESPECT_CGROUP_MEMORY_BY_DEFAULT = Trueis the one line that sets the default.DS_CPU_RESPECT_CGROUP_MEMORY=0turns it off,=1turns it on.export_envs()returns the variable, so the launcher forwards it to every node.abstract_accelerator.pydoes not change.How to test
Needs Docker with cgroup v2. Save the first script from the collapsed sections at the end as
check_cgroup_memory.sh. One command installsmaster(before) and this branch (after) in the same container with 1 GiB. It printstotal_memory()andavailable_memory()from three cgroups withDS_CPU_RESPECT_CGROUP_MEMORYunset, and once more with=0.Output (Ubuntu 24.04 VM, kernel 6.8, 3.8 GiB, cgroup v2, Docker 29.1 with the systemd driver):
Before, every case reports the host. After,
totalis the limit of each case. In C,availableis the free headroom of the 800 MiB parent, which the 500 MiB sibling also uses. In A, the container's 1 GiB also holds that sibling. With=0, the values are host RAM again.On a cgroup v1 host, the second script,
check_cgroup_v1.sh, runs the same cases with the same arguments. Same VM booted withsystemd.unified_cgroup_hierarchy=0, Docker 29.1 with the cgroupfs driver and its default host cgroup namespace:Each script ran twice per cgroup version.
totaland the C row are identical in all runs.availablein A and B reads live usage, so it moves between runs.Proof
ZeRO training on the CPU accelerator, three Adam steps, flag
0and1, two fresh containers each:| processes | precision | ZeRO 1 | ZeRO 2 | ZeRO 3 |
|---|---|---|---|---|
| 1 | fp32 | identical losses | identical losses | identical losses |
| 1 | bf16 | identical losses | identical losses | identical losses |
| 2 (
deepspeedlauncher, gloo) | fp32 | identical losses | identical losses | identical losses |Performance
Throughput and peak RSS, flag
0and1(1is the code path that is now the default), 30 timed steps on a 4×1024 MLP. Four runs per cell, two fresh containers, flag alternated. Median steps/s with range:| processes | precision | ZeRO | steps/s, flag 0 | steps/s, flag 1 | peak RSS, flag 0 / 1 |
|---|---|---|---|---|---|
| 1 | fp32 | 1 | 23.8 (22.9–24.7) | 24.2 (23.2–25.2) | 575 / 575 MiB |
| 1 | fp32 | 2 | 24.3 (24.0–24.9) | 23.3 (22.4–24.1) | 572 / 571 MiB |
| 1 | fp32 | 3 | 22.1 (21.1–26.5) | 22.1 (20.9–22.7) | 606 / 606 MiB |
| 1 | bf16 | 1 | 41.5 (37.7–42.4) | 41.2 (40.7–42.5) | 530 / 530 MiB |
| 1 | bf16 | 2 | 42.0 (40.1–42.5) | 41.6 (33.5–42.6) | 530 / 530 MiB |
| 1 | bf16 | 3 | 54.1 (45.2–55.4) | 54.2 (51.3–56.7) | 556 / 556 MiB |
| 2 | fp32 | 1 | 11.1 (10.8–11.4) | 11.4 (10.4–11.7) | 524 / 524 MiB |
| 2 | fp32 | 2 | 11.2 (10.3–12.4) | 11.9 (11.2–12.6) | 524 / 522 MiB |
| 2 | fp32 | 3 | 11.0 (10.7–11.1) | 10.6 (9.9–11.0) | 564 / 564 MiB |
Peak RSS does not change. The throughput differences go in both directions and stay inside the run-to-run range. A call counter shows why: ZeRO 1 and 2 call the memory queries once at setup, ZeRO 3 does not call them, and no configuration calls them during training steps.
Cost of one query, best of 5 × 5000 calls, process three levels below the cgroup root:
| |
total_memory()|available_memory()||---|---|---|
| master | 12.4 µs | 12.5 µs |
| this PR,
=0| 13.1 µs | 12.9 µs || this PR, default (on) | 81.5 µs | 80.7 µs |
Measured on ca6a5a6, before the default changed; the two rows are the same code paths. With the cap on, a query reads
/proc/self/cgroup,/proc/self/mountinfoand the cgroup files: about 70 µs more, once per run.Twelve new tests. They cover a limit on a parent cgroup, the smallest limit, the v1 sentinel, the headroom of every limited level, mountinfo resolution with several mounts, the launcher export, and default × override. One test uses
/proc/self/cgroup, the cgroup mountinfo lines and the memory files captured in a Docker container on the cgroup v1 host above, so the v1 path is checked against a real layout. With the mount-root strip removed from the code, that test fails and the hand-written v1 tests still pass.Hardware. Apple M4 host, Docker Desktop Linux 6.10 VM (aarch64, 10 CPUs, 7.8 GiB, cgroup v2). Python 3.12.16, torch 2.14.1+cpu,
DS_BUILD_OPS=0. No GPU. cgroup v1, the v2 re-run and the unit tests above: Lima VM on the same host, Ubuntu 24.04, kernel 6.8.0-142 (aarch64, 4 vCPU, 3.8 GiB), Docker 29.1.3, Python 3.12.16, torch 2.15.0+cpu.Compatibility matrix.
deepspeed.compile(is_deepcompile_supported()requires CUDA) and the inference v2 KV cache (CUDA kernels only).see_memory_usage()and the timer read host RAM only to log it.No open PR changes the CPU accelerator memory functions (searched
cgroup,cpu_accelerator available_memory,cpu accelerator memory,1891 in:bodyon 2026-10-27).AI assistance was used: an AI coding agent (Claude Code) wrote the change and the tests; I reviewed each changed line and ran the tests, the reproduction and the ZeRO runs.
<details>
<summary>check_cgroup_memory.sh (cgroup v2)</summary>
</details>
<details>
<summary>check_cgroup_v1.sh (cgroup v1)</summary>
</details>
Precedent. The same walk is already in production elsewhere. Rust's
std::thread::available_parallelismtakes the smallestcpu.maxfrom the process cgroup up to the mount (quota_v2). The JDK fixed limits on parent nested cgroups in JDK-8322420 (2024) and the hierarchical memory limit in JDK-8370572 (2025). Go 1.25 finds the process cgroup through/proc/self/cgroupand/proc/self/mountinfo(cgroup_linux.go), reading the leaf only. vLLM's CPU-quota reader walks the hierarchy (_cgroup_cpu_limit); vllm-project/vllm#64104 adds the same for memory. The same fix for OpenObserve merged on 2026-10-28 (openobserve/openobserve#15906).