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.

test(examples): the demo's cap must be derived, forwarded and reported

Aashish481a279ede21
diff --git a/tests/test_example_server_limits.py b/tests/test_example_server_limits.py
index 49f62bd7..eba2c081 100644
--- a/tests/test_example_server_limits.py
+++ b/tests/test_example_server_limits.py
@@ -8,19 +8,27 @@ examples/server.py bounded `states` to 64 and left the rest open: 20 000 questio
a 5 MB state were both accepted where the shipped server answers 413. The bounds are
read from laya.serve rather than restated, so the two cannot drift.
-Scope: the question count and the state size, answered 413 as laya.serve answers them.
-`Question.instructions` and `criteria` still carry unbounded text that no per-field
-bound can see; capping the request body is the backstop for those and is left out
-deliberately -- see the PR description.
+Scope: the question count and the state size, answered 413 as laya.serve answers them, and
+the resident-checkpoint cap the app's Router is built with. `Question.instructions` and
+`criteria` still carry unbounded text that no per-field bound can see; capping the request
+body is the backstop for those and is left out deliberately -- see the PR description.
Driven over HTTP through TestClient. No weights are loaded; the router stays unbuilt, so
-a request that passes validation answers 503, which is the assertion for "accepted".
+a request that passes validation answers 503, which is the assertion for "accepted". The
+one arm that needs a Router enters the lifespan, which is also the arm that checks the cap.
+
+On `main` this file is 19 checks, all of them about request size. It is now 37: the other
+eighteen follow the cap from the environment to the constructor, to the running Router, to
+`/health` in both JSON and HTML, and back out through the `--reload` push. The first
+nineteen are untouched.
Run: python tests/test_example_server_limits.py
"""
+import importlib
import json
import os
import sys
+import types
os.environ.setdefault("TOKENIZERS_PARALLELISM", "false")
os.environ.setdefault("LAYA_PRELOAD", "0")
@@ -49,6 +57,9 @@ def main():
print("SKIP: fastapi/httpx not installed -- pip install laya[serve] httpx")
return 0
try:
+ # examples/server.py reads the environment at import, so the cap under test has to
+ # be absent/present *before* the import, never patched after it.
+ os.environ.pop("LAYA_MAX_LOADED", None)
import server as demo
except ImportError as exc:
if (getattr(exc, "name", None) or "").split(".")[0] not in ("fastapi", "httpx", "starlette", "multipart"):
@@ -132,6 +143,100 @@ def main():
ok("the page and health endpoints are unaffected",
client.get("/").status_code == 200 and client.get("/health").status_code == 200)
+ # --- the resident-checkpoint cap: derived, not copied -------------------
+ # examples/server.py used to build its Router with `max_loaded=1`, a copy of a default
+ # laya/router.py retired in #180. A cap of one cannot hold both english and
+ # multilingual, so the demo ran the churn #172 measured and fixed -- one checkpoint
+ # rebuilt per alternating-language request -- while the library and laya.serve did not.
+ # What is asserted here is that the demo asks for nothing unless asked to, and that
+ # what it then reports is the number the running Router holds.
+ from laya.router import Router
+
+ ok("an unset LAYA_MAX_LOADED means 'not asked for', not a number of this file's own",
+ demo._CFG["max_loaded"] is None, repr(demo._CFG["max_loaded"]))
+ ok("so the key never reaches the constructor",
+ "max_loaded" not in demo._router_kwargs(demo._CFG),
+ repr(demo._router_kwargs(demo._CFG)))
+ # Before a Router exists the page has no resident count to state. This is the check
+ # that fails if the page goes back to guessing one (`cfg.get('max_loaded', 1)`).
+ ok("and while loading, the page states no resident count",
+ "kept in memory" not in client.get("/health", headers={"accept": "text/html"}).text,
+ client.get("/health", headers={"accept": "text/html"}).text[:200])
+
+ with client: # the lifespan builds the Router; preload is off
+ # The witness that this arm needed no weights: LAYA_PRELOAD=0 above means the
+ # Router the app built holds no checkpoint yet, so the cap is the only thing here
+ # that could have been loaded.
+ ok("building it downloaded nothing",
+ not demo.ROUTER._agents, repr(sorted(demo.ROUTER._agents)))
+ cap = demo.ROUTER.max_loaded
+ ok("the running Router holds laya's own default",
+ cap == Router().max_loaded, "demo %r vs laya %r" % (cap, Router().max_loaded))
+ ok("which is the two #180 settled on, not the one before it",
+ cap > 1, repr(cap))
+ ok("/health reports the cap the Router really holds",
+ client.get("/health").json()["config"]["max_loaded"] == cap, repr(cap))
+ ok("and the HTML page prints that same number",
+ ("up to %d kept in memory" % cap)
+ in client.get("/health", headers={"accept": "text/html"}).text, repr(cap))
+
+ def with_cap(value):
+ """Re-import the demo the way uvicorn starts it, with LAYA_MAX_LOADED set to `value`."""
+ if value is None:
+ os.environ.pop("LAYA_MAX_LOADED", None)
+ else:
+ os.environ["LAYA_MAX_LOADED"] = value
+ return importlib.reload(demo)
+
+ # The default moving must not take the operator's own number away with it.
+ for raw, want in (("3", 3), ("1", 1), (" 4 ", 4)):
+ fresh = with_cap(raw)
+ with TestClient(fresh.app) as c:
+ ok("LAYA_MAX_LOADED=%r still reaches the Router" % raw,
+ fresh.ROUTER.max_loaded == want, repr(fresh.ROUTER.max_loaded))
+ ok("LAYA_MAX_LOADED=%r still shows up in /health" % raw,
+ c.get("/health").json()["config"]["max_loaded"] == want, raw)
+ fresh = with_cap(None)
+ from laya.serve import build_router
+
+ ok("the demo and laya.serve leave the cap to the same place",
+ fresh._router_kwargs(fresh._CFG).get("max_loaded", Router().max_loaded)
+ == build_router().max_loaded,
+ "%r vs %r" % (fresh._router_kwargs(fresh._CFG), build_router().max_loaded))
+
+ # --- the --reload env push has to survive the round trip ----------------
+ # With reload=True uvicorn re-imports `server:app` in a child process and only the
+ # environment crosses over. Writing str(None) for "not asked for" would land on the
+ # int() that reads LAYA_MAX_LOADED in that child and stop the server at import, so the
+ # unset case must push nothing at all. uvicorn is replaced so main() never binds a port.
+ real_uvicorn = sys.modules.get("uvicorn")
+ started = {}
+ fake = types.ModuleType("uvicorn")
+ fake.run = lambda target, **kw: started.update(target=target)
+ sys.modules["uvicorn"] = fake
+ argv = sys.argv
+
+ def start(args):
+ with_cap(None)
+ sys.argv = ["server.py"] + args
+ demo.main()
+ return os.environ.get("LAYA_MAX_LOADED", "(absent)")
+
+ try:
+ ok("--reload with no flag pushes no cap, so the reimport can read it",
+ start(["--reload"]) == "(absent)", repr(started))
+ ok("--reload --max-loaded 3 pushes 3",
+ start(["--reload", "--max-loaded", "3"]) == "3", repr(started))
+ ok("without --reload nothing is pushed at all",
+ start([]) == "(absent)", repr(started))
+ finally:
+ sys.argv = argv
+ if real_uvicorn is not None:
+ sys.modules["uvicorn"] = real_uvicorn
+ else:
+ sys.modules.pop("uvicorn", None)
+ with_cap(None) # leave the module as the rest of the file found it
+
print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))
for f in FAIL:
print(" FAIL " + f)