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.

Merge pull request #455 from PerryLink/fix/maxlen-message

Nandakishorffb564c28eab
diff --cc laya/agent.py
index 953a3124,55cdaa53..a8f2fbd2
--- a/laya/agent.py
+++ b/laya/agent.py
@@@ -822,13 -670,26 +822,28 @@@ class Agent(HookRegistry)
items = []
for qid in ids:
q = internal[qid]
- seq, markers = build_sequence(self.tok, state, q, max_len, head_max_len,
- truncate_left=truncate_left, state_ids=state_ids)
+ seq, markers, stats = build_sequence(self.tok, state, q, max_len, head_max_len,
+ option_order=q.get("option_order"),
+ truncate_left=truncate_left, state_ids=state_ids,
+ return_stats=True)
- 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"]]})
+ items.append({"ids": seq, "markers": markers, "qtype": QTYPES[q["t"]], "options": stats})
return items
def _amp_enabled_for(self, rows: int) -> bool:
diff --cc laya/onnx_agent.py
index 0b47943d,192eea02..dee6431b
--- a/laya/onnx_agent.py
+++ b/laya/onnx_agent.py
@@@ -543,27 -305,44 +543,27 @@@ class ONNXAgent(HookRegistry)
)
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.
+ # 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: its %d options and question text need %d tokens, more than "
- "max_len=%d allows once head_max_len=%d is spent on them; lower head_max_len, "
- "raise max_len, or use fewer options"
- % (qid, n_opts, len(seq), max_len, head_max_len))
+ "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)
-
- # Prepare ONNX inputs as numpy arrays
- ort_inputs = {
- "input_ids": b["input_ids"].numpy().astype(np.int64),
- "attention_mask": b["attention_mask"].numpy().astype(np.int64),
- "marker_pos": b["marker_pos"].numpy().astype(np.int64),
- "marker_mask": b["marker_mask"].numpy().astype(bool),
- "qtype": b["qtype"].numpy().astype(np.int64),
- }
-
- # Run ONNX inference
- ort_outs = self.session.run(["logits", "act_logits"], ort_inputs)
- logits = ort_outs[0]
- act_logits = ort_outs[1]
-
- # Compute softmax for actions manually in numpy
- act_exp = np.exp(act_logits - np.max(act_logits, axis=-1, keepdims=True))
- act = act_exp / np.sum(act_exp, axis=-1, keepdims=True)
+ items.append({"ids": seq, "markers": markers, "qtype": QTYPES[q["t"]], "options": stats})
+ return items
+ def _decode_answers(self, logits, act, items: List[Dict[str, Any]], ids: List[str],
+ internal: Dict[str, Any], offset: int,
+ lang: Optional[str] = None) -> Dict[str, Any]:
+ """Decode the `len(ids)` head rows starting at `offset` into this state's answers."""
answers = {}
- n_tokens = int(b["attention_mask"].sum())
-
for r, qid in enumerate(ids):
- q = self._to_internal(questions[qid])
+ q = internal[qid]
k = len(items[r]["markers"])
qt = QTYPES[q["t"]]
t_scale = self.temperature_by_options.get(temp_bucket(qt, k), self.temperature[qt])
diff --cc tests/test_onnx_lang_parity.py
index 0a14ff34,36eae06a..588f477d
--- a/tests/test_onnx_lang_parity.py
+++ b/tests/test_onnx_lang_parity.py
@@@ -128,23 -128,33 +128,30 @@@ check("lang/hyphen subtag resolves to t
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:
+ # The parsing now lives in one shared helper (`common.resolve_lang_temperatures`) rather than
+ # being written out twice, so the guard cannot be read off this `__init__`'s source any more.
+ # Asserting that both backends CALL the helper is the stronger claim anyway: the previous
+ # version passed while the two copies had already drifted, which is how ONNXAgent kept the
+ # `cfg.get(...)`-then-`len(...)` shape after Agent was fixed.
+ src = inspect.getsource(ONNXAgent.__init__)
+ check_true("lang/__init__ parses overrides through the shared validator",
+ "resolve_lang_temperatures" in src)
+ from laya.common import resolve_lang_temperatures as _resolve
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))
+ _resolve({"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: BLE001
FAIL.append("lang/guard check raised %r" % e)