mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-14 22:17:00 +00:00
feat: embed on the worker, and stop batching the ONNX pass
The API embeds every query it serves, so it held its own copy of the model: ~890 MB it never needed. EMBEDDINGS_DELEGATE_TO_WORKER (on by default) sends the text to the Celery worker instead and gets the vector back, taking an API process from 1176 MB to 285 MB with no ONNX Runtime imported at all. The client embeds locally when it finds itself inside a worker task, so the worker never dispatches to itself -- the same self-deadlock DOCUMENT_PARSE_QUEUE avoids on the parsing side. EMBEDDINGS_BASE_URL still wins over it, and remains the right answer for production. ensure_vector_schema was constructing the embeddings instance purely to read .dimension off it, loading several hundred MB of ONNX into every API and worker process at import. For a model the registry describes that is a lookup; only an unregistered name now falls back to loading. EMBEDDINGS_BATCH_SIZE was sizing two unrelated things: chunks per store transaction (and per remote embed request) and documents per ONNX forward pass. Each pass pads every input up to its longest, and that waste grows with the square of chunk length, so at the 1250-token default a batch of 32 peaked at 6.6 GB and took 326s where a batch of 1 peaked at 2.9 GB and took 90s. The forward pass is now sized by EMBEDDINGS_MODEL_BATCH_SIZE, defaulting to 1; storage and remote batching are unchanged at 32. reembed embeds in-process: a batch job that walks the whole index should not round-trip every chunk through a broker, and loading the model there reports a real failure instead of timing out against an empty queue. Also drops the mpnet zip download from the docs and the devcontainer, which pointed at a SentenceTransformers export with no ONNX graph and had been inert since the FastEmbed swap; corrects the claim that any sentence-transformers model works; and settles the Configuring/Settings pages on what the registry and the repository metadata actually decide.
This commit is contained in:
1 parent
ef51ee97b3
commit
47e53a71c4
20 files changed
+679
-266
No files matched your search
@@ -74,8 +74,8 @@ class TestEnsureVectorSchemaCreates:
|
||||
cursor = MagicMock()
|
||||
conn.cursor.return_value = cursor
|
||||
with patch("psycopg.connect", return_value=conn) as connect, patch(
|
||||
"application.vectorstore.base.get_embeddings",
|
||||
return_value=_embeddings(dimension),
|
||||
"application.vectorstore.model_registry.dimension_for",
|
||||
return_value=dimension,
|
||||
), patch(
|
||||
"application.vectorstore.pgvector.PGVectorStore.create_schema"
|
||||
) as vector_schema, patch(
|
||||
@@ -130,8 +130,8 @@ class TestEnsureVectorSchemaDimensionCheck:
|
||||
):
|
||||
conn = MagicMock()
|
||||
with patch("psycopg.connect", return_value=conn), patch(
|
||||
"application.vectorstore.base.get_embeddings",
|
||||
return_value=_embeddings(1536),
|
||||
"application.vectorstore.model_registry.dimension_for",
|
||||
return_value=1536,
|
||||
), patch("application.vectorstore.pgvector.PGVectorStore.create_schema"), patch(
|
||||
"application.vectorstore.pgvector.PGVectorStore.table_dimension",
|
||||
return_value=768,
|
||||
@@ -226,3 +226,42 @@ class TestBootGating:
|
||||
assert 'os.environ.setdefault("AUTO_VECTOR_SCHEMA", "false")' in (
|
||||
conftest.read_text()
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestBootDoesNotLoadTheModel:
|
||||
"""The hook needs an integer, not an inference session.
|
||||
|
||||
It used to build the embeddings instance to read ``.dimension`` off it,
|
||||
loading several hundred MB of ONNX into every API and worker process at
|
||||
import. For a model the registry describes that is a lookup.
|
||||
"""
|
||||
|
||||
def _run(self, registry_dim, loader):
|
||||
conn = MagicMock()
|
||||
conn.cursor.return_value = MagicMock()
|
||||
with patch("psycopg.connect", return_value=conn), patch(
|
||||
"application.vectorstore.model_registry.dimension_for",
|
||||
return_value=registry_dim,
|
||||
), patch(
|
||||
"application.vectorstore.base.build_local_embeddings", loader
|
||||
), patch(
|
||||
"application.vectorstore.pgvector.PGVectorStore.create_schema"
|
||||
) as vector_schema, patch(
|
||||
"application.vectorstore.pgvector.PGVectorStore.table_dimension",
|
||||
return_value=None,
|
||||
):
|
||||
ensure_vector_schema()
|
||||
return vector_schema
|
||||
|
||||
def test_a_registered_model_is_never_constructed(self, vector_settings):
|
||||
loader = MagicMock()
|
||||
vector_schema = self._run(768, loader)
|
||||
loader.assert_not_called()
|
||||
assert vector_schema.call_args.kwargs["dimension"] == 768
|
||||
|
||||
def test_an_unregistered_model_still_falls_back_to_loading(self, vector_settings):
|
||||
loader = MagicMock(return_value=_embeddings(1024))
|
||||
vector_schema = self._run(None, loader)
|
||||
loader.assert_called_once()
|
||||
assert vector_schema.call_args.kwargs["dimension"] == 1024
|
||||
Reference in new issue
Block a user