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.

Qwen serve: read the process's own swapped-out pages back before READY

YanissAmz47aa5da6f299
diff --git a/exllamav3/util/memory.py b/exllamav3/util/memory.py
index 9327c26..276c011 100644
--- a/exllamav3/util/memory.py
+++ b/exllamav3/util/memory.py
@@ -656,3 +656,42 @@ def check_host_memory(nbytes: int, what: str):
f"in host memory (fewer offloaded experts, no --ngram_ram, ...) or set "
f"EXL3_HOST_MEM_RESERVE_MB=0 to skip this check."
)
+
+
+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
+ 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}
+ try:
+ libc = ctypes.CDLL(None, use_errno = True)
+ t0 = time.perf_counter()
+ todo = []
+ cur = None
+ with open("/proc/self/smaps") as f:
+ for line in f:
+ m = re.match(r"^([0-9a-f]+)-([0-9a-f]+) (\S+) ", line)
+ if m:
+ cur = [int(m.group(1), 16), int(m.group(2), 16), m.group(3), 0]
+ 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:
+ info["maps"] += 1
+ info["seconds"] = round(time.perf_counter() - t0, 3)
+ with open("/proc/self/status") as f:
+ for line in f:
+ if line.startswith("VmSwap:"):
+ info["swap_after_kib"] = int(line.split()[1])
+ except Exception as e:
+ info["error"] = repr(e)
+ return info
diff --git a/tools/qwen/serve.py b/tools/qwen/serve.py
index 4ecbbd5..98cba46 100644
--- a/tools/qwen/serve.py
+++ b/tools/qwen/serve.py
@@ -1709,6 +1709,9 @@ def load_all(args: argparse.Namespace):
startup_health.stage("warm-up")
with Heartbeat("warm-up (first decode steps)"):
engine.warmup()
+ if os.environ.get("EXL3_SWAP_IN", "1") != "0":
+ from exllamav3.util.memory import swap_in_process
+ print(f"qserve: process pages swapped out during the load, read back before READY: {swap_in_process()}", flush=True)
return engine, template, defaults, model_dir, t0