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(calibrate): skip global binning under lang temperature override

Bruce-Yiic7cde4575837
diff --git a/laya/agent.py b/laya/agent.py
index bf17f6b8..8fdaee89 100644
--- a/laya/agent.py
+++ b/laya/agent.py
@@ -1218,7 +1218,8 @@ class Agent(HookRegistry):
# callers, report both: `answer_confidence` is the calibrated one, on every
# question type, so a caller can gate across types on a single number.
ans_raw = answer_confidence(p, k)
- if getattr(self, "binning_map", None):
+ lang_override = bool(lang and lang.split("-")[0].lower() in self.lang_temperatures)
+ if getattr(self, "binning_map", None) and not lang_override:
ans_conf = round(
apply_binning_map(ans_raw, temp_bucket(qt, k), self.binning_map), 4
)
diff --git a/laya/onnx_agent.py b/laya/onnx_agent.py
index d87b50f2..5ca5fb93 100644
--- a/laya/onnx_agent.py
+++ b/laya/onnx_agent.py
@@ -670,9 +670,8 @@ class ONNXAgent(HookRegistry):
# 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_raw = answer_confidence(p, k)
- if binning_map:
- from .calibrate import apply_binning_map
-
+ lang_override = bool(lang and lang.split("-")[0].lower() in self.lang_temperatures)
+ if binning_map and not lang_override:
ans_conf = round(
apply_binning_map(ans_raw, temp_bucket(qt, k), binning_map), 4
)
diff --git a/tests/test_calibrate.py b/tests/test_calibrate.py
index a0c74401..21cbabfe 100644
--- a/tests/test_calibrate.py
+++ b/tests/test_calibrate.py
@@ -533,6 +533,23 @@ onnx_select.binning_map = {"noul:2": {"bins": 2, "values": [0.95, 0.05]}}
onnx_with_map = onnx_select._decode_answers(select_logits, bin_act, bin_items, onnx_select_ids, onnx_select_internal, 0)
check("onnx/binning remaps confidence", onnx_with_map["flag"]["answer_confidence"], 0.05)
+agent_bin = Agent.__new__(Agent)
+agent_bin.temperature = [1.0, 1.0, 1.0]
+agent_bin.temperature_by_options = {}
+agent_bin.binning_map = {"noul:2": {"bins": 2, "values": [0.95, 0.05]}}
+agent_binned = agent_bin._decode_answers(select_logits, bin_act, bin_items, onnx_select_ids, onnx_select_internal, 0)
+check("agent/binning remaps confidence", agent_binned["flag"]["answer_confidence"], 0.05)
+agent_bin.lang_temperatures = {"zh": {"temperature": [1.0, 1.0, 1.0], "temperature_by_options": {}}}
+agent_lang = agent_bin._decode_answers(select_logits, bin_act, bin_items, onnx_select_ids, onnx_select_internal, 0, lang="zh")
+check("agent/lang override skips binning", agent_lang["flag"]["answer_confidence"], 0.8)
+onnx_lang = ONNXAgent.__new__(ONNXAgent)
+onnx_lang.temperature = [1.0, 1.0, 1.0]
+onnx_lang.temperature_by_options = {}
+onnx_lang.binning_map = {"noul:2": {"bins": 2, "values": [0.95, 0.05]}}
+onnx_lang.lang_temperatures = {"zh": {"temperature": [1.0, 1.0, 1.0], "temperature_by_options": {}}}
+onnx_runtime = onnx_lang._decode_answers(select_logits, bin_act, bin_items, onnx_select_ids, onnx_select_internal, 0, lang="zh")
+check("onnx/lang override skips binning", onnx_runtime["flag"]["answer_confidence"], 0.8)
+
# A payload with no version is the original schema and must still load, even onto an
# agent that has its own identity. No warning: there is no recorded checkpoint to disagree with.
legacy = {"temperature": [1.4, 1.2, 1.1], "temperature_by_options": {"noul:2": 1.5}}