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.

perf(router): bypass predict context and OS entropy when no hooks are active

Hamidreza79abd9cbc209
diff --git a/laya/router.py b/laya/router.py
index f538d589..19866dc0 100644
--- a/laya/router.py
+++ b/laya/router.py
@@ -41,7 +41,7 @@ from typing import Any, Dict, List, Optional, Sequence, Tuple, Union
from .confidence import apply_confidence_gate, check_min_confidence
from .hooks import (
- HookRegistry, PredictContext, aggregate_usage, compose_hooks, dispatch, normalise_hooks,
+ AsyncHook, BaseHook, HookRegistry, PredictContext, aggregate_usage, compose_hooks, dispatch, normalise_hooks,
validate_timeout,
)
from .hooks import _SKIP_DEFAULTS
@@ -391,6 +391,17 @@ _ROUTER_OWNED_AGENT_ARGS = frozenset({
})
+def _has_on_route(hooks: Sequence[Any]) -> bool:
+ """True if at least one hook provides a non-default on_route implementation."""
+ for hook in hooks:
+ while isinstance(hook, AsyncHook):
+ hook = hook.hook
+ method = getattr(hook, "on_route", None)
+ if method is not None and getattr(method, "__func__", method) is not BaseHook.on_route:
+ return True
+ return False
+
+
def check_agent_kwargs(agent_kwargs: Dict[str, Any]) -> None:
"""Reject `agent_kwargs` names a Router cannot honour, before any checkpoint is loaded.
@@ -1202,6 +1213,8 @@ class Router(HookRegistry):
raise_errors = self.hooks_raise if hooks_raise is None else bool(hooks_raise)
active = compose_hooks(self.hooks, hooks)
timeout = self.hooks_timeout if hooks_timeout is None else validate_timeout(hooks_timeout)
+ if not active or not _has_on_route(active):
+ return decision
ctx = PredictContext(states=[state], questions=questions or {}, decision=decision, router=self)
dispatch(active, "on_route", ctx, raise_errors=raise_errors, lock=self._hooks_lock, timeout=timeout)
return ctx.decision
@@ -1564,6 +1577,14 @@ class Router(HookRegistry):
"""
if not isinstance(requests, SequenceABC) or isinstance(requests, (str, bytes)):
raise TypeError("requests must be a sequence of request dictionaries")
+ if not requests:
+ return []
+
+ use_fast = type(self).route is Router.route
+ timeout = self.hooks_timeout if hooks_timeout is None else validate_timeout(hooks_timeout)
+ active = compose_hooks(self.hooks, hooks)
+ raise_errors = self.hooks_raise if hooks_raise is None else bool(hooks_raise)
+ has_on_route = _has_on_route(active)
decisions: List[RouteDecision] = []
for i, request in enumerate(requests):
@@ -1583,20 +1604,51 @@ class Router(HookRegistry):
% (i, type(questions).__name__)
)
- decisions.append(
- self.route(
+ if not use_fast:
+ decisions.append(
+ self.route(
+ request["state"],
+ questions,
+ model=request.get("model"),
+ task=request.get("task"),
+ lang=request.get("lang"),
+ lang_guess=request.get("lang_guess"),
+ hooks=hooks,
+ hooks_raise=hooks_raise,
+ hooks_timeout=hooks_timeout,
+ **({"explain": True} if explain else {}),
+ )
+ )
+ elif not has_on_route:
+ decisions.append(
+ self._route(
+ request["state"],
+ questions,
+ model=request.get("model"),
+ task=request.get("task"),
+ lang=request.get("lang"),
+ lang_guess=request.get("lang_guess"),
+ **({"explain": True} if explain else {}),
+ )
+ )
+ else:
+ decision = self._route(
request["state"],
questions,
model=request.get("model"),
task=request.get("task"),
lang=request.get("lang"),
lang_guess=request.get("lang_guess"),
- hooks=hooks,
- hooks_raise=hooks_raise,
- hooks_timeout=hooks_timeout,
**({"explain": True} if explain else {}),
)
- )
+ ctx = PredictContext(
+ states=[request["state"]],
+ questions=questions,
+ decision=decision,
+ router=self,
+ )
+ dispatch(active, "on_route", ctx, raise_errors=raise_errors, lock=self._hooks_lock, timeout=timeout)
+ decisions.append(ctx.decision)
return decisions
diff --git a/tests/test_router_batch.py b/tests/test_router_batch.py
index bd172878..ddf199ef 100644
--- a/tests/test_router_batch.py
+++ b/tests/test_router_batch.py
@@ -587,3 +587,27 @@ def test_predict_batch_memoizes_identical_question_schema(monkeypatch):
assert len(results) == 4
# Exactly 2 schema serializations instead of 4
assert len(calls) == 2
+
+
+def test_route_batch_reaches_subclass_route_override():
+ """A subclass that overrides route() must have its override invoked by route_batch()."""
+ from laya.router import RouteDecision
+
+ class CustomRouter(Router):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.route_calls = []
+
+ def route(self, state, questions=None, **kwargs):
+ self.route_calls.append(state)
+ decision = super().route(state, questions, **kwargs)
+ return RouteDecision(dict(decision, reason="custom_override"))
+
+ router = CustomRouter()
+ items = [request("first request"), request("second request")]
+ decisions = router.route_batch(items)
+
+ assert router.route_calls == ["first request", "second request"]
+ assert len(decisions) == 2
+ assert [d["reason"] for d in decisions] == ["custom_override", "custom_override"]
+