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(common): truncate_left kept the whole state when no room was left (#112)

Ahmed Abdullah5647adbc04ff
diff --git a/laya/common.py b/laya/common.py
index 9003e08f..ab2a303d 100644
--- a/laya/common.py
+++ b/laya/common.py
@@ -82,7 +82,8 @@ def build_sequence(
ids.append(tok.sep_token_id)
room = max(0, max_len - len(ids) - 1)
st = tok(serialize_state(state).replace(mask_tok, " "), add_special_tokens=False)["input_ids"]
- st = st[-room:] if truncate_left else st[:room]
+ # not st[-room:]: with no room left, st[-0:] is the whole state rather than none of it
+ st = st[max(0, len(st) - room):] if truncate_left else st[:room]
ids = ids + st + [tok.sep_token_id]
return ids[:max_len], [m for m in markers if m < max_len]
diff --git a/tests/test_criteria.py b/tests/test_criteria.py
index d9418fdf..583de24b 100644
--- a/tests/test_criteria.py
+++ b/tests/test_criteria.py
@@ -278,6 +278,29 @@ check_true("good/noul with criteria is a probability",
check("good/usage has no output tokens", out["usage"]["output_tokens"], 0)
check_true("good/usage counted input tokens", out["usage"]["input_tokens"] > 0, str(out["usage"]))
+# --------------------------------------------------------------- build_sequence left truncation
+# With no room left for the state, `st[-0:]` kept all of it: the closing [SEP] was replaced by the
+# *first* state token, i.e. the wrong end of the state and an unterminated sequence.
+from laya.common import build_sequence # noqa: E402
+
+
+class _SeqTok:
+ mask_token, mask_token_id, cls_token_id, sep_token_id = "[MASK]", 1, 2, 3
+
+ def __init__(self):
+ self.vocab = {}
+
+ def __call__(self, text, add_special_tokens=False):
+ return {"input_ids": [self.vocab.setdefault(w, 100 + len(self.vocab)) for w in text.split()]}
+
+
+_tok, _q = _SeqTok(), {"t": "noul", "ins": "Is it urgent?", "crit": None}
+_full = len(build_sequence(_tok, "", _q, 10 ** 6)[0]) # prompt + closing [SEP], no state
+for room, kept in [(0, []), (2, ["two", "three"]), (10, ["one", "two", "three"])]:
+ ids = build_sequence(_tok, "one two three", _q, _full + room, truncate_left=True)[0]
+ check("truncate_left/room=%d keeps the tail" % room, ids[_full - 1:],
+ [_tok.vocab[w] for w in kept] + [_tok.sep_token_id])
+
print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))
for f in FAIL: