Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b92c2128dd | ||
|
|
c2b99e69ce |
@@ -10,6 +10,7 @@ def transformers_v5_compat():
|
||||
Qwen3VLMoeTextConfig.tie_word_embeddings = False
|
||||
|
||||
_patch_qwen35_lora()
|
||||
monkey_patch_dp_engine_core_pause_resume_deadlock()
|
||||
|
||||
|
||||
def _patch_qwen35_lora():
|
||||
@@ -712,3 +713,35 @@ def monkey_patch_fused_moe_lora_dp():
|
||||
self.base_layer._replace_quant_method(new_method)
|
||||
|
||||
FusedMoEWithLoRA._inject_lora_into_fused_moe = _fixed_inject
|
||||
|
||||
|
||||
def monkey_patch_dp_engine_core_pause_resume_deadlock():
|
||||
"""Fix deadlock with pause/resume and collective_rpc in DP engine core.
|
||||
|
||||
When a request arrives for an already-completed wave while the scheduler is
|
||||
paused, the unpatched code sends a start_wave notification that triggers
|
||||
collective_rpc on other DP engines. But the paused engine can't participate
|
||||
in the collective, causing a deadlock.
|
||||
|
||||
Fix: only send the start_wave notification when the scheduler is unpaused,
|
||||
and explicitly set engines_running=True before notifying.
|
||||
|
||||
Upstream: https://github.com/vllm-project/vllm/pull/37024
|
||||
"""
|
||||
from vllm.v1.core.sched.interface import PauseState
|
||||
from vllm.v1.engine import EngineCoreOutputs
|
||||
from vllm.v1.engine.core import DPEngineCoreProc, EngineCore
|
||||
from vllm.v1.request import Request
|
||||
|
||||
_base_add_request = EngineCore.add_request
|
||||
|
||||
def _patched_add_request(self, request: Request, request_wave: int = 0):
|
||||
_base_add_request(self, request, request_wave)
|
||||
if self.has_coordinator and request_wave != self.current_wave:
|
||||
if request_wave > self.current_wave:
|
||||
self.current_wave = request_wave
|
||||
elif not self.engines_running and self.scheduler.pause_state == PauseState.UNPAUSED:
|
||||
self.engines_running = True
|
||||
self.output_queue.put_nowait((-1, EngineCoreOutputs(start_wave=self.current_wave)))
|
||||
|
||||
DPEngineCoreProc.add_request = _patched_add_request
|
||||
|
||||
@@ -41,7 +41,7 @@ from prime_rl.trainer.weights import (
|
||||
)
|
||||
from prime_rl.trainer.world import get_world
|
||||
from prime_rl.utils.logger import get_logger
|
||||
from prime_rl.utils.vlm import get_language_model, get_vision_encoder
|
||||
from prime_rl.utils.vlm import get_language_model, get_vision_encoder, is_vlm_architecture
|
||||
|
||||
|
||||
def _patch_qwen3_5_moe_conversion_mapping():
|
||||
@@ -193,8 +193,7 @@ def get_model(
|
||||
f"Loading model config (name={config.name}, attn={config.attn}, trust_remote_code={config.trust_remote_code})"
|
||||
)
|
||||
|
||||
# VLM mode is enabled by setting [model.vlm] in config
|
||||
is_vlm = config.vlm is not None
|
||||
is_vlm_training = config.vlm is not None
|
||||
|
||||
if "Qwen3.5" in config.name or "qwen3_5" in config.name.lower():
|
||||
_patch_qwen3_5_text_position_ids()
|
||||
@@ -207,8 +206,9 @@ def get_model(
|
||||
),
|
||||
)
|
||||
model_config.use_cache = False
|
||||
is_vlm_arch = is_vlm_architecture(model_config)
|
||||
|
||||
if is_vlm:
|
||||
if is_vlm_training:
|
||||
logger.info(f"Detected vision-language model: {config.name}")
|
||||
if config.optimization_dtype != "bfloat16" or config.reduce_dtype != "bfloat16":
|
||||
raise ValueError(
|
||||
@@ -261,9 +261,9 @@ def get_model(
|
||||
target_config.num_hidden_layers = num_hidden_layers
|
||||
|
||||
# Determine the implementation to use
|
||||
custom_vlm_cls = get_custom_vlm_cls(model_config) if is_vlm else None
|
||||
custom_vlm_cls = get_custom_vlm_cls(model_config) if is_vlm_arch else None
|
||||
if config.impl == "auto":
|
||||
if is_vlm:
|
||||
if is_vlm_arch:
|
||||
impl_to_use = "custom" if custom_vlm_cls is not None else "hf"
|
||||
else:
|
||||
impl_to_use = "custom" if supports_custom_impl(model_config) else "hf"
|
||||
@@ -272,13 +272,12 @@ def get_model(
|
||||
impl_to_use = config.impl
|
||||
|
||||
with device:
|
||||
if is_vlm:
|
||||
if impl_to_use == "custom" and custom_vlm_cls is not None:
|
||||
model_cls = custom_vlm_cls
|
||||
else:
|
||||
from transformers import AutoModelForImageTextToText
|
||||
if impl_to_use == "custom" and custom_vlm_cls is not None:
|
||||
model_cls = custom_vlm_cls
|
||||
elif is_vlm_arch:
|
||||
from transformers import AutoModelForImageTextToText
|
||||
|
||||
model_cls = AutoModelForImageTextToText
|
||||
model_cls = AutoModelForImageTextToText
|
||||
else:
|
||||
match impl_to_use:
|
||||
case "hf":
|
||||
@@ -288,7 +287,7 @@ def get_model(
|
||||
|
||||
load_model_start_time = time.perf_counter()
|
||||
# HF VLM models require torch_dtype; custom PrimeRL models and text Auto models use dtype
|
||||
use_torch_dtype = is_vlm and model_cls is not custom_vlm_cls
|
||||
use_torch_dtype = is_vlm_arch and model_cls is not custom_vlm_cls
|
||||
dtype_kwarg = {"torch_dtype": dtype} if use_torch_dtype else {"dtype": dtype}
|
||||
if device == torch.device("meta"):
|
||||
logger.info(f"Loading model {config.name} using {model_cls.__name__} to meta device")
|
||||
@@ -303,8 +302,8 @@ def get_model(
|
||||
)
|
||||
logger.debug(f"Loaded model {config.name} in {time.perf_counter() - load_model_start_time:.2f} seconds")
|
||||
|
||||
# For VLM models, freeze the vision encoder
|
||||
if is_vlm:
|
||||
# For VLM training, freeze the vision encoder
|
||||
if is_vlm_training:
|
||||
freeze_vision_encoder(model, override_attr=config.vlm.vision_encoder_attr)
|
||||
|
||||
assert model.lm_head.weight.dtype == dtype, (
|
||||
@@ -343,15 +342,15 @@ def setup_fsdp(model: nn.Module, config: ModelConfig, parallel_dims: ParallelDim
|
||||
|
||||
dp_mod_ep_mesh = parallel_dims.world_mesh[tuple(dp_mod_ep_mesh_dim_names)]
|
||||
|
||||
is_vlm = config.vlm is not None
|
||||
if is_vlm:
|
||||
is_vlm_training = config.vlm is not None
|
||||
if is_vlm_training:
|
||||
vision_encoder = get_vision_encoder(model, override=config.vlm.vision_encoder_attr)
|
||||
if vision_encoder is None:
|
||||
raise ValueError(f"VLM model {config.name} has no recognized vision encoder")
|
||||
fully_shard(vision_encoder, mesh=hsdp_mesh, **fsdp_config)
|
||||
get_logger().info("Applied FSDP to frozen vision encoder")
|
||||
|
||||
language_model = get_language_model(model, override=config.vlm.language_model_attr if is_vlm else None)
|
||||
language_model = get_language_model(model, override=config.vlm.language_model_attr if is_vlm_training else None)
|
||||
transformer_layers = language_model.layers
|
||||
|
||||
for transformer_block in transformer_layers:
|
||||
|
||||
@@ -80,6 +80,11 @@ def get_language_model(model: nn.Module, override: str | None = None) -> nn.Modu
|
||||
return model.model
|
||||
|
||||
|
||||
def is_vlm_architecture(model_config: PretrainedConfig) -> bool:
|
||||
"""Check if the model config belongs to a known VLM architecture."""
|
||||
return _get_model_info_from_config(model_config) is not None
|
||||
|
||||
|
||||
def get_layer_prefix(model_config: PretrainedConfig, override: str | None = None) -> str:
|
||||
"""Return the weight key prefix for language model layers.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user