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
4 changes: 3 additions & 1 deletion atom/entrypoints/openai_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

initialize_metrics()

from atom.utils import envs, set_ulimit
from atom.utils import envs, raise_memlock_limit, set_ulimit

if envs.USE_ATOMESH_ENTRYPOINTS:
from atom.entrypoints.atomesh.server import main
Expand All @@ -24,4 +24,6 @@
# subprocesses it spawns) start, so high connection concurrency does not
# exhaust file descriptors. Inherited by spawned children.
set_ulimit()
# RDMA KV registration on the spawned workers needs locked memory.
raise_memlock_limit()
main()
23 changes: 23 additions & 0 deletions atom/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,29 @@ def set_ulimit(target_soft_limit: int = 65535) -> None:
)


def raise_memlock_limit() -> None:
"""Raise the RLIMIT_MEMLOCK soft limit to the hard limit. RDMA drivers charge a
GPU ``ibv_reg_mr`` against it before their peer-memory fallback."""
try:
import resource
except ImportError: # POSIX-only.
return

soft, hard = resource.getrlimit(resource.RLIMIT_MEMLOCK)
if soft == hard:
return
try:
resource.setrlimit(resource.RLIMIT_MEMLOCK, (hard, hard))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this raise is process-wide, so engram mlock2 also flips from refused to granted now. intended? if so please note it in docs..

logger.info("Raised RLIMIT_MEMLOCK soft limit %d -> %d", soft, hard)
except (ValueError, OSError) as e:
logger.warning(
"Failed to raise RLIMIT_MEMLOCK soft limit %d to the hard limit %d: %s",
soft,
hard,
e,
)


@contextlib.contextmanager
def set_device_control_env_var(config: "Config", local_dp_rank: int):
"""Publish this engine's GPU slice under the RLHF device-map variable.
Expand Down
47 changes: 47 additions & 0 deletions tests/test_raise_memlock_limit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# SPDX-License-Identifier: MIT
# Copyright (C) 2024-2026, Advanced Micro Devices, Inc. All rights reserved.

import resource

import pytest

from atom.utils import raise_memlock_limit


def _limits(monkeypatch, soft, hard):
calls = []
monkeypatch.setattr(resource, "getrlimit", lambda kind: (soft, hard))
monkeypatch.setattr(
resource, "setrlimit", lambda kind, lim: calls.append((kind, lim))
)
return calls


@pytest.mark.parametrize("hard", [resource.RLIM_INFINITY, 64 << 30])
def test_the_soft_limit_rises_to_the_hard_limit(monkeypatch, hard):
calls = _limits(monkeypatch, 8 << 20, hard)

raise_memlock_limit()

assert calls == [(resource.RLIMIT_MEMLOCK, (hard, hard))]


def test_a_soft_limit_already_at_the_hard_limit_is_left_alone(monkeypatch):
calls = _limits(monkeypatch, resource.RLIM_INFINITY, resource.RLIM_INFINITY)

raise_memlock_limit()

assert calls == []


def test_a_refused_raise_only_warns(monkeypatch, caplog):
monkeypatch.setattr(resource, "getrlimit", lambda kind: (8 << 20, 64 << 30))

def refuse(kind, lim):
raise OSError("refused")

monkeypatch.setattr(resource, "setrlimit", refuse)

raise_memlock_limit()

assert "Failed to raise RLIMIT_MEMLOCK" in caplog.text
Loading