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(agent): make usage["windows"] total on every predict_long path

Aashishea222a6113a4
diff --git a/README.md b/README.md
index c339df94..e1d8a8b7 100644
--- a/README.md
+++ b/README.md
@@ -576,7 +576,9 @@ result = agent.predict_long(state, questions, hooks=[AuditLog()]) # the scan,
- `noul` takes the strongest window (the statement holds if any window supports it).
- `choice` / `score` take the most-confident window — averaging over a long, mostly-neutral
document lets the neutral majority out-vote the one window that saw the deciding span.
-- A state that already fits one window is passed straight to `system_one` (identical output).
+- A state that already fits one window is passed straight to `system_one` (identical output, plus
+ `usage["windows"] = 1`). The key is total: `1` single window, `N` scanned windows, `0` a hook
+ answered a multi-window document before the model read any of it.
- Hooks wrap the inference that answers the state, so on a scanned document `on_predict_start`
fires once with `ctx.states` holding the decoded windows, not the state you passed in (it was
tokenized to produce them). A start hook that answers with `ctx.skip(...)` is answering the
diff --git a/docs/hooks/api.md b/docs/hooks/api.md
index fc14b733..e6d904d2 100644
--- a/docs/hooks/api.md
+++ b/docs/hooks/api.md
@@ -244,7 +244,10 @@ agent.predict(...) # alias of system_one
once, and `ctx.states` holds the decoded window texts in scan order rather than the caller's
state, which was tokenized to produce them. A start hook that answers with `ctx.skip(...)` is
answering the document, so its one result is returned as-is with no `window` key and
- `usage["windows"]` at `0` -- no window scored it.
+ `usage["windows"]` at `0` -- no window scored it. `usage["windows"]` is total over the three
+ paths: `0` here, `1` for a state that fit a single window (a hook that answered it is answering
+ that one window, so the count is the same as the model's), and `N` for a document scanned in `N`
+ overlapping windows.
### Router
diff --git a/laya/agent.py b/laya/agent.py
index 5cc8d044..c7407a31 100644
--- a/laya/agent.py
+++ b/laya/agent.py
@@ -1011,6 +1011,9 @@ class Agent(HookRegistry):
hooks_timeout: Override the Agent's `hooks_timeout` for this call.
Returns a single result dict, the same shape as `system_one`, with `usage["windows"]` added.
+ The key is always present and counts the windows the answer was read from: `1` for a state
+ that fit one window, `N` for a document scanned in `N` overlapping windows, and `0` when a
+ start hook answered a multi-window document before any window was scored.
"""
if aggregate != "auto":
raise ValueError("predict_long: only aggregate='auto' is supported")
@@ -1023,9 +1026,15 @@ class Agent(HookRegistry):
state_ids = self.tok(serialize_state(state).replace(self.tok.mask_token, " "),
add_special_tokens=False)["input_ids"]
- # Fits in one window: identical to a plain call, no windowing overhead.
+ # Fits in one window: identical to a plain call, no windowing overhead. `windows` is still
+ # written, so the key is total over the three paths this method can take and a caller can
+ # ask "how much of the document did the model read?" without handling a KeyError on the
+ # shortest, most common inputs. The result is copied first: a start hook that answers with
+ # `ctx.skip(...)` hands back its own payload dict, and it may be a cached object.
if len(state_ids) <= budget:
- return self.system_one(state, questions, lang=lang, **hook_kwargs)
+ single = dict(self.system_one(state, questions, lang=lang, **hook_kwargs))
+ single["usage"] = {**(single.get("usage") or {}), "windows": 1}
+ return single
step = stride if (stride and stride > 0) else max(1, budget // 2)
windows, starts = [], []
diff --git a/tests/test_predict_long.py b/tests/test_predict_long.py
index d02eb3bc..a174b689 100644
--- a/tests/test_predict_long.py
+++ b/tests/test_predict_long.py
@@ -7,6 +7,7 @@ window), and the per-call hook controls it forwards to whichever of those two ca
behaviour on real weights is exercised in tests/test_local_e2e.py.
"""
import os
+import re
import sys
import warnings
@@ -74,6 +75,17 @@ a = make_agent(lambda s, q: [])
short = a.predict_long({"body": "x" * 50}, Q) # 50 tokens <= budget 72
check("short/delegates to system_one", short["answers"], {"_via": "system_one"})
check("short/no predict_batch call", a._calls["batch_states"], None)
+check("short/one window is reported", short["usage"]["windows"], 1)
+check("short/system_one's own usage is kept", short["usage"]["input_tokens"], 1)
+
+# 1b. the count is added to a copy, because a start hook that skips hands back the caller's own
+# payload dict and that object may be cached and reused
+payload = {"model": "laya-rl-agent", "answers": {"_via": "system_one"}, "usage": {"input_tokens": 1}}
+a = make_agent(lambda s, q: [])
+a.system_one = lambda state, questions, lang=None, **controls: payload
+via_hook = a.predict_long({"body": "x" * 50}, Q)
+check("short/a new result dict comes back", via_hook is payload, False)
+check("short/the caller's payload is not written to", payload["usage"], {"input_tokens": 1})
# 2. long state -> overlapping windows, aggregated per question
def canned(states, q):
@@ -173,6 +185,31 @@ check("skip/the caller is told", [w.category.__name__ for w in caught], ["Runtim
a = make_agent(lambda s, q: [{"answers": {}, "usage": {}}] * 2)
check_raises("skip/rejects a count matching nothing", ValueError, lambda: a.predict_long(LONG, Q))
+# 10. the count is total, so one question works on every result predict_long can return
+short_all = make_agent(lambda s, q: []).predict_long({"body": "x" * 50}, Q)
+scan_agent = make_agent(canned)
+scanned_all = scan_agent.predict_long(LONG, Q)
+with warnings.catch_warnings():
+ warnings.simplefilter("ignore")
+ hook_all = make_agent(one_document_answer).predict_long(LONG, Q)
+check("total/no path leaves the key out",
+ [r["usage"].get("windows", "<absent>") for r in (short_all, scanned_all, hook_all)],
+ [1, len(scan_agent._calls["batch_states"]), 0])
+check("total/0 separates a hook answer from a single-window one",
+ (hook_all["usage"]["windows"], short_all["usage"]["windows"]), (0, 1))
+
+
+# 11. the page that teaches this key teaches the value the code actually writes
+README = open(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
+ "README.md"), encoding="utf-8").read()
+bullet = README[README.index("A state that already fits one window"):][:600]
+check("docs/README pins the single-window value", 'usage["windows"] = 1' in bullet, True)
+check("docs/README states the key is total", "The key is total" in bullet, True)
+check("docs/README documents all three counts", sorted(set(re.findall(r"`([0-9N])`", bullet))),
+ ["0", "1", "N"])
+check("docs/predict_long's docstring says the key is total",
+ "always present" in (Agent.predict_long.__doc__ or ""), True)
+
print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))
for f in FAIL:
print(" FAIL " + f)