Repository navigation
[Model] Add tower and connector LoRA support for LFM2-VL #51498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
|
||
| 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
|
|
@@ -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( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be changed to ReplicatedLinear as well?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. I changed it in 195e139. The layer is |
||
| 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( | ||
|
|
@@ -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 | ||
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.