Quickstart¶
Four short steps: search with a published SAE, train an SAE of your own, train a retriever on it, and evaluate the result. Each links to the guide with the details.
1. Search with an SAE, nothing trained¶
An SAE on its encoder is already a sparse encoder. build_sparse_encoder takes a directory or a Hugging Face Hub repository of an exported SAE and returns a Sentence Transformers SparseEncoder:
from muscari.heads import build_sparse_encoder
model = build_sparse_encoder("tkhshtsh0917/modernbert-ja-310m-sae-65k")
queries = model.encode_query(["名古屋の名物料理は?"])
documents = model.encode_document(
[
"味噌カツは名古屋の名物で、豚カツに八丁味噌のたれをかけて食べる。",
"富士山は静岡県と山梨県にまたがる日本一高い山である。",
]
)
print(model.similarity(queries, documents))
Each vector has one dimension per latent of the SAE, 65,536 here, and a few hundred of them are non-zero. To look inside the SAE itself, token by token, and to see why two texts match:
from muscari import EncoderActivationSource, load_sae, shared_latents
sae = load_sae("tkhshtsh0917/modernbert-ja-310m-sae-65k")
source = EncoderActivationSource.from_pretrained(sae.config["base_model"])
states = source.encode_batch(["日本で一番高い山はどこ?"]) # (content tokens, d_in)
features = sae.encode(states.to(sae.encoder_weight.device)) # k non-zero per token
# Why do two texts match? The latents they share, and the token of each text behind them.
pair = ("日本で一番高い山はどこ?", "富士山は日本一高い山です。")
for entry in shared_latents(source, sae, *pair, top=6):
tokens = f"{entry.query_token} ↔ {entry.document_token}"
print(f"{entry.latent:>6} {entry.weight:5.2f} {tokens}")
43688 8.58 ? ↔ 。
12710 5.52 山 ↔ 山
475 3.97 山 ↔ 山
24623 3.81 一番高い ↔ 高い
20339 3.80 一番高い ↔ 高い
40825 3.25 日本で ↔ 日本一
A latent has no name, so this is how a score over latents is read. 一番高い and 高い, and 日本で and 日本一, are different tokens joined by a latent, which an index of exact terms cannot do. Latent 43688 is strongest on the last token of every text tried, so it adds about the same to every score: a candidate for masked_latents.
More: Build a retriever on an SAE, Evaluate and inspect an SAE.
2. Train an SAE¶
muscari train-sae streams texts through a frozen encoder and trains a Top-K SAE on the states of the content tokens. A small first run:
muscari train-sae \
--trainer topk \
--model cl-nagoya/ruri-v3-30m \
--dataset cl-nagoya/ruri-v3-dataset-ft \
--subsets jsquad \
--columns "anc=検索クエリ: " "pos=検索文書: " "neg=検索文書: " \
--num-latents 8192 \
--total-tokens 5000000 \
--dead-after-tokens 1000000 \
--out runs/sae
--columns lists the dataset columns to read and the prompt the encoder expects for each. The run writes sae.safetensors, sae_config.json and the held-out evaluation in metrics.json. The defaults without the three size options are 65,536 latents and one billion tokens.
More: Train an SAE, Files a run writes.
3. Train a retriever¶
The shortest training script puts the SAE on its encoder and trains with Sentence Transformers' trainer:
python examples/train_sae_head.py --sae runs/sae \
--query-prompt "検索クエリ: " --document-prompt "検索文書: " \
--dataset cl-nagoya/ruri-v3-dataset-ft --subsets jsquad \
--query-column anc --positive-column pos \
--max-steps 1000 --out runs/retriever
examples/train_vocabulary_head.py does the same with a SPLADE-style head on the language model, and examples/train_retriever is the script with every option: hard negatives, distillation, caps and the published recipes.
More: Train a retriever, Examples.
4. Use and evaluate it¶
from muscari.evaluation.models import load_model
model = load_model("runs/retriever/model")
queries = model.encode_query(["名古屋の名物料理は?"])
documents = model.encode_document(["味噌カツは名古屋の名物です。"])
scores = model.similarity(queries, documents)
muscari eval-retriever --model runs/retriever/model \
--benchmark jqara --caps full 64/256 --out runs/retriever/eval.json
More: Evaluate a retriever.