Update README.md
Browse files
README.md
CHANGED
|
@@ -89,40 +89,20 @@ flash-attn>2.3.5
|
|
| 89 |
#### Huggingface Transformers
|
| 90 |
|
| 91 |
```python
|
| 92 |
-
from transformers import
|
| 93 |
import torch
|
| 94 |
import numpy as np
|
| 95 |
|
| 96 |
-
|
| 97 |
-
class MiniCPMRerankerLLamaTokenizer(LlamaTokenizer):
|
| 98 |
-
def build_inputs_with_special_tokens(
|
| 99 |
-
self, token_ids_0, token_ids_1 = None
|
| 100 |
-
):
|
| 101 |
-
"""
|
| 102 |
-
- single sequence: `<s> X </s>`
|
| 103 |
-
- pair of sequences: `<s> A </s> B`
|
| 104 |
-
|
| 105 |
-
Args:
|
| 106 |
-
token_ids_0 (`List[int]`):
|
| 107 |
-
List of IDs to which the special tokens will be added.
|
| 108 |
-
token_ids_1 (`List[int]`, *optional*):
|
| 109 |
-
Optional second list of IDs for sequence pairs.
|
| 110 |
-
|
| 111 |
-
Returns:
|
| 112 |
-
`List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens.
|
| 113 |
-
"""
|
| 114 |
-
|
| 115 |
-
if token_ids_1 is None:
|
| 116 |
-
return super().build_inputs_with_special_tokens(token_ids_0)
|
| 117 |
-
bos = [self.bos_token_id]
|
| 118 |
-
sep = [self.eos_token_id]
|
| 119 |
-
return bos + token_ids_0 + sep + token_ids_1
|
| 120 |
|
| 121 |
model_name = "openbmb/MiniCPM-Reranker"
|
| 122 |
-
tokenizer =
|
| 123 |
tokenizer.padding_side = "right"
|
| 124 |
|
| 125 |
-
model = AutoModelForSequenceClassification.from_pretrained(model_name, trust_remote_code=True,
|
|
|
|
|
|
|
|
|
|
| 126 |
model.eval()
|
| 127 |
|
| 128 |
@torch.no_grad()
|
|
@@ -154,37 +134,14 @@ print(np.array(scores)) # [[[-4.7460938][-8.8515625]]]
|
|
| 154 |
|
| 155 |
```python
|
| 156 |
from sentence_transformers import CrossEncoder
|
| 157 |
-
from transformers import LlamaTokenizer
|
| 158 |
import torch
|
| 159 |
|
| 160 |
-
#
|
| 161 |
-
class MiniCPMRerankerLLamaTokenizer(LlamaTokenizer):
|
| 162 |
-
def build_inputs_with_special_tokens(
|
| 163 |
-
self, token_ids_0, token_ids_1 = None
|
| 164 |
-
):
|
| 165 |
-
"""
|
| 166 |
-
- single sequence: `<s> X </s>`
|
| 167 |
-
- pair of sequences: `<s> A </s> B`
|
| 168 |
-
|
| 169 |
-
Args:
|
| 170 |
-
token_ids_0 (`List[int]`):
|
| 171 |
-
List of IDs to which the special tokens will be added.
|
| 172 |
-
token_ids_1 (`List[int]`, *optional*):
|
| 173 |
-
Optional second list of IDs for sequence pairs.
|
| 174 |
-
|
| 175 |
-
Returns:
|
| 176 |
-
`List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens.
|
| 177 |
-
"""
|
| 178 |
-
|
| 179 |
-
if token_ids_1 is None:
|
| 180 |
-
return super().build_inputs_with_special_tokens(token_ids_0)
|
| 181 |
-
bos = [self.bos_token_id]
|
| 182 |
-
sep = [self.eos_token_id]
|
| 183 |
-
return bos + token_ids_0 + sep + token_ids_1
|
| 184 |
-
|
| 185 |
model_name = "openbmb/MiniCPM-Reranker"
|
| 186 |
-
model = CrossEncoder(model_name,max_length=1024,trust_remote_code=True, automodel_args={"
|
| 187 |
-
|
|
|
|
|
|
|
| 188 |
model.tokenizer.padding_side = "right"
|
| 189 |
|
| 190 |
query = "中国的首都是哪里?"
|
|
|
|
| 89 |
#### Huggingface Transformers
|
| 90 |
|
| 91 |
```python
|
| 92 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 93 |
import torch
|
| 94 |
import numpy as np
|
| 95 |
|
| 96 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
model_name = "openbmb/MiniCPM-Reranker"
|
| 99 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 100 |
tokenizer.padding_side = "right"
|
| 101 |
|
| 102 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name, trust_remote_code=True, torch_dtype=torch.float16).to("cuda")
|
| 103 |
+
# You can also use the following code to use flash_attention_2
|
| 104 |
+
# model = AutoModelForSequenceClassification.from_pretrained(model_name, trust_remote_code=True,attn_implementation="flash_attention_2", torch_dtype=torch.float16).to("cuda")
|
| 105 |
+
|
| 106 |
model.eval()
|
| 107 |
|
| 108 |
@torch.no_grad()
|
|
|
|
| 134 |
|
| 135 |
```python
|
| 136 |
from sentence_transformers import CrossEncoder
|
|
|
|
| 137 |
import torch
|
| 138 |
|
| 139 |
+
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
model_name = "openbmb/MiniCPM-Reranker"
|
| 141 |
+
model = CrossEncoder(model_name,max_length=1024,trust_remote_code=True, automodel_args={"torch_dtype": torch.float16})
|
| 142 |
+
# You can also use the following code to use flash_attention_2
|
| 143 |
+
#model = CrossEncoder(model_name,max_length=1024,trust_remote_code=True, automodel_args={"attn_implementation":"flash_attention_2","torch_dtype": torch.float16})
|
| 144 |
+
|
| 145 |
model.tokenizer.padding_side = "right"
|
| 146 |
|
| 147 |
query = "中国的首都是哪里?"
|