Sync from GitHub 9bc0d02
Browse files- models/colembed.py +21 -18
models/colembed.py
CHANGED
|
@@ -5,6 +5,10 @@ Two ZeroGPU entry points:
|
|
| 5 |
_search_on_gpu question -> top-K (doc, page, score) via MaxSim over
|
| 6 |
batches of page embeddings streamed from the store
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
forward_images/forward_queries return zero-padded [batch, tokens, dim] tensors
|
| 9 |
with real tokens L2-normalized, so padding rows are exactly zero. We strip them
|
| 10 |
before storing and rely on the same property when scoring zero-padded batches.
|
|
@@ -27,11 +31,22 @@ from core.constants import (
|
|
| 27 |
SEARCH_GPU_DURATION,
|
| 28 |
)
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
@spaces.GPU(duration=EMBED_GPU_DURATION)
|
| 32 |
-
def _embed_pages_on_gpu(
|
| 33 |
with torch.no_grad():
|
| 34 |
-
embs =
|
| 35 |
out = []
|
| 36 |
for emb in embs: # [tokens, dim]; zero rows are padding
|
| 37 |
mask = emb.abs().sum(dim=-1) > 0
|
|
@@ -40,10 +55,10 @@ def _embed_pages_on_gpu(model, images: list[Image.Image]) -> list[np.ndarray]:
|
|
| 40 |
|
| 41 |
|
| 42 |
@spaces.GPU(duration=SEARCH_GPU_DURATION)
|
| 43 |
-
def _search_on_gpu(
|
| 44 |
results = []
|
| 45 |
with torch.no_grad():
|
| 46 |
-
q =
|
| 47 |
for refs, batch in store.iter_page_batches(doc_ids, SCORE_PAGES_PER_BATCH):
|
| 48 |
emb = torch.from_numpy(batch).to(q.device) # [B, T, D] float16
|
| 49 |
sim = torch.einsum("qd,btd->bqt", q, emb).float()
|
|
@@ -59,24 +74,12 @@ def _search_on_gpu(model, question: str, store, doc_ids, top_k: int):
|
|
| 59 |
class ColEmbed:
|
| 60 |
MODEL_ID = COLEMBED_MODEL_ID
|
| 61 |
|
| 62 |
-
def __init__(self, device: str = "cuda", dtype: torch.dtype = torch.bfloat16):
|
| 63 |
-
self.model = (
|
| 64 |
-
AutoModel.from_pretrained(
|
| 65 |
-
self.MODEL_ID,
|
| 66 |
-
trust_remote_code=True,
|
| 67 |
-
torch_dtype=dtype,
|
| 68 |
-
attn_implementation=COLEMBED_ATTN,
|
| 69 |
-
)
|
| 70 |
-
.to(device)
|
| 71 |
-
.eval()
|
| 72 |
-
)
|
| 73 |
-
|
| 74 |
def embed_pages(self, images: list[Image.Image]) -> list[np.ndarray]:
|
| 75 |
"""Embed page images -> list of [n_tokens, dim] float16 arrays."""
|
| 76 |
-
return _embed_pages_on_gpu(
|
| 77 |
|
| 78 |
def search(
|
| 79 |
self, question: str, store, doc_ids: list[str] | None, top_k: int
|
| 80 |
) -> list[tuple[str, int, float]]:
|
| 81 |
"""Return the top_k (doc_id, page_num, score) across the given docs."""
|
| 82 |
-
return _search_on_gpu(
|
|
|
|
| 5 |
_search_on_gpu question -> top-K (doc, page, score) via MaxSim over
|
| 6 |
batches of page embeddings streamed from the store
|
| 7 |
|
| 8 |
+
The model is a module-level global: ZeroGPU packs module-level CUDA tensors at
|
| 9 |
+
startup and shares them with the GPU worker, whereas function arguments are
|
| 10 |
+
pickled — and the trust_remote_code model class is not picklable.
|
| 11 |
+
|
| 12 |
forward_images/forward_queries return zero-padded [batch, tokens, dim] tensors
|
| 13 |
with real tokens L2-normalized, so padding rows are exactly zero. We strip them
|
| 14 |
before storing and rely on the same property when scoring zero-padded batches.
|
|
|
|
| 31 |
SEARCH_GPU_DURATION,
|
| 32 |
)
|
| 33 |
|
| 34 |
+
_MODEL = (
|
| 35 |
+
AutoModel.from_pretrained(
|
| 36 |
+
COLEMBED_MODEL_ID,
|
| 37 |
+
trust_remote_code=True,
|
| 38 |
+
dtype=torch.bfloat16,
|
| 39 |
+
attn_implementation=COLEMBED_ATTN,
|
| 40 |
+
)
|
| 41 |
+
.to("cuda")
|
| 42 |
+
.eval()
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
|
| 46 |
@spaces.GPU(duration=EMBED_GPU_DURATION)
|
| 47 |
+
def _embed_pages_on_gpu(images: list[Image.Image]) -> list[np.ndarray]:
|
| 48 |
with torch.no_grad():
|
| 49 |
+
embs = _MODEL.forward_images(images, batch_size=EMBED_BATCH_SIZE)
|
| 50 |
out = []
|
| 51 |
for emb in embs: # [tokens, dim]; zero rows are padding
|
| 52 |
mask = emb.abs().sum(dim=-1) > 0
|
|
|
|
| 55 |
|
| 56 |
|
| 57 |
@spaces.GPU(duration=SEARCH_GPU_DURATION)
|
| 58 |
+
def _search_on_gpu(question: str, store, doc_ids, top_k: int):
|
| 59 |
results = []
|
| 60 |
with torch.no_grad():
|
| 61 |
+
q = _MODEL.forward_queries([question], batch_size=1)[0].to(torch.float16)
|
| 62 |
for refs, batch in store.iter_page_batches(doc_ids, SCORE_PAGES_PER_BATCH):
|
| 63 |
emb = torch.from_numpy(batch).to(q.device) # [B, T, D] float16
|
| 64 |
sim = torch.einsum("qd,btd->bqt", q, emb).float()
|
|
|
|
| 74 |
class ColEmbed:
|
| 75 |
MODEL_ID = COLEMBED_MODEL_ID
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
def embed_pages(self, images: list[Image.Image]) -> list[np.ndarray]:
|
| 78 |
"""Embed page images -> list of [n_tokens, dim] float16 arrays."""
|
| 79 |
+
return _embed_pages_on_gpu(images)
|
| 80 |
|
| 81 |
def search(
|
| 82 |
self, question: str, store, doc_ids: list[str] | None, top_k: int
|
| 83 |
) -> list[tuple[str, int, float]]:
|
| 84 |
"""Return the top_k (doc_id, page_num, score) across the given docs."""
|
| 85 |
+
return _search_on_gpu(question, store, doc_ids, top_k)
|