#!/usr/bin/env python3 """Memory-cap probe for the GPU-sharing article. What does a process see, and how far can it allocate, with and without an MPS device-memory limit (CUDA_MPS_PINNED_DEVICE_MEM_LIMIT) on a unified-memory GPU? Prints one JSON line per step: {"step": "start", "mem_get_info": [free, total], "props_total": ..., "is_integrated": ..., "psutil_available": ...} {"step": "alloc", "gib": N, "mem_get_info": [...], "psutil_available": ..., "ok": true} {"step": "stop", "reason": "...", "gib": N} It allocates 1 GiB tensors until one fails, until --stop-gib, or until host MemAvailable drops under --floor-gib (so earlyoom never has to step in). It frees everything before exiting. Run with the vLLM venv's python; touch-writes each tensor so the pages are really backed. python mem_probe.py --stop-gib 32 --floor-gib 16 """ import argparse import json import os import sys import time def avail_bytes(): with open("/proc/meminfo") as f: for line in f: if line.startswith("MemAvailable:"): return int(line.split()[1]) * 1024 return -1 def main(): ap = argparse.ArgumentParser() ap.add_argument("--stop-gib", type=int, default=32, help="stop after this many GiB were allocated") ap.add_argument("--floor-gib", type=int, default=16, help="stop when host MemAvailable falls under this") ap.add_argument("--hold-s", type=float, default=5.0, help="seconds to hold the peak before freeing") a = ap.parse_args() import torch def emit(**kw): kw["t"] = round(time.time(), 2) kw["mps_limit_env"] = os.environ.get("CUDA_MPS_PINNED_DEVICE_MEM_LIMIT") kw["mps_pipe_env"] = os.environ.get("CUDA_MPS_PIPE_DIRECTORY") print(json.dumps(kw), flush=True) props = torch.cuda.get_device_properties(0) free, total = torch.cuda.mem_get_info(0) emit(step="start", device=props.name, mem_get_info=[free, total], props_total=props.total_memory, is_integrated=bool(getattr(props, "is_integrated", False)), psutil_available=avail_bytes(), torch=torch.__version__, cuda=torch.version.cuda) held, gib, reason = [], 0, "stop-gib reached" while gib < a.stop_gib: if avail_bytes() < a.floor_gib * 2**30: reason = f"host MemAvailable under {a.floor_gib} GiB" break try: t = torch.empty(2**30, dtype=torch.uint8, device="cuda") t.fill_(1) # back the pages torch.cuda.synchronize() except Exception as e: # torch.OutOfMemoryError or a CUDA error under MPS reason = f"allocation {gib + 1} failed: {type(e).__name__}: {str(e).splitlines()[0][:300]}" break held.append(t) gib += 1 free, total = torch.cuda.mem_get_info(0) emit(step="alloc", gib=gib, mem_get_info=[free, total], psutil_available=avail_bytes(), ok=True) emit(step="stop", reason=reason, gib=gib) time.sleep(a.hold_s) del held torch.cuda.empty_cache() free, total = torch.cuda.mem_get_info(0) emit(step="freed", mem_get_info=[free, total], psutil_available=avail_bytes()) return 0 if __name__ == "__main__": sys.exit(main())