vllm.model_executor.models.vision ¶
VisionFeatureSelectStrategy module-attribute ¶
VisionFeatureSelectStrategy: TypeAlias = (
VisionFeatureSelectStrategyStr
| Callable[[Tensor], Tensor]
)
VisionFeatureSelectStrategyStr module-attribute ¶
VisionFeatureSelectStrategyStr = Literal[
"class", "default", "full"
]
VisionEncoderInfo ¶
Source code in vllm/model_executor/models/vision.py
VisionLanguageConfig ¶
_RootConfig ¶
_get_vision_feature_selector ¶
_get_vision_feature_selector(
strategy: VisionFeatureSelectStrategy | str,
) -> Callable[[Tensor], Tensor]
Source code in vllm/model_executor/models/vision.py
get_llm_pos_ids_for_vision ¶
get_llm_pos_ids_for_vision(
start_idx: int,
vision_idx: int,
spatial_merge_size: int,
t_index: list[int],
grid_hs: Tensor,
grid_ws: Tensor,
) -> Tensor
Source code in vllm/model_executor/models/vision.py
get_load_balance_assignment ¶
get_load_balance_assignment(
sizes: list[int], num_gpus: int = 2
) -> tuple[list[int], list[int], list[int]]
Generate load balancing assignment and metadata for distributing data across GPUs. The load is determined by the total image sizes, not the number of images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sizes | list[int] | The size of each image | required |
num_gpus | int | Number of GPUs to balance across | 2 |
Returns:
| Name | Type | Description |
|---|---|---|
shuffle_indices | list[int] | Indices to reorder data for balanced loading |
gpu_sample_counts | list[int] | Number of samples assigned to each GPU |
grouped_sizes_per_gpu | list[int] | Total size assigned to each GPU |
Source code in vllm/model_executor/models/vision.py
get_num_selected_vision_tokens ¶
get_num_selected_vision_tokens(
num_vision_tokens: int,
strategy: VisionFeatureSelectStrategy | str,
) -> int
Source code in vllm/model_executor/models/vision.py
get_vision_encoder_info ¶
get_vision_encoder_info(
hf_config: VisionLanguageConfig,
) -> VisionEncoderInfo
Source code in vllm/model_executor/models/vision.py
get_vit_attn_backend ¶
get_vit_attn_backend(
head_size: int,
dtype: dtype,
*,
attn_backend_override: AttentionBackendEnum
| None = None,
) -> AttentionBackendEnum
Get the available attention backend for Vision Transformer.
Source code in vllm/model_executor/models/vision.py
resolve_visual_encoder_outputs ¶
resolve_visual_encoder_outputs(
encoder_outputs: Tensor | list[Tensor],
post_layer_norm: LayerNorm | None,
*,
select_layers: list[int] | None = None,
max_possible_layers: int | None = None,
last_hs_proc: Callable[[Tensor], Tensor] | None = None,
feature_select_strategy: VisionFeatureSelectStrategy
| None = None,
) -> Tensor
Given the outputs a visual encoder module that may correspond to the output of the last layer, or a list of hidden states to be stacked, handle post normalization and resolve it into a single output tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoder_outputs | Tensor | list[Tensor] | Output of encoder's last layer or all hidden states. | required |
post_layer_norm | LayerNorm | None | Post norm to apply to the output of the encoder. | required |
select_layers | list[int] | None | Optional layer indices to grab from the encoder outputs; if provided, encoder outputs must be a list. | None |
max_possible_layers | int | None | Total layers in the fully loaded visual encoder. | None |
last_hs_proc | Callable[[Tensor], Tensor] | None | Optional callable to be applied to the last layer if it is used, e.g., pooling head for Siglip. This is done prior to feature selection and layer normalization. If select_layers are provided, the output of last_hs_proc must be able to be concatenated with the other select_layers along the last dimension. | None |
feature_select_strategy | VisionFeatureSelectStrategy | None | Defines how to select the hidden states from each layer. | None |
Source code in vllm/model_executor/models/vision.py
151 152 153 154 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 | |
run_dp_sharded_mrope_vision_model ¶
run_dp_sharded_mrope_vision_model(
vision_model: Module,
pixel_values: Tensor,
grid_thw_list: list[list[int]],
*,
rope_type: Literal["rope_3d", "rope_2d"],
) -> tuple[Tensor, ...]
Run a vision model with data parallelism (DP) sharding. The function will shard the input image tensor on the first dimension and run the vision model. This function is used to run the vision model with mrope.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vision_model | Module | Vision model. | required |
pixel_values | Tensor | Image/Video input tensor. | required |
grid_thw_list | list[list[int]] | List of grid dimensions for each image | required |
rope_type | Literal['rope_3d', 'rope_2d'] | Type of rope used in the vision model. Different rope types have different dimension to do ViT. "rope_3d" for 3D rope (e.g., Qwen2.5-VL) "rope_2d" for 2D rope (e.g., Kimi-VL) | required |
Returns: torch.Tensor: Output image embeddings
Example
Source code in vllm/model_executor/models/vision.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 | |
run_dp_sharded_vision_model ¶
Run a vision model with data parallelism (DP) sharding. The function will shard the input image tensor on the first dimension and run the vision model
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_input | Tensor | Image input tensor. | required |
vision_model | Module | Vision model. | required |
Returns: torch.Tensor: Output image embeddings
Source code in vllm/model_executor/models/vision.py
should_torch_compile_mm_vit ¶
should_torch_compile_mm_vit(
vllm_config: VllmConfig,
) -> bool
Callable to be passed to @support_torch_compile's enable_if argument.