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(docker): bake one snapshot per repository so both checkpoints resolve
gqchenf69bf111ce17
diff --git a/docker/prefetch_modelscope.py b/docker/prefetch_modelscope.pyindex 5f42a475..95ca680c 100644--- a/docker/prefetch_modelscope.py+++ b/docker/prefetch_modelscope.py@@ -34,6 +34,7 @@ import urllib.errorimport urllib.parseimport urllib.requestfrom pathlib import Path+from typing import DictFILES_API = "https://modelscope.cn/api/v1/models/{repo}/repo/files"FILE_API = "https://modelscope.cn/api/v1/models/{repo}/repo"@@ -164,20 +165,15 @@ def list_repo(repo: str, revision: str, subfolder: str):def snapshot_commit(tip: str, entries: list) -> str:- """The commit to key the baked snapshot by: the weights' own, else the revision's tip.+ """The commit the baked snapshot is keyed by: the tip of the requested revision.- The weights' commit is the one a caller would pin to get *these* bytes back, so it is the- more useful key. A mixed-commit fetch still lands as one snapshot directory, because the- offline path never compares the file list against a commit -- but that is a warning, not a- failure: a maintenance commit to a README must not break a build.+ A mirror repository can hold files from several uploads, so the commit a checkpoint's own+ weights sit on is not a repository-wide key -- and one repository has to bake *one* snapshot+ directory, because `refs/<revision>` names a single commit that every checkpoint in that+ repository resolves through. The tip is that key. It is printed at build time, and it is what+ a caller pins with `revision=` or `LAYA_REVISION` to get exactly these bytes."""- commits = {entry["revision"] for entry in entries if entry["revision"]}- weights = [entry["revision"] for entry in entries- if entry["rel"] == "model.safetensors" and entry["revision"]]- if len(commits) > 1:- print(" note: files span commits %s; keying the snapshot by the weights' commit"- % sorted(commits), file=sys.stderr)- return (weights[0] if weights else "") or tip+ return tip or max((entry["revision"] for entry in entries if entry["revision"]), default="")def download(repo: str, revision: str, entry: dict, dest: Path) -> None:@@ -241,17 +237,32 @@ def seed(cache: Path, repo: str, commit: str, revision: str, staging: Path) -> N(refs / name).write_text(commit, encoding="utf-8")-def prefetch(spec: str, revision: str, cache: Path) -> None:- repo, subfolder = split_spec(spec)- tip, entries = list_repo(repo, revision, subfolder)+def prefetch(specs: list, revision: str, cache: Path) -> None:+ """Bake every requested checkpoint of one repository into a single snapshot.++ One repository, one snapshot: `refs/<revision>` names one commit, and both+ `Agent("ns/repo")` and `Agent("ns/repo", subfolder="...")` resolve the revision the same way,+ so the root checkpoint and each subfolder have to land in the same directory.+ """+ repo = specs[0][0]+ entries, tip, staged = [], "", {}+ for _, subfolder in specs:+ if subfolder in staged:+ continue+ folder_tip, items = list_repo(repo, revision, subfolder)+ if not items:+ print(" %s: no checkpoint files, skipped" % (subfolder or "."))+ continue+ staged[subfolder] = True+ tip = tip or folder_tip+ entries += [entry for entry in items if entry["path"] not in+ {other["path"] for other in entries}]if not entries:- raise SystemExit("laya: %s%s holds no checkpoint files at revision %r"- % (repo, "/" + subfolder if subfolder else "", revision))+ raise SystemExit("laya: %s holds no checkpoint files at revision %r" % (repo, revision))commit = snapshot_commit(tip, entries)if not commit:- raise SystemExit("laya: %s%s names no commit at revision %r, so there is nothing to key "- "the baked snapshot by" % (repo, "/" + subfolder if subfolder else "",- revision))+ raise SystemExit("laya: %s names no commit at revision %r, so there is nothing to key the "+ "baked snapshot by" % (repo, revision))repo_dir = cache / repo_folder(repo)staging = repo_dir / "snapshots" / (".staging-" + str(os.getpid()))try:@@ -278,13 +289,18 @@ def main() -> int:parser.add_argument("--cache-dir", default=None,help="hub cache directory; defaults to HF_HUB_CACHE or $HF_HOME/hub")args = parser.parse_args()- specs = expand_model(args.model)+ specs = [split_spec(spec) for spec in expand_model(args.model)]cache = cache_root(args.cache_dir)cache.mkdir(parents=True, exist_ok=True)- for spec in specs:- print("modelscope/%s at revision %s" % (spec, args.revision))- prefetch(spec, args.revision, cache)- print("baked %d checkpoint(s) into %s" % (len(specs), cache))+ grouped: Dict[str, list] = {}+ for repo, subfolder in specs:+ grouped.setdefault(repo, []).append(subfolder)+ for repo, subfolders in grouped.items():+ print("modelscope/%s at revision %s (%s)"+ % (repo, args.revision, ", ".join(sub or "root" for sub in subfolders)))+ prefetch([(repo, sub) for sub in subfolders], args.revision, cache)+ print("baked %d checkpoint(s) in %d repository(ies) into %s"+ % (len(specs), len(grouped), cache))return 0diff --git a/docs/docker.md b/docs/docker.mdindex 1c9dbb31..69cc4636 100644--- a/docs/docker.md+++ b/docs/docker.md@@ -215,21 +215,32 @@ to build every checkpoint and fail on the first one that was not baked; set`LAYA_MODELS` to the list you baked when you bake more, and `MODELSCOPE_MODEL=all`when a deployment really does serve the family.-Beyond the four types the argument also takes `repo[:subfolder]` specs, comma- or+Several types can be named at once -- `MODELSCOPE_MODEL="english multilingual"`+bakes both, about 1.5 GB -- which is what a serving image usually wants: the+`Router` chooses between the English and the multilingual checkpoint on its own,+and one it was not given answers `500 inference failed` with+`does not contain 'rl_agent_config.json'` in the log. Checkpoints that share a+repository always bake into one snapshot, because a cached revision resolves to a+single directory; the root checkpoint and each subfolder are both in it.++Beyond the types the argument also takes `repo[:subfolder]` specs, comma- orspace-separated, which is how the mirror's standalone repositories([laya](https://modelscope.cn/models/convaiinnovations/laya),[laya-multilingual](https://modelscope.cn/models/convaiinnovations/laya-multilingual),[laya-typed-decisions](https://modelscope.cn/models/convaiinnovations/laya-typed-decisions))or a fine-tuned checkpoint is baked. A standalone repository is what-`Agent("convaiinnovations/laya-multilingual")` loads directly; the `Router`'s default-is the bundled path, so a type is normally what a serving image wants.+`Agent("convaiinnovations/laya-multilingual")` loads directly; the `Router`'s+default is the bundled path, so a type is normally what a serving image wants.Two details about pins and provenance. The build prints the commit the snapshot is-keyed by -- pass that SHA as `revision=` or `LAYA_REVISION` to pin a load to exactly-what was baked. Hub-side pins do not describe a mirror snapshot: `reviewed` names-Hugging Face commits, and the SHA-256 digest map is keyed to Hub artifact hashes, so-neither applies here. The build already refuses a download that does not match the-mirror's own reported size and digest, which is the equivalent guarantee for this+keyed by, which is the tip of the revision that was baked -- pass that SHA as+`revision=` or `LAYA_REVISION` to pin a load to exactly what was baked. A mirror+repository can hold files from several uploads, so that tip is the one+repository-wide key there is. Hub-side pins do not describe a mirror snapshot:+`reviewed` names Hugging Face commits, and the SHA-256 digest map is keyed to Hub+artifact hashes, so neither applies here. The build already refuses a download+that does not match the mirror's own reported size and digest, which is the+equivalent guarantee for thispath. And the weights come from whichever mirror account the argument names, which isits own supply-chain decision for the deployment to make.diff --git a/tests/test_modelscope_prefetch.py b/tests/test_modelscope_prefetch.pyindex 143b5f49..63f03e5b 100644--- a/tests/test_modelscope_prefetch.py+++ b/tests/test_modelscope_prefetch.py@@ -19,6 +19,7 @@ import http.clientimport importlib.utilimport jsonimport os+import sysimport tempfileimport unittestimport urllib.parse@@ -62,6 +63,14 @@ def tree(path):# way the mirror does: the checkpoint's tokenizer and encoder are pages of their own, and a sibling# checkpoint directory is a page this bake must not walk into.PAGES = {+ "": [+ entry("model.safetensors", WEIGHTS_COMMIT),+ entry("rl_agent_config.json", WEIGHTS_COMMIT),+ entry("README.md", TIP_COMMIT),+ tree("encoder"),+ tree("multilingual"),+ ],+ "encoder": [entry("encoder/config.json", WEIGHTS_COMMIT)],"multilingual": [entry("multilingual/model.safetensors", WEIGHTS_COMMIT),entry("multilingual/rl_agent_config.json", WEIGHTS_COMMIT),@@ -73,6 +82,9 @@ PAGES = {],"multilingual/tokenizer": [entry("multilingual/tokenizer/tokenizer.json", WEIGHTS_COMMIT)],"multilingual/encoder": [entry("multilingual/encoder/config.json", WEIGHTS_COMMIT)],+ "typed-decisions": [tree("typed-decisions/tokenizer"), tree("typed-decisions/encoder")],+ "typed-decisions/tokenizer": [],+ "typed-decisions/encoder": [],}@@ -205,6 +217,28 @@ class TypeTests(unittest.TestCase):self.assertEqual(prefetch.expand_model("models/custom:typed-decisions models/other"),["models/custom:typed-decisions", "models/other"])+ def test_main_drives_the_whole_argument(self):+ """`--model` to baked snapshot, through the CLI: the shape main() passes to prefetch()."""+ with tempfile.TemporaryDirectory() as tmp:+ cache = Path(tmp) / "hub"++ def urlopen(target, **_):+ request = target if isinstance(target, urllib.request.Request) else urllib.request.Request(target)+ return FakeResponse(request)++ argv = ["prefetch_modelscope.py", "--model", "english multilingual",+ "--revision", "master", "--cache-dir", str(cache)]+ with patch.object(sys, "argv", argv), patch.object(prefetch.urllib.request, "urlopen", urlopen):+ self.assertEqual(prefetch.main(), 0)+ # Both types of one repository land in one merged snapshot, keyed by the revision tip.+ snapshots = sorted(p.name for p in+ (cache / "models--convaiinnovations--laya" / "snapshots").iterdir())+ self.assertEqual(snapshots, [TIP_COMMIT])+ snapshot = cache / "models--convaiinnovations--laya" / "snapshots" / TIP_COMMIT+ for rel in ("rl_agent_config.json", "model.safetensors", "encoder/config.json",+ "multilingual/rl_agent_config.json", "multilingual/model.safetensors"):+ self.assertTrue(snapshot.joinpath(rel).is_file(), rel)+def test_a_bad_value_is_refused_with_the_valid_names(self):for value in ("", "french", "multilingual,french", "ml,spanish"):with self.assertRaises(SystemExit) as caught:@@ -254,6 +288,16 @@ class BakeTests(unittest.TestCase):repo, _ = prefetch.split_spec(self.SPEC)return self.cache / ("models--" + repo.replace("/", "--"))+ def specs(self):+ """The subfolders under test, in the (repo, subfolder) shape prefetch() takes.++ One repository bakes one snapshot, so the default is the single checkpoint this class+ names and a test can add more of the same repository with `bake_subfolders`.+ """+ repo, spec_sub = prefetch.split_spec(self.SPEC)+ subfolders = getattr(self, "bake_subfolders", None) or [spec_sub]+ return [(repo, sub) for sub in subfolders]+def bake(self, response=FakeResponse):def urlopen(target, **_):# `list_repo` passes a URL, `download` passes a Request; the mirror answers both.@@ -262,7 +306,7 @@ class BakeTests(unittest.TestCase):return response(request)with patch.object(prefetch.urllib.request, "urlopen", urlopen):- prefetch.prefetch(self.SPEC, "master", self.cache)+ prefetch.prefetch(self.specs(), "master", self.cache)return self.folder()def published(self, repo_dir):@@ -274,7 +318,7 @@ class BakeTests(unittest.TestCase):def test_seeded_layout_matches_what_the_hub_writes(self):repo_dir = self.bake()- snapshot = repo_dir / "snapshots" / WEIGHTS_COMMIT+ snapshot = repo_dir / "snapshots" / TIP_COMMITself.assertTrue(snapshot.is_dir(), "no snapshot for the weights' commit")for rel in ("multilingual/rl_agent_config.json", "multilingual/model.safetensors","multilingual/tokenizer/tokenizer.json", "multilingual/encoder/config.json"):@@ -283,17 +327,45 @@ class BakeTests(unittest.TestCase):# drops `multilingual/` resolves and then fails with "Subfolder not found".self.assertEqual(snapshot.joinpath("multilingual", "model.safetensors").read_bytes(),CONTENTS["multilingual/model.safetensors"])+ self.assertEqual(snapshot.joinpath("multilingual", "rl_agent_config.json").read_bytes(),+ CONTENTS["multilingual/rl_agent_config.json"])# The runtime asks for `main`, the mirror publishes `master`: both names resolve.- self.assertEqual((repo_dir / "refs" / "main").read_text(), WEIGHTS_COMMIT)- self.assertEqual((repo_dir / "refs" / "master").read_text(), WEIGHTS_COMMIT)+ # Keyed by the revision's tip: one repository bakes one snapshot, because refs/<revision>+ # names a single commit and every checkpoint in that repository resolves through it.+ self.assertEqual((repo_dir / "refs" / "main").read_text(), TIP_COMMIT)+ self.assertEqual((repo_dir / "refs" / "master").read_text(), TIP_COMMIT)# Sibling files never enter the snapshot.self.assertFalse(snapshot.joinpath("multilingual", "README.md").exists())self.assertFalse(snapshot.joinpath("README.md").exists())- self.assertEqual(self.published(repo_dir), [WEIGHTS_COMMIT])+ self.assertEqual(self.published(repo_dir), [TIP_COMMIT])++ def test_one_repository_bakes_one_merged_snapshot(self):+ """A bundle bake carries both checkpoints, or the second one is unreachable.++ `refs/main` names a single commit, so baking english and multilingual separately leaves+ only the last one resolvable -- which is exactly what the English request hit.+ """+ self.SPEC = "convaiinnovations/laya"+ self.bake_subfolders = ["", "multilingual"]+ repo_dir = self.bake()+ snapshot = repo_dir / "snapshots" / TIP_COMMIT+ # English sits at the repository root, multilingual under its own subfolder: both loaded+ # by the same `Agent` revision argument, so both have to be in the same directory.+ self.assertTrue(snapshot.joinpath("rl_agent_config.json").is_file(), "english")+ self.assertTrue(snapshot.joinpath("model.safetensors").is_file(), "english weights")+ self.assertTrue(snapshot.joinpath("multilingual", "rl_agent_config.json").is_file(),+ "multilingual")+ self.assertTrue(snapshot.joinpath("multilingual", "model.safetensors").is_file(),+ "multilingual weights")+ self.assertEqual(self.published(repo_dir), [TIP_COMMIT])+ self.assertEqual((repo_dir / "refs" / "main").read_text(), TIP_COMMIT)+ # A third checkpoint in the same repository is walked past, not fetched.+ self.assertFalse(snapshot.joinpath("typed-decisions").exists())def test_a_standalone_repo_lands_at_the_snapshot_root(self):"""No subfolder: the checkpoint's own files sit at the snapshot root, as on the Hub."""self.SPEC = "convaiinnovations/laya-multilingual"+ self.bake_subfolders = [""]pages = {"": [entry("model.safetensors", WEIGHTS_COMMIT),@@ -307,7 +379,7 @@ class BakeTests(unittest.TestCase):}with patch.dict(PAGES, pages, clear=True):repo_dir = self.bake()- snapshot = repo_dir / "snapshots" / WEIGHTS_COMMIT+ snapshot = repo_dir / "snapshots" / TIP_COMMITself.assertTrue(snapshot.joinpath("model.safetensors").is_file())self.assertTrue(snapshot.joinpath("rl_agent_config.json").is_file())self.assertTrue(snapshot.joinpath("tokenizer", "tokenizer.json").is_file())@@ -329,34 +401,36 @@ class BakeTests(unittest.TestCase):"multilingual/tokenizer/*", "multilingual/encoder/*"],cache_dir=str(self.cache), local_files_only=True,)- expected = self.cache / "models--convaiinnovations--laya" / "snapshots" / WEIGHTS_COMMIT+ expected = self.cache / "models--convaiinnovations--laya" / "snapshots" / TIP_COMMITself.assertEqual(Path(os.path.realpath(resolved)), Path(os.path.realpath(str(expected))))def test_the_commit_the_bake_keyed_resolves_as_a_pin(self):- """`revision=` reaching the weights' own commit must hit the same snapshot."""+ """`revision=` reaching the revision's tip must hit the same snapshot."""from huggingface_hub import snapshot_downloadself.bake()- resolved = snapshot_download("convaiinnovations/laya", revision=WEIGHTS_COMMIT,+ resolved = snapshot_download("convaiinnovations/laya", revision=TIP_COMMIT,cache_dir=str(self.cache), local_files_only=True)- self.assertEqual(Path(os.path.realpath(resolved)).name, WEIGHTS_COMMIT)+ self.assertEqual(Path(os.path.realpath(resolved)).name, TIP_COMMIT)def test_a_commit_the_bake_did_not_key_misses(self):- """A commit that was not baked must miss, not silently serve the weights' snapshot."""+ """An upload commit the bake did not key must miss, not serve a snapshot it is not in."""from huggingface_hub import snapshot_downloadfrom huggingface_hub.errors import LocalEntryNotFoundErrorself.bake()with self.assertRaises(LocalEntryNotFoundError):- snapshot_download("convaiinnovations/laya", revision=TIP_COMMIT,+ snapshot_download("convaiinnovations/laya", revision=WEIGHTS_COMMIT,cache_dir=str(self.cache), local_files_only=True)def test_a_dropped_connection_is_retried_and_resumed(self):"""A mid-body drop must resume byte-aligned, not restart or duplicate the file."""repo_dir = self.bake(Dropping)- snapshot = repo_dir / "snapshots" / WEIGHTS_COMMIT+ snapshot = repo_dir / "snapshots" / TIP_COMMITself.assertEqual(snapshot.joinpath("multilingual", "model.safetensors").read_bytes(),CONTENTS["multilingual/model.safetensors"])+ self.assertEqual(snapshot.joinpath("multilingual", "rl_agent_config.json").read_bytes(),+ CONTENTS["multilingual/rl_agent_config.json"])def test_truncated_download_refuses_to_publish(self):with patch.object(prefetch, "RETRIES", 2), patch.object(prefetch, "BACKOFF", 0):@@ -377,7 +451,7 @@ class BakeTests(unittest.TestCase):def test_rebaking_the_same_commit_replaces_the_snapshot(self):self.bake()repo_dir = self.bake()- self.assertEqual(self.published(repo_dir), [WEIGHTS_COMMIT])+ self.assertEqual(self.published(repo_dir), [TIP_COMMIT])if __name__ == "__main__":