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.
Python calibrationclassificationdecision-modelhuggingfacejevmodernbertmultilingualnlppythonpytorchroutingtyped-decisionszero-shot
fix(rust,dotnet): clip shortlist cosines to the documented [-1, 1]
thread3d6b435de4332d
diff --git a/laya-dotnet/src/Laya/LayaShortlist.cs b/laya-dotnet/src/Laya/LayaShortlist.csindex 19fc6a9d..751f8ae8 100644--- a/laya-dotnet/src/Laya/LayaShortlist.cs+++ b/laya-dotnet/src/Laya/LayaShortlist.cs@@ -283,7 +283,16 @@ public static class LayaShortlistfor (var i = 0; i < docs.Length; i++){var denom = Norm(docs[i]) * qn;- if (denom > 0.0) sims[i] = Dot(docs[i], query) / denom;+ if (denom > 0.0)+ {+ // Python clips (`np.clip(dot / denom, -1.0, 1.0)`, laya/shortlist.py::_Cosine) and+ // laya-go ports that with TestCosineIsClipped. The quotient rounds outside [-1, 1]+ // for vectors that are parallel but not exactly representable -- a self-cosine of+ // [34, 15.285714285714286, 70.85714285714286] is 1.0000000000000002 -- so an+ // unclipped value breaks the range ShortlistInfo.Scores documents. Math.Clamp+ // returns NaN unchanged for a NaN input, as the Go port also leaves it alone.+ sims[i] = Math.Clamp(Dot(docs[i], query) / denom, -1.0, 1.0);+ }}return sims;}diff --git a/laya-dotnet/tests/Laya.Tests/LayaShortlistTests.cs b/laya-dotnet/tests/Laya.Tests/LayaShortlistTests.csindex f4b60078..f7c61603 100644--- a/laya-dotnet/tests/Laya.Tests/LayaShortlistTests.cs+++ b/laya-dotnet/tests/Laya.Tests/LayaShortlistTests.cs@@ -360,6 +360,31 @@ public sealed class LayaShortlistTeststhrow new InvalidOperationException($"no shortlist_probe.json entry labelled '{label}'");}+ [Fact]+ public void ScoresAreClippedToTheDocumentedRange()+ {+ // ShortlistInfo.Scores is documented as a cosine similarity, i.e. within [-1, 1]. Python+ // clips the quotient (np.clip(dot / denom, -1.0, 1.0)) and laya-go pins that with+ // TestCosineIsClipped; without the clip this port reported 1.0000000000000002 for a+ // self-cosine, because a parallel-vector quotient can round above one.+ var v = new[] { 34.0, 15.285714285714286, 70.85714285714286 };+ double[][] embed(IReadOnlyList<string> texts) => [.. texts.Select(_ => (double[])v.Clone())];+ var questions = new QuestionSet+ {+ ["intent"] = Question.Choice("Pick one", new Dictionary<string, object?>(StringComparer.Ordinal)+ {+ ["a"] = null, ["b"] = null, ["c"] = null,+ }),+ };++ var result = LayaShortlist.Predict(new Recorder(), "pick me", questions, embed, k: 2);++ var scores = result.Shortlist!["intent"].Scores;+ Assert.NotNull(scores);+ Assert.All(scores!, s => Assert.InRange(s, -1.0, 1.0));+ Assert.All(scores!, s => Assert.Equal(1.0, s));+ }+[Theory][MemberData(nameof(ShortlistProbeCases))]public void EveryShortlistProbeEntry(string label)diff --git a/laya-rust/src/laya_shortlist.rs b/laya-rust/src/laya_shortlist.rsindex 8d2f90b1..6d72aeeb 100644--- a/laya-rust/src/laya_shortlist.rs+++ b/laya-rust/src/laya_shortlist.rs@@ -316,7 +316,14 @@ fn cosine(query: &[f64], docs: &[Vec<f64>]) -> Vec<f64> {let denom = doc_norm * query_norm;if denom > 0.0 {let dot: f64 = doc.iter().zip(query).map(|(a, b)| a * b).sum();- sims[i] = dot / denom;+ // Python clips (`np.clip(dot / denom, -1.0, 1.0)`, `laya/shortlist.py::_cosine`) and+ // laya-go ports that with `TestCosineIsClipped` ("the clip is the reference's+ // contract"). The quotient rounds OUTSIDE [-1, 1] for vectors that are parallel but+ // not exactly representable -- a self-cosine of [34, 15.285714285714286,+ // 70.85714285714286] is 1.0000000000000002 -- so an unclipped value breaks the range+ // this API documents for `ShortlistInfo::scores`. `clamp` propagates NaN unchanged,+ // matching laya-go, which also leaves NaN alone.+ sims[i] = (dot / denom).clamp(-1.0, 1.0);}}simsdiff --git a/laya-rust/tests/laya_shortlist_tests.rs b/laya-rust/tests/laya_shortlist_tests.rsindex 14d9c4be..1649e9e4 100644--- a/laya-rust/tests/laya_shortlist_tests.rs+++ b/laya-rust/tests/laya_shortlist_tests.rs@@ -269,6 +269,46 @@ fn predict_reduces_choice_and_forwards_other_questions_unchanged() {);}+#[test]+fn scores_are_clipped_to_the_documented_range() {+ // `ShortlistInfo::scores` is documented as a cosine similarity, i.e. within [-1, 1]. Python+ // clips the quotient (`np.clip(dot / denom, -1.0, 1.0)`) and laya-go pins that with+ // `TestCosineIsClipped`; without the clip this port reported 1.0000000000000002 for a+ // self-cosine, because a parallel-vector quotient can round above one.+ let v = vec![34.0, 15.285714285714286, 70.85714285714286];+ let embed = |texts: &[String]| -> Result<Vec<Vec<f64>>> {+ Ok(texts.iter().map(|_| v.clone()).collect())+ };+ let criteria = [("a", Value::Null), ("b", Value::Null), ("c", Value::Null)];+ let mut questions = QuestionSet::new();+ questions = questions+ .with("intent", Question::choice("Pick one", criteria).unwrap())+ .unwrap();++ let result = LayaShortlist::predict(+ &FirstLabelPredictor,+ json!("pick me"),+ &questions,+ embed,+ 2,+ )+ .unwrap();+ let scores = result.shortlist().unwrap()["intent"]+ .scores+ .as_ref()+ .unwrap();+ for score in scores {+ assert!(+ (-1.0..=1.0).contains(score),+ "cosine {score} is outside the documented [-1, 1]"+ );+ }+ assert!(+ scores.iter().all(|s| *s == 1.0),+ "a self-cosine clips to exactly 1.0, got {scores:?}"+ );+}+#[test]fn predict_passthrough_forwards_the_original_question() {let full_criteria = [