Spaces:
Running on Zero
Running on Zero
Shuai Wang commited on
Commit ·
d01bc67
1
Parent(s): f09db0a
Fix @spaces.GPU decorator, stable search timing via averaging
Browse files- app.py +2 -5
- backend.py +7 -2
app.py
CHANGED
|
@@ -24,10 +24,7 @@ import matplotlib.pyplot as plt
|
|
| 24 |
import numpy as np
|
| 25 |
|
| 26 |
|
| 27 |
-
|
| 28 |
-
import spaces
|
| 29 |
-
except ImportError:
|
| 30 |
-
spaces = None
|
| 31 |
|
| 32 |
from backend import get_model_and_tokenizer, run_all_sizes, run_all_sizes_indexed, SIZES, DEVICE
|
| 33 |
from examples import HOW_TO_USE_CONTENT
|
|
@@ -1104,7 +1101,7 @@ def on_size_change(results, size_idx: int, corpus_name: str, active_qrels):
|
|
| 1104 |
page_indicator_html, gr.update(interactive=False), gr.update(interactive=total_pages > 1), 0
|
| 1105 |
|
| 1106 |
|
| 1107 |
-
@spaces.GPU
|
| 1108 |
def run_demo(query: str, corpus_name: str, size_idx: int, active_qrels):
|
| 1109 |
"""Run all 6 Starbucks sizes and return results for the selected size."""
|
| 1110 |
import torch
|
|
|
|
| 24 |
import numpy as np
|
| 25 |
|
| 26 |
|
| 27 |
+
import spaces
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
from backend import get_model_and_tokenizer, run_all_sizes, run_all_sizes_indexed, SIZES, DEVICE
|
| 30 |
from examples import HOW_TO_USE_CONTENT
|
|
|
|
| 1101 |
page_indicator_html, gr.update(interactive=False), gr.update(interactive=total_pages > 1), 0
|
| 1102 |
|
| 1103 |
|
| 1104 |
+
@spaces.GPU
|
| 1105 |
def run_demo(query: str, corpus_name: str, size_idx: int, active_qrels):
|
| 1106 |
"""Run all 6 Starbucks sizes and return results for the selected size."""
|
| 1107 |
import torch
|
backend.py
CHANGED
|
@@ -254,13 +254,18 @@ def compute_ranking(
|
|
| 254 |
query_emb: np.ndarray,
|
| 255 |
doc_embs: np.ndarray,
|
| 256 |
documents: list[str],
|
|
|
|
| 257 |
) -> tuple[list[tuple[str, float]], float]:
|
| 258 |
"""Rank documents via dot-product similarity (numpy fallback)."""
|
| 259 |
-
|
| 260 |
scores = np.dot(doc_embs, query_emb)
|
| 261 |
ranked_idx = np.argsort(scores)[::-1]
|
| 262 |
-
search_time_ms = (time.perf_counter() - start) * 1000
|
| 263 |
ranked = [(documents[i], float(scores[i])) for i in ranked_idx]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
return ranked, search_time_ms
|
| 265 |
|
| 266 |
|
|
|
|
| 254 |
query_emb: np.ndarray,
|
| 255 |
doc_embs: np.ndarray,
|
| 256 |
documents: list[str],
|
| 257 |
+
n_timing_runs: int = 50,
|
| 258 |
) -> tuple[list[tuple[str, float]], float]:
|
| 259 |
"""Rank documents via dot-product similarity (numpy fallback)."""
|
| 260 |
+
# Warm up + actual run for ranking
|
| 261 |
scores = np.dot(doc_embs, query_emb)
|
| 262 |
ranked_idx = np.argsort(scores)[::-1]
|
|
|
|
| 263 |
ranked = [(documents[i], float(scores[i])) for i in ranked_idx]
|
| 264 |
+
# Stable timing: average over multiple runs (sub-ms operations are noisy)
|
| 265 |
+
start = time.perf_counter()
|
| 266 |
+
for _ in range(n_timing_runs):
|
| 267 |
+
np.dot(doc_embs, query_emb)
|
| 268 |
+
search_time_ms = (time.perf_counter() - start) * 1000 / n_timing_runs
|
| 269 |
return ranked, search_time_ms
|
| 270 |
|
| 271 |
|