akanyaani commited on
Commit
d45dd4d
·
verified ·
1 Parent(s): 3e1ac5a

Model card: bfloat16 is the default precision, outputs refreshed

Browse files
Files changed (1) hide show
  1. README.md +13 -13
README.md CHANGED
@@ -163,9 +163,9 @@ print(query_embs.shape, doc_embs.shape)
163
  # (3, 1024) (4, 1024)
164
 
165
  print(model.similarity(query_embs, doc_embs))
166
- # tensor([[0.7576, 0.0962, 0.1732, 0.1833],
167
- # [0.1373, 0.8103, 0.0362, 0.2305],
168
- # [0.1879, 0.0734, 0.5977, 0.0014]])
169
  ```
170
 
171
  ## Best Practices
@@ -196,7 +196,7 @@ code_docs = ["def reverse(s):\n return s[::-1]", "def add(a, b):\n return
196
  q = model.encode(code_queries, prompt_name="CodeRetrieval")
197
  d = model.encode(code_docs, prompt_name="Document")
198
  print(model.similarity(q, d))
199
- # tensor([[0.5560, 0.2400]])
200
  ```
201
 
202
  For a symmetric task, use the same prompt for every text:
@@ -206,9 +206,9 @@ texts = ["This phone is really good", "Yeh phone bahut accha hai", "My order was
206
 
207
  embs = model.encode(texts, prompt_name="SentenceSimilarity")
208
  print(model.similarity(embs, embs))
209
- # tensor([[1.0000, 0.7550, 0.2029],
210
- # [0.7550, 1.0000, 0.0294],
211
- # [0.2029, 0.0294, 1.0000]])
212
  ```
213
 
214
  `encode_query` uses the `query` prompt (same text as `SearchQuery`) and `encode_document` uses the `document` prompt (same text as `Document`). The model also holds prompts keyed by MTEB task type (for example `Retrieval-query`, `Retrieval-document`, `STS`, `BitextMining`), so MTEB picks the right prompt for each task. `model.prompts` lists all of them.
@@ -245,7 +245,7 @@ model.max_seq_length = 1024
245
 
246
  ### 4. Numerical Precision
247
 
248
- The weights are stored in float32. On a GPU you can load the model in half precision to halve memory and speed up inference. In our checks against float32, float16 embeddings stayed above 0.998 cosine similarity even for a 16K-token input. bfloat16 stayed above 0.999 up to 4K tokens but dropped to about 0.988 at 16K tokens, so use float16 or float32 for long documents. On CPU, float32 is the safe default.
249
 
250
  ```python
251
  import torch
@@ -253,13 +253,13 @@ import torch
253
  model = SentenceTransformer(
254
  "FrontiersMind/EmbeddingNandi-450M",
255
  trust_remote_code=True,
256
- model_kwargs={"dtype": torch.float16},
257
  )
258
  ```
259
 
260
  ## Usage with Transformers
261
 
262
- EmbeddingNandi-450M also loads with Hugging Face `transformers` using `trust_remote_code=True` for both the tokenizer and the model. Add the task prompt in front of each text, take the mean over all tokens (prompt tokens included) and L2 normalize. This gives the same embeddings as Sentence Transformers.
263
 
264
  <details>
265
  <summary>Show the code</summary>
@@ -310,9 +310,9 @@ documents = [
310
 
311
  scores = embed(queries, QUERY_PROMPT) @ embed(documents, DOCUMENT_PROMPT).T # cosine similarity
312
  print(scores)
313
- # tensor([[0.7576, 0.0962, 0.1732, 0.1833],
314
- # [0.1373, 0.8103, 0.0362, 0.2305],
315
- # [0.1879, 0.0734, 0.5977, 0.0014]])
316
  ```
317
 
318
  </details>
 
163
  # (3, 1024) (4, 1024)
164
 
165
  print(model.similarity(query_embs, doc_embs))
166
+ # tensor([[0.7564, 0.0961, 0.1738, 0.1838],
167
+ # [0.1359, 0.8079, 0.0362, 0.2289],
168
+ # [0.1872, 0.0741, 0.5996, 0.0011]])
169
  ```
170
 
171
  ## Best Practices
 
196
  q = model.encode(code_queries, prompt_name="CodeRetrieval")
197
  d = model.encode(code_docs, prompt_name="Document")
198
  print(model.similarity(q, d))
199
+ # tensor([[0.5554, 0.2409]])
200
  ```
201
 
202
  For a symmetric task, use the same prompt for every text:
 
206
 
207
  embs = model.encode(texts, prompt_name="SentenceSimilarity")
208
  print(model.similarity(embs, embs))
209
+ # tensor([[1.0000, 0.7536, 0.2031],
210
+ # [0.7536, 1.0000, 0.0294],
211
+ # [0.2031, 0.0294, 1.0000]])
212
  ```
213
 
214
  `encode_query` uses the `query` prompt (same text as `SearchQuery`) and `encode_document` uses the `document` prompt (same text as `Document`). The model also holds prompts keyed by MTEB task type (for example `Retrieval-query`, `Retrieval-document`, `STS`, `BitextMining`), so MTEB picks the right prompt for each task. `model.prompts` lists all of them.
 
245
 
246
  ### 4. Numerical Precision
247
 
248
+ The weights are stored in `bfloat16` and the model loads in `bfloat16` by default. This is the precision used for the benchmark results above, and it works well for inputs up to a few thousand tokens. For very long inputs, load the model in `float32` (or `float16` on GPU) for more accurate embeddings: on a 16K-token input, `float32` and `float16` stay above 0.998 cosine similarity to the original `float32` model, while `bfloat16` is about 0.988.
249
 
250
  ```python
251
  import torch
 
253
  model = SentenceTransformer(
254
  "FrontiersMind/EmbeddingNandi-450M",
255
  trust_remote_code=True,
256
+ model_kwargs={"dtype": torch.float32}, # or torch.float16 on GPU
257
  )
258
  ```
259
 
260
  ## Usage with Transformers
261
 
262
+ EmbeddingNandi-450M also loads with Hugging Face `transformers` using `trust_remote_code=True` for both the tokenizer and the model. Add the task prompt in front of each text, take the mean over all tokens (prompt tokens included) and L2 normalize. This gives the same embeddings as Sentence Transformers, up to tiny `bfloat16` rounding differences.
263
 
264
  <details>
265
  <summary>Show the code</summary>
 
310
 
311
  scores = embed(queries, QUERY_PROMPT) @ embed(documents, DOCUMENT_PROMPT).T # cosine similarity
312
  print(scores)
313
+ # tensor([[0.7565, 0.0961, 0.1738, 0.1837],
314
+ # [0.1358, 0.8079, 0.0361, 0.2288],
315
+ # [0.1873, 0.0740, 0.5995, 0.0010]])
316
  ```
317
 
318
  </details>