Spaces:
Paused
Paused
sync from GitHub: fix: indentation error in _embed_image_batch
Browse files
visual_rag/embedding/visual_embedder.py
CHANGED
|
@@ -667,61 +667,51 @@ class VisualEmbedder:
|
|
| 667 |
|
| 668 |
if return_token_info:
|
| 669 |
input_ids = processed["input_ids"]
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
g =
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
"n_rows": n_rows,
|
| 716 |
-
"n_cols": n_cols,
|
| 717 |
-
"num_tiles": (n_rows * n_cols + 1) if n_rows and n_cols else None,
|
| 718 |
-
"grid_t": grid_t,
|
| 719 |
-
"grid_h": grid_h,
|
| 720 |
-
"grid_w": grid_w,
|
| 721 |
-
"grid_h_eff": grid_h_eff,
|
| 722 |
-
"grid_w_eff": grid_w_eff,
|
| 723 |
-
}
|
| 724 |
-
)
|
| 725 |
|
| 726 |
batch_embeddings = self.model(**processed)
|
| 727 |
|
|
|
|
| 667 |
|
| 668 |
if return_token_info:
|
| 669 |
input_ids = processed["input_ids"]
|
| 670 |
+
batch_n_rows = processed.get("n_rows")
|
| 671 |
+
batch_n_cols = processed.get("n_cols")
|
| 672 |
+
batch_grid_thw = processed.get("image_grid_thw", None)
|
| 673 |
+
if batch_grid_thw is None:
|
| 674 |
+
batch_grid_thw = processed.get("grid_thw", None)
|
| 675 |
+
if batch_grid_thw is None:
|
| 676 |
+
batch_grid_thw = processed.get("image_grid", None)
|
| 677 |
+
|
| 678 |
+
for j in range(input_ids.shape[0]):
|
| 679 |
+
image_token_mask = input_ids[j] == self.image_token_id
|
| 680 |
+
visual_indices = torch.where(image_token_mask)[0].cpu().numpy().tolist()
|
| 681 |
+
|
| 682 |
+
n_rows = batch_n_rows[j].item() if batch_n_rows is not None else None
|
| 683 |
+
n_cols = batch_n_cols[j].item() if batch_n_cols is not None else None
|
| 684 |
+
grid_t = grid_h = grid_w = None
|
| 685 |
+
grid_h_eff = grid_w_eff = None
|
| 686 |
+
if batch_grid_thw is not None:
|
| 687 |
+
try:
|
| 688 |
+
g = batch_grid_thw[j]
|
| 689 |
+
if hasattr(g, "dim") and g.dim() == 2:
|
| 690 |
+
g = g[0]
|
| 691 |
+
t, h, w = [int(x) for x in g.detach().cpu().tolist()]
|
| 692 |
+
grid_t, grid_h, grid_w = t, h, w
|
| 693 |
+
num_visual = int(len(visual_indices))
|
| 694 |
+
if int(h) * int(w) == num_visual:
|
| 695 |
+
grid_h_eff, grid_w_eff = int(h), int(w)
|
| 696 |
+
elif (
|
| 697 |
+
h % 2 == 0 and w % 2 == 0 and (h // 2) * (w // 2) == num_visual
|
| 698 |
+
):
|
| 699 |
+
grid_h_eff, grid_w_eff = int(h // 2), int(w // 2)
|
| 700 |
+
except Exception:
|
| 701 |
+
pass
|
| 702 |
+
|
| 703 |
+
token_infos.append({
|
| 704 |
+
"visual_token_indices": visual_indices,
|
| 705 |
+
"num_visual_tokens": len(visual_indices),
|
| 706 |
+
"n_rows": n_rows,
|
| 707 |
+
"n_cols": n_cols,
|
| 708 |
+
"num_tiles": (n_rows * n_cols + 1) if n_rows and n_cols else None,
|
| 709 |
+
"grid_t": grid_t,
|
| 710 |
+
"grid_h": grid_h,
|
| 711 |
+
"grid_w": grid_w,
|
| 712 |
+
"grid_h_eff": grid_h_eff,
|
| 713 |
+
"grid_w_eff": grid_w_eff,
|
| 714 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 715 |
|
| 716 |
batch_embeddings = self.model(**processed)
|
| 717 |
|