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): a temperature wider than a double falls back like any other unreadable one

Aashish636abd0c5f53
diff --git a/laya/common.py b/laya/common.py
index 31df5707..6155edba 100644
--- a/laya/common.py
+++ b/laya/common.py
@@ -868,7 +868,15 @@ def clamp_temperature(t, lo: float = TEMP_MIN, hi: float = TEMP_MAX) -> float:
return 1.0
try:
t = float(t)
- except (TypeError, ValueError):
+ except (TypeError, ValueError, OverflowError):
+ # `OverflowError` is a number this function cannot read, not a bug in the caller: JSON's
+ # grammar has no double limit, so a temperature written as a 401-digit integer literal
+ # parses as an `int` and `float()` refuses it. (`1e400` in the same file is fine -- JSON
+ # reads that as a float, which is `inf`, and the check below already answers 1.0 for it.)
+ # `confidence._finite`, `hooks.validate_timeout` and `calibrate.apply_calibration_payload`
+ # all read this exception as "does not read as a number" already; here it used to escape,
+ # and with it every `Agent`/`ONNXAgent`/`load_calibration` load that had read the
+ # checkpoint off disk by then.
return 1.0
if t != t or t in (float("inf"), float("-inf")): # NaN / inf
return 1.0
@@ -890,7 +898,11 @@ def temperature_rejected(raw, applied: float) -> bool:
return True
try:
value = float(raw)
- except (TypeError, ValueError):
+ except (TypeError, ValueError, OverflowError):
+ # The same "does not read as a number" `clamp_temperature` answers with 1.0, so the entry
+ # is named rather than the loader raising while it builds the warning about it. A wide
+ # integer literal is that case: it has a neutral fallback to report, and the value that
+ # `float()` cannot hold is exactly the one a reader needs to see.
return True
return value != applied
diff --git a/tests/test_calibrate.py b/tests/test_calibrate.py
index e7263ff8..5174333f 100644
--- a/tests/test_calibrate.py
+++ b/tests/test_calibrate.py
@@ -798,6 +798,32 @@ check_true(
[str(w.message) for w in caught],
)
+# The same guard has a third arm, and this is the one the file format makes reachable: JSON keeps
+# arbitrary precision, so a calibration file whose temperature is written out as hundreds of digits
+# loads as an `int` and `float()` answers it with `OverflowError`. `clamp_temperature` caught
+# TypeError and ValueError only, so the very payload this function's docstring promises "cannot
+# crash a later forward" crashed the load instead -- while `1e400`, the same number one character
+# away in the same file, parses as a float, arrives as `inf`, and has always clamped quietly.
+WIDE = 10 ** 400 # 401 digits: inside the print limit, outside float range
+wide_payload = {**bad_payload, "temperature": [WIDE, 1.2, 1.1],
+ "temperature_by_options": {"choice:2": 2 ** 1024}}
+wide_agent = Agent.__new__(Agent)
+wide_agent.model_id_or_path = "convaiinnovations/laya"
+wide_agent.subfolder = None
+wide_agent.cfg = {"encoder": "answerdotai/ModernBERT-large"}
+with warnings.catch_warnings(record=True) as caught:
+ warnings.simplefilter("always")
+ apply_calibration_payload(wide_agent, wide_payload)
+check("wide-int-load/temperature", wide_agent.temperature, [1.0, 1.2, 1.1])
+check("wide-int-load/by_options", wide_agent.temperature_by_options, {"choice:2": 1.0})
+check_true(
+ "wide-int-load/warns and names each value digit for digit",
+ any(issubclass(w.category, RuntimeWarning)
+ and "temperature[0]=%d -> 1" % WIDE in str(w.message)
+ and "choice:2=%d -> 1" % (2 ** 1024) 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_router.py b/tests/test_router.py
index a0bcf706..16cf4137 100644
--- a/tests/test_router.py
+++ b/tests/test_router.py
@@ -860,6 +860,16 @@ check("clamp/nan falls back to neutral", clamp_temperature(float("nan")), 1.0)
check("clamp/inf falls back to neutral", clamp_temperature(float("inf")), 1.0)
check("clamp/bools are not temperatures", clamp_temperature(True), 1.0)
check("clamp/False is not a sharpening zero", clamp_temperature(False), 1.0)
+# A JSON integer wider than a double is not a temperature either. `json.load` keeps arbitrary
+# precision, so a 401-digit literal arrives as an `int` and `float()` answers it with
+# `OverflowError` -- which used to escape the clamp, and every load that calls it, after the
+# checkpoint had already been read off disk. `2 ** 1024` is the smallest power of two `float()`
+# refuses, and the same number written as a JSON *float* (`1e400`) parses as `inf`, which the
+# clamp has always answered with 1.0.
+check("clamp/wide integer falls back to neutral", clamp_temperature(10 ** 400), 1.0)
+check("clamp/negative wide integer falls back to neutral", clamp_temperature(-(10 ** 400)), 1.0)
+check("clamp/smallest unconvertible power of two", clamp_temperature(2 ** 1024), 1.0)
+check("clamp/convertible neighbor still clamps", clamp_temperature(2 ** 1023), TEMP_MAX)
check("clamp/bounds are sane", TEMP_MIN <= 1.0 <= TEMP_MAX, True)
# 13 options is the bucket the reported skill-router landed in
check("clamp/13 options is the 11+ bucket", temp_bucket(QTYPES["choice"], 13), "choice:11+")
diff --git a/tests/test_temperature_loading.py b/tests/test_temperature_loading.py
index 756afe65..d6ca36e4 100644
--- a/tests/test_temperature_loading.py
+++ b/tests/test_temperature_loading.py
@@ -138,6 +138,27 @@ class TemperatureLoadingTests(unittest.TestCase):
self.assertIn("noul:2", messages[0])
self.assert_predictions(agent, choice=1.0, score=2.0, noul=1.0)
+ def test_json_integer_wider_than_a_float_uses_neutral_fallback_and_warn(self):
+ # JSON keeps arbitrary precision, so a checkpoint whose `rl_agent_config.json` carries a
+ # temperature written out as hundreds of digits loads as an `int` and `float()` answers it
+ # with `OverflowError`. The clamp caught TypeError and ValueError only, so the failure was
+ # not a reported fallback but a load that died after reading the weights off disk -- while
+ # the same file with `1e400` parses as a float, arrives as `inf`, and has always loaded.
+ # `2 ** 1024` is the smallest power of two `float()` refuses.
+ for value in (10 ** 400, -(10 ** 400), 2 ** 1024):
+ 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("uncalibrated", messages[0])
+ # The value the clamp replaced is named in the warning, digit for digit.
+ self.assertIn(str(value), messages[0])
+ self.assert_predictions(agent, choice=1.0, score=2.0, noul=3.0)
+
def test_out_of_range_values_keep_existing_clamps(self):
agent, messages = self.load_config(temperature=[0.1, 10, -1],
temperature_by_options={"choice:2": 9, "score:3-5": 0})
@@ -194,6 +215,14 @@ class TemperatureRejectionPredicateTests(unittest.TestCase):
({}, 1.0, True),
(float("nan"), 1.0, True),
(float("inf"), 1.0, True),
+ # A number `float()` cannot hold is the same answer: JSON keeps arbitrary precision,
+ # so these reach a loader as `int` and the clamp installs its neutral 1.0.
+ (10 ** 400, 1.0, True),
+ (-(10 ** 400), 1.0, True),
+ (2 ** 1024, 1.0, True),
+ # Wide but still convertible: `float()` reads this one, so the ordinary out-of-range
+ # comparison decides it, exactly as it decides the `9.0` row above.
+ (2 ** 1023, TEMP_MAX, True),
]
for raw, applied, want in cases:
with self.subTest(raw=raw, applied=applied):