-
Notifications
You must be signed in to change notification settings - Fork 25.8k
Add bit vector support to semantic text #123187
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
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
1319a40
Updated TestDenseInferenceServiceExtension to support bit embeddings
Mikep86 d0378c1
Updated semantic query builder tests to support bit embeddings
Mikep86 04719e2
Fixed how TestDenseInferenceServiceExtension generates bit embeddings
Mikep86 716200d
Update ShardBulkInferenceActionFilterIT to test using bit embeddings
Mikep86 48b5d0c
Updated SemanticTextFieldTests to generate well-formed bit embeddings
Mikep86 2bfae5e
Added helper/util methods
Mikep86 aefcefd
Added more helper/util methods
Mikep86 6cb4345
Spotless
Mikep86 e4c3921
Use helper/util methods
Mikep86 90fc4fc
Updated ShardBulkInferenceActionFilterTests to test using all element…
Mikep86 4aa6fa6
Updated SemanticInferenceMetadataFieldsRecoveryTests to test all elem…
Mikep86 1296b4d
Merge branch 'main' into semantic-text_bit-vector-tests
Mikep86 399faba
Update docs/changelog/123187.yaml
Mikep86 ab83cf7
Added YAML test
Mikep86 ca711fb
Updated changelog
Mikep86 a438ad8
Merge branch 'main' into semantic-text_bit-vector-tests
elasticmachine 0815989
Merge branch 'main' into semantic-text_bit-vector-tests
Mikep86 dc3e387
Refactored to remove getEmbeddingLength and getDimensions methods
Mikep86 214c663
Remove TODO
Mikep86 693e1e6
Merge branch 'main' into semantic-text_bit-vector-tests
elasticmachine 7fa409c
Merge branch 'main' into semantic-text_bit-vector-tests
elasticmachine e85ea6e
Added comment
Mikep86 3e94ab2
Merge branch 'main' into semantic-text_bit-vector-tests
Mikep86 9441a74
Updated SemanticInferenceMetadataFieldsRecoveryTests to not use cosin…
Mikep86 b24835a
Merge branch 'main' into semantic-text_bit-vector-tests
Mikep86 1614fc9
Don't use dot product
Mikep86 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,5 @@ | ||
| pr: 123187 | ||
| summary: Add bit vector support to semantic text | ||
| area: Vector Search | ||
| type: enhancement | ||
| issues: [] |
62 changes: 62 additions & 0 deletions
62
...src/test/java/org/elasticsearch/index/mapper/vectors/DenseVectorFieldMapperTestUtils.java
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,62 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.index.mapper.vectors; | ||
|
|
||
| import com.carrotsearch.randomizedtesting.RandomizedContext; | ||
| import com.carrotsearch.randomizedtesting.generators.RandomNumbers; | ||
|
|
||
| import org.elasticsearch.inference.SimilarityMeasure; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Random; | ||
|
|
||
| public class DenseVectorFieldMapperTestUtils { | ||
| private DenseVectorFieldMapperTestUtils() {} | ||
|
|
||
| public static List<SimilarityMeasure> getSupportedSimilarities(DenseVectorFieldMapper.ElementType elementType) { | ||
| return switch (elementType) { | ||
| case FLOAT, BYTE -> List.of(SimilarityMeasure.values()); | ||
| case BIT -> List.of(SimilarityMeasure.L2_NORM); | ||
| }; | ||
| } | ||
|
|
||
| public static int getEmbeddingLength(DenseVectorFieldMapper.ElementType elementType, int dimensions) { | ||
| return switch (elementType) { | ||
| case FLOAT, BYTE -> dimensions; | ||
| case BIT -> { | ||
| assert dimensions % Byte.SIZE == 0; | ||
| yield dimensions / Byte.SIZE; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| public static int randomCompatibleDimensions(DenseVectorFieldMapper.ElementType elementType, int max) { | ||
| if (max < 1) { | ||
| throw new IllegalArgumentException("max must be at least 1"); | ||
| } | ||
|
|
||
| return switch (elementType) { | ||
| case FLOAT, BYTE -> RandomNumbers.randomIntBetween(random(), 1, max); | ||
| case BIT -> { | ||
| if (max < 8) { | ||
| throw new IllegalArgumentException("max must be at least 8 for bit vectors"); | ||
| } | ||
|
|
||
| // Generate a random dimension count that is a multiple of 8 | ||
| int maxEmbeddingLength = max / 8; | ||
| yield RandomNumbers.randomIntBetween(random(), 1, maxEmbeddingLength) * 8; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| private static Random random() { | ||
| return RandomizedContext.current().getRandom(); | ||
| } | ||
| } |
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 |
|---|---|---|
|
|
@@ -33,13 +33,11 @@ | |
| import org.elasticsearch.common.xcontent.support.XContentMapValues; | ||
| import org.elasticsearch.index.IndexVersion; | ||
| import org.elasticsearch.index.mapper.InferenceMetadataFieldsMapper; | ||
| import org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper; | ||
| import org.elasticsearch.index.shard.ShardId; | ||
| import org.elasticsearch.inference.ChunkedInference; | ||
| import org.elasticsearch.inference.InferenceService; | ||
| import org.elasticsearch.inference.InferenceServiceRegistry; | ||
| import org.elasticsearch.inference.Model; | ||
| import org.elasticsearch.inference.SimilarityMeasure; | ||
| import org.elasticsearch.inference.TaskType; | ||
| import org.elasticsearch.inference.UnparsedModel; | ||
| import org.elasticsearch.license.MockLicenseState; | ||
|
|
@@ -650,7 +648,7 @@ private static class StaticModel extends TestModel { | |
| } | ||
|
|
||
| public static StaticModel createRandomInstance() { | ||
| TestModel testModel = randomModel(randomFrom(TaskType.TEXT_EMBEDDING, TaskType.SPARSE_EMBEDDING)); | ||
| TestModel testModel = TestModel.createRandomInstance(); | ||
|
Member
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. Nice test cleanup here 👍 |
||
| return new StaticModel( | ||
| testModel.getInferenceEntityId(), | ||
| testModel.getTaskType(), | ||
|
|
@@ -673,18 +671,4 @@ boolean hasResult(String text) { | |
| return resultMap.containsKey(text); | ||
| } | ||
| } | ||
|
|
||
| private static TestModel randomModel(TaskType taskType) { | ||
| var dimensions = taskType == TaskType.TEXT_EMBEDDING ? randomIntBetween(2, 64) : null; | ||
| var similarity = taskType == TaskType.TEXT_EMBEDDING ? randomFrom(SimilarityMeasure.values()) : null; | ||
| var elementType = taskType == TaskType.TEXT_EMBEDDING ? DenseVectorFieldMapper.ElementType.FLOAT : null; | ||
| return new TestModel( | ||
| randomAlphaOfLength(4), | ||
| taskType, | ||
| randomAlphaOfLength(10), | ||
| new TestModel.TestServiceSettings(randomAlphaOfLength(4), dimensions, similarity, elementType), | ||
| new TestModel.TestTaskSettings(randomInt(3)), | ||
| new TestModel.TestSecretSettings(randomAlphaOfLength(4)) | ||
| ); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.