Skip to content
Edit this page

Build a retriever on the vocabulary

from muscari.heads import build_vocabulary_encoder

model = build_vocabulary_encoder(
    "sbintuitions/modernbert-ja-30m",
    shift="auto",
    shift_texts=documents_sample,
    fan_out=16,
)

VocabularyPooling is SPLADE's pooling with the changes of SPARSEUP: a shift subtracted from the logits before the ReLU, estimated here as a quantile of the head's logits on your documents, and a limit on the logits each position keeps. On modernbert-ja-30m, plain SPLADE pooling starts with nearly every dimension active and its training collapsed in this project's runs, as SPARSEUP and Jang et al. report for ModernBERT; with the shift it trains. Entries that differ in case or word-boundary marker can be folded together, and entries such as punctuation can be masked. rescale_head divides the head's projection instead (Jang et al.), and graft_head puts a head on an encoder that was saved without one.

An experimental alternative to the shift thresholds each token's probabilities instead of its logits, so that the head is sparse by its normalisation and trains without a sparsity regulariser:

model = build_vocabulary_encoder(
    "sbintuitions/modernbert-ja-30m",
    probability_threshold=1 / 64,
    energy=0.25,
    energy_center="auto",
    saturate=False,
    shift_texts=documents_sample,
    fan_out=12,
)

capacity=0.05 adds a cap: no entry should appear in more than 5% of the texts, held by one bias per entry that training adjusts. Both are the project's own. On modernbert-ja-30m the threshold gave more accurate small vectors than the shift, and the cap of 5% cut the FLOPS to about half at the same accuracy; on modernbert-ja-310m the threshold stayed 0.03 to 0.04 nDCG@10 below the shifted head and the SAE head, with or without the cap. Read the measurements before choosing either.

Train it

examples/train_vocabulary_head.py is the shortest complete training script, with the shift estimated from the training documents; Train a retriever covers the losses and every option.