Commit ·
eb02943
1
Parent(s): b55116e
initial
Browse files- benchmark/config.py +29 -6
- benchmark/embedder.py +86 -2
- benchmark/runner.py +3 -0
- requirements.txt +4 -3
benchmark/config.py
CHANGED
|
@@ -15,24 +15,47 @@ MODELS = [
|
|
| 15 |
"passage_prefix": "passage: ",
|
| 16 |
},
|
| 17 |
{
|
| 18 |
-
"name": "
|
| 19 |
-
"query_prefix": "
|
| 20 |
-
"passage_prefix": "
|
| 21 |
},
|
| 22 |
{
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
"query_prefix": "query: ",
|
| 25 |
"passage_prefix": "passage: ",
|
| 26 |
},
|
| 27 |
{
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
"query_prefix": "",
|
| 30 |
"passage_prefix": "",
|
| 31 |
},
|
| 32 |
{
|
| 33 |
-
|
|
|
|
| 34 |
"query_prefix": "",
|
| 35 |
"passage_prefix": "",
|
|
|
|
| 36 |
},
|
| 37 |
]
|
| 38 |
|
|
|
|
| 15 |
"passage_prefix": "passage: ",
|
| 16 |
},
|
| 17 |
{
|
| 18 |
+
"name": "BAAI/bge-m3",
|
| 19 |
+
"query_prefix": "",
|
| 20 |
+
"passage_prefix": "",
|
| 21 |
},
|
| 22 |
{
|
| 23 |
+
# 4B/8B variants are skipped: their fp32 weights (~16GB / ~32GB) don't fit
|
| 24 |
+
# this machine's 8GB VRAM or its ~15GB system RAM.
|
| 25 |
+
"name": "Qwen/Qwen3-Embedding-0.6B",
|
| 26 |
+
"query_prefix": (
|
| 27 |
+
"Instruct: Given a web search query, retrieve relevant passages "
|
| 28 |
+
"that answer the query\nQuery:"
|
| 29 |
+
),
|
| 30 |
+
"passage_prefix": "",
|
| 31 |
+
},
|
| 32 |
+
{
|
| 33 |
+
"name": "intfloat/multilingual-e5-large",
|
| 34 |
"query_prefix": "query: ",
|
| 35 |
"passage_prefix": "passage: ",
|
| 36 |
},
|
| 37 |
{
|
| 38 |
+
# trust_remote_code is required to load Jina's custom modeling code; the
|
| 39 |
+
# model uses task-specific LoRA adapters selected via an encode()-time
|
| 40 |
+
# `task` kwarg rather than a text prefix.
|
| 41 |
+
"name": "jinaai/jina-embeddings-v3",
|
| 42 |
+
"query_prefix": "",
|
| 43 |
+
"passage_prefix": "",
|
| 44 |
+
"trust_remote_code": True,
|
| 45 |
+
"query_encode_kwargs": {"task": "retrieval.query"},
|
| 46 |
+
"passage_encode_kwargs": {"task": "retrieval.passage"},
|
| 47 |
+
},
|
| 48 |
+
{
|
| 49 |
+
"name": "sentence-transformers/LaBSE",
|
| 50 |
"query_prefix": "",
|
| 51 |
"passage_prefix": "",
|
| 52 |
},
|
| 53 |
{
|
| 54 |
+
# trust_remote_code is required to load Alibaba's custom modeling code.
|
| 55 |
+
"name": "Alibaba-NLP/gte-multilingual-base",
|
| 56 |
"query_prefix": "",
|
| 57 |
"passage_prefix": "",
|
| 58 |
+
"trust_remote_code": True,
|
| 59 |
},
|
| 60 |
]
|
| 61 |
|
benchmark/embedder.py
CHANGED
|
@@ -3,20 +3,102 @@
|
|
| 3 |
import time
|
| 4 |
|
| 5 |
import torch
|
|
|
|
| 6 |
from sentence_transformers import SentenceTransformer
|
| 7 |
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
class TimedEmbedder:
|
| 10 |
-
def __init__(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
self.model_name = model_name
|
| 12 |
self.query_prefix = query_prefix
|
| 13 |
self.passage_prefix = passage_prefix
|
|
|
|
|
|
|
| 14 |
self.device = device or ("cuda" if torch.cuda.is_available() else "cpu")
|
| 15 |
|
| 16 |
start = time.perf_counter()
|
| 17 |
-
self.model = SentenceTransformer(
|
|
|
|
|
|
|
| 18 |
self.load_time_sec = time.perf_counter() - start
|
| 19 |
|
|
|
|
|
|
|
| 20 |
self.param_count = sum(p.numel() for p in self.model.parameters())
|
| 21 |
self.model_size_mb = sum(
|
| 22 |
p.numel() * p.element_size() for p in self.model.parameters()
|
|
@@ -37,6 +119,7 @@ class TimedEmbedder:
|
|
| 37 |
normalize_embeddings=True,
|
| 38 |
convert_to_numpy=True,
|
| 39 |
show_progress_bar=False,
|
|
|
|
| 40 |
)
|
| 41 |
elapsed = time.perf_counter() - start
|
| 42 |
return embeddings, elapsed
|
|
@@ -50,6 +133,7 @@ class TimedEmbedder:
|
|
| 50 |
normalize_embeddings=True,
|
| 51 |
convert_to_numpy=True,
|
| 52 |
show_progress_bar=False,
|
|
|
|
| 53 |
)
|
| 54 |
elapsed = time.perf_counter() - start
|
| 55 |
return embedding, elapsed
|
|
|
|
| 3 |
import time
|
| 4 |
|
| 5 |
import torch
|
| 6 |
+
import transformers
|
| 7 |
from sentence_transformers import SentenceTransformer
|
| 8 |
|
| 9 |
|
| 10 |
+
def _patch_missing_tied_weights_keys():
|
| 11 |
+
"""Some custom `trust_remote_code` models (e.g. jina-embeddings-v3's LoRA
|
| 12 |
+
wrapper) never call `PreTrainedModel.post_init()`, so `transformers` >= 5's
|
| 13 |
+
weight-loading path crashes with AttributeError on `self.all_tied_weights_keys`.
|
| 14 |
+
Turning it into a property with a per-instance fallback default of `{}` keeps
|
| 15 |
+
every normal model's behavior identical (post_init still sets it the same way)
|
| 16 |
+
while letting non-conformant custom code fall back to "no tied weights" instead
|
| 17 |
+
of crashing.
|
| 18 |
+
"""
|
| 19 |
+
base = transformers.PreTrainedModel
|
| 20 |
+
if getattr(base, "_omicon_tied_weights_patched", False):
|
| 21 |
+
return
|
| 22 |
+
|
| 23 |
+
def getter(self):
|
| 24 |
+
return self.__dict__.setdefault("_all_tied_weights_keys_fallback", {})
|
| 25 |
+
|
| 26 |
+
def setter(self, value):
|
| 27 |
+
self.__dict__["_all_tied_weights_keys_fallback"] = value
|
| 28 |
+
|
| 29 |
+
base.all_tied_weights_keys = property(getter, setter)
|
| 30 |
+
base._omicon_tied_weights_patched = True
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
_patch_missing_tied_weights_keys()
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def _refresh_procedurally_computed_buffers(sentence_transformer_model, device):
|
| 37 |
+
"""Some `trust_remote_code` models (e.g. Alibaba-NLP/gte-multilingual-base) compute
|
| 38 |
+
real values for non-persistent buffers (RoPE cos/sin caches, a `position_ids` arange)
|
| 39 |
+
with tensor math inside `__init__` (torch.arange, .cos(), .sin(), einsum). `transformers`
|
| 40 |
+
>= 5's `from_pretrained` unconditionally builds every model under a `meta` device context
|
| 41 |
+
for speed -- `low_cpu_mem_usage`/`_fast_init` are now silently dropped, with no opt-out.
|
| 42 |
+
Ops on meta tensors don't produce real values, and only checkpoint weights get properly
|
| 43 |
+
materialized afterward, so these procedural buffers come out as uninitialized memory
|
| 44 |
+
instead of the values `__init__` intended. Re-running the same buffer-computation methods
|
| 45 |
+
now that the model lives on a real device reproduces exactly what `__init__` was supposed
|
| 46 |
+
to do. This is a no-op (via hasattr guards) for any model that isn't shaped this way.
|
| 47 |
+
"""
|
| 48 |
+
try:
|
| 49 |
+
auto_model = sentence_transformer_model[0].auto_model
|
| 50 |
+
except (IndexError, AttributeError, KeyError):
|
| 51 |
+
return
|
| 52 |
+
|
| 53 |
+
embeddings = getattr(auto_model, "embeddings", None)
|
| 54 |
+
if embeddings is None:
|
| 55 |
+
return
|
| 56 |
+
|
| 57 |
+
# `rotary_emb` is specific to this custom architecture (e.g. GTE's NewEmbeddings) --
|
| 58 |
+
# standard BERT/RoBERTa-family embeddings also have a `position_ids` buffer, but with
|
| 59 |
+
# a different (1, max_pos) shape, so only touch it when we've confirmed we're looking
|
| 60 |
+
# at this specific rope-based custom module.
|
| 61 |
+
rotary = getattr(embeddings, "rotary_emb", None)
|
| 62 |
+
if rotary is None or not hasattr(rotary, "_set_cos_sin_cache"):
|
| 63 |
+
return
|
| 64 |
+
|
| 65 |
+
rotary._set_cos_sin_cache(
|
| 66 |
+
seq_len=rotary.max_seq_len_cached, device=device, dtype=rotary.cos_cached.dtype
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
position_ids = getattr(embeddings, "position_ids", None)
|
| 70 |
+
if position_ids is not None and position_ids.dim() == 1:
|
| 71 |
+
embeddings.register_buffer(
|
| 72 |
+
"position_ids", torch.arange(position_ids.shape[0], device=device), persistent=False
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
class TimedEmbedder:
|
| 77 |
+
def __init__(
|
| 78 |
+
self,
|
| 79 |
+
model_name,
|
| 80 |
+
query_prefix="",
|
| 81 |
+
passage_prefix="",
|
| 82 |
+
device=None,
|
| 83 |
+
trust_remote_code=False,
|
| 84 |
+
query_encode_kwargs=None,
|
| 85 |
+
passage_encode_kwargs=None,
|
| 86 |
+
):
|
| 87 |
self.model_name = model_name
|
| 88 |
self.query_prefix = query_prefix
|
| 89 |
self.passage_prefix = passage_prefix
|
| 90 |
+
self.query_encode_kwargs = query_encode_kwargs or {}
|
| 91 |
+
self.passage_encode_kwargs = passage_encode_kwargs or {}
|
| 92 |
self.device = device or ("cuda" if torch.cuda.is_available() else "cpu")
|
| 93 |
|
| 94 |
start = time.perf_counter()
|
| 95 |
+
self.model = SentenceTransformer(
|
| 96 |
+
model_name, device=self.device, trust_remote_code=trust_remote_code
|
| 97 |
+
)
|
| 98 |
self.load_time_sec = time.perf_counter() - start
|
| 99 |
|
| 100 |
+
_refresh_procedurally_computed_buffers(self.model, self.device)
|
| 101 |
+
|
| 102 |
self.param_count = sum(p.numel() for p in self.model.parameters())
|
| 103 |
self.model_size_mb = sum(
|
| 104 |
p.numel() * p.element_size() for p in self.model.parameters()
|
|
|
|
| 119 |
normalize_embeddings=True,
|
| 120 |
convert_to_numpy=True,
|
| 121 |
show_progress_bar=False,
|
| 122 |
+
**self.passage_encode_kwargs,
|
| 123 |
)
|
| 124 |
elapsed = time.perf_counter() - start
|
| 125 |
return embeddings, elapsed
|
|
|
|
| 133 |
normalize_embeddings=True,
|
| 134 |
convert_to_numpy=True,
|
| 135 |
show_progress_bar=False,
|
| 136 |
+
**self.query_encode_kwargs,
|
| 137 |
)
|
| 138 |
elapsed = time.perf_counter() - start
|
| 139 |
return embedding, elapsed
|
benchmark/runner.py
CHANGED
|
@@ -137,6 +137,9 @@ def run_benchmark(
|
|
| 137 |
model_name,
|
| 138 |
query_prefix=model_cfg.get("query_prefix", ""),
|
| 139 |
passage_prefix=model_cfg.get("passage_prefix", ""),
|
|
|
|
|
|
|
|
|
|
| 140 |
)
|
| 141 |
print(
|
| 142 |
f" loaded in {embedder.load_time_sec:.2f}s | "
|
|
|
|
| 137 |
model_name,
|
| 138 |
query_prefix=model_cfg.get("query_prefix", ""),
|
| 139 |
passage_prefix=model_cfg.get("passage_prefix", ""),
|
| 140 |
+
trust_remote_code=model_cfg.get("trust_remote_code", False),
|
| 141 |
+
query_encode_kwargs=model_cfg.get("query_encode_kwargs"),
|
| 142 |
+
passage_encode_kwargs=model_cfg.get("passage_encode_kwargs"),
|
| 143 |
)
|
| 144 |
print(
|
| 145 |
f" loaded in {embedder.load_time_sec:.2f}s | "
|
requirements.txt
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
sentence-transformers
|
| 2 |
-
ipywidgets
|
| 3 |
jupyter
|
| 4 |
-
rank-bm25
|
| 5 |
numpy
|
| 6 |
-
qdrant-client
|
|
|
|
|
|
| 1 |
sentence-transformers
|
| 2 |
+
ipywidgets
|
| 3 |
jupyter
|
| 4 |
+
rank-bm25
|
| 5 |
numpy
|
| 6 |
+
qdrant-client
|
| 7 |
+
einops
|