diff --git a/serve/server.py b/serve/server.py index cb261765..f35ca86d 100644 --- a/serve/server.py +++ b/serve/server.py @@ -1501,6 +1501,10 @@ class Server(ThreadingHTTPServer): # either one (a forgotten second start of run-.bat). Without it the second start fails loudly instead. allow_reuse_address = os.name != "nt" + def handle_error(self, request, client_address): + if not isinstance(sys.exc_info()[1], ConnectionError): # a client that hangs up needs no stack trace + super().handle_error(request, client_address) + def warn_tight_ram(arena_mib) -> None: """The model's experts live in RAM (INFO arena_mib, engine 0.1.10+). With less than ~6 GB left beside them for the @@ -1768,15 +1772,19 @@ def main() -> int: import webbrowser webbrowser.open(f"http://{'127.0.0.1' if a.host in ('0.0.0.0', '') else a.host}:{a.port}/") try: - threading.Event().wait() + while True: + time.sleep(1) # Windows never delivers Ctrl+C to an untimed Event.wait() except KeyboardInterrupt: - httpd.shutdown() - if hasattr(engine, "close"): - engine.close() - if vision: - vision.close() - if hub is not None: - hub.close() + print("\n[strata] stopping (Ctrl+C again to end the engine at once) ...", flush=True) + closers = [httpd.shutdown, getattr(engine, "close", None), vision.close if vision else None, + hub.close if hub is not None else None] + for close in filter(None, closers): + try: + close() + except KeyboardInterrupt: # a second Ctrl+C: don't wait for the engine to free its memory + if getattr(engine, "proc", None): + engine.proc.kill() + print("[strata] stopped", flush=True) return 0 diff --git a/setup.py b/setup.py index 0c30e244..be3f2f83 100644 --- a/setup.py +++ b/setup.py @@ -1531,7 +1531,7 @@ def write_run_script(model, cfg_path, port): if WIN: script = ROOT / f"run-{model.lower()}.bat" script.write_text("@echo off\r\ntitle Strata " + model + "\r\ncd /d \"" + str(ROOT) + "\"\r\n" + - " ".join(f'"{x}"' for x in serve) + "\r\npause\r\n", encoding="utf-8") + " ".join(f'"{x}"' for x in serve) + "\r\nif errorlevel 1 pause\r\n", encoding="utf-8") else: script = ROOT / f"run-{model.lower()}.sh" script.write_text("#!/bin/sh\ncd \"" + str(ROOT) + "\"\nexec " + " ".join(f'"{x}"' for x in serve) + "\n", diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 3b0bba44..9a93edd3 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -2064,6 +2064,7 @@ int main(int argc, char** argv) { // STRATA_TEST_CACHE_FAIL=N: the first N opens fail as an out-of-commit cudaMalloc does (tests the retry) int fake_fails = std::getenv("STRATA_TEST_CACHE_FAIL") ? std::atoi(std::getenv("STRATA_TEST_CACHE_FAIL")) : 0; int failed = 0; + int zero_reads = 0; for (int attempt = 0;; ++attempt) { bool ok = false; if (fake_fails > 0) { @@ -2107,9 +2108,11 @@ int main(int argc, char** argv) { cudaMemGetInfo(&free_b, &total_b); const int64_t want = (int64_t) o.vram_reserve_mib << 20; if ((int64_t) free_b >= want - (64ll << 20)) break; - // short by (want - free); a figure of 0 only says "at least", so then give back a quarter as well + // short by (want - free); a figure of 0 only says "at least": the first two such reads give back 1 GiB + // each (under WDDM the free figure read before the allocation runs ~0.7 GiB high), later ones a quarter int64_t give = want - (int64_t) free_b + (64ll << 20); - if (free_b < ((size_t) 16 << 20)) give = std::max(give, xcache.bytes() / 4); + if (free_b < ((size_t) 16 << 20)) + give = std::max(give, ++zero_reads <= 2 ? 1ll << 30 : xcache.bytes() / 4); const int64_t keep_bytes = xcache.bytes() - give; std::fprintf(stderr, "strata generate: only %lld MiB free once the slots are written (reserve %d MiB); " "shrinking the expert cache\n", (long long) (free_b >> 20), o.vram_reserve_mib);