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(shortlist): refuse a cache write whose embedding width changed

Aashish83428f43d7e2
diff --git a/laya/shortlist.py b/laya/shortlist.py
index 105cf805..038945ac 100644
--- a/laya/shortlist.py
+++ b/laya/shortlist.py
@@ -231,6 +231,20 @@ def cached_embed_fn(
% (len(missing), tuple(fresh.shape))
)
fresh = np.nan_to_num(fresh, copy=True, nan=0.0, posinf=0.0, neginf=0.0)
+ if rows_by_text:
+ # Rows stored under one dimensionality cannot be stacked against rows of
+ # another: a changed embedder (or one whose dimension drifts between calls)
+ # would otherwise surface rows of mixed width, which `_rank` reads as one
+ # matrix. Deciding by identity is what the docstring already asks of the
+ # caller ("clear the cache if the model changes") -- this refuses the call
+ # instead of returning a matrix that silently mixes both.
+ want = next(iter(rows_by_text.values())).shape[0]
+ if fresh.shape[1] != want:
+ raise ValueError(
+ "embed_fn returned dim %d, but the cache holds dim %d; "
+ "call cache_clear() if the model behind embed_fn changed"
+ % (fresh.shape[1], want)
+ )
with lock:
for key, row in zip(missing, fresh):
rows_by_text[key] = row
diff --git a/tests/test_shortlist.py b/tests/test_shortlist.py
index e52dc528..0e77f5ad 100644
--- a/tests/test_shortlist.py
+++ b/tests/test_shortlist.py
@@ -721,6 +721,39 @@ check_true(
mt_cached.cache_info()["hits"] + mt_cached.cache_info()["misses"] == 32,
)
+# --------------------------------------------------------------- cache dimensionality
+# A row stored under one dimensionality cannot stack against a row of another: the
+# docstring asks the caller to clear the cache when the model changes, and the cache
+# refuses the call instead of returning a matrix that silently mixes both.
+
+
+class SwapEmbed:
+ """One callable whose width changes between calls, like a swapped model."""
+
+ def __init__(self):
+ self.dim = 2
+ self.calls = []
+
+ def __call__(self, texts):
+ self.calls.append(list(texts))
+ return [[1.0] * self.dim for _text in texts]
+
+
+swap_fn = SwapEmbed()
+swap_cached = cached_embed_fn(swap_fn)
+swap_cached(["alpha"])
+check("cache/dim first width cached", swap_cached.cache_info()["size"], 1)
+swap_fn.dim = 3
+try:
+ swap_cached(["beta"])
+ check_true("cache/dim drift refused", False, "no error raised")
+except ValueError as exc:
+ check_true("cache/dim drift refused", "cache_clear()" in str(exc), str(exc))
+check("cache/dim drift caches nothing new", swap_cached.cache_info()["size"], 1)
+swap_cached.cache_clear()
+swap_cached(["beta"])
+check("cache/dim clear then re-embed works", swap_cached.cache_info()["size"], 1)
+
print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))
for f in FAIL: