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.

fix(backends): make eager install push a replacement forward aside

Aashishb738272aef95
diff --git a/laya/backends/eager.py b/laya/backends/eager.py
index c8851384..81c45f16 100644
--- a/laya/backends/eager.py
+++ b/laya/backends/eager.py
@@ -6,9 +6,23 @@ class EagerBackend(Backend):
name = "eager"
def install(self) -> None:
- # Nothing to swap: `model.forward` is already the eager forward. Marking it installed keeps
- # the lifecycle uniform for the Agent.
+ # Usually there is nothing to swap: `model.forward` is the class forward. It is not always --
+ # the base class documents that a backend may find an instance `forward` already on the model,
+ # left by another backend or by the legacy `fast=True` path, which `laya.backends.install`
+ # does not own. Pushing that replacement aside is what makes installing eager mean the stock
+ # forward answers; leaving it would report `eager` from `agent.backend` while a graph backend
+ # still ran every request.
+ if self.installed:
+ return
+ self._prior_forward = self.model.__dict__.pop("forward", None)
self.installed = True
def uninstall(self) -> None:
+ # The replacement goes back exactly as the base contract has it, so eager stacked over
+ # another backend leaves that backend's work in place rather than a half-switched model.
+ if not self.installed:
+ return
+ if self._prior_forward is not None:
+ self.model.forward = self._prior_forward
+ self._prior_forward = None
self.installed = False
diff --git a/tests/test_backends.py b/tests/test_backends.py
index aa53dcba..3bf3ad0f 100644
--- a/tests/test_backends.py
+++ b/tests/test_backends.py
@@ -154,6 +154,45 @@ e = EagerBackend(agent)
check("eager/max_len is unbounded", e.max_len, None)
check("eager/warmup is free", e.warmup(), 0.0)
+# ------------------------------------------------------------------ eager honours the lifecycle
+# `Backend.install` documents that the model may already carry an instance `forward` -- another
+# backend's, or the legacy `fast=True` one -- and puts it back on uninstall. Eager overrides that
+# lifecycle and did none of it, so installing eager over a replacement left the replacement
+# answering every request (`agent.backend` reporting "eager" all the while) and its `uninstall`
+# was a no-op that left the shadow on the model with nothing owning it.
+shadow = Doubling(agent)
+shadow.install()
+check_true("eager over a backend/the replacement runs first", torch.equal(agent.model(**b)[0], ref * 2))
+sh = EagerBackend(agent)
+sh.install()
+check_true("eager over a backend/install makes the stock forward answer", torch.equal(agent.model(**b)[0], ref))
+check_true("eager over a backend/install leaves no shadow on the model", "forward" not in agent.model.__dict__)
+check_true("eager over a backend/stock_forward is the model, not the shadow",
+ torch.equal(sh.stock_forward(**b)[0], ref))
+sh.install()
+check_true("eager over a backend/install is idempotent", torch.equal(agent.model(**b)[0], ref))
+sh.uninstall()
+check_true("eager over a backend/uninstall puts the replacement back", torch.equal(agent.model(**b)[0], ref * 2))
+check_true("eager over a backend/the replacement is still owned", "forward" in agent.model.__dict__)
+sh.uninstall()
+check_true("eager over a backend/uninstall is idempotent", torch.equal(agent.model(**b)[0], ref * 2))
+shadow.uninstall()
+check_true("eager over a backend/both leaving is the stock model again",
+ untouched(agent.model) and torch.equal(agent.model(**b)[0], ref))
+
+# `laya.backends.install` uninstalls `agent._backend` only. A shadow owned by the agent's legacy
+# `_fast` path is still on the model when eager is asked for, and eager is what has to clear it.
+legacy = fake_agent()
+ln = Doubling(legacy)
+ln.install()
+check("install(eager) over a legacy shadow/name", backends.install(legacy, "eager").name, "eager")
+check_true("install(eager) over a legacy shadow/the stock forward answers",
+ torch.equal(legacy.model(**b)[0], ref))
+check_true("install(eager) over a legacy shadow/no shadow left behind", "forward" not in legacy.model.__dict__)
+ln.uninstall()
+check_true("install(eager) over a legacy shadow/stock model after the owner leaves",
+ untouched(legacy.model) and torch.equal(legacy.model(**b)[0], ref))
+
# ------------------------------------------------------------------ the Agent surface
import inspect # noqa: E402
from laya.agent import Agent, load # noqa: E402