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 main into this branch and resolve conflicts
Nandakishor2b543304f42e
diff --cc laya/agent.pyindex 7e779f03,f3ca21d1..3a225d66--- a/laya/agent.py+++ b/laya/agent.py@@@ -27,10 -27,8 +27,11 @@@ from .common importserialize_state,temp_bucket,)-from .hooks import HookRegistry, PredictContext, aggregate_usage, compose_hooks, dispatch, normalise_hooks+from .hooks import (+ HookRegistry, PredictContext, aggregate_usage, compose_hooks, dispatch, normalise_hooks,+ validate_timeout,+)+ from .revisions import resolve_revision, snapshot_revision, verify_digestsdef _fix_tokenizer_config(path: str):@@@ -932,10 -1110,10 +1122,11 @@@ RLAgent = Agendef load(model_id_or_path: str = "convaiinnovations/laya", device: Optional[str] = None,token: Optional[str] = None, subfolder: Optional[str] = None, fast: bool = False,+ revision: Optional[str] = None, expected_sha256: Optional[Dict[str, str]] = None,lang_temperatures: Optional[Dict[str, Dict[str, Any]]] = None,hooks=None, on_predict_start=None, on_predict_end=None,- hooks_raise: bool = True, hooks_concurrent: bool = True) -> Agent:+ hooks_raise: bool = True, hooks_concurrent: bool = True,+ hooks_timeout: Optional[float] = None) -> Agent:"""Load a Laya agent.`subfolder` picks one checkpoint out of a repo that bundles several:@@@ -948,7 -1127,7 +1140,8 @@@`laya.hooks`."""return Agent(model_id_or_path, device=device, token=token, subfolder=subfolder, fast=fast,+ revision=revision, expected_sha256=expected_sha256,lang_temperatures=lang_temperatures,hooks=hooks, on_predict_start=on_predict_start, on_predict_end=on_predict_end,- hooks_raise=hooks_raise, hooks_concurrent=hooks_concurrent)+ hooks_raise=hooks_raise, hooks_concurrent=hooks_concurrent,+ hooks_timeout=hooks_timeout)diff --cc laya/hooks.pyindex 29a7b1a9,7e459a46..58059b8d--- a/laya/hooks.py+++ b/laya/hooks.py@@@ -215,134 -214,10 +216,135 @@@ def compose_hooks(installed, hooks=NoneReads the process-wide defaults at call time, so hooks set after construction still apply."""- return default_hooks() + list(installed) + normalise_hooks(hooks, on_predict_start, on_predict_end)+ defaults = [] if _SKIP_DEFAULTS.get() else default_hooks()+ return defaults + list(installed) + normalise_hooks(hooks, on_predict_start, on_predict_end)+def validate_timeout(value: Optional[float]) -> Optional[float]:+ """Return *value* as a positive float, or ``None`` for no limit.++ A non-positive timeout is rejected here rather than left to+ ``thread.join``: ``join(0)`` and ``join(-1)`` return before the hook has+ started, so the outcome of a fast hook with such a value is a race.+ """+ if value is None:+ return None+ timeout = float(value)+ if timeout <= 0:+ raise ValueError("hooks_timeout must be a positive number or None; got %r" % (value,))+ return timeout+++_BACKGROUND_LOOP: Optional[asyncio.AbstractEventLoop] = None+_BACKGROUND_LOOP_LOCK = threading.Lock()+++def _background_loop() -> asyncio.AbstractEventLoop:+ """A daemon event loop thread, for running coroutines when the caller already has a loop."""+ global _BACKGROUND_LOOP+ with _BACKGROUND_LOOP_LOCK:+ if _BACKGROUND_LOOP is None or not _BACKGROUND_LOOP.is_running():+ _BACKGROUND_LOOP = asyncio.new_event_loop()+ thread = threading.Thread(target=_BACKGROUND_LOOP.run_forever, daemon=True,+ name="laya-async-hooks")+ thread.start()+ return _BACKGROUND_LOOP+++def run_coroutine_sync(coro: Awaitable[Any], loop: Optional[asyncio.AbstractEventLoop] = None) -> Any:+ """Run an awaitable to completion from synchronous code.++ Uses `asyncio.run` when the calling thread has no running loop. When it does (a caller+ inside an async function, or a framework that already runs a loop), the coroutine is run on+ a dedicated background loop so the calling thread can block on it without deadlocking. Pass+ `loop` to use a specific loop instead of the background one.++ A supplied `loop` must be running somewhere, and must not be the calling thread's own+ loop. Both are checked: the first would otherwise block forever with no coroutine ever+ scheduled, and the second would block the only thread that could run the coroutine.+ """+ if loop is not None:+ if not loop.is_running():+ raise ValueError("run_coroutine_sync: the loop passed is not running")+ try:+ running = asyncio.get_running_loop()+ except RuntimeError:+ running = None+ if loop is running:+ raise ValueError(+ "run_coroutine_sync: the loop passed is running in the calling thread; "+ "blocking on it would deadlock"+ )+ return asyncio.run_coroutine_threadsafe(coro, loop).result()+ try:+ asyncio.get_running_loop()+ except RuntimeError:+ return asyncio.run(coro)+ return asyncio.run_coroutine_threadsafe(coro, _background_loop()).result()+++class AsyncHook:+ """Wrap an async hook so its coroutine event methods run to completion in the sync core.++ The wrapped object may implement any subset of the events as `async def` methods, or as+ plain callables that return awaitables. Each event is run with `run_coroutine_sync`, so it+ works whether the caller is synchronous or already inside an event loop.++ from laya import AsyncHook++ class Remote(BaseHook):+ async def on_predict_end(self, ctx):+ await ship(ctx.results)++ agent = laya.load("convaiinnovations/laya", hooks=[AsyncHook(Remote())])++ Pass `loop` to funnel every coroutine onto a specific loop; otherwise a background loop is+ started on demand when the caller already has one.+ """++ __slots__ = ("hook", "loop")++ def __init__(self, hook: Any, loop: Optional[asyncio.AbstractEventLoop] = None):+ if isinstance(hook, type):+ raise TypeError("AsyncHook wraps an instance, not a class; got %s" % hook.__name__)+ if not any(hasattr(hook, event) for event in HOOK_EVENTS):+ raise TypeError(+ "AsyncHook wraps an object implementing at least one of %s; got %s"+ % (", ".join(HOOK_EVENTS), type(hook).__name__)+ )+ self.hook = hook+ self.loop = loop++ def _run(self, event: str, ctx: PredictContext) -> None:+ method = getattr(self.hook, event, None)+ if method is None:+ return+ result = method(ctx)+ if inspect.isawaitable(result):+ run_coroutine_sync(result, loop=self.loop)++ def on_predict_start(self, ctx: PredictContext) -> None:+ self._run("on_predict_start", ctx)++ def on_predict_end(self, ctx: PredictContext) -> None:+ self._run("on_predict_end", ctx)++ def on_route(self, ctx: PredictContext) -> None:+ self._run("on_route", ctx)++ def on_load(self, ctx: PredictContext) -> None:+ self._run("on_load", ctx)++ def on_evict(self, ctx: PredictContext) -> None:+ self._run("on_evict", ctx)++ def on_error(self, ctx: PredictContext) -> None:+ self._run("on_error", ctx)++ def __repr__(self) -> str:+ return "AsyncHook(%r)" % (self.hook,)++class HookRegistry:"""Mixin giving a runtime-mutable hook list.diff --cc laya/onnx_agent.pyindex 46df9b1d,0f9c4182..0558e8f5--- a/laya/onnx_agent.py+++ b/laya/onnx_agent.py@@@ -7,10 -7,8 +7,11 @@@ from typing import Any, Dict, Optionalimport numpy as np-from laya.hooks import HookRegistry, PredictContext, aggregate_usage, compose_hooks, dispatch, normalise_hooks+from laya.hooks import (+ HookRegistry, PredictContext, aggregate_usage, compose_hooks, dispatch, normalise_hooks,+ validate_timeout,+)+ from laya.revisions import resolve_revision, snapshot_revision, verify_digestsfrom laya.common import (QTYPES,answer_confidence,diff --cc laya/router.pyindex a70e46dd,69cd9ded..7dd999eb--- a/laya/router.py+++ b/laya/router.py@@@ -35,10 -35,8 +35,11 @@@ import timfrom collections.abc import Sequence as SequenceABCfrom typing import Any, Dict, List, Optional, Sequence, Union-from .hooks import HookRegistry, PredictContext, aggregate_usage, compose_hooks, dispatch, normalise_hooks+from .hooks import (+ HookRegistry, PredictContext, aggregate_usage, compose_hooks, dispatch, normalise_hooks,+ validate_timeout,+)+ from .hooks import _SKIP_DEFAULTSfrom .lang import analyse# The hub repo bundles all three checkpoints; only the requested subfolder is downloaded.diff --cc tests/test_hooks.pyindex f1e290fc,8d5b0240..978fee69--- a/tests/test_hooks.py+++ b/tests/test_hooks.py@@@ -1017,159 -1017,16 +1017,168 @@@ finallycheck("defaults/cover router lifecycle", ld.events,[("load", "english"), ("evict", "english"), ("load", "multilingual")])+ events = []+ _hooks.set_default_hooks([LevelTag("default")])+ r = Router()+ r.attach("english", make_fake())+ r.predict("a", QUESTIONS, model="english")+ r.predict_batch([req("b")])+ _hooks.clear_default_hooks()+ check("defaults/fire once per request", events, [("default", "start", "router"), ("default", "end", "router")] * 2)++# --------------------------------------------------------------- async hooks+import asyncio # noqa: E402++from laya import AsyncHook # noqa: E402+from laya.hooks import run_coroutine_sync # noqa: E402++calls = []+++class AsyncAudit:+ async def on_predict_end(self, ctx):+ await asyncio.sleep(0)+ calls.append("wrapped")+++f = make_fake()+f.add_hook(AsyncHook(AsyncAudit()))+f.predict_batch(["s0"], QUESTIONS)+check("async/wrapped hook awaited", calls, ["wrapped"])++calls.clear()+++async def async_end(ctx):+ await asyncio.sleep(0)+ calls.append("plain")+++f = make_fake()+f.predict_batch(["s0"], QUESTIONS, on_predict_end=async_end)+check("async/plain callable awaited", calls, ["plain"])++calls.clear()+++async def async_start(ctx):+ calls.append("loop")+++f = make_fake()+++async def _in_loop():+ f.predict_batch(["s0"], QUESTIONS, on_predict_start=async_start)+++asyncio.run(_in_loop())+check("async/works inside a running loop", calls, ["loop"])+++async def _seven():+ return 7+++check("async/run_coroutine_sync returns", run_coroutine_sync(_seven()), 7)+check_raises("async/AsyncHook rejects a class", TypeError, lambda: AsyncHook(AsyncAudit))+++# --------------------------------------------------------------- hook timeout+def slow_hook(ctx):+ time.sleep(0.3)+++f = make_fake()+raised = None+try:+ f.predict_batch(["s0"], QUESTIONS, on_predict_start=slow_hook, hooks_timeout=0.05)+except TimeoutError as exc:+ raised = str(exc)+check_true("timeout/raises TimeoutError", isinstance(raised, str) and "exceeded" in raised)++f = make_fake()+with warnings.catch_warnings(record=True) as caught:+ warnings.simplefilter("always")+ res = f.predict_batch(["s0"], QUESTIONS, on_predict_start=slow_hook,+ hooks_timeout=0.05, hooks_raise=False)+check("timeout/hooks_raise=False continues", len(res), 1)+check_true("timeout/hooks_raise=False warns",+ any(issubclass(w.category, RuntimeWarning) for w in caught))++f = make_fake()+f.hooks_timeout = 0.05+raised = None+try:+ f.predict_batch(["s0"], QUESTIONS, on_predict_start=slow_hook)+except TimeoutError:+ raised = True+check("timeout/instance-level applies", raised, True)++f = make_fake()+f.hooks_timeout = 0.05+res = f.predict_batch(["s0"], QUESTIONS, on_predict_start=slow_hook, hooks_timeout=5.0)+check("timeout/per-call override wins", len(res), 1)++f = make_fake()+res = f.predict_batch(["s0"], QUESTIONS, on_predict_end=lambda ctx: None, hooks_timeout=1.0)+check("timeout/fast hook unaffected", len(res), 1)+++# --------------------------------------------------------------- input validation and context+import contextvars # noqa: E402++from laya.hooks import validate_timeout # noqa: E402++check_raises("timeout/zero is rejected", ValueError, lambda: validate_timeout(0))+check_raises("timeout/negative is rejected", ValueError, lambda: validate_timeout(-0.5))+check("timeout/positive passes through", validate_timeout(1.5), 1.5)+check("timeout/None means no limit", validate_timeout(None), None)++f = make_fake()+check_raises("timeout/zero per call is rejected", ValueError,+ lambda: f.predict_batch(["s0"], QUESTIONS, hooks_timeout=0))++not_running = asyncio.new_event_loop()+try:+ check_raises("async/a non-running loop is rejected", ValueError,+ lambda: run_coroutine_sync(_seven(), loop=not_running))+finally:+ not_running.close()+++async def _own_loop():+ own = asyncio.get_running_loop()+ try:+ run_coroutine_sync(_seven(), loop=own)+ except ValueError:+ return "raised"+ return "no"+++check("async/the calling thread's own loop is rejected", asyncio.run(_own_loop()), "raised")+check_raises("async/AsyncHook rejects an object with no events", TypeError,+ lambda: AsyncHook(object()))++_cv_seen = contextvars.ContextVar("cv_seen", default=None)+_cv_calls = []+++class CvHook:+ def on_predict_start(self, ctx):+ _cv_calls.append(_cv_seen.get())+++f = make_fake()+_token = _cv_seen.set("request-1")+try:+ f.predict_batch(["s0"], QUESTIONS, hooks=[CvHook()], hooks_timeout=1.0)+finally:+ _cv_seen.reset(_token)+check("timeout/a timed hook sees the caller's contextvars", _cv_calls, ["request-1"])++# --------------------------------------------------------------- reportprint("\n%d passed, %d failed" % (len(PASS), len(FAIL)))for f_ in FAIL: