-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract an abstract model for sparse and dense sentence transformer t…
…ranslator Signed-off-by: xinyual <[email protected]>
- Loading branch information
Showing
3 changed files
with
66 additions
and
73 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
...thms/src/main/java/org/opensearch/ml/engine/algorithms/SentenceTransformerTranslator.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,62 @@ | ||
package org.opensearch.ml.engine.algorithms; | ||
|
||
import ai.djl.huggingface.tokenizers.Encoding; | ||
import ai.djl.huggingface.tokenizers.HuggingFaceTokenizer; | ||
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.ndarray.types.DataType; | ||
import ai.djl.translate.Batchifier; | ||
import ai.djl.translate.ServingTranslator; | ||
import ai.djl.translate.TranslatorContext; | ||
import org.opensearch.ml.common.output.model.MLResultDataType; | ||
import org.opensearch.ml.common.output.model.ModelTensor; | ||
import org.opensearch.ml.common.output.model.ModelTensors; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
abstract public class SentenceTransformerTranslator implements ServingTranslator { | ||
protected HuggingFaceTokenizer tokenizer; | ||
|
||
@Override | ||
public Batchifier getBatchifier() { | ||
return Batchifier.STACK; | ||
} | ||
@Override | ||
public void prepare(TranslatorContext ctx) throws IOException { | ||
Path path = ctx.getModel().getModelPath(); | ||
tokenizer = HuggingFaceTokenizer.builder().optPadding(true).optTokenizerPath(path.resolve("tokenizer.json")).build(); | ||
} | ||
|
||
@Override | ||
public NDList processInput(TranslatorContext ctx, Input input) { | ||
String sentence = input.getAsString(0); | ||
NDManager manager = ctx.getNDManager(); | ||
NDList ndList = new NDList(); | ||
Encoding encodings = tokenizer.encode(sentence); | ||
long[] indices = encodings.getIds(); | ||
long[] attentionMask = encodings.getAttentionMask(); | ||
|
||
NDArray indicesArray = manager.create(indices); | ||
indicesArray.setName("input1.input_ids"); | ||
|
||
NDArray attentionMaskArray = manager.create(attentionMask); | ||
attentionMaskArray.setName("input1.attention_mask"); | ||
|
||
ndList.add(indicesArray); | ||
ndList.add(attentionMaskArray); | ||
return ndList; | ||
} | ||
|
||
@Override | ||
public void setArguments(Map<String, ?> arguments) { | ||
} | ||
} |
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