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): merge per-question usage across predict_long windows
Bruce-Yii63e35af59bfa
diff --git a/laya/agent.py b/laya/agent.pyindex 5ca3861c..eacc2c68 100644--- a/laya/agent.py+++ b/laya/agent.py@@ -1268,13 +1268,26 @@ class Agent(HookRegistry):"token_end": min(starts[best] + budget, len(state_ids)),"count": len(results)}answers[qid] = ans- # Aggregate usage generically so fields predict_batch may grow later (e.g. the fallback- # counters from #351) are propagated, not silently dropped: sum numeric fields across- # windows, carry any non-numeric field through, then record the window count.+ # Aggregate usage generically so fields predict_batch may grow later (e.g. the+ # fallback counters from #351) are propagated, not silently dropped: sum numeric+ # fields across windows, merge the per-question records, then record the window+ # count.+ # A per-question field has to be merged rather than replaced. `usage["options"]` is a+ # dict keyed by question id, set only on the windows where option spans actually+ # collapsed, so replacing it left the caller holding whichever collapsing window came+ # last. The deciding window is the most confident one, not the last one, so that could+ # report a collapse for a window that did not decide while the deciding window's own+ # record was gone.usage: Dict[str, Any] = {}for r in results:for key, val in r["usage"].items():- usage[key] = (usage.get(key, 0) + val) if isinstance(val, (int, float)) else val+ prev = usage.get(key)+ if isinstance(val, (int, float)):+ usage[key] = (prev if isinstance(prev, (int, float)) else 0) + val+ elif isinstance(val, dict) and isinstance(prev, dict):+ usage[key] = {**prev, **val}+ else:+ usage[key] = valusage["output_tokens"] = 0usage["windows"] = len(results)return {"model": "laya-rl-agent", "answers": answers, "usage": usage}diff --git a/laya/onnx_agent.py b/laya/onnx_agent.pyindex c31778a9..a927b7ec 100644--- a/laya/onnx_agent.py+++ b/laya/onnx_agent.py@@ -508,11 +508,24 @@ class ONNXAgent(HookRegistry):"count": len(results)}answers[qid] = ans# Aggregate usage generically so fields predict_batch may grow later are propagated- # rather than silently dropped, then record the window count.+ # rather than silently dropped: sum numeric fields across windows, merge the+ # per-question records, then record the window count.+ # A per-question field has to be merged rather than replaced. `usage["options"]` is a+ # dict keyed by question id, set only on the windows where option spans actually+ # collapsed, so replacing it left the caller holding whichever collapsing window came+ # last. The deciding window is the most confident one, not the last one, so that could+ # report a collapse for a window that did not decide while the deciding window's own+ # record was gone.usage: Dict[str, Any] = {}for r in results:for key, val in r["usage"].items():- usage[key] = (usage.get(key, 0) + val) if isinstance(val, (int, float)) else val+ prev = usage.get(key)+ if isinstance(val, (int, float)):+ usage[key] = (prev if isinstance(prev, (int, float)) else 0) + val+ elif isinstance(val, dict) and isinstance(prev, dict):+ usage[key] = {**prev, **val}+ else:+ usage[key] = valusage["output_tokens"] = 0usage["windows"] = len(results)return {"model": "laya-rl-agent-onnx", "answers": answers, "usage": usage}diff --git a/tests/test_onnx_long.py b/tests/test_onnx_long.pyindex 035dbc7f..47a69e2e 100644--- a/tests/test_onnx_long.py+++ b/tests/test_onnx_long.py@@ -177,6 +177,42 @@ check_true("aggregate/the fixture actually varies across windows (parity is notlen({pw["answers"]["dept"]["answer_confidence"] for pw in per_window}) > 1)+# ------------------------------------------------- per-question usage fields merge across windows+# `usage["options"]` is a dict keyed by question id and is set only on windows where option+# spans collapsed, so it is a per-question record rather than a scalar counter. Replacing it+# per window left the caller holding whichever collapsing window came last, and the deciding+# window is the most confident one rather than the last one. Every window is given a record+# keyed by its own index, so the aggregate must keep all of them.+_onnx = _bare_onnx()+_orig_batch = _onnx.predict_batch+_scan = []+++def _with_collapse(sts, q, **kw):+ _scan.extend(sts)+ rows = _orig_batch(list(sts), q, **kw)+ for i, row in enumerate(rows):+ row["usage"] = dict(row["usage"])+ row["usage"]["options"] = {"w%d" % i: {"total": 10 + i, "distinct": i + 1,+ "tokens_per_option": 0.5}}+ return rows+++_onnx.predict_batch = _with_collapse+_merged = _onnx.predict_long(LONG_STATE, QUESTIONS)+_seen = _merged["usage"].get("options") or {}+_nwin = len(_scan)+_won = _merged["answers"]["dept"]["window"]["index"]+check("collapse/every window record survives the merge",+ sorted(_seen), sorted("w%d" % i for i in range(_nwin)))+check_true("collapse/the deciding window kept its own record",+ "w%d" % _won in _seen, sorted(_seen))+check("collapse/record contents are carried per window, not summed",+ [_seen.get("w%d" % i, {}).get("total") for i in range(_nwin)],+ [10 + i for i in range(_nwin)])+check_true("collapse/more than one window, so this is a real test", _nwin > 1, _nwin)++# ---------------------------------------------------------------- shared session runs and chunkingshared = _bare_onnx()shared.predict_long(LONG_STATE, QUESTIONS)diff --git a/tests/test_predict_long.py b/tests/test_predict_long.pyindex 67d54d69..2c4b7c2f 100644--- a/tests/test_predict_long.py+++ b/tests/test_predict_long.py@@ -170,6 +170,53 @@ check("long/noul names the deciding window", res["answers"]["flag"]["window"]["icheck("long/window start is the 3rd overlap offset", res["answers"]["dept"]["window"]["token_start"], 72)check("long/window carries the count", res["answers"]["flag"]["window"]["count"], nwin)++# 2b. a per-question usage field is merged across windows, not replaced+# `usage["options"]` is a dict keyed by question id, set only on the windows where option+# spans actually collapsed. The deciding window is the most confident one, so it is not+# necessarily the last window that collapsed: replacing instead of merging reported a+# collapse for a window that did not decide and dropped the deciding window's own record.+def collapsing(states, q):+ # window 0 collapses dept and is the most confident; every later window collapses flag,+ # so the last collapsing window is not the deciding one+ out = []+ for i, _ in enumerate(states):+ conf = 0.9 if i == 0 else 0.4+ which = "dept" if i == 0 else "flag"+ total, distinct = (58, 42) if which == "dept" else (30, 12)+ opt = {which: {"total": total, "distinct": distinct, "tokens_per_option": 0.5}}+ out.append({"answers": {+ "dept": {"type": "choice", "choice": "b" if i == 0 else "a",+ "probabilities": {"a": 1 - conf, "b": conf}, "confidence": conf,+ "answer_confidence": conf, "action": {"act_probability": 1.0}},+ "flag": {"type": "noul", "noul": 0.9 if i == 0 else 0.1, "confidence": conf,+ "answer_confidence": conf, "action": {"act_probability": 1.0}},+ }, "usage": {"input_tokens": 10, "options": opt}})+ return out+++_seen_windows = []+++def collapsing_capturing(states, q, **kw):+ _seen_windows.extend(states)+ return collapsing(states, q, **kw)+++a = make_agent(collapsing_capturing)+res = a.predict_long({"body": "y" * 300}, Q)+opts = res["usage"].get("options") or {}+nwin = len(_seen_windows)+decided = res["answers"]["dept"]["window"]["index"]+check("collapse/records from more than one window survive", sorted(opts), ["dept", "flag"])+check("collapse/the deciding window is the confident first one", decided, 0)+check_true("collapse/which is not the last window scanned", decided != nwin - 1, [decided, nwin])+check("collapse/so the deciding window's own record is reported", opts.get("dept"), {+ "total": 58, "distinct": 42, "tokens_per_option": 0.5})+check("collapse/a later non-deciding window is reported too", opts.get("flag"), {+ "total": 30, "distinct": 12, "tokens_per_option": 0.5})+check("collapse/numerics still sum across every window", res["usage"]["input_tokens"], 10 * nwin)+# 3. only aggregate="auto" is supporteda = make_agent(canned)check_raises("aggregate/rejects unknown mode", ValueError,