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
fix(calibrate): bin runtime confidences at full precision and validate bin values
Bruce-Yii321485776d2b
diff --git a/laya/agent.py b/laya/agent.pyindex 1bcd47ab..bf17f6b8 100644--- a/laya/agent.py+++ b/laya/agent.py@@ -1217,11 +1217,13 @@ class Agent(HookRegistry):# scaling fits and ECE measures. Rather than change one underneath existing# 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_conf = round(answer_confidence(p, k), 4)+ ans_raw = answer_confidence(p, k)if getattr(self, "binning_map", None):ans_conf = round(- apply_binning_map(ans_conf, temp_bucket(qt, k), self.binning_map), 4+ apply_binning_map(ans_raw, temp_bucket(qt, k), self.binning_map), 4)+ else:+ ans_conf = round(ans_raw, 4)ext = {"act_probability": round(float(act[r, 0]), 4)}if q["t"] == "choice":diff --git a/laya/calibrate.py b/laya/calibrate.pyindex a9ad656c..d25ce6a9 100644--- a/laya/calibrate.py+++ b/laya/calibrate.py@@ -639,10 +639,21 @@ def apply_calibration_payload(obj, payload: Dict[str, Any]) -> None:"calibration JSON binning_map[%r] must have \"values\" of length \"bins\" (%d)"% (name, n_bins))try:- [float(v) for v in values]+ parsed_values = [float(v) for v in values]except (TypeError, ValueError) as exc:raise ValueError("calibration JSON binning_map[%r] values must be numbers, got %r" % (name, values)) from exc+ for v in values:+ if isinstance(v, bool) or not isinstance(v, (int, float)):+ raise ValueError(+ "calibration JSON binning_map[%r] values must be numbers, got %r" % (name, values))+ for v in parsed_values:+ if not np.isfinite(v):+ raise ValueError(+ "calibration JSON binning_map[%r] values must be finite, got %r" % (name, values))+ if not 0.0 <= v <= 1.0:+ raise ValueError(+ "calibration JSON binning_map[%r] values must be in [0, 1], got %r" % (name, values))obj.binning_map = binningif version >= CALIBRATION_VERSION:_warn_if_identity_mismatch(obj, payload)diff --git a/laya/onnx_agent.py b/laya/onnx_agent.pyindex 899c2b48..d87b50f2 100644--- a/laya/onnx_agent.py+++ b/laya/onnx_agent.py@@ -669,11 +669,15 @@ class ONNXAgent(HookRegistry):# `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)+ ans_raw = answer_confidence(p, k)if binning_map:+ from .calibrate import apply_binning_map+ans_conf = round(- apply_binning_map(ans_conf, temp_bucket(qt, k), binning_map), 4+ apply_binning_map(ans_raw, temp_bucket(qt, k), binning_map), 4)+ else:+ ans_conf = round(ans_raw, 4)ext = {"act_probability": round(float(act[offset + r, 0]), 4)}if q["t"] == "choice":diff --git a/tests/test_calibrate.py b/tests/test_calibrate.pyindex 8d7e0541..a0c74401 100644--- a/tests/test_calibrate.py+++ b/tests/test_calibrate.py@@ -481,6 +481,58 @@ bin_decoder.binning_map = bmremapped = bin_decoder._decode_answers(bin_logits, bin_act, bin_items, bin_ids, bin_internal, 0)check("decode/binning remaps confidence", remapped["pick"]["answer_confidence"], 0.2)+# Q1: the fitter bins full-precision answer_confidence; the runtime must bin the same+# value and only round the final public field. Boundary case: a max(p) just below a+# histogram boundary rounds to the boundary, and the rounded value would pick the wrong bin.+boundary_bins = {"noul:2": {"bins": 20, "values": [round(0.05 * i, 2) for i in range(20)]}}+bounce_target = 0.9499995 # just below bin 19's start at 0.95+boundary_logits = np.log([[1.0 - bounce_target, bounce_target]]) * 1.0+boundary_act = np.array([[0.01, 0.99]])+boundary_ids = ["flag"]+boundary_internal = {"flag": {"t": "noul", "crit": None}}+boundary_items = [{"markers": [0, 1]}]++agent_boundary = Agent.__new__(Agent)+agent_boundary.temperature = [1.0, 1.0, 1.0]+agent_boundary.temperature_by_options = {}+agent_boundary.binning_map = boundary_bins+agent_decoded = agent_boundary._decode_answers(+ boundary_logits, boundary_act, boundary_items, boundary_ids, boundary_internal, 0+)+check(+ "decode/binning uses unrounded value (agent)",+ agent_decoded["flag"]["answer_confidence"],+ boundary_bins["noul:2"]["values"][18],+)++from laya.onnx_agent import ONNXAgent # noqa: E402++onnx_boundary = ONNXAgent.__new__(ONNXAgent)+onnx_boundary.temperature = [1.0, 1.0, 1.0]+onnx_boundary.temperature_by_options = {}+onnx_boundary.binning_map = boundary_bins+onnx_decoded = onnx_boundary._decode_answers(+ boundary_logits, boundary_act, boundary_items, boundary_ids, boundary_internal, 0+)+check(+ "decode/binning uses unrounded value (onnx)",+ onnx_decoded["flag"]["answer_confidence"],+ agent_decoded["flag"]["answer_confidence"],+)++# Q3: cross-backend selectability — the same installed map must move both decodes to the same value.+onnx_select = ONNXAgent.__new__(ONNXAgent)+onnx_select.temperature = [1.0, 1.0, 1.0]+onnx_select.temperature_by_options = {}+onnx_select_ids = ["flag"]+onnx_select_internal = {"flag": {"t": "noul", "crit": None}}+select_logits = np.log([[0.8, 0.2]]) * 1.0+onnx_no_map = onnx_select._decode_answers(select_logits, bin_act, bin_items, onnx_select_ids, onnx_select_internal, 0)+check("onnx/no binning keeps scaled confidence", onnx_no_map["flag"]["answer_confidence"], 0.8)+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)+# 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}}@@ -556,6 +608,13 @@ _refuses("shape/non-object payload", ["temperature", [1.0, 1.0, 1.0]], "must be_refuses("shape/binning not object", {"temperature": [1.0, 1.0, 1.0], "binning_map": [1, 2]}, "binning_map")_refuses("shape/binning wrong entry", {"temperature": [1.0, 1.0, 1.0], "binning_map": {"a": 1}}, "bins")_refuses("shape/binning values length", {"temperature": [1.0, 1.0, 1.0], "binning_map": {"a": {"bins": 2, "values": [0.5]}}}, "values")+_refuses("shape/binning value above 1", {"temperature": [1.0, 1.0, 1.0], "binning_map": {"a": {"bins": 1, "values": [1.5]}}}, "[0, 1]")+_refuses("shape/binning value below 0", {"temperature": [1.0, 1.0, 1.0], "binning_map": {"a": {"bins": 1, "values": [-0.1]}}}, "[0, 1]")+_refuses("shape/binning NaN", {"temperature": [1.0, 1.0, 1.0], "binning_map": {"a": {"bins": 1, "values": [float("nan")]}}}, "finite")+_refuses("shape/binning Infinity", {"temperature": [1.0, 1.0, 1.0], "binning_map": {"a": {"bins": 1, "values": [float("inf")]}}}, "finite")+_refuses("shape/binning bool value", {"temperature": [1.0, 1.0, 1.0], "binning_map": {"a": {"bins": 1, "values": [True]}}}, "number")+_refuses("shape/binning string value", {"temperature": [1.0, 1.0, 1.0], "binning_map": {"a": {"bins": 1, "values": ["0.5"]}}}, "number")+_refuses("shape/binning null value", {"temperature": [1.0, 1.0, 1.0], "binning_map": {"a": {"bins": 1, "values": [None]}}}, "number")_refuses("shape/scalar temperature", {"temperature": 5}, "[3 floats]")_refuses("shape/string temperature", {"temperature": "abc"}, "[3 floats]")_refuses("shape/dict temperature", {"temperature": {"a": 1, "b": 2, "c": 3}}, "[3 floats]")