vllm.multimodal.evs ΒΆ
compute_mrope_for_media ΒΆ
compute_mrope_for_media(
video_size_thw: LongTensor,
spatial_merge_size: int,
tokens_per_second: float = 1.0,
video_second_per_grid: float = 1.0,
) -> Tensor
Computes the mrope for video embeddings based on the grid dimensions. Computed mrope positions match original qwen 2.5 implementation, but positions are built for media being the first element in sequence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
video_size_thw | LongTensor | Media size (num frames, rows, cols) | required |
spatial_merge_size | int | Size reduction for rows & cols dimensions. | required |
tokens_per_second | float | Number of tokens per second. | 1.0 |
video_second_per_grid | float | Number of seconds per video. | 1.0 |
Returns:
Type | Description |
---|---|
Tensor | Tensor of shape |
Tensor | represents mrope positions [0:3), while the last channel |
Tensor | contains value of llm_grid_w repeated for all positions. |
Source code in vllm/multimodal/evs.py
compute_retained_tokens_count ΒΆ
Compute the number of retained tokens for a given video. Method ensures that we retain all the tokens from the first frame regardless of the pruning rate.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tokens_per_frame | int | The number of tokens per frame. | required |
num_frames | int | The total number of frames. | required |
q | float | The pruning rate. | required |
Returns:
Type | Description |
---|---|
int | The number of retained tokens. |
Source code in vllm/multimodal/evs.py
compute_retention_mask ΒΆ
compute_retention_mask(
video_embeds: Tensor,
video_size_thw: Union[LongTensor, tuple[int, int, int]],
spatial_merge_size: int,
q: float,
) -> Tensor
Computes the retention mask for input video embeddings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
video_embeds | `torch.Tensor` | The input video embeddings of shape | required |
video_size_thw | `torch.LongTensor` of shape `(3)` | The temporal, height and width of video. | required |
spatial_merge_size | int | Size reduction for rows & cols dimensions. | required |
q | float | ( | required |
Returns:
Type | Description |
---|---|
Tensor |
|
Source code in vllm/multimodal/evs.py
recompute_mrope_positions ΒΆ
recompute_mrope_positions(
input_ids: LongTensor,
multimodal_positions: list[Tensor],
mrope_positions: LongTensor,
num_computed_tokens: int,
vision_start_token_id: int,
image_token_id: int,
video_token_id: int,
) -> tuple[LongTensor, int]
Update part of input mrope positions. Original mrope_positions are computed incorrectly, so once we prune media tokens we should reflect this in the mrope positions for the LLM.
This method supports chunked prefill approach where multimodal_embeddings are passed to LLM in chunks, so input multimodal_embeddings may contain zero, some or even some part of all multimodal_embeddings for a given prompt.
Each multimodal_positions has 4 extra channels (First 3 channels corresponds to original 3 mrope positions, last channel is the maximum width of the media repeated). Provided multimodal_positions do not reflect location of media position in sequence - they are computed like the media is in the 0-th position in the sequence.
Method works as follows: it recomputes mrope_positions starting from the num_computed_tokens
for total_len_of_multimodal_embeddings
and then shifts all text tokens that goes after total_len_of_multimodal_embeddings.
It also handles case when multimodal_embeddings is partial (e.g. one media is split into two prefill stages)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_ids | LongTensor | (N,) All input tokens of the prompt (entire sequence). | required |
multimodal_positions | list[Tensor] | List of mrope positsions for each media. | required |
mrope_positions | LongTensor | Existing mrope positions (4, N) for entire sequence. | required |
num_computed_tokens | int | A number of computed tokens so far. | required |
vision_start_token_id | int | Token indicating start of vision media. | required |
image_token_id | int | Image token id | required |
video_token_id | int | Video token id | required |
Returns:
Type | Description |
---|---|
tuple[LongTensor, int] | Tuple of (mrope_positions, mrope_position_delta). |
Source code in vllm/multimodal/evs.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
|