|
Download code/PYTHON.md from changh95/all-MiniLM-L6-v2: direct link, hf CLI and curl.
- Browser
- Download file 2.58 kB
-
https://huggingface.co/changh95/all-MiniLM-L6-v2/resolve/main/code/PYTHON.md
- Command line
-
hf download hf://changh95/all-MiniLM-L6-v2/code/PYTHON.md
-
curl -L -o PYTHON.md https://huggingface.co/changh95/all-MiniLM-L6-v2/resolve/main/code/PYTHON.md
2.58 kB
| # all-MiniLM-L6-v2 on Tenstorrent p150: Python API | |
| `tt_minilm` runs [sentence-transformers/all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2) | |
| on one Tenstorrent Blackhole p150. It needs tt-metal's `ttnn` built at the commit in `patches/README.md` with both | |
| patches applied (ETH dispatch: the 12 x 10 grid the megakernel needs), plus `numpy<2`, `safetensors`, | |
| `huggingface_hub` and `tokenizers` (`pip install -e .` from the repo root). | |
| ```python | |
| from tt_minilm.api import MiniLM | |
| with MiniLM.from_pretrained() as m: # weights (pinned revision) -> device (ETH, 12x10) -> megakernel -> warm-up | |
| emb = m.encode(["A man is playing a guitar.", "Someone is playing an instrument."]) # [2, 384] float32, unit norm | |
| print(emb @ emb.T) # cosine similarities | |
| tok = m.encode("one text", pooling="none") # [n_tokens, 384] token embeddings (L2-normalized per token) | |
| raw = m.encode(["a", "b"], normalize=False, pooling="cls", max_length=64, truncate="left") | |
| ``` | |
| `encode(texts, normalize=True, pooling="mean", max_length=None, truncate="right")` equals | |
| `SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2").encode(texts)` for the defaults: | |
| | argument | values | default | | |
| | --- | --- | --- | | |
| | `texts` | a string (returns `[384]`) or a list of strings (returns `[n, 384]`) | required | | |
| | `normalize` | L2-normalize the output | `True` | | |
| | `pooling` | `mean` (the trained pooling: mean over `[CLS]`, the word pieces and `[SEP]`), `cls`, `max`, `none` (token embeddings, a list of `[n_i, 384]`) | `mean` | | |
| | `max_length` | 2 .. `MINILM_MAX_SEQ_LEN` (256) tokens incl. `[CLS]` / `[SEP]` | 256 | | |
| | `truncate` | `right` (official: keep the start), `left` (keep the end), `error` (`InputError` if longer) | `right` | | |
| `m.tokenize(texts, max_length, truncate)` returns the id lists and `m.embed_ids(id_lists, pooling=..., normalize=...)` | |
| embeds pre-tokenized texts. `m.info` reports the engine, the device and the configuration. Texts are sent to the | |
| device in batches of up to `MINILM_MAX_BATCH` (32). | |
| `from_pretrained(model_id=None, revision=None, weights_dir=None, engine=None, device=None, dispatch=None, | |
| config=None, warmup=None)`: weights from `weights_dir` / `MINILM_WEIGHTS_DIR` / a local directory / the Hugging Face | |
| snapshot at the pinned revision; `engine` `mk` (the megakernel, default), `ttnn` (tt-nn operations) or `cpu` (the | |
| float32 reference, no device). The other server-start settings come from the `MINILM_*` environment | |
| (`tt_minilm.config.resolve_config`, the table in `SERVING.md`). | |