-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[ML] Add internal action to return the Rerank window size #132169
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 11 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b2d6dd2
Add internal get reranker size action
davidkyle 00c8286
test case
davidkyle 3004d7c
wip reranker interface
davidkyle d5ebe19
Add rerank to the unit tests
davidkyle 11e8aad
Integ test
davidkyle 7950536
fix the tests
davidkyle 539f468
[CI] Auto commit changes from spotless
91646ed
address review comments
davidkyle 1fd8732
per service logic
davidkyle 6d70792
Merge branch 'main' into rerank-window-size-action
davidkyle 0ba789f
Merge branch 'main' into rerank-window-size-action
davidkyle d2c3c03
Fix tests
davidkyle 545168b
action renamed
davidkyle 6c19f4f
Merge branch 'main' into rerank-window-size-action
davidkyle 1cb529b
Merge branch 'main' into rerank-window-size-action
davidkyle 07ed01a
Increase values
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
26 changes: 26 additions & 0 deletions
26
server/src/main/java/org/elasticsearch/inference/RerankingInferenceService.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,26 @@ | ||
| /* | ||
| * 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.inference; | ||
|
|
||
| public interface RerankingInferenceService { | ||
|
|
||
| /** | ||
| * The default window size for small reranking models (512 input tokens). | ||
| */ | ||
| int CONSERVATIVE_DEFAULT_WINDOW_SIZE = 250; | ||
|
|
||
| /** | ||
| * The reranking model's max window or an approximation of | ||
| * measured in the number of words. | ||
| * @param modelId The model ID | ||
| * @return Window size in words | ||
| */ | ||
| int rerankerWindowSize(String modelId); | ||
| } | ||
103 changes: 103 additions & 0 deletions
103
.../main/java/org/elasticsearch/xpack/core/inference/action/GetRerankerWindowSizeAction.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,103 @@ | ||
| /* | ||
| * 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.core.inference.action; | ||
|
|
||
| import org.elasticsearch.action.ActionRequest; | ||
| import org.elasticsearch.action.ActionRequestValidationException; | ||
| import org.elasticsearch.action.ActionResponse; | ||
| import org.elasticsearch.action.ActionType; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Objects; | ||
|
|
||
| public class GetRerankerWindowSizeAction extends ActionType<GetRerankerWindowSizeAction.Response> { | ||
|
|
||
| public static final GetRerankerWindowSizeAction INSTANCE = new GetRerankerWindowSizeAction(); | ||
| public static final String NAME = "cluster:internal/xpack/inference/rerankwindowsize/get"; | ||
|
|
||
| public GetRerankerWindowSizeAction() { | ||
| super(NAME); | ||
| } | ||
|
|
||
| public static class Request extends ActionRequest { | ||
|
|
||
| private final String inferenceEntityId; | ||
|
|
||
| public Request(String inferenceEntityId) { | ||
| this.inferenceEntityId = inferenceEntityId; | ||
| } | ||
|
|
||
| public Request(StreamInput in) throws IOException { | ||
| super(in); | ||
| this.inferenceEntityId = in.readString(); | ||
| } | ||
|
|
||
| public String getInferenceEntityId() { | ||
| return inferenceEntityId; | ||
| } | ||
|
|
||
| @Override | ||
| public void writeTo(StreamOutput out) throws IOException { | ||
| super.writeTo(out); | ||
| out.writeString(inferenceEntityId); | ||
| } | ||
|
|
||
| @Override | ||
| public ActionRequestValidationException validate() { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| Request request = (Request) o; | ||
| return Objects.equals(inferenceEntityId, request.inferenceEntityId); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hashCode(inferenceEntityId); | ||
| } | ||
| } | ||
|
|
||
| public static class Response extends ActionResponse { | ||
|
|
||
| private final int windowSize; | ||
|
|
||
| public Response(int windowSize) { | ||
| this.windowSize = windowSize; | ||
| } | ||
|
|
||
| public Response(StreamInput in) throws IOException { | ||
| this.windowSize = in.readVInt(); | ||
| } | ||
|
|
||
| public int getWindowSize() { | ||
| return windowSize; | ||
| } | ||
|
|
||
| @Override | ||
| public void writeTo(StreamOutput out) throws IOException { | ||
| out.writeVInt(windowSize); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| Response response = (Response) o; | ||
| return windowSize == response.windowSize; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hashCode(windowSize); | ||
| } | ||
| } | ||
| } |
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
55 changes: 55 additions & 0 deletions
55
...nalClusterTest/java/org/elasticsearch/xpack/inference/integration/RerankWindowSizeIT.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,55 @@ | ||
| /* | ||
| * 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.integration; | ||
|
|
||
| import org.elasticsearch.ElasticsearchStatusException; | ||
| import org.elasticsearch.plugins.Plugin; | ||
| import org.elasticsearch.test.ESIntegTestCase; | ||
| import org.elasticsearch.test.ESTestCase; | ||
| import org.elasticsearch.xpack.core.inference.action.GetRerankerWindowSizeAction; | ||
| import org.elasticsearch.xpack.inference.LocalStateInferencePlugin; | ||
| import org.elasticsearch.xpack.inference.Utils; | ||
| import org.elasticsearch.xpack.inference.mock.TestInferenceServicePlugin; | ||
| import org.elasticsearch.xpack.inference.registry.ModelRegistry; | ||
| import org.junit.Before; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.List; | ||
|
|
||
| import static org.hamcrest.Matchers.containsString; | ||
|
|
||
| @ESTestCase.WithoutEntitlements // due to dependency issue ES-12435 | ||
| public class RerankWindowSizeIT extends ESIntegTestCase { | ||
davidkyle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Before | ||
| public void setup() throws Exception { | ||
| ModelRegistry modelRegistry = internalCluster().getCurrentMasterNodeInstance(ModelRegistry.class); | ||
| Utils.storeRerankModel("rerank-endpoint", modelRegistry); | ||
| Utils.storeSparseModel("sparse-endpoint", modelRegistry); | ||
| } | ||
|
|
||
| @Override | ||
| protected Collection<Class<? extends Plugin>> nodePlugins() { | ||
| return List.of(LocalStateInferencePlugin.class, TestInferenceServicePlugin.class); | ||
| } | ||
|
|
||
| public void testRerankWindowSizeAction() { | ||
| var response = client().execute(GetRerankerWindowSizeAction.INSTANCE, new GetRerankerWindowSizeAction.Request("rerank-endpoint")) | ||
| .actionGet(); | ||
| assertEquals(333, response.getWindowSize()); | ||
| } | ||
|
|
||
| public void testActionNotAReranker() { | ||
| var e = expectThrows( | ||
| ElasticsearchStatusException.class, | ||
| () -> client().execute(GetRerankerWindowSizeAction.INSTANCE, new GetRerankerWindowSizeAction.Request("sparse-endpoint")) | ||
| .actionGet() | ||
| ); | ||
| assertThat(e.getMessage(), containsString("Inference endpoint [sparse-endpoint] does not have the rerank task type")); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.