A smaller encoder for queries¶
Documents are encoded once, offline. Queries are encoded at search time, so the query encoder sets the latency of every search. Lassance and Clinchant ("An Efficiency Study for SPLADE Models", 2022) therefore give queries a smaller encoder than documents, next to an L1 regulariser that keeps queries short. muscari builds such a model with Sentence Transformers' Router: encode_query goes through one stack of modules and encode_document through another, and both write vectors in the same dimensions.
Measured once, on a small scale
With modernbert-ja-310m for documents and modernbert-ja-30m for queries, a vocabulary head lost 0.05 nDCG@10 on JQaRA and encoded a query in a fifth of the time on a CPU. The stitched SAE head of this page was far behind after the same short training and had not converged. The tables are under A smaller encoder for queries.
Heads on the language model¶
Two encoders with the same tokenizer have the same vocabulary, so their vocabulary heads share their dimensions as they are. Build each side with the usual builder and combine them:
from muscari.heads import build_vocabulary_encoder, combine_encoders
head = {"shift": "auto", "shift_texts": sample, "fan_out": 12}
documents = build_vocabulary_encoder("sbintuitions/modernbert-ja-310m", **head)
queries = build_vocabulary_encoder("sbintuitions/modernbert-ja-30m", **head)
model = combine_encoders(queries, documents)
model.encode_query(["名古屋の名物料理は?"]) # through the 30M encoder
model.encode_document(["味噌カツは名古屋の名物です。"]) # through the 310M encoder
combine_encoders refuses encoders whose output sizes differ or whose tokenizers give a token different ids. Nothing aligns the two sides at this point: each head reads its own language model. Training aligns them.
SAE heads¶
An SAE belongs to one encoder: its latents are directions in that encoder's hidden states. A second encoder with another hidden size needs a head of its own that lands in the same latents. build_asymmetric_sae_encoder builds it in two steps:
- With the same tokenizer the two encoders see the same tokens, so the states of the small encoder are regressed onto the states of the large one, token by token, over a sample of queries (a ridge regression,
fit_state_map). - The SAE's encoder is composed with that linear map, which gives a head from the small hidden size to the SAE's latents (
stitched_query_head). This head is trainable; the document side keeps the SAE as it is.
from muscari.heads import build_asymmetric_sae_encoder
model = build_asymmetric_sae_encoder(
"tkhshtsh0917/modernbert-ja-310m-sae-65k", # documents: the SAE on its encoder
"sbintuitions/modernbert-ja-30m", # queries: smaller, same tokenizer
texts=query_sample, # a few hundred queries to fit the map on
)
The map is a start, not a result: train the model before using it, and expect it to need more training than a symmetric model. In the one measurement here 2,000 steps were not enough. Starting the query head this way is this project's own choice and follows no published method.
Training¶
A routed model needs to know which column of the training data goes through which route. With Sentence Transformers' trainer that is router_mapping:
from sentence_transformers.sparse_encoder import SparseEncoderTrainingArguments
arguments = SparseEncoderTrainingArguments(
output_dir="runs/asymmetric/checkpoints",
router_mapping={
"anchor": "query",
"positive": "document",
"negative_1": "document",
},
)
The recipe script does this with one option, for every head:
python examples/train_retriever --head vocab --shift auto --fan-out 12 \
--model sbintuitions/modernbert-ja-310m --query-model sbintuitions/modernbert-ja-30m \
--query-regularizer l1 \
--dataset your/dataset --out runs/asymmetric
--query-regularizer l1 is the other half of the efficiency study: it penalises every query weight alike, which shortens queries more directly than FLOPS does.
Saving, loading and evaluating¶
The model saves and loads like any other; muscari.evaluation.models.load_model and muscari eval-retriever accept it, and muscari.heads.head_of(model, "query") and head_of(model, "document") return the two heads. LoadBalancedSpladeLoss does not support routed models.