Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

swap_in_process: advise only the page runs marked as swapped

YanissAmz4c1399f1d5e4
diff --git a/exllamav3/util/memory.py b/exllamav3/util/memory.py
index 276c011..2d7428d 100644
--- a/exllamav3/util/memory.py
+++ b/exllamav3/util/memory.py
@@ -659,16 +659,18 @@ def check_host_memory(nbytes: int, what: str):
def swap_in_process(min_swap_kib: int = 1024) -> dict:
- """Bring the process's own swapped-out pages back into RAM (Linux). The 95 GB weight load leaves little free RAM, so the kernel
+ """Bring the process's own swapped-out pages back into RAM (Linux). The weight load leaves little free RAM, so the kernel
swaps out cold pages of the process itself (Python heap, library code, small anonymous maps). The first requests then take a
major page fault (about 1 ms each) every time a decode path not yet used touches such a page: 40-130 per request, on text the
- server has not seen, until all those pages are back. This reads them back once, before READY. Only the maps that report
- Swap > min_swap_kib are advised (MADV_POPULATE_WRITE for private writable maps, MADV_POPULATE_READ for the rest), so the cost
- is the swapped size (a few hundred MB, well under a second). Contents are unchanged. Returns stats; never raises."""
- import ctypes, os, re, time
- info = {"swap_before_kib": 0, "swap_after_kib": 0, "maps": 0, "seconds": 0.0}
+ server has not seen, until all those pages are back. This reads them back once, before READY. Only the page runs that
+ /proc/self/pagemap marks as swapped are advised (MADV_POPULATE_READ), inside the maps that report Swap > min_swap_kib, so pages
+ never touched stay unallocated and the cost is the swapped size (a few hundred MB, well under a second). Contents are
+ unchanged. Returns stats; never raises."""
+ import ctypes, re, struct, time
+ info = {"swap_before_kib": 0, "swap_after_kib": 0, "maps": 0, "runs": 0, "seconds": 0.0}
try:
libc = ctypes.CDLL(None, use_errno = True)
+ page = 4096
t0 = time.perf_counter()
todo = []
cur = None
@@ -680,13 +682,28 @@ def swap_in_process(min_swap_kib: int = 1024) -> dict:
todo.append(cur)
elif line.startswith("Swap:") and cur is not None:
cur[3] = int(line.split()[1])
- for lo, hi, perms, swap in todo:
- if swap < min_swap_kib or perms[0] != "r":
- continue
- info["swap_before_kib"] += swap
- advice = 23 if perms[1] == "w" and perms[3] == "p" else 22 # MADV_POPULATE_WRITE / MADV_POPULATE_READ
- if libc.madvise(ctypes.c_void_p(lo), ctypes.c_size_t(hi - lo), advice) == 0:
+ with open("/proc/self/pagemap", "rb", buffering = 0) as pm:
+ for lo, hi, perms, swap in todo:
+ if swap < min_swap_kib or perms[0] != "r":
+ continue
+ info["swap_before_kib"] += swap
info["maps"] += 1
+ step = 1 << 16 # pages per pagemap read (512 KiB of entries)
+ for first in range(lo // page, hi // page, step):
+ count = min(step, hi // page - first)
+ pm.seek(first * 8)
+ entries = struct.unpack(f"<{count}Q", pm.read(count * 8))
+ i = 0
+ while i < count:
+ if not entries[i] >> 62 & 1: # bit 62: page is in swap
+ i += 1
+ continue
+ j = i
+ while j < count and entries[j] >> 62 & 1:
+ j += 1
+ if libc.madvise(ctypes.c_void_p((first + i) * page), ctypes.c_size_t((j - i) * page), 22) == 0:
+ info["runs"] += 1 # 22 = MADV_POPULATE_READ
+ i = j
info["seconds"] = round(time.perf_counter() - t0, 3)
with open("/proc/self/status") as f:
for line in f: