sentence-transformers
Safetensors
English
nomic_bert
flash-attention
code-retrieval
nomic-bert
bf16
custom_code
Instructions to use handwoven8588/CodeRankEmbed-flash-attn with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use handwoven8588/CodeRankEmbed-flash-attn with sentence-transformers:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("handwoven8588/CodeRankEmbed-flash-attn", trust_remote_code=True) sentences = [ "The weather is lovely today.", "It's so sunny outside!", "He drove to the stadium." ] embeddings = model.encode(sentences) similarities = model.similarity(embeddings, embeddings) print(similarities.shape) # [3, 3] - Notebooks
- Google Colab
- Kaggle
Card: three charts, full tables folded; trim two Behavior bullets
Browse files- .gitattributes +3 -0
- README.md +26 -9
- assets/encode-memory.png +3 -0
- assets/encode-time.png +3 -0
- assets/parity.png +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
assets/encode-time.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
assets/encode-memory.png filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
assets/parity.png filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -60,11 +60,8 @@ file ships all three paths itself, so no runtime patching or post-load hooks are
|
|
| 60 |
tier, so a transient failure never pins the device to a slower tier. A forced override (e.g. `NOMIC_BERT_ATTN_IMPL=torch_varlen`) is not probed and **raises**
|
| 61 |
`RuntimeError` if that tier's precondition doesn't hold — a forced tier never falls back
|
| 62 |
silently. An unrecognized override raises `ValueError`.
|
| 63 |
-
- **See which tier engaged**: `model[0].auto_model.attention_impl` after a forward pass
|
| 64 |
-
|
| 65 |
-
override=...` INFO log line (one line per distinct `(impl, device)`). When `flash_attn` is
|
| 66 |
-
unavailable the line names why (`flash_attn=absent(<error>)`); an installed `flash_attn` that
|
| 67 |
-
fails to import also logs a WARNING.
|
| 68 |
- **Padding-free on the varlen tiers.** The model unpads the batch once, right after the input
|
| 69 |
ids, runs the embeddings and every encoder layer on the real tokens only, and pads back once at
|
| 70 |
the end. `last_hidden_state` keeps its `[B, S, 768]` shape, with zeros at padding positions, so
|
|
@@ -76,8 +73,6 @@ file ships all three paths itself, so no runtime patching or post-load hooks are
|
|
| 76 |
the `flash_attn` package installed, because transformers checks for it at load and raises
|
| 77 |
otherwise, even though the attention itself still runs on whichever tier the dispatch picks. On
|
| 78 |
the `eager` tier the flattened batch is re-padded and runs the padded path.
|
| 79 |
-
- **`revision=` pins everything.** Loading with `revision=<commit>` fetches the code, config,
|
| 80 |
-
tokenizer **and weights** from that commit.
|
| 81 |
- **Loads bf16 by default.** `flash_attn` and `torch_varlen` both require half precision and the
|
| 82 |
model runs bf16 in any real serving setup, so the weights are stored bf16 and `config.json`
|
| 83 |
declares `torch_dtype: bfloat16`. The upstream custom `from_pretrained` silently dropped
|
|
@@ -128,8 +123,15 @@ model[0].auto_model.encoder = torch.compile(model[0].auto_model.encoder, dynamic
|
|
| 128 |
## Parity & performance
|
| 129 |
|
| 130 |
The weights are the original CodeRankEmbed weights (bf16-cast), so embeddings match the fp32
|
| 131 |
-
original to within bf16 precision.
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
**Protocol.** The first 16,384 `document` fields (real Python functions) of
|
| 135 |
[`lightonai/cornstack`](https://huggingface.co/datasets/lightonai/cornstack)'s Python split, in
|
|
@@ -152,6 +154,9 @@ pre-dispatch revision at batch 32 peaks at 4,456 MiB and takes 23.8 s (3090 Ti)
|
|
| 152 |
(5090 Laptop).
|
| 153 |
|
| 154 |
|
|
|
|
|
|
|
|
|
|
| 155 |
| GPU | load | batch size | tier | mean cos | min cos | encode peak | wall |
|
| 156 |
| --- | --- | --- | --- | --- | --- | --- | --- |
|
| 157 |
| RTX 3090 Ti (sm_86) | default | 4 | `eager` | 0.999923 | 0.999404 | 4,321 MiB | 41.791 s |
|
|
@@ -191,6 +196,8 @@ pre-dispatch revision at batch 32 peaks at 4,456 MiB and takes 23.8 s (3090 Ti)
|
|
| 191 |
| RTX 5090 Laptop GPU (sm_120) | flattened | 2,048 | `torch_varlen` | out of memory | | | |
|
| 192 |
| RTX 5090 Laptop GPU (sm_120) | flattened | 2,048 | `flash_attn` | out of memory | | | |
|
| 193 |
|
|
|
|
|
|
|
| 194 |
Cosines rounded to 6 decimal places, VRAM to the nearest MiB. `torch 2.12.1+cu130`,
|
| 195 |
`transformers 5.11.0`, `sentence-transformers 6.1.0`, `flash-attn 2.8.3` (`2.8.3.post1` on the
|
| 196 |
3090 Ti).
|
|
@@ -217,6 +224,9 @@ and long files. Wall time covers the whole corpus; peak VRAM is the peak CUDA al
|
|
| 217 |
included. The largest sorted batch on each card is the largest batch size that fit (the 5090
|
| 218 |
Laptop was sharing about 0.4 GiB with another process).
|
| 219 |
|
|
|
|
|
|
|
|
|
|
| 220 |
| GPU | load | order | batch size | tier | wall (768 files) | peak VRAM |
|
| 221 |
| --- | --- | --- | --- | --- | --- | --- |
|
| 222 |
| RTX 3090 Ti (sm_86) | default | sorted | 4 | `torch_varlen` | 5.61 s | 1,296 MiB |
|
|
@@ -252,6 +262,8 @@ Laptop was sharing about 0.4 GiB with another process).
|
|
| 252 |
| RTX 5090 Laptop GPU (sm_120) | flattened | file | 32 | `torch_varlen` | 4.70 s | 1,630 MiB |
|
| 253 |
| RTX 5090 Laptop GPU (sm_120) | flattened | file | 64 | `torch_varlen` | 4.66 s | 2,811 MiB |
|
| 254 |
|
|
|
|
|
|
|
| 255 |
Flattening keeps peak memory well below the default load at every sorted batch size and fits a
|
| 256 |
batch of 628 on 24 GB, where the default load fits 353. It also makes a text's embedding
|
| 257 |
independent of the other texts in its batch: between the file-order and sorted runs, the minimum
|
|
@@ -264,6 +276,9 @@ The parity table's 16,384 functions on `torch_varlen`, with the encoder layers e
|
|
| 264 |
(`torch.compile(model[0].auto_model.encoder, dynamic=True)`). Cosine is against the same
|
| 265 |
pre-dispatch encode as the parity table.
|
| 266 |
|
|
|
|
|
|
|
|
|
|
| 267 |
| GPU | load | batch size | eager | compiled | compiled min cos (mean) |
|
| 268 |
| --- | --- | --- | --- | --- | --- |
|
| 269 |
| RTX 3090 Ti (sm_86) | default | 32 | 16.38 s, 2,511 MiB | 14.05 s, 1,635 MiB | 0.998156 (0.999943) |
|
|
@@ -279,6 +294,8 @@ pre-dispatch encode as the parity table.
|
|
| 279 |
| RTX 5090 Laptop GPU (sm_120) | flattened | 256 | 14.35 s, 7,072 MiB | 11.66 s, 4,601 MiB | 0.999250 (0.999944) |
|
| 280 |
| RTX 5090 Laptop GPU (sm_120) | flattened | 1,024 | 14.67 s, 17,412 MiB | 11.80 s, 11,326 MiB | 0.999250 (0.999944) |
|
| 281 |
|
|
|
|
|
|
|
| 282 |
Compiling makes the encode 1.17–1.24× faster and cuts its peak memory by about a third, because
|
| 283 |
inductor fuses the element-wise work around each attention call; the default load's batch of 1,024
|
| 284 |
then fits. It compiles one graph with no graph breaks, once per process (the first encode took about
|
|
|
|
| 60 |
tier, so a transient failure never pins the device to a slower tier. A forced override (e.g. `NOMIC_BERT_ATTN_IMPL=torch_varlen`) is not probed and **raises**
|
| 61 |
`RuntimeError` if that tier's precondition doesn't hold — a forced tier never falls back
|
| 62 |
silently. An unrecognized override raises `ValueError`.
|
| 63 |
+
- **See which tier engaged**: `model[0].auto_model.attention_impl` after a forward pass. The
|
| 64 |
+
modeling file also logs the tier, and why, once at INFO.
|
|
|
|
|
|
|
|
|
|
| 65 |
- **Padding-free on the varlen tiers.** The model unpads the batch once, right after the input
|
| 66 |
ids, runs the embeddings and every encoder layer on the real tokens only, and pads back once at
|
| 67 |
the end. `last_hidden_state` keeps its `[B, S, 768]` shape, with zeros at padding positions, so
|
|
|
|
| 73 |
the `flash_attn` package installed, because transformers checks for it at load and raises
|
| 74 |
otherwise, even though the attention itself still runs on whichever tier the dispatch picks. On
|
| 75 |
the `eager` tier the flattened batch is re-padded and runs the padded path.
|
|
|
|
|
|
|
| 76 |
- **Loads bf16 by default.** `flash_attn` and `torch_varlen` both require half precision and the
|
| 77 |
model runs bf16 in any real serving setup, so the weights are stored bf16 and `config.json`
|
| 78 |
declares `torch_dtype: bfloat16`. The upstream custom `from_pretrained` silently dropped
|
|
|
|
| 123 |
## Parity & performance
|
| 124 |
|
| 125 |
The weights are the original CodeRankEmbed weights (bf16-cast), so embeddings match the fp32
|
| 126 |
+
original to within bf16 precision. Everything below is measured **on the same GPU** per row, on
|
| 127 |
+
two cards, against the pre-dispatch revision of this repo; the full tables are folded under each
|
| 128 |
+
section.
|
| 129 |
+
|
| 130 |
+

|
| 131 |
+
|
| 132 |
+

|
| 133 |
+
|
| 134 |
+

|
| 135 |
|
| 136 |
**Protocol.** The first 16,384 `document` fields (real Python functions) of
|
| 137 |
[`lightonai/cornstack`](https://huggingface.co/datasets/lightonai/cornstack)'s Python split, in
|
|
|
|
| 154 |
(5090 Laptop).
|
| 155 |
|
| 156 |
|
| 157 |
+
<details>
|
| 158 |
+
<summary>Every tier, both loads, both GPUs (table)</summary>
|
| 159 |
+
|
| 160 |
| GPU | load | batch size | tier | mean cos | min cos | encode peak | wall |
|
| 161 |
| --- | --- | --- | --- | --- | --- | --- | --- |
|
| 162 |
| RTX 3090 Ti (sm_86) | default | 4 | `eager` | 0.999923 | 0.999404 | 4,321 MiB | 41.791 s |
|
|
|
|
| 196 |
| RTX 5090 Laptop GPU (sm_120) | flattened | 2,048 | `torch_varlen` | out of memory | | | |
|
| 197 |
| RTX 5090 Laptop GPU (sm_120) | flattened | 2,048 | `flash_attn` | out of memory | | | |
|
| 198 |
|
| 199 |
+
</details>
|
| 200 |
+
|
| 201 |
Cosines rounded to 6 decimal places, VRAM to the nearest MiB. `torch 2.12.1+cu130`,
|
| 202 |
`transformers 5.11.0`, `sentence-transformers 6.1.0`, `flash-attn 2.8.3` (`2.8.3.post1` on the
|
| 203 |
3090 Ti).
|
|
|
|
| 224 |
included. The largest sorted batch on each card is the largest batch size that fit (the 5090
|
| 225 |
Laptop was sharing about 0.4 GiB with another process).
|
| 226 |
|
| 227 |
+
<details>
|
| 228 |
+
<summary>768 stack-edu files, both loads, both GPUs (table)</summary>
|
| 229 |
+
|
| 230 |
| GPU | load | order | batch size | tier | wall (768 files) | peak VRAM |
|
| 231 |
| --- | --- | --- | --- | --- | --- | --- |
|
| 232 |
| RTX 3090 Ti (sm_86) | default | sorted | 4 | `torch_varlen` | 5.61 s | 1,296 MiB |
|
|
|
|
| 262 |
| RTX 5090 Laptop GPU (sm_120) | flattened | file | 32 | `torch_varlen` | 4.70 s | 1,630 MiB |
|
| 263 |
| RTX 5090 Laptop GPU (sm_120) | flattened | file | 64 | `torch_varlen` | 4.66 s | 2,811 MiB |
|
| 264 |
|
| 265 |
+
</details>
|
| 266 |
+
|
| 267 |
Flattening keeps peak memory well below the default load at every sorted batch size and fits a
|
| 268 |
batch of 628 on 24 GB, where the default load fits 353. It also makes a text's embedding
|
| 269 |
independent of the other texts in its batch: between the file-order and sorted runs, the minimum
|
|
|
|
| 276 |
(`torch.compile(model[0].auto_model.encoder, dynamic=True)`). Cosine is against the same
|
| 277 |
pre-dispatch encode as the parity table.
|
| 278 |
|
| 279 |
+
<details>
|
| 280 |
+
<summary>Eager vs compiled, both loads, both GPUs (table)</summary>
|
| 281 |
+
|
| 282 |
| GPU | load | batch size | eager | compiled | compiled min cos (mean) |
|
| 283 |
| --- | --- | --- | --- | --- | --- |
|
| 284 |
| RTX 3090 Ti (sm_86) | default | 32 | 16.38 s, 2,511 MiB | 14.05 s, 1,635 MiB | 0.998156 (0.999943) |
|
|
|
|
| 294 |
| RTX 5090 Laptop GPU (sm_120) | flattened | 256 | 14.35 s, 7,072 MiB | 11.66 s, 4,601 MiB | 0.999250 (0.999944) |
|
| 295 |
| RTX 5090 Laptop GPU (sm_120) | flattened | 1,024 | 14.67 s, 17,412 MiB | 11.80 s, 11,326 MiB | 0.999250 (0.999944) |
|
| 296 |
|
| 297 |
+
</details>
|
| 298 |
+
|
| 299 |
Compiling makes the encode 1.17–1.24× faster and cuts its peak memory by about a third, because
|
| 300 |
inductor fuses the element-wise work around each attention call; the default load's batch of 1,024
|
| 301 |
then fits. It compiles one graph with no graph breaks, once per process (the first encode took about
|
assets/encode-memory.png
ADDED
|
Git LFS Details
|
assets/encode-time.png
ADDED
|
Git LFS Details
|
assets/parity.png
ADDED
|
Git LFS Details
|