Skip to content
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

ArcticEmbedLEncoder #2694

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
76 changes: 76 additions & 0 deletions src/main/java/io/anserini/encoder/dense/ArcticEmbedLEncoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Anserini: A Lucene toolkit for reproducible information retrieval research
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.anserini.encoder.dense;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ai.onnxruntime.OnnxTensor;
import ai.onnxruntime.OrtException;
import ai.onnxruntime.OrtSession;

public class ArcticEmbedLEncoder extends DenseEncoder {
static private final String MODEL_URL = "";

static private final String VOCAB_URL = "";

static private final String MODEL_NAME = "";

static private final String VOCAB_NAME = "";

static private final String INSTRUCTION = "Represent this sentence for searching relevant passages: ";

static private final int MAX_SEQ_LEN = 512;

public ArcticEmbedLEncoder() throws IOException, OrtException, URISyntaxException {
super(MODEL_NAME, MODEL_URL, VOCAB_NAME, VOCAB_URL);
}

@Override
public float[] encode(String query) throws OrtException {
// Keep basic tokenization for now since we know we need tokens (SPLADE does this)
List<String> queryTokens = new ArrayList<>();
queryTokens.add("[CLS]");
queryTokens.addAll(this.tokenizer.tokenize(INSTRUCTION + query));
queryTokens.add("[SEP]");

Map<String, OnnxTensor> inputs = new HashMap<>();
long[] queryTokenIds = convertTokensToIds(this.tokenizer, queryTokens, this.vocab, MAX_SEQ_LEN);
long[][] inputTokenIds = new long[1][queryTokenIds.length];
inputTokenIds[0] = queryTokenIds;

long[][] attentionMask = new long[1][queryTokenIds.length];
Arrays.fill(attentionMask[0], 1);

inputs.put("input_ids", OnnxTensor.createTensor(environment, inputTokenIds));
inputs.put("attention_mask", OnnxTensor.createTensor(environment, attentionMask));

float[] weights = null;
try (OrtSession.Result results = this.session.run(inputs)) {
weights = ((float[][][]) results.get("last_hidden_state").get().getValue())[0][0];
weights = normalize(weights);
} catch (OrtException e) {
e.printStackTrace();
}
return weights;
}
}
Loading