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.

fix(serve): read the checkpoints a client may name from laya.router

Aashishceb6db710107
diff --git a/laya/serve.py b/laya/serve.py
index ed08d39a..38a23ca2 100644
--- a/laya/serve.py
+++ b/laya/serve.py
@@ -56,11 +56,6 @@ from typing import Any, Dict, Optional
# propagates there without this module setting up any handlers.
_log = logging.getLogger("laya.serve")
-# The three checkpoint names the router understands; used to decide whether a
-# client's `model` field names a Laya checkpoint (honour it) or is some other
-# Jev model id (ignore it and let the router auto-select).
-_KNOWN_MODELS = {"english", "multilingual", "typed-decisions"}
-
# Guardrails for unauthenticated remote input. The state is tokenized once per
# question and collated into one tensor, so an unbounded body can OOM the worker;
# the single-worker pool means one large request would also starve /health.
@@ -78,13 +73,6 @@ MAX_TOTAL_OPTIONS = 512
DEFAULT_MAX_CONCURRENT = 16
# Server-side ceiling on per-request max_len/head_max_len token budget overrides.
DEFAULT_MAX_TOKEN_BUDGET = 8192
-# Public Hugging Face ids, accepted so a client can name a checkpoint. The root bundle is
-# deliberately absent: the documented ``convaiinnovations/laya`` value means
-# "let the Router choose", rather than pinning the English checkpoint.
-_PUBLISHED_MODEL_IDS = {
- "convaiinnovations/laya-multilingual": "multilingual",
- "convaiinnovations/laya-typed-decisions": "typed-decisions",
-}
def _env_bool(name: str, default: bool) -> bool:
@@ -94,23 +82,39 @@ def _env_bool(name: str, default: bool) -> bool:
return v.strip().lower() in ("1", "true", "yes", "on")
+def _published_model_ids() -> Dict[str, str]:
+ """Public Hugging Face ids accepted so a client can name a checkpoint.
+
+ Inverted from `laya.router.STANDALONE_MODELS` at call time rather than typed out: the
+ ids a client may send belong to that registry, and a copy here would silently auto-route
+ a checkpoint the router already knows. The root bundle is deliberately absent -- the
+ documented ``convaiinnovations/laya`` value means "let the Router choose", rather than
+ pinning the English checkpoint. Imported here rather than at module scope, the way this
+ module's other `laya.router` use does, so ``import laya.serve`` stays cheap.
+ """
+ from .router import BUNDLE_REPO, STANDALONE_MODELS
+
+ return {repo: name for name, repo in STANDALONE_MODELS.items() if repo != BUNDLE_REPO}
+
+
def _resolve_model(model: Optional[str]) -> Optional[str]:
"""Map a client's `model` field onto a Laya checkpoint, or None to auto-route."""
if not model:
return None
- published = _PUBLISHED_MODEL_IDS.get(str(model).strip().lower())
+ published = _published_model_ids().get(str(model).strip().lower())
if published is not None:
return published
from .router import normalise_name
# normalise_name raises ValueError on anything that is not a known checkpoint
- # or alias. A Jev client's `model` field (e.g. "jev-1") is expected to miss;
- # treat that as "no explicit checkpoint" and let the router auto-select.
+ # or alias, and returns a name from router.DEFAULT_MODELS when it does accept one --
+ # so it is the only list of accepted names this needs. A Jev client's `model` field
+ # (e.g. "jev-1") is expected to miss; treat that as "no explicit checkpoint" and let
+ # the router auto-select.
try:
- key = normalise_name(model)
+ return normalise_name(model)
except Exception:
return None
- return key if key in _KNOWN_MODELS else None
def _resolve_max_concurrent() -> int:
diff --git a/tests/test_serve.py b/tests/test_serve.py
index dab066ab..8d63958f 100644
--- a/tests/test_serve.py
+++ b/tests/test_serve.py
@@ -387,6 +387,44 @@ def test_helpers():
assert _env_bool("X_FLAG", True) is True
+def test_resolve_model_follows_the_router_registry(monkeypatch):
+ """A checkpoint added to laya.router must become pinnable over HTTP with no edit here.
+
+ serve used to keep two hand-copies of that registry -- a set of accepted names and a map of
+ published Hugging Face ids. Either copy left behind means a client that names a real
+ checkpoint is auto-routed instead, and the response reports whichever model did answer, so
+ the dropped pin is invisible from the outside. Only the name maps are read; nothing loads a
+ checkpoint.
+ """
+ from laya import router as router_mod
+
+ monkeypatch.setitem(router_mod.DEFAULT_MODELS, "spanish", (router_mod.BUNDLE_REPO, "spanish"))
+ monkeypatch.setitem(router_mod.STANDALONE_MODELS, "spanish", "convaiinnovations/laya-spanish")
+ monkeypatch.setitem(router_mod._ALIASES, "es", "spanish")
+
+ # The engine accepts the new name and its alias, which is the whole contract serve needs.
+ assert router_mod.normalise_name("spanish") == "spanish"
+ assert router_mod.normalise_name("es") == "spanish"
+
+ for named in ("spanish", "SPANISH", "es", "convaiinnovations/laya-spanish"):
+ assert _resolve_model(named) == "spanish", named
+
+ # And the pin reaches inference rather than stopping in the resolver.
+ client, fake = _client(monkeypatch)
+ r = client.post("/v1/systemone", json={**REQ, "model": "spanish"})
+ assert r.status_code == 200, r.status_code
+ assert fake.calls[-1]["model"] == "spanish", fake.calls[-1]
+
+ # Every standalone id the router publishes pins its checkpoint here -- except the bundle
+ # repo, whose documented meaning stays "let the Router choose".
+ for name, repo in router_mod.STANDALONE_MODELS.items():
+ want = None if repo == router_mod.BUNDLE_REPO else name
+ assert _resolve_model(repo) is want, repo
+
+ # A name the router does not know is still the caller's own model id: ignored, not an error.
+ assert _resolve_model("jev-1") is None
+
+
def test_thread_limit(monkeypatch):
pytest.importorskip("torch")
monkeypatch.delenv("LAYA_THREADS", raising=False)