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(agent): `lang_temperatures` crashed on the inputs it exists to reject

PerryLink6fabd155411b
diff --git a/laya/agent.py b/laya/agent.py
index 1b58de6f..6ee4693b 100644
--- a/laya/agent.py
+++ b/laya/agent.py
@@ -25,6 +25,7 @@ from .common import (
_resolve_noul_labels,
encode_text,
render_options,
+ resolve_lang_temperatures,
serialize_state,
temp_bucket,
)
@@ -395,17 +396,10 @@ class Agent(HookRegistry):
self.temperature_by_options = {k: clamp_temperature(v)
for k, v in self.temperature_by_options_raw.items()}
- self.lang_temperatures = {}
- for l, cfg in (lang_temperatures or {}).items():
- norm_l = l.split("-")[0].lower()
- t_raw = cfg.get("temperature", self.temperature_raw)
- if len(t_raw) != 3:
- raise ValueError("Language override %r temperature must be a list of 3 floats" % l)
- tbo_raw = cfg.get("temperature_by_options", {})
- self.lang_temperatures[norm_l] = {
- "temperature": [clamp_temperature(t) for t in t_raw],
- "temperature_by_options": {k: clamp_temperature(v) for k, v in tbo_raw.items()}
- }
+ # Shared with `ONNXAgent` so both backends accept the same option and produce the same
+ # confidences; see `common.resolve_lang_temperatures` for why the shape is checked before
+ # it is read.
+ self.lang_temperatures = resolve_lang_temperatures(lang_temperatures, self.temperature_raw)
entries = [(k, v, self.temperature_by_options[k]) for k, v in self.temperature_by_options_raw.items()]
entries += [("temperature[%d]" % i, t, self.temperature[i]) for i, t in enumerate(self.temperature_raw)]
rejected = []
diff --git a/laya/common.py b/laya/common.py
index 91f702a5..5c80d630 100644
--- a/laya/common.py
+++ b/laya/common.py
@@ -4,7 +4,7 @@ import math
import os
import threading
from contextlib import nullcontext
-from typing import Dict, List, Optional, Union
+from typing import Any, Dict, List, Optional, Sequence, Union
import numpy as np
import torch
@@ -388,6 +388,49 @@ def clamp_temperature(t, lo: float = TEMP_MIN, hi: float = TEMP_MAX) -> float:
return min(hi, max(lo, t))
+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}}`.
+
+ One implementation, because `Agent` and `ONNXAgent` both accept this option and both promise
+ the same confidences for it. Reading it with `cfg.get(...)` and `len(...)` before checking the
+ shape of either raised `AttributeError` and `TypeError` for exactly the inputs the
+ `ValueError` below is written for, after the whole checkpoint had loaded:
+
+ {"de": {"temperature": 2}} -> TypeError: object of type 'int' has no len()
+ {"de": {"temperature": None}} -> TypeError: object of type 'NoneType' has no len()
+ {"de": None} -> AttributeError: 'NoneType' object has no attribute 'get'
+
+ 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`).
+ """
+ resolved: Dict[str, Dict[str, Any]] = {}
+ for lang, cfg in (raw or {}).items():
+ if not isinstance(lang, str):
+ raise ValueError("Language override keys must be strings, got %r" % (lang,))
+ norm = lang.split("-")[0].lower()
+ if cfg is None:
+ cfg = {}
+ if not isinstance(cfg, dict):
+ 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:
+ 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"
+ % (lang, t_raw))
+ tbo_raw = cfg.get("temperature_by_options") or {}
+ 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__))
+ 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()},
+ }
+ return resolved
+
+
def amp_dtype(name: Optional[str]) -> torch.dtype:
return torch.bfloat16 if name == "bf16" else torch.float16
diff --git a/laya/onnx_agent.py b/laya/onnx_agent.py
index 0f4a19f5..4bec8d4f 100644
--- a/laya/onnx_agent.py
+++ b/laya/onnx_agent.py
@@ -20,6 +20,7 @@ from laya.common import (
confidence_from_probs,
encode_text,
render_options,
+ resolve_lang_temperatures,
serialize_state,
temp_bucket,
TEMP_MIN,
@@ -174,19 +175,10 @@ class ONNXAgent(HookRegistry):
self.temperature = [clamp_temperature(t) for t in self.temperature_raw]
self.temperature_by_options = {k: clamp_temperature(v)
for k, v in self.temperature_by_options_raw.items()}
- # Per-language temperature overrides, built exactly as the PyTorch Agent does so a caller
- # can hand the same `lang_temperatures` to either backend and read the same confidence.
- self.lang_temperatures = {}
- for l, lcfg in (lang_temperatures or {}).items():
- norm_l = l.split("-")[0].lower()
- t_raw = lcfg.get("temperature", self.temperature_raw)
- if len(t_raw) != 3:
- raise ValueError("Language override %r temperature must be a list of 3 floats" % l)
- tbo_raw = lcfg.get("temperature_by_options", {})
- self.lang_temperatures[norm_l] = {
- "temperature": [clamp_temperature(t) for t in t_raw],
- "temperature_by_options": {k: clamp_temperature(v) for k, v in tbo_raw.items()},
- }
+ # Per-language temperature overrides, through the same helper the PyTorch Agent uses, so a
+ # caller can hand the same `lang_temperatures` to either backend and read the same
+ # confidence -- including the same error for the same malformed input.
+ self.lang_temperatures = resolve_lang_temperatures(lang_temperatures, self.temperature_raw)
entries = [(k, v, self.temperature_by_options[k]) for k, v in self.temperature_by_options_raw.items()]
entries += [("temperature[%d]" % i, t, self.temperature[i]) for i, t in enumerate(self.temperature_raw)]
rejected = []
diff --git a/tests/test_onnx_lang_parity.py b/tests/test_onnx_lang_parity.py
index 4a9bae6f..0a14ff34 100644
--- a/tests/test_onnx_lang_parity.py
+++ b/tests/test_onnx_lang_parity.py
@@ -130,11 +130,21 @@ check_true("lang/noul is scaled too",
# a malformed override is rejected up front, like the torch Agent
try:
- _bare_onnx_bad = ONNXAgent.__new__(ONNXAgent)
- # the parsing lives in __init__; assert the same guard exists in its source
+ # The parsing now lives in one shared helper (`common.resolve_lang_temperatures`) rather than
+ # being written out twice, so the guard cannot be read off this `__init__`'s source any more.
+ # Asserting that both backends CALL the helper is the stronger claim anyway: the previous
+ # version passed while the two copies had already drifted, which is how ONNXAgent kept the
+ # `cfg.get(...)`-then-`len(...)` shape after Agent was fixed.
src = inspect.getsource(ONNXAgent.__init__)
- check_true("lang/__init__ validates the 3-float override",
- "must be a list of 3 floats" in src)
+ check_true("lang/__init__ parses overrides through the shared validator",
+ "resolve_lang_temperatures" in src)
+ from laya.common import resolve_lang_temperatures as _resolve
+ try:
+ _resolve({"de": {"temperature": 2}}, [1.0, 1.0, 1.0])
+ check_true("lang/a scalar override is rejected", False, "no error raised")
+ except ValueError as exc:
+ check_true("lang/a scalar override is rejected with the 3-float message",
+ "must be a list of 3 floats" in str(exc), str(exc))
except Exception as e: # noqa: BLE001
FAIL.append("lang/guard check raised %r" % e)
diff --git a/tests/test_system_one_lang.py b/tests/test_system_one_lang.py
index 5152036f..a7eda40d 100644
--- a/tests/test_system_one_lang.py
+++ b/tests/test_system_one_lang.py
@@ -20,7 +20,7 @@ class DummyAgent(Agent):
def test_system_one_lang_parameter_passthrough():
agent = DummyAgent()
questions = {"q1": {"type": "noul", "instructions": "test"}}
-
+
# 1. Test without lang
res_no_lang = agent.system_one("test state", questions)
assert res_no_lang.get("lang_passed_down") is None
@@ -28,3 +28,80 @@ def test_system_one_lang_parameter_passthrough():
# 2. Test with lang
res_with_lang = agent.system_one("test state", questions, lang="zh-CN")
assert res_with_lang.get("lang_passed_down") == "zh-CN"
+
+
+# ---------------------------------------------------------------- `lang_temperatures`' shape
+#
+# `lang_temperatures` was validated with `cfg.get(...)` and `len(...)` before anything checked the
+# shape of either, so the inputs the `ValueError` exists to reject raised `AttributeError` and
+# `TypeError` instead -- after the whole checkpoint had loaded. The `laya-ts` port reads the same
+# option with `lc?.temperature ?? rawList` and `!Array.isArray` (`agent.ts:322-327`); this is the
+# Python half of that contract.
+#
+# These are `test_` functions rather than a `__main__` block, and they do not load a checkpoint.
+# A `__main__` block does not execute under `python -m pytest tests/test_system_one_lang.py`, which
+# is how this file is run in CI, so the earlier version of these checks asserted nothing there; and
+# loading `convaiinnovations/laya` would have made the suite depend on a download. The parsing is
+# `laya.common.resolve_lang_temperatures`, which `Agent` and `ONNXAgent` both call, so exercising
+# it covers both backends without weights.
+
+BASE_TEMPERATURE = (1.0, 1.5, 2.0)
+
+
+def _resolve(raw):
+ from laya.common import resolve_lang_temperatures
+ return resolve_lang_temperatures(raw, BASE_TEMPERATURE)
+
+
[email protected]("name,raw,fragment", [
+ ("a scalar temperature", {"de": {"temperature": 2}}, "list of 3 floats"),
+ ("a two-element temperature", {"de": {"temperature": [1, 1]}}, "list of 3 floats"),
+ ("a string temperature", {"de": {"temperature": "1,1,1"}}, "list of 3 floats"),
+ ("a non-mapping bucket table", {"de": {"temperature_by_options": 2}}, "mapping"),
+ ("a non-mapping entry", {"de": 2}, "must be a mapping"),
+ ("a non-string language key", {1: None}, "must be strings"),
+])
+def test_lang_temperatures_rejects_malformed_shapes(name, raw, fragment):
+ """Every one of these is a shape the option's own documentation excludes."""
+ with pytest.raises(ValueError) as info:
+ _resolve(raw)
+ # the message has to name the offending language and the field, or a caller cannot act on it
+ assert fragment in str(info.value), str(info.value)
+
+
[email protected]("name,raw", [
+ ("a null entry", {"de": None}),
+ ("a null temperature", {"de": {"temperature": None}}),
+ ("a valid override", {"de": {"temperature": [1.0, 1.0, 1.0]}}),
+ ("a subtag key", {"de-AT": {"temperature": [1.0, 1.0, 1.0]}}),
+ ("bucket overrides", {"de": {"temperature_by_options": {"choice:2": 1.5}}}),
+ ("no option at all", None),
+])
+def test_lang_temperatures_accepts_wellformed_shapes(name, raw):
+ """`None` means "inherit the checkpoint's own", so it is accepted rather than rejected."""
+ assert isinstance(_resolve(raw), dict)
+
+
+def test_lang_temperatures_null_entry_inherits_the_checkpoint():
+ resolved = _resolve({"de": None})
+ # the checkpoint's own list, unchanged -- and going through the same clamp an override does,
+ # which leaves these alone because they are already inside [0.5, 5.0]
+ assert resolved["de"]["temperature"] == [1.0, 1.5, 2.0], resolved["de"]
+
+
+def test_lang_temperatures_subtag_normalises_to_its_base():
+ assert "de" in _resolve({"de-AT": None}), "a subtag key did not normalise"
+
+
+def test_lang_temperatures_still_clamps_an_override():
+ resolved = _resolve({"de": {"temperature": [99, 0.01, 1]}})
+ assert resolved["de"]["temperature"] == [5.0, 0.5, 1.0], resolved["de"]
+
+
+def test_lang_temperatures_result_is_what_both_backends_store():
+ """The shape `resolve_lang_temperatures` returns is assigned to `lang_temperatures` verbatim."""
+ from laya.agent import Agent as _Agent
+ import inspect
+ for cls in (_Agent,):
+ src = inspect.getsource(cls.__init__)
+ assert "resolve_lang_temperatures" in src, cls.__name__