|
| 1 | +"""An admitted CPU copy of accepted native weights, never a training candidate.""" |
| 2 | + |
| 3 | +import copy |
| 4 | +import threading |
| 5 | +import time |
| 6 | +from contextlib import ExitStack |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +from rlm.v100.common import atomic_json |
| 10 | +from rlm.v100.competition import helper_client, managed_server |
| 11 | +from rlm.v100.protection import file_hash |
| 12 | +from rlm.v100.researchers import available_ram_gib |
| 13 | +from rlm.v100.serving import assert_served_expert |
| 14 | + |
| 15 | + |
| 16 | +def cpu_profile(accepted: dict) -> dict: |
| 17 | + if accepted["runtime"].get("backend", "llamacpp") not in ("llamacpp", "native"): |
| 18 | + raise ValueError("CPU chat requires accepted native GGUF weights") |
| 19 | + profile = copy.deepcopy(accepted) |
| 20 | + profile["server"].update( |
| 21 | + gpu_layers=0, |
| 22 | + slots=1, |
| 23 | + threads=2, |
| 24 | + context_per_slot=8192, |
| 25 | + batch_size=64, |
| 26 | + ubatch_size=32, |
| 27 | + flash_attention="off", |
| 28 | + cache_type="f16", |
| 29 | + draft_model="", |
| 30 | + draft_tokens=0, |
| 31 | + ) |
| 32 | + profile["runtime"].update( |
| 33 | + base_url="http://127.0.0.1:8094", |
| 34 | + context_window=8192, |
| 35 | + max_output_tokens=1024, |
| 36 | + enable_thinking=False, |
| 37 | + max_timeout=600, |
| 38 | + ) |
| 39 | + profile["memory"].update(chunk_tokens=512, fanout=4, retrieve_count=4) |
| 40 | + profile.setdefault("resources", {}).pop("mtp_validation", None) |
| 41 | + profile["resources"].update( |
| 42 | + device="cpu", |
| 43 | + min_available_ram_gib=16, |
| 44 | + cpu_guard=True, |
| 45 | + min_free_ram_gib=4, |
| 46 | + ) |
| 47 | + return profile |
| 48 | + |
| 49 | + |
| 50 | +class AcceptedCPUChat: |
| 51 | + def __init__(self, root: Path, directory: Path, stop: threading.Event | None = None): |
| 52 | + self.root, self.directory = root, directory |
| 53 | + self.stack = ExitStack() |
| 54 | + self.identity = None |
| 55 | + self.client = None |
| 56 | + self.last_used = 0.0 |
| 57 | + self.sha256 = None |
| 58 | + self.profile = None |
| 59 | + self.stop = stop or threading.Event() |
| 60 | + |
| 61 | + def close(self) -> None: |
| 62 | + active = self.client is not None |
| 63 | + self.stack.close() |
| 64 | + self.stack = ExitStack() |
| 65 | + self.client, self.identity, self.sha256, self.profile = None, None, None, None |
| 66 | + if active: |
| 67 | + atomic_json( |
| 68 | + self.directory / "accepted-cpu-chat-status.json", |
| 69 | + { |
| 70 | + "available": False, |
| 71 | + "state": "closed; accepted weights retained", |
| 72 | + }, |
| 73 | + ) |
| 74 | + |
| 75 | + def maintain(self) -> None: |
| 76 | + if self.client is not None and ( |
| 77 | + time.monotonic() - self.last_used > 180 or available_ram_gib() < 4 |
| 78 | + ): |
| 79 | + self.close() |
| 80 | + |
| 81 | + def get(self, accepted: dict): |
| 82 | + if self.stop.is_set(): |
| 83 | + raise RuntimeError("Mission is stopping; CPU chat cannot start") |
| 84 | + model = Path(accepted["server"]["model"]) |
| 85 | + stat = model.stat() |
| 86 | + identity = (str(model.resolve()), stat.st_size, stat.st_mtime_ns) |
| 87 | + if self.identity != identity: |
| 88 | + self.close() |
| 89 | + if self.client is not None: |
| 90 | + try: |
| 91 | + assert_served_expert( |
| 92 | + self.client, self.profile, self.root, {"files": {"model-gguf": self.sha256}} |
| 93 | + ) |
| 94 | + props = self.client.request("/props") |
| 95 | + if Path(props["model_path"]).resolve() != model.resolve(): |
| 96 | + raise ValueError("CPU chat endpoint no longer serves accepted weights") |
| 97 | + except Exception: |
| 98 | + self.close() |
| 99 | + raise |
| 100 | + self.last_used = time.monotonic() |
| 101 | + return self.client |
| 102 | + required = max(16, stat.st_size / 2**30 * 1.5 + 4) |
| 103 | + if available_ram_gib() < required: |
| 104 | + raise ValueError(f"CPU chat needs {required:.1f} GiB available RAM; no model loaded") |
| 105 | + profile = cpu_profile(accepted) |
| 106 | + profile["resources"]["min_available_ram_gib"] = required |
| 107 | + profile["resources"]["max_rss_gib"] = stat.st_size / 2**30 * 1.5 + 2 |
| 108 | + path = self.directory / "accepted-cpu-chat.json" |
| 109 | + atomic_json(path, profile) |
| 110 | + self.sha256 = file_hash(model) |
| 111 | + try: |
| 112 | + actual = self.stack.enter_context( |
| 113 | + managed_server( |
| 114 | + path, self.root, self.directory / "accepted-cpu-chat.log", cancel=self.stop |
| 115 | + ) |
| 116 | + ) |
| 117 | + client = helper_client(actual, self.root) |
| 118 | + assert_served_expert(client, actual, self.root, {"files": {"model-gguf": self.sha256}}) |
| 119 | + except Exception: |
| 120 | + self.close() |
| 121 | + raise |
| 122 | + self.client, self.identity = client, identity |
| 123 | + self.profile = actual |
| 124 | + self.last_used = time.monotonic() |
| 125 | + client.activity_actor = "chat-master-cpu" |
| 126 | + atomic_json( |
| 127 | + self.directory / "accepted-cpu-chat-status.json", |
| 128 | + { |
| 129 | + "available": True, |
| 130 | + "accepted_model_sha256": self.sha256, |
| 131 | + "model": str(model), |
| 132 | + "context": 8192, |
| 133 | + "threads": 2, |
| 134 | + "required_available_ram_gib": required, |
| 135 | + "scope": "Identical accepted weights, CPU only; not a promoted candidate", |
| 136 | + }, |
| 137 | + ) |
| 138 | + return client |
0 commit comments