[HiCache] Fix cgroup page-cache accounting and the sizing fallback when cgroup discovery fails (#42420)
Co-authored-by: merrymercy <merrymercy@users.noreply.github.com>
Co-authored-by: xiezhq-hermann <xiezhq-hermann@users.noreply.github.com>
Co-authored-by: nvjullin <nvjullin@users.noreply.github.com>
(cherry picked from commit 4ab720e655)
This commit is contained in:
@@ -91,7 +91,7 @@ def auto_size_hicache(
|
||||
return
|
||||
requested = get_memory().hicache_ratio
|
||||
device_bytes = _estimate_hicache_bytes(params, draft_plan)
|
||||
budget = int(host_memory_budget_bytes() * fraction)
|
||||
budget = int(host_memory_budget_bytes(auto_size=True) * fraction)
|
||||
ratio = min(requested, budget * (1 - _ALLOCATION_SLACK_FRACTION) / device_bytes)
|
||||
# One collective before any pool is built: PP stages own different pool
|
||||
# counts, so a per-pool collective could deadlock.
|
||||
|
||||
@@ -15,6 +15,11 @@ def _unescape_mount_path(value: str) -> str:
|
||||
return re.sub(r"\\([0-7]{3})", lambda m: chr(int(m[1], 8)), value)
|
||||
|
||||
|
||||
def _memory_stat(directory: Path) -> dict[str, int]:
|
||||
lines = (directory / "memory.stat").read_text().splitlines()
|
||||
return {key: int(value) for key, value in map(str.split, lines)}
|
||||
|
||||
|
||||
def _cgroup_memory_headroom(proc_root: Path = Path("/proc")) -> int | None:
|
||||
memberships = {}
|
||||
try:
|
||||
@@ -65,6 +70,13 @@ def _cgroup_memory_headroom(proc_root: Path = Path("/proc")) -> int | None:
|
||||
usage_name = (
|
||||
"memory.current" if filesystem == "cgroup2" else "memory.usage_in_bytes"
|
||||
)
|
||||
# File LRU pages, as MemAvailable counts; shmem sits on the anon LRU.
|
||||
# v1's unprefixed counters omit descendants, unlike its usage.
|
||||
page_cache_keys = (
|
||||
("active_file", "inactive_file")
|
||||
if filesystem == "cgroup2"
|
||||
else ("total_active_file", "total_inactive_file")
|
||||
)
|
||||
while True:
|
||||
for name in limits:
|
||||
try:
|
||||
@@ -78,7 +90,10 @@ def _cgroup_memory_headroom(proc_root: Path = Path("/proc")) -> int | None:
|
||||
# Do not silently ignore an unreadable usage file for a known
|
||||
# limit: falling back to host RAM could overrun the container.
|
||||
usage = int((directory / usage_name).read_text())
|
||||
remaining = max(0, limit - usage)
|
||||
stat = _memory_stat(directory)
|
||||
page_cache = sum(stat[key] for key in page_cache_keys)
|
||||
# Charged page cache is reclaimed under the limit, not used.
|
||||
remaining = max(0, limit - max(0, usage - page_cache))
|
||||
headroom = remaining if headroom is None else min(headroom, remaining)
|
||||
if directory == mount:
|
||||
break
|
||||
@@ -90,10 +105,25 @@ def _cgroup_memory_headroom(proc_root: Path = Path("/proc")) -> int | None:
|
||||
return headroom
|
||||
|
||||
|
||||
def available_host_memory_bytes() -> int:
|
||||
"""Conservative allocatable RAM; charged file cache is not assumed reclaimable."""
|
||||
def available_host_memory_bytes(*, allow_cgroup_fallback: bool = False) -> int:
|
||||
"""Allocatable RAM, counting reclaimable page cache as free."""
|
||||
available = psutil.virtual_memory().available
|
||||
cgroup_headroom = _cgroup_memory_headroom()
|
||||
try:
|
||||
cgroup_headroom = _cgroup_memory_headroom()
|
||||
except (OSError, RuntimeError) as exc:
|
||||
if not allow_cgroup_fallback:
|
||||
raise RuntimeError(
|
||||
"Cannot automatically size HiCache host memory; "
|
||||
"set --hicache-size explicitly (GB per rank), leaving room "
|
||||
"for other memory use within the container limit."
|
||||
) from exc
|
||||
logger.warning(
|
||||
"Cannot determine cgroup memory headroom: %s. Checking explicitly "
|
||||
"sized HiCache pools against host available memory; ensure all "
|
||||
"local ranks' pools fit within the container memory limit.",
|
||||
exc,
|
||||
)
|
||||
return available
|
||||
if cgroup_headroom is not None:
|
||||
logger.info(
|
||||
"HiCache memory headroom: host %.1f GiB, cgroup %.1f GiB",
|
||||
|
||||
@@ -61,12 +61,16 @@ def ranks_per_host() -> int:
|
||||
return max(launch_world_size // get_parallel().nnodes, 1)
|
||||
|
||||
|
||||
def host_memory_budget_bytes(requested_bytes: int = 0) -> int:
|
||||
def host_memory_budget_bytes(
|
||||
requested_bytes: int = 0, *, auto_size: bool = False
|
||||
) -> int:
|
||||
"""Host RAM this rank may claim for a HiCache pool.
|
||||
|
||||
Bound machine availability by the visible cgroup limits before splitting
|
||||
among local ranks. Independent engines with separate container budgets
|
||||
therefore size against their own remaining allowance.
|
||||
Auto-sizing requires successful cgroup discovery; explicit pool sizing
|
||||
can fall back to checking host availability when discovery fails.
|
||||
|
||||
Inside host_memory_budget_scope, requested_bytes is booked against the
|
||||
snapshot when it fits; the allowance before booking is returned.
|
||||
@@ -77,7 +81,10 @@ def host_memory_budget_bytes(requested_bytes: int = 0) -> int:
|
||||
_host_memory_budget.set(available - requested_bytes)
|
||||
return available
|
||||
|
||||
free = available_host_memory_bytes() - HICACHE_HOST_MEMORY_RESERVE_BYTES
|
||||
free = (
|
||||
available_host_memory_bytes(allow_cgroup_fallback=not auto_size)
|
||||
- HICACHE_HOST_MEMORY_RESERVE_BYTES
|
||||
)
|
||||
return free // ranks_per_host()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user