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(onnx): let ONNXAgent authenticate its Hub download the way Agent does
Aashish7121b33b7947
diff --git a/laya/onnx_agent.py b/laya/onnx_agent.pyindex 0f4a19f5..ed643163 100644--- a/laya/onnx_agent.py+++ b/laya/onnx_agent.py@@ -43,6 +43,7 @@ class ONNXAgent(HookRegistry):self,model_id_or_path: str,onnx_path: str = "laya.onnx",+ token: Optional[str] = None,subfolder: Optional[str] = None,revision: Optional[str] = None,expected_sha256: Optional[Dict[str, str]] = None,@@ -60,6 +61,9 @@ class ONNXAgent(HookRegistry):model_id_or_path: HuggingFace Hub ID or local path to the original PyTorch checkpoint(used to load the tokenizer and config).onnx_path: Path to the exported .onnx file.+ token: Optional HuggingFace token for a private or gated checkpoint; falls back+ to `$HF_TOKEN`, exactly as `Agent` does. Only the tokenizer and config are+ fetched -- the graph itself is the local `onnx_path`.subfolder: Optional subfolder if downloading from a repo bundle.revision: Optional Hub revision (commit SHA/branch/tag). When omitted,huggingface_hub's normal default and existing offline cache are used.@@ -102,6 +106,7 @@ class ONNXAgent(HookRegistry):revision = resolve_revision(model_id_or_path, revision)prefix = f"{subfolder}/" if subfolder else ""kw = {+ "token": token or os.environ.get("HF_TOKEN") or None,"allow_patterns": [prefix + name for name in ("rl_agent_config.json", "tokenizer/*", "encoder/*",)],diff --git a/tests/test_download.py b/tests/test_download.pyindex 9fd8f1f7..f01c3617 100644--- a/tests/test_download.py+++ b/tests/test_download.py@@ -2,6 +2,7 @@Run: python tests/test_download.py"""+import inspectimport jsonimport osfrom pathlib import Path@@ -22,8 +23,15 @@ from tokenizers import Tokenizer # noqa: E402from tokenizers.models import WordLevel # noqa: E402from transformers import BertConfig, BertModel, PreTrainedTokenizerFast # noqa: E402-from laya import load # noqa: E402+from laya import Agent, load # noqa: E402from laya.common import DecisionModel # noqa: E402+from laya.onnx_agent import ONNXAgent # noqa: E402+++class _NoRuntime:+ """Stand-in for the onnxruntime module. `ONNXAgent.__init__` binds the name before it+ touches the Hub, and the download asserted on below happens well before a session is+ built, so nothing here is ever called."""class DownloadTests(unittest.TestCase):@@ -138,6 +146,61 @@ class DownloadTests(unittest.TestCase):torch.testing.assert_close(value, weights[name], rtol=0, atol=0)self.assertEqual(agent.predict("hello", self.questions), self.expected)+ def test_onnx_agent_accepts_every_hub_option_the_agent_does(self):+ # Both runtimes download the same checkpoint from the same place, so an option that+ # selects *which* checkpoint, or *how* to authenticate for it, has to exist on both.+ # Read off the signatures so the next Hub-side option added to Agent has to be+ # copied across rather than silently diverging again.+ # fast/compile -- the TileLang path and torch.compile, neither of which exists+ # inside onnxruntime.+ # device -- ONNXAgent picks an execution provider from what onnxruntime+ # reports and takes no override; a different asymmetry, with its+ # own fix.+ not_for_onnxruntime = {"fast", "compile", "device"}+ agent_side = (set(inspect.signature(Agent.__init__).parameters)+ - {"self", "model_id_or_path"} - not_for_onnxruntime)+ onnx_side = set(inspect.signature(ONNXAgent.__init__).parameters) - {"self"}+ missing = agent_side - onnx_side+ self.assertEqual(missing, set(),+ "ONNXAgent cannot set: %s" % ", ".join(sorted(missing)))++ def test_onnx_agent_forwards_the_token_to_the_download(self):+ # `token` is only useful if it reaches snapshot_download. Drive the real constructor+ # with the transport replaced and read the call it makes: the graph file is absent+ # on purpose, so the load stops right after the download it is meant to authenticate.+ # onnxruntime is imported at the top of __init__ and never used before that stop, so+ # an empty stand-in keeps this a no-extra-required check.+ with tempfile.TemporaryDirectory() as snapshot:+ (Path(snapshot) / "rl_agent_config.json").write_text(json.dumps({+ "encoder": "unused/offline", "head_layers": 0, "act_costs": {"act": 0},+ "max_len": 64, "head_max_len": 32,+ }))+ with patch.dict(sys.modules, {"onnxruntime": _NoRuntime}), \+ patch("huggingface_hub.snapshot_download", return_value=snapshot) as download:+ with self.assertRaises(FileNotFoundError):+ ONNXAgent("test/private-model", token="test-token",+ onnx_path=str(self.repo / "absent.onnx"))+ self.assertEqual(download.call_args.kwargs["token"], "test-token")+ # the allow-list is unchanged: a token must not widen what gets fetched+ self.assertNotIn("model.safetensors", download.call_args.kwargs["allow_patterns"])++ def test_onnx_agent_token_defaults_and_reads_the_environment(self):+ with tempfile.TemporaryDirectory() as snapshot:+ (Path(snapshot) / "rl_agent_config.json").write_text(json.dumps({+ "encoder": "unused/offline", "head_layers": 0, "act_costs": {"act": 0},+ "max_len": 64, "head_max_len": 32,+ }))+ with patch.dict(sys.modules, {"onnxruntime": _NoRuntime}), \+ patch("huggingface_hub.snapshot_download", return_value=snapshot) as download:+ with self.assertRaises(FileNotFoundError):+ ONNXAgent("test/private-model", onnx_path=str(self.repo / "absent.onnx"))+ self.assertIsNone(download.call_args.kwargs["token"])++ with patch.dict(os.environ, {"HF_TOKEN": "env-token"}):+ with self.assertRaises(FileNotFoundError):+ ONNXAgent("test/private-model", onnx_path=str(self.repo / "absent.onnx"))+ self.assertEqual(download.call_args.kwargs["token"], "env-token")+if __name__ == "__main__":torch.set_num_threads(1)diff --git a/tests/test_hooks_api.py b/tests/test_hooks_api.pyindex fa750e30..91aced75 100644--- a/tests/test_hooks_api.py+++ b/tests/test_hooks_api.py@@ -69,6 +69,9 @@ for label, fn in (("Agent.__init__", Agent.__init__), ("load", load),check_param("Router.__init__", Router.__init__, "lang_guess", None)check_param("Router.__init__", Router.__init__, "revisions", None)+# ONNXAgent downloads the same Hub artifacts as Agent, so it authenticates the same way+check_param("ONNXAgent.__init__", ONNXAgent.__init__, "token", None)+# --------------------------------------------------------------- predict surfacesfor label, fn in (("Agent.predict_batch", Agent.predict_batch),("Agent.system_one", Agent.system_one),