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(agent): resolve checkpoint names and aliases in load()

Yi-111-aaff18e97431d
diff --git a/laya/agent.py b/laya/agent.py
index c98e6859..e6d11f46 100644
--- a/laya/agent.py
+++ b/laya/agent.py
@@ -1661,10 +1661,29 @@ def load(model_id_or_path: str = "convaiinnovations/laya", device: Optional[str]
laya.load("convaiinnovations/laya", fast=True) # TileLang GPU fast path
laya.load("convaiinnovations/laya", compile=True) # torch.compile the model
+ `model_id_or_path` also accepts a checkpoint name or alias -- the same ones
+ `Router` resolves, so both entry points read one table:
+
+ laya.load("typed-decisions")
+ laya.load("ml") # multilingual
+
+ Anything else (a Hub repo id, a local directory) is passed to `Agent` unchanged.
+
`revision`/`expected_sha256` pin and verify the downloaded artifacts; see `Agent`.
`hooks` / `on_predict_start` / `on_predict_end` observe or shape every prediction; see
`laya.hooks`. `calibration` is the same optional JSON path accepted by `Agent`.
"""
+ # A registry name or alias resolves to the same (repo, subfolder) the Router would
+ # pick, instead of being handed to the Hub as a repo id. A caller who spelled out a
+ # subfolder is loading that subfolder of whatever repo they named, so the name is only
+ # resolved when it is the whole argument -- and an existing directory always wins over a
+ # name, so a local checkpoint that happens to be called `english` still loads from disk.
+ if subfolder is None and not os.path.exists(model_id_or_path):
+ from .router import resolve_model_spec
+
+ spec = resolve_model_spec(model_id_or_path)
+ if spec is not None:
+ model_id_or_path, subfolder = spec
return Agent(model_id_or_path, device=device, token=token, subfolder=subfolder, fast=fast,
compile=compile,
revision=revision, expected_sha256=expected_sha256,
diff --git a/laya/router.py b/laya/router.py
index 36a0ec6d..fd401129 100644
--- a/laya/router.py
+++ b/laya/router.py
@@ -34,7 +34,7 @@ import os
import threading
import time
from collections.abc import Sequence as SequenceABC
-from typing import Any, Dict, List, Optional, Sequence, Union
+from typing import Any, Dict, List, Optional, Sequence, Tuple, Union
from .confidence import check_min_confidence, flag_low_confidence
from .hooks import (
@@ -118,6 +118,22 @@ def normalise_name(name: str) -> str:
return key
+def resolve_model_spec(name: str) -> Optional[Tuple[str, Optional[str]]]:
+ """Registry spec for a checkpoint name or alias, or None when it is not one.
+
+ The non-raising sibling of :func:`normalise_name`, for callers that also accept
+ things the registry knows nothing about -- a Hub repo id, a local directory, an
+ ONNX export. Those pass through untouched; a name or alias the registry does know
+ resolves to its ``(repo, subfolder)`` pair, so ``load("typed-decisions")`` and
+ ``Router(model="typed-decisions")`` name the same checkpoint from one table.
+ """
+ try:
+ key = normalise_name(name)
+ except ValueError:
+ return None
+ return tuple(_split(DEFAULT_MODELS[key]))
+
+
def match_typed_decisions_workflow(questions: Dict[str, Any]) -> Optional[str]:
"""Name of the typed-decisions workflow whose question ids these are, else None.
diff --git a/tests/test_download.py b/tests/test_download.py
index aa294d1b..3e163d47 100644
--- a/tests/test_download.py
+++ b/tests/test_download.py
@@ -122,6 +122,27 @@ class DownloadTests(unittest.TestCase):
with self.subTest(subfolder=subfolder):
self.check_download("test/bundled-models", subfolder)
+ def test_a_checkpoint_name_or_alias_reaches_the_same_download_as_a_repo_id(self):
+ # #780: `Router` resolved `english` / `typed-decisions` / `ml`, but `load()` forwarded the
+ # same word to the Hub as a repo id, so `laya.load("typed-decisions")` 404'd. One registry
+ # now serves both, and this asserts the transport the name produces is the one the spelled
+ # -out `repo_id` + `subfolder` pair already produces.
+ for name, repo_id, subfolder in [("english", "convaiinnovations/laya", None),
+ ("laya", "convaiinnovations/laya", None),
+ ("typed-decisions", "convaiinnovations/laya", "typed-decisions"),
+ ("typed", "convaiinnovations/laya", "typed-decisions"),
+ ("ml", "convaiinnovations/laya", "multilingual"),
+ ("ML", "convaiinnovations/laya", "multilingual")]:
+ with self.subTest(name=name):
+ with patch("huggingface_hub.snapshot_download",
+ return_value=str(self.repo)) as download:
+ agent = load(name, device="cpu", token="test-token")
+ self.assertEqual(download.call_args.args[0], repo_id)
+ patterns = download.call_args.kwargs["allow_patterns"]
+ prefix = subfolder + "/" if subfolder else ""
+ self.assertIn(prefix + "model.safetensors", patterns)
+ self.assertEqual(agent.predict("hello", self.questions), self.expected)
+
def test_local_paths_do_not_download(self):
with patch("huggingface_hub.snapshot_download") as download:
for subfolder in (None, "multilingual", "variants/english"):
diff --git a/tests/test_router.py b/tests/test_router.py
index d243aafa..580103ac 100644
--- a/tests/test_router.py
+++ b/tests/test_router.py
@@ -15,9 +15,11 @@ from laya.router import ( # noqa: E402
STANDALONE_MODELS,
_english_from_code,
_repo_str,
+ _split,
Router,
match_typed_decisions_workflow,
normalise_name,
+ resolve_model_spec,
)
PASS, FAIL = [], []
@@ -315,6 +317,25 @@ except ValueError:
PASS.append("alias/unknown raises")
+# --------------------------------------------------------------------- registry spec resolution (#780)
+# `load()` resolves a name or alias through this table instead of forwarding it to the Hub as a
+# repo id, so both entry points read one registry. Anything the registry does not know -- a repo
+# id, a local path, an ONNX export -- resolves to None and is left alone.
+for name, want in [("english", ("convaiinnovations/laya", None)),
+ ("laya", ("convaiinnovations/laya", None)),
+ ("typed-decisions", ("convaiinnovations/laya", "typed-decisions")),
+ ("typed", ("convaiinnovations/laya", "typed-decisions")),
+ ("ml", ("convaiinnovations/laya", "multilingual")),
+ ("MULTI", ("convaiinnovations/laya", "multilingual")),
+ (" typed-decisions ", ("convaiinnovations/laya", "typed-decisions"))]:
+ check("spec/" + name, resolve_model_spec(name), want)
+check("spec/agrees with normalise_name",
+ resolve_model_spec("decisions"), tuple(_split(DEFAULT_MODELS[normalise_name("decisions")])))
+for unknown in ("convaiinnovations/laya", "test/custom-model", "/tmp/checkpoint", "./local",
+ "nope", "", "convaiinnovations/laya-typed-decisions"):
+ check("spec/not a name: " + (unknown or "<empty>"), resolve_model_spec(unknown), None)
+
+
# --------------------------------------------------------------------- routing decisions
r = Router()
Q_GENERIC = {"dept": {"type": "choice", "instructions": "Which team?",