-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add helper function to pack dense vectors for efficient uploading #3219
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
Open
miguelgrinberg
wants to merge
3
commits into
elastic:main
Choose a base branch
from
miguelgrinberg:base64-dense-vectors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| # Licensed to Elasticsearch B.V. under one or more contributor | ||
| # license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright | ||
| # ownership. Elasticsearch B.V. licenses this file to you under | ||
| # the Apache License, Version 2.0 (the "License"); you may | ||
| # not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| import argparse | ||
| import asyncio | ||
| import json | ||
| import os | ||
| import time | ||
|
|
||
| import numpy as np | ||
|
|
||
| from elasticsearch import OrjsonSerializer | ||
| from elasticsearch.dsl import AsyncDocument, NumpyDenseVector, async_connections | ||
| from elasticsearch.dsl.types import DenseVectorIndexOptions | ||
| from elasticsearch.helpers import async_bulk, pack_dense_vector | ||
|
|
||
| async_connections.create_connection( | ||
| hosts=[os.environ["ELASTICSEARCH_URL"]], serializer=OrjsonSerializer() | ||
| ) | ||
|
|
||
|
|
||
| class Doc(AsyncDocument): | ||
| title: str | ||
| text: str | ||
| emb: np.ndarray = NumpyDenseVector( | ||
| dtype=np.float32, index_options=DenseVectorIndexOptions(type="flat") | ||
| ) | ||
|
|
||
| class Index: | ||
| name = "benchmark" | ||
|
|
||
|
|
||
| async def upload( | ||
| data_file: str, chunk_size: int, repetitions: int, pack: bool | ||
| ) -> tuple[float, float]: | ||
| with open(data_file, "rt") as f: | ||
| # read the data file, which comes in ndjson format and convert it to JSON | ||
| json_data = "[" + f.read().strip().replace("\n", ",") + "]" | ||
| dataset = json.loads(json_data) | ||
|
|
||
| # replace the embedding lists with numpy arrays for performance | ||
| dataset = [ | ||
| { | ||
| "docid": doc["docid"], | ||
| "title": doc["title"], | ||
| "text": doc["text"], | ||
| "emb": np.array(doc["emb"], dtype=np.float32), | ||
| } | ||
| for doc in dataset | ||
| ] | ||
|
|
||
| # create mapping and index | ||
| if await Doc._index.exists(): | ||
| await Doc._index.delete() | ||
| await Doc.init() | ||
| await Doc._index.refresh() | ||
|
|
||
| async def get_next_document(): | ||
| for i in range(repetitions): | ||
| for doc in dataset: | ||
| yield { | ||
| "_index": "benchmark", | ||
| "_id": doc["docid"] + "_" + str(i), | ||
| "_source": { | ||
| "title": doc["title"], | ||
| "text": doc["text"], | ||
| "emb": doc["emb"], | ||
| }, | ||
| } | ||
|
|
||
| async def get_next_document_packed(): | ||
| for i in range(repetitions): | ||
| for doc in dataset: | ||
| yield { | ||
| "_index": "benchmark", | ||
| "_id": doc["docid"] + "_" + str(i), | ||
| "_source": { | ||
| "title": doc["title"], | ||
| "text": doc["text"], | ||
| "emb": pack_dense_vector(doc["emb"]), | ||
| }, | ||
| } | ||
|
|
||
| start = time.time() | ||
| result = await async_bulk( | ||
| client=async_connections.get_connection(), | ||
| chunk_size=chunk_size, | ||
| actions=get_next_document_packed() if pack else get_next_document(), | ||
| stats_only=True, | ||
| ) | ||
| duration = time.time() - start | ||
| assert result[1] == 0 | ||
| return result[0], duration | ||
|
|
||
|
|
||
| async def main(): | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("data_file", metavar="JSON_DATA_FILE") | ||
| parser.add_argument( | ||
| "--chunk-sizes", | ||
| "-s", | ||
| type=int, | ||
| nargs="+", | ||
| help="Chunk size(s) for bulk uploader", | ||
| ) | ||
| parser.add_argument( | ||
| "--repetitions", | ||
| "-r", | ||
| type=int, | ||
| default=1, | ||
| help="Number of times the dataset is repeated (default: 1)", | ||
| ) | ||
| parser.add_argument( | ||
| "--runs", | ||
| type=int, | ||
| default=3, | ||
| help="Number of runs that are averaged for each chunk size (default: 3)", | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| for chunk_size in args.chunk_sizes: | ||
| print(f"Uploading '{args.data_file}' with chunk size {chunk_size}...") | ||
| runs = [] | ||
| packed_runs = [] | ||
| for _ in range(args.runs): | ||
| runs.append( | ||
| await upload(args.data_file, chunk_size, args.repetitions, False) | ||
| ) | ||
| packed_runs.append( | ||
| await upload(args.data_file, chunk_size, args.repetitions, True) | ||
| ) | ||
|
|
||
| # ensure that all runs uploaded the same number of documents | ||
| size = runs[0][0] | ||
| for run in runs: | ||
| assert run[0] == size | ||
| for run in packed_runs: | ||
| assert run[0] == size | ||
|
|
||
| dur = sum([run[1] for run in runs]) / len(runs) | ||
| packed_dur = sum([run[1] for run in packed_runs]) / len(packed_runs) | ||
|
|
||
| print(f"Size: {size}") | ||
| print(f"float duration: {dur:.02f}s / {size / dur:.02f} docs/s") | ||
| print( | ||
| f"float base64 duration: {packed_dur:.02f}s / {size / packed_dur:.02f} docs/s" | ||
| ) | ||
| print(f"Speed up: {dur / packed_dur:.02f}x") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I like the name, because it means we can support more than numpy in the future if we want to.
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.
Even now, you can pass a list of floats and it will handle it too (I'm converting the list to numpy inside the function though).