Yeroyan commited on
Commit
5fbd020
·
1 Parent(s): 518c15b

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
- batch_n_rows = processed.get("n_rows")
671
- batch_n_cols = processed.get("n_cols")
672
- # Qwen2/2.5-VL style grid information (T, H, W)
673
- batch_grid_thw = processed.get("image_grid_thw", None)
674
- if batch_grid_thw is None:
675
- batch_grid_thw = processed.get("grid_thw", None)
676
- if batch_grid_thw is None:
677
- batch_grid_thw = processed.get("image_grid", None)
678
-
679
- for j in range(input_ids.shape[0]):
680
- # Find visual token indices
681
- image_token_mask = input_ids[j] == self.image_token_id
682
- visual_indices = torch.where(image_token_mask)[0].cpu().numpy().tolist()
683
-
684
- n_rows = batch_n_rows[j].item() if batch_n_rows is not None else None
685
- n_cols = batch_n_cols[j].item() if batch_n_cols is not None else None
686
- grid_t = grid_h = grid_w = None
687
- grid_h_eff = grid_w_eff = None
688
- if batch_grid_thw is not None:
689
- try:
690
- g = batch_grid_thw[j]
691
- # sometimes [B, N, 3] -> take first image
692
- if hasattr(g, "dim") and g.dim() == 2:
693
- g = g[0]
694
- t, h, w = [int(x) for x in g.detach().cpu().tolist()]
695
- grid_t, grid_h, grid_w = t, h, w
696
- # ColQwen2.5/Qwen2.5-VL uses a 2×2 spatial merge internally, but different
697
- # processor versions expose different grids:
698
- # - Some expose the *post-merge* token grid (H×W == num_visual_tokens)
699
- # - Others expose the *pre-merge* pixel/patch grid ((H/2)×(W/2) == num_visual_tokens)
700
- # We infer the effective grid by matching the observed token count.
701
- num_visual = int(len(visual_indices))
702
- if int(h) * int(w) == num_visual:
703
- grid_h_eff, grid_w_eff = int(h), int(w)
704
- elif (
705
- h % 2 == 0 and w % 2 == 0 and (h // 2) * (w // 2) == num_visual
706
- ):
707
- grid_h_eff, grid_w_eff = int(h // 2), int(w // 2)
708
- except Exception:
709
- pass
710
-
711
- token_infos.append(
712
- {
713
- "visual_token_indices": visual_indices,
714
- "num_visual_tokens": len(visual_indices),
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