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
fix(hooks): refuse a skip() whose count breaks the contract it documents
Aashish417ddf6b5836
diff --git a/laya/hooks.py b/laya/hooks.pyindex 2a1bec51..f2e3696e 100644--- a/laya/hooks.py+++ b/laya/hooks.py@@ -50,8 +50,22 @@ class PredictContext:`results` replaces the whole call, so it carries one entry per state in `ctx.states` --the shape `predict_batch` returns -- in that order. A hook fires once per call, and a- call can carry many states.+ call can carry many states. The one other accepted shape is a single entry for the+ whole call, which is what `predict_long` takes as the document's answer (its scan+ hands the hook every window as a state, and refuses anything but one result).++ The count is checked here, against this contract, so a wrong one fails inside the+ hook under the caller's `hooks_raise` policy instead of downstream: `Router.predict`+ indexed `results[0]` of an empty list (an `IndexError`, which serve maps to 500),+ and `predict_batch` returned a shorter list than it was given states, quietly+ dropping rows the caller was about to zip against."""+ states = self.states+ if (isinstance(states, (list, tuple)) and isinstance(results, (list, tuple))+ and len(results) not in (1, len(states))):+ raise ValueError(+ "ctx.skip() takes one result for the whole call or one per state in "+ "ctx.states (%d); got %d" % (len(states), len(results)))self.results = resultsdiff --git a/tests/test_batch.py b/tests/test_batch.pyindex 876ef57f..b96f1b9d 100644--- a/tests/test_batch.py+++ b/tests/test_batch.py@@ -171,7 +171,12 @@ f = make_length_fake()check("length/empty inputs", f.predict_batch([], QUESTIONS, sort_by_length=True), [])check("length/empty questions", f.predict_batch(states[:1], {}, sort_by_length=True)[0]["answers"], {})check("length/empty cases do not encode", f.encoded, [])-f.predict_batch(states, QUESTIONS, sort_by_length=True, on_predict_start=lambda ctx: ctx.skip([]))+# The cache pattern carries one entry per state (see docs/hooks/api.md); an empty skip+# would return zero rows for this 37-state call, which `skip()` refuses as the contract+# violation it is. What this pins is that a skip short-circuits before sorting/encoding.+f.predict_batch(states, QUESTIONS, sort_by_length=True,+ on_predict_start=lambda ctx: ctx.skip(+ [{"model": "cached", "answers": {}, "usage": {}} for _ in ctx.states]))check("length/cache skip does not encode", f.encoded, [])plain, grouped = make_length_fake(), make_length_fake()diff --git a/tests/test_hooks.py b/tests/test_hooks.pyindex 090b422f..7a9b6fb6 100644--- a/tests/test_hooks.py+++ b/tests/test_hooks.py@@ -436,6 +436,83 @@ with f.hooks_installed(Tag(log, "t"), Tag(log, "t")):passcheck("hooks_installed/no residue when nothing was installed before", len(f.hooks), 0)++# --------------------------------------------------------------- skip() counts+# `skip()` replaces the whole call, so the count is part of the contract its docstring+# states: one result per state, or a single result for the whole call (the shape+# `predict_long` takes as the document's answer -- its scan hands the hook every window+# as a state). Anything else used to pass through unchecked: `Router.predict` then+# indexed `results[0]` of an empty list (an `IndexError`, a 500 over serve), and+# `predict_batch` returned a shorter list than it was given states, silently dropping+# rows the caller was about to zip against.++def _skipper(*results):+ def hook(ctx):+ ctx.skip(list(results))+ return hook+++CACHED = {"model": "cache", "usage": {},+ "answers": {"a": {"type": "noul", "noul": 0.9}, "b": {"type": "noul", "noul": 0.1}}}++# one per state: the docs' cache pattern, and the shape `predict_batch` returns+f = make_fake()+res = f.predict_batch(["s0", "s1", "s2"], QUESTIONS, on_predict_start=_skipper(CACHED, CACHED, CACHED))+check("skip/one per state is accepted", len(res), 3)+check("skip/forward never ran", len(f._encode_states), 0)+check("skip/the payload comes back untouched", res[0], CACHED)++# one result for the whole call: what a `predict_long` scan's hook answers with, even+# though `ctx.states` holds every window. Pinned here because the scan's own check+# (`predict_long: a start hook answered this state with N results`) reads the other+# shape as a mistake, and this is the path it accepts.+f = make_fake()+res = f.predict_batch(["s0", "s1"], QUESTIONS, on_predict_start=_skipper(CACHED))+check("skip/one result for the whole call is accepted", len(res), 1)+check("skip/whole-call answer: forward never ran", len(f._encode_states), 0)++# short and long counts are refused at the hook, with the contract in the message+f = make_fake()+err = None+try:+ f.predict_batch(["s0", "s1", "s2"], QUESTIONS, on_predict_start=_skipper(CACHED, CACHED))+except ValueError as exc:+ err = exc+check_true("skip/a short list is refused", err is not None, repr(err))+check_true("skip/the refusal names the contract",+ err is not None and "one per state" in str(err), repr(err))+check("skip/a refused skip leaves inference untouched", len(f._encode_states), 0)++f = make_fake()+err = None+try:+ f.predict_batch(["s0"], QUESTIONS, on_predict_start=_skipper())+except ValueError as exc:+ err = exc+check_true("skip/an empty answer for a non-empty call is refused", err is not None, repr(err))+check_true("skip/the refusal counts the states",+ err is not None and "(1); got 0" in str(err), repr(err))++# under hooks_raise=False a refusal warns like any other hook failure and the call still+# gets an answer: the cache is wrong, not the request.+f = make_fake()+with warnings.catch_warnings(record=True) as caught:+ warnings.simplefilter("always")+ res = f.predict_batch(["s0"], QUESTIONS, on_predict_start=_skipper(), hooks_raise=False)+check("skip/refusal under hooks_raise=False falls back to inference", len(res), 1)+check_true("skip/refusal under hooks_raise=False is warned",+ any(issubclass(w.category, RuntimeWarning) and "ctx.skip()" in str(w.message)+ for w in caught), [str(w.message) for w in caught])++# Router.predict used to turn an empty answer into `ctx.results[0]` -- IndexError, a 500+# over serve. The same refusal, raised where the contract is written.+_skip_router = Router()+_skip_router.attach("english", FakeAgent())+check_raises("router/skip empty is a ValueError, not an IndexError",+ ValueError,+ lambda: _skip_router.predict("hello", QUESTIONS,+ on_predict_start=lambda ctx: ctx.skip([])))+log = []r = Router()r.add_hook(Tag(log, "router"))