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
README pass: base_model_relation=quantized, fix CPU-eager wording (bf16, not bit-identical), drop internal refs + first-person plural
Browse files
README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
base_model:
|
| 3 |
-
|
| 4 |
library_name: sentence-transformers
|
| 5 |
license: mit
|
| 6 |
tags:
|
|
@@ -16,38 +16,33 @@ pipeline_tag: feature-extraction
|
|
| 16 |
|
| 17 |
# CodeRankEmbed-flash-attn
|
| 18 |
|
| 19 |
-
A **bf16
|
| 20 |
-
with **flash-attention
|
| 21 |
-
the weights are the original CodeRankEmbed weights cast to bf16 (no further
|
| 22 |
-
|
| 23 |
-
instead of the original eager `O(seqΒ²)` path.
|
| 24 |
-
|
| 25 |
-
> **About the model tree:** this repo is a derivative of `nomic-ai/CodeRankEmbed` (same weights,
|
| 26 |
-
> bf16-cast + owned flash-attn code), **not** a finetune of `Snowflake/snowflake-arctic-embed-m-long`.
|
| 27 |
-
> The Snowflake model is CodeRankEmbed's *grandparent* (nomic-ai initialized from it); we derive
|
| 28 |
-
> directly from nomic-ai/CodeRankEmbed, which is also where the fp32 reference weights live.
|
| 29 |
|
| 30 |
## Why
|
| 31 |
|
| 32 |
`nomic-ai/CodeRankEmbed` loads through `trust_remote_code`, and its attention path is **eager
|
| 33 |
-
only** β activation memory grows as `batch Γ heads Γ seqΒ²`, which OOMs at large batches even
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
|
| 39 |
## Behavior
|
| 40 |
|
| 41 |
- **Loads bf16 by default.** Weights are stored bf16 (half the download: 274 MB vs 547 MB) and
|
| 42 |
-
`config.json` declares `torch_dtype: bfloat16`.
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
model loads and encodes on any host
|
|
|
|
| 51 |
|
| 52 |
## Usage
|
| 53 |
|
|
@@ -67,8 +62,9 @@ d = model.encode(codes, normalize_embeddings=True)
|
|
| 67 |
|
| 68 |
## Parity & performance
|
| 69 |
|
| 70 |
-
|
| 71 |
-
`flash_attn` 2.8.3, CLS pooling,
|
|
|
|
| 72 |
|
| 73 |
| metric | `nomic-ai/CodeRankEmbed` (fp32, eager) | this repo (bf16, flash-varlen) |
|
| 74 |
| --- | --- | --- |
|
|
@@ -77,29 +73,26 @@ Same weights (bf16-cast) β embeddings match the fp32 original. Measured on an
|
|
| 77 |
| throughput (bs=64) | 52,000 tok/s | **162,000 tok/s** (β3.1Γ faster) |
|
| 78 |
|
| 79 |
Same 512-snippet corpus (~236k tokens, 20β900 tokens each), batch size 64. The flash-varlen path
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
merged penstock #264 monkeypatch (same `unpad β flash_attn_varlen_qkvpacked_func β repad` path),
|
| 83 |
-
verified at parity > 0.997 on both a 4060 and the 3090 Ti above.
|
| 84 |
|
| 85 |
## What changed vs the source repo
|
| 86 |
|
| 87 |
-
1. **Weights**: fp32 β bf16 (lossy cast,
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
`config.torch_dtype` β checkpoint dtype) so the model loads in its declared dtype.
|
| 93 |
3. **Flash-varlen forward**: `NomicBertAttention.forward` gains an `unpad β
|
| 94 |
-
flash_attn_varlen_qkvpacked_func(causal=False) β repad` branch (CUDA+flash_attn); the original
|
| 95 |
-
eager block is kept
|
| 96 |
`get_extended_attention_mask` on the flash path. Rotary embeddings are applied to the dense
|
| 97 |
`[B, S, 3, H, D]` tensor **before** unpadding β the correctness keystone.
|
| 98 |
|
| 99 |
## License & attribution
|
| 100 |
|
| 101 |
-
MIT β same license as `nomic-ai/CodeRankEmbed` (see `NOTICE`). The weights, tokenizer, and the
|
| 102 |
-
|
| 103 |
derives from Tri Dao's BERT implementation, and `CodeRankEmbed` was trained by the CoRNStack team
|
| 104 |
(Suresh et al., 2025). Cite their work:
|
| 105 |
|
|
|
|
| 1 |
---
|
| 2 |
+
base_model: nomic-ai/CodeRankEmbed
|
| 3 |
+
base_model_relation: quantized
|
| 4 |
library_name: sentence-transformers
|
| 5 |
license: mit
|
| 6 |
tags:
|
|
|
|
| 16 |
|
| 17 |
# CodeRankEmbed-flash-attn
|
| 18 |
|
| 19 |
+
A **bf16 quantization of [`nomic-ai/CodeRankEmbed`](https://huggingface.co/nomic-ai/CodeRankEmbed)**
|
| 20 |
+
with **flash-attention built into a custom `modeling_hf_nomic_bert.py` shipped in this repo.** It is
|
| 21 |
+
not a finetune β the weights are the original CodeRankEmbed weights cast to bf16 (no further
|
| 22 |
+
training) β and attention runs via `flash_attn` varlen instead of the original eager `O(seqΒ²)` path.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
## Why
|
| 25 |
|
| 26 |
`nomic-ai/CodeRankEmbed` loads through `trust_remote_code`, and its attention path is **eager
|
| 27 |
+
only** β activation memory grows as `batch Γ heads Γ seqΒ²`, which OOMs at large batches even though
|
| 28 |
+
the model is only 137M params. `flash_attn`'s varlen path computes the same attention in `O(N)`
|
| 29 |
+
memory by packing unpadded sequences, so the large batches that OOM the eager path run comfortably β
|
| 30 |
+
with **parity embeddings** (no quality change). This repo ships that flash path in the modeling file
|
| 31 |
+
itself, so no runtime patching or post-load hooks are needed.
|
| 32 |
|
| 33 |
## Behavior
|
| 34 |
|
| 35 |
- **Loads bf16 by default.** Weights are stored bf16 (half the download: 274 MB vs 547 MB) and
|
| 36 |
+
`config.json` declares `torch_dtype: bfloat16`. The upstream custom `from_pretrained` silently
|
| 37 |
+
dropped `torch_dtype` and always loaded fp32; the copy in this repo honors it, so the model loads
|
| 38 |
+
bf16 natively, like any normal HF model. Pass `torch_dtype=torch.float32` to load fp32 (note: the
|
| 39 |
+
stored weights are bf16-precision, so this only widens the dtype, not the precision).
|
| 40 |
+
- **CUDA + `flash_attn`** β flash-varlen path (fast, low VRAM). Attention tensors are cast to bf16
|
| 41 |
+
internally as a safety net; with the default bf16 load this is a no-op.
|
| 42 |
+
- **CPU, or no `flash_attn`** β the original eager attention algorithm runs unchanged. Because the
|
| 43 |
+
model is bf16, eager runs in bf16 here β numerically equivalent to the flash path, just without
|
| 44 |
+
its memory and throughput wins. The model loads and encodes on any host; the forward selects the
|
| 45 |
+
path automatically (`_FLASH_AVAILABLE and hidden_states.is_cuda`).
|
| 46 |
|
| 47 |
## Usage
|
| 48 |
|
|
|
|
| 62 |
|
| 63 |
## Parity & performance
|
| 64 |
|
| 65 |
+
The weights are the original CodeRankEmbed weights (bf16-cast), so embeddings match the fp32
|
| 66 |
+
original to within bf16 precision. Measured on an RTX 3090 Ti, `flash_attn` 2.8.3, CLS pooling,
|
| 67 |
+
L2-normalized, batch size 64:
|
| 68 |
|
| 69 |
| metric | `nomic-ai/CodeRankEmbed` (fp32, eager) | this repo (bf16, flash-varlen) |
|
| 70 |
| --- | --- | --- |
|
|
|
|
| 73 |
| throughput (bs=64) | 52,000 tok/s | **162,000 tok/s** (β3.1Γ faster) |
|
| 74 |
|
| 75 |
Same 512-snippet corpus (~236k tokens, 20β900 tokens each), batch size 64. The flash-varlen path
|
| 76 |
+
also scales to far larger batches (bs=256 fits in ~7.6 GB) where eager O(seqΒ²) blows up. Parity
|
| 77 |
+
(>0.997) reproduced on both an RTX 4060 and the 3090 Ti.
|
|
|
|
|
|
|
| 78 |
|
| 79 |
## What changed vs the source repo
|
| 80 |
|
| 81 |
+
1. **Weights**: fp32 β bf16 (lossy cast, parity-neutral). Halves the download.
|
| 82 |
+
2. **`from_pretrained` dtype fix**: the upstream custom `from_pretrained` instantiated the model
|
| 83 |
+
fp32 and `load_state_dict`-ed the checkpoint into fp32 params, **ignoring `torch_dtype`**. The
|
| 84 |
+
copy here adds the standard transformers dtype resolution (explicit arg β `config.torch_dtype` β
|
| 85 |
+
checkpoint dtype) so the model loads in its declared dtype.
|
|
|
|
| 86 |
3. **Flash-varlen forward**: `NomicBertAttention.forward` gains an `unpad β
|
| 87 |
+
flash_attn_varlen_qkvpacked_func(causal=False) β repad` branch (CUDA + flash_attn); the original
|
| 88 |
+
eager block is kept as the fallback. `NomicBertModel.forward` skips
|
| 89 |
`get_extended_attention_mask` on the flash path. Rotary embeddings are applied to the dense
|
| 90 |
`[B, S, 3, H, D]` tensor **before** unpadding β the correctness keystone.
|
| 91 |
|
| 92 |
## License & attribution
|
| 93 |
|
| 94 |
+
MIT β same license as `nomic-ai/CodeRankEmbed` (see `NOTICE`). The weights, tokenizer, and the bulk
|
| 95 |
+
of the modeling file are a verbatim derivative of `nomic-ai/CodeRankEmbed`; the modeling file
|
| 96 |
derives from Tri Dao's BERT implementation, and `CodeRankEmbed` was trained by the CoRNStack team
|
| 97 |
(Suresh et al., 2025). Cite their work:
|
| 98 |
|