-
Notifications
You must be signed in to change notification settings - Fork 106
pgvector improvements #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
69068cd
6dcd9fa
eb167f9
65c6005
4eef95a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,16 @@ | |
import psycopg | ||
from pgvector.psycopg import register_vector | ||
|
||
from engine.base_client.distances import Distance | ||
from engine.base_client.upload import BaseUploader | ||
from engine.clients.pgvector.config import get_db_config | ||
|
||
|
||
class PgVectorUploader(BaseUploader): | ||
DISTANCE_MAPPING = { | ||
Distance.L2: "vector_l2_ops", | ||
Distance.COSINE: "vector_cosine_ops", | ||
} | ||
conn = None | ||
cur = None | ||
upload_params = {} | ||
|
@@ -27,10 +32,28 @@ def upload_batch( | |
vectors = np.array(vectors) | ||
|
||
# Copy is faster than insert | ||
with cls.cur.copy("COPY items (id, embedding) FROM STDIN") as copy: | ||
with cls.cur.copy( | ||
"COPY items (id, embedding) FROM STDIN WITH (FORMAT BINARY)" | ||
) as copy: | ||
copy.set_types(["integer", "vector"]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Binary format requires |
||
for i, embedding in zip(ids, vectors): | ||
copy.write_row((i, embedding)) | ||
|
||
@classmethod | ||
def post_upload(cls, distance): | ||
try: | ||
hnsw_distance_type = cls.DISTANCE_MAPPING[distance] | ||
except KeyError: | ||
raise IncompatibilityError(f"Unsupported distance metric: {distance}") | ||
|
||
cls.conn.execute("SET max_parallel_workers = 128") | ||
cls.conn.execute("SET max_parallel_maintenance_workers = 128") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will use all available cores to build the index (let me know if it should do something different) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want to utilize all cores, would not it be better to get the number of cores from OS rather than to hardcode it to 128? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Setting a high upper limit should be simpler than trying to get the exact number of cores from the server (and won't make a difference in the number of cores used). |
||
cls.conn.execute( | ||
f"CREATE INDEX ON items USING hnsw (embedding {hnsw_distance_type}) WITH (m = {cls.upload_params['hnsw_config']['m']}, ef_construction = {cls.upload_params['hnsw_config']['ef_construct']})" | ||
) | ||
Comment on lines
+51
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, might be a bit unfair to the other competitors, which also support loading in a "split-mode", however everyone is welcome to come and improve the setup if they think it is not optimised 🤷♂️ |
||
|
||
return {} | ||
|
||
@classmethod | ||
def delete_client(cls): | ||
if cls.cur: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,104 +3,88 @@ | |
"name": "pgvector-default", | ||
"engine": "pgvector", | ||
"connection_params": {}, | ||
"collection_params": { | ||
"hnsw_config": { "m": 16, "ef_construct": 128 } | ||
}, | ||
"collection_params": {}, | ||
"search_params": [ | ||
{ "parallel": 1, "search_params": { "hnsw_ef": 128 } } | ||
{ "parallel": 8, "search_params": { "hnsw_ef": 128 } } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not used for running the experiments. But okay :) |
||
], | ||
"upload_params": { "parallel": 1, "batch_size": 1024 } | ||
"upload_params": { "parallel": 16, "batch_size": 1024, "hnsw_config": { "m": 16, "ef_construct": 128 } } | ||
}, | ||
{ | ||
"name": "pgvector-parallel", | ||
"engine": "pgvector", | ||
"connection_params": {}, | ||
"collection_params": { | ||
"hnsw_config": { "m": 16, "ef_construct": 128 } | ||
}, | ||
"collection_params": {}, | ||
"search_params": [ | ||
{ "parallel": 8, "search_params": { "hnsw_ef": 128 } }, | ||
{ "parallel": 16, "search_params": { "hnsw_ef": 128 } }, | ||
{ "parallel": 100, "search_params": { "hnsw_ef": 128 } } | ||
], | ||
"upload_params": { "parallel": 1, "batch_size": 1024 } | ||
"upload_params": { "parallel": 1, "batch_size": 1024, "hnsw_config": { "m": 16, "ef_construct": 128 } } | ||
}, | ||
{ | ||
"name": "pgvector-m-16-ef-128", | ||
"engine": "pgvector", | ||
"connection_params": {}, | ||
"collection_params": { | ||
"hnsw_config": { "m": 16, "ef_construct": 128 } | ||
}, | ||
"collection_params": {}, | ||
"search_params": [ | ||
{ "parallel": 1, "search_params": { "hnsw_ef": 64 } }, { "parallel": 1, "search_params": { "hnsw_ef": 128 } }, { "parallel": 1, "search_params": { "hnsw_ef": 256 } }, { "parallel": 1, "search_params": { "hnsw_ef": 512 } }, | ||
{ "parallel": 100, "search_params": { "hnsw_ef": 64 } }, { "parallel": 100, "search_params": { "hnsw_ef": 128 } }, { "parallel": 100, "search_params": { "hnsw_ef": 256 } }, { "parallel": 100, "search_params": { "hnsw_ef": 512 } } | ||
], | ||
"upload_params": { "parallel": 16 } | ||
"upload_params": { "parallel": 16, "hnsw_config": { "m": 16, "ef_construct": 128 } } | ||
}, | ||
{ | ||
"name": "pgvector-m-32-ef-128", | ||
"engine": "pgvector", | ||
"connection_params": {}, | ||
"collection_params": { | ||
"hnsw_config": { "m": 32, "ef_construct": 128 } | ||
}, | ||
"collection_params": {}, | ||
"search_params": [ | ||
{ "parallel": 1, "search_params": { "hnsw_ef": 64 } }, { "parallel": 1, "search_params": { "hnsw_ef": 128 } }, { "parallel": 1, "search_params": { "hnsw_ef": 256 } }, { "parallel": 1, "search_params": { "hnsw_ef": 512 } }, | ||
{ "parallel": 100, "search_params": { "hnsw_ef": 64 } }, { "parallel": 100, "search_params": { "hnsw_ef": 128 } }, { "parallel": 100, "search_params": { "hnsw_ef": 256 } }, { "parallel": 100, "search_params": { "hnsw_ef": 512 } } | ||
], | ||
"upload_params": { "parallel": 16 } | ||
"upload_params": { "parallel": 16, "hnsw_config": { "m": 32, "ef_construct": 128 } } | ||
}, | ||
{ | ||
"name": "pgvector-m-32-ef-256", | ||
"engine": "pgvector", | ||
"connection_params": {}, | ||
"collection_params": { | ||
"hnsw_config": { "m": 32, "ef_construct": 256 } | ||
}, | ||
"collection_params": {}, | ||
"search_params": [ | ||
{ "parallel": 1, "search_params": { "hnsw_ef": 64 } }, { "parallel": 1, "search_params": { "hnsw_ef": 128 } }, { "parallel": 1, "search_params": { "hnsw_ef": 256 } }, { "parallel": 1, "search_params": { "hnsw_ef": 512 } }, | ||
{ "parallel": 100, "search_params": { "hnsw_ef": 64 } }, { "parallel": 100, "search_params": { "hnsw_ef": 128 } }, { "parallel": 100, "search_params": { "hnsw_ef": 256 } }, { "parallel": 100, "search_params": { "hnsw_ef": 512 } } | ||
], | ||
"upload_params": { "parallel": 16 } | ||
"upload_params": { "parallel": 16, "hnsw_config": { "m": 32, "ef_construct": 256 } } | ||
}, | ||
{ | ||
"name": "pgvector-m-32-ef-512", | ||
"engine": "pgvector", | ||
"connection_params": {}, | ||
"collection_params": { | ||
"hnsw_config": { "m": 32, "ef_construct": 512 } | ||
}, | ||
"collection_params": {}, | ||
"search_params": [ | ||
{ "parallel": 1, "search_params": { "hnsw_ef": 64 } }, { "parallel": 1, "search_params": { "hnsw_ef": 128 } }, { "parallel": 1, "search_params": { "hnsw_ef": 256 } }, { "parallel": 1, "search_params": { "hnsw_ef": 512 } }, | ||
{ "parallel": 100, "search_params": { "hnsw_ef": 64 } }, { "parallel": 100, "search_params": { "hnsw_ef": 128 } }, { "parallel": 100, "search_params": { "hnsw_ef": 256 } }, { "parallel": 100, "search_params": { "hnsw_ef": 512 } } | ||
], | ||
"upload_params": { "parallel": 16 } | ||
"upload_params": { "parallel": 16, "hnsw_config": { "m": 32, "ef_construct": 512 } } | ||
}, | ||
{ | ||
"name": "pgvector-m-64-ef-256", | ||
"engine": "pgvector", | ||
"connection_params": {}, | ||
"collection_params": { | ||
"hnsw_config": { "m": 64, "ef_construct": 256 } | ||
}, | ||
"collection_params": {}, | ||
"search_params": [ | ||
{ "parallel": 1, "search_params": { "hnsw_ef": 64 } }, { "parallel": 1, "search_params": { "hnsw_ef": 128 } }, { "parallel": 1, "search_params": { "hnsw_ef": 256 } }, { "parallel": 1, "search_params": { "hnsw_ef": 512 } }, | ||
{ "parallel": 100, "search_params": { "hnsw_ef": 64 } }, { "parallel": 100, "search_params": { "hnsw_ef": 128 } }, { "parallel": 100, "search_params": { "hnsw_ef": 256 } }, { "parallel": 100, "search_params": { "hnsw_ef": 512 } } | ||
], | ||
"upload_params": { "parallel": 16 } | ||
"upload_params": { "parallel": 16, "hnsw_config": { "m": 64, "ef_construct": 256 } } | ||
}, | ||
{ | ||
"name": "pgvector-m-64-ef-512", | ||
"engine": "pgvector", | ||
"connection_params": {}, | ||
"collection_params": { | ||
"hnsw_config": { "m": 64, "ef_construct": 512 } | ||
}, | ||
"collection_params": {}, | ||
"search_params": [ | ||
{ "parallel": 1, "search_params": { "hnsw_ef": 64 } }, { "parallel": 1, "search_params": { "hnsw_ef": 128 } }, { "parallel": 1, "search_params": { "hnsw_ef": 256 } }, { "parallel": 1, "search_params": { "hnsw_ef": 512 } }, | ||
{ "parallel": 100, "search_params": { "hnsw_ef": 64 } }, { "parallel": 100, "search_params": { "hnsw_ef": 128 } }, { "parallel": 100, "search_params": { "hnsw_ef": 256 } }, { "parallel": 100, "search_params": { "hnsw_ef": 512 } } | ||
], | ||
"upload_params": { "parallel": 16 } | ||
"upload_params": { "parallel": 16, "hnsw_config": { "m": 64, "ef_construct": 512 } } | ||
} | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PGVECTOR_PORT
was previously unused