feat(m0): measure the game's own frame times and try yielding by priority instead of a GPU cap
The 95 % GPU cap is gone: it did not lower utilisation, slowed the LLM and a YouTube video did not stutter without it. What it was meant to protect is now measured directly in a game. The new on-demand game_frames stage records the game's frame times with PresentMon (pinned by hash, game process only, CSV deleted after reading) with Jarvis off, idle and busy in the game placement, and busy while Jarvis yields: its processes at below-normal CPU and GPU scheduling priority, read back to confirm, then restored. Busy rows run A B B A. The design docs record the candidate and the pre-registered rule for adopting it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -38,6 +38,7 @@ LLM 단계는 8080 포트를 씁니다. 다른 프로그램이 이 포트를 쓰
|
||||
| 게임 중 자동 조절 | 이미 받은 Qwen3.6-35B-A3B를 평소 배치(동시 1개)와 게임 중 배치 3개(전문가 층 2GB 맥락 32K, 2GB 맥락 16K, 0GB 맥락 16K)로 차례로 다시 시작해서, 배치마다 전환 시간, 남는 VRAM, 출력 속도, 음성 첫 문장, 맥락 8천 토큰 첫 토큰 시간을 잽니다. 마지막에 평소 배치로 한 번 더 켜서 되돌리는 시간도 잽니다 |
|
||||
| LLM과 음성 인식 함께 | 음성 인식(STT)을 먼저 켜 두고 LLM을 평소 배치(동시 1개, 2개)와 게임 중 배치(전문가 2GB·0GB, 맥락 16K)로 켭니다. LLM을 재는 동안 음성 인식은 10명 동시 전사를 계속 돌립니다. 게임 중 배치는 음성 인식을 GPU에 둔 경우와 CPU에 둔 경우를 둘 다 잽니다. 배치마다 남는 VRAM, LLM 속도, 10명 동시 전사 시간, 한 사람 전사 시간을 적습니다 |
|
||||
| 영상 끊김 (`run_m0_video.bat`로만) | 유튜브 영상을 틀어 두고 Jarvis를 끈 때, 켜 두고 쉴 때, 바쁠 때(GPU 95% 제한 없이와 있을 때)의 끊긴 프레임 수와 긴 멈춤 횟수 |
|
||||
| 게임 프레임 (`run_m0_game_frames.bat`로만) | 게임을 켜 두고 Jarvis를 끈 때, 켜 두고 쉴 때, 바쁠 때(우선순위를 낮춰 게임에 양보할 때와 안 할 때)의 게임 화면 평균 fps, 1% low, 50ms·100ms 넘는 프레임 수 |
|
||||
| STT 자리와 부분 전사 간격 | 평소 배치를 STT 자리를 남긴 전문가 층 몫으로 켜고 STT를 쉬지 않고 돌리며 잽니다. 게임 중 배치(전문가 2GB)는 STT를 쉬지 않고 돌릴 때, 부분 전사를 2초마다 할 때, 부분 전사 없이 최종 전사만 할 때를 잽니다(게임은 켜지 않음). 배치마다 남는 VRAM, LLM 속도, 최종 전사 시간과 기다린 시간, STT가 GPU를 쓴 비율을 적습니다 |
|
||||
|
||||
## 개인정보
|
||||
@@ -48,6 +49,7 @@ LLM 단계는 8080 포트를 씁니다. 다른 프로그램이 이 포트를 쓰
|
||||
- 게임 감지는 실행 중인 프로그램 이름과 창 모양만 봅니다. 게임 화면은 저장하지 않습니다. 설치된 게임은 이름과 안티치트 유무만 기록합니다.
|
||||
- 음성 테스트는 키트에 들어 있는 합성 음성만 씁니다. 마이크는 쓰지 않습니다.
|
||||
- 영상 끊김 단계(`video_drops`)는 유튜브에 공개된 Blender 영상을 새 임시 브라우저 프로필로 엽니다. 로그인하지 않고, 평소 쓰시는 프로필은 건드리지 않으며, 끝나면 그 임시 프로필을 지웁니다. 저장하는 것은 영상의 프레임 수, 끊긴 프레임 수, 화질, 광고가 나왔는지뿐입니다.
|
||||
- 게임 프레임 단계(`game_frames`)는 PresentMon(인텔의 공개 도구)으로 게임이 프레임을 내보낸 시각만 기록합니다. 게임 화면은 보지 않고, 게임 이름이 든 원본 기록은 읽자마자 지웁니다. 저장하는 것은 fps, 1% low, 긴 프레임 수 같은 숫자뿐입니다.
|
||||
- PC 제어 도구 단계는 다른 프로그램의 창 제목과 화면 글자를 저장하지 않습니다. 개수, 걸린 시간, 오류 코드, 계산기 결과 숫자만 저장합니다. 이미 켜 둔 계산기가 있으면 건드리지 않고 그 확인을 건너뛰며, 브라우저는 평소 쓰시는 프로필이 아닌 새 임시 프로필로 엽니다.
|
||||
|
||||
## 옵션 (PowerShell에서 직접 실행할 때)
|
||||
@@ -58,7 +60,7 @@ LLM 단계는 8080 포트를 씁니다. 다른 프로그램이 이 포트를 쓰
|
||||
.\run_m0.ps1 -Only stt,tts_melo -Redo # 특정 단계만 다시
|
||||
```
|
||||
|
||||
단계 이름: `sysinfo`, `winchecks`, `vdisplay`, `tts_melo`, `tts_cosy2`, `tts_cosy3`, `stt`, `llm_fast`, `llm_recommended`, `tools_fast`, `tools_recommended`, `desktop`, `gamemode`, `gamemode_stt`, `gamemode_tune`, `gamemode_live`, `video_drops`(마지막 두 단계는 `-Only`로 고를 때만 돕니다)
|
||||
단계 이름: `sysinfo`, `winchecks`, `vdisplay`, `tts_melo`, `tts_cosy2`, `tts_cosy3`, `stt`, `llm_fast`, `llm_recommended`, `tools_fast`, `tools_recommended`, `desktop`, `gamemode`, `gamemode_stt`, `gamemode_tune`, `gamemode_live`, `video_drops`, `game_frames`(마지막 세 단계는 `-Only`로 고를 때만 돕니다)
|
||||
|
||||
`tools_fast`, `tools_recommended`는 도구 호출만 다시 잽니다. 처음과 같은 방식(고치기 전)과, 자비스의 수정(지시문 규칙, 계획 단계, 건너뛴 도구 직접 실행)을 넣은 방식을 같은 모델로 비교합니다. 이미 받은 모델을 쓰므로 모델 하나에 몇 분이면 끝납니다.
|
||||
|
||||
@@ -76,4 +78,6 @@ LLM 단계는 8080 포트를 씁니다. 다른 프로그램이 이 포트를 쓰
|
||||
|
||||
`run_m0_video.bat`을 더블클릭하면 Jarvis 옆에서 보는 영상이 끊기는지 잽니다(`video_drops`). 크롬(없으면 엣지)이 새 임시 프로필로 떠서 유튜브의 Blender 영상(Big Buck Bunny, 1080p 60fps, 소리 끔)을 틉니다. 광고가 나오면 건너뛸 수 있을 때 건너뛰고 끝날 때까지 기다립니다. 영상을 틀어 둔 채로 (1) Jarvis를 끈 상태 1분, (2) LLM(평소 배치, STT 자리와 여유 1GB)과 STT를 켜 두고 쉬는 상태 1분, (3) LLM이 답하고 STT가 10명 동시 전사를 쉬지 않고 하는 상태를 GPU 95% 제한 없이 두 번, 제한을 켜고 두 번, (4) 다시 Jarvis를 끈 상태 1분을 잽니다. 각각 끊긴 프레임 수(유튜브 "전문가용 통계"의 Dropped Frames와 같은 값)와, 화면에 프레임이 50ms·100ms 넘게 안 바뀐 횟수를 셉니다. 10~15분 걸립니다. 그동안 브라우저 창을 가리거나 최소화하지 말고 PC에 손대지 않으시면 됩니다. 광고, 일시정지, 버퍼링, 건너뛰기가 끼었거나 화질이 1080p 60fps가 아니었던 구간, 제한을 켰는데 Jarvis를 실제로 멈추지 못한 구간은 기록만 하고 비교에서는 뺍니다. 영상이 멈추거나 유튜브 오류가 뜨거나 페이지가 새로고침되면 키트가 알아서 다시 틀고(재생을 누르고, 안 되면 페이지를 다시 엽니다) 그 구간을 최대 3번까지 다시 잽니다. 직접 새로고침하지 않으셔도 됩니다. 화질이나 프레임 수가 떨어진 구간은 GPU 탓일 수 있어서 다시 재지 않습니다.
|
||||
|
||||
`run_m0_game_frames.bat`을 더블클릭하면 Jarvis가 게임 화면을 끊기게 하는지 잽니다(`game_frames`). 게임 프레임은 Windows 이벤트 기록으로 재므로 처음에 관리자 권한을 묻는 창이 한 번 뜹니다. 게임을 켜기 전에 "예"를 눌러 주세요. 그다음 게임을 켜고 플레이 화면까지 들어간 뒤 Enter를 누르고 15초 안에 게임으로 돌아가면 됩니다. 끝날 때까지 캐릭터를 한자리에 세워 두고 마우스·키보드에 손대지 마세요. 창마다 1분씩 (1) 게임만, (2) Jarvis(게임 중 배치)를 켜 두고 쉴 때, (3) LLM이 답하고 음성 인식이 최종 전사를 하는 동안을 그냥 두 번, Jarvis의 CPU·GPU 우선순위를 낮춰 게임에 양보하게 하고 두 번, (4) 다시 게임만 잽니다. 10~15분 걸리고 끝나면 알림음이 울립니다. 게임이 앞에 없었던 구간, 게임이 프레임을 거의 내지 않은 구간, 우선순위가 실제로 낮아지지 않은 양보 구간은 기록만 하고 비교에서 뺍니다. 프레임 제한(수직 동기화나 fps 제한)은 평소 쓰시는 대로 두셔도 됩니다.
|
||||
|
||||
다 끝나고 필요 없으면 작업 폴더(`\JarvisM0`)를 통째로 지우면 됩니다. Python, 모델, Strata(엔진과 설정 포함)는 모두 그 안에 설치하도록 지정해 두었습니다.
|
||||
|
||||
@@ -55,14 +55,18 @@ STAGES = [
|
||||
# the cap is off by default until this shows it helps). Opens YouTube, so only when asked for.
|
||||
("video_drops", "영상 끊김: Jarvis를 돌리는 동안 유튜브 영상의 끊긴 프레임", "base", "llm.py",
|
||||
["model=fast", "only=video"], 0),
|
||||
# Does Jarvis make the user's game drop frames, and does yielding by priority help (the cap is gone,
|
||||
# 2026-11-11). Needs the user's game and administrator rights (event tracing), so only when asked for.
|
||||
("game_frames", "게임 프레임: Jarvis를 돌리는 동안 게임 화면의 프레임 (직접 게임을 켜 주셔야 합니다)", "base", "llm.py",
|
||||
["model=fast", "only=frames"], 0),
|
||||
# With the user's game running. Needs someone at the PC, so only when asked for (--only).
|
||||
("gamemode_live", "게임을 켠 상태로 재기 (직접 게임을 켜 주셔야 합니다)", "base", "llm.py",
|
||||
["model=fast", "only=live"], 0),
|
||||
]
|
||||
# Stages left out of a full run: they need the user there after the unattended part, or open a website.
|
||||
ON_DEMAND = {"gamemode_live", "video_drops"}
|
||||
ON_DEMAND = {"gamemode_live", "video_drops", "game_frames"}
|
||||
# Stages that start the STT worker next to the LLM and need the stt venv's Python.
|
||||
NEEDS_STT = {"gamemode_stt", "gamemode_tune", "gamemode_live", "video_drops"}
|
||||
NEEDS_STT = {"gamemode_stt", "gamemode_tune", "gamemode_live", "video_drops", "game_frames"}
|
||||
|
||||
|
||||
def free_gb(path: Path) -> float:
|
||||
|
||||
485
scripts/m0/m0lib/frames.py
Normal file
485
scripts/m0/m0lib/frames.py
Normal file
@@ -0,0 +1,485 @@
|
||||
"""Stage part: does Jarvis make the user's game drop frames, and does yielding to the game help?
|
||||
|
||||
The GPU cap is gone (2026-11-11): pausing Jarvis for 5 % of every 200 ms did not lower nvidia-smi's
|
||||
utilisation, cost 6 to 10 % of the LLM's output, and a YouTube video next to a busy Jarvis did not
|
||||
stutter with or without it. What is left unmeasured is a game. So this stage measures what the user
|
||||
would notice, the game's own frame times, instead of a GPU-wide number:
|
||||
|
||||
- PresentMon (Intel's open-source tool, pinned by hash) records when the game presents each frame
|
||||
through Windows' event tracing. It sees no picture; the kit keeps only the frame-time numbers and
|
||||
deletes its CSV (which names the game) right after reading it. Event tracing needs administrator
|
||||
rights, so this stage runs elevated (run_m0_game_frames.bat asks once, before the game is started).
|
||||
- Windows: the game alone, Jarvis loaded and idle, Jarvis busy in the game placement the app uses
|
||||
(the LLM answering, the STT doing only finals, as decided for a game), and busy while Jarvis yields:
|
||||
its processes at below-normal CPU priority and below-normal GPU scheduling priority. Unlike the cap
|
||||
nothing is paused; Windows hands the game the cores and the GPU first only when it wants them. The
|
||||
busy rows run A B B A (plain, yield, yield, plain) so a slow drift does not count for one side.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import csv
|
||||
import hashlib
|
||||
import io
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from m0lib import gpucap, llm
|
||||
from m0lib.common import IS_WINDOWS, StageResult, gpu_memory_mib, interactive, ask, say, stats, wait_enter
|
||||
from m0lib.llm_bench import KOREAN_PROMPTS, NO_THINK, VOICE_SYSTEM
|
||||
from m0lib.llm_client import ChatClient
|
||||
|
||||
PRESENTMON_VERSION = "2.6.0"
|
||||
PRESENTMON_URL = (f"https://github.com/GameTechDev/PresentMon/releases/download/v{PRESENTMON_VERSION}/"
|
||||
f"PresentMon-{PRESENTMON_VERSION}-x64.exe")
|
||||
PRESENTMON_SHA256 = "b2a706bc6ad475749e3b7e3409263aa1e6906d45bdcf993f6dbc0f660188f1af"
|
||||
WINDOW_S = 60
|
||||
SETTLE_S = 5 # Jarvis busy (or yielding) before a window starts
|
||||
HITCH_MS = (50, 100) # a frame this long is a visible hitch
|
||||
MIN_FRAMES = 300 # under this in a minute (5 fps) the game was not really drawing
|
||||
ROUNDS = ("busy", "busy_yield", "busy_yield_2", "busy_2")
|
||||
# The foreground window belongs to one of these when the user did not get back to the game.
|
||||
NOT_A_GAME = {"windowsterminal.exe", "openconsole.exe", "conhost.exe", "cmd.exe", "powershell.exe",
|
||||
"pwsh.exe", "explorer.exe"}
|
||||
# Windows' values: process priority classes and D3DKMT_SCHEDULINGPRIORITYCLASS.
|
||||
CPU_NORMAL, CPU_BELOW_NORMAL = 0x20, 0x4000
|
||||
GPU_NORMAL, GPU_BELOW_NORMAL = 2, 1
|
||||
GAME_LEVEL = next(lv for lv in llm.TOGETHER if lv["key"] == "game_experts_2g_16k_stt_gpu")
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# Windows: rights, the game's process, priorities.
|
||||
|
||||
def is_admin() -> bool:
|
||||
if not IS_WINDOWS:
|
||||
return False
|
||||
import ctypes
|
||||
try:
|
||||
return bool(ctypes.windll.shell32.IsUserAnAdmin())
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def foreground_process() -> tuple[int, str] | None:
|
||||
"""(pid, exe file name in lower case) of the window in front, or None."""
|
||||
if not IS_WINDOWS:
|
||||
return None
|
||||
import ctypes
|
||||
from ctypes import wintypes
|
||||
user32 = ctypes.WinDLL("user32")
|
||||
k32 = ctypes.WinDLL("kernel32")
|
||||
user32.GetForegroundWindow.restype = ctypes.c_void_p
|
||||
user32.GetWindowThreadProcessId.argtypes = [ctypes.c_void_p, ctypes.POINTER(wintypes.DWORD)]
|
||||
k32.OpenProcess.restype = ctypes.c_void_p
|
||||
k32.OpenProcess.argtypes = [wintypes.DWORD, wintypes.BOOL, wintypes.DWORD]
|
||||
k32.QueryFullProcessImageNameW.argtypes = [ctypes.c_void_p, wintypes.DWORD, wintypes.LPWSTR,
|
||||
ctypes.POINTER(wintypes.DWORD)]
|
||||
k32.CloseHandle.argtypes = [ctypes.c_void_p]
|
||||
hwnd = user32.GetForegroundWindow()
|
||||
if not hwnd:
|
||||
return None
|
||||
pid = wintypes.DWORD()
|
||||
user32.GetWindowThreadProcessId(hwnd, ctypes.byref(pid))
|
||||
h = k32.OpenProcess(0x1000, False, pid.value) # PROCESS_QUERY_LIMITED_INFORMATION
|
||||
name = ""
|
||||
if h:
|
||||
try:
|
||||
buf = ctypes.create_unicode_buffer(1024)
|
||||
n = wintypes.DWORD(len(buf))
|
||||
if k32.QueryFullProcessImageNameW(h, 0, buf, ctypes.byref(n)):
|
||||
name = Path(buf.value).name.lower()
|
||||
finally:
|
||||
k32.CloseHandle(h)
|
||||
return pid.value, name
|
||||
|
||||
|
||||
_API: dict = {}
|
||||
|
||||
|
||||
def _api() -> dict:
|
||||
import ctypes
|
||||
from ctypes import wintypes
|
||||
if not _API:
|
||||
k32 = ctypes.WinDLL("kernel32", use_last_error=True)
|
||||
gdi = ctypes.WinDLL("gdi32")
|
||||
k32.OpenProcess.restype = ctypes.c_void_p
|
||||
k32.OpenProcess.argtypes = [wintypes.DWORD, wintypes.BOOL, wintypes.DWORD]
|
||||
k32.CloseHandle.argtypes = [ctypes.c_void_p]
|
||||
k32.GetPriorityClass.argtypes = [ctypes.c_void_p]
|
||||
k32.GetPriorityClass.restype = wintypes.DWORD
|
||||
k32.SetPriorityClass.argtypes = [ctypes.c_void_p, wintypes.DWORD]
|
||||
gdi.D3DKMTGetProcessSchedulingPriorityClass.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_int)]
|
||||
gdi.D3DKMTGetProcessSchedulingPriorityClass.restype = ctypes.c_long
|
||||
gdi.D3DKMTSetProcessSchedulingPriorityClass.argtypes = [ctypes.c_void_p, ctypes.c_int]
|
||||
gdi.D3DKMTSetProcessSchedulingPriorityClass.restype = ctypes.c_long
|
||||
_API.update(k32=k32, gdi=gdi, ctypes=ctypes)
|
||||
return _API
|
||||
|
||||
|
||||
def get_priority(pid: int) -> tuple[int | None, int | None]:
|
||||
"""(CPU priority class, GPU scheduling priority class) of a process; None where unreadable."""
|
||||
a = _api()
|
||||
h = a["k32"].OpenProcess(0x0200 | 0x1000, False, pid) # SET_INFORMATION | QUERY_LIMITED
|
||||
if not h:
|
||||
return None, None
|
||||
try:
|
||||
cpu = a["k32"].GetPriorityClass(h) or None
|
||||
val = a["ctypes"].c_int()
|
||||
gpu = val.value if a["gdi"].D3DKMTGetProcessSchedulingPriorityClass(h, a["ctypes"].byref(val)) == 0 else None
|
||||
return cpu, gpu
|
||||
finally:
|
||||
a["k32"].CloseHandle(h)
|
||||
|
||||
|
||||
def set_priority(pid: int, cpu: int | None, gpu: int | None) -> tuple[bool, bool]:
|
||||
"""Set what is given; (CPU done, GPU done)."""
|
||||
a = _api()
|
||||
h = a["k32"].OpenProcess(0x0200 | 0x1000, False, pid)
|
||||
if not h:
|
||||
return False, False
|
||||
try:
|
||||
ok_cpu = cpu is None or bool(a["k32"].SetPriorityClass(h, cpu))
|
||||
ok_gpu = gpu is None or a["gdi"].D3DKMTSetProcessSchedulingPriorityClass(h, gpu) == 0
|
||||
return ok_cpu, ok_gpu
|
||||
finally:
|
||||
a["k32"].CloseHandle(h)
|
||||
|
||||
|
||||
class Yield:
|
||||
"""Jarvis's processes at below-normal CPU and GPU priority while the block runs, put back after.
|
||||
Each lowering is read back, so a priority Windows ignored does not count as lowered."""
|
||||
|
||||
def __init__(self, pids: list[int]):
|
||||
self.pids = list(pids)
|
||||
self.before: dict[int, tuple[int | None, int | None]] = {}
|
||||
self.cpu_lowered = self.gpu_lowered = self.restored = 0
|
||||
self.not_restored: list[int] = []
|
||||
|
||||
def __enter__(self) -> "Yield":
|
||||
for pid in self.pids:
|
||||
self.before[pid] = get_priority(pid)
|
||||
set_priority(pid, CPU_BELOW_NORMAL, GPU_BELOW_NORMAL)
|
||||
cpu, gpu = get_priority(pid)
|
||||
self.cpu_lowered += cpu == CPU_BELOW_NORMAL
|
||||
self.gpu_lowered += gpu == GPU_BELOW_NORMAL
|
||||
return self
|
||||
|
||||
def __exit__(self, *exc) -> None:
|
||||
for pid, (cpu, gpu) in self.before.items():
|
||||
ok = set_priority(pid, cpu or CPU_NORMAL, GPU_NORMAL if gpu is None else gpu)
|
||||
if all(ok):
|
||||
self.restored += 1
|
||||
else:
|
||||
self.not_restored.append(pid)
|
||||
|
||||
def summary(self) -> dict:
|
||||
return {"processes": len(self.pids), "cpu_lowered": self.cpu_lowered, "gpu_lowered": self.gpu_lowered,
|
||||
"restored": self.restored, "not_restored": len(self.not_restored)}
|
||||
|
||||
|
||||
def yield_problems(summary: dict) -> list[str]:
|
||||
"""Why a yielding window did not yield: no process, nothing lowered, or a priority not put back."""
|
||||
out = []
|
||||
if not summary.get("processes"):
|
||||
out.append("yield found no Jarvis process")
|
||||
elif not summary.get("cpu_lowered") and not summary.get("gpu_lowered"):
|
||||
out.append("yield lowered no priority")
|
||||
if summary.get("not_restored"):
|
||||
out.append(f"{summary['not_restored']} process(es) kept the lowered priority")
|
||||
return out
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# PresentMon and the frame times.
|
||||
|
||||
def ensure_presentmon(work: Path) -> Path:
|
||||
exe = work / "tools" / f"PresentMon-{PRESENTMON_VERSION}-x64.exe"
|
||||
if exe.exists() and hashlib.sha256(exe.read_bytes()).hexdigest() == PRESENTMON_SHA256:
|
||||
return exe
|
||||
exe.parent.mkdir(parents=True, exist_ok=True)
|
||||
part = exe.with_suffix(".part")
|
||||
with urllib.request.urlopen(PRESENTMON_URL, timeout=120) as resp:
|
||||
part.write_bytes(resp.read())
|
||||
got = hashlib.sha256(part.read_bytes()).hexdigest()
|
||||
if got != PRESENTMON_SHA256:
|
||||
part.unlink()
|
||||
raise RuntimeError(f"PresentMon download has the wrong hash ({got[:12]})")
|
||||
part.replace(exe)
|
||||
return exe
|
||||
|
||||
|
||||
def presentmon_cmd(exe: Path, pid: int, seconds: int, out_csv: Path) -> list[str]:
|
||||
return [str(exe), "--process_id", str(pid), "--output_file", str(out_csv), "--timed", str(seconds),
|
||||
"--terminate_after_timed", "--no_console_stats", "--v1_metrics", "--no_track_input",
|
||||
"--session_name", "JarvisM0", "--stop_existing_session"]
|
||||
|
||||
|
||||
def capture(exe: Path, pid: int, seconds: int, out_csv: Path, log: Path) -> tuple[list[dict], str | None]:
|
||||
"""The game's frames over ``seconds`` (rows of PresentMon's CSV) and an error, if any. The CSV
|
||||
names the game, so it is deleted as soon as it is read."""
|
||||
out_csv.unlink(missing_ok=True)
|
||||
with open(log, "a", encoding="utf-8") as f:
|
||||
try:
|
||||
p = subprocess.run(presentmon_cmd(exe, pid, seconds, out_csv), stdout=f, stderr=subprocess.STDOUT,
|
||||
timeout=seconds + 60)
|
||||
code = p.returncode
|
||||
except subprocess.TimeoutExpired:
|
||||
code = "timeout"
|
||||
try:
|
||||
text = out_csv.read_text(encoding="utf-8", errors="replace")
|
||||
except OSError:
|
||||
return [], f"PresentMon wrote no frames (exit {code})"
|
||||
finally:
|
||||
out_csv.unlink(missing_ok=True)
|
||||
return parse_csv(text), None if code == 0 else f"PresentMon exited with {code}"
|
||||
|
||||
|
||||
def parse_csv(text: str) -> list[dict]:
|
||||
"""PresentMon's rows with lower-case column names (1.x writes msBetweenPresents, 2.x MsBetweenPresents)."""
|
||||
return [{(k or "").strip().lower(): (v or "").strip() for k, v in row.items()}
|
||||
for row in csv.DictReader(io.StringIO(text))]
|
||||
|
||||
|
||||
def _num(value: str) -> float | None:
|
||||
try:
|
||||
return float(value)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
def frame_stats(rows: list[dict], seconds: float) -> dict:
|
||||
"""Frame rate, 1 % low (the 99th-percentile frame time as a rate) and hitches of the game's main
|
||||
swap chain (the one with the most frames)."""
|
||||
chains: dict[str, list[dict]] = {}
|
||||
for x in rows:
|
||||
chains.setdefault(x.get("swapchainaddress", ""), []).append(x)
|
||||
main = max(chains.values(), key=len) if chains else []
|
||||
ms = [v for v in (_num(x.get("msbetweenpresents")) for x in main[1:]) if v is not None and v > 0]
|
||||
out: dict[str, Any] = {"seconds": round(seconds, 1), "frames": len(ms)}
|
||||
if not ms:
|
||||
return {**out, "valid": False, "invalid_because": ["the game drew no frames"]}
|
||||
srt = sorted(ms)
|
||||
p99 = srt[min(len(srt) - 1, int(len(srt) * 0.99))]
|
||||
out.update(fps=round(len(ms) / (sum(ms) / 1000), 1), low1_fps=round(1000 / p99, 1),
|
||||
p99_ms=round(p99, 2), max_ms=round(srt[-1], 1),
|
||||
hitches_over_ms={str(h): sum(v > h for v in ms) for h in HITCH_MS},
|
||||
not_shown=sum(x.get("dropped") == "1" for x in main[1:]))
|
||||
reasons = []
|
||||
if len(ms) < MIN_FRAMES * seconds / 60:
|
||||
reasons.append(f"the game drew only {len(ms)} frames")
|
||||
out["valid"] = not reasons
|
||||
if reasons:
|
||||
out["invalid_because"] = reasons
|
||||
return out
|
||||
|
||||
|
||||
def compare(rows: list[dict]) -> dict:
|
||||
"""The game alone, next to busy Jarvis and next to yielding Jarvis, over the valid windows, with
|
||||
the change against the game alone in per cent."""
|
||||
def pool(kind: str) -> dict:
|
||||
valid = [x["game"] for x in rows if x.get("kind") == kind and (x.get("game") or {}).get("valid")]
|
||||
if not valid:
|
||||
return {"windows": 0}
|
||||
n = len(valid)
|
||||
return {"windows": n, "fps": round(sum(v["fps"] for v in valid) / n, 1),
|
||||
"low1_fps": round(sum(v["low1_fps"] for v in valid) / n, 1),
|
||||
**{f"hitches_over_{h}ms": sum(v["hitches_over_ms"][str(h)] for v in valid) for h in HITCH_MS}}
|
||||
|
||||
out = {k: pool(k) for k in ("off", "busy", "busy_yield")}
|
||||
off = out["off"]
|
||||
for k in ("busy", "busy_yield"):
|
||||
if off["windows"] and out[k]["windows"]:
|
||||
out[k]["vs_off_pct"] = {m: round((out[k][m] - off[m]) / off[m] * 100, 1) for m in ("fps", "low1_fps")}
|
||||
return out
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# Jarvis busy for as long as a window lasts.
|
||||
|
||||
class BusyLlm:
|
||||
"""The LLM answering back to back (a long answer, then a voice answer) until the block ends."""
|
||||
|
||||
def __init__(self, c: ChatClient):
|
||||
self.c = c
|
||||
self.stop = threading.Event()
|
||||
self.decode: list[float] = []
|
||||
self.voice: list[float] = []
|
||||
self.errors: list[str] = []
|
||||
self.thread = threading.Thread(target=self._loop, daemon=True)
|
||||
|
||||
def _loop(self) -> None:
|
||||
voice = [p for kind, p in KOREAN_PROMPTS if kind == "voice"]
|
||||
i = 0
|
||||
while not self.stop.is_set():
|
||||
try:
|
||||
a = self.c.chat([{"role": "user", "content": f"({i}) 한국의 사계절을 각각 세 문장씩 자세히 설명해 줘."}],
|
||||
max_tokens=320, extra=NO_THINK)
|
||||
if a.get("decode_tok_s"):
|
||||
self.decode.append(a["decode_tok_s"])
|
||||
if voice and not self.stop.is_set():
|
||||
a = self.c.chat([{"role": "system", "content": VOICE_SYSTEM},
|
||||
{"role": "user", "content": voice[i % len(voice)]}], max_tokens=120, extra=NO_THINK)
|
||||
if a.get("first_sentence") is not None:
|
||||
self.voice.append(a["first_sentence"])
|
||||
except Exception as e:
|
||||
self.errors.append(f"{type(e).__name__}")
|
||||
if len(self.errors) > 20:
|
||||
return
|
||||
time.sleep(1)
|
||||
i += 1
|
||||
|
||||
def __enter__(self) -> "BusyLlm":
|
||||
self.thread.start()
|
||||
return self
|
||||
|
||||
def __exit__(self, *exc) -> None:
|
||||
self.stop.set()
|
||||
self.thread.join(timeout=120)
|
||||
|
||||
def summary(self) -> dict:
|
||||
return {"decode_tok_s": stats(self.decode), "voice_first_sentence": stats(self.voice),
|
||||
"errors": sorted(set(self.errors))}
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# The stage.
|
||||
|
||||
def ask_for_a_game() -> bool:
|
||||
if not interactive():
|
||||
return False
|
||||
say(" 게임을 켠 채로 게임 화면의 프레임을 잽니다. 10~15분 걸립니다.")
|
||||
if ask(" 지금 게임을 켜고 재시겠습니까?") != "y":
|
||||
return False
|
||||
wait_enter(" 게임을 켜고 플레이 화면까지 들어간 뒤 Enter를 누르고, 15초 안에 게임으로 돌아가 주세요. "
|
||||
"끝날 때까지 캐릭터를 한자리에 세워 두고 마우스·키보드에 손대지 말아 주세요. 끝나면 알림음이 울립니다:")
|
||||
time.sleep(15)
|
||||
return True
|
||||
|
||||
|
||||
def measure(r: StageResult, s: "llm.Strata", preset: dict, context: int, stt_python: str) -> None:
|
||||
work = s.log.parent.parent
|
||||
pm_log = s.log.with_name("presentmon.log")
|
||||
rows: list[dict] = []
|
||||
r["frame_rows"] = rows
|
||||
if not is_admin():
|
||||
r.error("admin", "게임 프레임은 관리자 권한이 있어야 잴 수 있습니다. run_m0_game_frames.bat으로 실행해 주세요.")
|
||||
return
|
||||
try:
|
||||
pm = ensure_presentmon(work)
|
||||
except Exception as e:
|
||||
r.error("presentmon", e)
|
||||
return
|
||||
r["presentmon"] = PRESENTMON_VERSION
|
||||
if not ask_for_a_game():
|
||||
r["skipped"] = "no game started"
|
||||
return
|
||||
fg = foreground_process()
|
||||
if fg is None or fg[1] in NOT_A_GAME:
|
||||
r.error("game", "게임 창이 앞에 없었습니다. 게임으로 돌아간 뒤 다시 실행해 주세요.")
|
||||
return
|
||||
game = fg[0]
|
||||
r.note("measured while the user's game was running (its name is not kept)")
|
||||
base = f"http://127.0.0.1:{llm.PORT}/v1"
|
||||
|
||||
def window(key: str, kind: str) -> dict:
|
||||
x: dict = {"key": key, "kind": kind}
|
||||
used = gpu_memory_mib()
|
||||
if used:
|
||||
x["vram_free_mib"] = used[1] - used[0]
|
||||
with gpucap.UtilSampler() as util:
|
||||
frames, err = capture(pm, game, WINDOW_S, work / "cache" / "frames.csv", pm_log)
|
||||
g = frame_stats(frames, WINDOW_S)
|
||||
g["gpu_util"] = util.summary()
|
||||
now = foreground_process()
|
||||
if now is not None and now[0] != game:
|
||||
g["valid"] = False
|
||||
g["invalid_because"] = g.get("invalid_because", []) + ["the game was not in front at the end"]
|
||||
if err:
|
||||
r.error(f"frames {key}", err)
|
||||
x["game"] = g
|
||||
rows.append(x)
|
||||
if not g["valid"]:
|
||||
r.note(f"{key}: not counted ({'; '.join(g['invalid_because'])})")
|
||||
say(f" {key}: {g.get('fps')} fps, 1% low {g.get('low1_fps')} fps, "
|
||||
f"{HITCH_MS[0]}ms 넘는 프레임 {(g.get('hitches_over_ms') or {}).get(str(HITCH_MS[0]))}개")
|
||||
r.save()
|
||||
return x
|
||||
|
||||
stt = None
|
||||
try:
|
||||
say(" - 게임만")
|
||||
window("game_only", "off")
|
||||
|
||||
say(" - Jarvis 켜기 (게임 중 배치: 전문가 2GB, 맥락 16K, 음성 인식 GPU)")
|
||||
s.stop()
|
||||
stt = llm.SttLoad(stt_python, work, "cuda", s.log.with_name("stt_load.log"), pace=llm._FINALS_ONLY)
|
||||
load = stt.start()
|
||||
r["stt_load"] = load
|
||||
if load.get("status") != "loaded":
|
||||
r.error("frames stt", "; ".join(load.get("errors") or []) or "the STT did not load")
|
||||
return
|
||||
lv = GAME_LEVEL
|
||||
r["placement"] = {"key": lv["key"], "sets": lv["sets"], "context": lv["context"], "parallel": lv["parallel"],
|
||||
"vision": lv["vision"], "stt_pace": llm._FINALS_ONLY}
|
||||
launch = s.launch(preset, lv["context"], parallel=lv["parallel"], vision=lv["vision"],
|
||||
extra_sets=list(lv["sets"]), timeout=3600)
|
||||
if not launch.get("ready"):
|
||||
r.error("frames llm", "the game placement did not start")
|
||||
return
|
||||
c = ChatClient(base)
|
||||
r["server"] = {"model": c.model}
|
||||
c.chat([{"role": "user", "content": "안녕"}], max_tokens=8)
|
||||
|
||||
say(" - Jarvis 켜 두고 쉬는 중")
|
||||
window("jarvis_idle", "idle")
|
||||
|
||||
stt.go()
|
||||
for key in ROUNDS:
|
||||
yielding = "yield" in key
|
||||
say(f" - Jarvis가 답하고 음성 인식이 최종 전사를 하는 중{' (게임에 양보)' if yielding else ''}")
|
||||
pids = llm.cap_pids(s, stt) if yielding else []
|
||||
y = Yield(pids)
|
||||
with y if yielding else contextlib.nullcontext(), BusyLlm(c) as busy:
|
||||
time.sleep(SETTLE_S)
|
||||
x = window(key, "busy_yield" if yielding else "busy")
|
||||
x["llm"] = busy.summary()
|
||||
for err in x["llm"]["errors"]:
|
||||
r.error(f"frames {key} llm", err)
|
||||
if yielding:
|
||||
x["yield"] = y.summary()
|
||||
problems = yield_problems(x["yield"])
|
||||
for why in problems:
|
||||
r.error(f"frames {key} yield", why)
|
||||
if problems:
|
||||
x["game"]["valid"] = False
|
||||
x["game"]["invalid_because"] = x["game"].get("invalid_because", []) + problems
|
||||
r.save()
|
||||
busy_stt = stt.finish()
|
||||
r["stt_busy"] = {k: busy_stt.get(k) for k in ("status", "single_s", "errors", "final_wait_s")
|
||||
if k in busy_stt}
|
||||
for err in llm.stt_gaps(busy_stt, llm._FINALS_ONLY):
|
||||
r.error("frames stt", err)
|
||||
stt.close()
|
||||
stt = None
|
||||
s.stop()
|
||||
|
||||
say(" - Jarvis 끄고 게임만 다시")
|
||||
window("game_only_again", "off")
|
||||
r["comparison"] = compare(rows)
|
||||
except Exception as e:
|
||||
r.error("frames", e)
|
||||
finally:
|
||||
if stt:
|
||||
stt.close()
|
||||
s.stop()
|
||||
say("\a 게임 프레임 측정이 끝났습니다. 게임을 꺼도 됩니다.")
|
||||
try:
|
||||
import winsound
|
||||
winsound.MessageBeep()
|
||||
except Exception:
|
||||
pass
|
||||
@@ -15,6 +15,8 @@ Options (--opt key=value):
|
||||
while the LLM answers, and both with Jarvis capped at 95 % of the GPU
|
||||
only=video a YouTube video's dropped frames with Jarvis off, idle and busy (the
|
||||
normal placement), busy also with the 95 % GPU cap (video.py)
|
||||
only=frames the user's game's frame times with Jarvis off, idle and busy (the game
|
||||
placement), busy also yielding to the game by priority (frames.py)
|
||||
stt_python=PATH the Python of the stt venv (m0.py passes it)
|
||||
"""
|
||||
|
||||
@@ -663,7 +665,7 @@ def measure_external(r: StageResult, url: str, context: int, only: str = "") ->
|
||||
if only == "tools":
|
||||
tools_only(r, c)
|
||||
return
|
||||
if only in LEVEL_SETS or only == "video":
|
||||
if only in LEVEL_SETS or only in ("video", "frames"):
|
||||
r.note("placements need Strata; not measured against an external server")
|
||||
return
|
||||
with GpuSampler() as g:
|
||||
@@ -682,7 +684,7 @@ def measure_strata(r: StageResult, work: Path, which: str, context: int, only: s
|
||||
return
|
||||
r["plan"] = s.plan(preset, context)
|
||||
r.save()
|
||||
if only in LEVEL_SETS or only == "video":
|
||||
if only in LEVEL_SETS or only in ("video", "frames"):
|
||||
s.stop()
|
||||
if port_in_use(PORT):
|
||||
r.error("port", f"다른 프로그램이 {PORT} 포트를 쓰고 있습니다. 그 프로그램을 끄고 다시 실행해 주세요.")
|
||||
@@ -694,6 +696,10 @@ def measure_strata(r: StageResult, work: Path, which: str, context: int, only: s
|
||||
from m0lib import video
|
||||
video.measure(r, s, preset, context, stt_python)
|
||||
return
|
||||
if only == "frames":
|
||||
from m0lib import frames
|
||||
frames.measure(r, s, preset, context, stt_python)
|
||||
return
|
||||
if only == "live":
|
||||
if not ask_for_a_game():
|
||||
r["skipped"] = "no game started"
|
||||
@@ -778,7 +784,8 @@ def main() -> int:
|
||||
context = int(opts.get("context", "32768"))
|
||||
only = opts.get("only", "")
|
||||
name = {"tools": f"tools_{which}", "levels": "gamemode", "together": "gamemode_stt",
|
||||
"live": "gamemode_live", "tune": "gamemode_tune", "video": "video_drops"}.get(only, f"llm_{which}")
|
||||
"live": "gamemode_live", "tune": "gamemode_tune", "video": "video_drops",
|
||||
"frames": "game_frames"}.get(only, f"llm_{which}")
|
||||
r = StageResult(name, out)
|
||||
try:
|
||||
if opts.get("url"):
|
||||
|
||||
12
scripts/m0/run_m0_game_frames.bat
Normal file
12
scripts/m0/run_m0_game_frames.bat
Normal file
@@ -0,0 +1,12 @@
|
||||
@echo off
|
||||
chcp 65001 >nul
|
||||
rem The game's frame times come from Windows event tracing, which needs administrator rights.
|
||||
rem Ask once here, before the game is started, so no prompt pops up over the game.
|
||||
net session >nul 2>&1
|
||||
if errorlevel 1 (
|
||||
powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
|
||||
exit /b
|
||||
)
|
||||
powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0run_m0.ps1" -Only game_frames -Redo %*
|
||||
echo.
|
||||
pause
|
||||
Reference in New Issue
Block a user