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): name the parameter when target_error cannot be compared to a range

Aashish58b12bc1aeb0
diff --git a/laya/calibrate.py b/laya/calibrate.py
index fd7e9d1d..bed20578 100644
--- a/laya/calibrate.py
+++ b/laya/calibrate.py
@@ -482,7 +482,17 @@ def fit_abstention_thresholds(records: Iterable, temperature: Sequence[float],
# be read as 0.0 -- the cut that abstains on every answer -- and `True` as 1.0, which gates
# nothing. Both fit a map, so nothing downstream would mention it. `TrainConfig` refuses the
# same bool on the way to this call (`train.py`), as does `_iter_records` for a `qtype`.
- if isinstance(target_error, bool) or not 0.0 <= target_error <= 1.0:
+ # Everything the range test cannot even compare -- a `"0.1"` from a config file, a `None` from a
+ # missing key -- left a bare `TypeError: '<=' not supported between instances of 'float' and
+ # 'str'`, naming neither the parameter nor the caller's mistake, while `min_bucket_n` one line
+ # below has always named itself. The comparison is run rather than replaced by an `isinstance`
+ # list so that a value which does compare -- `Decimal`, which `numbers.Real` does not cover --
+ # keeps fitting.
+ try:
+ comparable = 0.0 <= target_error <= 1.0
+ except TypeError:
+ comparable = False
+ if isinstance(target_error, bool) or not comparable:
raise ValueError("target_error must be in [0.0, 1.0], got %r" % (target_error,))
min_bucket_n = _positive_int("min_bucket_n", min_bucket_n)
recs = _iter_records(records)
diff --git a/tests/test_conformal_abstention.py b/tests/test_conformal_abstention.py
index 4dd15907..84f5754d 100644
--- a/tests/test_conformal_abstention.py
+++ b/tests/test_conformal_abstention.py
@@ -9,6 +9,7 @@ Run: python tests/test_conformal_abstention.py
import math
import os
import sys
+from decimal import Decimal
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
@@ -289,6 +290,29 @@ for _bad, _coerces in ((True, 1.0), (False, 0.0)):
check("target_error/%r is refused by name" % (_bad,), _res,
"target_error must be in [0.0, 1.0], got %r" % (_bad,))
+# The same arm, for the values the range test cannot compare at all rather than cannot see: a
+# `"0.1"` read out of a JSON config, a `None` from a key that was not there. Each used to leave the
+# interpreter's own `TypeError: '<=' not supported between instances of 'float' and 'str'`, which
+# names the operator and not the parameter, and `min_bucket_n` one line below has always named
+# itself for these shapes.
+for _bad in ("0.1", None, 1j, [0.1], {"t": 0.1}):
+ try:
+ fit_abstention_thresholds(_small, [1.0, 1.0, 1.0], {}, target_error=_bad, min_bucket_n=1)
+ _res = "no ValueError"
+ except ValueError as _exc:
+ _res = str(_exc)
+ except Exception as _exc:
+ _res = "raised %s: %s" % (type(_exc).__name__, _exc)
+ check("target_error/%r is refused by name" % (_bad,), _res,
+ "target_error must be in [0.0, 1.0], got %r" % (_bad,))
+
+# A value that compares as a real number but is not a `float` keeps working: `Decimal` was accepted
+# before the shape check existed and the only thing it is used for is `rate <= target_error`.
+check("target_error/a Decimal still fits",
+ fit_abstention_thresholds(_small, [1.0, 1.0, 1.0], {}, target_error=Decimal("0.10"),
+ min_bucket_n=1),
+ fit_abstention_thresholds(_small, [1.0, 1.0, 1.0], {}, target_error=0.10, min_bucket_n=1))
+
# And the arm did not get stricter than the two ends it exists to protect: refusing 0.0 and 1.0
# would refuse a caller genuinely asking for no tolerance, and those two are what the bools meant.
check("target_error/0.0 still fits the abstain-on-everything cut",