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: reject targets longer than their own marker count in mixed batches (#311) (#312)

Aashish Mahato5877415e3c57
diff --git a/laya-ts/src/common.ts b/laya-ts/src/common.ts
index 014a0212..f0b176a7 100644
--- a/laya-ts/src/common.ts
+++ b/laya-ts/src/common.ts
@@ -147,8 +147,17 @@ export function collateItems(batch: CollateItem[][], padId: number): CollatedBat
});
const out: CollatedBatch = { inputIds, attentionMask, markerPos, markerMask, qtype, label, meta };
if (hasTarget) {
- out.target = items.map((it) => {
+ out.target = items.map((it, i) => {
const t = Array.isArray(it.target) ? (it.target as number[]) : [];
+ const k = it.markers.length;
+ if (t.length > k) {
+ // Same guard as py collate_items: the limit is this item's own marker count, not the
+ // batch-wide K — a wider sibling row must not legitimise extra entries (#311).
+ throw new Error(
+ `collateItems: item ${i} has ${t.length} target entries but only ${k} marker positions; ` +
+ "a target needs one entry per option",
+ );
+ }
return [...t, ...Array(K - t.length).fill(0)];
});
}
diff --git a/laya-ts/tests/parity-sequence.test.ts b/laya-ts/tests/parity-sequence.test.ts
index 6ef24927..bbe12cd2 100644
--- a/laya-ts/tests/parity-sequence.test.ts
+++ b/laya-ts/tests/parity-sequence.test.ts
@@ -77,4 +77,17 @@ describe("common parity", () => {
expect(collateItems([], 0)).toBeNull();
expect(collateItems([[]], 0)).toBeNull();
});
+ it("collateItems rejects a target longer than its own marker count, even in mixed batches (#311)", () => {
+ // the first row's target fits the batch-wide K=4 but exceeds its own 2 markers
+ const mixed = [[
+ { ids: [1], markers: [0, 1], qtype: 1, target: [0.2, 0.3, 0.5] },
+ { ids: [2], markers: [0, 1, 2, 3], qtype: 1, target: [0.25, 0.25, 0.25, 0.25] },
+ ]];
+ expect(() => collateItems(mixed as any, 0)).toThrow(/one entry per option/);
+ const overK = [[{ ids: [1], markers: [0], qtype: 1, target: [0.0, 1.0] }]];
+ expect(() => collateItems(overK as any, 0)).toThrow(/one entry per option/);
+ // a shorter target is padded with zeros, not rejected
+ const short = collateItems([[{ ids: [1], markers: [0, 1, 2], qtype: 1, target: [1.0] }]] as any, 0)!;
+ expect(short.target).toEqual([[1.0, 0.0, 0.0]]);
+ });
});
diff --git a/laya/common.py b/laya/common.py
index 45aa99e5..648f4d98 100644
--- a/laya/common.py
+++ b/laya/common.py
@@ -327,13 +327,15 @@ def collate_items(batch, pad_id: int):
mpos[i, :k] = torch.tensor(it["markers"])
mmask[i, :k] = True
if has_target and "target" in it:
- if len(it["target"]) > kmax:
+ if len(it["target"]) > k:
# Otherwise this lands as "The expanded size of the tensor (k) must match the
# existing size (kmax)" from inside the assignment, which says nothing about the
- # actual mistake: a target with more entries than the item has options.
+ # actual mistake: a target with more entries than the item has options. The limit
+ # is this item's own marker count, not the batch-wide kmax: in a mixed-width
+ # batch a longer sibling row must not legitimise extra entries (#311).
raise ValueError(
"collate_items: item %d has %d target entries but only %d marker positions; "
- "a target needs one entry per option" % (i, len(it["target"]), kmax))
+ "a target needs one entry per option" % (i, len(it["target"]), k))
target[i, : len(it["target"])] = torch.tensor(it["target"], dtype=torch.float32)
res = {
diff --git a/tests/test_training.py b/tests/test_training.py
index ab1394c5..4075e87f 100644
--- a/tests/test_training.py
+++ b/tests/test_training.py
@@ -257,6 +257,24 @@ except ValueError as e:
except Exception as e: # noqa: BLE001
FAIL.append("collate/oversized target raised %s instead of ValueError: %s" % (type(e).__name__, e))
+# #311: in a mixed-width batch the limit is each item's own marker count, not the batch-wide max
+try:
+ collate_items([[
+ {"ids": [1], "markers": [0, 1], "qtype": 1, "target": [0.2, 0.3, 0.5]},
+ {"ids": [2], "markers": [0, 1, 2, 3], "qtype": 1, "target": [0.25, 0.25, 0.25, 0.25]},
+ ]], pad_id=0)
+ FAIL.append("collate/mixed-width oversized target raises ValueError (nothing raised)")
+except ValueError as e:
+ check_true("collate/mixed-width oversized target raises a clear ValueError",
+ "one entry per option" in str(e), str(e)[:90])
+except Exception as e: # noqa: BLE001
+ FAIL.append("collate/mixed-width oversized target raised %s instead of ValueError: %s"
+ % (type(e).__name__, e))
+
+# a target shorter than the item's own markers is still padded with zeros, not rejected
+b3 = collate_items([[{"ids": [1], "markers": [0, 1, 2], "qtype": 1, "target": [1.0]}]], pad_id=0)
+check("collate/short target padded with zeros", b3["target"][0].tolist(), [1.0, 0.0, 0.0])
+
# =============================================================== build_sequence
class _Tok: