Key the vectors model cache by configuration (#1223)

This commit is contained in:
Morgan Carr
2026-09-04 16:47:49 -04:00
committed by GitHub
parent 21460347dd
commit 767272f2d5
2 changed files with 57 additions and 3 deletions
+10 -3
View File
@@ -96,16 +96,23 @@ class Vectors:
model
"""
# Build a cache key from settings that change the loaded model when a configuration is present.
if self.config:
config = {"path": path, "method": self.config.get("method"), "vectors": self.config.get("vectors", {})}
key = json.dumps(config, sort_keys=True, default=str)
else:
key = path
# Check if model is cached
if self.models and path in self.models:
return self.models[path]
if self.models and key in self.models:
return self.models[key]
# Create new model
model = self.loadmodel(path)
# Store model in cache
if self.models is not None and path:
self.models[path] = model
self.models[key] = model
return model
+47
View File
@@ -654,6 +654,53 @@ class TestEmbeddings(unittest.TestCase):
# Close embeddings
embeddings.close()
def testSubindexVectors(self):
"""
Test subindexes with the same model path and different vectors settings
"""
# Build data array
data = [(uid, text, None) for uid, text in enumerate(self.data)]
# Subindexes share a models cache. The same model path with different vectors settings must load separate models.
path = "neuml/colbert-bert-tiny"
embeddings = Embeddings(
{
"defaults": False,
"indexes": {
"index1": {"path": path, "vectors": {"muvera": {"repetitions": 1}}},
"index2": {"path": path, "vectors": {"muvera": {"repetitions": 2}}},
},
}
)
embeddings.index(data)
# MUVERA output dimensions = repetitions * 2^5 * 16
self.assertEqual(embeddings.transform("feel good story", index="index1").shape, (512,))
self.assertEqual(embeddings.transform("feel good story", index="index2").shape, (1024,))
self.assertEqual(embeddings.indexes["index1"].config["dimensions"], 512)
self.assertEqual(embeddings.indexes["index2"].config["dimensions"], 1024)
self.assertIsNot(embeddings.indexes["index1"].model.model, embeddings.indexes["index2"].model.model)
# Close embeddings
embeddings.close()
# Per-encode settings share the same loaded model
embeddings = Embeddings(
{
"defaults": False,
"indexes": {
"index1": {"path": path, "maxlength": 32, "vectors": {"muvera": {"repetitions": 1}}},
"index2": {"path": path, "maxlength": 64, "vectors": {"muvera": {"repetitions": 1}}},
},
}
)
embeddings.index(data)
self.assertIs(embeddings.indexes["index1"].model.model, embeddings.indexes["index2"].model.model)
# Close embeddings
embeddings.close()
def testTerms(self):
"""
Test extracting keyword terms from queries