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): refuse a wrongly typed or negative max_len at load

kevin-gatimu15d6dcf7c6f5
diff --git a/laya-rust/CHANGELOG.md b/laya-rust/CHANGELOG.md
index 141093ec..318e4140 100644
--- a/laya-rust/CHANGELOG.md
+++ b/laya-rust/CHANGELOG.md
@@ -64,7 +64,7 @@ regenerated from laya (Python) 0.4.11 for the supported paths (see the known gap
- 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.
- 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: `LayaEngine::create` loads and `predict`/`predict_long` return the same `TypeError` text; `LayaConfig::parse` on its own refuses it). 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.
+- 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.
- Config JSON errors read like Python's `JSONDecodeError` (`Expecting ',' delimiter: line 1 column 7 (char 6)`, checked on 1,500 generated malformed texts), a file is read in text mode (`
` becomes `
` first), and `temperature` / `temperature_by_options` of the wrong JSON type fail while loading with Python's `TypeError` / `AttributeError` text (a string iterates its characters, a dict its keys).
diff --git a/laya-rust/README.md b/laya-rust/README.md
index 3a54b1a7..48332f4e 100644
--- a/laya-rust/README.md
+++ b/laya-rust/README.md
@@ -742,6 +742,19 @@ Rust port groups the corresponding functions into a unit struct with associated
- **`Agent::new_with_defaults()`, reused across all five files in one download.** Matches Python's
single reused HTTP session, but there's no async cancellation token — a download that must be
cancellable should run on its own thread and be dropped/joined by the caller.
+- **Config values Python reads loosely are refused at load.** `max_len` and `head_max_len` accept a
+ non-negative JSON integer or a bool (`true` is 1, `false` is 0), as Python does. A float, string,
+ `null`, list or object is refused when the engine is created, with the `TypeError` text Python
+ raises at its first prediction (`head_max_len` is met first); a negative value is refused with
+ `max_len=-5: must be a non-negative integer`, since Python slices from the end for it. Python
+ also tolerates a fractional `head_max_len` below about 4.0; that is refused here too.
+- **Lone surrogate escapes in config JSON are refused.** `"\ud800"` loads in Python and is an error
+ here, because a Rust `String` cannot hold a lone surrogate.
+- **JSON nesting is cut off at 1000 levels.** Python's limit depends on the interpreter's
+ recursion limit.
+- **A `temperature` list shorter than three** loads and fails with `IndexError: list index out of
+ range` for the first question whose type index it lacks, as Python does; entries past the third
+ are ignored.
---
diff --git a/laya-rust/src/laya_config.rs b/laya-rust/src/laya_config.rs
index bce89cb9..5cd99e68 100644
--- a/laya-rust/src/laya_config.rs
+++ b/laya-rust/src/laya_config.rs
@@ -291,47 +291,36 @@ fn check_option_layout(root: &PyJson) -> Result<()> {
}
}
-/// What `cfg.get(key, default)` gave for `max_len` / `head_max_len`, as Python's arithmetic and
-/// slicing read it.
-enum Length {
- Value(usize),
- /// A float, string, `null`, list or object: Python raises `TypeError` the first time it builds
- /// a sequence. The Python type name.
- BadType(&'static str),
-}
-
-/// The checks behind [`Length`]:
-/// - missing: the default; `true`/`false` are the ints 1/0; `-0` is 0; an integer beyond the
-/// platform's range is as good as unbounded here;
-/// - a negative integer slices from the end in Python, which has no counterpart in this port: an
-/// error naming that (a float `head_max_len` below 4.0 also survives in Python; that corner is
-/// not ported).
-fn config_length(root: &PyJson, key: &str, default: usize) -> Result<Length> {
+/// `cfg.get(key, default)` for `max_len` / `head_max_len`, as Python's arithmetic and slicing
+/// read it. Accepted: a non-negative integer, a bool (`true` is 1, `false` is 0), `-0` (0), and
+/// an integer beyond the platform's range (as good as unbounded). Refused at load:
+/// - a float, string, `null`, list or object: Python's `TypeError`, which it raises the first
+/// time a prediction builds a sequence;
+/// - a negative integer: Python slices from the end for it, which this port does not emulate.
+fn config_length(root: &PyJson, key: &str, default: usize) -> Result<usize> {
+ let refused = |kind: &str| LayaError::InvalidConfig(length_type_error(kind));
match root.get(key) {
- None => Ok(Length::Value(default)),
- Some(PyJson::Bool(b)) => Ok(Length::Value(usize::from(*b))),
+ None => Ok(default),
+ Some(PyJson::Bool(b)) => Ok(usize::from(*b)),
Some(PyJson::Int(literal)) => {
let digits = literal.trim_start_matches('-');
if digits.bytes().all(|b| b == b'0') {
- Ok(Length::Value(0))
+ Ok(0)
} else if literal.starts_with('-') {
Err(LayaError::InvalidConfig(format!(
- "{key}={literal} is negative: Python slices from the end for it, which this \
- port does not support"
+ "{key}={literal}: must be a non-negative integer"
)))
} else {
- Ok(Length::Value(
- digits
- .parse::<u64>()
- .map_or(usize::MAX, |v| usize::try_from(v).unwrap_or(usize::MAX)),
- ))
+ Ok(digits
+ .parse::<u64>()
+ .map_or(usize::MAX, |v| usize::try_from(v).unwrap_or(usize::MAX)))
}
}
- Some(PyJson::Float(_)) => Ok(Length::BadType("float")),
- Some(PyJson::Str(_)) => Ok(Length::BadType("str")),
- Some(PyJson::Null) => Ok(Length::BadType("NoneType")),
- Some(PyJson::Array(_)) => Ok(Length::BadType("list")),
- Some(PyJson::Object(_)) => Ok(Length::BadType("dict")),
+ Some(PyJson::Float(_)) => Err(refused("float")),
+ Some(PyJson::Str(_)) => Err(refused("str")),
+ Some(PyJson::Null) => Err(refused("NoneType")),
+ Some(PyJson::Array(_)) => Err(refused("list")),
+ Some(PyJson::Object(_)) => Err(refused("dict")),
}
}
@@ -344,76 +333,16 @@ fn length_type_error(kind: &str) -> String {
}
}
-/// A `max_len` / `head_max_len` that makes Python raise `TypeError` at the first prediction,
-/// while loading the checkpoint succeeds. The engine keeps it and returns it from `predict` /
-/// `predict_long`.
-#[derive(Debug, Clone)]
-pub(crate) struct DeferredLengthError {
- /// Raised by a prediction with questions: `build_head` meets `head_max_len` first, then
- /// `max_len`.
- pub(crate) with_questions: String,
- /// Raised by `predict_long` without questions, where `window_budget` evaluates
- /// `max_len - head_max_len - 8` first.
- pub(crate) no_questions: String,
- /// Set when `predict_long` without questions gets past that expression because only floats
- /// are wrong: Python then scans against this float window (`max(64, max_len - head_max_len -
- /// 8)`), which a state at most that long fits in one pass, and anything longer fails with the
- /// slice `TypeError` in `with_questions`.
- pub(crate) no_questions_window: Option<f64>,
-}
-
/// What Python only discovers when a prediction needs it, kept by the engine (not part of the
/// public [`LayaConfig`]).
#[derive(Debug, Clone, Default)]
pub(crate) struct Deferred {
- /// A `max_len` / `head_max_len` of the wrong type.
- pub(crate) length: Option<DeferredLengthError>,
/// The real length of a `temperature` list shorter than three: Python indexes
/// `self.temperature[qtype]` for every question, so a question type at or past it raises
/// `IndexError`, however its bucket resolves.
pub(crate) short_temperature: Option<usize>,
}
-fn deferred_length_error(
- root: &PyJson,
- max: &Length,
- head: &Length,
-) -> Option<DeferredLengthError> {
- let kind = |l: &Length| -> &'static str {
- match l {
- Length::Value(_) => "int",
- Length::BadType(k) => k,
- }
- };
- let with_questions = match (head, max) {
- (Length::BadType(k), _) | (_, Length::BadType(k)) => length_type_error(k),
- _ => return None,
- };
- // Without questions `window_budget` starts with `max_len - head_max_len`; a float on its own
- // survives that, anything else does not.
- let (left, right) = (kind(max), kind(head));
- let non_numeric = |k: &str| !matches!(k, "int" | "float");
- let float_only = !(non_numeric(left) || non_numeric(right));
- let no_questions = if float_only {
- with_questions.clone()
- } else {
- format!("TypeError: unsupported operand type(s) for -: '{left}' and '{right}'")
- };
- let as_f64 = |key: &str, default: f64| match root.get(key) {
- Some(PyJson::Float(f)) => *f,
- Some(PyJson::Bool(b)) => f64::from(u8::from(*b)),
- Some(PyJson::Int(literal)) => literal.parse().unwrap_or(default),
- _ => default,
- };
- let no_questions_window = float_only
- .then(|| (as_f64("max_len", 512.0) - as_f64("head_max_len", 192.0) - 8.0).max(64.0));
- Some(DeferredLengthError {
- with_questions,
- no_questions,
- no_questions_window,
- })
-}
-
/// Python opens the file in text mode, which turns `\r\n` and a lone `\r` into `\n` before
/// `json.load` sees the text; the line, column and offset in a decode error count the result.
pub(crate) fn python_text_mode(text: String) -> String {
@@ -431,13 +360,12 @@ impl LayaConfig {
Self::parse(&python_text_mode(std::fs::read_to_string(path)?))
}
- /// [`Self::load`] for the engine: a `max_len` / `head_max_len` of the wrong type is returned
- /// instead of refused, because Python only fails at the first prediction.
+ /// [`Self::load`] for the engine, which also keeps what Python only discovers at prediction
+ /// time.
#[track_caller]
pub(crate) fn load_deferred(path: impl AsRef<Path>) -> Result<(Self, Deferred)> {
Self::parse_with(
&python_text_mode(std::fs::read_to_string(path)?),
- true,
std::panic::Location::caller(),
)
}
@@ -445,14 +373,10 @@ impl LayaConfig {
/// Parse a config from JSON text.
#[track_caller]
pub fn parse(json: &str) -> Result<Self> {
- Self::parse_with(json, false, std::panic::Location::caller()).map(|(config, _)| config)
+ Self::parse_with(json, std::panic::Location::caller()).map(|(config, _)| config)
}
- fn parse_with(
- json: &str,
- defer_length_types: bool,
- at: &std::panic::Location<'_>,
- ) -> Result<(Self, Deferred)> {
+ fn parse_with(json: &str, at: &std::panic::Location<'_>) -> Result<(Self, Deferred)> {
let root = PyJson::parse(json)?;
check_option_layout(&root)?;
// Python builds `[clamp_temperature(t) for t in temperature]` and then the by-options dict,
@@ -503,37 +427,17 @@ impl LayaConfig {
}
}
+ // Refused at load: Python's `build_head` meets `head_max_len` before `max_len`.
+ let head_max_len = config_length(&root, "head_max_len", 192)?;
+ let max_len = config_length(&root, "max_len", 512)?;
+
if let Some(warning) = temperature_warning(&options, &elements) {
emit_warnings(&[warning], at);
}
- // Python reads these with `cfg.get("max_len", 512)` and only fails when a prediction uses
- // them. `parse` refuses a value of the wrong type at load, after the load-time warning
- // above; the engine defers it to its first prediction, as Python does.
- let max = config_length(&root, "max_len", 512)?;
- let head = config_length(&root, "head_max_len", 192)?;
let deferred = Deferred {
- length: deferred_length_error(&root, &max, &head),
short_temperature: (elements.len() < 3).then_some(elements.len()),
};
- if !defer_length_types && deferred.length.is_some() {
- let kind_of = |l: &Length| match l {
- Length::BadType(k) => Some(*k),
- Length::Value(_) => None,
- };
- let kind = kind_of(&max)
- .or_else(|| kind_of(&head))
- .unwrap_or("NoneType");
- return Err(LayaError::InvalidConfig(length_type_error(kind)));
- }
- let max_len = match max {
- Length::Value(v) => v,
- Length::BadType(_) => 512,
- };
- let head_max_len = match head {
- Length::Value(v) => v,
- Length::BadType(_) => 192,
- };
Ok((
LayaConfig {
@@ -555,93 +459,59 @@ impl LayaConfig {
mod tests {
use super::LayaConfig;
- // Messages from Python 3.12: `common.build_sequence` / `window_budget` with these two values,
- // which is what the first prediction (with and without questions) hits.
+ // Python 3.12: the `TypeError` the first prediction with questions raises (`build_head` meets
+ // `head_max_len` first, then `max_len`); a wrongly typed or negative length is refused at load.
#[test]
- fn engine_load_defers_a_wrongly_typed_length_with_pythons_messages() {
- let operand = |l: &str, r: &str| {
- format!("TypeError: unsupported operand type(s) for -: '{l}' and '{r}'")
+ fn a_wrongly_typed_or_negative_length_is_refused_at_load_with_pythons_text() {
+ let operand = |kind: &str| {
+ format!("TypeError: unsupported operand type(s) for -: '{kind}' and 'int'")
};
let slice = "TypeError: slice indices must be integers or None or have an __index__ method";
- let cases: [(&str, String, String); 8] = [
- (
- r#"{"max_len": "512"}"#,
- operand("str", "int"),
- operand("str", "int"),
- ),
- (
- r#"{"max_len": null}"#,
- operand("NoneType", "int"),
- operand("NoneType", "int"),
- ),
- (
- r#"{"head_max_len": "x"}"#,
- operand("str", "int"),
- operand("int", "str"),
- ),
- (
- r#"{"head_max_len": null}"#,
- operand("NoneType", "int"),
- operand("int", "NoneType"),
- ),
+ let cases: [(&str, String); 9] = [
+ (r#"{"max_len": "512"}"#, operand("str")),
+ (r#"{"max_len": null}"#, operand("NoneType")),
+ (r#"{"head_max_len": "x"}"#, operand("str")),
+ (r#"{"head_max_len": null}"#, operand("NoneType")),
+ (r#"{"max_len": 512.0}"#, slice.to_string()),
+ (r#"{"head_max_len": [1]}"#, operand("list")),
+ // both wrong: head_max_len is met first
(
r#"{"max_len": "x", "head_max_len": null}"#,
- operand("NoneType", "int"),
- operand("str", "NoneType"),
+ operand("NoneType"),
),
+ (r#"{"max_len": 512.0, "head_max_len": "x"}"#, operand("str")),
(
- r#"{"max_len": 512.0}"#,
- slice.to_string(),
+ r#"{"max_len": "x", "head_max_len": 192.0}"#,
slice.to_string(),
),
+ ];
+ for (config, expected) in cases {
+ match LayaConfig::parse(config) {
+ Err(crate::error::LayaError::InvalidConfig(got)) => {
+ assert_eq!(expected, got, "{config}");
+ }
+ other => panic!("{config}: expected InvalidConfig, got {other:?}"),
+ }
+ }
+ for (config, expected) in [
(
- r#"{"max_len": 512.0, "head_max_len": "x"}"#,
- operand("str", "int"),
- operand("float", "str"),
+ r#"{"max_len": -5}"#,
+ "max_len=-5: must be a non-negative integer",
),
(
- r#"{"head_max_len": [1]}"#,
- operand("list", "int"),
- operand("int", "list"),
+ r#"{"head_max_len": -1}"#,
+ "head_max_len=-1: must be a non-negative integer",
),
- ];
- for (config, with_questions, no_questions) in cases {
- // Refused by the public `parse`, loaded (with the defaults) for the engine.
- assert!(LayaConfig::parse(config).is_err(), "{config}");
- let (loaded, deferred) =
- LayaConfig::parse_with(config, true, std::panic::Location::caller()).unwrap();
- let deferred = deferred
- .length
- .unwrap_or_else(|| panic!("{config}: nothing deferred"));
- assert_eq!(with_questions, deferred.with_questions, "{config}");
- assert_eq!(no_questions, deferred.no_questions, "{config}");
- assert_eq!(
- (512, 192),
- (loaded.max_len, loaded.head_max_len),
- "{config}"
- );
- }
- // Only floats wrong and no questions: Python's `window_budget` passes with this window
- // (a state of at most that many tokens is answered, a longer one hits the slice error).
- for (config, window) in [
- (r#"{"max_len": 512.0}"#, Some(312.0)),
- (r#"{"head_max_len": 192.0}"#, Some(312.0)),
- (r#"{"max_len": 300.5}"#, Some(100.5)),
- (r#"{"max_len": 100.0, "head_max_len": 10.0}"#, Some(82.0)),
- (r#"{"max_len": 100.0, "head_max_len": 90.0}"#, Some(64.0)),
- (r#"{"max_len": "x"}"#, None),
- (r#"{"max_len": 512.0, "head_max_len": "x"}"#, None),
] {
- let (_, deferred) =
- LayaConfig::parse_with(config, true, std::panic::Location::caller()).unwrap();
- let deferred = deferred.length.unwrap();
- assert_eq!(window, deferred.no_questions_window, "{config}");
- }
- for config in [r#"{}"#, r#"{"max_len": 256, "head_max_len": true}"#] {
- let (_, deferred) =
- LayaConfig::parse_with(config, true, std::panic::Location::caller()).unwrap();
- assert!(deferred.length.is_none(), "{config}");
+ match LayaConfig::parse(config) {
+ Err(crate::error::LayaError::InvalidConfig(got)) => assert_eq!(expected, got),
+ other => panic!("{config}: expected InvalidConfig, got {other:?}"),
+ }
}
+ }
+
+ #[test]
+ fn a_short_temperature_list_is_kept_for_the_engine_and_a_longer_one_is_ignored() {
// A `temperature` list shorter than three loads; the engine fails the question types whose
// index it lacks (checked against `_decode_answers`: choice needs 1 entry, score 2, noul
// 3, with or without a matching `temperature_by_options` bucket). Longer lists are fine.
@@ -655,7 +525,7 @@ mod tests {
(r#"{}"#, None),
] {
let (loaded, deferred) =
- LayaConfig::parse_with(config, true, std::panic::Location::caller()).unwrap();
+ LayaConfig::parse_with(config, std::panic::Location::caller()).unwrap();
assert_eq!(short, deferred.short_temperature, "{config}");
assert_eq!(3, loaded.temperature.len().min(3), "{config}");
}
@@ -665,11 +535,5 @@ mod tests {
3.0,
crate::calibration::Calibration::resolve_temperature(&longer, noul, 2)
);
-
- // A negative length stays refused even for the engine.
- assert!(
- LayaConfig::parse_with(r#"{"max_len": -5}"#, true, std::panic::Location::caller())
- .is_err()
- );
}
}
diff --git a/laya-rust/src/laya_engine.rs b/laya-rust/src/laya_engine.rs
index a19b4def..532f1e68 100644
--- a/laya-rust/src/laya_engine.rs
+++ b/laya-rust/src/laya_engine.rs
@@ -71,8 +71,7 @@ pub struct LayaEngine {
tokenizer: Box<dyn LayaTokenizer>,
config: LayaConfig,
artifacts: ModelArtifacts,
- /// A `max_len` / `head_max_len` of the wrong type: Python loads the checkpoint and raises
- /// `TypeError` at the first prediction, so this is returned from `predict*`, not from `create`.
+ /// What Python only discovers at prediction time (a `temperature` list shorter than three).
deferred: Deferred,
}
@@ -236,29 +235,7 @@ impl LayaEngine {
options: &LongOptions,
) -> Result<LayaResult> {
options.check_aggregate()?;
- // Python reads `cfg.get("max_len")` right after the argument checks, and
- // `window_budget` is the first thing to use it.
let tokenizer = self.tokenizer.as_ref();
- if let Some(error) = &self.deferred.length {
- if !questions.is_empty() {
- return Err(LayaError::InvalidConfig(error.with_questions.clone()));
- }
- let Some(window) = error.no_questions_window else {
- return Err(LayaError::InvalidConfig(error.no_questions.clone()));
- };
- // Only floats are wrong and there are no questions: Python's `window_budget` passes,
- // a state that fits the float window is answered (with no answers) in one pass, and
- // a longer one fails on its first slice.
- let state_ids = SequenceBuilder::encode_state(tokenizer, &state)?;
- if state_ids.len() as f64 > window {
- return Err(LayaError::InvalidConfig(error.with_questions.clone()));
- }
- let single = self.predict_states(&[state], questions)?.remove(0);
- return Ok(single.with_scan_usage(ScanUsage {
- windows: 1,
- truncated_windows: 0,
- }));
- }
let explicit_window = options.window.unwrap_or(0);
let (budget, warnings) = long_prediction::window_budget_warned(
tokenizer,
@@ -327,9 +304,6 @@ impl LayaEngine {
.map(|_| LayaResult::new(MODEL_NAME, Vec::new(), HashMap::new(), Usage::default()))
.collect());
}
- if let Some(error) = &self.deferred.length {
- return Err(LayaError::InvalidConfig(error.with_questions.clone()));
- }
let mut items = Vec::with_capacity(ids.len() * states.len());
let mut state_usages = Vec::with_capacity(states.len());
diff --git a/laya-rust/tests/config_numbers_tests.rs b/laya-rust/tests/config_numbers_tests.rs
index 28a3f9fc..aa2d1ddc 100644
--- a/laya-rust/tests/config_numbers_tests.rs
+++ b/laya-rust/tests/config_numbers_tests.rs
@@ -83,7 +83,10 @@ fn max_len_and_head_max_len_follow_python() {
assert_eq!(operand("list"), error_of(&format!(r#"{{"{key}": []}}"#)));
assert_eq!(operand("dict"), error_of(&format!(r#"{{"{key}": {{}}}}"#)));
assert_eq!(slice, error_of(&format!(r#"{{"{key}": 512.0}}"#)));
- assert!(error_of(&format!(r#"{{"{key}": -5}}"#)).contains("negative"));
+ assert_eq!(
+ format!("{key}=-5: must be a non-negative integer"),
+ error_of(&format!(r#"{{"{key}": -5}}"#))
+ );
}
}
diff --git a/laya-rust/tests/predict_long_tests.rs b/laya-rust/tests/predict_long_tests.rs
index 00acd62c..38cf0033 100644
--- a/laya-rust/tests/predict_long_tests.rs
+++ b/laya-rust/tests/predict_long_tests.rs
@@ -487,11 +487,13 @@ impl Drop for ScratchDir {
}
}
-/// Python loads a checkpoint whose `max_len` / `head_max_len` has the wrong type and raises the
-/// `TypeError` at the first prediction (`build_head`, `window_budget`); `LayaEngine` must do the
-/// same. The messages were produced by Python's `common.build_sequence` and `window_budget`.
+/// Checkpoint configs whose `max_len` / `head_max_len` / `temperature` Python reads loosely. A
+/// wrongly typed or negative length is refused when the engine is created, with the `TypeError`
+/// text Python raises at the first prediction; a `temperature` list shorter than three loads and
+/// fails with Python's `IndexError` for a question type whose index it lacks; a longer one is
+/// fine. The messages were produced by Python's `common.build_sequence` and `_decode_answers`.
#[test]
-fn a_wrongly_typed_length_in_the_config_fails_at_the_first_prediction() {
+fn config_lengths_and_temperatures_are_refused_or_deferred_as_agreed() {
let Some(source) = common::test_artifacts::locate(LayaCheckpoint::English).filter(|dir| {
dir.join("model.onnx").is_file() && dir.join("tokenizer").join("tokenizer.json").is_file()
}) else {
@@ -510,19 +512,47 @@ fn a_wrongly_typed_length_in_the_config_fails_at_the_first_prediction() {
);
let operand =
|kind: &str| format!("TypeError: unsupported operand type(s) for -: '{kind}' and 'int'");
+ let make = |change: Option<(&str, Value)>| {
+ let _ = std::fs::remove_dir_all(&scratch.0);
+ link_tree(&source, &scratch.0);
+ let config_path = scratch.0.join("rl_agent_config.json");
+ std::fs::remove_file(&config_path).unwrap();
+ let mut config: Value = serde_json::from_str(&config_text).unwrap();
+ if let Some((key, value)) = change {
+ config[key] = value;
+ }
+ std::fs::write(&config_path, serde_json::to_string(&config).unwrap()).unwrap();
+ LayaEngine::from_directory(&scratch.0)
+ };
- // (key, JSON value, expected message); `None` keeps the shipped config as it is.
- let cases = [
- (Some(("max_len", Value::from("512"))), Some(operand("str"))),
+ // Refused at create / from_directory.
+ let refused = [
+ (("max_len", Value::from("512")), operand("str")),
+ (("head_max_len", Value::Null), operand("NoneType")),
+ (
+ ("max_len", Value::from(300.5)),
+ "TypeError: slice indices must be integers or None or have an __index__ method"
+ .to_string(),
+ ),
(
- Some(("head_max_len", Value::Null)),
- Some(operand("NoneType")),
+ ("max_len", Value::from(-5)),
+ "max_len=-5: must be a non-negative integer".to_string(),
),
- // Python indexes `self.temperature[2]` for a noul question: a one-entry list fails there
- // with IndexError, and entries past the third are ignored.
+ ];
+ for (change, expected) in refused {
+ match make(Some(change)) {
+ Err(LayaError::InvalidConfig(got)) => assert_eq!(expected, got),
+ Err(other) => panic!("expected InvalidConfig({expected}), got {other:?}"),
+ Ok(_) => panic!("expected InvalidConfig({expected}), but the engine loaded"),
+ }
+ }
+
+ // Loads; the short temperature list fails predict and predict_long (a noul question needs
+ // index 2); a longer list and the shipped config predict normally.
+ let cases = [
(
Some(("temperature", serde_json::json!([1.0]))),
- Some("IndexError: list index out of range".to_string()),
+ Some("IndexError: list index out of range"),
),
(
Some(("temperature", serde_json::json!([1.0, 1.0, 1.0, 9.0]))),
@@ -531,18 +561,7 @@ fn a_wrongly_typed_length_in_the_config_fails_at_the_first_prediction() {
(None, None),
];
for (change, expected) in cases {
- let _ = std::fs::remove_dir_all(&scratch.0);
- link_tree(&source, &scratch.0);
- let config_path = scratch.0.join("rl_agent_config.json");
- std::fs::remove_file(&config_path).unwrap();
- let mut config: Value = serde_json::from_str(&config_text).unwrap();
- if let Some((key, value)) = &change {
- config[*key] = value.clone();
- }
- std::fs::write(&config_path, serde_json::to_string(&config).unwrap()).unwrap();
-
- let engine = LayaEngine::from_directory(&scratch.0)
- .expect("loading succeeds: Python only fails at the first prediction");
+ let engine = make(change).expect("these configs load");
let predicted = engine.predict("Please refund my order.", &questions);
let scanned =
engine.predict_long("Please refund my order.", &questions, &LongOptions::new());
@@ -556,50 +575,10 @@ fn a_wrongly_typed_length_in_the_config_fails_at_the_first_prediction() {
}
}
None => {
- predicted.expect("a valid config predicts normally");
- scanned.expect("a valid config scans normally");
+ predicted.expect("predicts normally");
+ scanned.expect("scans normally");
}
}
drop(engine);
}
-
- // Only a float is wrong (`max_len` 300.5, so Python's float window is max(64, 300.5 - 192 - 8)
- // = 100.5 tokens). Python's `predict_long` without questions answers a state that fits it
- // (empty answers, `windows: 1`) and fails on the first slice of a longer one; with questions
- // both `predict` and `predict_long` raise the slice-indices TypeError. Probed with Python's
- // `ONNXAgent.predict_long` on a session-less agent.
- let slice = "TypeError: slice indices must be integers or None or have an __index__ method";
- let _ = std::fs::remove_dir_all(&scratch.0);
- link_tree(&source, &scratch.0);
- let config_path = scratch.0.join("rl_agent_config.json");
- std::fs::remove_file(&config_path).unwrap();
- let mut config: Value = serde_json::from_str(&config_text).unwrap();
- config["max_len"] = Value::from(300.5);
- config["head_max_len"] = Value::from(192);
- std::fs::write(&config_path, serde_json::to_string(&config).unwrap()).unwrap();
- let engine = LayaEngine::from_directory(&scratch.0).expect("loading succeeds");
- let none = QuestionSet::new();
- let options = LongOptions::new();
-
- let short = engine
- .predict_long("short text", &none, &options)
- .expect("a state inside the float window is answered");
- assert!(short.ids().is_empty());
- assert_eq!(Some(1), short.scan_usage().map(|u| u.windows));
-
- let long_state = "word ".repeat(400);
- match engine.predict_long(long_state.as_str(), &none, &options) {
- Err(LayaError::InvalidConfig(got)) => assert_eq!(slice, got),
- other => panic!("expected the slice TypeError, got {other:?}"),
- }
- for outcome in [
- engine.predict("short text", &questions),
- engine.predict_long("short text", &questions, &options),
- ] {
- match outcome {
- Err(LayaError::InvalidConfig(got)) => assert_eq!(slice, got),
- other => panic!("expected the slice TypeError, got {other:?}"),
- }
- }
- drop(engine);
}