Skip to content

vllm.model_executor.models.ernie45_vl

Inference-only Ernie VL model compatible with HuggingFace weights.

Classes:

Functions:

Ernie4_5_VLImagePixelInputs

Bases: TensorSchema

Dimensions
  • np: The total number of patches over each image over each prompt in the batch
  • ni: Number of images
  • cps: Number of channels * patch_size * patch_size
Source code in vllm/model_executor/models/ernie45_vl.py
class Ernie4_5_VLImagePixelInputs(TensorSchema):
    """
    Dimensions:
        - np: The total number of patches over each image over each prompt in
              the batch
        - ni: Number of images
        - cps: Number of channels * patch_size * patch_size
    """

    type: Literal["pixel_values"]

    pixel_values: Annotated[torch.Tensor, TensorShape("np", "cps")]
    image_grid_thw: Annotated[torch.Tensor, TensorShape("ni", 3)]

Ernie4_5_VLMoeForConditionalGeneration

Bases: Module, SupportsMultiModal, SupportsLoRA, SupportsPP, SupportsMRoPE, SupportsEncoderCudaGraph

Source code in vllm/model_executor/models/ernie45_vl.py
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
@MULTIMODAL_REGISTRY.register_processor(
    Ernie4_5VLMultiModalProcessor,
    info=Ernie4_5_VLProcessingInfo,
    dummy_inputs=Ernie4_5_VLDummyInputsBuilder,
)
class Ernie4_5_VLMoeForConditionalGeneration(
    nn.Module,
    SupportsMultiModal,
    SupportsLoRA,
    SupportsPP,
    SupportsMRoPE,
    SupportsEncoderCudaGraph,
):
    packed_modules_mapping = {
        "qkv_proj": [
            "q_proj",
            "k_proj",
            "v_proj",
        ],
        "gate_up_proj": [
            "gate_proj",
            "up_proj",
        ],
    }

    # To ensure correct weight loading and mapping.
    hf_to_vllm_mapper = WeightsMapper(
        orig_to_new_prefix={
            "lm_head.": "language_model.lm_head.",
            "model.": "language_model.model.",
            # model.resampler_model.-> language_model.model.resampler_model.
            # language_model.model.resampler_model. -> resampler_model.
            "language_model.model.resampler_model.": "resampler_model.",
        },
        # resampler_weight_mappings
        orig_to_new_substr={
            "spatial_linear.0.": "spatial_linear1.",
            "spatial_linear.2.": "spatial_linear2.",
            "spatial_linear.3.": "spatial_norm.",
            "temporal_linear.0.": "temporal_linear1.",
            "temporal_linear.2.": "temporal_linear2.",
            "temporal_linear.3.": "temporal_norm.",
        },
    )

    @classmethod
    def get_placeholder_str(cls, modality: str, i: int) -> str | None:
        if modality.startswith("image"):
            return "<|IMAGE_START|><|image@placeholder|><|IMAGE_END|>"
        if modality.startswith("video"):
            return "<|VIDEO_START|><|video@placeholder|><|VIDEO_END|>"

        raise ValueError("Only image or video modality is supported")

    def __init__(self, vllm_config: VllmConfig, prefix: str = "") -> None:
        super().__init__()
        config = vllm_config.model_config.hf_config
        quant_config = vllm_config.quant_config
        multimodal_config = vllm_config.model_config.multimodal_config

        self.config = config
        self.multimodal_config = multimodal_config

        with self._mark_tower_model(vllm_config, {"image", "video"}):
            self.vision_model = Ernie4_5_VisionTransformer(
                config.vision_config,
                norm_eps=getattr(config, "rms_norm_eps", 1e-6),
                quant_config=quant_config,
                prefix=maybe_prefix(prefix, "vision_model"),
            )
            self.resampler_model = VariableResolutionResamplerModel(
                self.config.pixel_hidden_size,
                self.config.hidden_size,
                self.config.spatial_conv_size,
                self.config.temporal_conv_size,
                config=self.config,
                prefix=maybe_prefix(prefix, "resampler_model"),
            )

        with self._mark_language_model(vllm_config):
            self.language_model = Ernie4_5_VLMoeForCausalLM(
                vllm_config=vllm_config,
                prefix=maybe_prefix(prefix, "language_model"),
            )

        self.visual_token_mask = None
        self.make_empty_intermediate_tensors = (
            self.language_model.make_empty_intermediate_tensors
        )
        if getattr(self.config, "im_patch_id", None):
            visual_token_ids = [
                token_id
                for token_id in [
                    self.config.im_patch_id,
                    getattr(self.config, "image_start_token_id", None),
                    getattr(self.config, "image_end_token_id", None),
                    getattr(self.config, "video_start_token_id", None),
                    getattr(self.config, "video_end_token_id", None),
                ]
                if token_id is not None
            ]
            self._visual_token_ids_tensor_cache = torch.tensor(
                visual_token_ids, dtype=torch.long
            )
        else:
            self._visual_token_ids_tensor_cache = None

    def compute_logits(
        self,
        hidden_states: torch.Tensor,
    ) -> torch.Tensor | None:
        return self.language_model.compute_logits(hidden_states)

    def _vision_forward(
        self,
        pixel_values: torch.Tensor,
        grid_thw: torch.Tensor,
    ) -> torch.Tensor:
        if grid_thw is not None:
            grid_thw = grid_thw[grid_thw > 0]
            if grid_thw.numel() % 3 != 0:
                raise ValueError(
                    f"grid_thw has {grid_thw.numel()} elements after filtering,"
                    "which is not divisible by 3."
                )
            grid_thw = grid_thw.reshape(-1, 3)
            # example: [[1,64,64],[2,80,80]] -> [[1,64,64],[1,80,80],[1,80,80]]
            grid_thw = F.pad(
                torch.repeat_interleave(grid_thw[:, 1:], grid_thw[:, 0], 0),
                [1, 0, 0, 0],
                value=1,
            )
        image_features = self.vision_model(pixel_values, grid_thw)
        return image_features

    def _set_visual_token_mask(self, input_ids: torch.Tensor) -> None:
        """Set mask for visual tokens (image/video patches and delimiters)."""
        if self._visual_token_ids_tensor_cache is None:
            self.visual_token_mask = None
            return
        # Create tensor on the correct device
        visual_token_ids_tensor = self._visual_token_ids_tensor_cache.to(
            device=input_ids.device,
            dtype=input_ids.dtype,
        )

        self.visual_token_mask = torch.isin(input_ids, visual_token_ids_tensor).reshape(
            -1, 1
        )

    def get_mrope_input_positions(
        self,
        input_tokens: list[int],
        mm_features: list[MultiModalFeatureSpec],
    ) -> tuple[torch.Tensor, int]:
        llm_pos_ids_list: list = []
        st = 0

        for (
            offset,
            llm_grid_t,
            llm_grid_h,
            llm_grid_w,
        ) in self.iter_mm_grid_thw(mm_features):
            text_len = offset - st
            st_idx = llm_pos_ids_list[-1].max() + 1 if len(llm_pos_ids_list) > 0 else 0
            llm_pos_ids_list.append(
                np.broadcast_to(np.arange(text_len), (3, text_len)) + st_idx
            )

            grid_indices = np.indices((llm_grid_t, llm_grid_h, llm_grid_w)).reshape(
                3, -1
            )
            llm_pos_ids_list.append(grid_indices + text_len + st_idx)
            st = offset + llm_grid_t * llm_grid_h * llm_grid_w

        if st < len(input_tokens):
            text_len = len(input_tokens) - st
            st_idx = llm_pos_ids_list[-1].max() + 1 if len(llm_pos_ids_list) > 0 else 0
            llm_pos_ids_list.append(
                np.broadcast_to(np.arange(text_len), (3, text_len)) + st_idx
            )

        llm_positions = np.concatenate(llm_pos_ids_list, axis=1).reshape(3, -1)
        mrope_position_delta = (llm_positions.max() + 1 - len(input_tokens)).item()
        return torch.from_numpy(llm_positions), mrope_position_delta

    def iter_mm_grid_thw(
        self, mm_features: list[MultiModalFeatureSpec]
    ) -> Iterator[tuple[int, int, int, int]]:
        spatial_conv_size = self.config.spatial_conv_size
        temporal_conv_size = self.config.temporal_conv_size

        for mm_feature in sorted(mm_features, key=lambda f: f.mm_position.offset):
            if mm_feature.data is None:
                raise ValueError("M-RoPE calculation requires multimodal feature data")

            offset = mm_feature.mm_position.offset
            if mm_feature.modality == "image":
                t, h, w = mm_feature.data["image_grid_thw"].data.tolist()
                yield offset, t, h // spatial_conv_size, w // spatial_conv_size
            elif mm_feature.modality == "video":
                t, h, w = mm_feature.data["video_grid_thw"].data.tolist()
                yield (
                    offset,
                    t // temporal_conv_size,
                    h // spatial_conv_size,
                    w // spatial_conv_size,
                )
            else:
                raise ValueError(f"Unsupported modality: {mm_feature.modality}")

    def _parse_and_validate_image_input(
        self, **kwargs: object
    ) -> Ernie4_5_VLImageInputs | None:
        pixel_values = kwargs.pop("pixel_values", None)
        image_grid_thw = kwargs.pop("image_grid_thw", None)

        if pixel_values is None:
            return None

        if pixel_values is not None:
            return Ernie4_5_VLImagePixelInputs(
                type="pixel_values",
                pixel_values=pixel_values,
                image_grid_thw=image_grid_thw,
            )

    def _parse_and_validate_video_input(
        self, **kwargs: object
    ) -> Ernie4_5_VLVideoInputs | None:
        pixel_values_videos = kwargs.pop("pixel_values_videos", None)
        video_grid_thw = kwargs.pop("video_grid_thw", None)

        if pixel_values_videos is None:
            return None

        if pixel_values_videos is not None:
            return Ernie4_5_VLVideoPixelInputs(
                type="pixel_values_videos",
                pixel_values_videos=pixel_values_videos,
                video_grid_thw=video_grid_thw,
            )

    # -- SupportsEncoderCudaGraph protocol methods --
    #
    # Only the vision transformer (self.vision_model) is captured into the
    # CUDA graph. The VariableResolutionResamplerModel (spatial merge +
    # projection) runs eagerly in encoder_eager_forward / postprocess_
    # encoder_output because its temporal path performs host-side (numpy)
    # indexing that cannot be captured. Image only: video uses a temporal
    # conv that changes the output token count, so it falls back to the
    # eager multimodal path for now.

    def get_encoder_cudagraph_config(self):
        from vllm.v1.worker.encoder_cudagraph_defs import EncoderCudaGraphConfig

        return EncoderCudaGraphConfig(
            modalities=["image"],
            # Ernie consumes a single rotary freqs tensor (not cos/sin)
            buffer_keys=[
                "pixel_values",
                "rotary_pos_emb",
                "cu_seqlens",
                "max_seqlen",
            ],
            # Post-merge embeddings, produced in postprocess_encoder_output,
            # are at the LM hidden dim, used only for DP gather sizing.
            out_hidden_size=self.config.hidden_size,
        )

    def get_encoder_cudagraph_budget_range(self, vllm_config) -> tuple[int, int]:
        # Min: a 224x224 image -> 16x16 patches (patch_size=14),
        # spatial_merge_size=2 -> 8x8 = 64 output tokens.
        min_budget = 64
        max_budget = min(
            vllm_config.scheduler_config.max_num_batched_tokens,
            vllm_config.model_config.max_model_len,
        )
        return (min_budget, max_budget)

    def get_encoder_cudagraph_item_specs(self, mm_kwargs: dict[str, Any]):
        from vllm.v1.worker.encoder_cudagraph_defs import EncoderItemSpec

        m = self.vision_model.spatial_merge_size
        grid_thw = mm_kwargs["image_grid_thw"].tolist()
        return [
            EncoderItemSpec(
                input_size=t * h * w,
                output_tokens=(t * h * w) // m // m,
            )
            for t, h, w in grid_thw
        ]

    def select_encoder_cudagraph_items(
        self, mm_kwargs: dict[str, Any], indices: list[int]
    ) -> dict[str, Any]:
        grid_thw = mm_kwargs["image_grid_thw"]
        pixel_values = mm_kwargs["pixel_values"]

        if len(indices) == 0:
            return {"pixel_values": pixel_values[:0], "image_grid_thw": grid_thw[:0]}

        # Cumulative patch offsets for slicing the concatenated pixel_values.
        patches_per_item = (grid_thw[:, 0] * grid_thw[:, 1] * grid_thw[:, 2]).tolist()
        cum_patches = [0]
        for p in patches_per_item:
            cum_patches.append(cum_patches[-1] + p)
        selected_pv = torch.cat(
            [pixel_values[cum_patches[i] : cum_patches[i + 1]] for i in indices]
        )
        return {"pixel_values": selected_pv, "image_grid_thw": grid_thw[indices]}

    def prepare_encoder_cudagraph_capture_inputs(
        self,
        token_budget,
        max_batch_size,
        max_frames_per_batch,
        device,
        dtype,
        path: str = "default",
    ):
        from vllm.v1.worker.encoder_cudagraph_defs import (
            EncoderCudaGraphCaptureInputs,
        )

        m = self.vision_model.spatial_merge_size
        # Ceil so the buffer fits the worst case of one item using the full
        # budget.
        per_mm_item_output = (token_budget + max_batch_size - 1) // max_batch_size
        # Image-format grid (T=1): h=m, w=per_mm_item_output*m, so the merged
        # token count per item equals per_mm_item_output.
        grid_config = [[1, m, per_mm_item_output * m] for _ in range(max_batch_size)]

        patch_embed = self.vision_model.patch_embed
        in_channels = patch_embed.in_channels
        patch_size = patch_embed.patch_size
        total_patches = sum(t * h * w for t, h, w in grid_config)
        flattened_patch_size = in_channels * patch_size * patch_size
        dummy_pixel_values = torch.randn(
            total_patches, flattened_patch_size, device=device, dtype=dtype
        )

        # max_seqlen is baked at capture: worst case is one item consuming the
        # full budget - seq_len = token_budget * spatial_merge_size**2.
        metadata = self.vision_model.prepare_encoder_metadata(
            grid_config,
            max_batch_size=max_batch_size,
            max_seqlen_override=token_budget * (m**2),
            device=device,
        )
        values = metadata | {"pixel_values": dummy_pixel_values}
        return EncoderCudaGraphCaptureInputs(values=values)

    def prepare_encoder_cudagraph_replay_buffers(
        self, mm_kwargs, max_batch_size, max_frames_per_batch, path: str = "default"
    ):
        from vllm.v1.worker.encoder_cudagraph_defs import (
            EncoderCudaGraphReplayBuffers,
        )

        # The per-image grids are needed as Python ints to size the buffers.
        with gpu_sync_allowed():
            grid_thw_list = mm_kwargs["image_grid_thw"].tolist()
        metadata = self.vision_model.prepare_encoder_metadata(
            grid_thw_list, max_batch_size=max_batch_size
        )
        values = metadata | {
            "pixel_values": mm_kwargs["pixel_values"],
        }
        return EncoderCudaGraphReplayBuffers(values=values)

    def encoder_cudagraph_forward(
        self, values: dict[str, torch.Tensor], path: str = "default"
    ) -> torch.Tensor:
        # Graph captures the ViT only, and the resampler runs in
        # postprocess_encoder_output.
        pixel_values = values.pop("pixel_values")
        return self.vision_model(pixel_values, encoder_metadata=values)

    def encoder_eager_forward(
        self, mm_kwargs: dict[str, Any], path: str = "default"
    ) -> torch.Tensor:
        # Eager fallback: run the full pipeline (ViT + resampler). The result
        # is scattered directly, so it must be the post-merge embeddings.
        pixel_values = mm_kwargs["pixel_values"].type(self.vision_model.dtype)
        grid_thw = mm_kwargs["image_grid_thw"].to(
            self.vision_model.device, non_blocking=True
        )
        image_features = self.vision_model(pixel_values, grid_thw)
        return self.resampler_model(image_features, grid_thw)

    def postprocess_encoder_output(
        self,
        outputs: dict[str, torch.Tensor],
        indices: list[int],
        per_item_out_tokens: list[int],
        dest,
        clone: bool = False,
        batch_mm_kwargs: dict[str, Any] | None = None,
    ) -> None:
        # The graph output is the raw ViT features (pre-merge), padded to the
        # token budget. Run the resampler eagerly on the valid portion using
        # the actual batch grid_thw, then scatter the post-merge embeddings.
        # Ernie only uses the single "default" encoder path.
        output = outputs["default"]
        grid_thw_cpu = batch_mm_kwargs["image_grid_thw"]
        grid_thw = grid_thw_cpu.to(output.device, non_blocking=True)
        # The valid token count slices the graph output for the eager
        # resampler call, so it has to come back to the host.
        num_valid = int(
            (grid_thw_cpu[:, 0] * grid_thw_cpu[:, 1] * grid_thw_cpu[:, 2]).sum()
        )
        image_embeds = self.resampler_model(output[:num_valid], grid_thw)
        scatter_output_slices(image_embeds, indices, per_item_out_tokens, dest, clone)

    def _process_image_input(
        self, image_input: Ernie4_5_VLImageInputs
    ) -> tuple[torch.Tensor, ...]:
        grid_thw = image_input["image_grid_thw"]
        assert grid_thw.ndim == 2

        pixel_values = image_input["pixel_values"].type(self.vision_model.dtype)
        image_features = self._vision_forward(
            pixel_values=pixel_values, grid_thw=grid_thw
        )
        image_embeds = self.resampler_model(image_features, grid_thw)

        merge_size = self.vision_model.spatial_merge_size
        sizes = grid_thw.prod(-1) // merge_size // merge_size

        return image_embeds.split(sizes.tolist())

    def _process_video_input(
        self, video_input: Ernie4_5_VLVideoInputs
    ) -> tuple[torch.Tensor, ...]:
        grid_thw = video_input["video_grid_thw"]
        assert grid_thw.ndim == 2

        pixel_values_videos = video_input["pixel_values_videos"].type(
            self.vision_model.dtype
        )
        video_features = self._vision_forward(
            pixel_values=pixel_values_videos, grid_thw=grid_thw
        )
        video_embeds = self.resampler_model(video_features, grid_thw)

        merge_size = self.vision_model.spatial_merge_size
        sizes = (
            (grid_thw.prod(-1) // self.config.temporal_conv_size)
            // merge_size
            // merge_size
        )

        return video_embeds.split(sizes.tolist())

    def _parse_and_validate_multimodal_inputs(self, **kwargs: object) -> dict:
        modalities = {}

        # Preserve the order of modalities if there are multiple of them
        # from the order of kwargs.
        for input_key in kwargs:
            if (
                input_key in ("pixel_values", "image_embeds")
                and "images" not in modalities
            ):
                modalities["images"] = self._parse_and_validate_image_input(**kwargs)
            if (
                input_key in ("pixel_values_videos", "video_embeds")
                and "videos" not in modalities
            ):
                modalities["videos"] = self._parse_and_validate_video_input(**kwargs)

        return modalities

    def embed_multimodal(self, **kwargs: object) -> MultiModalEmbeddings | None:
        modalities = self._parse_and_validate_multimodal_inputs(**kwargs)
        if not modalities:
            return None

        # The result multimodal_embeddings is tuple of tensors, with each
        # tensor corresponding to a multimodal data item (image or video).
        multimodal_embeddings: tuple[torch.Tensor, ...] = ()

        # NOTE: It is important to iterate over the keys in this dictionary
        # to preserve the order of the modalities.
        for modality in modalities:
            if modality == "images":
                image_input = modalities["images"]
                image_embeddings = self._process_image_input(image_input)
                multimodal_embeddings += tuple(image_embeddings)
            if modality == "videos":
                video_input = modalities["videos"]
                video_embeddings = self._process_video_input(video_input)
                multimodal_embeddings += tuple(video_embeddings)

        return multimodal_embeddings

    def embed_input_ids(
        self,
        input_ids: torch.Tensor,
        multimodal_embeddings: MultiModalEmbeddings | None = None,
        *,
        is_multimodal: torch.Tensor | None = None,
    ) -> torch.Tensor:
        if multimodal_embeddings is not None and len(multimodal_embeddings) > 0:
            self._set_visual_token_mask(input_ids)

        # This is to satisfy the type checker for each overload
        if multimodal_embeddings is None or is_multimodal is None:
            return super().embed_input_ids(input_ids)

        return super().embed_input_ids(
            input_ids,
            multimodal_embeddings=multimodal_embeddings,
            is_multimodal=is_multimodal,
        )

    def forward(
        self,
        input_ids: torch.Tensor | None,
        positions: torch.Tensor,
        intermediate_tensors: IntermediateTensors | None = None,
        inputs_embeds: torch.Tensor | None = None,
        **kwargs,
    ):
        forward_kwargs = {
            "input_ids": input_ids,
            "positions": positions,
            "intermediate_tensors": intermediate_tensors,
            "inputs_embeds": inputs_embeds,
        }

        if self.visual_token_mask is not None:
            if self.visual_token_mask.shape[0] != inputs_embeds.shape[0]:
                padding_len = inputs_embeds.shape[0] - self.visual_token_mask.shape[0]
                # right pad False
                pad = torch.zeros(
                    (padding_len, self.visual_token_mask.shape[1]),
                    dtype=self.visual_token_mask.dtype,
                    device=self.visual_token_mask.device,
                )
                self.visual_token_mask = torch.cat([self.visual_token_mask, pad], dim=0)

            forward_kwargs.update({"visual_token_mask": self.visual_token_mask})
            self.visual_token_mask = None

        hidden_states = self.language_model.model(
            **forward_kwargs,
            **kwargs,
        )

        return hidden_states

    def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]:
        loader = AutoWeightsLoader(self)
        return loader.load_weights(weights, mapper=self.hf_to_vllm_mapper)

_set_visual_token_mask(input_ids)

Set mask for visual tokens (image/video patches and delimiters).

Source code in vllm/model_executor/models/ernie45_vl.py
def _set_visual_token_mask(self, input_ids: torch.Tensor) -> None:
    """Set mask for visual tokens (image/video patches and delimiters)."""
    if self._visual_token_ids_tensor_cache is None:
        self.visual_token_mask = None
        return
    # Create tensor on the correct device
    visual_token_ids_tensor = self._visual_token_ids_tensor_cache.to(
        device=input_ids.device,
        dtype=input_ids.dtype,
    )

    self.visual_token_mask = torch.isin(input_ids, visual_token_ids_tensor).reshape(
        -1, 1
    )

Ernie4_5_VLVideoPixelInputs

Bases: TensorSchema

Dimensions
  • np: The total number of patches over each image over each prompt in the batch
  • ni: Number of images
  • cps: Number of channels * temporal_patch_size * patch_size * patch_size
Source code in vllm/model_executor/models/ernie45_vl.py
class Ernie4_5_VLVideoPixelInputs(TensorSchema):
    """
    Dimensions:
        - np: The total number of patches over each image over each prompt in
              the batch
        - ni: Number of images
        - cps: Number of channels * temporal_patch_size * patch_size *
              patch_size
    """

    type: Literal["pixel_values_videos"]
    pixel_values_videos: Annotated[torch.Tensor, TensorShape("np", "cps")]
    video_grid_thw: Annotated[torch.Tensor, TensorShape("ni", 3)]

Ernie4_5_VisionAttention

Bases: Module

VisionAttention using VLLM framework APIs

Source code in vllm/model_executor/models/ernie45_vl.py
class Ernie4_5_VisionAttention(nn.Module):
    """VisionAttention using VLLM framework APIs"""

    def __init__(
        self,
        embed_dim: int,
        num_heads: int,
        projection_size: int,
        quant_config: QuantizationConfig | None = None,
        prefix: str = "",
    ) -> None:
        super().__init__()
        # Per attention head and per partition values.
        self.tp_size = parallel_state.get_tensor_model_parallel_world_size()
        self.tp_rank = parallel_state.get_tensor_model_parallel_rank()
        self.hidden_size_per_attention_head = dist_utils.divide(
            projection_size, num_heads
        )
        self.num_attention_heads_per_partition = dist_utils.divide(
            num_heads, self.tp_size
        )

        self.qkv = QKVParallelLinear(
            hidden_size=embed_dim,
            head_size=self.hidden_size_per_attention_head,
            total_num_heads=num_heads,
            total_num_kv_heads=num_heads,
            bias=True,
            quant_config=quant_config,
            prefix=f"{prefix}.qkv",
        )
        self.proj = RowParallelLinear(
            input_size=projection_size,
            output_size=embed_dim,
            quant_config=quant_config,
            prefix=f"{prefix}.proj",
        )

        self.attn = MMEncoderAttention(
            num_heads=self.num_attention_heads_per_partition,
            head_size=self.hidden_size_per_attention_head,
            scale=self.hidden_size_per_attention_head**-0.5,
            prefix=f"{prefix}.attn",
        )

        self.apply_rotary_emb = ApplyRotaryEmb(
            enforce_enable=True,
            enable_fp32_compute=True,
        )

    def split_qkv(self, qkv: torch.Tensor) -> tuple[torch.Tensor, ...]:
        # [s, b, 3 * head * head_dim]
        seq_len, bs, _ = qkv.shape
        if self.tp_size > 1:
            qkv = all_gather_interleave(qkv, self.qkv.hidden_size, self.tp_size)

        # [s, b, 3 * head * head_dim] -> 3 * [s, b, head * head_dim]
        q, k, v = qkv.chunk(3, dim=2)

        # 3 * [s, b, head * head_dim]
        if self.tp_size > 1:
            splitter = partial(
                dist_utils.split_tensor_along_last_dim, num_partitions=self.tp_size
            )
            q = splitter(q)[self.tp_rank]
            k = splitter(k)[self.tp_rank]
            v = splitter(v)[self.tp_rank]

        # 3 * [s, b, head * head_dim] -> 3 * [s, b, head, head_dim]
        new_shape = (
            seq_len,
            bs,
            self.num_attention_heads_per_partition,
            self.hidden_size_per_attention_head,
        )
        q, k, v = (x.view(*new_shape) for x in (q, k, v))
        return q, k, v

    def forward(
        self,
        x: torch.Tensor,
        cu_seqlens: torch.Tensor,
        rotary_pos_emb: torch.Tensor,
        max_seqlen: torch.Tensor | None = None,  # Only used for Flash Attention
    ) -> torch.Tensor:
        # [s, b, c] --> [s, b, head * 3 * head_dim]
        x, _ = self.qkv(x)

        # [s, b, 3 * head * head_dim] -> 3 * [s, b, head, head_dim]
        q, k, v = self.split_qkv(x)

        q, k, v = (rearrange(x, "s b ... -> b s ...").contiguous() for x in (q, k, v))
        if rotary_pos_emb is not None:
            qk_concat = torch.cat([q, k], dim=0)
            qk_rotated = self.apply_rotary_emb(
                qk_concat,
                rotary_pos_emb.cos(),
                rotary_pos_emb.sin(),
            )
            q, k = torch.chunk(qk_rotated, 2, dim=0)

        output = self.attn(
            query=q,
            key=k,
            value=v,
            cu_seqlens=cu_seqlens,
            max_seqlen=max_seqlen,
        )
        context_layer = rearrange(output, "b s h d -> s b (h d)").contiguous()

        output, _ = self.proj(context_layer)
        return output

Ernie4_5_VisionTransformer

Bases: Module

Methods:

Source code in vllm/model_executor/models/ernie45_vl.py
class Ernie4_5_VisionTransformer(nn.Module):
    def __init__(
        self,
        vision_config,
        norm_eps: float = 1e-6,
        quant_config: QuantizationConfig | None = None,
        prefix: str = "",
    ) -> None:
        super().__init__()
        patch_size = vision_config.patch_size
        spatial_merge_size = vision_config.spatial_merge_size
        in_channels = vision_config.in_channels
        hidden_size = vision_config.hidden_size
        embed_dim = vision_config.embed_dim
        depth = vision_config.depth
        num_heads = vision_config.num_heads
        mlp_ratio = vision_config.mlp_ratio

        self.spatial_merge_size = spatial_merge_size
        self.num_heads = num_heads
        self.embed_dim = embed_dim

        self.patch_embed = Ernie4_5_VisionPatchEmbed(
            patch_size=patch_size,
            in_channels=in_channels,
            embed_dim=embed_dim,
            prefix=f"{prefix}.patch_embed",
        )

        norm_layer = partial(nn.LayerNorm, eps=norm_eps)
        head_dim = embed_dim // num_heads
        self.rotary_pos_emb = Ernie4_5_VisionRotaryEmbedding(head_dim // 2)

        self.blocks = nn.ModuleList(
            [
                Ernie4_5_VisionBlock(
                    dim=embed_dim,
                    num_heads=num_heads,
                    mlp_ratio=mlp_ratio,
                    norm_layer=norm_layer,
                    quant_config=quant_config,
                    prefix=f"{prefix}.blocks.{layer_idx}",
                )
                for layer_idx in range(depth)
            ]
        )

        assert hidden_size == embed_dim, (
            "vit's config.hidden must be equal to config.embed_dim"
        )
        self.ln = nn.LayerNorm(hidden_size, eps=1e-6)

        self.attn_backend = get_vit_attn_backend(
            head_size=head_dim,
            dtype=torch.get_default_dtype(),
        )

    @property
    def dtype(self) -> torch.dtype:
        return self.patch_embed.proj.weight.dtype

    @property
    def device(self) -> torch.device:
        return self.patch_embed.proj.weight.device

    def rot_pos_emb(self, grid_thw: torch.Tensor) -> torch.Tensor:
        pos_ids = []
        for t, h, w in grid_thw:
            hpos_ids = torch.arange(h).unsqueeze(1).expand(-1, w)
            wpos_ids = torch.arange(w).unsqueeze(0).expand(h, -1)
            hpos_ids = (
                hpos_ids.reshape(
                    h // self.spatial_merge_size,
                    self.spatial_merge_size,
                    w // self.spatial_merge_size,
                    self.spatial_merge_size,
                )
                .permute(0, 2, 1, 3)
                .flatten()
            )
            wpos_ids = (
                wpos_ids.reshape(
                    h // self.spatial_merge_size,
                    self.spatial_merge_size,
                    w // self.spatial_merge_size,
                    self.spatial_merge_size,
                )
                .permute(0, 2, 1, 3)
                .flatten()
            )
            pos_ids.append(torch.stack([hpos_ids, wpos_ids], dim=-1).repeat(t, 1))
        pos_ids = torch.cat(pos_ids, dim=0)
        max_grid_size = grid_thw[:, 1:].max()
        rotary_pos_emb_full = self.rotary_pos_emb(max_grid_size)
        # `pos_ids` is built on the host; stage it over non-blocking so the
        # gather below doesn't index a device tensor with a CPU one.
        pos_ids = pos_ids.to(rotary_pos_emb_full.device, non_blocking=True)
        rotary_pos_emb = rotary_pos_emb_full[pos_ids].flatten(1)
        return rotary_pos_emb

    def compute_attn_mask_seqlen(self, cu_seqlens: torch.Tensor) -> torch.Tensor | None:
        max_seqlen = None
        if self.attn_backend in {
            AttentionBackendEnum.FLASH_ATTN,
            AttentionBackendEnum.ROCM_AITER_FA,
            AttentionBackendEnum.TRITON_ATTN,
        }:
            max_seqlen = (cu_seqlens[1:] - cu_seqlens[:-1]).max()
        return max_seqlen

    def prepare_encoder_metadata(
        self,
        grid_thw: torch.Tensor | list[list[int]],
        *,
        num_pad: int = 0,
        max_batch_size: int | None = None,
        max_seqlen_override: int | None = None,
        device: torch.device | None = None,
    ) -> dict[str, torch.Tensor]:
        """Compute encoder metadata outside the CUDA graph.

        Splits the rotary embeddings, ``cu_seqlens`` and ``max_seqlen`` out of
        `forward` so they can be precomputed on the host side and fed into a
        captured graph through fixed buffers. Shared by the eager path, CUDA
        graph capture and CUDA graph replay

        Args:
            grid_thw: Per-frame grid sizes as a ``[num_frames, 3]`` tensor or
                an equivalent list of ``[t, h, w]``.
            num_pad: Legacy ``cu_seqlens`` padding
            max_batch_size: If set, pad ``cu_seqlens`` to ``max_batch_size + 1``
                entries for a fixed graph buffer shape.
            max_seqlen_override: If set, use this value for ``max_seqlen``
                instead of deriving it from ``cu_seqlens``. CUDA graph capture
                bakes ``max_seqlen`` at capture time, so it must cover the
                worst-case replay.
            device: Device for the returned tensors. Defaults to ``self.device``.

        Returns:
            Dict with ``rotary_pos_emb``, ``cu_seqlens`` and ``max_seqlen``.
        """
        if device is None:
            device = self.device
        if not isinstance(grid_thw, torch.Tensor):
            grid_thw = torch.tensor(grid_thw, dtype=torch.int32)

        rotary_pos_emb = self.rot_pos_emb(grid_thw).to(device)

        cu_seqlens = torch.repeat_interleave(
            grid_thw[:, 1] * grid_thw[:, 2], grid_thw[:, 0]
        ).cumsum(dim=0, dtype=torch.int32)

        zeros = cu_seqlens.new_zeros(1)
        if num_pad > 0:
            cu_seqlens = torch.cat([zeros, cu_seqlens, zeros])
            cu_seqlens[-1] = cu_seqlens[-2] + num_pad
        else:
            cu_seqlens = torch.cat([zeros, cu_seqlens])

        # Pad cu_seqlens to a fixed number of sequences so the CUDA graph
        # replay buffer keeps a constant shape across batches.
        if max_batch_size is not None:
            num_seqs = cu_seqlens.numel() - 1
            if num_seqs < max_batch_size:
                cu_seqlens = torch.cat(
                    [
                        cu_seqlens,
                        cu_seqlens.new_full(
                            (max_batch_size - num_seqs,), cu_seqlens[-1]
                        ),
                    ]
                )

        # max_seqlen is baked into the graph at capture time, so capture passes
        # a worst-case override. compute_attn_mask_seqlen keeps it on CPU
        # (consumed via .item()), avoiding a captured D2H copy.
        if max_seqlen_override is not None:
            max_seqlen = torch.tensor(max_seqlen_override, dtype=torch.int32)
        else:
            max_seqlen = self.compute_attn_mask_seqlen(cu_seqlens)

        cu_seqlens = cu_seqlens.to(device, non_blocking=True)

        return {
            "rotary_pos_emb": rotary_pos_emb,
            "cu_seqlens": cu_seqlens,
            "max_seqlen": max_seqlen,
        }

    def forward(
        self,
        hidden_states: torch.Tensor,
        grid_thw: torch.Tensor | None = None,
        num_pad: int = 0,
        *,
        encoder_metadata: dict[str, torch.Tensor] | None = None,
    ) -> torch.Tensor:
        hidden_states = self.patch_embed(hidden_states)

        if encoder_metadata is None:
            # Eager path: compute metadata inline
            encoder_metadata = self.prepare_encoder_metadata(
                grid_thw, num_pad=num_pad, device=hidden_states.device
            )
        rotary_pos_emb = encoder_metadata["rotary_pos_emb"]
        cu_seqlens = encoder_metadata["cu_seqlens"]
        max_seqlen = encoder_metadata["max_seqlen"]

        # add batch size
        if hidden_states.ndim == 2:
            hidden_states = hidden_states.unsqueeze(dim=1)

        for blk in self.blocks:
            hidden_states = blk(
                hidden_states,
                cu_seqlens=cu_seqlens,
                rotary_pos_emb=rotary_pos_emb,
                max_seqlen=max_seqlen,
            )

        final_output = self.ln(hidden_states)

        if final_output.ndim == 3:
            final_output = final_output.squeeze(dim=1)

        return final_output

    def load_weights(self, weights) -> set[str]:
        params_dict = dict(self.named_parameters(remove_duplicate=False))
        loaded_params: set[str] = set()

        for name, loaded_weight in weights:
            param = params_dict[name]
            weight_loader = getattr(param, "weight_loader", default_weight_loader)
            weight_loader(param, loaded_weight)
            loaded_params.add(name)
        return loaded_params

prepare_encoder_metadata(grid_thw, *, num_pad=0, max_batch_size=None, max_seqlen_override=None, device=None)

Compute encoder metadata outside the CUDA graph.

Splits the rotary embeddings, cu_seqlens and max_seqlen out of forward so they can be precomputed on the host side and fed into a captured graph through fixed buffers. Shared by the eager path, CUDA graph capture and CUDA graph replay

Parameters:

  • grid_thw

    (Tensor | list[list[int]]) –

    Per-frame grid sizes as a [num_frames, 3] tensor or an equivalent list of [t, h, w].

  • num_pad

    (int, default: 0 ) –

    Legacy cu_seqlens padding

  • max_batch_size

    (int | None, default: None ) –

    If set, pad cu_seqlens to max_batch_size + 1 entries for a fixed graph buffer shape.

  • max_seqlen_override

    (int | None, default: None ) –

    If set, use this value for max_seqlen instead of deriving it from cu_seqlens. CUDA graph capture bakes max_seqlen at capture time, so it must cover the worst-case replay.

  • device

    (device | None, default: None ) –

    Device for the returned tensors. Defaults to self.device.

Returns:

  • dict[str, Tensor]

    Dict with rotary_pos_emb, cu_seqlens and max_seqlen.

Source code in vllm/model_executor/models/ernie45_vl.py
def prepare_encoder_metadata(
    self,
    grid_thw: torch.Tensor | list[list[int]],
    *,
    num_pad: int = 0,
    max_batch_size: int | None = None,
    max_seqlen_override: int | None = None,
    device: torch.device | None = None,
) -> dict[str, torch.Tensor]:
    """Compute encoder metadata outside the CUDA graph.

    Splits the rotary embeddings, ``cu_seqlens`` and ``max_seqlen`` out of
    `forward` so they can be precomputed on the host side and fed into a
    captured graph through fixed buffers. Shared by the eager path, CUDA
    graph capture and CUDA graph replay

    Args:
        grid_thw: Per-frame grid sizes as a ``[num_frames, 3]`` tensor or
            an equivalent list of ``[t, h, w]``.
        num_pad: Legacy ``cu_seqlens`` padding
        max_batch_size: If set, pad ``cu_seqlens`` to ``max_batch_size + 1``
            entries for a fixed graph buffer shape.
        max_seqlen_override: If set, use this value for ``max_seqlen``
            instead of deriving it from ``cu_seqlens``. CUDA graph capture
            bakes ``max_seqlen`` at capture time, so it must cover the
            worst-case replay.
        device: Device for the returned tensors. Defaults to ``self.device``.

    Returns:
        Dict with ``rotary_pos_emb``, ``cu_seqlens`` and ``max_seqlen``.
    """
    if device is None:
        device = self.device
    if not isinstance(grid_thw, torch.Tensor):
        grid_thw = torch.tensor(grid_thw, dtype=torch.int32)

    rotary_pos_emb = self.rot_pos_emb(grid_thw).to(device)

    cu_seqlens = torch.repeat_interleave(
        grid_thw[:, 1] * grid_thw[:, 2], grid_thw[:, 0]
    ).cumsum(dim=0, dtype=torch.int32)

    zeros = cu_seqlens.new_zeros(1)
    if num_pad > 0:
        cu_seqlens = torch.cat([zeros, cu_seqlens, zeros])
        cu_seqlens[-1] = cu_seqlens[-2] + num_pad
    else:
        cu_seqlens = torch.cat([zeros, cu_seqlens])

    # Pad cu_seqlens to a fixed number of sequences so the CUDA graph
    # replay buffer keeps a constant shape across batches.
    if max_batch_size is not None:
        num_seqs = cu_seqlens.numel() - 1
        if num_seqs < max_batch_size:
            cu_seqlens = torch.cat(
                [
                    cu_seqlens,
                    cu_seqlens.new_full(
                        (max_batch_size - num_seqs,), cu_seqlens[-1]
                    ),
                ]
            )

    # max_seqlen is baked into the graph at capture time, so capture passes
    # a worst-case override. compute_attn_mask_seqlen keeps it on CPU
    # (consumed via .item()), avoiding a captured D2H copy.
    if max_seqlen_override is not None:
        max_seqlen = torch.tensor(max_seqlen_override, dtype=torch.int32)
    else:
        max_seqlen = self.compute_attn_mask_seqlen(cu_seqlens)

    cu_seqlens = cu_seqlens.to(device, non_blocking=True)

    return {
        "rotary_pos_emb": rotary_pos_emb,
        "cu_seqlens": cu_seqlens,
        "max_seqlen": max_seqlen,
    }

all_gather_interleave(local_tensor, hidden_size, tp_size)

All-gather the input tensor interleavely across model parallel group.

Source code in vllm/model_executor/models/ernie45_vl.py
def all_gather_interleave(local_tensor, hidden_size: int, tp_size: int):
    """All-gather the input tensor interleavely across model parallel group."""
    import torch.distributed as dist

    gathered_tensors = [torch.zeros_like(local_tensor) for _ in range(tp_size)]
    dist.all_gather(
        gathered_tensors, local_tensor, group=parallel_state.get_tp_group().device_group
    )

    gathered_tensors_split = [
        torch.split(tensor, hidden_size // tp_size, -1) for tensor in gathered_tensors
    ]
    ordered_tensors = [
        tensor for pair in zip(*gathered_tensors_split) for tensor in pair
    ]
    result_tensor = torch.cat(ordered_tensors, dim=-1)
    return result_tensor