Skip to content
Edit this page

Evaluate and inspect an SAE

metrics.json and eval-sae report, on held-out tokens:

Metric Meaning
fvu fraction of variance unexplained: squared reconstruction error over the variance around the mean activation
l0 mean number of active latents per token
never_fired_frac share of latents that never fire on the evaluated tokens
rare_frac share of latents that fire less than a tenth as often as the average latent
dense_frac share of latents that fire on more than 10% of the tokens
median_frequency, max_frequency firing frequency of the median latent and of the most frequent one
by_kind token share, fvu and l0 separately for each kind of text, that is for queries and documents: the prompt of a column, or its label where columns carry labels

A sparse index does not store tokens. It stores one vector per query or document, pooled over the tokens with a maximum, and pays for every non-zero dimension. text_level describes the dictionary after that pooling, where a latent counts as active in a text if it fires on at least one token:

Metric Meaning
texts, tokens_per_text number of evaluated texts of this kind and their mean length
l0, l0_median, l0_p95 active latents per text: mean, median and 95th percentile
never_fired_frac share of latents active in none of the texts
dense_frac share of latents active in more than 10% of the texts
max_document_frequency largest share of texts in which a single latent is active
flops for each pair of kinds, the sum over latents of the product of their activation probabilities: the expected number of multiplications when one text of the first kind is scored against one of the second through an inverted index

flops for queries and documents is the FLOPS figure that naver/splade reports for a query and a document index, after Paria et al.. It is a property of the SAE alone here: nothing has been trained for retrieval, and retrieval training changes it.

inspect-sae writes one record per latent to latents.jsonl:

Field Meaning
frequency, frequency_by_kind share of tokens the latent fires on, overall and among the tokens of each kind of text
text_frequency share of texts in which it fires on at least one token
mean_activation its mean value where it fires
first_token_share, last_token_share, mean_position share of its firings on the first and the last content token of a text, and their mean relative position from 0 to 1
top_tokens the tokens it fires on most, each with share (part of the latent's firings on this token) and token_rate (part of the token's occurrences that fire the latent)

These fields separate three things a latent can follow other than meaning: one token, one kind of text, or a position in the text.

Through the encoder's own head

Reconstruction error does not say whether what the SAE loses matters. Two measurements put the SAE back into the encoder and look at what the encoder does with it.

probe-sae reads a reranking dataset with one candidate document per row and ranks the candidates of each query. --benchmark jqara reads the test split of JQaRA (--split dev the development split) and, as the benchmark's own evaluator does, puts each document's title in front of its text; --without-title leaves the title out, which is how the probe results were measured. --dataset or --data-files with --query-column, --document-column, --label-column, --group-column and --title-column read any other. By default 300 queries are sampled, and every figure comes with its standard error over them (ndcg@10_stderr).

Result Meaning
dense.original nDCG@10 and MRR@10 when ranking by cosine similarity of the encoder's mean-pooled embeddings
dense.spliced the same after every content token state is replaced by the SAE's reconstruction
dense.empty the same after every content token state is replaced by the SAE's decoder bias, which is what the SAE returns for an empty code
dense.recovered (spliced - empty) / (original - empty): the share of the encoder's ranking quality that survives the SAE
dense.cosine cosine similarity between the original and the spliced embedding of the same text: mean, 5th percentile and minimum
sparse.plain ranking by the dot product of the pooled SAE features: log1p, then the maximum over the content tokens
sparse.idf the same with each latent weighted by its inverse document frequency among the candidate documents
sparse.bm25 ranking by BM25 over summed activations, as Latent Terms does: the activations of a text's tokens are summed and passed through --bm25-transform (the square root by default), a document's weights take the place of term frequencies, and --bm25-k1 (8) and --bm25-b (0.7) are the paper's defaults
sparse.sum_dot ranking by the dot product of the same summed weights, without the statistics of BM25
sparse.l0 active latents per query and per document
sparse.flops the sum over latents of the share of queries times the share of documents in which the latent is active
sparse.masked_latents the latents left out with --masked-latents, each with those two shares and its part of flops before it was removed
hybrid.rrf ranking that fuses dense.original and sparse.plain, which come from the same pass through the encoder, by reciprocal rank fusion (constant 60, nothing to tune)
hybrid.weighted the same two rankings fused as a weighted sum of their scores, each standardised over the query's candidates, for dense weights from 0.1 to 0.9; choose the weight on one split and report it on another

Every hybrid entry also holds over_dense and over_sparse, its mean per-query gain over each of the two rankings with the standard error of that paired difference. sparse.bm25 holds over_plain, over_idf and over_dense in the same way, and majority_latents, the number of latents active in more than half of the documents. The paper's IDF, log((N - n + 0.5) / (n + 0.5)), is negative for those latents, which words rarely are and the latents of an SAE can be, so the default is --bm25-idf lucene, log(1 + (N - n + 0.5) / (n + 0.5)); --bm25-idf okapi gives the paper's formula. The paper does not define the length of a document; here it is the sum of the document's weights. The measurements page has a measurement. The dense and hybrid rows need an encoder trained to produce mean-pooled embeddings, such as ruri-v3. The sparse rows are the state of an SAE-based sparse retriever before it is trained, so they are a starting point and not a result.

For an encoder that still has its language-model head, eval-sae --mlm-texts 100 adds mlm: the masked-language-model loss with the encoder untouched (loss_clean), with the final states of the content tokens replaced by the SAE's reconstruction (loss_spliced) and by their mean (loss_mean), and recovered = (loss_mean - loss_spliced) / (loss_mean - loss_clean). The Shizuku SAE card reports this quantity. It does not state the masking rate; the default here is 15% and the value used is written next to the result.

After training, metrics.json also holds:

Metric Meaning
train_unused_frac share of latents that did not fire during the last 10 million training tokens (the whole run if it was shorter)
tokens_encoded_by_kind how many training tokens came from queries and from documents
epochs_started how many passes over the deduplicated texts were started
encoder_seconds, sae_seconds, wall_seconds time spent in the encoder, in the SAE updates (topk trainer) and in total

Held-out texts are chosen by a hash of the text itself, so a document that appears in many rows is never split between training and evaluation. By default 32 tokens per latent are evaluated, and at least 200,000; with fewer, rare latents have no chance to fire and never_fired_frac is inflated. eval-sae evaluates only the held-out texts when it is given the data the SAE was trained on, and everything otherwise.

The probe measures ranking with nothing trained. No metric here measures the quality of a trained retriever.

Why two texts match

inspect-sae describes the latents over a corpus. shared_latents answers a question about one pair of texts: which latents are active in both, how much each adds to their score, and on which token of either text it is strongest.

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"])

pair = ("日本で一番高い山はどこ?", "富士山は日本一高い山です。")
for entry in shared_latents(source, sae, *pair, top=8):
    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  日本で ↔ 日本一
 55309  3.15  山 ↔ 山
 14128  2.99  はどこ ↔ 高い

Each text is pooled as an SAE head pools it before any training, the maximum over its tokens of log1p(activation), and the score of the pair is the dot product of the two vectors. The weights of all shared latents add up to it: 59.96 here, over 24 latents. token_latents returns the tokens of one text with their activations, and examples/use_sae.py prints both for a query and several documents.

The output above is from the SAE this project publishes, run on Apple MPS. With three queries and nine pairs in all, which is a look and not a measurement, it showed three things a list of latent numbers does not:

  • Different tokens joined by a latent. Latents 24623 and 20339 link 一番高い to 高い, and 40825 links 日本で to 日本一. In other pairs latent 35476 linked エンジニア to 開発 and latent 6688 linked できる to 可能な. An index of exact terms has none of these matches.
  • Latents that tell nothing apart. Latent 43688 is the largest entry of all nine pairs, with a weight of 7.2 to 8.6, and in every text it is strongest on the last token, whether that is ?, 。 or the last word of a query without punctuation. It adds about the same to every score, and its posting list would hold every document. Latents 37025 and 59878 are among the eight largest entries of eight and of seven of the nine pairs. masked_latents removes such latents from a head, and the next section measures what that changes.
  • Why a ranking is wrong. For the query 在宅でできるエンジニアの仕事 the untrained vectors score 東京本社に毎日出社できる営業職を募集しています。 (32.92) above フルリモート勤務可能な開発職を募集しています。 (27.95). Four latents that link できる to できる add 11.8 to the first. The links to the second that carry the meaning are weaker: エンジニア to 開発 adds 3.28 on latent 35476, and できる to 可能な 1.24.

A relevant document that shares no latent with its query fails in the representation, before any weighting; one that shares latents and still ranks low fails in the weights. These vectors are those of a head before retrieval training, and nothing here says how a trained one ranks.

Latents that are active everywhere

A latent that is active in nearly every text adds about the same to every score, so it ranks nothing, and an inverted index keeps a posting for it in nearly every document. inspect-sae lists the latents of an SAE by text_frequency on the texts you give it. probe-sae leaves latents out of the sparse vectors of queries and documents with --masked-latents, which is what head_kwargs={"masked_latents": [...]} does in an SAE head:

muscari probe-sae --sae tkhshtsh0917/modernbert-ja-310m-sae-65k --benchmark jqara \
  --max-queries 100 --masked-latents 43688

sparse.flops is the FLOPS of the pooled queries and documents, and sparse.masked_latents holds, for each latent left out, the share of queries and of documents it was active in and its part of the FLOPS. The dense rankings do not change. To choose the latents by how often they are active, and to compare several choices after pooling once:

from muscari.analysis.probe import pool_collection, probe_collection, text_frequencies, without

collection = pool_collection(source, sae, examples, "", "")  # the two prompts; none here
_, document_frequency = text_frequencies(collection)
frequent = (document_frequency > 0.5).nonzero().flatten().tolist()

before = probe_collection(collection, examples)["sparse"]
after = probe_collection(without(collection, frequent), examples)["sparse"]

examples are RerankingExamples, as group_reranking_rows builds them from the rows of a reranking dataset.

On the SAE this project publishes, one latent is active in every text of the sample and leaving it out changes nothing in the ranking; the 72 latents that are active in more than half of the documents carry half of the FLOPS, and the ranking is not worse without them. The table is under The published SAE.