Repository navigation
fix(engram): mlock in-place UVA page runs while registered (on by default, best effort) #2898
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
521272a
fix(engram): opt-in mlock of in-place UVA registrations (ATOM_ENGRAM_…
yhl-amd 71ff9a3
Merge remote-tracking branch 'origin/main' into hyi/engram-mlock
yhl-amd 0b6d34e
fix(engram): mlock in-place UVA registrations by default, only once c…
yhl-amd bc01784
fix(engram): mlock the in-place shard once the TP group keeps it
yhl-amd 413eeb0
tools(engram): bench_uva_shard mlocks the in-place shard like the server
yhl-amd a9824b6
Merge remote-tracking branch 'origin/main' into hyi/engram-mlock
yhl-amd 38fd2bb
fix(engram): tie the mlock to _PageRuns; lock and fault in one parall…
yhl-amd 4ba5db4
Merge remote-tracking branch 'origin/main' into hyi/engram-mlock
yhl-amd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| """In-place UVA registrations are `mlock`ed: `_mlock` locks a shared read-only | ||
| file mapping (what an in-place registration pins) and only warns when the | ||
| kernel refuses; `mlock_in_place` locks only what is registered in place.""" | ||
|
|
||
| import ctypes | ||
| import logging | ||
| import mmap | ||
| import resource | ||
| import sys | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
| import torch | ||
|
|
||
| from atom.model_ops.engram import host_memory | ||
|
|
||
| pytestmark = pytest.mark.skipif( | ||
| not sys.platform.startswith("linux"), reason="mlock / VmLck are Linux-only" | ||
| ) | ||
|
|
||
|
|
||
| def _vm_lck_kb() -> int: | ||
| with open("/proc/self/status") as f: | ||
| for line in f: | ||
| if line.startswith("VmLck:"): | ||
| return int(line.split()[1]) | ||
| raise AssertionError("no VmLck in /proc/self/status") | ||
|
|
||
|
|
||
| def _table(tmp_path, pages: int) -> np.memmap: | ||
| path = tmp_path / "table.bin" | ||
| path.write_bytes(b"\x01" * (pages * mmap.PAGESIZE)) | ||
| return np.memmap(path, dtype=np.uint8, mode="r") | ||
|
|
||
|
|
||
| def _mlock_allowed(lo: int, nbytes: int) -> bool: | ||
| """Whether this process may lock `nbytes` at `lo` (probe, then unlock).""" | ||
| libc = ctypes.CDLL(None, use_errno=True) | ||
| libc.mlock.argtypes = [ctypes.c_void_p, ctypes.c_size_t] | ||
| libc.munlock.argtypes = [ctypes.c_void_p, ctypes.c_size_t] | ||
| if libc.mlock(lo, nbytes) != 0: | ||
| return False | ||
| libc.munlock(lo, nbytes) | ||
| return True | ||
|
|
||
|
|
||
| def test_mlock_locks_the_mapping(tmp_path): | ||
| table = _table(tmp_path, 4) | ||
| lo = table.ctypes.data | ||
| if not _mlock_allowed(lo, table.nbytes): | ||
| pytest.skip("RLIMIT_MEMLOCK too small for 4 pages") | ||
| before = _vm_lck_kb() | ||
| host_memory._mlock([(lo, lo + table.nbytes)]) | ||
| assert _vm_lck_kb() - before >= table.nbytes // 1024 | ||
|
|
||
|
|
||
| def test_mlock_refusal_only_warns(tmp_path, caplog): | ||
| table = _table(tmp_path, 1) | ||
| lo = table.ctypes.data | ||
| soft, hard = resource.getrlimit(resource.RLIMIT_MEMLOCK) | ||
| try: | ||
| resource.setrlimit(resource.RLIMIT_MEMLOCK, (0, hard)) | ||
| except (ValueError, OSError): | ||
| pytest.skip("cannot lower RLIMIT_MEMLOCK") | ||
| try: | ||
| if _mlock_allowed(lo, table.nbytes): | ||
| pytest.skip("mlock allowed despite RLIMIT_MEMLOCK=0 (CAP_IPC_LOCK)") | ||
| before = _vm_lck_kb() | ||
| with caplog.at_level(logging.WARNING): | ||
| host_memory._mlock([(lo, lo + table.nbytes)] * 3) | ||
| finally: | ||
| resource.setrlimit(resource.RLIMIT_MEMLOCK, (soft, hard)) | ||
| assert _vm_lck_kb() == before | ||
| warnings = [r for r in caplog.records if r.levelno == logging.WARNING] | ||
| assert len(warnings) == 1 and "mlock" in warnings[0].getMessage() | ||
|
|
||
|
|
||
| def test_mlock_in_place_skips_what_is_not_registered(monkeypatch): | ||
| """Only ranges `_PAGE_RUNS` holds (in place) are locked; a copy's memory | ||
| is locked by `hipHostMalloc` already.""" | ||
| page = host_memory._PAGE | ||
| runs = host_memory._PageRuns() | ||
| monkeypatch.setattr(host_memory, "_PAGE_RUNS", runs) | ||
| locked = [] | ||
| monkeypatch.setattr(host_memory, "_mlock", lambda ranges: locked.extend(ranges)) | ||
| registered = torch.ones(3 * page, dtype=torch.uint8) | ||
| copied = torch.ones(3 * page, dtype=torch.uint8) | ||
| base = registered.data_ptr() | ||
| lo = base - base % page | ||
| hi = -(-(base + registered.numel()) // page) * page | ||
| runs._runs[lo] = [hi, 1] | ||
| host_memory.mlock_in_place([registered, copied]) | ||
| assert locked == [(lo, hi)] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bench tool registers in place without mlock now, so its VmLck report no longer matches server. maybe call mlock_in_place there too, or note it only measures registration?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 413eeb0: the bench calls
mlock_in_placeafter enabling the tables, as_enable_uvadoes, so its VmLck and timing match the server.