Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

Merge pull request #574 from aashish254/feat-serve-health

Nandakishorf964afd5301b
diff --cc laya/serve.py
index 05a2223f,3bd40391..a751f5b5
--- a/laya/serve.py
+++ b/laya/serve.py
@@@ -11,26 -11,30 +11,31 @@@ probe
Configuration is entirely via environment variables so the same entry point
serves a laptop dev run and a systemd unit:
-====================== ============================================ =========
-env var meaning default
-====================== ============================================ =========
-``LAYA_HOST`` bind address 0.0.0.0
-``LAYA_PORT`` bind port 8000
-``LAYA_DEVICE`` torch device for every checkpoint (auto)
-``LAYA_PRELOAD`` build the checkpoints at startup, not lazily 1
-``LAYA_MODELS`` comma list to preload (english,multilingual, (all)
- typed-decisions); empty = every checkpoint
-``LAYA_THREADS`` cap torch intra-op threads (CPU inference). (torch
- Keep <= physical cores; oversubscribing the default)
- logical/hyperthread count is a large regression.
-``LAYA_AUTO_TASK`` auto-route to the typed-decisions checkpoint 0
-``LAYA_API_KEY`` if set, require ``Authorization: Bearer <it>`` (none)
-``LAYA_LOG_LEVEL`` uvicorn log level info
-``LAYA_MAX_CONCURRENT`` cap on requests past auth at once; excess 16
- gets 503 (see below)
-====================== ============================================ =========
+========================= ============================================ =========
+env var meaning default
+========================= ============================================ =========
+``LAYA_HOST`` bind address 0.0.0.0
+``LAYA_PORT`` bind port 8000
+``LAYA_DEVICE`` torch device for every checkpoint (auto)
+``LAYA_PRELOAD`` build the checkpoints at startup, not lazily 1
+``LAYA_MODELS`` comma list to preload (english,multilingual, (all)
+ typed-decisions); empty = every checkpoint
+``LAYA_THREADS`` cap torch intra-op threads (CPU inference). (torch
+ Keep <= physical cores; oversubscribing the default)
+ logical/hyperthread count is a large regression.
+``LAYA_AUTO_TASK`` auto-route to the typed-decisions checkpoint 0
+``LAYA_API_KEY`` if set, require ``Authorization: Bearer <it>`` (none)
+``LAYA_LOG_LEVEL`` uvicorn log level info
+``LAYA_MAX_CONCURRENT`` cap on requests past auth at once; excess 16
+ gets 503 (see below)
+``LAYA_MAX_TOKEN_BUDGET`` cap on per-request max_len / head_max_len 8192
+========================= ============================================ =========
+ ``LAYA_DEVICE`` is a preference, not a guarantee: an ``Agent`` that asks for a
+ GPU it cannot get falls back to CPU silently and keeps answering correctly.
+ ``/health`` reports where a resident checkpoint actually computes, using the
+ same helpers the MCP status tool uses (``laya.mcp.device``).
+
Imports of heavy dependencies (fastapi, uvicorn, torch via Router) are all
deferred into the functions that need them, so ``import laya.serve`` stays cheap
and touches no GPU -- which is what keeps the Nix ``pythonImportsCheck`` honest.
@@@ -346,26 -317,24 +357,38 @@@ def create_app(router: Optional[Any] =
@app.get("/health")
def health() -> Dict[str, Any]:
- from .mcp.device import router_agent
+ # `device` is where a resident checkpoint really computes, not what was asked for:
+ # `Agent.device` reflects the silent GPU -> CPU fallback, so a container that asked
+ # for a GPU it did not get says so. With nothing resident it is the configured
+ # preference, and `device_is_preference` tells the reader which of the two it is
+ # looking at. Same convention, and the same three keys, as `laya_status` over MCP.
+ checkpoint_devices: Dict[str, str] = {}
+ for name in (router.loaded or []):
+ device = agent_device(router_agent(router, name))
+ if device:
+ checkpoint_devices[name] = device
+ actual = next(iter(checkpoint_devices.values()), None)
+
+ # Per-agent CPU-fallback counters, read through the same side-effect-free
+ # accessor the MCP status tool uses -- never Router.load(), which reorders the
+ # LRU and rebuilds evicted checkpoints. Every access is getattr-guarded so
+ # injected routers predating the counters stay health-compatible, like `revisions`.
+ # This counts the *scoped* fallback in _infer (a request that demotes to CPU and
+ # restores the device); a checkpoint built on CPU because the GPU was never
+ # available is reported by `device`, not here -- see #574.
+ fallbacks = {}
+ for name in router.loaded or []:
+ agent = router_agent(router, name)
+ fallbacks[name] = {"count": getattr(agent, "cpu_fallback_count", 0),
+ "last_reason": getattr(agent, "last_fallback_reason", None)}
return {
"status": "ok",
"loaded": router.loaded,
"revisions": getattr(router, "loaded_revisions", {}),
- "device": os.environ.get("LAYA_DEVICE") or "auto",
+ "device": actual or resolve_device(),
+ "device_is_preference": actual is None,
+ "checkpoint_devices": checkpoint_devices,
+ "cpu_fallbacks": fallbacks,
}
@app.post("/v1/systemone")
diff --cc tests/test_serve.py
index 55046ee2,b905afd1..fb7575dd
--- a/tests/test_serve.py
+++ b/tests/test_serve.py
@@@ -11,8 -15,9 +15,10 @@@ import pytes
fastapi = pytest.importorskip("fastapi")
from fastapi.testclient import TestClient # noqa: E402
+ ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+
from laya.serve import ( # noqa: E402
+ DEFAULT_MAX_TOKEN_BUDGET,
MAX_BODY_BYTES,
_apply_thread_limit,
_env_bool,