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(onnx): catch an in-place question rewrite in the scan-budget guard

sameedkhan17e0e655929368
diff --git a/laya/onnx_agent.py b/laya/onnx_agent.py
index 5ca5fb93..27ab5c4d 100644
--- a/laya/onnx_agent.py
+++ b/laya/onnx_agent.py
@@ -1,3 +1,4 @@
+import copy
import json
import os
import threading
@@ -476,6 +477,14 @@ class ONNXAgent(HookRegistry):
internal = {qid: self._to_internal(questions[qid]) for qid in ids}
budget, step_default, _ = window_budget(self.tok, [internal[qid] for qid in ids], max_len,
head_max_len, window=window, stride=stride)
+ # Snapshot the questions the scan was just sized against, BEFORE any start hook can
+ # rewrite them, as the torch `predict_long` does (5f29170). `==` over the caller's mapping
+ # cannot see an in-place rewrite: a hook that adds options to `ctx.questions[q]["criteria"]`
+ # -- the `widen_for_high_cardinality` pattern in `docs/hooks/patterns.md` -- mutates the
+ # same nested dict, so both sides change together, `_check_scan_budget` returns early, and
+ # the windows are re-truncated while `usage["windows"]` reports them all read. A shallow
+ # copy shares the nested dicts and misses it exactly the same way.
+ asked = copy.deepcopy(questions)
state_ids = encode_text(
self.tok,
@@ -492,7 +501,7 @@ class ONNXAgent(HookRegistry):
# fit one window was silently truncated by a re-budgeting hook and still reported
# `windows: 1`, i.e. "the model read all of it" -- measured, 138 of 240 state tokens
# never reached the model, while a longer document on the identical input hard-failed.
- _check_scan_budget(self, evidence, budget, max_len, head_max_len, questions)
+ _check_scan_budget(self, evidence, budget, max_len, head_max_len, asked)
single["usage"] = {**(single.get("usage") or {}), "windows": 0 if evidence["answered"] else 1}
return single
@@ -521,7 +530,7 @@ class ONNXAgent(HookRegistry):
# `ctx.max_len`/`ctx.head_max_len` identically), so leaving it off here meant the bug was
# fully live on this path while the other agent refused the identical input -- measured,
# 34.6% of a document reaching no model.
- _check_scan_budget(self, evidence, budget, max_len, head_max_len, questions)
+ _check_scan_budget(self, evidence, budget, max_len, head_max_len, asked)
if evidence["answered"]:
# A hook answered the document before any window was scored: pass that answer
diff --git a/tests/test_onnx_long.py b/tests/test_onnx_long.py
index 6c5b5de8..c5f55826 100644
--- a/tests/test_onnx_long.py
+++ b/tests/test_onnx_long.py
@@ -333,6 +333,38 @@ check("questions/no-op preserves the unhooked result",
_bare_onnx().predict_long(LONG_STATE, QUESTIONS))
+# A hook that widens a question IN PLACE must be refused here exactly as on the torch agent
+# (tests/test_predict_long.py, "an in-place question rewrite is refused"). Compared against the
+# caller's own mapping it cannot be seen: the hook mutates the same nested dict, so
+# `questions == asked` stays True and the scan proceeds with windows `build_sequence` re-truncates.
+# Measured on this fixture, 8 added options cut the room from 28 to 12 state tokens: all 14 windows
+# were truncated and 32 of 200 tokens reached no model, and a 24-token state that fit one window
+# lost 12 while reporting `windows: 1`. The guard's own message is asserted, because too many
+# options for max_len raise a different ValueError from `_encode_state` that would pass a bare
+# `check_raises`. Fresh mappings per call, because the hook mutates what it is handed.
+def _inplace_questions():
+ return {"dept": {"type": "choice", "instructions": "?", "criteria": {"a": "x", "b": "y"}},
+ "urgent": dict(QUESTIONS["urgent"])}
+
+
+def _widen_in_place(ctx):
+ ctx.questions["dept"]["criteria"].update({"opt%02d" % i: "d" * 20 for i in range(8)})
+
+
+for name, inplace_state in (("a windowed state", LONG_STATE),
+ ("a one-window state", LONG_STATE[:24])):
+ try:
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore")
+ _bare_onnx().predict_long(inplace_state, _inplace_questions(),
+ on_predict_start=_widen_in_place)
+ except ValueError as exc:
+ check_true("questions/an in-place widening rewrite is refused on %s" % name,
+ "after the start hooks ran" in str(exc), repr(exc))
+ else:
+ FAIL.append("questions/an in-place widening rewrite is refused on %s: did not raise" % name)
+
+
def _annotate_first_window(ctx):
ctx.results[0]["answers"]["review"] = {"type": "noul", "noul": 0.9, "answer_confidence": 0.9}
diff --git a/tests/test_predict_long.py b/tests/test_predict_long.py
index 7199efc2..d2986620 100644
--- a/tests/test_predict_long.py
+++ b/tests/test_predict_long.py
@@ -1050,7 +1050,7 @@ check_raises("questions/the torch scan validates before _to_internal", ValueErro
# says the contract binds both agents.
_onnx_src = inspect.getsource(ONNXAgent.predict_long)
check_true("hook budget/the ONNX scan checks its budget too",
- "_check_scan_budget(self, evidence, budget, max_len, head_max_len, questions)" in _onnx_src,
+ "_check_scan_budget(self, evidence, budget, max_len, head_max_len, asked)" in _onnx_src,
"")
check("hook budget/both agents check it on the single-window path too",
(inspect.getsource(Agent.predict_long).count("_check_scan_budget("),