feat(runtime-adapter): load models before serving
A cold engine emits no execution evidence while it loads weights and compiles, so the Gateway's attempt lease expires mid-load, the attempt is fenced, and the next attempt pays the same cost — a loop that never produces audio. Loading every READY model before the socket accepts work moves that cost to startup, where preflight already expects to wait, so the first Execute begins inference immediately. A prewarm failure is reported rather than fatal, and --no-prewarm restores the previous behavior.
This commit is contained in:
@@ -36,6 +36,12 @@ def main(argv: list[str] | None = None) -> int:
|
||||
default=10.0,
|
||||
help="selfcheck RPC timeout in seconds (default: 10)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--no-prewarm",
|
||||
action="store_true",
|
||||
help="serve immediately without loading models first (the first "
|
||||
"execution then pays weight loading and compilation)",
|
||||
)
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
if args.selfcheck:
|
||||
@@ -43,10 +49,16 @@ def main(argv: list[str] | None = None) -> int:
|
||||
|
||||
return selfcheck(timeout_s=args.timeout)
|
||||
|
||||
from .production import build_runtime_context # noqa: PLC0415
|
||||
from .production import build_runtime_context, prewarm_engines # noqa: PLC0415
|
||||
from .server import resolve_socket_path, serve # noqa: PLC0415
|
||||
|
||||
return serve(build_runtime_context(), resolve_socket_path(args.socket))
|
||||
context = build_runtime_context()
|
||||
if not args.no_prewarm:
|
||||
# Deliberately before the socket exists: the Gateway's preflight and
|
||||
# first offer should both find a runtime that can start inference at
|
||||
# once, rather than one that spends an attempt lease compiling.
|
||||
prewarm_engines(context)
|
||||
return serve(context, resolve_socket_path(args.socket))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -6,6 +6,8 @@ torch or the engine registry.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
|
||||
from . import ADAPTER_VERSION
|
||||
from ._paths import ensure_backend_on_path
|
||||
from .inventory import ProductionInventory, slots_per_device
|
||||
@@ -32,3 +34,30 @@ def build_runtime_context() -> RuntimeContext:
|
||||
engine_provider=production_engine_provider,
|
||||
slot_limit=slots,
|
||||
)
|
||||
|
||||
def prewarm_engines(context: RuntimeContext) -> None:
|
||||
"""Load and compile every READY model before the socket accepts work.
|
||||
|
||||
The GPU Gateway leases an attempt for a bounded window and renews it from
|
||||
execution evidence. A cold engine produces no evidence: weight loading and
|
||||
torch compilation can run for minutes emitting nothing, so the lease
|
||||
expires mid-load, the attempt is fenced, the Job requeues, and the next
|
||||
attempt pays the same cost — a loop that never yields audio.
|
||||
|
||||
Paying that cost once at startup, before the adapter is reachable, means
|
||||
the first real Execute begins inference immediately. Preflight already
|
||||
refuses a runtime with no READY model, so a failure here is reported and
|
||||
the model is dropped from the advertised set rather than being offered as
|
||||
schedulable capacity the node cannot actually serve promptly.
|
||||
"""
|
||||
ensure_backend_on_path()
|
||||
for model in context.inventory.models():
|
||||
if model.state != "ready":
|
||||
continue
|
||||
try:
|
||||
context.engine_provider(model.catalog_model_id)
|
||||
except Exception as error: # noqa: BLE001 - reported, never fatal
|
||||
print(
|
||||
f"runtime adapter: prewarm of {model.catalog_model_id} failed: {error}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user