Skip to content
Edit this page

Build a retriever on an SAE

muscari.heads puts an exported SAE on top of its encoder as a SparseEncoder. The head, SAEPooling, takes the place of the language-model head and pooling of a SPLADE model: the hidden state of every content token goes through the SAE's encoder, the active latents are saturated with log1p, and the maximum over the tokens gives one dimension per latent.

from muscari.heads import build_sparse_encoder

model = build_sparse_encoder(
    "runs/ruri-v3-310m-sae",
    query_prompt="検索クエリ: ",
    document_prompt="検索文書: ",
)
queries = model.encode_query(["名古屋のエンジニア求人"])
documents = model.encode_document(["名古屋市内の開発職を募集しています。", "東京の営業職です。"])
scores = model.similarity(queries, documents)

The sources disagree on most choices around this head, and each is a setting: the SAE frozen (SPLARE, Shizuku) or trained with the encoder (trainable=True: SAE-SPLADE, SlideOn); log1p or raw activations (saturation="none": SlideOn); the SAE's own k per token or another (k=8: SAE-SPLADE); unit-length token states (an SAE trained with --normalize-inputs: SlideOn); a trained factor per latent (latent_gate=True). An SAE of an inner layer of a decoder language model, for example one brought in with muscari import-sae, goes on that model cut after the layer, optionally with bidirectional attention (bidirectional=True: SPLARE).

Latents that are active in nearly every text cannot tell texts apart. inspect-sae lists them by text_frequency, and head_kwargs={"masked_latents": [...]} removes them from the output. probe-sae --masked-latents shows what leaving them out changes before anything is trained: Latents that are active everywhere.

From a published SAE

build_sparse_encoder and SAEPooling.from_sae take a directory or a Hugging Face Hub repository that holds sae.safetensors and sae_config.json:

from muscari.heads import build_sparse_encoder

model = build_sparse_encoder("tkhshtsh0917/modernbert-ja-310m-sae-65k")

This project publishes two SAEs: that one, of sbintuitions/modernbert-ja-310m, and tkhshtsh0917/ruri-v3-pt-310m-sae-65k, of cl-nagoya/ruri-v3-pt-310m. The second encoder expects a prefix before each text, which the SAE was trained with:

model = build_sparse_encoder("tkhshtsh0917/ruri-v3-pt-310m-sae-65k", query_prompt="検索クエリ: ", document_prompt="検索文書: ")

The encoder is the base model the SAE records. Before any training the model already ranks: its vectors are the ones muscari probe-sae reports as sparse.plain. examples/use_retriever.py --sae ... searches a few documents this way, and examples/use_sae.py shows the latents of each token without any head.

Train it

examples/train_sae_head.py is the shortest complete training script; Train a retriever covers the losses and every option. Train an SAE head in float32: with bfloat16 autocast it ended clearly lower in one comparison.