vllm.model_executor.models.moonvit ¶
Learnable2DInterpPosEmb ¶
Bases: Module
Source code in vllm/model_executor/models/moonvit.py
get_pos_embeds ¶
Build packed per-token positional embeddings for a list of grids.
Returns a tensor of shape (sum(h * w), dim) formed by interpolating the learned (height, width, dim) weight to each (h, w) grid and concatenating the flattened results in the same order as grid_hws_list. Lives outside the captured CUDA graph so the per-grid Python iteration is safe.
Source code in vllm/model_executor/models/moonvit.py
MLP2 ¶
Bases: Module
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dims | list[int] | [in_dim, hidden_dim, out_dim] | required |
bias | bool | whether to use bias in linear layer. | True |
Source code in vllm/model_executor/models/moonvit.py
MoonVisionPatchEmbed ¶
Bases: Module
Source code in vllm/model_executor/models/moonvit.py
forward ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x | (L, Channels) | input tensor | required |
grid_hw | (N, 2) | grid height and width | None |
pos_embeds | Tensor | None | precomputed positional embeddings of shape | None |
Returns:
| Type | Description |
|---|---|
Tensor | (L, Cout) tensor |
Source code in vllm/model_executor/models/moonvit.py
MoonVitEncoderLayer ¶
Bases: Module
Source code in vllm/model_executor/models/moonvit.py
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 | |
attention_qkvpacked ¶
attention_qkvpacked(
x: Tensor,
cu_seqlens: Tensor,
rope_freqs_cis: Tensor | None = None,
max_seqlen: Tensor | None = None,
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x | Tensor | (seqlen, hidden_dim) | required |
cu_seqlens | Tensor | | required |
max_seqlen | Tensor | None | Optional precomputed scalar tensor. When omitted it is derived from | None |
Source code in vllm/model_executor/models/moonvit.py
forward ¶
forward(
hidden_states: Tensor,
cu_seqlens: Tensor,
rope_freqs_cis: Tensor | None = None,
max_seqlen: Tensor | None = None,
) -> Tensor
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_states | Tensor | non-packed (B, N, D) or packed (L, D). if non-packed, seqlens should be None, if packed, seqlens should be set | required |
max_seqlen | Tensor | None | optional precomputed max-sequence-length scalar. | None |
Returns:
| Name | Type | Description |
|---|---|---|
output | Tensor | same shape of input, non-packed (B, N, D) for non-packed input, (L, D) for packed input |
Source code in vllm/model_executor/models/moonvit.py
MoonVitPretrainedModel ¶
Bases: PreTrainedModel
Source code in vllm/model_executor/models/moonvit.py
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 | |
forward ¶
forward(
pixel_values: Tensor,
grid_hw: Tensor,
*,
encoder_metadata: dict[str, Any] | None = None,
) -> Tensor | list[Tensor]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pixel_values | Tensor | The input pixel values. | required |
grid_hw | Tensor | The grid height and width. | required |
encoder_metadata | dict[str, Any] | None | Optional precomputed metadata produced by :meth: | None |
Source code in vllm/model_executor/models/moonvit.py
prepare_encoder_metadata ¶
prepare_encoder_metadata(
grid_hws_list: list[list[int]] | list[tuple[int, int]],
*,
max_batch_size: int | None = None,
max_seqlen_override: int | None = None,
device: device | None = None,
) -> dict[str, Any]
Precompute every grid-dependent input the encoder needs.
Shared by the eager forward path and the CUDA graph capture/replay path. Everything here is computed outside the captured graph, so per-grid Python iteration and .tolist() round-trips are fine; the values are then copied into fixed-shape buffers for replay.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid_hws_list | list[list[int]] | list[tuple[int, int]] | List of | required |
max_batch_size | int | None | When set, | None |
max_seqlen_override | int | None | Override the per-replay max sequence length scalar. At capture this must be a safe upper bound (worst case: a single image consuming the full token budget) because the value is baked into the captured graph. | None |
device | device | None | Device for the metadata tensors. Defaults to the model's parameter device. | None |
Source code in vllm/model_executor/models/moonvit.py
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 | |
Rope2DPosEmb ¶
Bases: Module
2D rotary position embedding with multi-resolution support.
This class is intended to be used in the following way: 1. Before training, create an instance of Rope2DPosEmb. This instance will hold the precomputed cis. 2. Before each forward pass, call get_freqs_cis_by_* to get the freqs_cis tensor for this iteration. 3. During the forward pass, pass the freqs_cis tensor to each attention layer, and call apply just before each attention operation. The rope is shared across all attention layers and all heads.
Refs: - RoFormer: https://arxiv.org/abs/2104.09864 - VisionLLaMA: https://arxiv.org/abs/2403.00522 - https://github.com/Meituan-AutoML/VisionLLaMA/blob/main/dit/models.py
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim | int | usually the multi-head attention dimension, should be divisible by 4 (TODO: relax this constraint if needed) | required |
max_height | int | the maximum height of the 2D grid | required |
max_width | int | the maximum width of the 2D grid | required |
theta_base | float | the base of the theta | 10000 |
device | str | the device to store the precomputed cis | device_type |
Source code in vllm/model_executor/models/moonvit.py
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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | |
precomputed_freqs_cis cached property ¶
precomputed_freqs_cis: Tensor
Calculate the cis(freqs) for each position in the 2D grid.
complex tensor of shape (max_height, max_width, dim//2) and value:
height axis: ret[h, w, 2i] = cis(h * theta_base(-4i/dim)) weight axis: ret[h, w, 2i+1] = cis(w * theta_base(-4i/dim)) with (i in [0, dim//4)) note: cis is a mathematical notation defined by cis x = cos x + i sin x,
get_freqs_cis_by_idx ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pos_idx | Tensor | tensor of shape (..., 2), It contains the (h, w) position indices of each 2D token. | required |
pos_idx_mask | Tensor | a mask of shape (...), the leading dimensions should be the same as pos_idx. Rope will only be applied to the tokens with True mask. | required |
Return: freqs_cis: tensor of shape (..., dim//2)
Source code in vllm/model_executor/models/moonvit.py
get_freqs_cis_by_seqlens ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid_hws | Tensor | containing list of (height, width) or (t, height, width) tuples. | required |
Returns: freqs_cis: tensor of shape (sum(t * height * width), dim//2)
Source code in vllm/model_executor/models/moonvit.py
get_freqs_cis_by_seqlens_list ¶
List-based variant of :meth:get_freqs_cis_by_seqlens.
Accepts a Python list of (h, w) pairs so callers that already operate outside the captured CUDA graph can avoid materializing a tensor + .tolist() round-trip.
Source code in vllm/model_executor/models/moonvit.py
_build_merge_gather_idx ¶
_build_merge_gather_idx(
grid_hws_list: list[list[int]] | list[tuple[int, int]],
merge_kernel_size: tuple[int, int],
) -> ndarray
Build the per-token gather indices used by :func:patch_merger_packed.
For each item with grid (h, w) and merge kernel (kh, kw), the output block at position (nh, nw) gathers the khkw input tokens at rows (nhkh + ih, nw*kw + iw) of that item, in (ih, iw) row-major order.
Source code in vllm/model_executor/models/moonvit.py
apply_rope ¶
(The leading dimensions of all inputs should be the same)
| Name | Type | Description | Default |
|---|---|---|---|
xq | Tensor | query, tensor of shape (..., num_heads, head_dim) | required |
xk | Tensor | key, tensor of shape (..., num_heads, head_dim) | required |
freqs_cis | Tensor | tensor of shape (..., head_dim/2), dtype=torch.complex64. It contains the precomputed cis(freqs) for each position in the 2D grid. | required |
Returns: xq_out, xk_out: tensors of shape (..., num_heads, head_dim)
Source code in vllm/model_executor/models/moonvit.py
patch_merger_packed ¶
CUDA-graph-safe equivalent of :func:patch_merger.
Uses a precomputed index tensor to gather the per-token reshape + permute that patch_merger does inside a Python loop. The output is the concatenated 3D tensor (sum(new_h * new_w), kh * kw, d_model), matching what torch.cat(patch_merger(...)) would produce.