Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions tests/models/multimodal/processing/test_lfm2_vl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this test?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the entire newly added test file in 195e139. This drops the LFM2-specific token-count round-trip, zero-token, and projector LoRA-wrapping assertions. The existing GPU model runner test still covers generic tower/connector mapping construction, but it mocks the helper results and does not cover those LFM2-specific details.


from types import SimpleNamespace

import pytest

from vllm.config.lora import LoRAConfig
from vllm.lora.layers import ReplicatedLinearWithLoRA
from vllm.model_executor.models.lfm2_vl import (
Lfm2VLForConditionalGeneration,
Lfm2VLMultiModalProjector,
)

from ...utils import build_model_context

get_num_mm_encoder_tokens = Lfm2VLForConditionalGeneration.get_num_mm_encoder_tokens
get_num_mm_connector_tokens = Lfm2VLForConditionalGeneration.get_num_mm_connector_tokens


@pytest.mark.parametrize("model_id", ["LiquidAI/LFM2-VL-450M"])
def test_num_mm_tokens_match_real_config(
model_id,
monkeypatch: pytest.MonkeyPatch,
):
ctx = build_model_context(model_id, limit_mm_per_prompt={"image": 1})
config = ctx.model_config.hf_config
stub = SimpleNamespace(config=config)
factor = config.downsample_factor
monkeypatch.setattr(
"vllm.model_executor.layers.linear.get_tensor_model_parallel_rank",
lambda: 0,
)
monkeypatch.setattr(
"vllm.model_executor.layers.linear.get_tensor_model_parallel_world_size",
lambda: 1,
)
monkeypatch.setattr(
"vllm.model_executor.parameter.get_tensor_model_parallel_rank",
lambda: 0,
)
monkeypatch.setattr(
"vllm.model_executor.parameter.get_tensor_model_parallel_world_size",
lambda: 1,
)
projector = Lfm2VLMultiModalProjector(config)
lora_config = LoRAConfig(max_loras=1, max_lora_rank=8)

for layer in (projector.linear_1, projector.linear_2):
assert ReplicatedLinearWithLoRA.can_replace_layer(
source_layer=layer,
lora_config=lora_config,
packed_modules_list=[],
model_config=config,
)

for num_image_tokens in (1, 17, 256, 1024):
encoder_tokens = get_num_mm_encoder_tokens(stub, num_image_tokens)
assert encoder_tokens == num_image_tokens * factor**2

connector_tokens = get_num_mm_connector_tokens(stub, encoder_tokens)
assert connector_tokens == num_image_tokens


@pytest.mark.parametrize(
("downsample_factor", "num_image_tokens"),
[
(1, 1),
(2, 17),
(3, 256),
(4, 1024),
],
)
def test_num_mm_tokens_roundtrip(downsample_factor, num_image_tokens):
stub = SimpleNamespace(config=SimpleNamespace(downsample_factor=downsample_factor))

encoder_tokens = get_num_mm_encoder_tokens(stub, num_image_tokens)
assert encoder_tokens == num_image_tokens * downsample_factor**2

connector_tokens = get_num_mm_connector_tokens(stub, encoder_tokens)
assert connector_tokens == num_image_tokens


def test_num_mm_tokens_zero():
stub = SimpleNamespace(config=SimpleNamespace(downsample_factor=2))

assert get_num_mm_encoder_tokens(stub, 0) == 0
assert get_num_mm_connector_tokens(stub, 0) == 0
19 changes: 17 additions & 2 deletions vllm/model_executor/models/lfm2_vl.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from vllm.config.multimodal import BaseDummyOptions
from vllm.forward_context import set_forward_context
from vllm.inputs import MultiModalDataDict
from vllm.model_executor.layers.linear import ReplicatedLinear
from vllm.model_executor.layers.mamba.mamba_utils import (
MambaStateCopyFunc,
MambaStateCopyFuncCalculator,
Expand Down Expand Up @@ -499,16 +500,20 @@ def __init__(
self.projector_use_layernorm = config.projector_use_layernorm
if self.projector_use_layernorm:
self.layer_norm = nn.LayerNorm(in_channels)
self.linear_1 = nn.Linear(
self.linear_1 = ReplicatedLinear(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be changed to ReplicatedLinear as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I changed it in 195e139. The layer is vision_tower.vision_model.embeddings.patch_embedding; as an nn.Linear, it was not discovered by get_supported_lora_modules() and could not be wrapped by ReplicatedLinearWithLoRA. I changed only this layer to ReplicatedLinear(..., return_bias=False), matching the existing Siglip2 NAFlex/Isaac pattern. The bias and state-dict names, tensor-only forward contract, weight loading, and fully replicated TP semantics are preserved.

in_channels,
config.projector_hidden_size,
bias=config.projector_bias,
prefix=maybe_prefix(prefix, "linear_1"),
return_bias=False,
)
self.act = ACT2FN[config.projector_hidden_act]
self.linear_2 = nn.Linear(
self.linear_2 = ReplicatedLinear(
config.projector_hidden_size,
config.text_config.hidden_size,
bias=config.projector_bias,
prefix=maybe_prefix(prefix, "linear_2"),
return_bias=False,
)

def forward(
Expand Down Expand Up @@ -1259,3 +1264,13 @@ def get_mm_mapping(self) -> MultiModelKeys:
connector="multi_modal_projector",
tower_model="vision_tower",
)

def get_num_mm_encoder_tokens(self, num_image_tokens: int) -> int:
downsample_factor = self.config.downsample_factor

return num_image_tokens * downsample_factor**2

def get_num_mm_connector_tokens(self, num_vision_tokens: int) -> int:
downsample_factor = self.config.downsample_factor

return num_vision_tokens // downsample_factor**2
Loading