Files
leleka/engine/strata/serve/keepawake.py
T
Pavlo RatushnyiandClaude Sonnet 5.5 87e2633afe engine/strata: bring the Strata subtree to upstream 41cf05a0 (engine 0.1.50) with the local patches re-applied
Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree
(570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base =
the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main).

Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR
experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details,
conflict table and left-out files are in docs/upstream.md.

Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61),
docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out.

scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run").
Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR
macros on. No GPU run, no quality gate.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 12:49:15 +01:00

95 lines
3.1 KiB
Python

"""Opt-in "prevent_sleep" (#1727): while at least one request is running or queued, the PC is kept awake.
Windows only. SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED) keeps the system from idling into sleep
(not the display: no ES_DISPLAY_REQUIRED); ES_CONTINUOUS alone puts the normal behaviour back. The state belongs
to the thread that set it, so one daemon thread sets and clears it; it is cleared when idle and on exit.
Elsewhere this does nothing.
"""
from __future__ import annotations
import atexit
import sys
import threading
ES_CONTINUOUS = 0x80000000
ES_SYSTEM_REQUIRED = 0x00000001
def _windows_call():
import ctypes
fn = ctypes.windll.kernel32.SetThreadExecutionState
fn.argtypes = [ctypes.c_uint32]
fn.restype = ctypes.c_uint32
return fn
class KeepAwake:
def __init__(self, call=None):
self._call = call
self._count = 0
self._cond = threading.Condition()
self._closed = False
self._thread = None
@classmethod
def create(cls, enabled: bool, say=print):
"""None when off. On a system without SetThreadExecutionState: one line, and None."""
if not enabled:
return None
if not sys.platform.startswith("win"):
say('[strata] "prevent_sleep" is for Windows only: ignored here', flush=True)
return None
try:
ka = cls(_windows_call())
except (ImportError, AttributeError, OSError) as e:
say(f'[strata] "prevent_sleep" could not be set up: {e}', flush=True)
return None
atexit.register(ka.close)
say('[strata] prevent_sleep: Windows stays awake while a request is running or queued', flush=True)
return ka
def _loop(self):
held = False
while True:
with self._cond:
while not self._closed and (self._count > 0) == held:
self._cond.wait()
if self._closed:
break
want = self._count > 0
try:
self._call(ES_CONTINUOUS | ES_SYSTEM_REQUIRED if want else ES_CONTINUOUS)
held = want
except Exception: # noqa: BLE001 never fail a request over this
held = want
try:
self._call(ES_CONTINUOUS)
except Exception: # noqa: BLE001
pass
def _start(self):
if self._thread is None:
self._thread = threading.Thread(target=self._loop, daemon=True, name="keep-awake")
self._thread.start()
def acquire(self):
with self._cond:
if self._closed:
return
self._count += 1
self._start()
self._cond.notify_all()
def release(self):
with self._cond:
self._count = max(0, self._count - 1)
self._cond.notify_all()
def close(self):
with self._cond:
self._closed = True
self._cond.notify_all()
t = self._thread
if t is not None and t is not threading.current_thread():
t.join(timeout=2)