handwoven8588 commited on
Commit
7961156
Β·
verified Β·
1 Parent(s): b283422

README pass: base_model_relation=quantized, fix CPU-eager wording (bf16, not bit-identical), drop internal refs + first-person plural

Browse files
Files changed (1) hide show
  1. README.md +35 -42
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
- base_model:
3
- - nomic-ai/CodeRankEmbed
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 derivative of [`nomic-ai/CodeRankEmbed`](https://huggingface.co/nomic-ai/CodeRankEmbed)**
20
- with **flash-attention baked natively into a modeling file we own.** Not a finetune β€”
21
- the weights are the original CodeRankEmbed weights cast to bf16 (no further training), and the
22
- `modeling_hf_nomic_bert.py` here is a derivative that runs attention via `flash_attn` varlen
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
- though the model is only 137M params. `flash_attn`'s varlen path computes the same attention in
35
- `O(N)` memory by packing unpadded sequences, so you can run the large batches that OOM the eager
36
- path β€” with **parity embeddings** (no quality change). This repo ships that flash path in the
37
- modeling file itself, so you get it **without a runtime monkeypatch**.
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`. We patched the upstream `from_pretrained`
43
- (which silently dropped `torch_dtype` and always loaded fp32) to honor it β€” so the model now
44
- loads bf16 natively, like any normal HF model. Pass `torch_dtype=torch.float32` if you want
45
- fp32 (note: the stored weights are bf16-precision, so this only widens the dtype, not the
46
- precision).
47
- - **CUDA + `flash_attn`** β†’ flash-varlen path (fast, low VRAM). The forward casts attention
48
- tensors to bf16 internally as a safety net, but with the default bf16 load this is a no-op.
49
- - **CPU, or no `flash_attn`** β†’ the original eager path, **byte-for-byte unchanged**, so the
50
- model loads and encodes on any host. The forward picks the path automatically.
 
51
 
52
  ## Usage
53
 
@@ -67,8 +62,9 @@ d = model.encode(codes, normalize_embeddings=True)
67
 
68
  ## Parity & performance
69
 
70
- Same weights (bf16-cast) β†’ embeddings match the fp32 original. Measured on an RTX 3090 Ti,
71
- `flash_attn` 2.8.3, CLS pooling, L2-normalized, batch size 64:
 
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
- additionally scales to far larger batches (bs=256 fits in ~7.6 GB) where eager O(seqΒ²) blows up.
81
- The cosine is hardware-independent β€” the flash-varlen forward is numerically identical to the
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, but parity-neutral β€” the production serving path was
88
- already bf16). Halves the download.
89
- 2. **`from_pretrained` fix**: the upstream custom `from_pretrained` instantiated the model fp32
90
- and `load_state_dict`-ed the checkpoint into fp32 params, **ignoring `torch_dtype`** (it was
91
- swallowed in `**kwargs`). We added the standard transformers dtype resolution (explicit arg β†’
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 verbatim as the fallback. `NomicBertModel.forward` skips
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
- bulk of the modeling file are a verbatim derivative of `nomic-ai/CodeRankEmbed`; the modeling file
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