-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Test ML model server #120270
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
Test ML model server #120270
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
0651731
Fix model downloading for very small models.
jan-elastic b1497fb
Test MlModelServer
jan-elastic 572ceb0
Tiny ELSER
jan-elastic 1c4dd60
unmute TextEmbeddingCrudIT and DefaultEndPointsIT
jan-elastic 2c82aae
update ELSER
jan-elastic 07e9046
Improve MlModelServer
jan-elastic bdf776b
tiny E5
jan-elastic 022e61f
more logging
jan-elastic 2408654
improved E5 model
jan-elastic d612dd2
tiny reranker
jan-elastic e092b7f
scan for ports
jan-elastic 49d97dd
[CI] Auto commit changes from spotless
16e1424
Serve default models when optimized model is requested
jan-elastic 08517f2
@ClassRule
jan-elastic 3cc62d3
polish code
jan-elastic 10cb533
Respect dynamic setting ML model repo
jan-elastic ca60b55
fix metadata for optimized models
jan-elastic 559b99f
improve logging
jan-elastic 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
1 change: 1 addition & 0 deletions
1
x-pack/plugin/inference/qa/inference-service-tests/build.gradle
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
146 changes: 146 additions & 0 deletions
146
...-service-tests/src/javaRestTest/java/org/elasticsearch/xpack/inference/MlModelServer.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,146 @@ | ||
| /* | ||
| * 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 com.sun.net.httpserver.HttpExchange; | ||
| import com.sun.net.httpserver.HttpServer; | ||
|
|
||
| import org.apache.http.HttpHeaders; | ||
| import org.apache.http.HttpStatus; | ||
| import org.apache.http.client.utils.URIBuilder; | ||
| import org.elasticsearch.logging.LogManager; | ||
| import org.elasticsearch.logging.Logger; | ||
| import org.elasticsearch.test.fixture.HttpHeaderParser; | ||
| import org.elasticsearch.xcontent.XContentParser; | ||
| import org.elasticsearch.xcontent.XContentParserConfiguration; | ||
| import org.elasticsearch.xcontent.XContentType; | ||
| import org.elasticsearch.xpack.core.XPackSettings; | ||
| import org.elasticsearch.xpack.core.ml.inference.trainedmodel.ModelPackageConfig; | ||
| import org.junit.rules.TestRule; | ||
| import org.junit.runner.Description; | ||
| import org.junit.runners.model.Statement; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.net.InetSocketAddress; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Random; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
|
|
||
| /** | ||
| * Simple model server to serve ML models. | ||
| * The URL path corresponds to a file name in this class's resources. | ||
| * If the file is found, its content is returned, otherwise 404. | ||
| * Respects a range header to serve partial content. | ||
| */ | ||
| public class MlModelServer implements TestRule { | ||
|
|
||
| private static final String HOST = "localhost"; | ||
| private static final Logger logger = LogManager.getLogger(MlModelServer.class); | ||
|
|
||
| private int port; | ||
|
|
||
| public String getUrl() { | ||
| return new URIBuilder().setScheme("http").setHost(HOST).setPort(port).toString(); | ||
| } | ||
|
|
||
| private void handle(HttpExchange exchange) throws IOException { | ||
| String rangeHeader = exchange.getRequestHeaders().getFirst(HttpHeaders.RANGE); | ||
| HttpHeaderParser.Range range = rangeHeader != null ? HttpHeaderParser.parseRangeHeader(rangeHeader) : null; | ||
| logger.info("request: {} range={}", exchange.getRequestURI().getPath(), range); | ||
|
|
||
| try (InputStream is = getInputStream(exchange)) { | ||
| int httpStatus; | ||
| long numBytes; | ||
| if (is == null) { | ||
| httpStatus = HttpStatus.SC_NOT_FOUND; | ||
| numBytes = 0; | ||
| } else if (range == null) { | ||
| httpStatus = HttpStatus.SC_OK; | ||
| numBytes = is.available(); | ||
| } else { | ||
| httpStatus = HttpStatus.SC_PARTIAL_CONTENT; | ||
| is.skipNBytes(range.start()); | ||
| numBytes = range.end() - range.start() + 1; | ||
| } | ||
| logger.info("response: {} {}", exchange.getRequestURI().getPath(), httpStatus); | ||
| exchange.sendResponseHeaders(httpStatus, numBytes); | ||
| try (OutputStream os = exchange.getResponseBody()) { | ||
| while (numBytes > 0) { | ||
| byte[] bytes = is.readNBytes((int) Math.min(1 << 20, numBytes)); | ||
| os.write(bytes); | ||
| numBytes -= bytes.length; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private InputStream getInputStream(HttpExchange exchange) throws IOException { | ||
| String path = exchange.getRequestURI().getPath().substring(1); // Strip leading slash | ||
| String modelId = path.substring(0, path.indexOf('.')); | ||
| String extension = path.substring(path.indexOf('.') + 1); | ||
|
|
||
| // If a model specifically optimized for some platform is requested, | ||
| // serve the default non-optimized model instead, which is compatible. | ||
| String defaultModelId = modelId; | ||
| for (String platform : XPackSettings.ML_NATIVE_CODE_PLATFORMS) { | ||
| defaultModelId = defaultModelId.replace("_" + platform, ""); | ||
| } | ||
|
|
||
| ClassLoader classloader = Thread.currentThread().getContextClassLoader(); | ||
| InputStream is = classloader.getResourceAsStream(defaultModelId + "." + extension); | ||
| if (is != null && modelId.equals(defaultModelId) == false && extension.equals("metadata.json")) { | ||
| // When an optimized version is requested, fix the default metadata, | ||
| // so that it contains the correct model ID. | ||
| try (XContentParser parser = XContentType.JSON.xContent().createParser(XContentParserConfiguration.EMPTY, is.readAllBytes())) { | ||
| is.close(); | ||
| ModelPackageConfig packageConfig = ModelPackageConfig.fromXContentLenient(parser); | ||
| packageConfig = new ModelPackageConfig.Builder(packageConfig).setPackedModelId(modelId).build(); | ||
| is = new ByteArrayInputStream(packageConfig.toString().getBytes(StandardCharsets.UTF_8)); | ||
| } | ||
| } | ||
| return is; | ||
| } | ||
|
|
||
| @Override | ||
| public Statement apply(Statement statement, Description description) { | ||
| return new Statement() { | ||
| @Override | ||
| public void evaluate() throws Throwable { | ||
| logger.info("Starting ML model server"); | ||
| HttpServer server = HttpServer.create(); | ||
| while (true) { | ||
| port = new Random().nextInt(10000, 65536); | ||
| try { | ||
| server.bind(new InetSocketAddress(HOST, port), 1); | ||
| } catch (Exception e) { | ||
| continue; | ||
| } | ||
| break; | ||
| } | ||
| logger.info("Bound ML model server to port {}", port); | ||
|
|
||
| ExecutorService executor = Executors.newCachedThreadPool(); | ||
| server.setExecutor(executor); | ||
| server.createContext("/", MlModelServer.this::handle); | ||
| server.start(); | ||
|
|
||
| try { | ||
| statement.evaluate(); | ||
| } finally { | ||
| logger.info("Stopping ML model server on port {}", port); | ||
| server.stop(1); | ||
| executor.shutdown(); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
| } |
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
25 changes: 25 additions & 0 deletions
25
...ference/qa/inference-service-tests/src/javaRestTest/resources/elser_model_2.metadata.json
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,25 @@ | ||
| { | ||
| "packaged_model_id": "elser_model_2", | ||
| "minimum_version": "11.0.0", | ||
| "size": 1859242, | ||
| "sha256": "602dbccfb2746e5700bf65d8019b06fb2ec1e3c5bfb980eb2005fc17c1bfe0c0", | ||
| "description": "Elastic Learned Sparse EncodeR v2", | ||
| "model_type": "pytorch", | ||
| "tags": [ | ||
| "elastic" | ||
| ], | ||
| "inference_config": { | ||
| "text_expansion": { | ||
| "tokenization": { | ||
| "bert": { | ||
| "do_lower_case": true, | ||
| "with_special_tokens": true, | ||
| "max_sequence_length": 512, | ||
| "truncate": "first", | ||
| "span": -1 | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "vocabulary_file": "elser_model_2.vocab.json" | ||
| } |
Binary file added
BIN
+1.77 MB
...k/plugin/inference/qa/inference-service-tests/src/javaRestTest/resources/elser_model_2.pt
Binary file not shown.
1 change: 1 addition & 0 deletions
1
.../inference/qa/inference-service-tests/src/javaRestTest/resources/elser_model_2.vocab.json
Large diffs are not rendered by default.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
...qa/inference-service-tests/src/javaRestTest/resources/multilingual-e5-small.metadata.json
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,32 @@ | ||
| { | ||
| "packaged_model_id": "multilingual-e5-small", | ||
| "minimum_version": "12.0.0", | ||
| "size": 5531160, | ||
| "sha256": "92e24566eff554d3a6808cc62731dbecf32db63e01801f3f62210aa9131c7a8b", | ||
| "description": "E5 small multilingual", | ||
| "model_type": "pytorch", | ||
| "tags": [], | ||
| "inference_config": { | ||
| "text_embedding": { | ||
| "tokenization": { | ||
| "xlm_roberta": { | ||
| "do_lower_case": false, | ||
| "with_special_tokens": true, | ||
| "max_sequence_length": 512, | ||
| "truncate": "first", | ||
| "span": -1 | ||
| } | ||
| }, | ||
| "embedding_size": 384 | ||
| } | ||
| }, | ||
| "prefix_strings": { | ||
| "search": "query: ", | ||
| "ingest": "passage: " | ||
| }, | ||
| "metadata": { | ||
| "per_allocation_memory_bytes": 557785256, | ||
| "per_deployment_memory_bytes": 470031872 | ||
| }, | ||
| "vocabulary_file": "multilingual-e5-small.vocab.json" | ||
| } |
Binary file added
BIN
+5.27 MB
.../inference/qa/inference-service-tests/src/javaRestTest/resources/multilingual-e5-small.pt
Binary file not shown.
1 change: 1 addition & 0 deletions
1
...ce/qa/inference-service-tests/src/javaRestTest/resources/multilingual-e5-small.vocab.json
Large diffs are not rendered by default.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
...n/inference/qa/inference-service-tests/src/javaRestTest/resources/rerank-v1.metadata.json
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,15 @@ | ||
| { | ||
| "packaged_model_id": "rerank-v1", | ||
| "minimum_version": "9.0.0", | ||
| "size": 12419194, | ||
| "sha256": "8d37d7240175b59a1a82f409e572c4d0136acff875da980ec5e5e1783263a042", | ||
| "description": "Elastic Rerank v1", | ||
| "model_type": "pytorch", | ||
| "tags": [ | ||
| "curated" | ||
| ], | ||
| "inference_config": { | ||
| "text_similarity": {"tokenization": {"deberta_v2": {"truncate": "balanced"}}} | ||
| }, | ||
| "vocabulary_file": "rerank-v1.vocab.json" | ||
| } |
Binary file added
BIN
+11.8 MB
x-pack/plugin/inference/qa/inference-service-tests/src/javaRestTest/resources/rerank-v1.pt
Binary file not shown.
1 change: 1 addition & 0 deletions
1
...ugin/inference/qa/inference-service-tests/src/javaRestTest/resources/rerank-v1.vocab.json
Large diffs are not rendered by default.
Oops, something went wrong.
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.
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.
Need to get
XPackSettings.ML_NATIVE_CODE_PLATFORMSinto the model server