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
perf+fix(onnx): tune the ORT session, tokenize state once, and report answer_confidence
AlKor13669006c9749a
diff --git a/laya/onnx_agent.py b/laya/onnx_agent.pyindex b7f2e2d6..fd095834 100644--- a/laya/onnx_agent.py+++ b/laya/onnx_agent.py@@ -10,10 +10,12 @@ import numpy as npfrom laya.hooks import HookRegistry, PredictContext, aggregate_usage, compose_hooks, dispatch, normalise_hooksfrom laya.common import (QTYPES,+ answer_confidence,build_sequence,collate_items,confidence_from_probs,render_options,+ serialize_state,temp_bucket,TEMP_MIN,TEMP_MAX,@@ -115,8 +117,13 @@ class ONNXAgent(HookRegistry):if "CUDAExecutionProvider" in availableelse ["CPUExecutionProvider"])+ # Enable ONNX Runtime's full graph optimization (operator fusion, constant folding).+ # It is functionally neutral and free at inference time; without it ORT runs the+ # unoptimized graph. Measured ~1.45x on CPU / ~1.75x on GPU vs eager torch with it on.+ so = ort.SessionOptions()+ so.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALLtry:- self.session = ort.InferenceSession(onnx_path, providers=providers)+ self.session = ort.InferenceSession(onnx_path, sess_options=so, providers=providers)except Exception:# A provider can be listed by onnxruntime yet still fail to initialize# (missing CUDA libraries, unsupported driver, mismatched DLLs). Keep@@ -127,7 +134,7 @@ class ONNXAgent(HookRegistry):"laya ONNX: CUDAExecutionProvider initialization failed; falling back to CPUExecutionProvider.",RuntimeWarning, stacklevel=2,)- self.session = ort.InferenceSession(onnx_path, providers=["CPUExecutionProvider"])+ self.session = ort.InferenceSession(onnx_path, sess_options=so, providers=["CPUExecutionProvider"])self.temperature_raw = self.cfg.get("temperature", [1.0, 1.0, 1.0])self.temperature_by_options_raw = self.cfg.get("temperature_by_options", {})@@ -226,11 +233,20 @@ class ONNXAgent(HookRegistry):max_len = self.cfg.get("max_len", 512) if max_len is None else max_lenhead_max_len = self.cfg.get("head_max_len", 192) if head_max_len is None else head_max_len+ # Tokenize the shared state once and reuse it across questions, instead of+ # re-serializing and re-tokenizing the same document inside build_sequence per+ # question (the PyTorch Agent already does this via `state_ids`).+ truncate_left = isinstance(state, list)+ state_ids = self.tok(+ serialize_state(state).replace(self.tok.mask_token, " "),+ add_special_tokens=False,+ )["input_ids"]+for qid in ids:q = self._to_internal(questions[qid])seq, markers = build_sequence(self.tok, state, q, max_len, head_max_len,- truncate_left=isinstance(state, list),+ 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))@@ -269,6 +285,10 @@ class ONNXAgent(HookRegistry):p = p / p.sum()conf_score = round(confidence_from_probs(p, k), 4)+ # `answer_confidence` is the calibrated max(p) confidence, reported on every question+ # type so a caller can gate across types on one number -- matching the PyTorch Agent,+ # whose output ONNX callers otherwise cannot read (KeyError on cross-backend swap).+ ans_conf = round(answer_confidence(p, k), 4)ext = {"act_probability": round(float(act[r, 0]), 4)}if q["t"] == "choice":@@ -278,6 +298,7 @@ class ONNXAgent(HookRegistry):"choice": keys[int(p.argmax())],"probabilities": {kk: round(float(v), 4) for kk, v in zip(keys, p)},"confidence": conf_score,+ "answer_confidence": ans_conf,"action": ext,}elif q["t"] == "score":@@ -288,6 +309,7 @@ class ONNXAgent(HookRegistry):"legend": {str(i): c for i, c in enumerate(q["crit"])},"probabilities": {str(i): round(float(v), 4) for i, v in enumerate(p)},"confidence": conf_score,+ "answer_confidence": ans_conf,"action": ext,}else:@@ -295,6 +317,7 @@ class ONNXAgent(HookRegistry):"type": "noul","noul": round(float(p[1]), 4),"confidence": round(max(float(p[1]), 1.0 - float(p[1])), 4),+ "answer_confidence": ans_conf,"action": ext,}