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(rust): a temperature wider than a double falls back to 1.0 like Python 0.4.20

kevin-gatimuda65a29dcf9e
diff --git a/laya-rust/CHANGELOG.md b/laya-rust/CHANGELOG.md
index 8e6fbf0c..9a2fc041 100644
--- a/laya-rust/CHANGELOG.md
+++ b/laya-rust/CHANGELOG.md
@@ -113,7 +113,7 @@ regenerated from laya (Python) 0.4.14 for the supported paths (see the known gap
checkpoint whose temperatures were clamped emits Python's warning, each with Python's text
through the `log` facade (`warn`, target `laya`); `log` is a new dependency.
- A string in a checkpoint's `temperature` or `temperature_by_options` is read as Python's `float(str)` reads it (surrounding whitespace, any-script digits, single underscores, `inf`/`nan`) before clamping, instead of being replaced by 1.0.
-- A checkpoint whose temperature is a JSON integer too large for a float fails to load with `LayaError::InvalidConfig("OverflowError: int too large to convert to float")`, as Python's `clamp_temperature` raises; a float literal such as `1e400` is read as infinity and replaced by 1.0, as in Python.
+- A checkpoint whose temperature is a JSON integer too large for a float loads with the neutral 1.0 for it, and the load warning names the integer digit for digit, as Python 0.4.20's `clamp_temperature` and `temperature_rejected` answer it; a float literal such as `1e400` is read as infinity and replaced by 1.0, as in Python.
- The bare JSON constants `NaN`, `Infinity` and `-Infinity`, which Python's `json.loads` reads as floats, are accepted in `rl_agent_config.json` and `tokenizer_config.json` (a temperature of `NaN` clamps to 1.0 with Python's warning).
- Checkpoint JSON (`rl_agent_config.json`, `tokenizer_config.json`) is read by a reader that follows Python's `json.loads`: integers keep their literal (the temperature warning prints `0` for `-0` and big integers as Python does), `NaN`/`Infinity`/`-Infinity` are numbers only in value position, and nothing is encoded in-band. `serde_json` now uses `float_roundtrip` so JSON floats equal Python's `float()` bit for bit. An identical warning is emitted once per process, as Python's default filter does.
- Config loading matches Python's `ONNXAgent.__init__` and `json.loads` further: `option_layout` other than `sequential` is refused with Python's message; an integer literal of more than 4300 digits is refused with Python's `ValueError` text; `max_len`/`head_max_len` read `true`/`false` as 1/0 and `-0` as 0, and a float, string, `null`, list or object is an error (Python fails at the first prediction; this port refuses the config when it is loaded, with the same `TypeError` text, `head_max_len` first). A negative value is refused too, since Python slices from the end for it. The JSON reader no longer re-validates the rest of the document for every character of a string.
diff --git a/laya-rust/src/laya_config.rs b/laya-rust/src/laya_config.rs
index 90432d49..5d02daaf 100644
--- a/laya-rust/src/laya_config.rs
+++ b/laya-rust/src/laya_config.rs
@@ -131,7 +131,9 @@ fn ascii_float_text(body: &str) -> Option<String> {
}
/// A JSON temperature entry as `clamp_temperature` reads it: a number as itself, a string through
-/// `float(str)`, and a bool or anything else not at all.
+/// `float(str)`, and a bool or anything else not at all. An integer too wide for a double does not
+/// convert either (Python's `OverflowError`, which `clamp_temperature` and `temperature_rejected`
+/// catch since 0.4.20), so it is `None` here and becomes the neutral 1.0, named in the warning.
pub(crate) fn temperature_number(raw: &PyJson) -> Option<f64> {
match raw {
PyJson::Int(literal) => PyJson::int_as_f64(literal),
@@ -141,18 +143,6 @@ pub(crate) fn temperature_number(raw: &PyJson) -> Option<f64> {
}
}
-/// Python's `OverflowError` from `clamp_temperature(<huge int>)`, which nothing catches, so the
-/// checkpoint fails to load.
-fn check_temperature_overflow<'a>(values: impl IntoIterator<Item = &'a PyJson>) -> Result<()> {
- let overflows = |v: &PyJson| matches!(v, PyJson::Int(l) if PyJson::int_as_f64(l).is_none());
- if values.into_iter().any(overflows) {
- return Err(LayaError::InvalidConfig(
- "OverflowError: int too large to convert to float".to_string(),
- ));
- }
- Ok(())
-}
-
/// Python's name for the type of a JSON value, as it appears in `TypeError` messages.
fn python_type_name(value: &PyJson) -> &'static str {
match value {
@@ -403,11 +393,9 @@ impl LayaConfig {
let root = PyJson::parse(json)?;
check_option_layout(&root)?;
// Python builds `[clamp_temperature(t) for t in temperature]` and then the by-options dict,
- // so each container's error, and each overflow, comes in that order.
+ // so each container's error comes in that order.
let elements = temperature_elements(&root)?;
- check_temperature_overflow(&elements)?;
let options = temperature_options(&root)?;
- check_temperature_overflow(options.iter().map(|(_, v)| v))?;
// `ONNXAgent.__init__` resolves `lang_temperatures` right after clamping the checkpoint's
// own, and only then warns about what it clamped: an invalid table fails the load before
diff --git a/laya-rust/tests/lang_temperatures_order_tests.rs b/laya-rust/tests/lang_temperatures_order_tests.rs
index 3ce853c9..5bd5a966 100644
--- a/laya-rust/tests/lang_temperatures_order_tests.rs
+++ b/laya-rust/tests/lang_temperatures_order_tests.rs
@@ -65,4 +65,19 @@ fn the_table_resolves_before_the_checkpoint_warning() {
take().is_empty(),
"the checkpoint warning must not be logged"
);
+
+ // Python 0.4.20: a temperature too wide for a double is the neutral 1.0 and is named, digit for
+ // digit, in the checkpoint's warning; an entry that inherits the list is not reported again,
+ // and the load does not fail.
+ let wide = format!("1{}", "0".repeat(400));
+ let config = format!(r#"{{"temperature": [{wide}, 1.2, 1.1]}}"#);
+ let inherit = vec![("de".to_string(), None)];
+ let loaded = LayaConfig::parse_with_lang_temperatures(&config, &inherit).unwrap();
+ assert_eq!(vec![1.0, 1.2, 1.1], loaded.temperature);
+ let seen = take();
+ assert_eq!(1, seen.len(), "{seen:?}");
+ assert!(
+ seen[0].contains(&format!("clamping temperature[0]={wide} -> 1.")),
+ "{seen:?}"
+ );
}
diff --git a/laya-rust/tests/temperature_strings_tests.rs b/laya-rust/tests/temperature_strings_tests.rs
index 9498337c..2d5b5a06 100644
--- a/laya-rust/tests/temperature_strings_tests.rs
+++ b/laya-rust/tests/temperature_strings_tests.rs
@@ -39,35 +39,46 @@ fn string_temperatures_follow_python_float_parsing() {
assert_eq!(vec![WARNING.to_string()], *seen);
}
-// Python: json.loads gives an `int` for a 400-digit literal and clamp_temperature raises
-// OverflowError("int too large to convert to float") uncaught; `1e400` is a float (inf -> 1.0);
-// 10**308 is still a valid int (-> 5.0); 10**309 overflows.
+// Python 0.4.20: json.loads gives an `int` for a 400-digit literal; `float()` raises OverflowError,
+// which `clamp_temperature` and `temperature_rejected` catch, so the entry becomes the neutral 1.0
+// and the loader's warning names it digit for digit. `1e400` is a float (inf -> 1.0); 10**308 is
+// still convertible (-> 5.0); 2**1024 is the smallest power of two that is not.
#[test]
-fn huge_integer_temperatures_fail_to_load_as_in_python_and_float_literals_do_not() {
+fn huge_integer_temperatures_fall_back_to_neutral_like_float_literals() {
let _guard = LOCK.lock().unwrap_or_else(|e| e.into_inner());
let _ = log::set_logger(&LOGGER);
log::set_max_level(log::LevelFilter::Warn);
- SEEN.lock().unwrap().clear();
let list = |entry: &str| format!(r#"{{"temperature": [1.0, {entry}, 1.0]}}"#);
let by_options =
|entry: &str| format!(r#"{{"temperature_by_options": {{"choice:2": {entry}}}}}"#);
let huge = format!("1{}", "0".repeat(400));
- for config in [
- list(&huge),
- list(&format!("-{huge}")),
- list(&format!("1{}", "0".repeat(309))),
- by_options(&huge),
- ] {
- let error = LayaConfig::parse(&config).unwrap_err();
- assert!(
- matches!(error, laya::error::LayaError::InvalidConfig(_)),
- "{error:?}"
- );
+ let two_1024 = "179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216";
+ for literal in [huge.clone(), format!("-{huge}"), two_1024.to_string()] {
+ SEEN.lock().unwrap().clear();
+ let config = LayaConfig::parse(&list(&literal)).unwrap();
+ assert_eq!(vec![1.0, 1.0, 1.0], config.temperature, "{literal}");
+ let seen = SEEN.lock().unwrap().clone();
assert_eq!(
- "OverflowError: int too large to convert to float",
- error.to_string()
+ vec![format!(
+ "laya ONNX: this checkpoint ships temperatures outside [0.5, 5] which would distort \
+ confidence; clamping temperature[1]={literal} -> 1. Treat confidence from the \
+ affected buckets as uncalibrated."
+ )],
+ seen
+ );
+
+ SEEN.lock().unwrap().clear();
+ let config = LayaConfig::parse(&by_options(&literal)).unwrap();
+ assert_eq!(Some(&1.0), config.temperature_by_options.get("choice:2"));
+ let seen = SEEN.lock().unwrap().clone();
+ assert_eq!(1, seen.len(), "{seen:?}");
+ assert!(
+ seen[0].contains(&format!("clamping choice:2={literal} -> 1. Treat")),
+ "{}",
+ seen[0]
);
}
+ SEEN.lock().unwrap().clear();
for (entry, applied) in [
("1e400".to_string(), 1.0),
("-1e400".to_string(), 1.0),