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.

perf: keep torch out of import laya (lazy torch-backed exports) (#108)

Emiliano G.O.43cde5a1a420
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index aebbab68..1a370b30 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -65,6 +65,7 @@ jobs:
python tests/test_head_checkpointing.py
python tests/test_packaging.py
python tests/test_tokenizer_cache.py
+ python tests/test_lazy_import.py
python tests/test_lang_guess.py
python tests/test_calibration_persistence.py
python tests/test_context_manager.py
diff --git a/laya/__init__.py b/laya/__init__.py
index 287d4ef3..5285efdd 100644
--- a/laya/__init__.py
+++ b/laya/__init__.py
@@ -1,15 +1,5 @@
"""Laya: Fast, non-autoregressive System 1 decision engine with calibrated probabilities."""
-from .agent import Agent, RLAgent, load
-from .common import (
- QTYPES,
- QTYPE_NAMES,
- confidence_from_probs,
- ece_score,
- proper_reward,
- render_options,
- td_lambda_targets,
-)
from .email import clean_email_body, email_state
from .lang import analyse as detect_language
from .lang import detect_script, is_english
@@ -21,16 +11,50 @@ from .presets import (
triage_questions,
)
from .router import DEFAULT_MODELS, RouteDecision, Router
-from .shortlist import embed_fn_from_agent, predict_shortlist, shortlist_choice
-from .integrations import (
- LayaEvaluator,
- LayaGuardrail,
- LayaGuardrailError,
- LayaRouter,
- LayaTriage,
-)
__version__ = "0.3.7"
+
+# Routing, language detection and email cleaning are pure Python. The torch-backed names are
+# resolved lazily so that `import laya` -- and therefore `from laya import Router` or
+# `from laya.lang import detect_script` -- does not pay torch's import time and memory.
+_LAZY_ATTRS = {
+ "Agent": (".agent", "Agent"),
+ "RLAgent": (".agent", "RLAgent"),
+ "load": (".agent", "load"),
+ "proper_reward": (".common", "proper_reward"),
+ "td_lambda_targets": (".common", "td_lambda_targets"),
+ "ece_score": (".common", "ece_score"),
+ "confidence_from_probs": (".common", "confidence_from_probs"),
+ "render_options": (".common", "render_options"),
+ "QTYPES": (".common", "QTYPES"),
+ "QTYPE_NAMES": (".common", "QTYPE_NAMES"),
+ "shortlist_choice": (".shortlist", "shortlist_choice"),
+ "predict_shortlist": (".shortlist", "predict_shortlist"),
+ "embed_fn_from_agent": (".shortlist", "embed_fn_from_agent"),
+ "LayaRouter": (".integrations", "LayaRouter"),
+ "LayaGuardrail": (".integrations", "LayaGuardrail"),
+ "LayaGuardrailError": (".integrations", "LayaGuardrailError"),
+ "LayaTriage": (".integrations", "LayaTriage"),
+ "LayaEvaluator": (".integrations", "LayaEvaluator"),
+}
+
+
+def __getattr__(name):
+ try:
+ module_name, attr = _LAZY_ATTRS[name]
+ except KeyError:
+ raise AttributeError("module %r has no attribute %r" % (__name__, name)) from None
+ import importlib
+
+ value = getattr(importlib.import_module(module_name, __name__), attr)
+ globals()[name] = value # cache: __getattr__ runs at most once per name
+ return value
+
+
+def __dir__():
+ return sorted(list(globals()) + list(_LAZY_ATTRS))
+
+
__all__ = [
"Agent",
"RLAgent",
diff --git a/tests/test_lazy_import.py b/tests/test_lazy_import.py
new file mode 100644
index 00000000..06896846
--- /dev/null
+++ b/tests/test_lazy_import.py
@@ -0,0 +1,76 @@
+"""`import laya` must not import torch; torch-backed names stay lazy.
+
+The pure-Python surface (routing, language detection, email cleaning) has to be usable
+without torch installed. The torch-backed names must still resolve when it is available.
+"""
+import os
+import subprocess
+import sys
+
+ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+sys.path.insert(0, ROOT)
+
+PASS, FAIL = [], []
+
+
+def check(name, got, want):
+ if got == want:
+ PASS.append(name)
+ else:
+ FAIL.append("%s: got %r, want %r" % (name, got, want))
+
+
+# ------------------------------------------------------------------ torch blocked
+PROBE = r'''
+import sys
+sys.path.insert(0, %r)
+class Blocker:
+ def find_spec(self, name, path=None, target=None):
+ if name == "torch" or name.startswith("torch."):
+ raise ImportError("torch blocked")
+ return None
+sys.meta_path.insert(0, Blocker())
+
+import laya
+assert "torch" not in sys.modules, "import laya pulled in torch"
+# `laya.serve` (#31) defers fastapi/uvicorn/torch into its functions, so importing it must
+# stay cheap too -- otherwise the lazy package init buys nothing for the server entry point.
+import laya.serve # noqa: F401
+assert "torch" not in sys.modules, "import laya.serve pulled in torch"
+script = laya.detect_script("The customer was charged twice")
+try:
+ laya.Agent
+ agent = "resolved-without-torch"
+except ImportError:
+ agent = "blocked"
+try:
+ laya.shortlist_choice
+ sh = "resolved-without-torch"
+except ImportError:
+ sh = "blocked"
+print(script, agent, sh)
+''' % ROOT
+
+proc = subprocess.run([sys.executable, "-c", PROBE], capture_output=True, text=True)
+check("no-torch/import laya succeeds", proc.returncode, 0)
+out = proc.stdout.strip().split()
+check("no-torch/detect_script works", out[0] if out else None, "latin")
+check("no-torch/Agent stays lazy", out[1] if len(out) > 1 else None, "blocked")
+check("no-torch/shortlist stays lazy", out[2] if len(out) > 2 else None, "blocked")
+
+
+# ------------------------------------------------------------------ torch available
+import laya # noqa: E402
+
+missing = [n for n in laya.__all__ if not hasattr(laya, n)]
+check("all __all__ names resolve with torch", missing, [])
+check("from-import of a lazy name", laya.Agent.__name__, "Agent")
+check("__dir__ lists lazy names", "load" in dir(laya), True)
+
+
+print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))
+for f in FAIL:
+ print(" FAIL", f)
+if not FAIL:
+ print("all lazy-import tests passed")
+sys.exit(1 if FAIL else 0)