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.

docs(onnx): document the calibration knob tests/test_calibrate.py pins

Aashishb0d6993f4688
diff --git a/laya/onnx_agent.py b/laya/onnx_agent.py
index 8a67f065..6f6b5f00 100644
--- a/laya/onnx_agent.py
+++ b/laya/onnx_agent.py
@@ -88,6 +88,9 @@ class ONNXAgent(HookRegistry):
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.
+ calibration: Optional path to a JSON map written by `Agent.save_calibration`
+ (see `Agent.load_calibration`), read onto this agent after its
+ checkpoint temperatures; exactly as the PyTorch `Agent` does.
"""
self.hooks = normalise_hooks(hooks, on_predict_start, on_predict_end)
self.hooks_raise = bool(hooks_raise)
@@ -211,7 +214,13 @@ class ONNXAgent(HookRegistry):
self.load_calibration(calibration)
def load_calibration(self, path: str) -> None:
- """Read a JSON map written by `save_calibration` onto this agent."""
+ """Read a JSON map written by `save_calibration` onto this agent.
+
+ A file with no `version` is treated as version 1 and still loads. A newer file
+ whose recorded checkpoint does not match this agent warns and still loads.
+ Values that are not numbers, or that sit outside `[TEMP_MIN, TEMP_MAX]`, are clamped
+ with `clamp_temperature` the same way checkpoint load is.
+ """
from .calibrate import apply_calibration_payload
with open(path) as f:
payload = json.load(f)
diff --git a/tests/test_calibrate.py b/tests/test_calibrate.py
index 7fe6a717..acea4547 100644
--- a/tests/test_calibrate.py
+++ b/tests/test_calibrate.py
@@ -16,6 +16,7 @@ import numpy as np
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from laya.agent import Agent, load # noqa: E402
+from laya.onnx_agent import ONNXAgent # noqa: E402
from laya.calibrate import ( # noqa: E402
CALIBRATION_VERSION,
ECE_HOLDOUT_FRAC,
@@ -362,6 +363,16 @@ load_src = inspect.getsource(load)
check_true("load/calibration kwarg", "calibration" in load_src)
check_true("load/forwards calibration", "calibration=calibration" in load_src)
+onnx_init_src = inspect.getsource(ONNXAgent.__init__)
+check_true("onnx/init documents the calibration kwarg",
+ "calibration" in onnx_init_src.split("Args:")[1] if "Args:" in onnx_init_src else False)
+check_true("onnx/init reads calibration after checkpoint temperatures",
+ "self.load_calibration(calibration)" in onnx_init_src)
+check_true("onnx/load_calibration documents version/mismatch/clamp behaviour",
+ "version 1 and still loads" in inspect.getsource(ONNXAgent.load_calibration))
+check_true("onnx/load_calibration is the shared applier",
+ "apply_calibration_payload" in inspect.getsource(ONNXAgent.load_calibration))
+
rec_src = inspect.getsource(records_from_labeled)
check_true("records/_encode_state", "_encode_state" in rec_src)
check_true("records/_decode_answers path", "_decode_answers" in rec_src)