-
Notifications
You must be signed in to change notification settings - Fork 25.8k
[ML] Fix timeout ingesting an empty string into a semantic_text field #117840
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 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3c27cc1
Sentence chunker should return 1 input for an empty string
davidkyle d217607
Handle empty case
davidkyle 9513926
Update docs/changelog/117840.yaml
davidkyle 0ab1ca8
handle inputs that do not chunk
davidkyle a4edf41
Merge branch 'main' into empty-input
davidkyle 78ea3e7
Merge branch 'main' into empty-input
davidkyle 71c1012
fix the tests
davidkyle 2a98904
Merge branch 'main' into empty-input
davidkyle 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: 117840 | ||
| summary: Fix timeout ingesting an empty string into a `semantic_text` field | ||
| area: Machine Learning | ||
| type: bug | ||
| issues: [] |
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 |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| import org.elasticsearch.xpack.core.inference.results.InferenceTextEmbeddingFloatResults; | ||
| import org.elasticsearch.xpack.core.inference.results.SparseEmbeddingResults; | ||
| import org.elasticsearch.xpack.core.ml.search.WeightedToken; | ||
| import org.hamcrest.Matchers; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
@@ -31,16 +32,62 @@ | |
|
|
||
| public class EmbeddingRequestChunkerTests extends ESTestCase { | ||
|
|
||
| public void testEmptyInput() { | ||
| public void testEmptyInput_WordChunker() { | ||
| var embeddingType = randomFrom(EmbeddingRequestChunker.EmbeddingType.values()); | ||
| var batches = new EmbeddingRequestChunker(List.of(), 100, 100, 10, embeddingType).batchRequestsWithListeners(testListener()); | ||
| assertThat(batches, empty()); | ||
| } | ||
|
|
||
| public void testBlankInput() { | ||
| public void testEmptyInput_SentenceChunker() { | ||
| var embeddingType = randomFrom(EmbeddingRequestChunker.EmbeddingType.values()); | ||
| var batches = new EmbeddingRequestChunker(List.of(), 10, embeddingType, new SentenceBoundaryChunkingSettings(250, 1)) | ||
| .batchRequestsWithListeners(testListener()); | ||
| assertThat(batches, empty()); | ||
| } | ||
|
|
||
| public void testWhitespaceInput_SentenceChunker() { | ||
|
Contributor
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. Do we need a test for whitespace input for the word chunker?
Member
Author
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. Covered by an existing test |
||
| var embeddingType = randomFrom(EmbeddingRequestChunker.EmbeddingType.values()); | ||
| var batches = new EmbeddingRequestChunker(List.of(" "), 10, embeddingType, new SentenceBoundaryChunkingSettings(250, 1)) | ||
| .batchRequestsWithListeners(testListener()); | ||
| assertThat(batches, hasSize(1)); | ||
| assertThat(batches.get(0).batch().inputs(), hasSize(1)); | ||
| assertThat(batches.get(0).batch().inputs().get(0), Matchers.is(" ")); | ||
| } | ||
|
|
||
| public void testBlankInput_WordChunker() { | ||
| var embeddingType = randomFrom(EmbeddingRequestChunker.EmbeddingType.values()); | ||
| var batches = new EmbeddingRequestChunker(List.of(""), 100, 100, 10, embeddingType).batchRequestsWithListeners(testListener()); | ||
| assertThat(batches, hasSize(1)); | ||
| assertThat(batches.get(0).batch().inputs(), hasSize(1)); | ||
| assertThat(batches.get(0).batch().inputs().get(0), Matchers.is("")); | ||
| } | ||
|
|
||
| public void testBlankInput_SentenceChunker() { | ||
| var embeddingType = randomFrom(EmbeddingRequestChunker.EmbeddingType.values()); | ||
| var batches = new EmbeddingRequestChunker(List.of(""), 10, embeddingType, new SentenceBoundaryChunkingSettings(250, 1)) | ||
| .batchRequestsWithListeners(testListener()); | ||
| assertThat(batches, hasSize(1)); | ||
| assertThat(batches.get(0).batch().inputs(), hasSize(1)); | ||
| assertThat(batches.get(0).batch().inputs().get(0), Matchers.is("")); | ||
| } | ||
|
|
||
| public void testInputThatDoesNotChunk_WordChunker() { | ||
| var embeddingType = randomFrom(EmbeddingRequestChunker.EmbeddingType.values()); | ||
| var batches = new EmbeddingRequestChunker(List.of("ABBAABBA"), 100, 100, 10, embeddingType).batchRequestsWithListeners( | ||
| testListener() | ||
| ); | ||
| assertThat(batches, hasSize(1)); | ||
| assertThat(batches.get(0).batch().inputs(), hasSize(1)); | ||
| assertThat(batches.get(0).batch().inputs().get(0), Matchers.is("ABBAABBA")); | ||
| } | ||
|
|
||
| public void testInputThatDoesNotChunk_SentenceChunker() { | ||
| var embeddingType = randomFrom(EmbeddingRequestChunker.EmbeddingType.values()); | ||
| var batches = new EmbeddingRequestChunker(List.of("ABBAABBA"), 10, embeddingType, new SentenceBoundaryChunkingSettings(250, 1)) | ||
| .batchRequestsWithListeners(testListener()); | ||
| assertThat(batches, hasSize(1)); | ||
| assertThat(batches.get(0).batch().inputs(), hasSize(1)); | ||
| assertThat(batches.get(0).batch().inputs().get(0), Matchers.is("ABBAABBA")); | ||
| } | ||
|
|
||
| public void testShortInputsAreSingleBatch() { | ||
|
|
||
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
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.
Do we need to trim the input before checking if it's empty? How does the chunker handle input that is only whitespace?
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.
Good catch @Mikep86 thanks.
There's a whole class of bugs for "things that don't chunk". Pure whitespace, a single character or the same character repeated thousands of times don't chunk. I've added test cases for these situations and the solution I've implemented here in this is to return the original input if it did not chunk. This applies to both the Word and Sentence chunker.
This makes me wonder if there should be an upper limit on the chunk size in terms of number of characters. A badly formed input contain latin characters but no whitespace would result in a single large chunk, think a binary file base64 encoded.