|
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 |
@@ -898,6 +899,28 @@ def __init__(self, config: CohereCompassVisionConfig, *inputs, **kwargs) -> None |
898 | 899 |
|
899 | 900 | self.post_init() |
900 | 901 |
|
| 902 | + def get_data_dependent_inputs(self, grid_thw: torch.Tensor, **kwargs) -> dict[str, Any]: |
| 903 | + """What the forward derives from `grid_thw` with data-dependent ops: the position embedding's `interp_indices` |
| 904 | + and `interp_weights`, the rotary `position_ids` of the patches, and the attention's `cu_seqlens` and |
| 905 | + `max_seqlen`.""" |
| 906 | + interp_indices, interp_weights = get_vision_interpolation_indices_and_weights( |
| 907 | + grid_thw, |
| 908 | + num_grid_per_side=self.num_grid_per_side, |
| 909 | + mode=self.interpolation_mode, |
| 910 | + align_corners=self.interpolation_align_corners, |
| 911 | + spatial_merge_size=self.config.spatial_merge_size, |
| 912 | + kwargs=kwargs, |
| 913 | + ) |
| 914 | + position_ids = get_vision_position_ids(grid_thw, self.spatial_merge_size, kwargs=kwargs) |
| 915 | + cu_seqlens, max_seqlen = get_vision_attention_seqlens(grid_thw, self.config, kwargs=kwargs) |
| 916 | + return { |
| 917 | + "interp_indices": interp_indices, |
| 918 | + "interp_weights": interp_weights, |
| 919 | + "position_ids": position_ids, |
| 920 | + "cu_seqlens": cu_seqlens, |
| 921 | + "max_seqlen": max_seqlen, |
| 922 | + } |
| 923 | + |
901 | 924 | @merge_with_config_defaults |
902 | 925 | @capture_outputs |
903 | 926 | def forward( |
@@ -1218,6 +1241,42 @@ def get_placeholder_mask( |
1218 | 1241 | ) |
1219 | 1242 | return special_image_mask, special_video_mask |
1220 | 1243 |
|
| 1244 | + def get_data_dependent_inputs( |
| 1245 | + self, |
| 1246 | + input_ids: torch.LongTensor | None = None, |
| 1247 | + attention_mask: torch.Tensor | None = None, |
| 1248 | + image_grid_thw: torch.LongTensor | None = None, |
| 1249 | + video_grid_thw: torch.LongTensor | None = None, |
| 1250 | + mm_token_type_ids: torch.IntTensor | None = None, |
| 1251 | + position_ids: torch.LongTensor | None = None, |
| 1252 | + **kwargs, |
| 1253 | + ) -> dict[str, Any]: |
| 1254 | + """What the forward derives from the processor's outputs with data-dependent ops: the M-RoPE |
| 1255 | + `position_ids`, the vision tower's inputs (see |
| 1256 | + [`Qwen2VisionTransformerPretrainedModel.get_data_dependent_inputs`]) prefixed `image_` and `video_`, as |
| 1257 | + `get_image_embeddings` and `get_video_embeddings` take them.""" |
| 1258 | + inputs = {} |
| 1259 | + for prefix, grid_thw in (("image_", image_grid_thw), ("video_", video_grid_thw)): |
| 1260 | + if grid_thw is not None: |
| 1261 | + tower_inputs = self.get_encoder(modality="image").get_data_dependent_inputs(grid_thw) |
| 1262 | + inputs.update({prefix + name: value for name, value in tower_inputs.items()}) |
| 1263 | + has_vision = image_grid_thw is not None or video_grid_thw is not None |
| 1264 | + if position_ids is None and input_ids is not None and mm_token_type_ids is not None and has_vision: |
| 1265 | + vision_position_ids, _ = self.get_rope_index( |
| 1266 | + input_ids, |
| 1267 | + image_grid_thw=image_grid_thw, |
| 1268 | + video_grid_thw=video_grid_thw, |
| 1269 | + attention_mask=attention_mask, |
| 1270 | + mm_token_type_ids=mm_token_type_ids, |
| 1271 | + ) |
| 1272 | + if attention_mask is not None: |
| 1273 | + text_position_ids = (attention_mask.long().cumsum(-1) - 1).masked_fill(attention_mask == 0, 0) |
| 1274 | + else: |
| 1275 | + text_position_ids = torch.arange(input_ids.shape[1], device=input_ids.device).expand_as(input_ids) |
| 1276 | + # The text positions first: the text model builds the attention mask from them |
| 1277 | + inputs["position_ids"] = torch.cat([text_position_ids[None], vision_position_ids], dim=0) |
| 1278 | + return inputs |
| 1279 | + |
1221 | 1280 | @deprecate_kwarg("past_key_values", "5.40.0", new_name="cache") |
1222 | 1281 | def compute_3d_position_ids( |
1223 | 1282 | self, |
@@ -1447,6 +1506,10 @@ def get_image_embeddings( |
1447 | 1506 | """ |
1448 | 1507 | return self.model.get_image_embeddings(pixel_values, image_grid_thw, **kwargs) |
1449 | 1508 |
|
| 1509 | + def get_data_dependent_inputs(self, *args, **kwargs) -> dict[str, Any]: |
| 1510 | + r"""See [`CohereCompassModel.get_data_dependent_inputs`].""" |
| 1511 | + return self.model.get_data_dependent_inputs(*args, **kwargs) |
| 1512 | + |
1450 | 1513 | @auto_docstring |
1451 | 1514 | def get_image_features( |
1452 | 1515 | self, |
|
0 commit comments