[Qwen3 VL] Add LoRA support for Qwen 3 VL (#12165)

This commit is contained in:
Jonah Bernard
2025-11-03 23:32:54 -05:00
committed by GitHub
parent 48d6bea1ea
commit a209fb05c1
4 changed files with 259 additions and 0 deletions

View File

@@ -14,6 +14,7 @@
# ==============================================================================
"""Inference-only Qwen3-VL model compatible with HuggingFace weights."""
import logging
import re
from functools import lru_cache, partial
from typing import Callable, Iterable, List, Optional, Tuple, Union
@@ -691,6 +692,13 @@ class Qwen3VLForConditionalGeneration(nn.Module):
def get_input_embeddings(self):
return self.model.embed_tokens
_lora_pattern = re.compile(
r"^model\.layers\.(\d+)\.(?:self_attn|mlp)\.(?:qkv_proj|o_proj|down_proj|gate_up_proj)$"
)
def should_apply_lora(self, module_name: str) -> bool:
return bool(self._lora_pattern.match(module_name))
def forward(
self,
input_ids: torch.Tensor,

View File

@@ -14,6 +14,7 @@
# ==============================================================================
"""Inference-only Qwen3-VL model compatible with HuggingFace weights."""
import logging
import re
from functools import lru_cache
from typing import Iterable, Optional, Tuple, Union
@@ -167,6 +168,14 @@ class Qwen3VLMoeForConditionalGeneration(Qwen3VLForConditionalGeneration):
):
super().__init__(config, quant_config, prefix, language_model_cls)
# Only allow LoRA on attention projections within text layers for MoE.
_lora_pattern_moe = re.compile(
r"^model\.layers\.(\d+)\.self_attn\.(?:qkv_proj|o_proj)$"
)
def should_apply_lora(self, module_name: str) -> bool:
return bool(self._lora_pattern_moe.match(module_name))
def load_weights(self, weights: Iterable[Tuple[str, torch.Tensor]]):
stacked_params_mapping = [
# (param_name, shard_name, shard_id)