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.

feat: Router.attach to share an already-built checkpoint (v0.3.3)

Nandakishor3152a82b974b
diff --git a/README.md b/README.md
index 6847dc80..f598b17c 100644
--- a/README.md
+++ b/README.md
@@ -230,6 +230,14 @@ router.preload(["english", "multilingual"]) # or just the two you serve
`preload` raises `max_loaded` to fit what it built, so the LRU cannot evict it immediately.
+If the process already has a checkpoint loaded for other reasons, hand it over instead of
+loading a second copy:
+
+```python
+router.attach("english", existing_agent) # no duplicate 421M parameters
+router.preload() # builds only what is still missing
+```
+
Measured on CPU with the demo Space's own workload:
| | per request | model loads |
diff --git a/laya/__init__.py b/laya/__init__.py
index cf7ecc5b..19e535ce 100644
--- a/laya/__init__.py
+++ b/laya/__init__.py
@@ -22,7 +22,7 @@ from .presets import (
)
from .router import DEFAULT_MODELS, RouteDecision, Router
-__version__ = "0.3.2"
+__version__ = "0.3.3"
__all__ = [
"Agent",
"RLAgent",
diff --git a/laya/router.py b/laya/router.py
index c0efda54..545063ab 100644
--- a/laya/router.py
+++ b/laya/router.py
@@ -194,6 +194,19 @@ class Router:
if k not in self._order:
self._agents.pop(k, None)
+ def attach(self, name: str, agent: Any):
+ """Register an already-built Agent under `name` instead of loading a second copy.
+
+ Useful when the process has a checkpoint loaded for other reasons: a demo that already
+ built `convaiinnovations/laya` can hand it to the router rather than pay for -- and hold
+ in memory -- a duplicate 421M parameters.
+ """
+ key = normalise_name(name)
+ self._agents[key] = agent
+ self._touch(key)
+ self.max_loaded = max(self.max_loaded, len(self._agents))
+ return agent
+
def preload(self, names: Optional[List[str]] = None):
"""Download and build checkpoints up front so no request ever pays a model load.
@@ -203,9 +216,10 @@ class Router:
the LRU would immediately evict what this just built.
"""
names = [normalise_name(n) for n in (names or list(self.models))]
- self.max_loaded = max(self.max_loaded, len(names))
+ self.max_loaded = max(self.max_loaded, len(names), len(self._agents))
for n in names:
- self.load(n)
+ if n not in self._agents: # an attached agent is already built
+ self.load(n)
return self
def unload(self, name: Optional[str] = None):
diff --git a/pyproject.toml b/pyproject.toml
index c2626981..be4372aa 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "laya"
-version = "0.3.2"
+version = "0.3.3"
description = "Fast, non-autoregressive System 1 decision engine with calibrated probabilities"
readme = "README.md"
requires-python = ">=3.8"
diff --git a/setup.py b/setup.py
index a158e188..9b93a818 100644
--- a/setup.py
+++ b/setup.py
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
setup(
name="laya",
- version="0.3.2",
+ version="0.3.3",
description="Fast, non-autoregressive System 1 decision engine with calibrated probabilities",
long_description=open("README.md").read(),
long_description_content_type="text/markdown",
diff --git a/tests/test_router.py b/tests/test_router.py
index cb2c7973..a256bb1c 100644
--- a/tests/test_router.py
+++ b/tests/test_router.py
@@ -255,6 +255,21 @@ _load_stub(rp2, "english")
check("preload/touch does not evict", sorted(rp2.loaded), ["english", "multilingual"])
+# --------------------------------------------------------------------- attach
+ra = stubbed_router(1)
+sentinel = _Stub("already-built")
+ra.attach("english", sentinel)
+check("attach/registers under the name", ra._agents["english"], sentinel)
+check("attach/counts as resident", "english" in ra.loaded, True)
+check("attach/raises max_loaded to hold it", ra.max_loaded >= 1, True)
+# attaching then loading another must not evict the attached one
+ra.max_loaded = max(ra.max_loaded, 2)
+_load_stub(ra, "multilingual")
+check("attach/survives a later load", sorted(ra.loaded), ["english", "multilingual"])
+check("attach/still the same object", ra._agents["english"] is sentinel, True)
+check("attach/accepts aliases", stubbed_router(1).attach("en", _Stub("x")) is not None, True)
+
+
# --------------------------------------------------------------------- report
print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))
for f in FAIL: