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(router): accept and compose per-call hooks in predict_batch and route_batch (#909)

Swatantra Yadav47da8ce28a66
diff --git a/laya/mcp/remote.py b/laya/mcp/remote.py
index 0631e622..cb6e4bf4 100644
--- a/laya/mcp/remote.py
+++ b/laya/mcp/remote.py
@@ -193,9 +193,14 @@ class RemoteRouter(Router):
self,
requests: Sequence[Dict[str, Any]],
batch_size: Optional[int] = None,
- hooks_timeout: Optional[float] = None,
min_confidence: Optional[float] = None,
sort_by_length: bool = False,
+ *,
+ hooks_timeout: Optional[float] = None,
+ hooks=None,
+ on_predict_start=None,
+ on_predict_end=None,
+ hooks_raise: Optional[bool] = None,
) -> List[Dict[str, Any]]:
"""One ``/v1/systemone`` call per request, answers in input order.
@@ -206,8 +211,9 @@ class RemoteRouter(Router):
keep working. The server applies its own batching inside each call.
"""
results: List[Dict[str, Any]] = []
- if hooks_timeout is not None:
- raise RemoteError("unsupported_remote", "hooks_timeout belongs to the server's hooks")
+ if any(value is not None for value in (hooks, on_predict_start, on_predict_end, hooks_raise, hooks_timeout)):
+ raise RemoteError("unsupported_remote",
+ "hooks are callables and cannot be sent to laya-serve; install them on the server")
for item in requests:
if not isinstance(item, dict):
raise TypeError("predict_batch requests must be dicts, got %s" % type(item).__name__)
diff --git a/laya/router.py b/laya/router.py
index 0d907adb..c70896f8 100644
--- a/laya/router.py
+++ b/laya/router.py
@@ -1167,6 +1167,9 @@ class Router(HookRegistry):
self,
requests: Sequence[Dict[str, Any]],
hooks_timeout: Optional[float] = None,
+ *,
+ hooks=None,
+ hooks_raise: Optional[bool] = None,
) -> List[RouteDecision]:
"""Route a heterogeneous request batch without loading any checkpoints.
@@ -1182,6 +1185,8 @@ class Router(HookRegistry):
``questions``.
hooks_timeout: Override the Router's ``hooks_timeout`` for this call, applied to
every request's ``on_route`` dispatch, as on :meth:`route`.
+ hooks: Per-call hook or sequence of hooks for this call.
+ hooks_raise: Override the Router's ``hooks_raise`` policy for this call.
"""
if not isinstance(requests, SequenceABC) or isinstance(requests, (str, bytes)):
raise TypeError("requests must be a sequence of request dictionaries")
@@ -1210,6 +1215,8 @@ class Router(HookRegistry):
task=request.get("task"),
lang=request.get("lang"),
lang_guess=request.get("lang_guess"),
+ hooks=hooks,
+ hooks_raise=hooks_raise,
hooks_timeout=hooks_timeout,
)
)
@@ -1220,9 +1227,14 @@ class Router(HookRegistry):
self,
requests: Sequence[Dict[str, Any]],
batch_size: Optional[int] = None,
- hooks_timeout: Optional[float] = None,
min_confidence: Optional[float] = None,
sort_by_length: bool = False,
+ *,
+ hooks_timeout: Optional[float] = None,
+ hooks=None,
+ on_predict_start=None,
+ on_predict_end=None,
+ hooks_raise: Optional[bool] = None,
) -> List[Dict[str, Any]]:
"""Route and execute a heterogeneous request batch with minimal model churn.
@@ -1256,16 +1268,21 @@ class Router(HookRegistry):
overrides.
batch_size: Optional maximum number of states per Agent forward-pass batch.
hooks_timeout: Override the Router's ``hooks_timeout`` for this call.
+ min_confidence: Optional float or per-bucket mapping for confidence gating.
sort_by_length: Forwarded to every ``Agent.predict_batch`` call, so each question
group pads to a shorter maximum; see ``Agent.predict_batch``. Results retain the
input order either way. Silently dropped for an attached agent whose
``predict_batch`` predates the knob (#294).
+ hooks: Per-call hook or sequence of hooks for this call.
+ on_predict_start: Plain callable or sequence of callables for start events.
+ on_predict_end: Plain callable or sequence of callables for end events.
+ hooks_raise: Override the Router's ``hooks_raise`` policy for this call.
Returns:
One normal Router prediction result per request, in the same order as the input.
"""
mc = check_min_confidence(min_confidence) if min_confidence is not None else None
- decisions = self.route_batch(requests, hooks_timeout=hooks_timeout)
+ decisions = self.route_batch(requests, hooks=hooks, hooks_raise=hooks_raise, hooks_timeout=hooks_timeout)
if not decisions:
return []
@@ -1288,8 +1305,8 @@ class Router(HookRegistry):
# list alone silently dropped every process-wide default from the batched path while
# keeping them on `predict`, so a default audit or metrics hook saw no Router-level event
# for a request that arrived through `predict_batch`.
- active = compose_hooks(self.hooks)
- raise_errors = self.hooks_raise
+ active = compose_hooks(self.hooks, hooks, on_predict_start, on_predict_end)
+ raise_errors = self.hooks_raise if hooks_raise is None else bool(hooks_raise)
timeout = self.hooks_timeout if hooks_timeout is None else validate_timeout(hooks_timeout)
for model_name, indices in groups.items():
diff --git a/tests/test_hooks.py b/tests/test_hooks.py
index f4d32f77..817a738c 100644
--- a/tests/test_hooks.py
+++ b/tests/test_hooks.py
@@ -1608,6 +1608,91 @@ check_raises("timeout/route_batch rejects a zero per-call timeout", ValueError,
lambda: Router().route_batch([req("a")], hooks_timeout=0))
+# ------------------------------------------- per-call hooks on Router.predict_batch and route_batch (#909)
+per_call_events = []
+
+
+class PerCallTrace:
+ def on_predict_start(self, ctx):
+ per_call_events.append(("start", ctx.states[0]))
+
+ def on_predict_end(self, ctx):
+ per_call_events.append(("end", ctx.states[0]))
+
+ def on_route(self, ctx):
+ per_call_events.append(("route", ctx.states[0]))
+
+
+r_batch, en_batch, _ = batch_router()
+
+# 1. per-call hooks on predict_batch
+r_batch.predict_batch([req("call1"), req("call2")], hooks=[PerCallTrace()])
+check("router_batch/per-call hooks execute for all requests",
+ per_call_events,
+ [("route", "call1"), ("route", "call2"),
+ ("start", "call1"), ("start", "call2"),
+ ("end", "call2"), ("end", "call1")])
+
+per_call_events.clear()
+
+# 2. per-call convenience callables on predict_start and predict_end
+seen_starts = []
+seen_ends = []
+r_batch.predict_batch([req("cb1")],
+ on_predict_start=lambda c: seen_starts.append(c.states[0]),
+ on_predict_end=lambda c: seen_ends.append(c.states[0]))
+check("router_batch/per-call callables execute", (seen_starts, seen_ends), (["cb1"], ["cb1"]))
+
+# 3. per-call hooks on route_batch
+route_trace = []
+
+
+class RouteTrace:
+ def on_route(self, ctx):
+ route_trace.append(ctx.states[0])
+
+
+r_batch.route_batch([req("rb1"), req("rb2")], hooks=[RouteTrace()])
+check("router_batch/route_batch per-call hooks execute", route_trace, ["rb1", "rb2"])
+
+# 4. per-call hooks_raise policy
+class FailingStartHook:
+ def on_predict_start(self, ctx):
+ raise ValueError("failing hook")
+
+
+check_raises("router_batch/hooks_raise=True propagates exception",
+ ValueError,
+ lambda: r_batch.predict_batch([req("fail1")], hooks=[FailingStartHook()], hooks_raise=True))
+
+with warnings.catch_warnings(record=True) as _warns:
+ warnings.simplefilter("always")
+ res = r_batch.predict_batch([req("warn1")], hooks=[FailingStartHook()], hooks_raise=False)
+ check("router_batch/hooks_raise=False returns result", len(res), 1)
+ check_true("router_batch/hooks_raise=False warns", any("failing hook" in str(w.message) for w in _warns))
+
+# 5. positional and keyword-only hook controls (#909 review)
+r_pos, _, _ = batch_router()
+
+# route_batch keeps hooks_timeout positional-or-keyword
+pos_decisions = r_pos.route_batch([req("pos1")], 1.0)
+check("router_batch/route_batch positional hooks_timeout", len(pos_decisions), 1)
+
+# route_batch takes hooks as keyword-only
+kw_decisions = r_pos.route_batch([req("pos1")], 1.0, hooks=[RouteTrace()])
+check("router_batch/route_batch keyword-only hooks", len(kw_decisions), 1)
+
+# predict_batch keeps hooks_timeout keyword-only
+pos_results = r_pos.predict_batch([req("pos2")], 8, hooks_timeout=1.0, hooks=[PerCallTrace()])
+check("router_batch/predict_batch keyword-only controls", len(pos_results), 1)
+
+# keyword-only enforcement: passing hook controls positionally raises TypeError
+check_raises("router_batch/route_batch rejects positional hooks", TypeError,
+ lambda: r_pos.route_batch([req("pos1")], 1.0, [RouteTrace()]))
+check_raises("router_batch/predict_batch rejects positional hooks_timeout past sort_by_length", TypeError,
+ lambda: r_pos.predict_batch([req("pos2")], 8, None, False, 1.0))
+
+
# --------------------------------------------------------------- report
print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))
for f_ in FAIL:
diff --git a/tests/test_hooks_api.py b/tests/test_hooks_api.py
index aa5fb0ac..5c1643cb 100644
--- a/tests/test_hooks_api.py
+++ b/tests/test_hooks_api.py
@@ -23,7 +23,7 @@ import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import laya # noqa: E402
-from laya import Agent, AsyncHook, BaseHook, PredictContext, PredictHook, Router, load # noqa: E402
+from laya import Agent, BaseHook, PredictContext, PredictHook, Router, load # noqa: E402
from laya.hooks import HOOK_EVENTS, Hook # noqa: E402
from laya.onnx_agent import ONNXAgent # noqa: E402
from laya.router import _question_schema # noqa: E402
@@ -135,6 +135,7 @@ for label, fn in (("Agent.predict_batch", Agent.predict_batch),
("Agent.system_one", Agent.system_one),
("Agent.predict_long", Agent.predict_long),
("Router.predict", Router.predict),
+ ("Router.predict_batch", Router.predict_batch),
("Router.predict_long", Router.predict_long),
("ONNXAgent.system_one", ONNXAgent.system_one)):
check_param(label, fn, "hooks", None)
@@ -165,8 +166,6 @@ for label, fn in (("Agent.predict_batch", Agent.predict_batch),
# Router.predict_batch has no call-level budget: a heterogeneous batch sets it per request, and
# the request keys `max_len` / `head_max_len` are read into each request's PredictContext.
-check_param("Router.predict_batch", Router.predict_batch, "batch_size", None)
-check_param("Router.predict_batch", Router.predict_batch, "hooks_timeout", None)
for param in ("max_len", "head_max_len"):
check("Router.predict_batch/%s is per-request, not a call argument" % param,
param in sig(Router.predict_batch), False)
@@ -176,10 +175,26 @@ for param in ("max_len", "head_max_len"):
check_param("shortlist_choice", laya.shortlist_choice, "return_scores", False,
inspect.Parameter.KEYWORD_ONLY)
-# route() takes per-call hooks so a hook can pin a checkpoint for one call
-check_param("Router.route", Router.route, "hooks", None)
-check_param("Router.route", Router.route, "hooks_raise", None)
-check_param("Router.route", Router.route, "hooks_timeout", None)
+# route() and route_batch() take per-call hooks so a hook can pin a checkpoint for one call
+for label, fn in (("Router.route", Router.route),
+ ("Router.route_batch", Router.route_batch)):
+ check_param(label, fn, "hooks", None)
+ check_param(label, fn, "hooks_raise", None)
+ check_param(label, fn, "hooks_timeout", None)
+
+# Positional compatibility: hook parameters are keyword-only to protect positional callers
+check("Router.route_batch/positional prefix",
+ [p.name for p in sig(Router.route_batch).values() if p.kind != inspect.Parameter.KEYWORD_ONLY],
+ ["requests", "hooks_timeout"])
+check("Router.predict_batch/positional prefix",
+ [p.name for p in sig(Router.predict_batch).values() if p.kind != inspect.Parameter.KEYWORD_ONLY],
+ ["requests", "batch_size", "min_confidence", "sort_by_length"])
+for param in ("hooks", "hooks_raise"):
+ check("Router.route_batch/%s is keyword-only" % param,
+ sig(Router.route_batch)[param].kind, inspect.Parameter.KEYWORD_ONLY)
+for param in ("hooks_timeout", "hooks", "on_predict_start", "on_predict_end", "hooks_raise"):
+ check("Router.predict_batch/%s is keyword-only" % param,
+ sig(Router.predict_batch)[param].kind, inspect.Parameter.KEYWORD_ONLY)
# --------------------------------------------------------------- aliases
check_true("Agent.predict is Agent.system_one", Agent.predict is Agent.system_one)