all-MiniLM-L6-v2 / code /PYTHON.md
changh95's picture
tt-model push all-minilm-l6-v2 (container)
1e8fbed verified
|
Raw History Blame Contribute Delete
2.58 kB

all-MiniLM-L6-v2 on Tenstorrent p150: Python API

tt_minilm runs 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).

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).