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(shortlist): refuse an agent that has no tok or model.encoder

Aashish4985bee8c826
diff --git a/laya/shortlist.py b/laya/shortlist.py
index 24614d12..2d984aa6 100644
--- a/laya/shortlist.py
+++ b/laya/shortlist.py
@@ -248,10 +248,16 @@ def embed_fn_from_agent(
if isinstance(batch_size, bool) or not isinstance(batch_size, int) or batch_size < 1:
raise ValueError("batch_size must be a positive integer, got %r" % (batch_size,))
- import torch
+ # The two attributes the docstring names are the only ones here that were never checked: an
+ # agent that is not a loaded checkpoint answered with the interpreter's own AttributeError, the
+ # one leak in this function, while `_call_predict` below refuses the same mistake for the agent
+ # it does use. Checked before `import torch` so a wrong argument does not pay for the import.
+ tok = getattr(agent, "tok", None)
+ encoder = getattr(getattr(agent, "model", None), "encoder", None)
+ if tok is None or encoder is None:
+ raise TypeError("agent must provide tok and model.encoder")
- tok = agent.tok
- encoder = agent.model.encoder
+ import torch
def embed_fn(texts: Sequence[str]) -> np.ndarray:
rows = ["" if text is None else str(text) for text in texts]
diff --git a/tests/test_shortlist.py b/tests/test_shortlist.py
index d06f2c3f..db64588a 100644
--- a/tests/test_shortlist.py
+++ b/tests/test_shortlist.py
@@ -496,6 +496,39 @@ check_raises("encoder/bad max_length", lambda: embed_fn_from_agent(tiny, max_len
check_raises("encoder/bad batch_size", lambda: embed_fn_from_agent(tiny, batch_size=True))
+# The two attributes the docstring names -- `agent.tok`, `agent.model.encoder` -- were read with no
+# check, so an object that is not a loaded checkpoint answered with the interpreter's own
+# AttributeError instead of naming what the helper needs. `_call_predict` refuses the same mistake
+# for the agent *it* uses, which is the sibling this now matches.
+class NoAttrs:
+ pass
+
+
+class TokOnly:
+ def __init__(self):
+ self.tok = TinyTok()
+
+
+class ModelWithoutEncoder:
+ def __init__(self):
+ self.tok = TinyTok()
+ self.model = type("M", (), {})()
+
+
+for _bad_agent in (NoAttrs(), TokOnly(), ModelWithoutEncoder()):
+ check_raises("encoder/agent/%s refused" % type(_bad_agent).__name__,
+ lambda _a=_bad_agent: embed_fn_from_agent(_a), TypeError)
+
+try:
+ embed_fn_from_agent(NoAttrs())
+ FAIL.append("encoder/agent refusal message: no TypeError")
+except TypeError as _exc:
+ check_true("encoder/agent refusal names both attributes",
+ str(_exc) == "agent must provide tok and model.encoder", repr(str(_exc)))
+except AttributeError as _exc:
+ FAIL.append("encoder/agent refusal message: leaked AttributeError (%s)" % _exc)
+
+
# ---------------------------------------------------------------- device changes, simulated entirely on CPU
class DeviceRecordingTensor:
def __init__(self, tensor, requests):