Skip to content
Edit this page

Examples

The scripts in examples/ are short and meant to be read. Each runs as it is and says in its first lines what it shows.

Script What it shows Needs
train_tiny.py muscari train-sae and eval-sae end to end on a random two-layer BERT; no download, seconds on a CPU base install
use_sae.py an SAE on its own: the latents of every token, the reconstruction error, and how texts compare by their pooled latents base install
use_retriever.py loading a sparse retriever, searching a few documents and showing the dimensions a query rests on; with --sae, an untrained SAE head retrieval
train_sae_head.py the plainest training of an SAE-based retriever: frozen SAE, contrastive loss, FLOPS retrieval
train_vocabulary_head.py the same for a SPLADE-style head with SPARSEUP's shift and fan-out retrieval
train_retriever/ the script with every option, and recipes with the published settings of seven methods retrieval
adapt_bidirectional.py adapting a decoder language model to bidirectional attention before retrieval training llm

An SAE on its own

python examples/use_sae.py --sae tkhshtsh0917/modernbert-ja-310m-sae-65k \
  --query "名古屋の名物料理は?" \
  --documents "味噌カツは名古屋の名物です。" "富士山は日本一高い山です。"

For every content token the script prints its strongest latents, then ranks the documents by the dot product of the pooled vectors and lists, under each document, the latents it shares with the query and the token of either text behind each one. No retrieval training is involved: this is what an SAE-based retriever starts from. Why two texts match reads such an output.

python examples/use_retriever.py --model runs/retriever/model
python examples/use_retriever.py --sae tkhshtsh0917/modernbert-ja-310m-sae-65k

A vocabulary head shows the tokens behind a query's largest weights; an SAE head shows latent ids, which muscari inspect-sae explains.

Train, one method per script

# SAE latents as the vocabulary
python examples/train_sae_head.py --sae runs/sae \
  --dataset cl-nagoya/ruri-v3-dataset-ft --subsets jsquad \
  --query-column anc --positive-column pos --out runs/sae-head

# the language-model head, shifted and limited per position
python examples/train_vocabulary_head.py --model sbintuitions/modernbert-ja-30m \
  --dataset cl-nagoya/ruri-v3-dataset-ft --subsets jsquad \
  --query-column anc --positive-column pos --out runs/vocabulary-head

# any published recipe, with evaluation before and after
python examples/train_retriever @examples/train_retriever/recipes/sparseup-v2.args \
  --model sbintuitions/modernbert-ja-30m --out runs/sparseup \
  --dataset your/dataset --negative-column negatives \
  --positive-score-column positive_score --negative-score-column negative_scores

The defaults of the short scripts are a starting point, not tuned values.

Offline smoke run

python examples/train_tiny.py --trainer topk
python examples/train_tiny.py --trainer saelens    # needs the "saelens" extra
python examples/train_tiny.py --trainer topk --workdir runs/tiny    # keep the files

The encoder is random, so the numbers say nothing about SAE quality. Use it to see the command line arguments in a working combination and the files a run produces.