-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Bhavana Ramaram <[email protected]>
- Loading branch information
Showing
10 changed files
with
492 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
75 changes: 75 additions & 0 deletions
75
...n/java/org/opensearch/ml/engine/algorithms/question_answering/QuestionAnsweringModel.java
This file contains 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,75 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.engine.algorithms.question_answering; | ||
|
||
import static org.opensearch.ml.engine.ModelHelper.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.opensearch.ml.common.FunctionName; | ||
import org.opensearch.ml.common.dataset.MLInputDataset; | ||
import org.opensearch.ml.common.dataset.TextDocsInputDataSet; | ||
import org.opensearch.ml.common.input.MLInput; | ||
import org.opensearch.ml.common.model.MLModelConfig; | ||
import org.opensearch.ml.common.output.model.ModelResultFilter; | ||
import org.opensearch.ml.common.output.model.ModelTensorOutput; | ||
import org.opensearch.ml.common.output.model.ModelTensors; | ||
import org.opensearch.ml.engine.algorithms.DLModel; | ||
import org.opensearch.ml.engine.annotation.Function; | ||
|
||
import ai.djl.inference.Predictor; | ||
import ai.djl.modality.Input; | ||
import ai.djl.modality.Output; | ||
import ai.djl.translate.TranslateException; | ||
import ai.djl.translate.Translator; | ||
import ai.djl.translate.TranslatorFactory; | ||
import lombok.extern.log4j.Log4j2; | ||
|
||
@Log4j2 | ||
@Function(FunctionName.QUESTION_ANSWERING) | ||
public class QuestionAnsweringModel extends DLModel { | ||
|
||
@Override | ||
public void warmUp(Predictor predictor, String modelId, MLModelConfig modelConfig) throws TranslateException { | ||
String question = "How is the weather?"; | ||
String context = "The weather is nice, it is beautiful day."; | ||
Input input = new Input(); | ||
input.add(question); | ||
input.add(context); | ||
|
||
// First request takes longer time. Predict once to warm up model. | ||
predictor.predict(input); | ||
} | ||
|
||
@Override | ||
public ModelTensorOutput predict(String modelId, MLInput mlInput) throws TranslateException { | ||
MLInputDataset inputDataSet = mlInput.getInputDataset(); | ||
List<ModelTensors> tensorOutputs = new ArrayList<>(); | ||
Output output; | ||
TextDocsInputDataSet textDocsInput = (TextDocsInputDataSet) inputDataSet; | ||
ModelResultFilter resultFilter = textDocsInput.getResultFilter(); | ||
String question = textDocsInput.getDocs().get(0); | ||
String context = textDocsInput.getDocs().get(1); | ||
Input input = new Input(); | ||
input.add(question); | ||
input.add(context); | ||
output = getPredictor().predict(input); | ||
tensorOutputs.add(parseModelTensorOutput(output, resultFilter)); | ||
return new ModelTensorOutput(tensorOutputs); | ||
} | ||
|
||
@Override | ||
public Translator<Input, Output> getTranslator(String engine, MLModelConfig modelConfig) throws IllegalArgumentException { | ||
return new QuestionAnsweringTranslator(); | ||
} | ||
|
||
@Override | ||
public TranslatorFactory getTranslatorFactory(String engine, MLModelConfig modelConfig) { | ||
return null; | ||
} | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
...a/org/opensearch/ml/engine/algorithms/question_answering/QuestionAnsweringTranslator.java
This file contains 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,77 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.engine.algorithms.question_answering; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.opensearch.ml.common.output.model.ModelTensor; | ||
import org.opensearch.ml.common.output.model.ModelTensors; | ||
import org.opensearch.ml.engine.algorithms.SentenceTransformerTranslator; | ||
|
||
import ai.djl.huggingface.tokenizers.Encoding; | ||
import ai.djl.modality.Input; | ||
import ai.djl.modality.Output; | ||
import ai.djl.ndarray.NDArray; | ||
import ai.djl.ndarray.NDList; | ||
import ai.djl.ndarray.NDManager; | ||
import ai.djl.translate.TranslatorContext; | ||
|
||
public class QuestionAnsweringTranslator extends SentenceTransformerTranslator { | ||
// private static final int[] AXIS = {0}; | ||
private List<String> tokens; | ||
|
||
@Override | ||
public NDList processInput(TranslatorContext ctx, Input input) { | ||
NDManager manager = ctx.getNDManager(); | ||
String question = input.getAsString(0); | ||
String paragraph = input.getAsString(1); | ||
NDList ndList = new NDList(); | ||
|
||
Encoding encodings = tokenizer.encode(question, paragraph); | ||
tokens = Arrays.asList(encodings.getTokens()); | ||
ctx.setAttachment("encoding", encodings); | ||
long[] indices = encodings.getIds(); | ||
long[] attentionMask = encodings.getAttentionMask(); | ||
|
||
NDArray indicesArray = manager.create(indices); | ||
indicesArray.setName("input_ids"); | ||
|
||
NDArray attentionMaskArray = manager.create(attentionMask); | ||
attentionMaskArray.setName("attention_mask"); | ||
|
||
ndList.add(indicesArray); | ||
ndList.add(attentionMaskArray); | ||
return ndList; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public Output processOutput(TranslatorContext ctx, NDList list) { | ||
Output output = new Output(200, "OK"); | ||
|
||
List<ModelTensor> outputs = new ArrayList<>(); | ||
|
||
NDArray startLogits = list.get(0); | ||
NDArray endLogits = list.get(1); | ||
int startIdx = (int) startLogits.argMax().getLong(); | ||
int endIdx = (int) endLogits.argMax().getLong(); | ||
if (startIdx >= endIdx) { | ||
int tmp = startIdx; | ||
startIdx = endIdx; | ||
endIdx = tmp; | ||
} | ||
String answer = tokenizer.buildSentence(tokens.subList(startIdx, endIdx + 1)); | ||
|
||
outputs.add(new ModelTensor("answer", answer)); | ||
|
||
ModelTensors modelTensorOutput = new ModelTensors(outputs); | ||
output.add(modelTensorOutput.toBytes()); | ||
return output; | ||
} | ||
|
||
} |
Oops, something went wrong.