|
21 | 21 | import itertools |
22 | 22 | from collections.abc import Callable |
23 | 23 | from dataclasses import dataclass |
| 24 | +from typing import Any |
24 | 25 |
|
25 | 26 | import torch |
26 | 27 | import torch.nn as nn |
@@ -897,6 +898,28 @@ def __init__(self, config: CohereCompassVisionConfig, *inputs, **kwargs) -> None |
897 | 898 |
|
898 | 899 | self.post_init() |
899 | 900 |
|
| 901 | + def get_data_dependent_inputs(self, grid_thw: torch.Tensor, **kwargs) -> dict[str, Any]: |
| 902 | + """What the forward derives from `grid_thw` with data-dependent ops: the position embedding's `interp_indices` |
| 903 | + and `interp_weights`, the rotary `position_ids` of the patches, and the attention's `cu_seqlens` and |
| 904 | + `max_seqlen`.""" |
| 905 | + interp_indices, interp_weights = get_vision_interpolation_indices_and_weights( |
| 906 | + grid_thw, |
| 907 | + num_grid_per_side=self.num_grid_per_side, |
| 908 | + mode=self.interpolation_mode, |
| 909 | + align_corners=self.interpolation_align_corners, |
| 910 | + spatial_merge_size=self.config.spatial_merge_size, |
| 911 | + kwargs=kwargs, |
| 912 | + ) |
| 913 | + position_ids = get_vision_position_ids(grid_thw, self.spatial_merge_size, kwargs=kwargs) |
| 914 | + cu_seqlens, max_seqlen = get_vision_attention_seqlens(grid_thw, self.config, kwargs=kwargs) |
| 915 | + return { |
| 916 | + "interp_indices": interp_indices, |
| 917 | + "interp_weights": interp_weights, |
| 918 | + "position_ids": position_ids, |
| 919 | + "cu_seqlens": cu_seqlens, |
| 920 | + "max_seqlen": max_seqlen, |
| 921 | + } |
| 922 | + |
900 | 923 | @merge_with_config_defaults |
901 | 924 | @capture_outputs |
902 | 925 | def forward( |
@@ -1185,6 +1208,42 @@ def get_placeholder_mask( |
1185 | 1208 | ) |
1186 | 1209 | return special_image_mask, special_video_mask |
1187 | 1210 |
|
| 1211 | + def get_data_dependent_inputs( |
| 1212 | + self, |
| 1213 | + input_ids: torch.LongTensor | None = None, |
| 1214 | + attention_mask: torch.Tensor | None = None, |
| 1215 | + image_grid_thw: torch.LongTensor | None = None, |
| 1216 | + video_grid_thw: torch.LongTensor | None = None, |
| 1217 | + mm_token_type_ids: torch.IntTensor | None = None, |
| 1218 | + position_ids: torch.LongTensor | None = None, |
| 1219 | + **kwargs, |
| 1220 | + ) -> dict[str, Any]: |
| 1221 | + """What the forward derives from the processor's outputs with data-dependent ops: the M-RoPE |
| 1222 | + `position_ids`, the vision tower's inputs (see |
| 1223 | + [`Qwen2VisionTransformerPretrainedModel.get_data_dependent_inputs`]) prefixed `image_` and `video_`, as |
| 1224 | + `get_image_embeddings` and `get_video_embeddings` take them.""" |
| 1225 | + inputs = {} |
| 1226 | + for prefix, grid_thw in (("image_", image_grid_thw), ("video_", video_grid_thw)): |
| 1227 | + if grid_thw is not None: |
| 1228 | + tower_inputs = self.get_encoder(modality="image").get_data_dependent_inputs(grid_thw) |
| 1229 | + inputs.update({prefix + name: value for name, value in tower_inputs.items()}) |
| 1230 | + has_vision = image_grid_thw is not None or video_grid_thw is not None |
| 1231 | + if position_ids is None and input_ids is not None and mm_token_type_ids is not None and has_vision: |
| 1232 | + vision_position_ids, _ = self.get_rope_index( |
| 1233 | + input_ids, |
| 1234 | + image_grid_thw=image_grid_thw, |
| 1235 | + video_grid_thw=video_grid_thw, |
| 1236 | + attention_mask=attention_mask, |
| 1237 | + mm_token_type_ids=mm_token_type_ids, |
| 1238 | + ) |
| 1239 | + if attention_mask is not None: |
| 1240 | + text_position_ids = (attention_mask.long().cumsum(-1) - 1).masked_fill(attention_mask == 0, 0) |
| 1241 | + else: |
| 1242 | + text_position_ids = torch.arange(input_ids.shape[1], device=input_ids.device).expand_as(input_ids) |
| 1243 | + # The text positions first: the text model builds the attention mask from them |
| 1244 | + inputs["position_ids"] = torch.cat([text_position_ids[None], vision_position_ids], dim=0) |
| 1245 | + return inputs |
| 1246 | + |
1188 | 1247 | @deprecate_kwarg("past_key_values", "5.40.0", new_name="cache") |
1189 | 1248 | def compute_3d_position_ids( |
1190 | 1249 | self, |
@@ -1371,6 +1430,10 @@ def __init__(self, config: CohereCompassConfig): |
1371 | 1430 |
|
1372 | 1431 | self.post_init() |
1373 | 1432 |
|
| 1433 | + def get_data_dependent_inputs(self, *args, **kwargs) -> dict[str, Any]: |
| 1434 | + r"""See [`CohereCompassModel.get_data_dependent_inputs`].""" |
| 1435 | + return self.model.get_data_dependent_inputs(*args, **kwargs) |
| 1436 | + |
1374 | 1437 | @can_return_tuple |
1375 | 1438 | @auto_docstring(checkpoint="CohereLabs/North-Micro-Vision-Instruct") |
1376 | 1439 | @deprecate_kwarg("past_key_values", "5.40.0", new_name="cache") |
|
0 commit comments