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): thread lang/lang_temperatures so ONNXAgent matches Agent

AlKor13964b59488dd8
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index c093bfde..070e13d2 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -103,6 +103,7 @@ jobs:
python tests/test_hooks_api.py
python tests/test_structured.py
python tests/test_structured_api.py
+ python tests/test_onnx_lang_parity.py
python tests/test_download.py
python tests/test_shortlist.py
python tests/test_decision_model.py
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index b80af635..8344b4ba 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -42,6 +42,7 @@ jobs:
python tests/test_criteria.py
python tests/test_batch.py
python tests/test_predict_long.py
+ python tests/test_onnx_lang_parity.py
python tests/test_email.py
python tests/test_download.py
python tests/test_shortlist.py
diff --git a/laya/onnx_agent.py b/laya/onnx_agent.py
index 0f9c4182..3f31e6df 100644
--- a/laya/onnx_agent.py
+++ b/laya/onnx_agent.py
@@ -46,6 +46,7 @@ class ONNXAgent(HookRegistry):
on_predict_end=None,
hooks_raise: bool = True,
hooks_concurrent: bool = True,
+ lang_temperatures: Optional[Dict[str, Dict[str, Any]]] = None,
):
"""Load a Laya agent backed by ONNX Runtime.
@@ -66,6 +67,10 @@ class ONNXAgent(HookRegistry):
on_predict_end (PredictHookArg): An opt-in end hook, run after inference.
hooks_raise: When False, a failing hook warns and inference continues.
hooks_concurrent: When False, hooks are serialised with a lock.
+ lang_temperatures: Optional per-language temperature overrides, keyed by language
+ code, each `{"temperature": [3 floats], "temperature_by_options": {}}`.
+ Applied when a `lang=` is passed to `system_one`/`predict`, mirroring
+ the PyTorch `Agent`; a cross-backend swap otherwise loses calibration.
"""
self.hooks = normalise_hooks(hooks, on_predict_start, on_predict_end)
self.hooks_raise = bool(hooks_raise)
@@ -161,6 +166,19 @@ class ONNXAgent(HookRegistry):
self.temperature = [clamp_temperature(t) for t in self.temperature_raw]
self.temperature_by_options = {k: clamp_temperature(v)
for k, v in self.temperature_by_options_raw.items()}
+ # Per-language temperature overrides, built exactly as the PyTorch Agent does so a caller
+ # can hand the same `lang_temperatures` to either backend and read the same confidence.
+ self.lang_temperatures = {}
+ for l, lcfg in (lang_temperatures or {}).items():
+ norm_l = l.split("-")[0].lower()
+ t_raw = lcfg.get("temperature", self.temperature_raw)
+ if len(t_raw) != 3:
+ raise ValueError("Language override %r temperature must be a list of 3 floats" % l)
+ tbo_raw = lcfg.get("temperature_by_options", {})
+ self.lang_temperatures[norm_l] = {
+ "temperature": [clamp_temperature(t) for t in t_raw],
+ "temperature_by_options": {k: clamp_temperature(v) for k, v in tbo_raw.items()},
+ }
entries = [(k, v, self.temperature_by_options[k]) for k, v in self.temperature_by_options_raw.items()]
entries += [("temperature[%d]" % i, t, self.temperature[i]) for i, t in enumerate(self.temperature_raw)]
rejected = []
@@ -198,11 +216,16 @@ class ONNXAgent(HookRegistry):
return q
def system_one(self, state: Union[str, dict, list], questions: Dict[str, Dict[str, Any]],
+ lang: Optional[str] = None,
hooks=None, on_predict_start=None, on_predict_end=None,
hooks_raise: Optional[bool] = None,
max_len: Optional[int] = None,
head_max_len: Optional[int] = None) -> Dict[str, Any]:
- """Evaluate typed questions, running any opt-in hooks around the inference."""
+ """Evaluate typed questions, running any opt-in hooks around the inference.
+
+ `lang` selects a per-language temperature override (see `lang_temperatures`), matching the
+ PyTorch `Agent.system_one` signature so either backend is a drop-in for the other.
+ """
active = compose_hooks(self.hooks, hooks, on_predict_start, on_predict_end)
raise_errors = self.hooks_raise if hooks_raise is None else bool(hooks_raise)
ctx = PredictContext(states=[state], questions=questions, model=self.model_id, agent=self,
@@ -215,7 +238,7 @@ class ONNXAgent(HookRegistry):
overrides["max_len"] = ctx.max_len
if ctx.head_max_len is not None:
overrides["head_max_len"] = ctx.head_max_len
- ctx.results = [self._infer(ctx.states[0], ctx.questions, **overrides)]
+ ctx.results = [self._infer(ctx.states[0], ctx.questions, lang=lang, **overrides)]
except BaseException as exc:
ctx.error = exc
try:
@@ -237,7 +260,8 @@ class ONNXAgent(HookRegistry):
return ctx.results[0]
def _infer(self, state: Union[str, dict, list], questions: Dict[str, Dict[str, Any]],
- max_len: Optional[int] = None, head_max_len: Optional[int] = None) -> Dict[str, Any]:
+ max_len: Optional[int] = None, head_max_len: Optional[int] = None,
+ lang: Optional[str] = None) -> Dict[str, Any]:
from .agent import Agent as _Agent
ids = list(questions.keys())
@@ -300,6 +324,9 @@ class ONNXAgent(HookRegistry):
k = len(items[r]["markers"])
qt = QTYPES[q["t"]]
t_scale = self.temperature_by_options.get(temp_bucket(qt, k), self.temperature[qt])
+ if lang and lang.split("-")[0].lower() in self.lang_temperatures:
+ l_cfg = self.lang_temperatures[lang.split("-")[0].lower()]
+ t_scale = l_cfg["temperature_by_options"].get(temp_bucket(qt, k), l_cfg["temperature"][qt])
z = logits[r, :k] / t_scale
p = np.exp(z - z.max())
p = p / p.sum()
diff --git a/tests/test_onnx_lang_parity.py b/tests/test_onnx_lang_parity.py
new file mode 100644
index 00000000..4a9bae6f
--- /dev/null
+++ b/tests/test_onnx_lang_parity.py
@@ -0,0 +1,145 @@
+"""ONNXAgent must accept `lang` / `lang_temperatures` like the PyTorch Agent.
+
+A caller who configures per-language temperatures and passes `lang=` to `Agent.system_one`
+gets calibrated confidence. Swapping the same call to `ONNXAgent` used to raise
+`TypeError: unexpected keyword argument 'lang'`, and an ONNXAgent built with `lang_temperatures`
+silently ignored them. This checks the parity without onnxruntime or a checkpoint: onnxruntime
+is imported lazily inside `__init__`, so the class imports, and `_infer` is driven with a stub
+session and a fake tokenizer (the same technique as tests/test_portability.py).
+
+Run: python tests/test_onnx_lang_parity.py
+"""
+import inspect
+import os
+import sys
+
+sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+
+import numpy as np # noqa: E402
+
+from laya.agent import Agent # noqa: E402
+from laya.common import clamp_temperature # noqa: E402
+from laya.onnx_agent import ONNXAgent # noqa: E402
+
+PASS, FAIL = [], []
+
+
+def check(name, got, want):
+ if got == want:
+ PASS.append(name)
+ else:
+ FAIL.append("%s:\n got %r\n want %r" % (name, got, want))
+
+
+def check_true(name, cond, detail=""):
+ if cond:
+ PASS.append(name)
+ else:
+ FAIL.append("%s %s" % (name, detail))
+
+
+# ---------------------------------------------------------------- signature parity with the torch Agent
+onnx_so = inspect.signature(ONNXAgent.system_one).parameters
+check_true("signature/system_one accepts lang", "lang" in onnx_so,
+ "params were %s" % list(onnx_so))
+check_true("signature/predict is system_one (accepts lang too)",
+ "lang" in inspect.signature(ONNXAgent.predict).parameters)
+check_true("signature/__init__ accepts lang_temperatures",
+ "lang_temperatures" in inspect.signature(ONNXAgent.__init__).parameters)
+# the torch Agent is the contract both sides must satisfy
+check_true("signature/matches the torch Agent",
+ "lang" in inspect.signature(Agent.system_one).parameters)
+
+
+# ---------------------------------------------------------------- a fake tokenizer + stub session drive _infer
+class _FakeTok:
+ cls_token_id, sep_token_id, mask_token_id, pad_token_id = 1, 2, 3, 0
+ mask_token = "[M]"
+
+ def __call__(self, text, add_special_tokens=False, truncation=False, max_length=None):
+ ids = [10 + (ord(c) % 40) for c in text]
+ if truncation and max_length:
+ ids = ids[:max_length]
+ return {"input_ids": ids}
+
+
+class _StubSession:
+ """Returns fixed logits so decoding is deterministic; one row per question, K columns."""
+
+ def __init__(self, logits, act_logits):
+ self._logits, self._act = logits, act_logits
+
+ def run(self, names, inputs):
+ return [self._logits, self._act]
+
+
+def _bare_onnx(lang_temperatures):
+ a = ONNXAgent.__new__(ONNXAgent)
+ a.model_id = "stub"
+ a.cfg = {"max_len": 64, "head_max_len": 32}
+ a.tok = _FakeTok()
+ a.temperature = [1.0, 1.0, 1.0]
+ a.temperature_by_options = {}
+ # two questions -> two rows; three option columns cover the widest question (the choice)
+ a.session = _StubSession(
+ np.array([[2.0, 1.0, 0.0], [2.0, 0.0, 0.0]], dtype=np.float32), # logits[row, :k]
+ np.array([[0.7, 0.3], [0.4, 0.6]], dtype=np.float32), # act_logits
+ )
+ a.lang_temperatures = {}
+ for l, cfg in (lang_temperatures or {}).items():
+ norm = l.split("-")[0].lower()
+ a.lang_temperatures[norm] = {
+ "temperature": [clamp_temperature(t) for t in cfg["temperature"]],
+ "temperature_by_options": {k: clamp_temperature(v)
+ for k, v in cfg.get("temperature_by_options", {}).items()},
+ }
+ return a
+
+
+QUESTIONS = {
+ "dept": {"type": "choice", "instructions": "Which team?",
+ "criteria": {"billing": "money", "support": "help", "sales": "buy"}},
+ "flag": {"type": "noul", "instructions": "Urgent?"},
+}
+
+# A German override with a clearly different temperature than the base [1,1,1].
+agent = _bare_onnx({"de": {"temperature": [3.0, 3.0, 3.0]}})
+
+base = agent._infer("some state text", QUESTIONS)
+de = agent._infer("some state text", QUESTIONS, lang="de")
+missing = agent._infer("some state text", QUESTIONS, lang="fr") # not configured -> base
+
+p_base = base["answers"]["dept"]["probabilities"]
+p_de = de["answers"]["dept"]["probabilities"]
+p_missing = missing["answers"]["dept"]["probabilities"]
+
+check_true("lang/override changes the calibrated probabilities",
+ p_base["billing"] != p_de["billing"],
+ "base=%r de=%r" % (p_base, p_de))
+# temperature 3 > 1 softens the distribution: the argmax probability falls
+check_true("lang/higher temperature softens the top probability",
+ p_de["billing"] < p_base["billing"])
+check("lang/unconfigured language falls back to base", p_missing, p_base)
+# a hyphen subtag resolves to the base language
+de_DE = agent._infer("some state text", QUESTIONS, lang="de-DE")
+check("lang/hyphen subtag resolves to the override",
+ de_DE["answers"]["dept"]["probabilities"], p_de)
+# the noul question is scaled the same way
+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
+try:
+ _bare_onnx_bad = ONNXAgent.__new__(ONNXAgent)
+ # the parsing lives in __init__; assert the same guard exists in its source
+ src = inspect.getsource(ONNXAgent.__init__)
+ check_true("lang/__init__ validates the 3-float override",
+ "must be a list of 3 floats" in src)
+except Exception as e: # noqa: BLE001
+ FAIL.append("lang/guard check raised %r" % e)
+
+
+print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))
+for f in FAIL:
+ print(" FAIL " + f)
+sys.exit(1 if FAIL else 0)