Skip to content

[8.19] [EIS] Implement chunked & batched inference for sparse text embeddings (#129922) #129958

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 2 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.inference.ChunkedInference;
import org.elasticsearch.inference.ChunkingSettings;
import org.elasticsearch.inference.EmptySecretSettings;
import org.elasticsearch.inference.EmptyTaskSettings;
import org.elasticsearch.inference.InferenceServiceConfiguration;
Expand All @@ -36,6 +37,8 @@
import org.elasticsearch.xpack.core.inference.results.ChunkedInferenceError;
import org.elasticsearch.xpack.core.inference.results.SparseEmbeddingResults;
import org.elasticsearch.xpack.core.ml.inference.results.ErrorInferenceResults;
import org.elasticsearch.xpack.inference.chunking.ChunkingSettingsBuilder;
import org.elasticsearch.xpack.inference.chunking.EmbeddingRequestChunker;
import org.elasticsearch.xpack.inference.external.action.SenderExecutableAction;
import org.elasticsearch.xpack.inference.external.http.sender.EmbeddingsInput;
import org.elasticsearch.xpack.inference.external.http.sender.HttpRequestSender;
Expand Down Expand Up @@ -68,6 +71,7 @@
import static org.elasticsearch.xpack.inference.services.ServiceFields.MODEL_ID;
import static org.elasticsearch.xpack.inference.services.ServiceUtils.createInvalidModelException;
import static org.elasticsearch.xpack.inference.services.ServiceUtils.parsePersistedConfigErrorMsg;
import static org.elasticsearch.xpack.inference.services.ServiceUtils.removeFromMap;
import static org.elasticsearch.xpack.inference.services.ServiceUtils.removeFromMapOrDefaultEmpty;
import static org.elasticsearch.xpack.inference.services.ServiceUtils.removeFromMapOrThrowIfNull;
import static org.elasticsearch.xpack.inference.services.ServiceUtils.throwIfNotEmptyMap;
Expand All @@ -77,6 +81,7 @@ public class ElasticInferenceService extends SenderService {

public static final String NAME = "elastic";
public static final String ELASTIC_INFERENCE_SERVICE_IDENTIFIER = "Elastic Inference Service";
public static final int SPARSE_TEXT_EMBEDDING_MAX_BATCH_SIZE = 512;

private static final EnumSet<TaskType> IMPLEMENTED_TASK_TYPES = EnumSet.of(
TaskType.SPARSE_EMBEDDING,
Expand Down Expand Up @@ -154,7 +159,8 @@ private static Map<String, DefaultModelConfig> initDefaultEndpoints(
new ElasticInferenceServiceSparseEmbeddingsServiceSettings(DEFAULT_ELSER_MODEL_ID_V2, null, null),
EmptyTaskSettings.INSTANCE,
EmptySecretSettings.INSTANCE,
elasticInferenceServiceComponents
elasticInferenceServiceComponents,
ChunkingSettingsBuilder.DEFAULT_SETTINGS
),
MinimalServiceSettings.sparseEmbedding(NAME)
)
Expand Down Expand Up @@ -284,12 +290,25 @@ protected void doChunkedInfer(
TimeValue timeout,
ActionListener<List<ChunkedInference>> listener
) {
// Pass-through without actually performing chunking (result will have a single chunk per input)
ActionListener<InferenceServiceResults> inferListener = listener.delegateFailureAndWrap(
(delegate, response) -> delegate.onResponse(translateToChunkedResults(inputs, response))
);
if (model instanceof ElasticInferenceServiceSparseEmbeddingsModel sparseTextEmbeddingsModel) {
var actionCreator = new ElasticInferenceServiceActionCreator(getSender(), getServiceComponents(), getCurrentTraceInfo());

List<EmbeddingRequestChunker.BatchRequestAndListener> batchedRequests = new EmbeddingRequestChunker<>(
inputs.getInputs(),
SPARSE_TEXT_EMBEDDING_MAX_BATCH_SIZE,
model.getConfigurations().getChunkingSettings()
).batchRequestsWithListeners(listener);

for (var request : batchedRequests) {
var action = sparseTextEmbeddingsModel.accept(actionCreator, taskSettings);
action.execute(EmbeddingsInput.fromStrings(request.batch().inputs().get(), inputType), timeout, request.listener());
}

return;
}

doInfer(model, inputs, taskSettings, timeout, inferListener);
// Model cannot perform chunked inference
listener.onFailure(createInvalidModelException(model));
}

@Override
Expand All @@ -308,6 +327,13 @@ public void parseRequestConfig(
Map<String, Object> serviceSettingsMap = removeFromMapOrThrowIfNull(config, ModelConfigurations.SERVICE_SETTINGS);
Map<String, Object> taskSettingsMap = removeFromMapOrDefaultEmpty(config, ModelConfigurations.TASK_SETTINGS);

ChunkingSettings chunkingSettings = null;
if (TaskType.SPARSE_EMBEDDING.equals(taskType)) {
chunkingSettings = ChunkingSettingsBuilder.fromMap(
removeFromMapOrDefaultEmpty(config, ModelConfigurations.CHUNKING_SETTINGS)
);
}

ElasticInferenceServiceModel model = createModel(
inferenceEntityId,
taskType,
Expand All @@ -316,7 +342,8 @@ public void parseRequestConfig(
serviceSettingsMap,
elasticInferenceServiceComponents,
TaskType.unsupportedTaskTypeErrorMsg(taskType, NAME),
ConfigurationParseContext.REQUEST
ConfigurationParseContext.REQUEST,
chunkingSettings
);

throwIfNotEmptyMap(config, NAME);
Expand Down Expand Up @@ -352,7 +379,8 @@ private static ElasticInferenceServiceModel createModel(
@Nullable Map<String, Object> secretSettings,
ElasticInferenceServiceComponents elasticInferenceServiceComponents,
String failureMessage,
ConfigurationParseContext context
ConfigurationParseContext context,
ChunkingSettings chunkingSettings
) {
return switch (taskType) {
case SPARSE_EMBEDDING -> new ElasticInferenceServiceSparseEmbeddingsModel(
Expand All @@ -363,7 +391,8 @@ private static ElasticInferenceServiceModel createModel(
taskSettings,
secretSettings,
elasticInferenceServiceComponents,
context
context,
chunkingSettings
);
case CHAT_COMPLETION -> new ElasticInferenceServiceCompletionModel(
inferenceEntityId,
Expand Down Expand Up @@ -400,13 +429,19 @@ public Model parsePersistedConfigWithSecrets(
Map<String, Object> taskSettingsMap = removeFromMapOrDefaultEmpty(config, ModelConfigurations.TASK_SETTINGS);
Map<String, Object> secretSettingsMap = removeFromMapOrDefaultEmpty(secrets, ModelSecrets.SECRET_SETTINGS);

ChunkingSettings chunkingSettings = null;
if (TaskType.SPARSE_EMBEDDING.equals(taskType)) {
chunkingSettings = ChunkingSettingsBuilder.fromMap(removeFromMap(config, ModelConfigurations.CHUNKING_SETTINGS));
}

return createModelFromPersistent(
inferenceEntityId,
taskType,
serviceSettingsMap,
taskSettingsMap,
secretSettingsMap,
parsePersistedConfigErrorMsg(inferenceEntityId, NAME)
parsePersistedConfigErrorMsg(inferenceEntityId, NAME),
chunkingSettings
);
}

Expand All @@ -415,13 +450,19 @@ public Model parsePersistedConfig(String inferenceEntityId, TaskType taskType, M
Map<String, Object> serviceSettingsMap = removeFromMapOrThrowIfNull(config, ModelConfigurations.SERVICE_SETTINGS);
Map<String, Object> taskSettingsMap = removeFromMapOrDefaultEmpty(config, ModelConfigurations.TASK_SETTINGS);

ChunkingSettings chunkingSettings = null;
if (TaskType.SPARSE_EMBEDDING.equals(taskType)) {
chunkingSettings = ChunkingSettingsBuilder.fromMap(removeFromMap(config, ModelConfigurations.CHUNKING_SETTINGS));
}

return createModelFromPersistent(
inferenceEntityId,
taskType,
serviceSettingsMap,
taskSettingsMap,
null,
parsePersistedConfigErrorMsg(inferenceEntityId, NAME)
parsePersistedConfigErrorMsg(inferenceEntityId, NAME),
chunkingSettings
);
}

Expand All @@ -436,7 +477,8 @@ private ElasticInferenceServiceModel createModelFromPersistent(
Map<String, Object> serviceSettings,
Map<String, Object> taskSettings,
@Nullable Map<String, Object> secretSettings,
String failureMessage
String failureMessage,
ChunkingSettings chunkingSettings
) {
return createModel(
inferenceEntityId,
Expand All @@ -446,7 +488,8 @@ private ElasticInferenceServiceModel createModelFromPersistent(
secretSettings,
elasticInferenceServiceComponents,
failureMessage,
ConfigurationParseContext.PERSISTENT
ConfigurationParseContext.PERSISTENT,
chunkingSettings
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.inference.ChunkingSettings;
import org.elasticsearch.inference.EmptySecretSettings;
import org.elasticsearch.inference.EmptyTaskSettings;
import org.elasticsearch.inference.ModelConfigurations;
Expand Down Expand Up @@ -37,7 +38,8 @@ public ElasticInferenceServiceSparseEmbeddingsModel(
Map<String, Object> taskSettings,
Map<String, Object> secrets,
ElasticInferenceServiceComponents elasticInferenceServiceComponents,
ConfigurationParseContext context
ConfigurationParseContext context,
ChunkingSettings chunkingSettings
) {
this(
inferenceEntityId,
Expand All @@ -46,7 +48,8 @@ public ElasticInferenceServiceSparseEmbeddingsModel(
ElasticInferenceServiceSparseEmbeddingsServiceSettings.fromMap(serviceSettings, context),
EmptyTaskSettings.INSTANCE,
EmptySecretSettings.INSTANCE,
elasticInferenceServiceComponents
elasticInferenceServiceComponents,
chunkingSettings
);
}

Expand All @@ -65,10 +68,11 @@ public ElasticInferenceServiceSparseEmbeddingsModel(
ElasticInferenceServiceSparseEmbeddingsServiceSettings serviceSettings,
@Nullable TaskSettings taskSettings,
@Nullable SecretSettings secretSettings,
ElasticInferenceServiceComponents elasticInferenceServiceComponents
ElasticInferenceServiceComponents elasticInferenceServiceComponents,
ChunkingSettings chunkingSettings
) {
super(
new ModelConfigurations(inferenceEntityId, taskType, service, serviceSettings, taskSettings),
new ModelConfigurations(inferenceEntityId, taskType, service, serviceSettings, taskSettings, chunkingSettings),
new ModelSecrets(secretSettings),
serviceSettings,
elasticInferenceServiceComponents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.elasticsearch.inference.EmptyTaskSettings;
import org.elasticsearch.inference.TaskType;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.inference.chunking.ChunkingSettingsBuilder;

public class ElasticInferenceServiceSparseEmbeddingsModelTests extends ESTestCase {

Expand All @@ -26,7 +27,8 @@ public static ElasticInferenceServiceSparseEmbeddingsModel createModel(String ur
new ElasticInferenceServiceSparseEmbeddingsServiceSettings(modelId, maxInputTokens, null),
EmptyTaskSettings.INSTANCE,
EmptySecretSettings.INSTANCE,
ElasticInferenceServiceComponents.of(url)
ElasticInferenceServiceComponents.of(url),
ChunkingSettingsBuilder.DEFAULT_SETTINGS
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ public void testUnifiedCompletionInfer_PropagatesProductUseCaseHeader() throws I
}
}

public void testChunkedInfer_PassesThrough() throws IOException {
public void testChunkedInfer() throws IOException {
var senderFactory = HttpRequestSenderTests.createSenderFactory(threadPool, clientManager);
var elasticInferenceServiceURL = getUrl(webServer);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.elasticsearch.test.ESSingleNodeTestCase;
import org.elasticsearch.xpack.inference.LocalStateInferencePlugin;
import org.elasticsearch.xpack.inference.Utils;
import org.elasticsearch.xpack.inference.chunking.ChunkingSettingsBuilder;
import org.elasticsearch.xpack.inference.external.http.sender.Sender;
import org.elasticsearch.xpack.inference.registry.ModelRegistry;
import org.elasticsearch.xpack.inference.services.elastic.DefaultModelConfig;
Expand Down Expand Up @@ -196,7 +197,8 @@ private static Map<String, DefaultModelConfig> initDefaultEndpoints() {
new ElasticInferenceServiceSparseEmbeddingsServiceSettings("elser-v2", null, null),
EmptyTaskSettings.INSTANCE,
EmptySecretSettings.INSTANCE,
ElasticInferenceServiceComponents.EMPTY_INSTANCE
ElasticInferenceServiceComponents.EMPTY_INSTANCE,
ChunkingSettingsBuilder.DEFAULT_SETTINGS
),
MinimalServiceSettings.sparseEmbedding(ElasticInferenceService.NAME)
)
Expand Down