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
feat(revisions): let LAYA_REVISION pick the checkpoint commit
Aashish7a96bec5a149
diff --git a/laya/revisions.py b/laya/revisions.pyindex 91014716..c2bc19a8 100644--- a/laya/revisions.py+++ b/laya/revisions.py@@ -4,6 +4,17 @@ Runtime loaders keep the Hub default revision unless the caller supplies one. Thcompatibility with existing offline caches, including ``HF_HUB_OFFLINE=1`` deployments. Thereviewed commit SHAs below are available for callers that opt in, and every loader accepts anoptional SHA-256 map to verify artifact integrity before weights reach the runtime.++Both halves are reachable the same way. `expected_sha256` falls back to+``LAYA_SHA256_DIGESTS``, and `resolve_revision` falls back to ``LAYA_REVISION``: a commit, branch+or tag applied to every checkpoint load, or the word ``reviewed`` to use `PINNED_REVISIONS` for+whichever repository is being loaded. A `revision` argument still outranks the environment, and an+unset or empty ``LAYA_REVISION`` leaves today's behaviour exactly as it was, so a container pins+its checkpoints with one environment line and no code.++The env fallback is for checkpoints, i.e. the loads in this module's callers (`Agent`,+`ONNXAgent`). `common.build_model`'s training-time base-encoder load is deliberately excluded:+that is a different repository, and it has no reviewed SHA here to resolve to."""import hashlibimport json@@ -19,14 +30,36 @@ PINNED_REVISIONS: Dict[str, str] = {"convaiinnovations/laya-typed-decisions": "1a793eb568e6718f15941d08f85432581df534e3",}+#: Opt-in `LAYA_REVISION` value meaning "the reviewed SHA of the repository being loaded".+#: The alternative is a commit/branch/tag, which is applied to every checkpoint load instead.+REVIEWED = "reviewed"+def resolve_revision(model_id_or_path: str, revision: Optional[str] = None) -> Optional[str]:"""Pick the revision to download.- An explicit `revision` is returned unchanged. Otherwise None is returned so+ An explicit `revision` is returned unchanged. Otherwise ``LAYA_REVISION``, stripped; empty or+ unset means "not asked for". The variable holds either a commit SHA/branch/tag, applied to+ every checkpoint load the way `Router(revision=)` applies one, or the word ``reviewed``, which+ looks `model_id_or_path` up in `PINNED_REVISIONS`.++ ``reviewed`` for a repository the table has no entry for raises rather than loading it+ unpinned: a pin that quietly resolves to nothing is the failure mode this control exists to+ prevent, and `verify_digests` refuses on the same principle. Everything else returns None sohuggingface_hub applies its normal default, preserving existing online/offline caches."""- return revision or None+ value = (revision or os.environ.get("LAYA_REVISION", "")).strip()+ if not value:+ return None+ if value == REVIEWED:+ pinned = PINNED_REVISIONS.get(model_id_or_path)+ if pinned is None:+ raise ValueError(+ "laya: LAYA_REVISION=reviewed, but %r has no reviewed SHA in "+ "PINNED_REVISIONS; set LAYA_REVISION to a commit or unset it"+ % model_id_or_path)+ return pinned+ return valuedef snapshot_revision(path: str) -> Optional[str]:diff --git a/tests/test_download.py b/tests/test_download.pyindex 9fd8f1f7..9080c5b9 100644--- a/tests/test_download.py+++ b/tests/test_download.py@@ -1,5 +1,9 @@"""Checkpoint download regression tests; tiny local weights, no network required.+Also pins the `LAYA_REVISION` lever of `laya/revisions.py`: which revision reaches `snapshot_download`,+what the Agent and the Router report back, and the two loads that must not see it (a local+directory, and `common.build_model`'s training-time base encoder).+Run: python tests/test_download.py"""import json@@ -24,6 +28,7 @@ from transformers import BertConfig, BertModel, PreTrainedTokenizerFast # noqa:from laya import load # noqa: E402from laya.common import DecisionModel # noqa: E402+from laya.revisions import PINNED_REVISIONS # noqa: E402class DownloadTests(unittest.TestCase):@@ -138,6 +143,130 @@ class DownloadTests(unittest.TestCase):torch.testing.assert_close(value, weights[name], rtol=0, atol=0)self.assertEqual(agent.predict("hello", self.questions), self.expected)+ # ------------------------------------------------------------------ LAYA_REVISION+ #+ # `PINNED_REVISIONS` published the reviewed commit of each published checkpoint and nothing in+ # `laya/` read it, while `LAYA_SHA256_DIGESTS` -- the other half of the same opt-in pair, in+ # the same module -- was already reachable from the environment. These replace only the Hub+ # transport, so the revision that reaches `snapshot_download`, the one the Agent reports back,+ # and the one a Router publishes in `loaded_revisions` (what `/health` serves) are all the real+ # thing. They live here rather than in `tests/test_revision_pinning.py` because this file's+ # subject is already "what the download call was given", and because this suite runs in CI+ # (`ci.yml:107`, `release.yml:47`) while that one is in no workflow.++ def hub_load(self, repo, env=None, **load_kwargs):+ """`load(repo)` with the Hub transport replaced. Returns (kwargs sent, agent, error)."""+ with patch.dict(os.environ), patch("huggingface_hub.snapshot_download",+ return_value=str(self.repo)) as download:+ if env is None:+ os.environ.pop("LAYA_REVISION", None)+ else:+ os.environ["LAYA_REVISION"] = env+ try:+ agent = load(repo, device="cpu", **load_kwargs)+ except Exception as error: # the cases below assert on it+ return download, None, error+ return download, agent, None++ def sent_revision(self, download):+ return (download.call_args.kwargs if download.call_args else {}).get("revision")++ def test_no_pin_asked_for_keeps_the_hub_default(self):+ download, agent, error = self.hub_load("convaiinnovations/laya")+ self.assertIsNone(error)+ self.assertNotIn("revision", download.call_args.kwargs)+ self.assertIsNone(agent.revision)+ self.assertEqual(agent.predict("hello", self.questions), self.expected)++ def test_environment_revision_reaches_the_download(self):+ download, agent, error = self.hub_load("convaiinnovations/laya", env="ab" * 20)+ self.assertIsNone(error)+ self.assertEqual(self.sent_revision(download), "ab" * 20)+ self.assertEqual(agent.revision, "ab" * 20)++ def test_reviewed_resolves_each_repositorys_own_pin(self):+ self.assertGreater(len(set(PINNED_REVISIONS.values())), 1,+ "the table has one pin per repository, so this sweep is meaningful")+ for repo, sha in PINNED_REVISIONS.items():+ with self.subTest(repo=repo):+ download, agent, error = self.hub_load(repo, env="reviewed")+ self.assertIsNone(error)+ self.assertEqual(self.sent_revision(download), sha)+ self.assertEqual(agent.revision, sha)++ def test_reviewed_refuses_a_repository_with_no_pin(self):+ download, agent, error = self.hub_load("acme/custom-checkpoint", env="reviewed")+ self.assertIsInstance(error, ValueError)+ self.assertIn("acme/custom-checkpoint", str(error))+ self.assertIn("LAYA_REVISION", str(error))+ self.assertIsNone(agent)+ download.assert_not_called()++ def test_an_explicit_revision_outranks_the_environment(self):+ download, agent, error = self.hub_load("convaiinnovations/laya", env="reviewed",+ revision="cd" * 20)+ self.assertIsNone(error)+ self.assertEqual(self.sent_revision(download), "cd" * 20)+ self.assertNotEqual(agent.revision, PINNED_REVISIONS["convaiinnovations/laya"])++ def test_an_empty_or_blank_environment_value_is_not_a_pin(self):+ for env in ("", " ", "\t"):+ with self.subTest(repr=repr(env)):+ download, agent, error = self.hub_load("convaiinnovations/laya", env=env)+ self.assertIsNone(error)+ self.assertNotIn("revision", download.call_args.kwargs)+ self.assertIsNone(agent.revision)++ def test_a_local_directory_never_consults_the_environment(self):+ download, agent, error = self.hub_load(str(self.repo), env="reviewed")+ self.assertIsNone(error)+ download.assert_not_called()+ self.assertIsNone(agent.revision)+ self.assertEqual(agent.predict("hello", self.questions), self.expected)++ def test_router_publishes_the_pinned_commit(self):+ """`/health`'s `revisions` block is `Router.loaded_revisions`, so the pin shows up there."""+ from laya import Router++ bundle = "convaiinnovations/laya"+ with patch.dict(os.environ), patch("huggingface_hub.snapshot_download",+ return_value=str(self.repo)):+ os.environ.pop("LAYA_REVISION", None)+ unpinned = Router()+ unpinned.load("english")+ self.assertEqual(unpinned.loaded_revisions, {"english": None})++ os.environ["LAYA_REVISION"] = "reviewed"+ pinned = Router()+ pinned.load("english")+ self.assertEqual(pinned.loaded_revisions, {"english": PINNED_REVISIONS[bundle]})++ def test_the_onnx_loader_resolves_through_the_same_function(self):+ """`ONNXAgent` must not grow its own copy of the rule, or the env pin covers one loader."""+ from laya import onnx_agent, revisions++ self.assertIs(onnx_agent.resolve_revision, revisions.resolve_revision)++ def test_the_base_encoder_load_ignores_the_environment(self):+ """`common.build_model`'s training-time load is a different repository, with no reviewed+ SHA to resolve to, so `LAYA_REVISION` deliberately does not reach it."""+ import laya.common as common++ seen = {}++ def from_pretrained(name, **kwargs):+ seen.update(kwargs)+ seen["name"] = name+ raise RuntimeError("stop after the call")++ cfg = {"encoder": "unused/base-encoder", "head_layers": 1, "act_costs": {"a": 0}}+ with patch.dict(os.environ, {"LAYA_REVISION": "reviewed"}), \+ patch("transformers.AutoModel.from_pretrained", side_effect=from_pretrained):+ with self.assertRaises(RuntimeError):+ common.build_model(cfg, pretrained=True)+ self.assertEqual(seen["name"], "unused/base-encoder")+ self.assertNotIn("revision", seen)+if __name__ == "__main__":torch.set_num_threads(1)