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): forward the per-call hooks_timeout through route_batch

Bruce-Yiid3f0dfe05bb9
diff --git a/laya/router.py b/laya/router.py
index 7dd999eb..3353b044 100644
--- a/laya/router.py
+++ b/laya/router.py
@@ -657,7 +657,11 @@ class Router(HookRegistry):
system_one = predict
- def route_batch(self, requests: Sequence[Dict[str, Any]]) -> List[RouteDecision]:
+ def route_batch(
+ self,
+ requests: Sequence[Dict[str, Any]],
+ hooks_timeout: Optional[float] = None,
+ ) -> List[RouteDecision]:
"""Route a heterogeneous request batch without loading any checkpoints.
Each request is a mapping with ``state`` and ``questions`` plus the same optional
@@ -666,6 +670,12 @@ class Router(HookRegistry):
This is intentionally separate from inference so callers can inspect or aggregate
routing decisions before paying model-load cost.
+
+ Args:
+ requests: Sequence of request dictionaries, each requiring ``state`` and
+ ``questions``.
+ hooks_timeout: Override the Router's ``hooks_timeout`` for this call, applied to
+ every request's ``on_route`` dispatch, as on :meth:`route`.
"""
if not isinstance(requests, SequenceABC) or isinstance(requests, (str, bytes)):
raise TypeError("requests must be a sequence of request dictionaries")
@@ -694,6 +704,7 @@ class Router(HookRegistry):
task=request.get("task"),
lang=request.get("lang"),
lang_guess=request.get("lang_guess"),
+ hooks_timeout=hooks_timeout,
)
)
@@ -733,7 +744,7 @@ class Router(HookRegistry):
Returns:
One normal Router prediction result per request, in the same order as the input.
"""
- decisions = self.route_batch(requests)
+ decisions = self.route_batch(requests, hooks_timeout=hooks_timeout)
if not decisions:
return []
diff --git a/tests/test_hooks.py b/tests/test_hooks.py
index 755e69d1..f08b3571 100644
--- a/tests/test_hooks.py
+++ b/tests/test_hooks.py
@@ -1179,6 +1179,63 @@ finally:
check("timeout/a timed hook sees the caller's contextvars", _cv_calls, ["request-1"])
+# ------------------------------------------- per-call timeout reaches on_route through the batches
+# #277 threaded the per-call timeout through predict_batch's own dispatches, but route_batch had no
+# hooks_timeout argument and predict_batch did not forward its override into it, so an on_route hook
+# reached through either batch entry point saw the instance timeout instead of the caller's.
+class SlowRoute:
+ def on_route(self, ctx):
+ time.sleep(0.3)
+
+
+def slow_route_router(**kwargs):
+ return batch_router(hooks=[SlowRoute()], **kwargs)[0]
+
+
+# route_batch takes the per-call timeout and applies it to on_route.
+raised = None
+try:
+ slow_route_router().route_batch([req("a")], hooks_timeout=0.05)
+except TimeoutError as exc:
+ raised = str(exc)
+check_true("timeout/route_batch per-call reaches on_route",
+ isinstance(raised, str) and "exceeded" in raised)
+
+# predict_batch forwards the same override into route_batch, so on_route times out there too.
+raised = None
+try:
+ slow_route_router().predict_batch([req("a")], hooks_timeout=0.05)
+except TimeoutError as exc:
+ raised = str(exc)
+check_true("timeout/predict_batch per-call reaches on_route",
+ isinstance(raised, str) and "exceeded" in raised)
+
+# No per-call override: the instance timeout is still the fallback for the batched route path.
+raised = None
+try:
+ slow_route_router(hooks_timeout=0.05).route_batch([req("a")])
+except TimeoutError:
+ raised = True
+check("timeout/route_batch instance-level applies", raised, True)
+
+# A longer per-call value overrides a shorter instance timeout, as on the other entry points.
+decisions = slow_route_router(hooks_timeout=0.05).route_batch([req("a")], hooks_timeout=5.0)
+check("timeout/route_batch per-call override wins", [d["model"] for d in decisions], ["english"])
+
+# With no hooks at all both batched paths are unchanged.
+plain_r, plain_en, _ = batch_router()
+check("timeout/route_batch without hooks is unchanged",
+ [d["model"] for d in plain_r.route_batch([req("a")])], ["english"])
+plain_out = plain_r.predict_batch([req("a")])
+check("timeout/predict_batch without hooks is unchanged", len(plain_out), 1)
+check("timeout/predict_batch without hooks keeps routing", plain_out[0]["routing"]["model"], "english")
+check("timeout/predict_batch without hooks still batches", [c[0] for c in plain_en.calls], [["a"]])
+
+# The forwarded value is still validated by route(), exactly like a direct route() call.
+check_raises("timeout/route_batch rejects a zero per-call timeout", ValueError,
+ lambda: Router().route_batch([req("a")], hooks_timeout=0))
+
+
# --------------------------------------------------------------- report
print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))
for f_ in FAIL: