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(train): skip questions whose options run past max_len instead of crashing the batch
GuilhermeF21af8be7663b
diff --git a/laya/train.py b/laya/train.pyindex 5e861f85..cbb062fc 100644--- a/laya/train.py+++ b/laya/train.py@@ -155,13 +155,16 @@ def encode_state(tok, state: Any, max_len: int) -> List[int]:def make_item(tok, q: Dict[str, Any], target: Sequence[float], state_ids: List[int],- head_max_len: int) -> Tuple[Optional[Dict[str, Any]], Optional[str]]:+ head_max_len: int, max_len: Optional[int] = None) -> Tuple[Optional[Dict[str, Any]], Optional[str]]:"""`(item, None)`, or `(None, reason)` when the question cannot be trained on as written.`q` is an internal question (`to_internal`). The reasons are `target_mismatch` (the target- does not have one entry per option) and `options_collapsed` (the head budget left two options- with the same token span, #538). Training on either would teach the model to tell apart- options it cannot see as different.+ does not have one entry per option), `options_collapsed` (the head budget left two options+ with the same token span, #538) and, when `max_len` is given, `options_beyond_max_len` (the+ head is longer than `max_len`, so `build_sequence` drops the markers of the last options --+ the case `Agent` refuses at inference with "only N of its M option markers fit"). Training on+ any of them would teach the model to tell apart options it cannot see as different, or crash+ the batch on a target longer than its markers."""k = len(render_options(q))if len(target) != k:@@ -169,6 +172,8 @@ def make_item(tok, q: Dict[str, Any], target: Sequence[float], state_ids: List[i_ids, markers, stats = build_head(tok, q, head_max_len)if len(markers) != k or stats["options_distinct"] < stats["options"]:return None, "options_collapsed"+ if max_len is not None and markers and markers[-1] >= max_len:+ return None, "options_beyond_max_len"return {"q": q, "state_ids": state_ids, "target": [float(v) for v in target],"qtype": QTYPES[q["t"]], "k": k}, None@@ -179,7 +184,7 @@ def items_from_rows(tok, rows: Iterable[Dict[str, Any]], max_len: int,`skipped` counts, by reason, questions that were labelled but could not become an item:`invalid_question` and `invalid_target` (a `ValueError` from `to_internal` or- `target_from_gold`), plus the two reasons `make_item` gives. Questions with no gold entry+ `target_from_gold`), plus the reasons `make_item` gives. Questions with no gold entryare not counted -- the row simply does not label them."""items, skipped = [], {}@@ -200,7 +205,7 @@ def items_from_rows(tok, rows: Iterable[Dict[str, Any]], max_len: int,except (ValueError, TypeError, KeyError):reason = "invalid_target"if reason is None:- item, reason = make_item(tok, q, target, state_ids, head_max_len)+ item, reason = make_item(tok, q, target, state_ids, head_max_len, max_len)if reason is None:items.append(item)else:diff --git a/tests/test_train.py b/tests/test_train.pyindex f7ca5558..6c118cda 100644--- a/tests/test_train.py+++ b/tests/test_train.py@@ -187,6 +187,28 @@ class DataTests(unittest.TestCase):(None, "options_collapsed"))self.assertIsNotNone(make_item(self.tok, q, [0.5, 0.5], state_ids, head_max_len=64)[0])+ def test_options_past_max_len_are_skipped_like_inference_refuses_them(self):+ # With a head longer than max_len, build_sequence drops the last options' markers and the+ # batch would crash on a target longer than its markers. Agent refuses the same question.+ from laya.common import build_head++ question = {"type": "choice", "instructions": "which ?",+ "criteria": ["one", "two", "three", "four", "five", "six", "seven"]}+ q = to_internal("q", question)+ _ids, markers, _stats = build_head(self.tok, q, 64)+ state_ids = encode_state(self.tok, "app", 64)+ target = [1.0 / 7] * 7+ self.assertEqual(make_item(self.tok, q, target, state_ids, 64, max_len=markers[-1]),+ (None, "options_beyond_max_len"))+ self.assertIsNotNone(make_item(self.tok, q, target, state_ids, 64, max_len=markers[-1] + 1)[0])+ self.assertIsNotNone(make_item(self.tok, q, target, state_ids, 64)[0]) # no max_len: not checked+ row = {"state": "app", "questions": {"q": question},+ "gold": {"q": {"probabilities": {"one": 1.0}}}}+ self.assertEqual(items_from_rows(self.tok, [row], markers[-1], 64), ([], {"options_beyond_max_len": 1}))+ # the same budget really does cut a marker in the sequence training would build+ _seq, kept = build_sequence(self.tok, "app", q, markers[-1], 64)+ self.assertEqual(len(kept), 6)+def test_target_length_must_match_options(self):state_ids = encode_state(self.tok, "app", 64)self.assertEqual(make_item(self.tok, to_internal("q", DEPARTMENT), [0.5, 0.5], state_ids, 40),