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 the lang_temperatures entries the clamp replaced

Aashishd552d9a84c8f
diff --git a/laya/common.py b/laya/common.py
index 76bfef28..15fbc3a5 100644
--- a/laya/common.py
+++ b/laya/common.py
@@ -889,6 +889,12 @@ def resolve_lang_temperatures(raw: Optional[Dict[str, Any]],
A `null` entry or a `null` temperature both mean "inherit the checkpoint's own", which is how
the `laya-ts` port reads the same option (`agent.ts:322-327`).
+
+ Every value still goes through `clamp_temperature`, and a value the clamp had to replace is named
+ in a `RuntimeWarning` for its language: `{"de": {"temperature": [True, 1.5, 2.0]}}` answers
+ `[1.0, 1.5, 2.0]` and `[99, 0.01, 1]` answers `[5.0, 0.5, 1.0]`, which is a different confidence
+ scale from the one the caller wrote. `temperature_rejected` is what distinguishes a replaced entry
+ from an accepted one.
"""
resolved: Dict[str, Dict[str, Any]] = {}
for lang, cfg in (raw or {}).items():
@@ -901,7 +907,8 @@ def resolve_lang_temperatures(raw: Optional[Dict[str, Any]],
raise ValueError("Language override %r must be a mapping, got %s"
% (lang, type(cfg).__name__))
t_raw = cfg.get("temperature")
- if t_raw is None:
+ inherited = t_raw is None
+ if inherited:
t_raw = base_temperature
if not isinstance(t_raw, (list, tuple)) or len(t_raw) != 3:
raise ValueError("Language override %r temperature must be a list of 3 floats, got %r"
@@ -910,9 +917,27 @@ def resolve_lang_temperatures(raw: Optional[Dict[str, Any]],
if not isinstance(tbo_raw, dict):
raise ValueError("Language override %r temperature_by_options must be a mapping of "
"bucket -> float, got %s" % (lang, type(tbo_raw).__name__))
+ temperature = [clamp_temperature(t) for t in t_raw]
+ by_options = {k: clamp_temperature(v) for k, v in tbo_raw.items()}
+ # An override that omits `temperature` inherits the checkpoint's own list, and the loader
+ # that got here has already reported the entries it replaced in it. Repeating that line once
+ # per language would bury the overrides a reader is actually holding.
+ entries = [] if inherited else [
+ ("temperature[%d]" % i, raw, applied)
+ for i, (raw, applied) in enumerate(zip(t_raw, temperature))
+ ]
+ entries += [("%s" % k, v, by_options[k]) for k, v in tbo_raw.items()]
+ rejected = ["%s=%r -> %g" % (name, raw, applied)
+ for name, raw, applied in entries if temperature_rejected(raw, applied)]
+ if rejected:
+ warnings.warn(
+ "laya: language override %r ships invalid temperatures or values outside "
+ "[%g, %g]; using %s. Treat confidence from the affected entries as uncalibrated."
+ % (lang, TEMP_MIN, TEMP_MAX, ", ".join(rejected)),
+ RuntimeWarning, stacklevel=2)
resolved[norm] = {
- "temperature": [clamp_temperature(t) for t in t_raw],
- "temperature_by_options": {k: clamp_temperature(v) for k, v in tbo_raw.items()},
+ "temperature": temperature,
+ "temperature_by_options": by_options,
}
return resolved
diff --git a/tests/test_system_one_lang.py b/tests/test_system_one_lang.py
index a7eda40d..7256a4f2 100644
--- a/tests/test_system_one_lang.py
+++ b/tests/test_system_one_lang.py
@@ -48,9 +48,9 @@ def test_system_one_lang_parameter_passthrough():
BASE_TEMPERATURE = (1.0, 1.5, 2.0)
-def _resolve(raw):
+def _resolve(raw, base_temperature=BASE_TEMPERATURE):
from laya.common import resolve_lang_temperatures
- return resolve_lang_temperatures(raw, BASE_TEMPERATURE)
+ return resolve_lang_temperatures(raw, base_temperature)
@pytest.mark.parametrize("name,raw,fragment", [
@@ -105,3 +105,101 @@ def test_lang_temperatures_result_is_what_both_backends_store():
for cls in (_Agent,):
src = inspect.getsource(cls.__init__)
assert "resolve_lang_temperatures" in src, cls.__name__
+
+
+# ------------------------------------------------------------------ what an override reports
+#
+# The clamp is the whole of what `lang_temperatures` does to a value, and until now it was the only
+# part with no voice: `{"de": {"temperature": [True, 1.5, 2.0]}}` answered `[1.0, 1.5, 2.0]` and
+# `[99, 0.01, 1]` answered `[5.0, 0.5, 1.0]` with nothing written anywhere, while the checkpoint's own
+# `temperature` list reported the same class of replacement. A per-language override is the one a user
+# hand-edits, so the silent case was the more likely one. These tests are at the shared helper rather
+# than behind a checkpoint because `Agent` and `ONNXAgent` both assign its return value verbatim.
+
+
+def _resolve_catching(raw, base_temperature=BASE_TEMPERATURE):
+ """Resolve `raw`, returning the result and every warning the load path emitted."""
+ import warnings
+ with warnings.catch_warnings(record=True) as caught:
+ warnings.simplefilter("always")
+ resolved = _resolve(raw, base_temperature)
+ return resolved, [str(w.message) for w in caught]
+
+
[email protected]("value", [True, False])
+def test_lang_temperatures_reports_a_replaced_bool(value):
+ """`clamp_temperature` refuses a bool, so the entry is a substitution and has to be named."""
+ resolved, messages = _resolve_catching(
+ {"de": {"temperature": [value, 1.5, 2.0], "temperature_by_options": {"choice:2": value}}})
+ assert resolved["de"]["temperature"] == [1.0, 1.5, 2.0], resolved["de"]
+ assert resolved["de"]["temperature_by_options"] == {"choice:2": 1.0}, resolved["de"]
+ assert len(messages) == 1, messages
+ assert "'de'" in messages[0], messages[0]
+ # both fields named: a reader has to know which override key to go fix
+ assert "temperature[0]=%r -> 1" % value in messages[0], messages[0]
+ assert "choice:2=%r -> 1" % value in messages[0], messages[0]
+ assert "uncalibrated" in messages[0], messages[0]
+
+
+def test_lang_temperatures_reports_an_out_of_range_entry():
+ resolved, messages = _resolve_catching({"de": {"temperature": [99, 0.01, 1]}})
+ assert resolved["de"]["temperature"] == [5.0, 0.5, 1.0], resolved["de"]
+ assert len(messages) == 1, messages
+ for fragment in ("temperature[0]=99 -> 5", "temperature[1]=0.01 -> 0.5"):
+ assert fragment in messages[0], messages[0]
+
+
+def test_lang_temperatures_reports_a_value_that_is_not_a_number():
+ resolved, messages = _resolve_catching({"de": {"temperature": [None, 1.5, 2.0]}})
+ assert resolved["de"]["temperature"] == [1.0, 1.5, 2.0], resolved["de"]
+ assert len(messages) == 1, messages
+ assert "temperature[0]=None -> 1" in messages[0], messages[0]
+
+
[email protected]("name,raw", [
+ ("an in-range override", {"de": {"temperature": [1.0, 1.5, 2.0]}}),
+ ("a numeric string that equals the applied value", {"de": {"temperature": ["1.5", 1.5, 2.0]}}),
+ ("a valid bucket table", {"de": {"temperature_by_options": {"choice:2": 1.5}}}),
+ ("an entry that inherits the checkpoint", {"de": None}),
+ ("no option at all", None),
+])
+def test_lang_temperatures_stays_silent_for_what_it_accepted(name, raw):
+ """A warning for a value the clamp left alone would train readers to ignore the warning."""
+ _, messages = _resolve_catching(raw)
+ assert messages == [], (name, messages)
+
+
+def test_lang_temperatures_names_the_language_as_the_user_wrote_it():
+ """The table keys on the base subtag, but the fix has to be made in the key that was typed."""
+ resolved, messages = _resolve_catching({"de-AT": {"temperature": [True, 1.5, 2.0]}})
+ assert "de" in resolved, resolved
+ assert len(messages) == 1, messages
+ assert "'de-AT'" in messages[0], messages[0]
+
+
+def test_lang_temperatures_warns_once_per_bad_language():
+ resolved, messages = _resolve_catching(
+ {"de": {"temperature": [True, 1.5, 2.0]},
+ "fr": {"temperature": [99, 1.5, 2.0]},
+ "es": {"temperature": [2.0, 2.0, 2.0]}})
+ assert resolved["es"]["temperature"] == [2.0, 2.0, 2.0], resolved["es"]
+ assert len(messages) == 2, messages
+ de = [m for m in messages if "'de'" in m]
+ fr = [m for m in messages if "'fr'" in m]
+ assert len(de) == 1 and len(fr) == 1, messages
+ # each line carries only its own language's entries
+ assert "temperature[0]=True" in de[0] and "temperature[0]=99" not in de[0], de[0]
+ assert "temperature[0]=99 -> 5" in fr[0] and "True" not in fr[0], fr[0]
+ assert not [m for m in messages if "'es'" in m], messages
+
+
+def test_lang_temperatures_does_not_repeat_the_base_diagnostic():
+ """An override that omits `temperature` inherits the checkpoint's own list.
+
+ The loader reporting its own replaced entries already said so once; saying it again per language
+ would bury the overrides a reader is actually holding. The inherited values still clamp.
+ """
+ resolved, messages = _resolve_catching({"de": None, "fr": None}, base_temperature=(True, 1.5, 2.0))
+ assert resolved["de"]["temperature"] == [1.0, 1.5, 2.0], resolved["de"]
+ assert resolved["fr"]["temperature"] == [1.0, 1.5, 2.0], resolved["fr"]
+ assert messages == [], messages