Upload README.md with huggingface_hub
Browse files
README.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
tags:
|
| 4 |
+
- executorch
|
| 5 |
+
- xnnpack
|
| 6 |
+
- pte
|
| 7 |
+
- on-device
|
| 8 |
+
- sentence-similarity
|
| 9 |
+
- feature-extraction
|
| 10 |
+
base_model:
|
| 11 |
+
- sentence-transformers/all-MiniLM-L12-v2
|
| 12 |
+
---
|
| 13 |
+
# all-MiniLM-L12-v2 — ExecuTorch
|
| 14 |
+
|
| 15 |
+
Sentence embeddings: text in, one vector out. For search, clustering and retrieval that
|
| 16 |
+
never leaves the device. The deeper sibling of
|
| 17 |
+
[all-MiniLM-L6-v2](https://huggingface.co/mlboydaisuke/all-MiniLM-L6-v2-ExecuTorch) —
|
| 18 |
+
twelve layers instead of six, the same 384-dimensional output and the same recipe.
|
| 19 |
+
|
| 20 |
+
- **Source**: sentence-transformers/all-MiniLM-L12-v2 — 33.4M parameters, 12 BERT layers,
|
| 21 |
+
384-dimensional output
|
| 22 |
+
- **License**: Apache-2.0
|
| 23 |
+
- **Input**: `input_ids` and `attention_mask`, both `[1, 256]` int64
|
| 24 |
+
- **Output**: `[1, 384]`, mean-pooled over the mask and L2-normalised inside the graph
|
| 25 |
+
|
| 26 |
+
## The pooling is in the graph, on purpose
|
| 27 |
+
|
| 28 |
+
sentence-transformers keeps the recipe per model in `1_Pooling/config.json` and
|
| 29 |
+
`modules.json`, and the four small models on this shelf do not agree:
|
| 30 |
+
|
| 31 |
+
| | pooling | normalised |
|
| 32 |
+
|---|---|---|
|
| 33 |
+
| all-MiniLM-L6-v2 | mean | yes |
|
| 34 |
+
| **all-MiniLM-L12-v2** | **mean** | **yes** |
|
| 35 |
+
| bge-small-en-v1.5 | **CLS** | yes |
|
| 36 |
+
| paraphrase-multilingual-L12 | mean | **no** |
|
| 37 |
+
|
| 38 |
+
This one was read off the repo rather than assumed from the family name: `modules.json`
|
| 39 |
+
lists `Transformer, Pooling, Normalize` and the pooling config sets
|
| 40 |
+
`pooling_mode_mean_tokens`. Getting it wrong does not throw — mean-pooling BGE, or
|
| 41 |
+
normalising the multilingual one, gives vectors that look fine and rank wrong — so it is
|
| 42 |
+
baked in rather than left to the caller.
|
| 43 |
+
|
| 44 |
+
## Verification
|
| 45 |
+
|
| 46 |
+
| build | file | size | latency | worst cosine vs eager |
|
| 47 |
+
|---|---|---|---|---|
|
| 48 |
+
| XNNPACK fp32 | `embed_all_minilm_l12_xnnpack_fp32.pte` | 133.0 MB | 29.2 ms | 1.000000 |
|
| 49 |
+
| XNNPACK fp16 | `embed_all_minilm_l12_xnnpack_fp16.pte` | 66.7 MB | 51.0 ms | 0.999998 |
|
| 50 |
+
| Core ML fp32 | `embed_all_minilm_l12_coreml_all.pte` | 66.9 MB | **3.6 ms** | 0.999955 |
|
| 51 |
+
|
| 52 |
+
Mac arm64, median of 10, one 256-token sequence — a reference point for relative cost, not
|
| 53 |
+
a device number. Eager fp32 on the same input is 17.4 ms. Cosine is measured against the
|
| 54 |
+
model run in eager through its own documented pooling, over eight sentences including one
|
| 55 |
+
in Japanese — not against random token ids, which would tell you nothing.
|
| 56 |
+
|
| 57 |
+
**And that the vectors are useful**, which agreement alone cannot show. A paraphrase
|
| 58 |
+
against an unrelated sentence:
|
| 59 |
+
|
| 60 |
+
```
|
| 61 |
+
0.606 same meaning vs -0.110 unrelated
|
| 62 |
+
```
|
| 63 |
+
|
| 64 |
+
Across languages — "機械学習のモデルを端末の上で動かす" against "On-device inference keeps
|
| 65 |
+
the data on the phone" — it scores **-0.089**, below its score for an unrelated English
|
| 66 |
+
sentence. That is the right answer for an English-only model, and the reason
|
| 67 |
+
paraphrase-multilingual is on the shelf next to it.
|
| 68 |
+
|
| 69 |
+
```bash
|
| 70 |
+
python convert/check_embed.py all_minilm_l12 fp32 # or fp16, int8, coreml
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
## Not shipped
|
| 74 |
+
|
| 75 |
+
**int8** converts and holds — worst cosine 0.998275 against eager over the same eight
|
| 76 |
+
sentences — but it comes out at **69.5 MB against fp16's 66.7 MB**, so nothing would pick
|
| 77 |
+
it. The arithmetic is the same as on L6: dynamic int8 quantises the linear weights and
|
| 78 |
+
leaves the token embedding table alone, and that table is 46.9 MB of the 133.0 MB model.
|
| 79 |
+
fp16 halves the table too. On a model whose weights are substantially a vocabulary, fp16
|
| 80 |
+
is the smaller build — the depth doubled from L6 and the table did not, which is why the
|
| 81 |
+
gap here (69.5 against 66.7) is narrower than L6's (58.6 against 45.3).
|
| 82 |
+
|
| 83 |
+
## Worth knowing about the speed
|
| 84 |
+
|
| 85 |
+
XNNPACK fp32 is **slower than PyTorch eager** here (29.2 ms against 17.4), and fp16 is
|
| 86 |
+
slower again while halving the file — XNNPACK has no fp16 kernels for this graph and
|
| 87 |
+
inserts casts instead. Core ML is the one that pays: **3.6 ms**, roughly five times eager,
|
| 88 |
+
100% delegated in a single subgraph. On iOS take the Core ML build; on Android the
|
| 89 |
+
portable file is fp32 unless the size matters more than the latency.
|
| 90 |
+
|
| 91 |
+
torch.export -> to_edge_transform_and_lower(partitioner) -> .pte
|
| 92 |
+
(conversion scripts: [executorch-models](https://github.com/john-rocky/executorch-models))
|