fix(app): log the card's real free memory, and short answers get their line too

Free memory is nvidia-smi's memory.free in the app and the voice worker, so the
driver's reserved share no longer counts as free. A card reading still running
when an answer ends finishes on its own and then logs, so an answer shorter
than one reading is logged too, and the answer still never waits for it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-17 15:48:01 +09:00
parent 61b68130c6
commit 44387685af
6 changed files with 89 additions and 34 deletions

View File

@@ -45,14 +45,15 @@ LLM_WAIT_S = 180.0
def nvidia_card() -> tuple[int, int] | None:
"""The first NVIDIA card's used and total memory in MiB (``nvidia-smi``), None when unreadable."""
"""The first NVIDIA card's free and total memory in MiB (``nvidia-smi``; free leaves out what the
driver reserves), None when unreadable."""
import subprocess
try:
out = subprocess.run(["nvidia-smi", "--query-gpu=memory.used,memory.total", "--format=csv,noheader,nounits"],
out = subprocess.run(["nvidia-smi", "--query-gpu=memory.free,memory.total", "--format=csv,noheader,nounits"],
capture_output=True, text=True, timeout=5,
creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0)).stdout
used, total = (int(v) for v in out.splitlines()[0].split(","))
return used, total
free, total = (int(v) for v in out.splitlines()[0].split(","))
return free, total
except Exception:
return None
@@ -120,7 +121,8 @@ class VoiceWorker:
self.history = history # jarvis.memory.history.HistoryStore or None
self.recorder = recorder # recordings.Recorder (voice.record on) or None
self.brain = brain
self.card = card # (used MiB, total MiB) of the graphics card, or None without one
self.card = card # (free MiB, total MiB) of the graphics card, or None without one
self.card_watches: set[asyncio.Task] = set() # readings still running after their answer
# One reply at a time per lane: voice answers never wait behind another person's chat answer
# (conversation.md: with an "answer everything" chat channel the LLM takes two or more
# requests at once). One person's replies stay one at a time across lanes, so their dialogue
@@ -771,30 +773,42 @@ class Session:
@contextlib.asynccontextmanager
async def _card_watch(self, label: str):
"""While an answer is written: the card's free memory, read every `CARD_EVERY_S`; the least
seen is logged once it ends (a slow answer can then be matched to a full card)."""
card = self.worker.card
seen is logged once it ends (a slow answer can then be matched to a full card). The answer
never waits for a reading: one still running when it ends finishes on its own, then logs."""
w = self.worker
card = w.card
if card is None:
yield
return
seen: list[tuple[int, int]] = []
ended = asyncio.Event()
t0, took = time.monotonic(), [0]
async def read():
async def watch():
while True:
got = await asyncio.to_thread(card)
if got:
seen.append(got)
await asyncio.sleep(CARD_EVERY_S)
if ended.is_set():
break
try:
await asyncio.wait_for(ended.wait(), CARD_EVERY_S)
break
except asyncio.TimeoutError:
pass
if seen:
total = seen[-1][1]
notice(f"{label} card: least {min(f for f, _ in seen)} MiB free of {total} MiB "
f"({len(seen)} readings, {took[0]} ms)", "llm")
t0 = time.monotonic()
task = asyncio.get_running_loop().create_task(read())
task = asyncio.get_running_loop().create_task(watch())
w.card_watches.add(task)
task.add_done_callback(w.card_watches.discard)
try:
yield
finally:
task.cancel()
if seen:
total = seen[-1][1]
notice(f"{label} card: least {min(total - u for u, _ in seen)} MiB free of {total} MiB "
f"({len(seen)} readings, {int((time.monotonic() - t0) * 1000)} ms)", "llm")
took[0] = int((time.monotonic() - t0) * 1000)
ended.set()
def _on_prepare(self, msg: dict) -> None:
"""A call still being said: the brain gets ready for it. One prepare per user runs at a time;

View File

@@ -535,6 +535,8 @@ in its tab (`app/src/admin.spec.md`, "Logs"): `stt` (utterance ends, STT batches
Without debug logging too, each answer and each scheduled task logs one `llm` line once the LLM is
done with it: the least free graphics memory read while it was written, the card's total, how many
readings and how long it took (`answer <id> card: least N MiB free of T MiB (k readings, ms)`). The
card is read with `nvidia-smi` once a second while the answer is written, in a thread, so the answer
never waits for it; without `nvidia-smi` nothing is read or logged. The line carries no text, so a
card is read with `nvidia-smi` (`memory.free`, which leaves out what the driver reserves) once a
second while the answer is written, in a thread, so the answer never waits for it: a reading still
running when the answer ends finishes on its own and the line follows it, so a short answer gets its
line too. Without `nvidia-smi` nothing is read or logged. The line carries no text, so a
slow answer can be matched to a full card (the engine log has the same answer's timings).