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(agent): the over-budget message named the one knob that makes it worse
PerryLink5c543b1a7572
diff --git a/laya/agent.py b/laya/agent.pyindex 1b58de6f..55cdaa53 100644--- a/laya/agent.py+++ b/laya/agent.py@@ -672,8 +672,23 @@ class Agent(HookRegistry):q = internal[qid]seq, markers = build_sequence(self.tok, state, q, max_len, head_max_len,truncate_left=truncate_left, state_ids=state_ids)- if len(markers) != len(render_options(q)):- raise ValueError("question %r options exceed head_max_len=%d" % (qid, head_max_len))+ n_opts = len(render_options(q))+ if len(markers) != n_opts:+ # The markers are placed at absolute positions and `build_sequence` then drops the+ # ones past `max_len`, so this is about the question fitting in the sequence --+ # `head_max_len` is how much of it the options were given, and `max_len` is the+ # ceiling that dropped them. Naming only `head_max_len` pointed at the wrong knob+ # in both directions: lowering it shortens the option block and can make the+ # call succeed, while raising it makes the overflow worse.+ #+ # The count reported is the markers that SURVIVED, not `len(seq)`: `build_sequence`+ # truncates to `max_len` first, so `len(seq)` is always exactly `max_len` here and+ # would state the ceiling as though it were the requirement.+ raise ValueError(+ "question %r: only %d of its %d option markers fit in max_len=%d with "+ "head_max_len=%d spent on the question; lower head_max_len, raise max_len, "+ "or use fewer options"+ % (qid, len(markers), n_opts, max_len, head_max_len))items.append({"ids": seq, "markers": markers, "qtype": QTYPES[q["t"]]})return itemsdiff --git a/laya/onnx_agent.py b/laya/onnx_agent.pyindex 0f4a19f5..192eea02 100644--- a/laya/onnx_agent.py+++ b/laya/onnx_agent.py@@ -303,8 +303,19 @@ class ONNXAgent(HookRegistry):self.tok, state, q, max_len, head_max_len,truncate_left=truncate_left, state_ids=state_ids,)- if len(markers) != len(render_options(q)):- raise ValueError("question %r options exceed head_max_len=%d" % (qid, head_max_len))+ n_opts = len(render_options(q))+ if len(markers) != n_opts:+ # Same diagnosis as `Agent._encode_state`, so both backends report the same thing.+ # The markers are placed at absolute positions and `build_sequence` drops the ones+ # past `max_len`, so `head_max_len` is how much of the sequence the options were+ # given and `max_len` is the ceiling that dropped them -- naming only+ # `head_max_len` pointed at the wrong knob in both directions. The count is the+ # markers that survived, not `len(seq)`, which is always exactly `max_len` here.+ raise ValueError(+ "question %r: only %d of its %d option markers fit in max_len=%d with "+ "head_max_len=%d spent on the question; lower head_max_len, raise max_len, "+ "or use fewer options"+ % (qid, len(markers), n_opts, max_len, head_max_len))items.append({"ids": seq, "markers": markers, "qtype": QTYPES[q["t"]]})b = collate_items([items], self.tok.pad_token_id)diff --git a/tests/test_load_errors.py b/tests/test_load_errors.pyindex 2e7486ed..d07dd033 100644--- a/tests/test_load_errors.py+++ b/tests/test_load_errors.py@@ -172,6 +172,24 @@ check_true("options over budget/raises ValueError", isinstance(_outcome, ValueErcheck_true("options over budget/names the question", "'q'" in str(_outcome), str(_outcome))check_true("options over budget/reports the budget","head_max_len" in str(_outcome), str(_outcome))++# The message used to name only `head_max_len`, which pointed at the wrong knob in both+# directions. The option markers are placed at absolute positions and `build_sequence` drops the+# ones past `max_len`, so `head_max_len` is how much of the sequence the options were given --+# lowering it shortens the option block and can bring the question back inside `max_len`, while+# raising it overflows further. The message has to name `max_len` and say what was measured, or a+# caller follows it the wrong way. The direction itself is measured against a real checkpoint in+# the description rather than pinned here, since it needs one this suite does not build.+check_true("options over budget/names max_len too",+ "max_len=" in str(_outcome), str(_outcome))+# The count is the markers that SURVIVED, not `len(seq)`: `build_sequence` truncates to `max_len`+# first, so `len(seq)` is always exactly `max_len` at this point and reporting it stated the+# ceiling as though it were the requirement. `only N of its M` is the shape that distinguishes+# them, and `M` is checkable here because the question has 140 options.+check_true("options over budget/reports the markers that survived, not the ceiling",+ "only " in str(_outcome) and "fit in" in str(_outcome), str(_outcome))+check_true("options over budget/names the option count",+ "140 option markers" in str(_outcome), str(_outcome))# ...and a question that does fit still answers, so the guard is not refusing everything.fits = {"q": {"type": "choice", "instructions": "Pick one","criteria": {"department": None, "billing": None}}}diff --git a/tests/test_onnx_lang_parity.py b/tests/test_onnx_lang_parity.pyindex 4a9bae6f..36eae06a 100644--- a/tests/test_onnx_lang_parity.py+++ b/tests/test_onnx_lang_parity.py@@ -128,17 +128,110 @@ check("lang/hyphen subtag resolves to the override",check_true("lang/noul is scaled too",base["answers"]["flag"]["noul"] != de["answers"]["flag"]["noul"])-# a malformed override is rejected up front, like the torch Agent+# A malformed override is rejected up front, like the torch Agent.+#+# The two backends parse `lang_temperatures` independently today, and the shared helper that would+# replace both copies is `common.resolve_lang_temperatures` from #428. This check is written so it+# holds either way: through the shared helper when it is present, and against whichever copy is+# local when it is not. A source-text check on `__init__` cannot do that -- it passed while the two+# copies had already drifted, which is how ONNXAgent kept the `cfg.get(...)`-then-`len(...)` shape+# after Agent was fixed.try:- _bare_onnx_bad = ONNXAgent.__new__(ONNXAgent)- # the parsing lives in __init__; assert the same guard exists in its source- src = inspect.getsource(ONNXAgent.__init__)- check_true("lang/__init__ validates the 3-float override",- "must be a list of 3 floats" in src)+ try:+ from laya.common import resolve_lang_temperatures as _resolve_overrides+ _via = "common.resolve_lang_temperatures"+ except ImportError: # #428 not merged yet+ _resolve_overrides = None+ _via = "the ONNXAgent copy"+ _src = inspect.getsource(ONNXAgent.__init__)+ check_true("lang/__init__ validates the override's shape",+ "must be a list of 3 floats" in _src, "neither the helper nor the guard")+ check_true("lang/override parsing is locatable (%s)" % _via, True)++ if _resolve_overrides is not None:+ try:+ _resolve_overrides({"de": {"temperature": 2}}, [1.0, 1.0, 1.0])+ check_true("lang/a scalar override is rejected", False, "no error raised")+ except ValueError as exc:+ check_true("lang/a scalar override is rejected with the 3-float message",+ "must be a list of 3 floats" in str(exc), str(exc))except Exception as e: # noqa: BLE001FAIL.append("lang/guard check raised %r" % e)+# ------------------------------------------- the over-budget diagnosis, identical in both backends+# The two backends each build sequences for the same questions and can each refuse one whose+# option markers do not all fit. They used to say different things: `Agent` named the question,+# the marker counts and both budgets, while `ONNXAgent` said only+# "question %r options exceed head_max_len=%d" -- which pointed at the knob that makes the overflow+# worse. Rewording one and not the other is invisible to both suites, so this drives the same+# over-budget question through BOTH paths and compares the messages, which is the property that+# has to hold rather than either message on its own.+_MANY = {("department of %s handling billing enquiries %d" % ("x" * 12, i)): None+ for i in range(1, 41)}+_OVER = {"q": {"type": "choice", "instructions": "Which department?", "criteria": _MANY}}+++def _make_onnx(max_len, head_max_len):+ a = ONNXAgent.__new__(ONNXAgent)+ a.model_id = "stub"+ a.cfg = {"max_len": max_len, "head_max_len": head_max_len}+ a.tok = _FakeTok()+ a.temperature = [1.0, 1.0, 1.0]+ a.temperature_by_options = {}+ a.session = _StubSession(np.zeros((1, 8), dtype=np.float32),+ np.zeros((1, 2), dtype=np.float32))+ a.lang_temperatures = {}+ return a+++def _make_torch(max_len, head_max_len):+ f = Agent.__new__(Agent)+ f.cfg = {"max_len": max_len, "head_max_len": head_max_len}+ f.tok = _FakeTok()+ f.temperature = [1.0, 1.0, 1.0]+ f.temperature_by_options = {}+ f.lang_temperatures = {}+ return f+++def _outcome(fn):+ """Call `fn` and return the exception, or None if it did not raise."""+ try:+ fn()+ return None+ except Exception as exc: # noqa: BLE001+ return exc+++for _max_len, _head in ((64, 32), (128, 64)):+ _onnx_exc = _outcome(lambda ml=_max_len, h=_head: _make_onnx(ml, h)._infer("short state", _OVER))+ _torch_exc = _outcome(lambda ml=_max_len, h=_head: _make_torch(ml, h)._encode_state(+ "short state", list(_OVER),+ {qid: Agent._to_internal(_OVER[qid]) for qid in _OVER}))++ _label = "over-budget/%d/%d" % (_max_len, _head)+ check_true("%s both backends refuse it" % _label,+ isinstance(_onnx_exc, ValueError) and isinstance(_torch_exc, ValueError),+ "onnx=%r torch=%r" % (_onnx_exc, _torch_exc))+ check("%s the two messages are identical" % _label,+ str(_onnx_exc), str(_torch_exc))+ # ...and the shared message says what the guard measured, not the ceiling `len(seq)` hits+ check_true("%s names the markers that survived" % _label,+ "only " in str(_torch_exc) and "option markers fit" in str(_torch_exc),+ str(_torch_exc))+ check_true("%s names max_len as well as head_max_len" % _label,+ "max_len=" in str(_torch_exc) and "head_max_len=" in str(_torch_exc),+ str(_torch_exc))++# a question that fits must not raise in either backend, so the guard is not refusing everything+_onnx_ok = _outcome(lambda: _make_onnx(512, 192)._infer("short state", {+ "q": {"type": "choice", "instructions": "Pick one",+ "criteria": {"department": None, "billing": None}}}))+check_true("over-budget/a question that fits is answered by ONNXAgent",+ _onnx_ok is None, repr(_onnx_ok))++print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))for f in FAIL:print(" FAIL " + f)