105 lines
4.3 KiB
Python
105 lines
4.3 KiB
Python
"""Public GPU summary. Never expose scheduler job IDs or host PID details."""
|
||
|
||
import math
|
||
from datetime import datetime, timezone
|
||
|
||
|
||
def summarize(gpus, scheduler, now, generated):
|
||
active, queued = [], None
|
||
try:
|
||
stamp = scheduler["updated_at"]
|
||
if (
|
||
type(stamp) not in (int, float)
|
||
or not math.isfinite(stamp)
|
||
or not now - 5 <= stamp <= now + 1
|
||
):
|
||
raise ValueError("Stale scheduler")
|
||
if scheduler.get("schema_version") == 2:
|
||
entries = scheduler.get("active_jobs", [])
|
||
if not isinstance(entries, list) or len(entries) > len(gpus):
|
||
raise ValueError("Invalid active list")
|
||
indexes = set()
|
||
for entry in entries:
|
||
primary = {"index": entry["gpu_index"], "uuid": entry["gpu_uuid"]}
|
||
members = entry.get("members", [primary])
|
||
if not isinstance(members, list) or len(members) not in (1, 2) or members[0] != primary:
|
||
raise ValueError("Invalid lease members")
|
||
for member in members:
|
||
index, uuid = member["index"], member["uuid"]
|
||
if (type(index) is not int or index in indexes or not any(
|
||
row["index"] == index and row["_uuid"] == uuid for row in gpus)):
|
||
raise ValueError("Invalid lease")
|
||
indexes.add(index)
|
||
active.append(dict(entry, gpu_index=index, gpu_uuid=uuid))
|
||
count = scheduler.get("queued_count")
|
||
if count is not None and (type(count) is not int or not 0 <= count <= 10000000):
|
||
raise ValueError("Invalid queue count")
|
||
queued = count
|
||
elif scheduler.get("state") == "leased":
|
||
lease = scheduler.get("lease") or {}
|
||
active = [
|
||
{"gpu_index": lease.get("index"), "gpu_uuid": lease.get("uuid"), "state": "running"}
|
||
]
|
||
except (KeyError, TypeError, ValueError):
|
||
scheduler, active, queued = None, [], None
|
||
active_indexes = set()
|
||
for entry in active:
|
||
gpu = next(
|
||
(
|
||
row
|
||
for row in gpus
|
||
if row["index"] == entry.get("gpu_index") and row["_uuid"] == entry.get("gpu_uuid")
|
||
),
|
||
None,
|
||
)
|
||
if gpu is None:
|
||
continue
|
||
active_indexes.add(gpu["index"])
|
||
state = entry.get("state")
|
||
gpu.update(
|
||
busy=True,
|
||
eligible=False,
|
||
state=state if state in {"preempting", "blocked", "launching"} else "running",
|
||
stage=entry.get("stage")
|
||
if entry.get("stage")
|
||
in {"launching", "executing", "cleanup", "model", "upscale", "collecting"}
|
||
else None,
|
||
)
|
||
logical_jobs = len(entries) if scheduler and scheduler.get("schema_version") == 2 else len(active_indexes)
|
||
running = logical_jobs + sum(bool(row["_own"]) and row["index"] not in active_indexes for row in gpus)
|
||
available = sum(row["eligible"] for row in gpus)
|
||
state = "running" if running else "ready" if available else "waiting"
|
||
reason = (
|
||
f"正在运行{running}个GPU任务,{available}张卡可接新任务。"
|
||
if running
|
||
else None
|
||
if available
|
||
else "等待可用GPU,外部任务优先。"
|
||
)
|
||
dispatch_blocked = bool(scheduler and scheduler.get("state") == "blocked")
|
||
if dispatch_blocked:
|
||
reason = "执行器需要管理员检查,新任务暂停派发。"
|
||
if not running:
|
||
state = "blocked"
|
||
available = 0
|
||
for gpu in gpus:
|
||
gpu["eligible"] = False
|
||
if gpu["state"] == "ready":
|
||
gpu["state"] = "waiting"
|
||
elif scheduler and "memory" in str(scheduler.get("reason", "")).lower():
|
||
reason = "共享内存不足,任务按入队顺序等待资源。"
|
||
for gpu in gpus:
|
||
gpu.pop("_uuid")
|
||
gpu.pop("_own")
|
||
return {
|
||
"available": True,
|
||
"state": state,
|
||
"reason": reason,
|
||
"gpus": gpus,
|
||
"running_count": running,
|
||
"available_count": available,
|
||
"queued_count": queued,
|
||
"dispatch_blocked": dispatch_blocked,
|
||
"updated_at": datetime.fromtimestamp(generated, timezone.utc).isoformat(),
|
||
}
|