Introduce config files for simple & warning-free Sentence Transformers integration

#2
by tomaarsen HF Staff - opened
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-sa-4.0
7
  pipeline_tag: feature-extraction
8
  library_name: transformers
 
 
9
  ---
10
 
11
  ## Model Summary
@@ -22,17 +24,20 @@ Make sure to install `transformers>=4.47.0` first!
22
  ### Transformers
23
 
24
  ```python
25
- from transformers import AutoModel, AutoTokenizer
 
26
  model = AutoModel.from_pretrained("reasonir/ReasonIR-8B", torch_dtype="auto", trust_remote_code=True)
 
 
27
 
28
  query = "The quick brown fox jumps over the lazy dog."
29
  document = "The quick brown fox jumps over the lazy dog."
30
  query_instruction = ""
31
  doc_instruction = ""
32
- model = model.to("cuda")
33
- model.eval()
34
  query_emb = model.encode(query, instruction=query_instruction)
35
  doc_emb = model.encode(document, instruction=doc_instruction)
 
36
  sim = query_emb @ doc_emb.T
37
  ```
38
 
@@ -43,26 +48,30 @@ When using `AutoModel`, it is important to:
43
 
44
  ### Sentence Transformers
45
 
46
- Ordinary retrieval models that use mean pooling can automatically be used with SentenceTransformer after being published on Huggingface.
47
 
48
  ```python
 
49
  from sentence_transformers import SentenceTransformer
 
50
  model_kwargs = {"torch_dtype": "auto"}
51
  model = SentenceTransformer("reasonir/ReasonIR-8B", trust_remote_code=True, model_kwargs=model_kwargs)
52
- model.set_pooling_include_prompt(include_prompt=False) # exclude the prompt during pooling
53
 
54
  query = "The quick brown fox jumps over the lazy dog."
55
  document = "The quick brown fox jumps over the lazy dog."
56
  query_instruction = ""
57
  doc_instruction = ""
 
58
  query_emb = model.encode(query, instruction=query_instruction)
59
  doc_emb = model.encode(document, instruction=doc_instruction)
60
- sim = query_emb @ doc_emb.T
 
61
  ```
62
 
63
  It is important to also include `trust_remote_code=True` and `torch_dtype="auto"` as discussed earlier.
64
 
65
- NOTE: there seems to be some very slight floating point discrepancy when using the SentenceTransformer (because it does not support bf16 precision), though it should not affect the results in general.
 
66
 
67
  ## Citation
68
  ```
 
6
  license: cc-by-sa-4.0
7
  pipeline_tag: feature-extraction
8
  library_name: transformers
9
+ tags:
10
+ - sentence-transformers
11
  ---
12
 
13
  ## Model Summary
 
24
  ### Transformers
25
 
26
  ```python
27
+ from transformers import AutoModel
28
+
29
  model = AutoModel.from_pretrained("reasonir/ReasonIR-8B", torch_dtype="auto", trust_remote_code=True)
30
+ model = model.to("cuda")
31
+ model.eval()
32
 
33
  query = "The quick brown fox jumps over the lazy dog."
34
  document = "The quick brown fox jumps over the lazy dog."
35
  query_instruction = ""
36
  doc_instruction = ""
37
+
 
38
  query_emb = model.encode(query, instruction=query_instruction)
39
  doc_emb = model.encode(document, instruction=doc_instruction)
40
+
41
  sim = query_emb @ doc_emb.T
42
  ```
43
 
 
48
 
49
  ### Sentence Transformers
50
 
51
+ In addition to Transformers, you can also use this model with Sentence Transformers
52
 
53
  ```python
54
+ # pip install sentence-transformers
55
  from sentence_transformers import SentenceTransformer
56
+
57
  model_kwargs = {"torch_dtype": "auto"}
58
  model = SentenceTransformer("reasonir/ReasonIR-8B", trust_remote_code=True, model_kwargs=model_kwargs)
 
59
 
60
  query = "The quick brown fox jumps over the lazy dog."
61
  document = "The quick brown fox jumps over the lazy dog."
62
  query_instruction = ""
63
  doc_instruction = ""
64
+
65
  query_emb = model.encode(query, instruction=query_instruction)
66
  doc_emb = model.encode(document, instruction=doc_instruction)
67
+
68
+ sim = model.similarity(query_emb, doc_emb)
69
  ```
70
 
71
  It is important to also include `trust_remote_code=True` and `torch_dtype="auto"` as discussed earlier.
72
 
73
+ > [!NOTE]
74
+ > 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.
75
 
76
  ## Citation
77
  ```
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
+ }