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_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
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
) -> AttentionBackendEnum
Get the attention backend for Vision Transformer.
Source code in vllm/model_executor/models/vision.py
is_vit_use_data_parallel ¶
Get the tensor parallel type 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
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 | |
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
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 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 | |
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.