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): size sequence buffers from their tokens, not the configured bound

kevin-gatimub3a0db2344bc
diff --git a/laya-rust/src/long_prediction.rs b/laya-rust/src/long_prediction.rs
index e310b8a4..27a3ee5e 100644
--- a/laya-rust/src/long_prediction.rs
+++ b/laya-rust/src/long_prediction.rs
@@ -143,7 +143,7 @@ pub(crate) fn plan_budget_warned(
these questions leave inside max_len={max_len}, so every window would be \
truncated to {room} on the way to the model; scanning with window={room} instead"
));
- } else if requested >= room * WINDOW_CLAMP_WARN_RATIO {
+ } else if requested >= room.saturating_mul(WINDOW_CLAMP_WARN_RATIO) {
warnings.push(format!(
"laya: predict_long: these questions leave only {room} of max_len={max_len} for \
the state (their heads take the rest), so the scan window is capped {requested} \
diff --git a/laya-rust/src/sequence_builder.rs b/laya-rust/src/sequence_builder.rs
index 09e1f017..53477873 100644
--- a/laya-rust/src/sequence_builder.rs
+++ b/laya-rust/src/sequence_builder.rs
@@ -181,11 +181,14 @@ impl SequenceBuilder {
let option_len_sum =
|opts: &[Vec<u32>]| -> i64 { opts.iter().map(|o| o.len() as i64).sum() };
- let mut opt_budget = head_max_len as i64 - option_len_sum(&option_ids);
+ // A bound beyond i64 (Python accepts any int) is as good as i64::MAX here: nothing the
+ // head can hold comes near it, and a plain `as i64` would wrap it negative.
+ let head_bound = i64::try_from(head_max_len).unwrap_or(i64::MAX);
+ let mut opt_budget = head_bound - option_len_sum(&option_ids);
let mut tokens_per_option = None;
if opt_budget < 16 {
let per = 4i64.max(Self::floor_div(
- head_max_len as i64 - 16,
+ head_bound - 16,
1i64.max(option_ids.len() as i64),
)) as usize;
tokens_per_option = Some(per);
@@ -194,7 +197,7 @@ impl SequenceBuilder {
fragment.truncate(per);
}
}
- opt_budget = head_max_len as i64 - option_len_sum(&option_ids);
+ opt_budget = head_bound - option_len_sum(&option_ids);
}
let head_take = (Self::MIN_HEAD_TOKENS as i64).max(opt_budget) as usize;
@@ -202,7 +205,10 @@ impl SequenceBuilder {
header_ids.truncate(head_take);
}
- let mut ids: Vec<u32> = Vec::with_capacity(head_max_len + 2);
+ // Sized from what is appended (CLS, header, SEP, the option fragments, SEP), never from
+ // the configured bound, which can be any size.
+ let appended = header_ids.len() + option_ids.iter().map(Vec::len).sum::<usize>() + 3;
+ let mut ids: Vec<u32> = Vec::with_capacity(appended);
ids.push(special.cls_id);
ids.extend_from_slice(&header_ids);
ids.push(special.sep_id);
@@ -240,7 +246,7 @@ impl SequenceBuilder {
) -> Result<usize> {
let head = Self::build_head(tokenizer, question, head_max_len)?;
// -1 reserves the SEP that closes the state.
- Ok((max_len as i64 - head.ids.len() as i64 - 1).max(0) as usize)
+ Ok(max_len.saturating_sub(head.ids.len() + 1))
}
/// The state's token ids exactly as `build_with_stats` slices them: serialized, mask token
@@ -267,7 +273,7 @@ impl SequenceBuilder {
} = Self::build_head(tokenizer, question, head_max_len)?;
// -1 reserves the final SEP that closes the whole sequence, appended below.
- let room = (max_len as i64 - ids.len() as i64 - 1).max(0) as usize;
+ let room = max_len.saturating_sub(ids.len() + 1);
let state_text = Self::scrub(&PythonJson::state(state), mask_token);
let state_ids = tokenizer.encode(&state_text)?;
let sliced: &[u32] = if truncate_left {
diff --git a/laya-rust/tests/sequence_builder_tests.rs b/laya-rust/tests/sequence_builder_tests.rs
index 64d6e22b..85de08bd 100644
--- a/laya-rust/tests/sequence_builder_tests.rs
+++ b/laya-rust/tests/sequence_builder_tests.rs
@@ -298,3 +298,23 @@ fn passes_string_instructions_through_untouched() {
SequenceBuilder::render_instructions(&q)
);
}
+
+// Python accepts any int for `max_len` / `head_max_len` (slicing by a huge int is fine) and builds
+// the same sequence as with the usual bounds when nothing is truncated (checked with
+// `common.build_sequence` for 2**61, 2**63 - 1, 2**64 - 1 and 10**30). The buffer must not be
+// sized from the bound: 2**61 + 2 elements of u32 is a capacity overflow, and `usize::MAX + 2`
+// overflows outright.
+#[test]
+fn a_huge_head_or_sequence_bound_builds_the_same_sequence_without_panicking() {
+ let tok = StubTokenizer::new();
+ let q = Question::choice("pick one", [("a", Value::Null), ("b", Value::Null)]).unwrap();
+ let state = json!("state text");
+ let base = SequenceBuilder::build(&tok, &state, &q, 512, 192, false).unwrap();
+ for huge in [1usize << 61, i64::MAX as usize, 1usize << 63, usize::MAX] {
+ for (max_len, head_max_len) in [(512, huge), (huge, 192), (huge, huge)] {
+ let built =
+ SequenceBuilder::build(&tok, &state, &q, max_len, head_max_len, false).unwrap();
+ assert_eq!(base, built, "max_len={max_len} head_max_len={head_max_len}");
+ }
+ }
+}