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.
Python calibrationclassificationdecision-modelhuggingfacejevmodernbertmultilingualnlppythonpytorchroutingtyped-decisionszero-shot
feat(router): reach the Agent options a Router never forwards
Aashish37e7376287bb
diff --git a/laya/router.py b/laya/router.pyindex 7dd999eb..e7e9ab4c 100644--- a/laya/router.py+++ b/laya/router.py@@ -28,6 +28,7 @@ primary routing signal.synthetic workflows and should not be a silent default."""import gc+import inspectimport jsonimport osimport threading@@ -174,6 +175,40 @@ def _english_from_code(value: Any) -> Optional[bool]:return primary in _ENGLISH_SUBTAGS+# Checkpoint options a Router will not accept through `agent_kwargs`: the ones it sets for itself+# on every `Agent(...)` it builds, plus the hook family, which has its own home in `Router(hooks=)`+# and would otherwise fire from two registries at once.+_ROUTER_OWNED_AGENT_ARGS = frozenset({+ "model_id_or_path", "device", "token", "subfolder", "revision",+ "hooks", "on_predict_start", "on_predict_end", "hooks_raise", "hooks_concurrent",+ "hooks_timeout",+})+++def check_agent_kwargs(agent_kwargs: Dict[str, Any]) -> None:+ """Reject `agent_kwargs` names a Router cannot honour, before any checkpoint is loaded.++ The accepted names are read out of `Agent.__init__`'s signature rather than written down here:+ the whole point of `agent_kwargs` is that this file stops keeping its own list of checkpoint+ options, so a hard-coded copy of them would go stale in exactly the way this argument removes.+ The import is only reached when a caller passes the argument, which is also the moment torch is+ about to be loaded anyway.+ """+ from .agent import Agent++ accepted = set(inspect.signature(Agent.__init__).parameters) - {"self"}+ reserved = sorted(set(agent_kwargs) & _ROUTER_OWNED_AGENT_ARGS)+ if reserved:+ raise ValueError(+ "Router sets %s itself; pass them to Router(...) instead of in agent_kwargs"+ % (", ".join(repr(n) for n in reserved),))+ unknown = sorted(set(agent_kwargs) - accepted)+ if unknown:+ raise ValueError(+ "Agent accepts none of %s; it accepts %s"+ % (", ".join(repr(n) for n in unknown), sorted(accepted - _ROUTER_OWNED_AGENT_ARGS)))++class Router(HookRegistry):"""Lazily loads Laya checkpoints and sends each request to the right one.@@ -207,6 +242,18 @@ class Router(HookRegistry):which is useful when standalone repositories were reviewed at different commits.Without either, huggingface_hub's normal default and existing offline cache are used.+ Anything else `laya.Agent` accepts is reachable through `agent_kwargs`, which is merged into+ every checkpoint the Router builds:++ Router(agent_kwargs={"lang_temperatures": {"de": {"temperature": [1.0, 1.4, 2.0]}}})+ Router(agent_kwargs={"expected_sha256": {"model.safetensors": "a3f1..."}})+ Router(agent_kwargs={"fast": True})++ The names the Router sets for itself -- `model_id_or_path`, `device`, `token`, `subfolder`,+ `revision` and the hook arguments -- are refused here rather than silently shadowed, and the+ remaining names are checked against `Agent.__init__` at construction, so a misspelled option+ fails on the `Router(...)` line instead of on the first request.+Hooks are opt-in and run at the Router level: `on_route` sees the routing decision,`on_load` / `on_evict` see model lifecycle, and `on_predict_start` / `on_predict_end`wrap the whole route+infer call. See `laya.hooks`.@@ -218,6 +265,7 @@ class Router(HookRegistry):hooks_concurrent = Truehooks_timeout = None_hooks_lock = None+ agent_kwargs: Dict[str, Any] = {}def __init__(self,@@ -226,6 +274,7 @@ class Router(HookRegistry):token: Optional[str] = None,revision: Optional[str] = None,revisions: Optional[Dict[str, Optional[str]]] = None,+ agent_kwargs: Optional[Dict[str, Any]] = None,max_loaded: int = 2,default: str = "english",auto_task_detection: bool = False,@@ -257,6 +306,11 @@ class Router(HookRegistry):self.revisions: Dict[str, Optional[str]] = {normalise_name(k): v for k, v in (revisions or {}).items()}+ # Options forwarded to every `Agent` this Router builds, checked now so a bad name fails on+ # this line rather than the first request that happens to load a checkpoint.+ self.agent_kwargs: Dict[str, Any] = dict(agent_kwargs or {})+ if self.agent_kwargs:+ check_agent_kwargs(self.agent_kwargs)self.max_loaded = max(1, int(max_loaded))self.default = normalise_name(default)self.auto_task_detection = bool(auto_task_detection)@@ -292,6 +346,8 @@ class Router(HookRegistry):model_revision = self.revisions.get(key, self.revision)if model_revision is not None:kwargs["revision"] = model_revision+ # Safe to merge last: the names this method just set are refused in `agent_kwargs`.+ kwargs.update(self.agent_kwargs)agent = Agent(repo, **kwargs)self._agents[key] = agentself._order.append(key)diff --git a/tests/test_hooks_api.py b/tests/test_hooks_api.pyindex fa750e30..c3fb236c 100644--- a/tests/test_hooks_api.py+++ b/tests/test_hooks_api.py@@ -68,6 +68,8 @@ for label, fn in (("Agent.__init__", Agent.__init__), ("load", load),# Router keeps lang_guess and explicit per-model revisions toocheck_param("Router.__init__", Router.__init__, "lang_guess", None)check_param("Router.__init__", Router.__init__, "revisions", None)+# ...and the remaining `Agent` options, forwarded to every checkpoint it builds+check_param("Router.__init__", Router.__init__, "agent_kwargs", None)# --------------------------------------------------------------- predict surfacesfor label, fn in (("Agent.predict_batch", Agent.predict_batch),diff --git a/tests/test_revision_pinning.py b/tests/test_revision_pinning.pyindex 176e8dd4..a0c1198f 100644--- a/tests/test_revision_pinning.py+++ b/tests/test_revision_pinning.py@@ -183,5 +183,122 @@ class RouterRevisionTests(unittest.TestCase):self.assertEqual(router.loaded_revisions, {"english": "sha-english"})+class RouterAgentKwargsTests(unittest.TestCase):+ """`agent_kwargs` is how a Router user reaches the rest of `Agent`'s constructor.++ Before it, `Router.load` built every checkpoint with exactly four arguments, so+ `lang_temperatures`, `expected_sha256`, `fast` and `compile` could only be set by giving up the+ Router and hand-building an `Agent` -- which also left the per-language grouping in+ `Router.predict_batch` unable to fire for any agent the Router owned.+ """++ TABLE = {"de": {"temperature": [2.0, 2.0, 2.0], "temperature_by_options": {"choice:2": 3.0}}}++ @staticmethod+ def _fake_agent():+ """(module holding Agent, the fake, the list each build is appended to).++ `check_agent_kwargs` reads the option names out of whatever `laya.agent.Agent` currently+ is, so the stand-in has to carry the real signature or these tests would be refused before+ they ever reached a build.+ """+ import inspect++ import laya.agent++ captured = []++ class FakeAgent:+ def __init__(self, repo, **kwargs):+ captured.append((repo, kwargs))++ __init__.__signature__ = inspect.signature(Agent.__init__)++ return laya.agent, FakeAgent, captured++ def test_agent_kwargs_reach_the_agent_build(self):+ module, fake, captured = self._fake_agent()+ with patch.object(module, "Agent", fake):+ Router(agent_kwargs={"lang_temperatures": self.TABLE}).load("english")+ self.assertEqual(captured[0][1]["lang_temperatures"], self.TABLE)++ def test_applied_to_every_checkpoint_the_router_builds(self):+ module, fake, captured = self._fake_agent()+ with patch.object(module, "Agent", fake):+ router = Router(agent_kwargs={"expected_sha256": {"model.safetensors": "0" * 64}})+ router.load("english")+ router.load("multi")+ self.assertEqual(len(captured), 2)+ for _repo, kwargs in captured:+ self.assertEqual(kwargs["expected_sha256"], {"model.safetensors": "0" * 64})++ def test_passing_nothing_leaves_the_build_exactly_as_it_was(self):+ module, fake, captured = self._fake_agent()+ with patch.object(module, "Agent", fake):+ Router().load("english")+ Router(revision="sha").load("english")+ self.assertEqual(sorted(captured[0][1]), ["device", "subfolder", "token"])+ self.assertEqual(sorted(captured[1][1]), ["device", "revision", "subfolder", "token"])++ def test_a_router_value_is_never_shadowed_by_an_agent_kwarg(self):+ module, fake, captured = self._fake_agent()+ with patch.object(module, "Agent", fake):+ Router(device="cuda", revisions={"english": "sha"},+ agent_kwargs={"lang_temperatures": self.TABLE}).load("english")+ self.assertEqual(captured[0][1]["device"], "cuda")+ self.assertEqual(captured[0][1]["revision"], "sha")++ def test_router_owned_names_are_refused(self):+ owned = ("model_id_or_path", "device", "token", "subfolder", "revision", "hooks",+ "on_predict_start", "on_predict_end", "hooks_raise", "hooks_concurrent",+ "hooks_timeout")+ for name in owned:+ with self.assertRaises(ValueError) as ctx:+ Router(agent_kwargs={name: "x"})+ self.assertIn(name, str(ctx.exception))+ self.assertIn("Router(...)", str(ctx.exception))++ def test_refusal_happens_at_construction_not_at_the_first_load(self):+ module, fake, captured = self._fake_agent()+ with patch.object(module, "Agent", fake):+ with self.assertRaises(ValueError):+ Router(agent_kwargs={"device": "cpu"}).load("english")+ self.assertEqual(captured, [])++ def test_unknown_option_is_refused_with_the_names_that_do_exist(self):+ with self.assertRaises(ValueError) as ctx:+ Router(agent_kwargs={"lang_tempertaures": self.TABLE})+ message = str(ctx.exception)+ self.assertIn("lang_tempertaures", message)+ # The typo is refused, and the accepted list carries the spelling the caller meant.+ self.assertIn("lang_temperatures", message)++ def test_accepted_names_are_read_from_agent_rather_than_copied_here(self):+ """The anti-drift check: this file must not grow its own list of checkpoint options."""+ import inspect++ from laya.router import _ROUTER_OWNED_AGENT_ARGS, check_agent_kwargs++ owned = set(_ROUTER_OWNED_AGENT_ARGS)+ accepted = set(inspect.signature(Agent.__init__).parameters) - {"self"} - owned+ self.assertTrue(accepted, "expected some Agent options to be reachable")+ for name in ("fast", "compile", "expected_sha256", "lang_temperatures"):+ self.assertIn(name, accepted)+ check_agent_kwargs({name: None for name in accepted})++ def test_the_callers_dict_is_copied_not_aliased(self):+ module, fake, captured = self._fake_agent()+ options = {"lang_temperatures": self.TABLE}+ with patch.object(module, "Agent", fake):+ router = Router(agent_kwargs=options)+ options["compile"] = True+ router.load("english")+ self.assertNotIn("compile", captured[0][1])++ def test_default_is_an_empty_build(self):+ self.assertEqual(Router().agent_kwargs, {})+ self.assertEqual(Router(agent_kwargs={}).agent_kwargs, {})++if __name__ == "__main__":unittest.main()