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): report clipped option-span collisions

kevin-gatimuef5bb7fce9c8
diff --git a/laya-rust/CHANGELOG.md b/laya-rust/CHANGELOG.md
index 4496ba77..5b766d86 100644
--- a/laya-rust/CHANGELOG.md
+++ b/laya-rust/CHANGELOG.md
@@ -18,6 +18,7 @@ goldens for the supported paths (see the known gaps below).
- Engine score legends render structured criteria as strings, matching Python 0.4.0.
- `NoMarkers` errors include actual/requested marker counts and the total sequence budget,
matching Python's actionable truncation diagnostic.
+- State usage also reports option-span collisions after clipping, including the applied cap.
- `LayaEngine`: `choice`, `score` and `noul` predictions over the exported ONNX checkpoints
(english, multilingual, typed-decisions); all questions for a state run in one batched forward pass.
- Loads both ONNX layouts: the fused `model.onnx` and the split `encoder.onnx` + `head.onnx`.
diff --git a/laya-rust/README.md b/laya-rust/README.md
index 1845f1a9..c632bca7 100644
--- a/laya-rust/README.md
+++ b/laya-rust/README.md
@@ -561,6 +561,7 @@ improvements unblocked; Rust drift needs a follow-up port. This workflow never p
Engine results expose Python 0.4.0's state-budget diagnostics via `LayaResult::state_usage()`:
original state tokens, worst-case dropped tokens, whether truncation occurred, and the affected
question ids. The existing `Usage` totals and result constructor remain source-compatible.
+`state_usage().options` additionally identifies questions whose clipped option spans collide.
To reproduce it, install CPU torch and [`tools/requirements-regen.txt`](tools/requirements-regen.txt),
then run from the repository root:
diff --git a/laya-rust/src/answers.rs b/laya-rust/src/answers.rs
index bb483c67..70db3075 100644
--- a/laya-rust/src/answers.rs
+++ b/laya-rust/src/answers.rs
@@ -221,6 +221,17 @@ impl Answer {
}
}
+/// Option-span collision diagnostics reported only when clipping makes options indistinguishable.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub struct OptionUsage {
+ /// Options requested by the question.
+ pub total: usize,
+ /// Distinct token spans remaining after per-option clipping.
+ pub distinct: usize,
+ /// Applied token cap, including the marker, or `None` when no cap was applied.
+ pub tokens_per_option: Option<usize>,
+}
+
/// State-budget diagnostics reported by Python's `usage` since 0.4.0.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StateUsage {
@@ -232,6 +243,8 @@ pub struct StateUsage {
pub truncated: bool,
/// Ids of questions that lost state tokens, in question order.
pub truncated_questions: Vec<String>,
+ /// Questions whose options collapsed to identical token spans.
+ pub options: IndexMap<String, OptionUsage>,
}
/// The result of one `predict` call: one answer per question, plus token usage.
diff --git a/laya-rust/src/laya_engine.rs b/laya-rust/src/laya_engine.rs
index 0bb14304..3d2c3743 100644
--- a/laya-rust/src/laya_engine.rs
+++ b/laya-rust/src/laya_engine.rs
@@ -180,6 +180,7 @@ impl LayaEngine {
state_tokens_dropped: 0,
truncated: false,
truncated_questions: Vec::new(),
+ options: IndexMap::new(),
};
// A conversation-turn list is serialized newest-last (see `PythonJson::state`), so the
@@ -206,6 +207,9 @@ impl LayaEngine {
state_usage.truncated = true;
state_usage.truncated_questions.push(id.clone());
}
+ if let Some(options) = stats.options {
+ state_usage.options.insert(id.clone(), options);
+ }
// A marker dropped for landing past max_len means an option is not in the sequence at
// all, so its logit would score whatever token happens to sit at position 0.
diff --git a/laya-rust/src/sequence_builder.rs b/laya-rust/src/sequence_builder.rs
index 38519fd2..24c28ab7 100644
--- a/laya-rust/src/sequence_builder.rs
+++ b/laya-rust/src/sequence_builder.rs
@@ -20,6 +20,7 @@ pub struct SequenceBuilder;
pub(crate) struct StateTokenStats {
pub tokens: usize,
pub dropped: usize,
+ pub options: Option<crate::answers::OptionUsage>,
}
impl SequenceBuilder {
@@ -168,11 +169,13 @@ impl SequenceBuilder {
|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);
+ let mut tokens_per_option = None;
if opt_budget < 16 {
let per = 4i64.max(Self::floor_div(
head_max_len as i64 - 16,
1i64.max(option_ids.len() as i64),
)) as usize;
+ tokens_per_option = Some(per);
for fragment in &mut option_ids {
if fragment.len() > per {
fragment.truncate(per);
@@ -222,6 +225,14 @@ impl SequenceBuilder {
let stats = StateTokenStats {
tokens: state_ids.len(),
dropped: state_ids.len() - sliced.len(),
+ options: {
+ let distinct = option_ids.iter().collect::<std::collections::HashSet<_>>().len();
+ (distinct < option_ids.len()).then_some(crate::answers::OptionUsage {
+ total: option_ids.len(),
+ distinct,
+ tokens_per_option,
+ })
+ },
};
Ok((ids, markers, stats))
}
diff --git a/laya-rust/tests/common/result_compare.rs b/laya-rust/tests/common/result_compare.rs
index 9be5925a..bba85d44 100644
--- a/laya-rust/tests/common/result_compare.rs
+++ b/laya-rust/tests/common/result_compare.rs
@@ -91,8 +91,24 @@ pub fn assert_result_matches(where_case: &str, expected: &Value, result: &LayaRe
pub fn assert_state_usage_matches(where_case: &str, expected: &Value, result: &LayaResult) {
let usage = result.state_usage().expect("engine result must report state usage");
let want = &expected["usage"];
- assert_eq!(want.as_object().unwrap().len(), 6,
+ let options = want.get("options").and_then(Value::as_object);
+ assert_eq!(want.as_object().unwrap().len(), 6 + usize::from(options.is_some()),
"{where_case}: new Python usage fields need a Rust port");
+ assert_eq!(options.map_or(0, |o| o.len()), usage.options.len(),
+ "{where_case}: usage.options count");
+ if let Some(options) = options {
+ for (id, expected) in options {
+ assert_eq!(expected.as_object().unwrap().len(), 3,
+ "{where_case}/{id}: new option usage fields need a Rust port");
+ let got = usage.options.get(id).expect("collapsed option diagnostic missing");
+ assert_eq!(expected["total"].as_u64().unwrap() as usize, got.total,
+ "{where_case}/{id}: usage.options.total");
+ assert_eq!(expected["distinct"].as_u64().unwrap() as usize, got.distinct,
+ "{where_case}/{id}: usage.options.distinct");
+ assert_eq!(expected["tokens_per_option"].as_u64().map(|v| v as usize), got.tokens_per_option,
+ "{where_case}/{id}: usage.options.tokens_per_option");
+ }
+ }
assert_eq!(
want["state_tokens"].as_u64().unwrap() as usize, usage.state_tokens,
"{where_case}: usage.state_tokens"