Downloads · 30 days
27
29% of all-time downloads
majdalkawaas/matryoshka-hypencoder
matryoshka-hypencoder is a feature extraction model from majdalkawaas. Use it when you need embeddings to search or compare text. The card lists the license as apache-2.0.
[](https://github.com/MajdAlkawaas/hypencoder-paper) [](https://arxiv.org/abs/2607.17457) [](./LICENSE)
Downloads · 30 days
27
29% of all-time downloads
All-time downloads
93
Public
Parameters
140M
800 MB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors558 MB · 70%
How the weights are stored.
F32140M · 100%
From the Hugging Face model README
The Matryoshka Hypencoder is a novel neural information retrieval model that combines the expressive power of query-specific relevance functions (Hypencoders) with the computational flexibility of Matryoshka Representation Learning (MRL).
This model generates a query-specific scoring network (a "Q-Net") whose parameters are structured as nested representations. This allows a single model to generate Q-Nets of varying widths (e.g., hidden dimensions of 128, 256, 512, or 768), enabling a dynamic trade-off between retrieval effectiveness and inference latency without needing to train or deploy multiple separate models.
This model was developed as part of a Master's dissertation extending the original Hypencoder architecture by Killingback et al.
[128, 256, 512, 768]The Matryoshka Hypencoder exhibits the property of graceful degradation. Truncating the Q-Net size drastically reduces inference latency while maintaining highly competitive retrieval performance.
In-Domain Performance (nDCG@10) & Efficiency:
| Q-Net Dimension | TREC DL '19 | TREC DL '20 | Q-Net Params | Avg. Latency Speedup* |
|---|---|---|---|---|
| 768 (Full) | 0.734 | 0.723 | 3.54M | 1.0x (Baseline) |
| 512 | 0.738 | 0.729 | 1.70M | 1.6x |
| 256 | 0.741 | 0.728 | 0.52M | 3.4x |
| 128 | 0.734 | 0.725 | 0.18M | 6.0x |
*Speedup refers to the reduction in document scoring time over a large corpus compared to the full 768-dim Q-Net.
In-domain performance comparison:
| Model | # Q-Net Params | DL'19 (nDCG@10) | DL'20 (nDCG@10) | Dev (MRR@10) |
|---|---|---|---|---|
| MH d=768 | 4,134,912 | .734 | .723 | .380† |
| MH d=512 | 1,970,176 | .738 | .729 | .381† |
| MH d=256 | 591,616 | .741 | .728 | .379† |
| MH d=128 | 197,632 | .734 | .725 | .375†* |
| Hyp-Fr (Ours) | ≈4.1M | .728 | .729 | .373† |
| Original Hypencoder | ≈4.1M | .742 | .730 | .386 |
| BM25 | - | .498 | .479 | .188 |
(MH d refers to our Matryoshka Hypencoder at a Q-Net hidden dimension of d. Hyp-Fr is our frozen-encoder model. † indicates a statistically significant difference (p < 0.05) compared to the Original Hypencoder baseline, and * shows significance compared to MH d=768.)
Zero-shot performance (nDCG@10) on out-of-domain BEIR datasets:
| Model | TC | FQA | DB | NF | WT |
|---|---|---|---|---|---|
| MH d=768 | .685 | .312 | .414 | .324 | .272 |
| MH d=512 | .684 | .311 | .414 | .324 | .271 |
| MH d=256 | .678† | .306†* | .414 | .324 | .266 |
| MH d=128 | .670† | .304†* | .412 | .323 | .259* |
| Hyp-Fr (Ours) | .699 | .314 | .419 | .311† | .250 |
| Original Hypencoder | .698 | .314 | .419 | .324 | .258 |
| BM25 | .610 | .253 | .303 | .318 | .332 |
(TC is TREC-COVID, FQA is FiQA, NF is NFCorpus, DB is DBPedia, and WT is Webis-Touché. † indicates a statistically significant difference (p < 0.05) compared to the Original Hypencoder baseline, and * shows significance compared to MH d=768.)
Average document scoring throughput:
| Q-Net | Dev | TC | FQA | DB | NF | WT |
|---|---|---|---|---|---|---|
| d=768 | 0.9M (1.0x) | 0.9M (1.0x) | 0.7M (1.0x) | 0.9M (1.0x) | 0.2M (1.0x) | 0.9M (1.0x) |
| d=512 | 1.4M (1.6x) | 1.3M (1.5x) | 0.9M (1.3x) | 1.4M (1.6x) | 0.2M (1.0x) | 1.4M (1.6x) |
| d=256 | 3.0M (3.4x) | 2.4M (2.8x) | 1.4M (2.0x) | 3.0M (3.4x) | 0.2M (1.1x) | 2.7M (3.0x) |
| d=128 | 5.6M (6.3x) | 3.3M (3.9x) | 1.8M (2.6x) | 5.6M (6.3x) | 0.2M (1.1x) | 4.1M (4.6x) |
(Documents scored per second. The speed up compared to the full-size Q-Net (d=768) is given in parentheses.)
Because this model uses a custom architecture, it requires the custom hypencoder-cb codebase to run. It cannot be used with standard transformers pipelines out-of-the-box.
First, clone the official implementation repository and install the dependencies:
git clone <URL_TO_YOUR_GITHUB_REPO> # Replace with your actual GitHub repo URL
cd hypencoder-paper
pip install -r requirements.txt
You can load the model directly from the Hugging Face Hub using the custom classes provided in the repository:
import torch
from hypencoder_cb.modeling.hypencoder import HypencoderDualEncoder
from transformers import AutoTokenizer
model_id = "majdalkawaas/matryoshka-hypencoder"
device = "cuda" if torch.cuda.is_available() else "cpu"
# Load the custom model architecture and weights
model = HypencoderDualEncoder.from_pretrained(model_id).to(device).eval()
tokenizer = AutoTokenizer.from_pretrained(model_id)
print(f"Model successfully loaded. Supported Q-Net sizes: {model.config.loss_kwargs[0]['matryoshka_dims']}")
To generate a Q-Net for a specific query and a specific Matryoshka dimension (e.g., 256):
from hypencoder_cb.modeling.q_net import MatryoshkaQNetFactory
from hypencoder_cb.modeling.similarity_and_losses import _truncate_parameters
query = "What is a hypernetwork?"
target_dimension = 256
# Tokenize the query
inputs = tokenizer(query, return_tensors="pt", truncation=True).to(device)
with torch.no_grad():
# 1. Generate the full-size parameters from the Hyper-head
query_output = model.query_encoder(**inputs)
# 2. Build the specific Q-Net using the Factory
factory = MatryoshkaQNetFactory(model.query_encoder.weight_to_model_converter)
q_nets = factory.build(
weight_matrices=query_output.generated_matrices,
bias_vectors=query_output.generated_vectors,
matryoshka_dims=[target_dimension],
is_training=False
)
# This is your highly efficient, query-specific scoring function
my_q_net = q_nets[target_dimension]
# You can now use `my_q_net` to score pre-computed document embeddings!
If you use this model in your research, please cite the original Hypencoder paper and this dissertation work.
@inproceedings{Alkawaas_2026,
title={The Matryoshka Hypencoder},
url={http://dx.doi.org/10.1145/3805712.3809980},
DOI={10.1145/3805712.3809980},
booktitle={Proceedings of the 49th International ACM SIGIR Conference on Research and Development in Information Retrieval},
publisher={ACM},
author={Alkawaas, Majd and MacAvaney, Sean},
year={2026},
month=July, pages={3574–3579} }