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
Merge pull request #568 from aashish254/feat-router-agent-kwargs
Nandakishor0d21f4a7d058
diff --cc laya/router.pyindex 5c8c3b1d,e7e9ab4c..fe54b99b--- a/laya/router.py+++ b/laya/router.py@@@ -175,40 -175,40 +176,74 @@@ def _english_from_code(value: Any) -> Oreturn primary in _ENGLISH_SUBTAGS+class _ScanLong:+ """Last start hook of `Router.predict_long`: score the routed state with the agent's+ `predict_long` rather than leave `predict` to call `system_one` on one window of it.++ It is appended after every other start hook, so a caller's hook has already had its say: one+ that answered (`ctx.skip(...)`) or rewrote the state/questions wins, and only what is left is+ scanned. The `lang` rule is `predict`'s -- an explicit `lang=`, else the language routing+ detected -- repeated here because `predict` computes it locally for its own call.+ """++ def __init__(self, window, stride, aggregate, batch_size, lang):+ self.window = window+ self.stride = stride+ self.aggregate = aggregate+ self.batch_size = batch_size+ self.lang = lang++ def on_predict_start(self, ctx):+ if ctx.results is not None:+ return+ if not hasattr(ctx.agent, "predict_long"):+ raise TypeError(+ "%s has no predict_long, so a state longer than its window cannot be scanned; "+ "the PyTorch Agent implements it, and an ONNX or hand-attached agent needs it too"+ % type(ctx.agent).__name__+ )+ lang = self.lang+ if lang is None:+ lang = (ctx.decision.get("detection") or {}).get("language")+ ctx.results = [ctx.agent.predict_long(+ ctx.states[0], ctx.questions, window=self.window, stride=self.stride,+ aggregate=self.aggregate, batch_size=self.batch_size, lang=lang)]+++ # 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.