Compare commits
1 Commits
main
...
scratch/sp
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4efaa55ed8 |
68
spike.py
Normal file
68
spike.py
Normal file
@@ -0,0 +1,68 @@
|
||||
"""Spike: verify BGE-M3 dense+sparse encoding via sentence-transformers.
|
||||
|
||||
RESULT: NO-GO.
|
||||
|
||||
The session prompt for F-04 specified an `encode()` call signature of
|
||||
``encode([...], return_dense=True, return_sparse=True, batch_size=2)``.
|
||||
This signature originates from the ``FlagEmbedding.BGEM3FlagModel`` API,
|
||||
not ``sentence_transformers.SentenceTransformer``.
|
||||
|
||||
With ``sentence-transformers==5.5.1`` and ``BAAI/bge-m3``:
|
||||
|
||||
>>> model.encode(['hello', 'world'], return_dense=True, return_sparse=True)
|
||||
ValueError: SentenceTransformer.encode() has been called with additional
|
||||
keyword arguments that this model does not use:
|
||||
['return_sparse', 'return_dense'].
|
||||
|
||||
The vanilla call ``model.encode([...])`` returns dense vectors of shape
|
||||
``(2, 1024)`` and dtype ``float32`` as expected — dense embeddings work.
|
||||
Sparse / lexical-weights output is NOT exposed via the standard
|
||||
``SentenceTransformer`` wrapper. To get the BGE-M3 sparse head we must:
|
||||
|
||||
(a) Install ``FlagEmbedding`` and load via ``BGEM3FlagModel``, or
|
||||
(b) Drive the underlying ``transformers`` model directly and apply the
|
||||
sparse projection head ourselves, or
|
||||
(c) Use ``sentence_transformers.SparseEncoder`` with a different
|
||||
(SPLADE-style) checkpoint.
|
||||
|
||||
Decision required from Theorist/Leader: which path?
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
|
||||
|
||||
def main() -> int:
|
||||
try:
|
||||
from sentence_transformers import SentenceTransformer
|
||||
|
||||
model = SentenceTransformer("BAAI/bge-m3")
|
||||
out = model.encode(
|
||||
["hello", "world"],
|
||||
return_dense=True,
|
||||
return_sparse=True,
|
||||
batch_size=2,
|
||||
)
|
||||
|
||||
if isinstance(out, dict):
|
||||
dense = out.get("dense_vecs")
|
||||
sparse = out.get("lexical_weights")
|
||||
else:
|
||||
dense = out
|
||||
sparse = None
|
||||
|
||||
assert dense is not None, "no dense output"
|
||||
assert dense.shape == (2, 1024), f"dense shape {dense.shape} != (2, 1024)"
|
||||
assert isinstance(sparse, list) and len(sparse) == 2, "sparse not list of 2"
|
||||
assert all(isinstance(d, dict) for d in sparse), "sparse elements not dicts"
|
||||
except Exception as exc: # noqa: BLE001 — spike must catch everything
|
||||
print(f"Spike: NO-GO — {type(exc).__name__}: {exc}")
|
||||
return 1
|
||||
|
||||
print("Spike: GO")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user