Introduce config files for simple & warning-free Sentence Transformers integration (#2)
Browse files- Introduce config files for simple & warning-free Sentence Transformers integration (326e580658d5683e1791d3fd45cac8d2ab37e5d0)
- 1_Pooling/config.json +10 -0
- README.md +16 -7
- config_sentence_transformers.json +10 -0
- modules.json +20 -0
- sentence_bert_config.json +4 -0
1_Pooling/config.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"word_embedding_dimension": 4096,
|
| 3 |
+
"pooling_mode_cls_token": false,
|
| 4 |
+
"pooling_mode_mean_tokens": true,
|
| 5 |
+
"pooling_mode_max_tokens": false,
|
| 6 |
+
"pooling_mode_mean_sqrt_len_tokens": false,
|
| 7 |
+
"pooling_mode_weightedmean_tokens": false,
|
| 8 |
+
"pooling_mode_lasttoken": false,
|
| 9 |
+
"include_prompt": false
|
| 10 |
+
}
|
README.md
CHANGED
|
@@ -6,6 +6,8 @@ language:
|
|
| 6 |
license: cc-by-nc-4.0
|
| 7 |
pipeline_tag: feature-extraction
|
| 8 |
library_name: transformers
|
|
|
|
|
|
|
| 9 |
---
|
| 10 |
|
| 11 |
## Model Summary
|
|
@@ -23,17 +25,20 @@ Make sure to install `transformers>=4.47.0` first!
|
|
| 23 |
### Transformers
|
| 24 |
|
| 25 |
```python
|
| 26 |
-
from transformers import AutoModel
|
|
|
|
| 27 |
model = AutoModel.from_pretrained("reasonir/ReasonIR-8B", torch_dtype="auto", trust_remote_code=True)
|
|
|
|
|
|
|
| 28 |
|
| 29 |
query = "The quick brown fox jumps over the lazy dog."
|
| 30 |
document = "The quick brown fox jumps over the lazy dog."
|
| 31 |
query_instruction = ""
|
| 32 |
doc_instruction = ""
|
| 33 |
-
|
| 34 |
-
model.eval()
|
| 35 |
query_emb = model.encode(query, instruction=query_instruction)
|
| 36 |
doc_emb = model.encode(document, instruction=doc_instruction)
|
|
|
|
| 37 |
sim = query_emb @ doc_emb.T
|
| 38 |
```
|
| 39 |
|
|
@@ -44,26 +49,30 @@ When using `AutoModel`, it is important to:
|
|
| 44 |
|
| 45 |
### Sentence Transformers
|
| 46 |
|
| 47 |
-
|
| 48 |
|
| 49 |
```python
|
|
|
|
| 50 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 51 |
model_kwargs = {"torch_dtype": "auto"}
|
| 52 |
model = SentenceTransformer("reasonir/ReasonIR-8B", trust_remote_code=True, model_kwargs=model_kwargs)
|
| 53 |
-
model.set_pooling_include_prompt(include_prompt=False) # exclude the prompt during pooling
|
| 54 |
|
| 55 |
query = "The quick brown fox jumps over the lazy dog."
|
| 56 |
document = "The quick brown fox jumps over the lazy dog."
|
| 57 |
query_instruction = ""
|
| 58 |
doc_instruction = ""
|
|
|
|
| 59 |
query_emb = model.encode(query, instruction=query_instruction)
|
| 60 |
doc_emb = model.encode(document, instruction=doc_instruction)
|
| 61 |
-
|
|
|
|
| 62 |
```
|
| 63 |
|
| 64 |
It is important to also include `trust_remote_code=True` and `torch_dtype="auto"` as discussed earlier.
|
| 65 |
|
| 66 |
-
NOTE
|
|
|
|
| 67 |
|
| 68 |
## Citation
|
| 69 |
```
|
|
|
|
| 6 |
license: cc-by-nc-4.0
|
| 7 |
pipeline_tag: feature-extraction
|
| 8 |
library_name: transformers
|
| 9 |
+
tags:
|
| 10 |
+
- sentence-transformers
|
| 11 |
---
|
| 12 |
|
| 13 |
## Model Summary
|
|
|
|
| 25 |
### Transformers
|
| 26 |
|
| 27 |
```python
|
| 28 |
+
from transformers import AutoModel
|
| 29 |
+
|
| 30 |
model = AutoModel.from_pretrained("reasonir/ReasonIR-8B", torch_dtype="auto", trust_remote_code=True)
|
| 31 |
+
model = model.to("cuda")
|
| 32 |
+
model.eval()
|
| 33 |
|
| 34 |
query = "The quick brown fox jumps over the lazy dog."
|
| 35 |
document = "The quick brown fox jumps over the lazy dog."
|
| 36 |
query_instruction = ""
|
| 37 |
doc_instruction = ""
|
| 38 |
+
|
|
|
|
| 39 |
query_emb = model.encode(query, instruction=query_instruction)
|
| 40 |
doc_emb = model.encode(document, instruction=doc_instruction)
|
| 41 |
+
|
| 42 |
sim = query_emb @ doc_emb.T
|
| 43 |
```
|
| 44 |
|
|
|
|
| 49 |
|
| 50 |
### Sentence Transformers
|
| 51 |
|
| 52 |
+
In addition to Transformers, you can also use this model with Sentence Transformers
|
| 53 |
|
| 54 |
```python
|
| 55 |
+
# pip install sentence-transformers
|
| 56 |
from sentence_transformers import SentenceTransformer
|
| 57 |
+
|
| 58 |
model_kwargs = {"torch_dtype": "auto"}
|
| 59 |
model = SentenceTransformer("reasonir/ReasonIR-8B", trust_remote_code=True, model_kwargs=model_kwargs)
|
|
|
|
| 60 |
|
| 61 |
query = "The quick brown fox jumps over the lazy dog."
|
| 62 |
document = "The quick brown fox jumps over the lazy dog."
|
| 63 |
query_instruction = ""
|
| 64 |
doc_instruction = ""
|
| 65 |
+
|
| 66 |
query_emb = model.encode(query, instruction=query_instruction)
|
| 67 |
doc_emb = model.encode(document, instruction=doc_instruction)
|
| 68 |
+
|
| 69 |
+
sim = model.similarity(query_emb, doc_emb)
|
| 70 |
```
|
| 71 |
|
| 72 |
It is important to also include `trust_remote_code=True` and `torch_dtype="auto"` as discussed earlier.
|
| 73 |
|
| 74 |
+
> [!NOTE]
|
| 75 |
+
> There are some very slight floating point discrepancy when using the model via SentenceTransformer caused by how the models are cast to the `bfloat16` dtype, though it should not affect the results in general.
|
| 76 |
|
| 77 |
## Citation
|
| 78 |
```
|
config_sentence_transformers.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"__version__": {
|
| 3 |
+
"sentence_transformers": "4.0.2",
|
| 4 |
+
"transformers": "4.48.2",
|
| 5 |
+
"pytorch": "2.6.0+cu124"
|
| 6 |
+
},
|
| 7 |
+
"prompts": {},
|
| 8 |
+
"default_prompt_name": null,
|
| 9 |
+
"similarity_fn_name": "cosine"
|
| 10 |
+
}
|
modules.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"idx": 0,
|
| 4 |
+
"name": "0",
|
| 5 |
+
"path": "",
|
| 6 |
+
"type": "sentence_transformers.models.Transformer"
|
| 7 |
+
},
|
| 8 |
+
{
|
| 9 |
+
"idx": 1,
|
| 10 |
+
"name": "1",
|
| 11 |
+
"path": "1_Pooling",
|
| 12 |
+
"type": "sentence_transformers.models.Pooling"
|
| 13 |
+
},
|
| 14 |
+
{
|
| 15 |
+
"idx": 2,
|
| 16 |
+
"name": "2",
|
| 17 |
+
"path": "2_Normalize",
|
| 18 |
+
"type": "sentence_transformers.models.Normalize"
|
| 19 |
+
}
|
| 20 |
+
]
|
sentence_bert_config.json
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"max_seq_length": 131072,
|
| 3 |
+
"do_lower_case": false
|
| 4 |
+
}
|