vllm.models.deepseek_v4.common.vision ¶
DeepSeek-V4 vision tower (ViT + aligner) with TP-sharded linears.
Ported from the official reference implementation (deepseek-ai/DeepSeek-V4-Flash-Vision-Exp). Weight names match the HF checkpoint so no renaming is needed at load time. Attention and MLP weights are tensor-parallel sharded (replicated when the vision head count is not divisible by TP size, or under --mm-encoder-tp-mode data); the patch embed and norms are replicated, so the residual stream is full-width on every rank.
Classes:
-
DeepseekV4Aligner–Spatial merge (downsample_ratio x downsample_ratio) + MLP projector.
-
DeepseekV4ViT–DeepSeek-V4 ViT: full bidirectional attention per image, 2D RoPE.
Functions:
-
build_packed_merge_metadata–Gather indices/mask for
DeepseekV4Aligner.forward_packed. -
build_packed_vit_metadata–Precompute packed-batch ViT metadata for
DeepseekV4ViT.forward_packed. -
run_dp_sharded_vision_tower–Run the ViT + aligner with images sharded across TP ranks.
DeepseekV4Aligner ¶
Bases: Module
Spatial merge (downsample_ratio x downsample_ratio) + MLP projector.
Methods:
-
forward_packed–Packed multi-image path for encoder CUDA graphs.
Source code in vllm/models/deepseek_v4/common/vision.py
forward_packed(x, merge_idx, merge_mask) ¶
Packed multi-image path for encoder CUDA graphs.
merge_idx/merge_mask come from build_packed_merge_metadata; gather+mask reproduces the eager F.pad + F.unfold merge exactly (unfold rows are channel-major, so the gathered patch rows are transposed before flattening).
Source code in vllm/models/deepseek_v4/common/vision.py
DeepseekV4ViT ¶
Bases: Module
DeepSeek-V4 ViT: full bidirectional attention per image, 2D RoPE.
Methods:
-
forward_packed–Varlen path for encoder CUDA graphs: multiple images packed along
Source code in vllm/models/deepseek_v4/common/vision.py
forward_packed(patches, cos, sin, cu_seqlens, max_seqlen) ¶
Varlen path for encoder CUDA graphs: multiple images packed along rows, per-image attention via cu_seqlens, RoPE tables precomputed on device. Pure-tensor (no host reads, no H2D), safe to capture.
Source code in vllm/models/deepseek_v4/common/vision.py
build_packed_merge_metadata(grids, downsample_ratio, *, device, dtype) ¶
Gather indices/mask for DeepseekV4Aligner.forward_packed.
For each aligner output row, the r x r source patch rows in the packed ViT output; positions past the image edge (the eager path pads them with zeros) get mask 0 and a clamped in-range index.
Source code in vllm/models/deepseek_v4/common/vision.py
build_packed_vit_metadata(grids, *, rope_dim, rope_theta, device, max_seqlen_override=None, cached=True) ¶
Precompute packed-batch ViT metadata for DeepseekV4ViT.forward_packed.
Parameters:
-
(grids¶list[list[int]] | list[tuple[int, int]]) –[n_vit_h, n_vit_w]per image, in packing order. -
(rope_dim¶int) –RoPE embedding dimension of the ViT.
-
(rope_theta¶float) –RoPE base frequency of the ViT.
-
(device¶device) –Device for the returned tensors (except
max_seqlen, which stays on the host). -
(max_seqlen_override¶int | None, default:None) –Worst-case value baked in at CUDA graph capture (the attention wrapper reads
max_seqlenon the host, so the capture-time value becomes a graph constant). -
(cached¶bool, default:True) –Use the shared
lru_cachefor RoPE tables. Capture-time dummy grids passFalseto avoid evicting real entries.
Source code in vllm/models/deepseek_v4/common/vision.py
run_dp_sharded_vision_tower(vision_model, aligner, patches, vit_grid) ¶
Run the ViT + aligner with images sharded across TP ranks.
Every rank holds the full tower weights (--mm-encoder-tp-mode data) and receives the full patches batch. Images are assigned to ranks by patch count (greedy load balancing), each rank encodes only its share, and per-image embeddings are exchanged with one padded all-gather and returned in the original image order.
Parameters:
-
(vision_model¶DeepseekV4ViT) –The (weight-replicated) ViT tower.
-
(aligner¶DeepseekV4Aligner) –The (weight-replicated) spatial-merge projector.
-
(patches¶Tensor) –(sum(n_vit_h * n_vit_w), 3, p, p)patches of all images. -
(vit_grid¶list[list[int]]) –[n_vit_h, n_vit_w]per image.
Returns:
Source code in vllm/models/deepseek_v4/common/vision.py
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 | |