-
Notifications
You must be signed in to change notification settings - Fork 974
base64 dense vector packing #1499
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
2
commits into
main
Choose a base branch
from
base64-dense-vector-packing
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.
+260
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
| /** | ||
| * Elasticsearch PHP Client | ||
| * | ||
| * @link https://github.com/elastic/elasticsearch-php | ||
| * @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co) | ||
| * @license https://opensource.org/licenses/MIT MIT License | ||
| * | ||
| * Licensed to Elasticsearch B.V under one or more agreements. | ||
| * Elasticsearch B.V licenses this file to you under the MIT License. | ||
| * See the LICENSE file in the project root for more information. | ||
| */ | ||
| declare(strict_types = 1); | ||
|
|
||
| namespace Elastic\Elasticsearch\Helper; | ||
|
|
||
| class Vectors | ||
| { | ||
| /** | ||
| * Pack a dense vector for efficient uploading. | ||
| * | ||
| * @param array<float> $vector | ||
| */ | ||
| public static function packDenseVector(array $vector): string | ||
| { | ||
| return base64_encode(pack('G*', ...$vector)); | ||
| } | ||
| } |
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,183 @@ | ||
| <?php | ||
| /** | ||
| * Elasticsearch PHP Client | ||
| * | ||
| * @link https://github.com/elastic/elasticsearch-php | ||
| * @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co) | ||
| * @license https://opensource.org/licenses/MIT MIT License | ||
| * | ||
| * Licensed to Elasticsearch B.V under one or more agreements. | ||
| * Elasticsearch B.V licenses this file to you under the MIT License. | ||
| * See the LICENSE file in the project root for more information. | ||
| */ | ||
| declare(strict_types = 1); | ||
| ini_set('memory_limit', '1024M'); | ||
|
|
||
| require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
|
||
| use Elastic\Elasticsearch\Helper\Vectors; | ||
|
|
||
| $ELASTICSEARCH_URL = ''; | ||
| $chunk_sizes = array(100, 250, 500, 1000); | ||
| $repetitions = 20; | ||
| $json_output = FALSE; | ||
| $runs = 3; | ||
| $dataset_file = ''; | ||
| $rest_index = null; | ||
| $dataset = []; | ||
| $index = 'benchmark'; | ||
|
|
||
| function upload($client, $index, $dataset, $chunk_size, $repetitions, $packed) { | ||
| // create index | ||
| if ($client->indices()->exists(['index' => $index])->getStatusCode() != 404) { | ||
| $client->indices()->delete(['index' => $index]); | ||
| } | ||
| $client->indices()->create([ | ||
| 'index' => $index, | ||
| 'body' => [ | ||
| 'mappings' => [ | ||
| 'properties' => [ | ||
| 'docid' => [ | ||
| 'type' => 'keyword', | ||
| ], | ||
| 'title' => [ | ||
| 'type' => 'text', | ||
| ], | ||
| 'text' => [ | ||
| 'type' => 'text', | ||
| ], | ||
| 'emb' => [ | ||
| 'type' => 'dense_vector', | ||
| 'index_options' => [ | ||
| 'type' => 'flat', | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ]); | ||
| $client->indices()->refresh(['index' => $index]); | ||
|
|
||
| // run the bulk upload | ||
| $len = sizeof($dataset); | ||
| $params = ['body' => []]; | ||
| $start = microtime(true); | ||
| for ($i = 1; $i <= $len * $repetitions; $i++) { | ||
| $doc = $dataset[($i - 1) % $len]; | ||
| $params['body'][] = ['index' => ['_index' => $index]]; | ||
| $params['body'][] = [ | ||
| 'docid' => $doc['docid'], | ||
| 'title' => $doc['title'], | ||
| 'text' => $doc['text'], | ||
| 'emb' => $packed ? Vectors::packDenseVector($doc['emb']) : $doc['emb'], | ||
| ]; | ||
| if ($i % $chunk_size == 0) { | ||
| $response = $client->bulk($params); | ||
| if ($response['errors']) { | ||
| echo 'Error during bulk upload. Exiting'; | ||
| exit(1); | ||
| } | ||
| $params = ['body' => []]; | ||
| unset($response); | ||
| } | ||
| } | ||
| if (!empty($params['body'])) { | ||
| $response = $client->bulk($params); | ||
| if ($response['errors']) { | ||
| echo 'Error during bulk upload. Exiting'; | ||
| exit(1); | ||
| } | ||
| unset($params); | ||
| unset($response); | ||
| } | ||
| return microtime(true) - $start; | ||
| } | ||
|
|
||
| $opts = getopt('s:r:', array('url:', 'json', 'runs:', 'help'), $rest_index); | ||
| if (array_key_exists('help', $opts)) { | ||
| echo 'Usage: ' . $argv[0] . '[-s CHUNK_SIZES] [-r REPETITIONS] [--url URL] [--json] [--runs RUNS] DATASET_FILE\n'; | ||
| echo ' -s CHUNK_SIZES List of chunk sizes to use, separated by commas (default: 100,250,500,1000)\n'; | ||
| echo ' -r REPETITIONS Number of times the dataset is repeated (default: 20)\n'; | ||
| echo ' --url URL The Elasticsearch connection URL\n'; | ||
| echo ' --json Output benchmark results in JSON format\n'; | ||
| echo ' --runs Number of runs that are averaged for each chunk size (default: 3)\n'; | ||
| exit(0); | ||
| } | ||
| if (!array_key_exists('url', $opts)) { | ||
| echo 'Error: --url argument is required.'; | ||
| exit(1); | ||
| } | ||
| else { | ||
| $ELASTICSEARCH_URL = $opts['url']; | ||
| } | ||
| if (array_key_exists('s', $opts)) { | ||
| $chunk_sizes = array_map(fn($v) => intval($v), explode(',', $opts['s'])); | ||
| } | ||
| if (array_key_exists('r', $opts)) { | ||
| $repetitions = intval($opts['r']); | ||
| } | ||
| if (array_key_exists('json', $opts)) { | ||
| $json_output = TRUE; | ||
| } | ||
| if (array_key_exists('runs', $opts)) { | ||
| $runs = intval($opts['runs']); | ||
| } | ||
| if (!$argv[$rest_index]) { | ||
| echo 'Error'; | ||
| exit(1); | ||
| } | ||
| else { | ||
| $dataset_file = $argv[$rest_index]; | ||
| } | ||
|
|
||
| // read CSV dataset | ||
| $f = fopen($dataset_file, 'rt'); | ||
| while (!feof($f)) { | ||
| $line = fgets($f); | ||
| if ($line !== FALSE) { | ||
| $dataset[] = json_decode($line, true); | ||
| } | ||
| } | ||
| fclose($f); | ||
|
|
||
| // initialize client | ||
| $client = Elastic\Elasticsearch\ClientBuilder::create() | ||
| ->setHosts([$ELASTICSEARCH_URL]) | ||
| ->build(); | ||
|
|
||
| // run the benchmark | ||
| $results = []; | ||
| foreach ($chunk_sizes as $chunk_size) { | ||
| if (!$json_output) { | ||
| echo 'Uploading ' . $dataset_file . ' with chunk size ' . $chunk_size . "...\n"; | ||
| } | ||
| $normal_runs = []; | ||
| $packed_runs = []; | ||
| for ($run = 0; $run < $runs; $run++) { | ||
| $normal_runs[] = upload($client, $index, $dataset, $chunk_size, $repetitions, FALSE); | ||
| $packed_runs[] = upload($client, $index, $dataset, $chunk_size, $repetitions, TRUE); | ||
| } | ||
| $t = array_sum($normal_runs) / $runs; | ||
| $pt = array_sum($packed_runs) / $runs; | ||
| $result = [ | ||
| 'dataset_size' => sizeof($dataset) * $repetitions, | ||
| 'chunk_size' => $chunk_size, | ||
| 'float32' => [ | ||
| 'duration' => intval($t * 1000 + 0.5), | ||
| ], | ||
| 'base64' => [ | ||
| 'duration' => intval($pt * 1000 + 0.5), | ||
| ], | ||
| ]; | ||
| $results[] = $result; | ||
| if (!$json_output) { | ||
| echo 'Size: ' . $result['dataset_size'] . "\n"; | ||
| echo 'float duration: ' . number_format($t, 2) . 's (' . number_format($result['dataset_size'] / $t, 2) . " docs/s)\n"; | ||
| echo 'base64 duration: ' . number_format($pt, 2) . 's (' . number_format($result['dataset_size'] / $pt, 2) . " docs/s)\n"; | ||
| echo 'Speed up: ' . number_format($t / $pt, 2) . "x\n"; | ||
| } | ||
| } | ||
|
|
||
| if ($json_output) { | ||
| echo json_encode($results, JSON_PRETTY_PRINT); | ||
| } | ||
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 think we're supposed to leave out the docid in the benchmark.
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 believe that refers to not using the
docidas the document id. This puts it as a regular field, which I saw others do as well. I don't think it will make any significant difference with or without this field, anyway.