-
Notifications
You must be signed in to change notification settings - Fork 25.5k
[ML] Support sparse embedding models in the elasticsearch inference service #112270
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
3 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,5 @@ | ||
pr: 112270 | ||
summary: Support sparse embedding models in the elasticsearch inference service | ||
area: Machine Learning | ||
type: enhancement | ||
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
134 changes: 134 additions & 0 deletions
134
...ice-tests/src/javaRestTest/java/org/elasticsearch/xpack/inference/CustomElandModelIT.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,134 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.inference; | ||
|
||
import org.elasticsearch.client.Request; | ||
import org.elasticsearch.core.Strings; | ||
import org.elasticsearch.inference.TaskType; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Base64; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class CustomElandModelIT extends InferenceBaseRestTest { | ||
|
||
// The model definition is taken from org.elasticsearch.xpack.ml.integration.TextExpansionQueryIT | ||
|
||
static final String BASE_64_ENCODED_MODEL = "UEsDBAAACAgAAAAAAAAAAAAAAAAAA" | ||
+ "AAAAAAUAA4Ac2ltcGxlbW9kZWwvZGF0YS5wa2xGQgoAWlpaWlpaWlpaWoACY19fdG9yY2hfXwpUaW55VG" | ||
+ "V4dEV4cGFuc2lvbgpxACmBfShYCAAAAHRyYWluaW5ncQGJWBYAAABfaXNfZnVsbF9iYWNrd2FyZF9ob29" | ||
+ "rcQJOdWJxAy5QSwcIITmbsFgAAABYAAAAUEsDBBQACAgIAAAAAAAAAAAAAAAAAAAAAAAdAB0Ac2ltcGxl" | ||
+ "bW9kZWwvY29kZS9fX3RvcmNoX18ucHlGQhkAWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWoWRT4+cMAzF7" | ||
+ "/spfASJomF3e0Ga3nrrn8vcELIyxAzRhAQlpjvbT19DWDrdquqBA/bvPT87nVUxwsm41xPd+PNtUi4a77" | ||
+ "KvXs+W8voBAHFSQY3EFCIiHKFp1+p57vs/ShyUccZdoIaz93aBTMR+thbPqru+qKBx8P4q/e8TyxRlmwVc" | ||
+ "tJp66H1YmCyS7WsZwD50A2L5V7pCBADGTTOj0bGGE7noQyqzv5JDfp0o9fZRCWqP37yjhE4+mqX5X3AdF" | ||
+ "ZHGM/2TzOHDpy1IvQWR+OWo3KwsRiKdpcqg4pBFDtm+QJ7nqwIPckrlnGfFJG0uNhOl38Sjut3pCqg26Qu" | ||
+ "Zy8BR9In7ScHHrKkKMW0TIucFrGQXCMpdaDO05O6DpOiy8e4kr0Ed/2YKOIhplW8gPr4ntygrd9ixpx3j9" | ||
+ "UZZVRagl2c6+imWUzBjuf5m+Ch7afphuvvW+r/0dsfn+2N9MZGb9+/SFtCYdhd83CMYp+mGy0LiKNs8y/e" | ||
+ "UuEA8B/d2z4dfUEsHCFSE3Ia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JcUxobHDUqQ8bIOW79BVBLBwhfSmPKdAIAAE4EAABQSwMEAAAICAAAAAAAAAAAAAAAAAAAAAAAA" | ||
+ "BkABQBzaW1wbGVtb2RlbC9jb25zdGFudHMucGtsRkIBAFqAAikuUEsHCG0vCVcEAAAABAAAAFBLAwQAAAgI" | ||
+ "AAAAAAAAAAAAAAAAAAAAAAAAEwA7AHNpbXBsZW1vZGVsL3ZlcnNpb25GQjcAWlpaWlpaWlpaWlpaWlpaWlp" | ||
+ "aWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWjMKUEsHCNGeZ1UCAAAAAgAAAFBLAQIAAA" | ||
+ "AACAgAAAAAAAAhOZuwWAAAAFgAAAAUAAAAAAAAAAAAAAAAAAAAAABzaW1wbGVtb2RlbC9kYXRhLnBrbFBLA" | ||
+ "QIAABQACAgIAAAAAABUhNyGggEAACADAAAdAAAAAAAAAAAAAAAAAKgAAABzaW1wbGVtb2RlbC9jb2RlL19f" | ||
+ "dG9yY2hfXy5weVBLAQIAABQACAgIAAAAAABfSmPKdAIAAE4EAAAnAAAAAAAAAAAAAAAAAJICAABzaW1wbGVt" | ||
+ "b2RlbC9jb2RlL19fdG9yY2hfXy5weS5kZWJ1Z19wa2xQSwECAAAAAAgIAAAAAAAAbS8JVwQAAAAEAAAAGQAA" | ||
+ "AAAAAAAAAAAAAACEBQAAc2ltcGxlbW9kZWwvY29uc3RhbnRzLnBrbFBLAQIAAAAACAgAAAAAAADRnmdVAgAA" | ||
+ "AAIAAAATAAAAAAAAAAAAAAAAANQFAABzaW1wbGVtb2RlbC92ZXJzaW9uUEsGBiwAAAAAAAAAHgMtAAAAAAAA" | ||
+ "AAAABQAAAAAAAAAFAAAAAAAAAGoBAAAAAAAAUgYAAAAAAABQSwYHAAAAALwHAAAAAAAAAQAAAFBLBQYAAAAABQAFAGoBAABSBgAAAAA="; | ||
|
||
static final long RAW_MODEL_SIZE; // size of the model before base64 encoding | ||
static { | ||
RAW_MODEL_SIZE = Base64.getDecoder().decode(BASE_64_ENCODED_MODEL).length; | ||
} | ||
|
||
// Test a sparse embedding model deployed with the ml trained models APIs | ||
public void testSparse() throws IOException { | ||
String modelId = "custom-text-expansion-model"; | ||
|
||
createTextExpansionModel(modelId); | ||
putModelDefinition(modelId, BASE_64_ENCODED_MODEL, RAW_MODEL_SIZE); | ||
putVocabulary( | ||
List.of("these", "are", "my", "words", "the", "washing", "machine", "is", "leaking", "octopus", "comforter", "smells"), | ||
modelId | ||
); | ||
|
||
var inferenceConfig = """ | ||
{ | ||
"service": "elasticsearch", | ||
"service_settings": { | ||
"model_id": "custom-text-expansion-model", | ||
"num_allocations": 1, | ||
"num_threads": 1 | ||
} | ||
} | ||
"""; | ||
|
||
var inferenceId = "sparse-inf"; | ||
putModel(inferenceId, inferenceConfig, TaskType.SPARSE_EMBEDDING); | ||
var results = inferOnMockService(inferenceId, List.of("washing", "machine")); | ||
deleteModel(inferenceId); | ||
assertNotNull(results.get("sparse_embedding")); | ||
} | ||
|
||
protected void createTextExpansionModel(String modelId) throws IOException { | ||
// with_special_tokens: false for this test with limited vocab | ||
Request request = new Request("PUT", "/_ml/trained_models/" + modelId); | ||
request.setJsonEntity(""" | ||
{ | ||
"description": "a text expansion model", | ||
"model_type": "pytorch", | ||
"inference_config": { | ||
"text_expansion": { | ||
"tokenization": { | ||
"bert": { | ||
"with_special_tokens": false | ||
} | ||
} | ||
} | ||
} | ||
}"""); | ||
client().performRequest(request); | ||
} | ||
|
||
protected void putVocabulary(List<String> vocabulary, String modelId) throws IOException { | ||
List<String> vocabularyWithPad = new ArrayList<>(); | ||
vocabularyWithPad.add("[PAD]"); | ||
vocabularyWithPad.add("[UNK]"); | ||
vocabularyWithPad.addAll(vocabulary); | ||
String quotedWords = vocabularyWithPad.stream().map(s -> "\"" + s + "\"").collect(Collectors.joining(",")); | ||
|
||
Request request = new Request("PUT", "_ml/trained_models/" + modelId + "/vocabulary"); | ||
request.setJsonEntity(Strings.format(""" | ||
{ "vocabulary": [%s] } | ||
""", quotedWords)); | ||
client().performRequest(request); | ||
} | ||
|
||
protected void putModelDefinition(String modelId, String base64EncodedModel, long unencodedModelSize) throws IOException { | ||
Request request = new Request("PUT", "_ml/trained_models/" + modelId + "/definition/0"); | ||
String body = Strings.format(""" | ||
{"total_definition_length":%s,"definition": "%s","total_parts": 1}""", unencodedModelSize, base64EncodedModel); | ||
request.setJsonEntity(body); | ||
client().performRequest(request); | ||
} | ||
} |
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
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.
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.
unfortunate