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 #501 from PerryLink/fix/onnx-maxlen-message

Nandakishorab525a8aff34
diff --cc laya/onnx_agent.py
index f53ad4c5,35c544e0..327dd07a
--- a/laya/onnx_agent.py
+++ b/laya/onnx_agent.py
@@@ -485,18 -250,46 +485,29 @@@ 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:
+ # 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.
+ 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))
items.append({"ids": seq, "markers": markers, "qtype": QTYPES[q["t"]]})
+ return items
- 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)
-
+ 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])