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(router,serve): refuse an empty, over-long or non-string checkpoint source pair

Aashish5a9d0782af69
diff --git a/laya/router.py b/laya/router.py
index f538d589..41b9cd46 100644
--- a/laya/router.py
+++ b/laya/router.py
@@ -891,6 +891,18 @@ class Router(HookRegistry):
if source is not None and not isinstance(source, (str, tuple, list)):
raise TypeError("checkpoint source for %r must be a repo id, a local path or a (repo, subfolder) "
"pair, got %s" % (name, type(source).__name__))
+ # The sequence half of that sentence was never opened. `_split` pads with one `None`, so an
+ # empty pair unpacks to one value and `registered`, `route(model=...)` and `load()` all fail
+ # later with `ValueError: not enough values to unpack (expected 2, got 1)` -- after the
+ # registration reported success -- while a third element is silently truncated and a number
+ # part is stringified into a repo id like `acme/5`. Refuse the shape here, where the name is
+ # known, so nothing is stored and no later call has to guess what the pair meant.
+ if isinstance(source, (tuple, list)) and (
+ not 1 <= len(source) <= 2
+ or not all(part is None or isinstance(part, str) for part in source)
+ or not any(isinstance(part, str) for part in source)):
+ raise TypeError("checkpoint source for %r must be a repo id, a local path or a (repo, subfolder) "
+ "pair of strings, got %r" % (name, source))
if isinstance(source, str) and source.startswith("~"):
source = os.path.expanduser(source) # a local path, never a Hub repo id
# After every check that can refuse the registration, and before the write. A filter
diff --git a/laya/serve.py b/laya/serve.py
index a853ab1d..c96bb0a2 100644
--- a/laya/serve.py
+++ b/laya/serve.py
@@ -733,6 +733,15 @@ def _extra_models_option() -> Dict[str, Any]:
out = {}
for name, source in data.items():
if isinstance(source, list):
+ # `[]`, a third element and a number are all lists, so they reached the Router and became
+ # `ValueError: not enough values to unpack` on the first request that routed to them (or a
+ # repo id like `acme/5`), long after the operator was told the value parsed. This option
+ # is the one place that still knows the name and can refuse the startup.
+ if (not 1 <= len(source) <= 2
+ or not all(part is None or isinstance(part, str) for part in source)
+ or not any(isinstance(part, str) for part in source)):
+ raise SystemExit("invalid LAYA_EXTRA_MODELS: source for %r must be a repo "
+ "id, a local path, or a [repo, subfolder] pair; got %r" % (name, source))
out[name] = tuple(source) # JSON has no tuple: [repo, subfolder]
elif isinstance(source, str):
out[name] = source
diff --git a/tests/test_router.py b/tests/test_router.py
index 2365792c..a0bcf706 100644
--- a/tests/test_router.py
+++ b/tests/test_router.py
@@ -1854,6 +1854,24 @@ try:
except TypeError:
check("register/a source must be a path, repo or pair", True, True)
+# The pair half of that sentence was unchecked, and an empty one is the worst case: `register`
+# reported success, stored the spec, and `registered` / `route(model=...)` / `load()` then raised
+# `ValueError: not enough values to unpack (expected 2, got 1)` from `_split` on every later call.
+for bad_source, why in (([], "empty pair"), (["a", "b", "c"], "three parts"),
+ ((None, None), "a pair naming no repo"),
+ (("acme/laya-tone", 5), "a number among the parts")):
+ _r = Router()
+ try:
+ _r.register("tone", bad_source)
+ check("register/refuses %s" % why, False, True)
+ except TypeError as _e:
+ check("register/refuses %s" % why, "'tone'" in str(_e), True)
+ check("register/%s stores nothing" % why, "tone" in _r.registered, False)
+for good in (("acme/laya-tone", None), ["org/repo", "folder"], ("only-repo",)):
+ check("register/still accepts %r" % (good,),
+ Router(models={"tone": good}).registered["tone"]["source"],
+ "/".join([p for p in (good if isinstance(good, list) else list(good)) if p is not None]))
+
# Every name-taking option accepts a registered name, the way it accepts a built-in one.
r3 = Router(models={"papers": "/tmp/laya-papers"}, revisions={"papers": "abc123"}, default="papers",
sha256_digests={"papers": {"model.safetensors": "a" * 64}})
diff --git a/tests/test_serve.py b/tests/test_serve.py
index 637f0cce..28df5497 100644
--- a/tests/test_serve.py
+++ b/tests/test_serve.py
@@ -4215,6 +4215,18 @@ def test_extra_models_option_refuses_a_malformed_value(monkeypatch):
assert "invalid LAYA_EXTRA_MODELS" in message
assert "'my-ckpt'" in message, message
+ # A list was believed on its type alone, so `[]` reached the Router and became
+ # `ValueError: not enough values to unpack` on the first request routed to it,
+ # and `["a", "b", "c"]` or `["a", 5]` became the wrong repo id in silence. Same
+ # refusal, same naming, at startup.
+ for bad in ([], ["a", "b", "c"], ["a", 5], [None, None]):
+ monkeypatch.setenv("LAYA_EXTRA_MODELS", json.dumps({"my-ckpt": bad}))
+ with pytest.raises(SystemExit) as raised:
+ _extra_models_option()
+ message = str(raised.value)
+ assert "invalid LAYA_EXTRA_MODELS" in message, (bad, message)
+ assert "'my-ckpt'" in message, (bad, message)
+
def test_extra_models_reach_the_router_the_server_builds(monkeypatch):
# Registration happens before preload, so the names resolve on the Router