worker_memory_cap.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python3
  2. """Cap each worker's share of the GB10's memory.
  3. vLLM never calls set_per_process_memory_fraction, so nothing bounds a worker:
  4. gpu_memory_utilization only sizes the KV pool, and every allocation outside it
  5. (the sparse indexer scores chunk x context/kpool, and grows with the session)
  6. takes host memory until the node stops forking and the watchdog resets it. On
  7. unified memory there is no separate VRAM to run out of first, and the OOM
  8. killer cannot reclaim a CUDA allocation.
  9. A fraction here turns that into torch.OutOfMemoryError: one request fails and
  10. the node keeps serving. Leave TORCH_MEM_FRACTION above gpu_memory_utilization,
  11. far enough that weights, KV and a full prefill's scratch all fit under it, and
  12. below 1.0 by whatever the host needs to stay alive.
  13. spark_mem_trace carries the logic and reads its own env at device init, so
  14. both the ceiling and the trace tune from .env without re-patching.
  15. """
  16. from pathlib import Path
  17. WORKER = Path(
  18. "/usr/local/lib/python3.12/dist-packages/vllm/v1/worker/gpu_worker.py"
  19. )
  20. ANCHOR = " torch.accelerator.set_device_index(self.device)\n"
  21. MARKER = "# [spark-mem-cap]"
  22. INSERT = f""" {MARKER}
  23. import spark_mem_trace
  24. spark_mem_trace.arm(self.device)
  25. """
  26. def main() -> None:
  27. text = WORKER.read_text()
  28. if MARKER in text:
  29. print("glm53: worker memory cap already patched")
  30. return
  31. if text.count(ANCHOR) != 1:
  32. raise SystemExit(
  33. f"glm53: expected exactly one device-init anchor in {WORKER.name}, "
  34. f"found {text.count(ANCHOR)} -- re-check the worker after a base bump"
  35. )
  36. WORKER.write_text(text.replace(ANCHOR, ANCHOR + INSERT, 1))
  37. cache = WORKER.parent / "__pycache__"
  38. if cache.is_dir():
  39. for pyc in cache.glob("gpu_worker*.pyc"):
  40. pyc.unlink(missing_ok=True)
  41. print("glm53: patched worker memory cap (TORCH_MEM_FRACTION arms it)")
  42. if __name__ == "__main__":
  43. main()