File size: 11,953 Bytes
2ff06a5 e005eea 2ff06a5 f60b8b2 65e48be 2ff06a5 4c9ebf6 2ff06a5 f60b8b2 2ff06a5 f60b8b2 e005eea f60b8b2 2ff06a5 e005eea f60b8b2 e005eea 65e48be 2ff06a5 f60b8b2 e005eea f60b8b2 e005eea f60b8b2 2ff06a5 f60b8b2 2ff06a5 f60b8b2 2ff06a5 09b6ae9 f60b8b2 2ff06a5 f60b8b2 2ff06a5 e005eea 2ff06a5 f60b8b2 2ff06a5 2e5574c 2ff06a5 2e5574c 2ff06a5 09b6ae9 190ef26 2ff06a5 f60b8b2 e005eea f60b8b2 e005eea d86454c e005eea 845d613 e005eea d86454c e005eea d86454c e005eea 2ff06a5 09b6ae9 d86454c 760f88c d86454c 2ff06a5 e005eea 760f88c e005eea d86454c e005eea 760f88c 09c7453 760f88c 09c7453 2e5574c f60b8b2 2e5574c 09c7453 2e5574c 09b6ae9 f60b8b2 09b6ae9 760f88c 09b6ae9 e005eea d86454c 09b6ae9 f60b8b2 09b6ae9 e005eea 09b6ae9 f60b8b2 09b6ae9 e005eea 2ff06a5 e005eea 760f88c 2ff06a5 e005eea 2ff06a5 f60b8b2 e005eea f60b8b2 e005eea f60b8b2 e005eea f60b8b2 2ff06a5 d86454c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | import time
import html
import gradio as gr
from datasets import load_dataset, load_from_disk
from huggingface_hub import hf_hub_download
import pandas as pd
from sentence_transformers import SentenceTransformer
from sentence_transformers.quantization import quantize_embeddings
import faiss
import numpy as np
# Load titles, texts, and int8 embeddings in a lazy Dataset, allowing us to efficiently access specific rows on demand
# Note that we never actually use the int8 embeddings for search directly, they are only used for rescoring after the binary search
title_text_int8_dataset = load_dataset(
"sentence-transformers/quantized-retrieval-data", split="train"
).select_columns(["url", "title", "text", "embedding"])
# title_text_int8_dataset = load_from_disk("wikipedia-mxbai-embed-int8-index").select_columns(["url", "title", "text", "embedding"])
TOTAL_NUM_DOCS = title_text_int8_dataset.num_rows
# Load the binary indices
binary_index_path = hf_hub_download(
repo_id="sentence-transformers/quantized-retrieval-data",
filename="wikipedia_ubinary_faiss_50m.index",
local_dir=".",
repo_type="dataset",
)
binary_ivf_index_path = hf_hub_download(
repo_id="sentence-transformers/quantized-retrieval-data",
filename="wikipedia_ubinary_ivf_faiss_50m.index",
local_dir=".",
repo_type="dataset",
)
binary_index: faiss.IndexBinaryFlat = faiss.read_index_binary(binary_index_path)
binary_ivf_index: faiss.IndexBinaryIVF = faiss.read_index_binary(binary_ivf_index_path)
# Load the SentenceTransformer model for embedding the queries
model = SentenceTransformer("mixedbread-ai/mxbai-embed-large-v1")
if model.device.type == "cuda":
model.bfloat16()
warmup_queries = [
"What is the capital of France?",
"Who is the president of the United States?",
"What is the largest mammal?",
"How to bake a chocolate cake?",
"What is the theory of relativity?",
]
model.encode_query(warmup_queries)
def search(
query,
top_k: int = 20,
rescore_multiplier: int = 4,
use_approx: bool = True,
):
# 1. Embed the query as float32
start_time = time.time()
query_embedding = model.encode_query(query)
embed_time = time.time() - start_time
# 2. Quantize the query to ubinary
start_time = time.time()
query_embedding_ubinary = quantize_embeddings(
query_embedding.reshape(1, -1), "ubinary"
)
quantize_time = time.time() - start_time
# 3. Search the binary index (either exact or approximate)
index = binary_ivf_index if use_approx else binary_index
start_time = time.time()
_scores, binary_ids = index.search(
query_embedding_ubinary, top_k * rescore_multiplier
)
binary_ids = binary_ids[0]
search_time = time.time() - start_time
num_docs_searched = len(binary_ids)
# 4. Load the corresponding int8 embeddings
start_time = time.time()
int8_embeddings = np.array(
title_text_int8_dataset[binary_ids]["embedding"], dtype=np.int8
)
load_int8_time = time.time() - start_time
# 5. Rescore the top_k * rescore_multiplier using the float32 query embedding and the int8 document embeddings
start_time = time.time()
scores = query_embedding @ int8_embeddings.T
rescore_time = time.time() - start_time
# 6. Sort the scores and return the top_k
start_time = time.time()
indices = scores.argsort()[::-1][:top_k]
top_k_indices = binary_ids[indices]
top_k_scores = scores[indices]
sort_time = time.time() - start_time
# 7. Load titles and texts for the top_k results
start_time = time.time()
raw_top_k_titles = title_text_int8_dataset[top_k_indices]["title"]
top_k_urls = title_text_int8_dataset[top_k_indices]["url"]
top_k_texts = title_text_int8_dataset[top_k_indices]["text"]
load_text_time = time.time() - start_time
# Build HTML cards for each result so the full row is visible at once
cards = []
for i in range(len(top_k_indices)):
title = html.escape(str(raw_top_k_titles[i]))
url = html.escape(str(top_k_urls[i]))
text = html.escape(str(top_k_texts[i]))
score_str = f"{top_k_scores[i]:.2f}"
rank_str = str(i + 1)
binary_rank_str = str(indices[i] + 1)
card_html = f"""
<div style=\"border: 1px solid var(--border-color-primary, #e0e0e0); border-radius: var(--block-radius); padding: 10px 12px; margin-bottom: 10px; background-color: var(--block-background-fill, transparent); color: inherit;\">
<div style=\"display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; margin-bottom: 4px;\">
<div style=\"font-size: 16px; font-weight: 600; min-width: 0;\">
<a href=\"{url}\" target=\"_blank\" style=\"text-decoration: none; color: var(--link-text-color, #1f6feb); padding-left: 0;\">{title}</a>
</div>
<div style=\"font-size: 12px; color: var(--body-text-color-subdued, #586069); text-align: right; white-space: nowrap;\">
Score: {score_str} • Rank: {rank_str} • Binary rank: {binary_rank_str}
</div>
</div>
<div style=\"font-size: 13px; line-height: 1.4; max-height: 8em; overflow: hidden;\">{text}</div>
</div>
"""
cards.append(card_html)
if cards:
cards_html = "\n".join(cards)
else:
cards_html = "<div>No results.</div>"
total_retrieval_time = (
quantize_time
+ search_time
+ load_int8_time
+ rescore_time
+ sort_time
+ load_text_time
)
num_docs_retrieved = len(top_k_indices)
search_mode = "Approximate (IVF)" if use_approx else "Exact"
summary_md = f"""
<div style=\"border: 1px solid var(--border-color-primary, #e0e0e0); border-radius: var(--block-radius); padding: 10px 12px; background-color: var(--block-background-fill, transparent);\">
<h3 style=\"margin-top: 0;\">Search Summary</h3>
<ul style=\"margin-top: 0; margin-bottom: 8px; padding-left: 18px;\">
<li>Total docs in corpus: {TOTAL_NUM_DOCS:,}</li>
<li>Docs searched: {num_docs_searched}</li>
<li>Docs retrieved: {num_docs_retrieved}</li>
<li>Search mode: {search_mode}</li>
</ul>
<h4>Timings (in seconds)</h4>
<ul style=\"margin-top: 0; margin-bottom: 0; padding-left: 18px;\">
<li>Embed on CPU: {embed_time:.4f}</li>
<li>Quantize: {quantize_time:.4f}</li>
<li>Search: {search_time:.4f}</li>
<li>Load int8: {load_int8_time:.4f}</li>
<li>Rescore: {rescore_time:.4f}</li>
<li>Sort: {sort_time:.4f}</li>
<li>Load text: {load_text_time:.4f}</li>
</ul>
<h5>Total retrieval time: {total_retrieval_time:.4f} seconds</h5>
</div>"""
return cards_html, summary_md
css = """
.no-pad-container {
--block-padding: 0px;
}
"""
with gr.Blocks(title="Quantized Retrieval") as demo:
with gr.Row():
with gr.Column(scale=3):
gr.HTML(
"""
<div style='border: 1px solid var(--border-color-primary, #e0e0e0); border-radius: var(--block-radius); padding: 12px 14px; background-color: var(--block-background-fill, transparent);'>
<h1 style='margin-top: 0;'>Quantized Retrieval - Binary Search with Scalar (int8) Rescoring</h1>
This demo showcases retrieval using<a href="https://huggingface.co/blog/embedding-quantization" style="padding-left: 0.5ch; padding-right: 0.5ch;">quantized embeddings</a>on a CPU. The corpus consists of<a href="https://huggingface.co/datasets/sentence-transformers/quantized-retrieval-data" style="padding-left: 0.5ch; padding-right: 0.5ch;">41 million texts</a>from Wikipedia articles.
</div>
""",
elem_classes="no-pad-container",
)
with gr.Accordion("Click to learn about the retrieval process", open=False):
gr.Markdown(
"""
Details:
1. The query is embedded using the [`mixedbread-ai/mxbai-embed-large-v1`](https://huggingface.co/mixedbread-ai/mxbai-embed-large-v1) SentenceTransformer model.
2. The query is quantized to binary using the `quantize_embeddings` function from the SentenceTransformers library.
3. A binary index (41M binary embeddings; 5.2GB of memory/disk space) is searched using the quantized query for the top 80 documents.
4. The top 80 documents are loaded on the fly from an int8 index on disk (41M int8 embeddings; 0 bytes of memory, 47.5GB of disk space).
5. The top 80 documents are rescored using the float32 query and the int8 embeddings to get the top 20 documents.
6. The top 20 documents are sorted by score.
7. The titles and texts of the top 20 documents are loaded on the fly from disk and displayed.
This process is designed to be memory efficient and fast, with the binary index being small enough to fit in memory and the int8 index being loaded as a view to save memory.
In total, this process requires keeping 1) the model in memory, 2) the binary index in memory, and 3) the int8 index on disk. With a dimensionality of 1024,
we need `1024 / 8 * num_docs` bytes for the binary index and `1024 * num_docs` bytes for the int8 index.
This is notably cheaper than doing the same process with float32 embeddings, which would require `4 * 1024 * num_docs` bytes of memory/disk space for the float32 index, i.e. 32x as much memory and 4x as much disk space.
Additionally, the binary index is much faster (up to 32x) to search than the float32 index, while the rescoring is also extremely efficient. In conclusion, this process allows for fast, scalable, cheap, and memory-efficient retrieval.
Feel free to check out the [code for this demo](https://huggingface.co/spaces/sentence-transformers/quantized-retrieval/blob/main/app.py) to learn more about how to apply this in practice.
Notes:
- The approximate search index (a binary Inverted File Index (IVF)) is in beta and has not been trained with a lot of data.
"""
)
query = gr.Textbox(
label="Query for Wikipedia articles",
placeholder="Enter a query to search for relevant texts from Wikipedia.",
)
search_button = gr.Button(value="Search", variant="secondary")
with gr.Column(scale=1):
top_k = gr.Slider(
minimum=10,
maximum=1000,
step=1,
value=20,
label="Number of documents to retrieve",
info="Number of documents to retrieve using binary search",
)
rescore_multiplier = gr.Slider(
minimum=1,
maximum=10,
step=1,
value=4,
label="Rescore multiplier",
info="Search for `rescore_multiplier` as many documents to rescore",
)
use_approx = gr.Radio(
choices=[("Approximate Search", True), ("Exact Search", False)],
value=True,
label="Search Settings",
)
with gr.Row():
with gr.Column(scale=3):
cards = gr.HTML(label="Results", elem_classes="no-pad-container")
with gr.Column(scale=1):
summary = gr.Markdown(label="Search Summary")
examples = gr.Examples(
examples=[
"What is the coldest metal to the touch?",
"Who won the FIFA World Cup in 2018?",
"How to make a paper airplane?",
"Who was the first woman to cross the Pacific ocean by plane?",
],
fn=search,
inputs=[query],
outputs=[cards, summary],
cache_examples=False,
run_on_click=True,
)
query.submit(
search,
inputs=[query, top_k, rescore_multiplier, use_approx],
outputs=[cards, summary],
)
search_button.click(
search,
inputs=[query, top_k, rescore_multiplier, use_approx],
outputs=[cards, summary],
)
demo.queue()
demo.launch(theme=gr.themes.Base(), css=css)
|