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): answer single-option questions instead of failing in TopK

hemanthrayuduu3313265690be
diff --git a/laya/onnx_agent.py b/laya/onnx_agent.py
index 714ddd8c..77fa87e5 100644
--- a/laya/onnx_agent.py
+++ b/laya/onnx_agent.py
@@ -775,12 +775,24 @@ class ONNXAgent(HookRegistry):
b = collate_items(per_state_items, self.tok.pad_token_id)
+ marker_pos = b["marker_pos"].numpy().astype(np.int64)
+ marker_mask = b["marker_mask"].numpy().astype(bool)
+ if marker_pos.shape[1] < 2:
+ # The exported head takes `topk(2)` of the option probabilities: the eager
+ # forward's one-option branch is a Python `if`, which the trace bakes in, so
+ # ONNX Runtime refuses a run whose questions all have one option. A masked
+ # second slot scores -1e4, is sliced off at decode, and gives the act head the
+ # zero second probability the eager branch pads in.
+ width = ((0, 0), (0, 2 - marker_pos.shape[1]))
+ marker_pos = np.pad(marker_pos, width)
+ marker_mask = np.pad(marker_mask, width)
+
# 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),
+ "marker_pos": marker_pos,
+ "marker_mask": marker_mask,
"qtype": b["qtype"].numpy().astype(np.int64),
}
diff --git a/tests/test_onnx_batch.py b/tests/test_onnx_batch.py
index a9f6cedf..97d9f58a 100644
--- a/tests/test_onnx_batch.py
+++ b/tests/test_onnx_batch.py
@@ -308,6 +308,60 @@ check("router/results carry the routing key", [sorted(r) for r in routed],
check("router/two states share one session run", router_agent.session.calls, [2 * 2])
+# ---------------------------------------------------------------- single-option questions
+# The exported graph traces the decision head's `topk(2)` (the eager forward's one-option branch is
+# a Python `if` on the marker count, which the trace bakes in), so ONNX Runtime refused a run whose
+# questions all had one option -- "k argument [2] should not be greater than specified axis dim
+# value [1]" -- while the PyTorch Agent answered it. This stub enforces the same rule, and scores a
+# masked slot -1e4 the way the graph's `masked_fill` does.
+class _ExportedGraphSession:
+ def __init__(self):
+ self.marker_widths = []
+ self.masks = []
+
+ def run(self, names, inputs):
+ mask = inputs["marker_mask"]
+ self.marker_widths.append(mask.shape[1])
+ self.masks.append(mask.tolist())
+ if mask.shape[1] < 2:
+ raise RuntimeError("[ONNXRuntimeError] : 1 : FAIL : Non-zero status code returned while "
+ "running TopK node. Status Message: k argument [2] should not be "
+ "greater than specified axis dim value [%d]" % mask.shape[1])
+ logits = np.where(mask, 1.0 + np.arange(mask.shape[1], dtype=np.float32), -1e4).astype(np.float32)
+ act = np.tile(np.array([[0.25, 0.75]], dtype=np.float32), (len(mask), 1))
+ return [logits, act]
+
+
+ONE_OPTION = {"dept": {"type": "choice", "instructions": "Which team?", "criteria": {"billing": "money"}}}
+ONE_LEVEL = {"level": {"type": "score", "instructions": "How urgent?", "criteria": ["not urgent"]}}
+
+for label, questions, key, want in (("choice", ONE_OPTION, "dept", {"billing": 1.0}),
+ ("score", ONE_LEVEL, "level", {"0": 1.0})):
+ single = _bare_onnx()
+ single.session = _ExportedGraphSession()
+ try:
+ got = single.predict("aa", questions)["answers"][key]["probabilities"]
+ except RuntimeError as e:
+ got = str(e)[:80]
+ check("single option/a one-option %s is answered, not refused by the graph" % label, got, want)
+ check("single option/%s: the extra slot is padded and masked" % label, single.session.masks, [[[True, False]]])
+
+single = _bare_onnx()
+single.session = _ExportedGraphSession()
+try:
+ got = [r["answers"]["dept"]["probabilities"] for r in single.predict_batch(STATES, ONE_OPTION)]
+except RuntimeError as e:
+ got = str(e)[:80]
+check("single option/the batch path answers too, in one run", (got, single.session.marker_widths),
+ ([{"billing": 1.0}] * len(STATES), [2]))
+
+wide = _bare_onnx()
+wide.session = _ExportedGraphSession()
+wide.predict("aa", QUESTIONS)
+check("single option/a run that already has two or more slots is sent as before",
+ wide.session.marker_widths, [3])
+
+
# --------------------------------------------------------------------------- report
print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))
for f in FAIL: