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(structured): let Router.decide_batch pin a checkpoint

hemanthrayuduu98a7b4db0447
diff --git a/laya/structured.py b/laya/structured.py
index 97da5e83..c4e3a24b 100644
--- a/laya/structured.py
+++ b/laya/structured.py
@@ -364,6 +364,11 @@ def decide(runner, state: Any, schema: Any = None, *, questions: Optional[Dict[s
return values
+# The keys `Router.predict_batch` reads off each request (`route_batch` and the token-budget
+# overrides) rather than accepting as keywords; laya-serve's BATCH_BODY_ITEM_CONTROLS plus `model`.
+_ROUTER_REQUEST_KEYS = ("model", "task", "lang", "lang_guess", "max_len", "head_max_len")
+
+
def decide_batch(runner, states: Sequence[Any], schema: Any = None, *,
questions: Optional[Dict[str, Any]] = None,
return_details: bool = False, min_confidence: Optional[float] = None,
@@ -409,8 +414,12 @@ def decide_batch(runner, states: Sequence[Any], schema: Any = None, *,
if hasattr(runner, "route_batch"):
# Router convention: one request dict per state, each carrying the shared
- # questions, so it routes, groups by checkpoint and restores input order.
- results = predict_batch([{"state": s, "questions": questions} for s in states],
+ # questions, so it routes, groups by checkpoint and restores input order. The routing
+ # pins and the token budget are read off each request there, not taken as keywords of
+ # predict_batch, so they ride on every request; the rest (batch_size, hooks, ...) is
+ # call-level.
+ per_request = {key: predict_kwargs.pop(key) for key in _ROUTER_REQUEST_KEYS if key in predict_kwargs}
+ results = predict_batch([dict({"state": s, "questions": questions}, **per_request) for s in states],
**predict_kwargs)
else:
# Agent convention: a list of states evaluated against one question set.
diff --git a/tests/test_structured.py b/tests/test_structured.py
index 31018efb..b2a82641 100644
--- a/tests/test_structured.py
+++ b/tests/test_structured.py
@@ -381,6 +381,71 @@ check("batch/router questions per request",
check("batch/router kwargs forwarded", router_like.calls[0]["kwargs"], {"batch_size": 3})
check("batch/router projection matches agent path", router_outs, outs)
+
+# A real Router, not a fake that takes **kwargs: Router.predict_batch reads model/task/lang/
+# lang_guess/max_len/head_max_len off each request and has no such keywords, so forwarding them
+# whole raised TypeError and `decide_batch(model=...)` could not pin a checkpoint at all -- the
+# only way to reach a registered one, since those are never auto-routed.
+class StubCheckpoint:
+ """An attached agent that answers every state and records how it was called."""
+
+ def __init__(self, name):
+ self.name, self.calls = name, []
+
+ def predict_batch(self, states, questions, **kwargs):
+ self.calls.append({"states": list(states), "kwargs": kwargs})
+ return [{"model": "laya-rl-agent", "usage": {"input_tokens": 1, "output_tokens": 0},
+ "answers": BATCH_ANSWERS[s]} for s in states]
+
+ def system_one(self, state, questions, **kwargs):
+ return self.predict_batch([state], questions, **kwargs)[0]
+
+ predict = system_one
+
+
+def stub_router():
+ router = laya.Router()
+ stubs = {name: StubCheckpoint(name) for name in ("english", "multilingual")}
+ for name, stub in stubs.items():
+ router.attach(name, stub)
+ return router, stubs
+
+
+for pin in ({"model": "multilingual"}, {"model": "ml"}, {"lang": "de"}, {"lang_guess": "de"}):
+ router, stubs = stub_router()
+ try:
+ pinned = router.decide_batch(STATES, schema=SCHEMA, **pin)
+ except TypeError as e:
+ pinned = repr(e)
+ check("batch/router %s answers every state on that checkpoint" % pin,
+ (pinned, [c["states"] for c in stubs["multilingual"].calls], stubs["english"].calls),
+ (outs, [STATES], []))
+
+router, stubs = stub_router()
+try:
+ router.decide_batch(STATES, schema=SCHEMA, model="english", max_len=256, head_max_len=128, batch_size=2)
+ seen = stubs["english"].calls[0]["kwargs"]
+ seen = {k: seen.get(k) for k in ("max_len", "head_max_len", "batch_size")}
+except TypeError as e:
+ seen = repr(e)
+check("batch/router per-request budget and call-level batch_size both reach the agent", seen,
+ {"max_len": 256, "head_max_len": 128, "batch_size": 2})
+
+router, stubs = stub_router()
+router.register("papers", "/models/papers")
+papers = StubCheckpoint("papers")
+router.attach("papers", papers)
+try:
+ pinned = router.decide_batch(STATES, schema=SCHEMA, model="papers")
+except TypeError as e:
+ pinned = repr(e)
+check("batch/router model= reaches a registered checkpoint", (pinned, [c["states"] for c in papers.calls]),
+ (outs, [STATES]))
+
+router, stubs = stub_router()
+check("batch/router decide_batch(model=) agrees with decide(model=)",
+ [router.decide(s, schema=SCHEMA, model="multilingual") for s in STATES], outs)
+
# a string is a sequence of states only accidentally: refuse it like route_batch does
check_raises("batch/rejects string states", TypeError,
lambda: decide_batch(FakeBatchRunner({}), "abc", schema=SCHEMA))