zhichao-geng
commited on
Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,144 @@
|
|
1 |
-
---
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
language: en
|
3 |
+
license: apache-2.0
|
4 |
+
tags:
|
5 |
+
- learned sparse
|
6 |
+
- opensearch
|
7 |
+
- transformers
|
8 |
+
- retrieval
|
9 |
+
- passage-retrieval
|
10 |
+
- query-expansion
|
11 |
+
- document-expansion
|
12 |
+
- bag-of-words
|
13 |
+
---
|
14 |
+
|
15 |
+
# opensearch-neural-sparse-encoding-v2-distill
|
16 |
+
This is a learned sparse retrieval model. It encodes the queries and documents to 30522 dimensional **sparse vectors**. The non-zero dimension index means the corresponding token in the vocabulary, and the weight means the importance of the token.
|
17 |
+
|
18 |
+
This model is trained on MS MARCO dataset.
|
19 |
+
|
20 |
+
OpenSearch neural sparse feature supports learned sparse retrieval with lucene inverted index. Link: https://opensearch.org/docs/latest/query-dsl/specialized/neural-sparse/. The indexing and search can be performed with OpenSearch high-level API.
|
21 |
+
|
22 |
+
## Select the model
|
23 |
+
The model should be selected considering search relevance, model inference and retrieval efficiency(FLOPS). We benchmark models' **zero-shot performance** on a subset of BEIR benchmark: TrecCovid,NFCorpus,NQ,HotpotQA,FiQA,ArguAna,Touche,DBPedia,SCIDOCS,FEVER,Climate FEVER,SciFact,Quora.
|
24 |
+
|
25 |
+
Overall, the v2 series of models have better search relevance, efficiency and inference speed than the v1 series. The specific advantages and disadvantages may vary across different datasets.
|
26 |
+
|
27 |
+
| Model | Inference-free for Retrieval | Model Parameters | AVG NDCG@10 | AVG FLOPS |
|
28 |
+
|-------|------------------------------|------------------|-------------|-----------|
|
29 |
+
| [opensearch-neural-sparse-encoding-v1](https://huggingface.co/opensearch-project/opensearch-neural-sparse-encoding-v1) | | 133M | 0.524 | 11.4 |
|
30 |
+
| [opensearch-neural-sparse-encoding-v2-distill](https://huggingface.co/opensearch-project/opensearch-neural-sparse-encoding-v2-distill) | | 67M | 0.528 | 8.3 |
|
31 |
+
| [opensearch-neural-sparse-encoding-doc-v1](https://huggingface.co/opensearch-project/opensearch-neural-sparse-encoding-doc-v1) | ✔️ | 133M | 0.490 | 2.3 |
|
32 |
+
| [opensearch-neural-sparse-encoding-doc-v2-distill](https://huggingface.co/opensearch-project/opensearch-neural-sparse-encoding-doc-v2-distill) | ✔️ | 67M | 0.504 | 1.8 |
|
33 |
+
|
34 |
+
|
35 |
+
## Usage (HuggingFace)
|
36 |
+
This model is supposed to run inside OpenSearch cluster. But you can also use it outside the cluster, with HuggingFace models API.
|
37 |
+
|
38 |
+
```python
|
39 |
+
import itertools
|
40 |
+
import torch
|
41 |
+
from transformers import AutoModelForMaskedLM, AutoTokenizer
|
42 |
+
|
43 |
+
|
44 |
+
# get sparse vector from dense vectors with shape batch_size * seq_len * vocab_size
|
45 |
+
def get_sparse_vector(feature, output):
|
46 |
+
values, _ = torch.max(output*feature["attention_mask"].unsqueeze(-1), dim=1)
|
47 |
+
values = torch.log(1 + torch.relu(values))
|
48 |
+
values[:,special_token_ids] = 0
|
49 |
+
return values
|
50 |
+
|
51 |
+
# transform the sparse vector to a dict of (token, weight)
|
52 |
+
def transform_sparse_vector_to_dict(sparse_vector):
|
53 |
+
sample_indices,token_indices=torch.nonzero(sparse_vector,as_tuple=True)
|
54 |
+
non_zero_values = sparse_vector[(sample_indices,token_indices)].tolist()
|
55 |
+
number_of_tokens_for_each_sample = torch.bincount(sample_indices).cpu().tolist()
|
56 |
+
tokens = [transform_sparse_vector_to_dict.id_to_token[_id] for _id in token_indices.tolist()]
|
57 |
+
|
58 |
+
output = []
|
59 |
+
end_idxs = list(itertools.accumulate([0]+number_of_tokens_for_each_sample))
|
60 |
+
for i in range(len(end_idxs)-1):
|
61 |
+
token_strings = tokens[end_idxs[i]:end_idxs[i+1]]
|
62 |
+
weights = non_zero_values[end_idxs[i]:end_idxs[i+1]]
|
63 |
+
output.append(dict(zip(token_strings, weights)))
|
64 |
+
return output
|
65 |
+
|
66 |
+
|
67 |
+
# load the model
|
68 |
+
model = AutoModelForMaskedLM.from_pretrained("opensearch-project/opensearch-neural-sparse-encoding-v2-distill")
|
69 |
+
tokenizer = AutoTokenizer.from_pretrained("opensearch-project/opensearch-neural-sparse-encoding-v2-distill")
|
70 |
+
|
71 |
+
# set the special tokens and id_to_token transform for post-process
|
72 |
+
special_token_ids = [tokenizer.vocab[token] for token in tokenizer.special_tokens_map.values()]
|
73 |
+
get_sparse_vector.special_token_ids = special_token_ids
|
74 |
+
id_to_token = ["" for i in range(tokenizer.vocab_size)]
|
75 |
+
for token, _id in tokenizer.vocab.items():
|
76 |
+
id_to_token[_id] = token
|
77 |
+
transform_sparse_vector_to_dict.id_to_token = id_to_token
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
query = "What's the weather in ny now?"
|
82 |
+
document = "Currently New York is rainy."
|
83 |
+
|
84 |
+
# encode the query & document
|
85 |
+
feature = tokenizer([query, document], padding=True, truncation=True, return_tensors='pt', return_token_type_ids=False)
|
86 |
+
output = model(**feature)[0]
|
87 |
+
sparse_vector = get_sparse_vector(feature, output)
|
88 |
+
|
89 |
+
# get similarity score
|
90 |
+
sim_score = torch.matmul(sparse_vector[0],sparse_vector[1])
|
91 |
+
print(sim_score) # tensor(22.3299, grad_fn=<DotBackward0>)
|
92 |
+
|
93 |
+
|
94 |
+
query_token_weight, document_query_token_weight = transform_sparse_vector_to_dict(sparse_vector)
|
95 |
+
for token in sorted(query_token_weight, key=lambda x:query_token_weight[x], reverse=True):
|
96 |
+
if token in document_query_token_weight:
|
97 |
+
print("score in query: %.4f, score in document: %.4f, token: %s"%(query_token_weight[token],document_query_token_weight[token],token))
|
98 |
+
|
99 |
+
|
100 |
+
|
101 |
+
# result:
|
102 |
+
# score in query: 2.9262, score in document: 2.1335, token: ny
|
103 |
+
# score in query: 2.5206, score in document: 1.5277, token: weather
|
104 |
+
# score in query: 2.0373, score in document: 2.3489, token: york
|
105 |
+
# score in query: 1.5786, score in document: 0.8752, token: cool
|
106 |
+
# score in query: 1.4636, score in document: 1.5132, token: current
|
107 |
+
# score in query: 0.7761, score in document: 0.8860, token: season
|
108 |
+
# score in query: 0.7560, score in document: 0.6726, token: 2020
|
109 |
+
# score in query: 0.7222, score in document: 0.6292, token: summer
|
110 |
+
# score in query: 0.6888, score in document: 0.6419, token: nina
|
111 |
+
# score in query: 0.6451, score in document: 0.8200, token: storm
|
112 |
+
# score in query: 0.4698, score in document: 0.7635, token: brooklyn
|
113 |
+
# score in query: 0.4562, score in document: 0.1208, token: julian
|
114 |
+
# score in query: 0.3484, score in document: 0.3903, token: wow
|
115 |
+
# score in query: 0.3439, score in document: 0.4160, token: usa
|
116 |
+
# score in query: 0.2751, score in document: 0.8260, token: manhattan
|
117 |
+
# score in query: 0.2013, score in document: 0.7735, token: fog
|
118 |
+
# score in query: 0.1989, score in document: 0.2961, token: mood
|
119 |
+
# score in query: 0.1653, score in document: 0.3437, token: climate
|
120 |
+
# score in query: 0.1191, score in document: 0.1533, token: nature
|
121 |
+
# score in query: 0.0665, score in document: 0.0600, token: temperature
|
122 |
+
# score in query: 0.0552, score in document: 0.3396, token: windy
|
123 |
+
```
|
124 |
+
|
125 |
+
The above code sample shows an example of neural sparse search. Although there is no overlap token in original query and document, but this model performs a good match.
|
126 |
+
|
127 |
+
## Detailed Search Relevance
|
128 |
+
|
129 |
+
| Dataset | [opensearch-neural-sparse-encoding-v1](https://huggingface.co/opensearch-project/opensearch-neural-sparse-encoding-v1) | [opensearch-neural-sparse-encoding-v2-distill](https://huggingface.co/opensearch-project/opensearch-neural-sparse-encoding-v2-distill) | [opensearch-neural-sparse-encoding-doc-v1](https://huggingface.co/opensearch-project/opensearch-neural-sparse-encoding-doc-v1) | [opensearch-neural-sparse-encoding-doc-v2-distill](https://huggingface.co/opensearch-project/opensearch-neural-sparse-encoding-doc-v2-distill) |
|
130 |
+
|---------|-------------------------------------------------------------------------|-------------------------------------------------------------------------------------|------------------------------------------------------------------------------|------------------------------------------------------------------------------------------|
|
131 |
+
| Trec Covid | 0.771 | 0.775 | 0.707 | 0.690 |
|
132 |
+
| NFCorpus | 0.360 | 0.347 | 0.352 | 0.343 |
|
133 |
+
| NQ | 0.553 | 0.561 | 0.521 | 0.528 |
|
134 |
+
| HotpotQA | 0.697 | 0.685 | 0.677 | 0.675 |
|
135 |
+
| FiQA | 0.376 | 0.374 | 0.344 | 0.357 |
|
136 |
+
| ArguAna | 0.508 | 0.551 | 0.461 | 0.496 |
|
137 |
+
| Touche | 0.278 | 0.278 | 0.294 | 0.287 |
|
138 |
+
| DBPedia | 0.447 | 0.435 | 0.412 | 0.418 |
|
139 |
+
| SCIDOCS | 0.164 | 0.173 | 0.154 | 0.166 |
|
140 |
+
| FEVER | 0.821 | 0.849 | 0.743 | 0.818 |
|
141 |
+
| Climate FEVER | 0.263 | 0.249 | 0.202 | 0.224 |
|
142 |
+
| SciFact | 0.723 | 0.722 | 0.716 | 0.715 |
|
143 |
+
| Quora | 0.856 | 0.863 | 0.788 | 0.841 |
|
144 |
+
| **Average** | **0.524** | **0.528** | **0.490** | **0.504** |
|