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(java): answer single-option questions instead of failing in TopK

Adeshina Lasisi1e68e9a0ce4e
diff --git a/.github/workflows/java.yml b/.github/workflows/java.yml
index 5b65c99f..54d1a207 100644
--- a/.github/workflows/java.yml
+++ b/.github/workflows/java.yml
@@ -179,7 +179,7 @@ jobs:
if: ${{ !cancelled() }}
run: >
python laya-java/scripts/check_test_results.py
- laya-java/laya-java/build/test-results/test --min-tests 963 --allow-aborted
+ laya-java/laya-java/build/test-results/test --min-tests 965 --allow-aborted
# The samples are single-file source programs, deliberately NOT a Gradle module: a
# `samples` subproject would inherit the root build's MavenPublication and
@@ -356,7 +356,7 @@ jobs:
if: ${{ !cancelled() }}
run: >
python laya-java/scripts/check_test_results.py
- laya-java/laya-java/build/test-results/test --min-tests 964 --allow-aborted
+ laya-java/laya-java/build/test-results/test --min-tests 966 --allow-aborted
--require-passed TestJvmVersionTest
- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
if: ${{ failure() }}
@@ -460,8 +460,10 @@ jobs:
checkpoints: multilingual
graph-model: multilingual
families: --only predict.json
- tests: --tests com.convaiinnovations.laya.PredictParityTest
- min-tests: 9
+ tests: >-
+ --tests com.convaiinnovations.laya.PredictParityTest
+ --tests com.convaiinnovations.laya.OneOptionGraphTest
+ min-tests: 12
timeout: 75
# Weight-bearing, and a SEPARATE cell rather than a second family in the one above.
# Three reasons, in order of how much they cost:
diff --git a/.github/workflows/release-java.yml b/.github/workflows/release-java.yml
index c9e6629f..7f2fb55c 100644
--- a/.github/workflows/release-java.yml
+++ b/.github/workflows/release-java.yml
@@ -83,7 +83,7 @@ jobs:
- name: The suite was not vacuous
run: >
python3 scripts/check_test_results.py laya-java/build/test-results/test
- --min-tests 963 --allow-aborted
+ --min-tests 965 --allow-aborted
- name: Build the jars
run: ./gradlew --no-daemon build -x test
diff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/sequence/Collator.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/sequence/Collator.java
index f42e9cb1..e6aa6ac0 100644
--- a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/sequence/Collator.java
+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/sequence/Collator.java
@@ -11,6 +11,13 @@ import java.util.List;
*/
public final class Collator {
+ /**
+ * The fewest option slots the graph accepts. The exported head takes {@code topk(2)} of the
+ * option probabilities, so a run whose questions all have one option is padded with a masked
+ * second slot: the graph scores it -1e4 and decode reads each row only to its own option count.
+ */
+ static final int MIN_MARKER_SLOTS = 2;
+
/** The five inputs the graph takes, flattened row-major. */
public record Batch(long[][] inputIds, long[][] attentionMask, long[][] markerPos,
boolean[][] markerMask, long[] qtype, int rows, int length, int markers) {
@@ -35,7 +42,7 @@ public final class Collator {
}
int rows = items.size();
int length = 0;
- int markerWidth = 0;
+ int markerWidth = MIN_MARKER_SLOTS;
for (Item item : items) {
length = Math.max(length, item.ids().length);
markerWidth = Math.max(markerWidth, item.markers().length);
diff --git a/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/OneOptionGraphTest.java b/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/OneOptionGraphTest.java
new file mode 100644
index 00000000..1c452ce5
--- /dev/null
+++ b/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/OneOptionGraphTest.java
@@ -0,0 +1,92 @@
+package com.convaiinnovations.laya;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Assumptions;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+/**
+ * One-option questions on a real graph. The exported head takes {@code topk(2)} over the option
+ * slots, so a run whose questions all have one option failed in ONNX Runtime until the collator
+ * padded a masked second slot.
+ */
+final class OneOptionGraphTest {
+
+ private static final String STATE = "I was charged twice for the same order, please refund me.";
+
+ private static Agent shared;
+
+ @AfterAll
+ static void closeAgent() {
+ if (shared != null) {
+ shared.close();
+ shared = null;
+ }
+ }
+
+ @Test
+ @DisplayName("a one-option choice is answered with that option at probability 1")
+ void oneOptionChoice() throws IOException {
+ Prediction got = agent().predict(STATE, Map.of("topic",
+ Question.choiceOf("What is this about?", Map.entry("billing", "a billing issue"))));
+ Answer.Choice choice = assertInstanceOf(Answer.Choice.class, got.answers().get("topic"));
+ assertEquals("billing", choice.choice());
+ assertEquals(Map.of("billing", 1.0), choice.probabilities());
+ // What laya's eager Agent and ONNXAgent give for this question on this checkpoint.
+ assertEquals(1.0, choice.actProbability(), 1e-4);
+ }
+
+ @Test
+ @DisplayName("a one-level score is answered at level 0")
+ void oneLevelScore() throws IOException {
+ Prediction got = agent().predict(STATE, Map.of("urgency",
+ Question.score("How urgent is this?", List.of("urgent"))));
+ Answer.Score score = assertInstanceOf(Answer.Score.class, got.answers().get("urgency"));
+ assertEquals(0.0, score.score());
+ assertEquals(1, score.probabilities().size());
+ assertEquals(1.0, score.probabilities().values().iterator().next());
+ }
+
+ @Test
+ @DisplayName("one-option questions batched together are each answered")
+ void oneOptionBatch() throws IOException {
+ Map<String, Question> questions = new LinkedHashMap<>();
+ questions.put("topic",
+ Question.choiceOf("What is this about?", Map.entry("billing", "a billing issue")));
+ questions.put("urgency", Question.score("How urgent is this?", List.of("urgent")));
+ List<Prediction> got = agent().predictBatch(List.of(STATE, "Where is my parcel?"),
+ questions, null, 0, false);
+ assertEquals(2, got.size());
+ for (Prediction prediction : got) {
+ assertEquals("billing",
+ ((Answer.Choice) prediction.answers().get("topic")).choice());
+ assertEquals(0.0, ((Answer.Score) prediction.answers().get("urgency")).score());
+ }
+ }
+
+ private static synchronized Agent agent() throws IOException {
+ if (shared == null) {
+ Path model = Fixtures.checkpoint("multilingual");
+ Assumptions.assumeTrue(model != null, Fixtures.missingCheckpoint("multilingual"));
+ String graph = System.getenv(Fixtures.GRAPH_ENV);
+ Assumptions.assumeTrue(graph != null && !graph.isBlank(),
+ "set " + Fixtures.GRAPH_ENV + " to an exported laya.onnx");
+ Path graphPath = Paths.get(graph);
+ Path directory = Files.isDirectory(graphPath) ? graphPath : graphPath.getParent();
+ Assumptions.assumeTrue(directory != null && Files.isDirectory(directory),
+ Fixtures.GRAPH_ENV + " does not name a graph: " + graph);
+ shared = Agent.open(model, directory);
+ }
+ return shared;
+ }
+}
diff --git a/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/sequence/CollatorTest.java b/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/sequence/CollatorTest.java
index e48f510f..2029cfc5 100644
--- a/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/sequence/CollatorTest.java
+++ b/laya-java/laya-java/src/test/java/com/convaiinnovations/laya/sequence/CollatorTest.java
@@ -44,7 +44,30 @@ final class CollatorTest {
// "no marker". A port that inferred liveness from the position would mask out option 0.
Collator.Batch batch = Collator.collate(List.of(
new Collator.Item(new int[] {7}, new int[] {0}, 1)), 0);
- assertArrayEquals(new long[] {0}, batch.markerPos()[0]);
- assertArrayEquals(new boolean[] {true}, batch.markerMask()[0]);
+ assertArrayEquals(new long[] {0, 0}, batch.markerPos()[0]);
+ assertArrayEquals(new boolean[] {true, false}, batch.markerMask()[0]);
+ }
+
+ @Test
+ @DisplayName("a run of one-option questions still sends two marker slots, the second masked")
+ void oneOptionRunPadsToTwoSlots() {
+ // The exported head takes topk(2) over the option slots; one slot fails in ONNX Runtime.
+ Collator.Batch batch = Collator.collate(List.of(
+ new Collator.Item(new int[] {5, 6}, new int[] {1}, 1),
+ new Collator.Item(new int[] {7}, new int[] {0}, 2)), 0);
+ assertEquals(2, batch.markers());
+ assertArrayEquals(new long[] {1, 0}, batch.markerPos()[0]);
+ assertArrayEquals(new boolean[] {true, false}, batch.markerMask()[0]);
+ assertArrayEquals(new long[] {0, 0}, batch.markerPos()[1]);
+ assertArrayEquals(new boolean[] {true, false}, batch.markerMask()[1]);
+ }
+
+ @Test
+ @DisplayName("a run already two slots or wider is not padded further")
+ void twoOrMoreSlotsUnchanged() {
+ Collator.Batch batch = Collator.collate(List.of(
+ new Collator.Item(new int[] {5}, new int[] {0, 0}, 0)), 0);
+ assertEquals(2, batch.markers());
+ assertArrayEquals(new boolean[] {true, true}, batch.markerMask()[0]);
}
}