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(hooks): run process-wide default hooks once per Router request

Rahul Sabharwale81c7e227e0d
diff --git a/laya/hooks.py b/laya/hooks.py
index 27f419fc..7e459a46 100644
--- a/laya/hooks.py
+++ b/laya/hooks.py
@@ -6,6 +6,7 @@ Everything here is pure Python: importing `laya` must not start pulling torch.
"""
from __future__ import annotations
+import contextvars
import threading
import time
import uuid
@@ -174,6 +175,7 @@ def normalise_hooks(
_DEFAULT_HOOKS: List[Any] = []
_DEFAULT_HOOKS_LOCK = threading.Lock()
+_SKIP_DEFAULTS = contextvars.ContextVar("laya_skip_default_hooks", default=False)
def default_hooks() -> List[Any]:
@@ -212,7 +214,8 @@ def compose_hooks(installed, hooks=None, on_predict_start=None, on_predict_end=N
Reads 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)
class HookRegistry:
diff --git a/laya/router.py b/laya/router.py
index 91920c08..e6463334 100644
--- a/laya/router.py
+++ b/laya/router.py
@@ -36,6 +36,7 @@ from collections.abc import Sequence as SequenceABC
from typing import Any, Dict, List, Optional, Sequence, Union
from .hooks import HookRegistry, PredictContext, aggregate_usage, compose_hooks, dispatch, normalise_hooks
+from .hooks import _SKIP_DEFAULTS
from .lang import analyse
# The hub repo bundles all three checkpoints; only the requested subfolder is downloaded.
@@ -560,6 +561,7 @@ class Router(HookRegistry):
if ctx.head_max_len is not None:
overrides["head_max_len"] = ctx.head_max_len
+ skip = _SKIP_DEFAULTS.set(True)
try:
result = agent.system_one(ctx.states[0], ctx.questions, lang=effective_lang, **overrides)
except TypeError as e:
@@ -567,6 +569,8 @@ class Router(HookRegistry):
result = agent.system_one(ctx.states[0], ctx.questions, **overrides)
else:
raise
+ finally:
+ _SKIP_DEFAULTS.reset(skip)
result["routing"] = dict(decision)
ctx.results = [result]
else:
@@ -777,6 +781,7 @@ class Router(HookRegistry):
batch_kwargs = dict(group["overrides"])
if group["lang"] is not None:
batch_kwargs["lang"] = group["lang"]
+ skip = _SKIP_DEFAULTS.set(True)
try:
batch_results = agent.predict_batch(
[ctx.states[0] for _, ctx in items],
@@ -797,6 +802,8 @@ class Router(HookRegistry):
)
else:
raise
+ finally:
+ _SKIP_DEFAULTS.reset(skip)
if len(batch_results) != len(items):
raise RuntimeError(
diff --git a/tests/test_hooks.py b/tests/test_hooks.py
index a5a40858..8d5b0240 100644
--- a/tests/test_hooks.py
+++ b/tests/test_hooks.py
@@ -1017,6 +1017,15 @@ finally:
check("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)
+
# --------------------------------------------------------------- report
print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))