|
|
|
@@ -453,3 +453,262 @@ def monkey_patch_minimax_m2_for_lora():
|
|
|
|
|
".up_proj.": ".w3.",
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def monkey_patch_harmony_stop_token_propagation():
|
|
|
|
|
"""Fix: vLLM 0.17.0 doesn't merge harmony stop tokens into per-request SamplingParams.
|
|
|
|
|
|
|
|
|
|
The harmony mode sets stop_token_ids (including <|call|> and <|return|>) in
|
|
|
|
|
default_sampling_params at server init, but ChatCompletionRequest.to_sampling_params()
|
|
|
|
|
ignores them, using only self.stop_token_ids (which defaults to []).
|
|
|
|
|
|
|
|
|
|
Upstream: https://github.com/vllm-project/vllm/issues/22519
|
|
|
|
|
"""
|
|
|
|
|
from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest
|
|
|
|
|
|
|
|
|
|
_original_to_sampling_params = ChatCompletionRequest.to_sampling_params
|
|
|
|
|
|
|
|
|
|
def _patched_to_sampling_params(self, max_tokens, default_sampling_params):
|
|
|
|
|
params = _original_to_sampling_params(self, max_tokens, default_sampling_params)
|
|
|
|
|
# Merge harmony stop tokens from default_sampling_params
|
|
|
|
|
default_stop_ids = default_sampling_params.get("stop_token_ids", [])
|
|
|
|
|
if default_stop_ids:
|
|
|
|
|
existing = set(params.stop_token_ids or [])
|
|
|
|
|
merged = list(existing | set(default_stop_ids))
|
|
|
|
|
params.stop_token_ids = merged
|
|
|
|
|
return params
|
|
|
|
|
|
|
|
|
|
ChatCompletionRequest.to_sampling_params = _patched_to_sampling_params
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def monkey_patch_fused_moe_lora_dp():
|
|
|
|
|
"""Fix: LoRA + MoE + DP>1 produces corrupted output in vLLM 0.17.0.
|
|
|
|
|
|
|
|
|
|
Two bugs:
|
|
|
|
|
1. LoRA injection sets supports_internal_mk=True (via moe_kernel not None),
|
|
|
|
|
causing the MoE runner to skip DP dispatch/combine. But the LoRA kernel
|
|
|
|
|
uses NoDPEP and doesn't handle DP internally.
|
|
|
|
|
2. LoRA decorators capture full-batch tensors but fire per-chunk inside the
|
|
|
|
|
kernel's chunk loop. At DP>=3, dispatched batch > CHUNK_SIZE causes
|
|
|
|
|
shape mismatches.
|
|
|
|
|
|
|
|
|
|
Fix: Replace _inject_lora_into_fused_moe with a version that:
|
|
|
|
|
(a) sets moe_kernel=None so the runner correctly dispatches
|
|
|
|
|
(b) makes decorators chunk-aware by tracking chunk offsets
|
|
|
|
|
|
|
|
|
|
Upstream: https://github.com/vllm-project/vllm/issues/23244
|
|
|
|
|
"""
|
|
|
|
|
import types
|
|
|
|
|
|
|
|
|
|
from vllm import envs
|
|
|
|
|
from vllm.distributed.utils import divide
|
|
|
|
|
from vllm.lora.layers.fused_moe import FusedMoEWithLoRA
|
|
|
|
|
from vllm.model_executor.layers.fused_moe.config import _get_config_dtype_str
|
|
|
|
|
from vllm.model_executor.layers.fused_moe.fused_marlin_moe import MarlinExperts
|
|
|
|
|
from vllm.model_executor.layers.fused_moe.fused_moe import TritonExperts
|
|
|
|
|
from vllm.model_executor.layers.fused_moe.fused_moe_modular_method import FusedMoEModularMethod
|
|
|
|
|
from vllm.model_executor.layers.fused_moe.gpt_oss_triton_kernels_moe import UnfusedOAITritonExperts
|
|
|
|
|
from vllm.model_executor.layers.fused_moe.modular_kernel import FusedMoEKernel
|
|
|
|
|
from vllm.model_executor.layers.fused_moe.prepare_finalize import MoEPrepareAndFinalizeNoDPEPModular
|
|
|
|
|
|
|
|
|
|
def _fixed_inject(self):
|
|
|
|
|
moe_state_dict = {}
|
|
|
|
|
top_k = self.base_layer.top_k
|
|
|
|
|
|
|
|
|
|
self.base_layer.ensure_moe_quant_config_init()
|
|
|
|
|
quant_config = self.base_layer.quant_method.moe_quant_config
|
|
|
|
|
|
|
|
|
|
if getattr(self.base_layer.quant_method, "supports_internal_mk", False):
|
|
|
|
|
m_fused_moe_fn = self.base_layer.quant_method.moe_kernel
|
|
|
|
|
m_fused_moe_fn.shared_experts = None
|
|
|
|
|
else:
|
|
|
|
|
prepare_finalize = MoEPrepareAndFinalizeNoDPEPModular()
|
|
|
|
|
m_fused_moe_fn = FusedMoEKernel(
|
|
|
|
|
prepare_finalize,
|
|
|
|
|
self.base_layer.quant_method.select_gemm_impl(prepare_finalize, self.base_layer),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if quant_config.use_mxfp4_w4a16:
|
|
|
|
|
assert isinstance(m_fused_moe_fn.impl.fused_experts, (MarlinExperts, UnfusedOAITritonExperts))
|
|
|
|
|
else:
|
|
|
|
|
assert isinstance(m_fused_moe_fn.impl.fused_experts, TritonExperts)
|
|
|
|
|
|
|
|
|
|
# --- Decorators (chunk-aware) ---
|
|
|
|
|
|
|
|
|
|
def fwd_decorator(layer, func):
|
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
|
moe_state_dict["hidden_states"] = kwargs["hidden_states"]
|
|
|
|
|
moe_state_dict["topk_ids"] = kwargs["topk_ids"]
|
|
|
|
|
moe_state_dict["topk_weights"] = kwargs["topk_weights"]
|
|
|
|
|
moe_state_dict["expert_map"] = kwargs["expert_map"]
|
|
|
|
|
moe_state_dict["apply_router_weight_on_input"] = kwargs["apply_router_weight_on_input"]
|
|
|
|
|
moe_state_dict["_chunk_offset"] = 0
|
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
def act_decorator(layer, func):
|
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
|
_, output, input = args
|
|
|
|
|
chunk_M = input.view(-1, top_k, input.shape[-1]).shape[0]
|
|
|
|
|
chunk_offset = moe_state_dict.get("_chunk_offset", 0)
|
|
|
|
|
hidden_states = moe_state_dict["hidden_states"][chunk_offset : chunk_offset + chunk_M]
|
|
|
|
|
topk_weights = moe_state_dict["topk_weights"][chunk_offset : chunk_offset + chunk_M]
|
|
|
|
|
curr_topk_ids = moe_state_dict["topk_ids"][chunk_offset : chunk_offset + chunk_M]
|
|
|
|
|
expert_map = moe_state_dict["expert_map"]
|
|
|
|
|
config_dtype = _get_config_dtype_str(
|
|
|
|
|
dtype=hidden_states.dtype, use_fp8_w8a8=False, use_int8_w8a16=False, use_int4_w4a16=False
|
|
|
|
|
)
|
|
|
|
|
num_tokens = hidden_states.size(0)
|
|
|
|
|
M = min(num_tokens, envs.VLLM_FUSED_MOE_CHUNK_SIZE)
|
|
|
|
|
max_lora_rank = self.w13_lora_a_stacked[0].shape[-2]
|
|
|
|
|
shrink_config, expand_config = self._get_lora_moe_configs(
|
|
|
|
|
op_prefix="w13",
|
|
|
|
|
num_loras=self.max_loras,
|
|
|
|
|
rank=max_lora_rank,
|
|
|
|
|
num_slices=self._w13_slices,
|
|
|
|
|
M=M,
|
|
|
|
|
layer=layer,
|
|
|
|
|
top_k=top_k,
|
|
|
|
|
config_dtype=config_dtype,
|
|
|
|
|
)
|
|
|
|
|
SPARSITY_FACTOR = 8
|
|
|
|
|
naive_block_assignment = (
|
|
|
|
|
expert_map is None
|
|
|
|
|
and num_tokens * top_k * SPARSITY_FACTOR <= self.base_layer.local_num_experts * self.max_loras
|
|
|
|
|
)
|
|
|
|
|
token_lora_mapping, sorted_token_ids_lora, expert_ids_lora, num_tokens_post_padded_lora = (
|
|
|
|
|
self.punica_wrapper.moe_lora_align_block_size(
|
|
|
|
|
curr_topk_ids,
|
|
|
|
|
num_tokens,
|
|
|
|
|
shrink_config["BLOCK_SIZE_M"],
|
|
|
|
|
self.base_layer.local_num_experts,
|
|
|
|
|
self.max_loras,
|
|
|
|
|
self.adapter_enabled,
|
|
|
|
|
expert_map,
|
|
|
|
|
naive_block_assignment=naive_block_assignment,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
moe_state_dict["sorted_token_ids_lora"] = sorted_token_ids_lora
|
|
|
|
|
moe_state_dict["expert_ids_lora"] = expert_ids_lora
|
|
|
|
|
moe_state_dict["num_tokens_post_padded_lora"] = num_tokens_post_padded_lora
|
|
|
|
|
moe_state_dict["token_lora_mapping"] = token_lora_mapping
|
|
|
|
|
if sorted_token_ids_lora is not None:
|
|
|
|
|
expert_ids_lora = expert_ids_lora.view(self.max_loras, -1)
|
|
|
|
|
sorted_token_ids_lora = sorted_token_ids_lora.view(self.max_loras, -1)
|
|
|
|
|
self.punica_wrapper.add_lora_fused_moe(
|
|
|
|
|
input.view(-1, top_k, input.shape[-1]),
|
|
|
|
|
hidden_states,
|
|
|
|
|
self.w13_lora_a_stacked,
|
|
|
|
|
self.w13_lora_b_stacked,
|
|
|
|
|
topk_weights,
|
|
|
|
|
sorted_token_ids_lora,
|
|
|
|
|
expert_ids_lora,
|
|
|
|
|
num_tokens_post_padded_lora,
|
|
|
|
|
max_lora_rank,
|
|
|
|
|
top_k,
|
|
|
|
|
shrink_config,
|
|
|
|
|
expand_config,
|
|
|
|
|
self.adapter_enabled,
|
|
|
|
|
fully_sharded=self.fully_sharded,
|
|
|
|
|
token_lora_mapping=token_lora_mapping,
|
|
|
|
|
)
|
|
|
|
|
result = func(*args, **kwargs)
|
|
|
|
|
moe_state_dict["intermediate_cache2"] = output
|
|
|
|
|
moe_state_dict["_chunk_M"] = chunk_M
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
def moe_sum_decorator(layer, func):
|
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
|
chunk_offset = moe_state_dict.get("_chunk_offset", 0)
|
|
|
|
|
chunk_M = moe_state_dict.get("_chunk_M", moe_state_dict["hidden_states"].size(0))
|
|
|
|
|
hidden_states = moe_state_dict["hidden_states"][chunk_offset : chunk_offset + chunk_M]
|
|
|
|
|
topk_weights = moe_state_dict["topk_weights"][chunk_offset : chunk_offset + chunk_M]
|
|
|
|
|
config_dtype = _get_config_dtype_str(
|
|
|
|
|
dtype=hidden_states.dtype, use_fp8_w8a8=False, use_int8_w8a16=False, use_int4_w4a16=False
|
|
|
|
|
)
|
|
|
|
|
num_tokens = hidden_states.size(0)
|
|
|
|
|
M = min(num_tokens, envs.VLLM_FUSED_MOE_CHUNK_SIZE)
|
|
|
|
|
max_lora_rank = self.w2_lora_a_stacked[0].shape[-2]
|
|
|
|
|
shrink_config, expand_config = self._get_lora_moe_configs(
|
|
|
|
|
op_prefix="w2",
|
|
|
|
|
num_loras=self.max_loras,
|
|
|
|
|
rank=max_lora_rank,
|
|
|
|
|
num_slices=1,
|
|
|
|
|
M=M,
|
|
|
|
|
layer=layer,
|
|
|
|
|
top_k=top_k,
|
|
|
|
|
config_dtype=config_dtype,
|
|
|
|
|
)
|
|
|
|
|
sorted_token_ids_lora = moe_state_dict["sorted_token_ids_lora"]
|
|
|
|
|
expert_ids_lora = moe_state_dict["expert_ids_lora"]
|
|
|
|
|
num_tokens_post_padded_lora = moe_state_dict["num_tokens_post_padded_lora"]
|
|
|
|
|
token_lora_mapping = moe_state_dict.get("token_lora_mapping")
|
|
|
|
|
if sorted_token_ids_lora is not None:
|
|
|
|
|
expert_ids_lora = expert_ids_lora.view(self.max_loras, -1)
|
|
|
|
|
sorted_token_ids_lora = sorted_token_ids_lora.view(self.max_loras, -1)
|
|
|
|
|
intermediate_cache2 = moe_state_dict["intermediate_cache2"]
|
|
|
|
|
intermediate_cache3 = args[0]
|
|
|
|
|
shard_size_w2 = divide(self.base_layer.hidden_size, self.tp_size)
|
|
|
|
|
self.punica_wrapper.add_lora_fused_moe(
|
|
|
|
|
intermediate_cache3,
|
|
|
|
|
intermediate_cache2,
|
|
|
|
|
self.w2_lora_a_stacked,
|
|
|
|
|
self.w2_lora_b_stacked,
|
|
|
|
|
topk_weights,
|
|
|
|
|
sorted_token_ids_lora,
|
|
|
|
|
expert_ids_lora,
|
|
|
|
|
num_tokens_post_padded_lora,
|
|
|
|
|
max_lora_rank,
|
|
|
|
|
top_k,
|
|
|
|
|
shrink_config,
|
|
|
|
|
expand_config,
|
|
|
|
|
self.adapter_enabled,
|
|
|
|
|
True,
|
|
|
|
|
fully_sharded=self.fully_sharded,
|
|
|
|
|
offset=shard_size_w2 * self.tp_rank if self.fully_sharded else 0,
|
|
|
|
|
token_lora_mapping=token_lora_mapping,
|
|
|
|
|
)
|
|
|
|
|
result = func(*args, **kwargs)
|
|
|
|
|
moe_state_dict["_chunk_offset"] = chunk_offset + chunk_M
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
# --- Install decorators and replace quant method ---
|
|
|
|
|
|
|
|
|
|
fused_experts = m_fused_moe_fn.impl.fused_experts
|
|
|
|
|
m_fused_moe_fn.apply = fwd_decorator(self.base_layer, m_fused_moe_fn.apply)
|
|
|
|
|
fused_experts.activation = act_decorator(self.base_layer, fused_experts.activation)
|
|
|
|
|
fused_experts.moe_sum = moe_sum_decorator(self.base_layer, fused_experts.moe_sum)
|
|
|
|
|
|
|
|
|
|
new_method = FusedMoEModularMethod(self.base_layer.quant_method, m_fused_moe_fn)
|
|
|
|
|
|
|
|
|
|
# Bug 1 fix: NoDPEP kernel makes supports_internal_mk=True, causing the
|
|
|
|
|
# runner to skip DP dispatch/combine. Set moe_kernel=None and patch apply().
|
|
|
|
|
if isinstance(m_fused_moe_fn.prepare_finalize, MoEPrepareAndFinalizeNoDPEPModular):
|
|
|
|
|
saved_kernel = new_method.moe_kernel
|
|
|
|
|
saved_disable_expert_map = new_method.disable_expert_map
|
|
|
|
|
new_method.moe_kernel = None
|
|
|
|
|
|
|
|
|
|
def _apply_with_saved_kernel(self, layer, x, topk_weights, topk_ids, shared_experts_input):
|
|
|
|
|
return saved_kernel.apply(
|
|
|
|
|
hidden_states=x,
|
|
|
|
|
w1=layer.w13_weight,
|
|
|
|
|
w2=layer.w2_weight,
|
|
|
|
|
topk_weights=topk_weights,
|
|
|
|
|
topk_ids=topk_ids,
|
|
|
|
|
activation=layer.activation,
|
|
|
|
|
global_num_experts=layer.global_num_experts,
|
|
|
|
|
apply_router_weight_on_input=layer.apply_router_weight_on_input,
|
|
|
|
|
expert_map=None if saved_disable_expert_map else layer.expert_map,
|
|
|
|
|
shared_experts_input=shared_experts_input,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
new_method.apply = types.MethodType(_apply_with_saved_kernel, new_method)
|
|
|
|
|
|
|
|
|
|
self.base_layer._replace_quant_method(new_method)
|
|
|
|
|
|
|
|
|
|
FusedMoEWithLoRA._inject_lora_into_fused_moe = _fixed_inject
|
|
|
|
|