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(common): report a bool temperature a checkpoint shipped, not load it silently

Aashish37f2620b9960
diff --git a/laya/agent.py b/laya/agent.py
index a011eeb5..67ea7760 100644
--- a/laya/agent.py
+++ b/laya/agent.py
@@ -44,6 +44,7 @@ from .common import (
resolve_lang_temperatures,
serialize_state,
temp_bucket,
+ temperature_rejected,
unpermute_probs,
uses_parallel_layout,
window_batch_cap,
@@ -728,13 +729,8 @@ class Agent(HookRegistry):
entries += [("temperature[%d]" % i, t, self.temperature[i]) for i, t in enumerate(self.temperature_raw)]
rejected = []
for name, raw, applied in entries:
- try:
- if float(raw) == applied:
- continue
- except (TypeError, ValueError):
- # Invalid entries already have a neutral fallback; diagnostics must not
- # repeat the failed conversion or prevent the checkpoint from loading.
- pass
+ if not temperature_rejected(raw, applied):
+ continue
rejected.append("%s=%r -> %g" % (name, raw, applied))
if rejected:
warnings.warn(
diff --git a/laya/calibrate.py b/laya/calibrate.py
index b24b871f..95b2bfa5 100644
--- a/laya/calibrate.py
+++ b/laya/calibrate.py
@@ -31,6 +31,7 @@ from .common import (
collate_items,
ece_score,
temp_bucket,
+ temperature_rejected,
)
# Per-bucket floor. Below this, `temperature_by_options` omits the bucket.
@@ -593,11 +594,8 @@ def _rejected_temperatures(entries):
"""Names whose raw value is not the number `clamp_temperature` kept, matching checkpoint load."""
rejected = []
for name, raw, applied in entries:
- try:
- if float(raw) == applied:
- continue
- except (TypeError, ValueError):
- pass
+ if not temperature_rejected(raw, applied):
+ continue
rejected.append("%s=%r -> %g" % (name, raw, applied))
return rejected
diff --git a/laya/common.py b/laya/common.py
index 465c5626..76bfef28 100644
--- a/laya/common.py
+++ b/laya/common.py
@@ -854,6 +854,26 @@ def clamp_temperature(t, lo: float = TEMP_MIN, hi: float = TEMP_MAX) -> float:
return min(hi, max(lo, t))
+def temperature_rejected(raw, applied: float) -> bool:
+ """Whether `raw` is not the number `clamp_temperature` turned into `applied`.
+
+ Loaders use this to name the entries they had to replace. The obvious test, `float(raw) !=
+ applied`, misses the bool case: `clamp_temperature(True)` returns the neutral 1.0 and
+ `float(True)` *is* 1.0, so a checkpoint or calibration file shipping `true` where a number
+ belongs read as an accepted temperature and loaded in silence, while `false` floated to 0.0,
+ mismatched and was reported. So ask the question `clamp_temperature` itself asks -- is this a
+ number at all -- instead of re-deriving its answer. A value that will not convert is rejected
+ the same way: it has a neutral fallback to report, and the diagnostic must never raise for it.
+ """
+ if isinstance(raw, bool):
+ return True
+ try:
+ value = float(raw)
+ except (TypeError, ValueError):
+ return True
+ return value != applied
+
+
def resolve_lang_temperatures(raw: Optional[Dict[str, Any]],
base_temperature: Sequence[float]) -> Dict[str, Dict[str, Any]]:
"""Parse the `lang_temperatures` option into `{language: {temperature, temperature_by_options}}`.
diff --git a/laya/onnx_agent.py b/laya/onnx_agent.py
index 77fa87e5..447f75f1 100644
--- a/laya/onnx_agent.py
+++ b/laya/onnx_agent.py
@@ -28,6 +28,7 @@ from laya.common import (
window_batch_cap,
window_budget,
temp_bucket,
+ temperature_rejected,
unpermute_probs,
TEMP_MIN,
TEMP_MAX,
@@ -204,13 +205,8 @@ class ONNXAgent(HookRegistry):
entries += [("temperature[%d]" % i, t, self.temperature[i]) for i, t in enumerate(self.temperature_raw)]
rejected = []
for name, raw, applied in entries:
- try:
- if float(raw) == applied:
- continue
- except (TypeError, ValueError):
- # Invalid entries already have a neutral fallback; diagnostics must not
- # repeat the failed conversion or prevent the checkpoint from loading.
- pass
+ if not temperature_rejected(raw, applied):
+ continue
rejected.append("%s=%r -> %g" % (name, raw, applied))
if rejected:
warnings.warn(
diff --git a/tests/test_calibrate.py b/tests/test_calibrate.py
index e3a7faca..6c0d8a34 100644
--- a/tests/test_calibrate.py
+++ b/tests/test_calibrate.py
@@ -718,6 +718,29 @@ check(
{"choice:2": TEMP_MIN, "noul:2": 1.0},
)
+# A JSON `true` is not a temperature either -- `clamp_temperature` refuses a bool -- but this
+# payload loaded in silence, because `float(True)` is exactly the neutral 1.0 the clamp installs
+# for it and the diagnostic only compared the two numbers. `false` was reported for the wrong
+# reason: it floats to 0.0, so it mismatched. Both now name the entry.
+bool_payload = {**bad_payload, "temperature": [True, 1.2, 1.1],
+ "temperature_by_options": {"choice:2": False}}
+bool_agent = Agent.__new__(Agent)
+bool_agent.model_id_or_path = "convaiinnovations/laya"
+bool_agent.subfolder = None
+bool_agent.cfg = {"encoder": "answerdotai/ModernBERT-large"}
+with warnings.catch_warnings(record=True) as caught:
+ warnings.simplefilter("always")
+ apply_calibration_payload(bool_agent, bool_payload)
+check("bool-load/temperature", bool_agent.temperature, [1.0, 1.2, 1.1])
+check("bool-load/by_options", bool_agent.temperature_by_options, {"choice:2": 1.0})
+check_true(
+ "bool-load/warns for both entries",
+ any(issubclass(w.category, RuntimeWarning)
+ and "temperature[0]=True -> 1" in str(w.message)
+ and "choice:2=False -> 1" in str(w.message) for w in caught),
+ [str(w.message) for w in caught],
+)
+
# --------------------------------------------------------------- payload shape
# The values above are tolerated and clamped; the *shape* is not negotiable, and the
diff --git a/tests/test_temperature_loading.py b/tests/test_temperature_loading.py
index d69aa180..bb7dc79e 100644
--- a/tests/test_temperature_loading.py
+++ b/tests/test_temperature_loading.py
@@ -23,7 +23,7 @@ from tokenizers.models import WordLevel # noqa: E402
from transformers import BertConfig, BertModel, PreTrainedTokenizerFast # noqa: E402
from laya import load # noqa: E402
-from laya.common import DecisionModel # noqa: E402
+from laya.common import TEMP_MAX, TEMP_MIN, DecisionModel, temperature_rejected # noqa: E402
class TemperatureLoadingTests(unittest.TestCase):
@@ -108,6 +108,24 @@ class TemperatureLoadingTests(unittest.TestCase):
self.assertIn(repr(value), messages[0])
self.assert_predictions(agent, choice=1.0, score=3.0, noul=4.0)
+ def test_boolean_entries_use_neutral_fallback_and_warn(self):
+ # A JSON `true`/`false` in a checkpoint config is not a temperature: `clamp_temperature`
+ # refuses a bool and installs the neutral 1.0. It loaded in silence for `true`, though,
+ # because the diagnostic compared `float(raw)` with the applied value and `float(True)` is
+ # exactly that 1.0 -- while `False` floated to 0.0, mismatched, and was reported.
+ for value in (True, False):
+ with self.subTest(value=value):
+ agent, messages = self.load_config(temperature=[value, 2.0, 3.0],
+ temperature_by_options={"choice:2": value})
+ self.assertEqual(agent.temperature, [1.0, 2.0, 3.0])
+ self.assertEqual(agent.temperature_by_options, {"choice:2": 1.0})
+ self.assertEqual(len(messages), 1)
+ self.assertIn("temperature[0]", messages[0])
+ self.assertIn("choice:2", messages[0])
+ self.assertIn(repr(value), messages[0])
+ self.assertIn("uncalibrated", messages[0])
+ self.assert_predictions(agent, choice=1.0, score=2.0, noul=3.0)
+
def test_nonfinite_values_keep_neutral_fallback_and_warn(self):
for value in (float("nan"), float("inf"), float("-inf"), "NaN", "Infinity", "-Infinity"):
with self.subTest(value=value):
@@ -151,6 +169,46 @@ class TemperatureLoadingTests(unittest.TestCase):
self.assert_predictions(agent, choice=1.0, score=1.0, noul=1.0)
+class TemperatureRejectionPredicateTests(unittest.TestCase):
+ """The diagnostic every loader shares, without a checkpoint to build.
+
+ `Agent.__init__`, `ONNXAgent.__init__` and `calibrate._rejected_temperatures` all ask this one
+ question, so the table below is the ONNX site's coverage too -- that suite needs `onnxruntime`,
+ which is not installed for a plain checkout.
+ """
+
+ def test_rejection_matches_what_the_clamp_actually_accepted(self):
+ cases = [
+ # A bool is not a number to `clamp_temperature`, and `float(True)` is exactly the 1.0
+ # it installs instead, so an equality check alone called both of them accepted.
+ (True, 1.0, True),
+ (False, 1.0, True),
+ (1.5, 1.5, False),
+ (2, 2.0, False),
+ ("1.5", 1.5, False),
+ (0.1, TEMP_MIN, True),
+ (9.0, TEMP_MAX, True),
+ (None, 1.0, True),
+ ("x", 1.0, True),
+ ([], 1.0, True),
+ ({}, 1.0, True),
+ (float("nan"), 1.0, True),
+ (float("inf"), 1.0, True),
+ ]
+ for raw, applied, want in cases:
+ with self.subTest(raw=raw, applied=applied):
+ self.assertIs(temperature_rejected(raw, applied), want)
+
+ def test_no_loader_rederives_the_comparison(self):
+ # Three loaders each carried their own copy of the predicate, and one copy drifting from
+ # `clamp_temperature` is how a `true` temperature loaded unreported. Walk the package
+ # rather than naming files, so a re-derived fourth copy is caught the same way.
+ root = Path(__file__).resolve().parents[1]
+ offenders = [p.relative_to(root).as_posix() for p in sorted((root / "laya").rglob("*.py"))
+ if "if float(raw) == applied:" in p.read_text()]
+ self.assertEqual(offenders, [])
+
+
if __name__ == "__main__":
torch.set_num_threads(1)
unittest.main()