From cf7a2ead54a51daf5a2dab24fcd93fddf6f18e8c Mon Sep 17 00:00:00 2001 From: Andreas Karatzas Date: Sun, 2 Aug 2026 05:56:00 +0000 Subject: [PATCH 1/5] [Bugfix][MoE] Fix fused block-scale orientation Signed-off-by: Andreas Karatzas --- .../moe/test_moe_weight_loading_padded.py | 83 ++++++++++++++++++- .../layers/fused_moe/routed_experts.py | 47 ++++++++++- 2 files changed, 125 insertions(+), 5 deletions(-) diff --git a/tests/kernels/moe/test_moe_weight_loading_padded.py b/tests/kernels/moe/test_moe_weight_loading_padded.py index bb377049d91e..0addc2ba0c30 100644 --- a/tests/kernels/moe/test_moe_weight_loading_padded.py +++ b/tests/kernels/moe/test_moe_weight_loading_padded.py @@ -106,9 +106,90 @@ def test_w2_per_channel_scale_is_untouched(self): def test_block_scale_is_untouched(self): # Block scales are [experts, 2 * intermediate / block, hidden / block] scale = torch.randn(8, 16, 24) - result = RoutedExperts._orient_fused_weight(scale, "w1", self.HIDDEN) + result = RoutedExperts._orient_fused_weight( + scale, + "w1", + self.HIDDEN, + target_shape=torch.Size((8, 16, 24)), + ) assert result.shape == (8, 16, 24) + def test_padded_target_does_not_guess_block_scale_orientation(self): + # Both orientations fit inside a target padded on both axes. Without + # quantization-layout metadata there is no sound way to choose one. + scale = torch.randn(8, 6, 16) + result = RoutedExperts._orient_fused_weight( + scale, + "w2", + unpadded_hidden=2048, + target_shape=torch.Size((8, 24, 24)), + ) + assert result.shape == (8, 6, 16) + + def test_square_target_does_not_guess_orientation(self): + scale = torch.arange(8 * 16 * 16).reshape(8, 16, 16) + result = RoutedExperts._orient_fused_weight( + scale, + "w1", + unpadded_hidden=2048, + target_shape=torch.Size((8, 16, 16)), + ) + assert result.data_ptr() == scale.data_ptr() + torch.testing.assert_close(result, scale) + + @pytest.mark.parametrize( + ("shard_id", "checkpoint_shape", "target_shape"), + [ + # Qwen3-VL stores block scales in checkpoint weight orientation. + ("w1", (8, 16, 12), (8, 12, 16)), + ("w2", (8, 6, 16), (8, 16, 6)), + ], + ) + def test_qwen3_vl_transposed_block_scale_matches_target( + self, + shard_id: str, + checkpoint_shape: tuple[int, ...], + target_shape: tuple[int, ...], + ): + scale = torch.arange(torch.tensor(checkpoint_shape).prod()).reshape( + checkpoint_shape + ) + result = RoutedExperts._orient_fused_weight( + scale, + shard_id, + unpadded_hidden=2048, + target_shape=torch.Size(target_shape), + ) + assert result.shape == target_shape + torch.testing.assert_close(result, scale.transpose(-1, -2)) + + @pytest.mark.parametrize( + ("shard_id", "checkpoint_shape", "target_shape"), + [ + ("w1", (8, 16, 12), (8, 6, 16)), + ("w2", (8, 6, 16), (8, 16, 3)), + ], + ) + def test_qwen3_vl_transposed_block_scale_matches_tp_shard( + self, + shard_id: str, + checkpoint_shape: tuple[int, ...], + target_shape: tuple[int, ...], + ): + scale = torch.arange(torch.tensor(checkpoint_shape).prod()).reshape( + checkpoint_shape + ) + result = RoutedExperts._orient_fused_weight( + scale, + shard_id, + unpadded_hidden=2048, + target_shape=torch.Size(target_shape), + tp_size=2, + ) + expected_shape = (8, 12, 16) if shard_id == "w1" else (8, 16, 6) + assert result.shape == expected_shape + torch.testing.assert_close(result, scale.transpose(-1, -2)) + class TestNarrowExpertDataForPadding: """Unit tests for _narrow_expert_data_for_padding.""" diff --git a/vllm/model_executor/layers/fused_moe/routed_experts.py b/vllm/model_executor/layers/fused_moe/routed_experts.py index 0cd0ad995078..bb17aceef0a1 100644 --- a/vllm/model_executor/layers/fused_moe/routed_experts.py +++ b/vllm/model_executor/layers/fused_moe/routed_experts.py @@ -423,14 +423,19 @@ def _get_hidden_dim(shard_dim: int, ndim: int) -> int: @staticmethod def _orient_fused_weight( - fused_weight: torch.Tensor, shard_id: str, unpadded_hidden: int + fused_weight: torch.Tensor, + shard_id: str, + unpadded_hidden: int, + target_shape: torch.Size | None = None, + tp_size: int = 1, ) -> torch.Tensor: """Normalise a fused expert tensor from either checkpoint orientation to (intermediate, hidden) for w1/w3 and (hidden, intermediate) for w2. Only transposes when the hidden dim is definitively on the wrong axis, - leaving tensors that have no hidden dim alone (e.g. a fused per-channel - scale, (2 * intermediate, 1)). + or when the transposed shape exactly matches the target parameter. The + latter resolves block scales, whose dimensions are both divided by the + block size and therefore do not contain the hidden size directly. """ if shard_id == "w2": hidden_axis, intermediate_axis = -2, -1 @@ -441,6 +446,24 @@ def _orient_fused_weight( and fused_weight.shape[intermediate_axis] == unpadded_hidden ): return fused_weight.transpose(-1, -2) + + if target_shape is not None and len(target_shape) >= 2: + # The caller only supplies canonical, TP-sharded block-scale + # targets. ``param`` is TP-local, whereas a fused checkpoint tensor + # contains the global intermediate dimension. Reconstruct that + # global shape: w1/w3 shard their first data axis and w2 its second. + expected_global_shape = list(target_shape[-2:]) + tp_shard_axis = 1 if shard_id == "w2" else 0 + expected_global_shape[tp_shard_axis] *= tp_size + checkpoint_shape = list(fused_weight.shape[-2:]) + # This is only a tie-breaker: require an exact reversed-shape + # match so padded, square, or otherwise ambiguous layouts are + # never guessed. + if ( + checkpoint_shape != expected_global_shape + and checkpoint_shape[::-1] == expected_global_shape + ): + return fused_weight.transpose(-1, -2) return fused_weight @staticmethod @@ -938,11 +961,27 @@ def load_weights( param_name = weight_name.removeprefix(f"{self.layer_name}.") param = getattr(self, param_name) if is_fused: + # PR #50137's hidden-axis heuristic cannot orient block + # scales because both dimensions are divided by the block + # size. Use the exact destination shape only for canonical, + # TP-sharded BLOCK parameters. Other quant formats may use + # transposed storage or replicated/full axes, for which + # shard_id alone does not describe the target layout. + can_match_block_target = ( + getattr(param, "quant_method", None) + == FusedMoeWeightScaleSupported.BLOCK.value + and not getattr(param, "is_transposed", False) + and not getattr(param, "load_full_w2", False) + ) # w1 and w3 share one fused tensor; use a local copy so the # transpose below doesn't mutate loaded_weight across # iterations (else w3 is transposed twice and wrongly chunked) fused_weight = self._orient_fused_weight( - loaded_weight, shard_id, unpadded_hidden + loaded_weight, + shard_id, + unpadded_hidden, + target_shape=param.shape if can_match_block_target else None, + tp_size=self.moe_config.tp_size, ) if shard_id in {"w1", "w3"}: # Repurpose expert_id for deconcatenating w1 and w3 From ce5ffa705ae957975e52282967692c0f0896c028 Mon Sep 17 00:00:00 2001 From: Andreas Karatzas Date: Tue, 4 Aug 2026 04:38:35 +0000 Subject: [PATCH 2/5] [MoE] Reuse intermediate axis for TP scaling Assisted-by: OpenAI Codex Signed-off-by: Andreas Karatzas --- vllm/model_executor/layers/fused_moe/routed_experts.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/vllm/model_executor/layers/fused_moe/routed_experts.py b/vllm/model_executor/layers/fused_moe/routed_experts.py index bb17aceef0a1..90d2f18a67bb 100644 --- a/vllm/model_executor/layers/fused_moe/routed_experts.py +++ b/vllm/model_executor/layers/fused_moe/routed_experts.py @@ -453,8 +453,7 @@ def _orient_fused_weight( # contains the global intermediate dimension. Reconstruct that # global shape: w1/w3 shard their first data axis and w2 its second. expected_global_shape = list(target_shape[-2:]) - tp_shard_axis = 1 if shard_id == "w2" else 0 - expected_global_shape[tp_shard_axis] *= tp_size + expected_global_shape[intermediate_axis] *= tp_size checkpoint_shape = list(fused_weight.shape[-2:]) # This is only a tie-breaker: require an exact reversed-shape # match so padded, square, or otherwise ambiguous layouts are From fe9d2e6140c7b278efd97861af0ba15aab18db29 Mon Sep 17 00:00:00 2001 From: Andreas Karatzas Date: Tue, 4 Aug 2026 04:48:55 +0000 Subject: [PATCH 3/5] [MoE] Clarify block-scale fallback flag Assisted-by: OpenAI Codex Signed-off-by: Andreas Karatzas --- vllm/model_executor/layers/fused_moe/routed_experts.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/vllm/model_executor/layers/fused_moe/routed_experts.py b/vllm/model_executor/layers/fused_moe/routed_experts.py index 90d2f18a67bb..361afb7c3773 100644 --- a/vllm/model_executor/layers/fused_moe/routed_experts.py +++ b/vllm/model_executor/layers/fused_moe/routed_experts.py @@ -966,7 +966,7 @@ def load_weights( # TP-sharded BLOCK parameters. Other quant formats may use # transposed storage or replicated/full axes, for which # shard_id alone does not describe the target layout. - can_match_block_target = ( + use_block_scale_shape_fallback = ( getattr(param, "quant_method", None) == FusedMoeWeightScaleSupported.BLOCK.value and not getattr(param, "is_transposed", False) @@ -979,7 +979,9 @@ def load_weights( loaded_weight, shard_id, unpadded_hidden, - target_shape=param.shape if can_match_block_target else None, + target_shape=( + param.shape if use_block_scale_shape_fallback else None + ), tp_size=self.moe_config.tp_size, ) if shard_id in {"w1", "w3"}: From 4bf6ca450b78310194bebdc81ace612aee6d268c Mon Sep 17 00:00:00 2001 From: Andreas Karatzas Date: Thu, 6 Aug 2026 17:18:38 +0000 Subject: [PATCH 4/5] Use explicit fused MoE checkpoint layout Co-authored-by: OpenAI Codex Signed-off-by: Andreas Karatzas --- .../moe/test_moe_weight_loading_padded.py | 87 +++---------------- vllm/model_executor/layers/fused_moe/layer.py | 4 + .../layers/fused_moe/routed_experts.py | 69 +++------------ vllm/model_executor/models/qwen3_moe.py | 13 ++- vllm/model_executor/models/qwen3_vl_moe.py | 17 ++++ 5 files changed, 57 insertions(+), 133 deletions(-) diff --git a/tests/kernels/moe/test_moe_weight_loading_padded.py b/tests/kernels/moe/test_moe_weight_loading_padded.py index 0addc2ba0c30..1c13af288e0b 100644 --- a/tests/kernels/moe/test_moe_weight_loading_padded.py +++ b/tests/kernels/moe/test_moe_weight_loading_padded.py @@ -71,123 +71,62 @@ class TestOrientFusedWeight: def test_w13_standard_orientation_is_untouched(self): weight = torch.randn(8, 2048, self.HIDDEN) - result = RoutedExperts._orient_fused_weight(weight, "w1", self.HIDDEN) + result = RoutedExperts._orient_fused_weight(weight, False) assert result.shape == (8, 2048, self.HIDDEN) def test_w13_transposed_checkpoint_is_normalised(self): # e.g. Qwen3 VL MoE stores [experts, hidden, 2 * intermediate] weight = torch.randn(8, self.HIDDEN, 2048) - result = RoutedExperts._orient_fused_weight(weight, "w3", self.HIDDEN) + result = RoutedExperts._orient_fused_weight(weight, True) assert result.shape == (8, 2048, self.HIDDEN) def test_w2_standard_orientation_is_untouched(self): weight = torch.randn(8, self.HIDDEN, 1024) - result = RoutedExperts._orient_fused_weight(weight, "w2", self.HIDDEN) + result = RoutedExperts._orient_fused_weight(weight, False) assert result.shape == (8, self.HIDDEN, 1024) def test_w2_transposed_checkpoint_is_normalised(self): weight = torch.randn(8, 1024, self.HIDDEN) - result = RoutedExperts._orient_fused_weight(weight, "w2", self.HIDDEN) + result = RoutedExperts._orient_fused_weight(weight, True) assert result.shape == (8, self.HIDDEN, 1024) def test_w13_per_channel_scale_is_untouched(self): # A fused per-channel scale has no hidden dim, so transposing it would # leave chunk()/TP sharding operating on the wrong axis. scale = torch.randn(8, 2048, 1) - result = RoutedExperts._orient_fused_weight(scale, "w1", self.HIDDEN) + result = RoutedExperts._orient_fused_weight(scale, False) assert result.shape == (8, 2048, 1) assert result.chunk(2, dim=1)[0].shape == (8, 1024, 1) def test_w2_per_channel_scale_is_untouched(self): scale = torch.randn(8, self.HIDDEN, 1) - result = RoutedExperts._orient_fused_weight(scale, "w2", self.HIDDEN) + result = RoutedExperts._orient_fused_weight(scale, False) assert result.shape == (8, self.HIDDEN, 1) def test_block_scale_is_untouched(self): # Block scales are [experts, 2 * intermediate / block, hidden / block] scale = torch.randn(8, 16, 24) - result = RoutedExperts._orient_fused_weight( - scale, - "w1", - self.HIDDEN, - target_shape=torch.Size((8, 16, 24)), - ) + result = RoutedExperts._orient_fused_weight(scale, False) assert result.shape == (8, 16, 24) - - def test_padded_target_does_not_guess_block_scale_orientation(self): - # Both orientations fit inside a target padded on both axes. Without - # quantization-layout metadata there is no sound way to choose one. - scale = torch.randn(8, 6, 16) - result = RoutedExperts._orient_fused_weight( - scale, - "w2", - unpadded_hidden=2048, - target_shape=torch.Size((8, 24, 24)), - ) - assert result.shape == (8, 6, 16) - - def test_square_target_does_not_guess_orientation(self): - scale = torch.arange(8 * 16 * 16).reshape(8, 16, 16) - result = RoutedExperts._orient_fused_weight( - scale, - "w1", - unpadded_hidden=2048, - target_shape=torch.Size((8, 16, 16)), - ) assert result.data_ptr() == scale.data_ptr() - torch.testing.assert_close(result, scale) @pytest.mark.parametrize( - ("shard_id", "checkpoint_shape", "target_shape"), + "checkpoint_shape", [ # Qwen3-VL stores block scales in checkpoint weight orientation. - ("w1", (8, 16, 12), (8, 12, 16)), - ("w2", (8, 6, 16), (8, 16, 6)), - ], - ) - def test_qwen3_vl_transposed_block_scale_matches_target( - self, - shard_id: str, - checkpoint_shape: tuple[int, ...], - target_shape: tuple[int, ...], - ): - scale = torch.arange(torch.tensor(checkpoint_shape).prod()).reshape( - checkpoint_shape - ) - result = RoutedExperts._orient_fused_weight( - scale, - shard_id, - unpadded_hidden=2048, - target_shape=torch.Size(target_shape), - ) - assert result.shape == target_shape - torch.testing.assert_close(result, scale.transpose(-1, -2)) - - @pytest.mark.parametrize( - ("shard_id", "checkpoint_shape", "target_shape"), - [ - ("w1", (8, 16, 12), (8, 6, 16)), - ("w2", (8, 6, 16), (8, 16, 3)), + (8, 16, 12), + (8, 6, 16), + (8, 16, 16), ], ) - def test_qwen3_vl_transposed_block_scale_matches_tp_shard( + def test_qwen3_vl_transposed_block_scale_uses_explicit_layout( self, - shard_id: str, checkpoint_shape: tuple[int, ...], - target_shape: tuple[int, ...], ): scale = torch.arange(torch.tensor(checkpoint_shape).prod()).reshape( checkpoint_shape ) - result = RoutedExperts._orient_fused_weight( - scale, - shard_id, - unpadded_hidden=2048, - target_shape=torch.Size(target_shape), - tp_size=2, - ) - expected_shape = (8, 12, 16) if shard_id == "w1" else (8, 16, 6) - assert result.shape == expected_shape + result = RoutedExperts._orient_fused_weight(scale, True) torch.testing.assert_close(result, scale.transpose(-1, -2)) diff --git a/vllm/model_executor/layers/fused_moe/layer.py b/vllm/model_executor/layers/fused_moe/layer.py index cfbadba7aced..28b994057fa1 100644 --- a/vllm/model_executor/layers/fused_moe/layer.py +++ b/vllm/model_executor/layers/fused_moe/layer.py @@ -130,6 +130,7 @@ def FusedMoEFactory( is_sequence_parallel: bool = False, reduce_results: bool = True, ckpt_names: tuple[str, str, str] = ("gate_proj", "down_proj", "up_proj"), + is_fused_checkpoint_transposed: bool = False, n_shared_experts: int | None = None, router_logits_dtype: torch.dtype | None = None, gate: torch.nn.Module | None = None, @@ -193,6 +194,8 @@ def FusedMoEFactory( the late-AR path. ckpt_names: Checkpoint parameter name tuple (gate_proj, down_proj, up_proj) used for weight loading + is_fused_checkpoint_transposed: Whether fused checkpoint weights and + block scales use transposed storage. n_shared_experts: Number of shared experts to fuse into the routed grouped GEMM (ROCm; requires aiter FSE or the router-append path) router_logits_dtype: Data type for router logits buffers @@ -378,6 +381,7 @@ def FusedMoEFactory( ckpt_gate_proj_name=ckpt_names[0], ckpt_down_proj_name=ckpt_names[1], ckpt_up_proj_name=ckpt_names[2], + is_fused_checkpoint_transposed=is_fused_checkpoint_transposed, # Extra params that are needed by quant_methods, pass along for now # Prefer getting these from other sources, e.g. moe_config or # router object diff --git a/vllm/model_executor/layers/fused_moe/routed_experts.py b/vllm/model_executor/layers/fused_moe/routed_experts.py index 361afb7c3773..a7b5c1fdb08e 100644 --- a/vllm/model_executor/layers/fused_moe/routed_experts.py +++ b/vllm/model_executor/layers/fused_moe/routed_experts.py @@ -61,6 +61,7 @@ def __init__( ckpt_gate_proj_name: str = "gate_proj", ckpt_down_proj_name: str = "down_proj", ckpt_up_proj_name: str = "up_proj", + is_fused_checkpoint_transposed: bool = False, # # Extra params that are needed by quant_methods, pass along for now # Prefer getting these from other sources, e.g. moe_config or @@ -86,6 +87,7 @@ def __init__( self.ckpt_gate_proj_name = ckpt_gate_proj_name self.ckpt_down_proj_name = ckpt_down_proj_name self.ckpt_up_proj_name = ckpt_up_proj_name + self.is_fused_checkpoint_transposed = is_fused_checkpoint_transposed self.expert_map_manager = expert_map_manager self.hidden_size = moe_config.hidden_dim self.global_num_experts = moe_config.num_experts @@ -424,45 +426,11 @@ def _get_hidden_dim(shard_dim: int, ndim: int) -> int: @staticmethod def _orient_fused_weight( fused_weight: torch.Tensor, - shard_id: str, - unpadded_hidden: int, - target_shape: torch.Size | None = None, - tp_size: int = 1, + is_fused_checkpoint_transposed: bool, ) -> torch.Tensor: - """Normalise a fused expert tensor from either checkpoint orientation - to (intermediate, hidden) for w1/w3 and (hidden, intermediate) for w2. - - Only transposes when the hidden dim is definitively on the wrong axis, - or when the transposed shape exactly matches the target parameter. The - latter resolves block scales, whose dimensions are both divided by the - block size and therefore do not contain the hidden size directly. - """ - if shard_id == "w2": - hidden_axis, intermediate_axis = -2, -1 - else: - hidden_axis, intermediate_axis = -1, -2 - if ( - fused_weight.shape[hidden_axis] != unpadded_hidden - and fused_weight.shape[intermediate_axis] == unpadded_hidden - ): + """Normalise a fused expert tensor to the vLLM weight layout.""" + if is_fused_checkpoint_transposed: return fused_weight.transpose(-1, -2) - - if target_shape is not None and len(target_shape) >= 2: - # The caller only supplies canonical, TP-sharded block-scale - # targets. ``param`` is TP-local, whereas a fused checkpoint tensor - # contains the global intermediate dimension. Reconstruct that - # global shape: w1/w3 shard their first data axis and w2 its second. - expected_global_shape = list(target_shape[-2:]) - expected_global_shape[intermediate_axis] *= tp_size - checkpoint_shape = list(fused_weight.shape[-2:]) - # This is only a tie-breaker: require an exact reversed-shape - # match so padded, square, or otherwise ambiguous layouts are - # never guessed. - if ( - checkpoint_shape != expected_global_shape - and checkpoint_shape[::-1] == expected_global_shape - ): - return fused_weight.transpose(-1, -2) return fused_weight @staticmethod @@ -942,9 +910,6 @@ def load_weights( self, weights: Iterable[tuple[str, torch.Tensor]] ) -> Iterable[str]: expert_mapping = self.get_expert_mapping(include_fused=True) - unpadded_hidden = ( - self.moe_config.hidden_dim_unpadded or self.moe_config.hidden_dim - ) for expert_name, loaded_weight in weights: qual_name = f"{self.layer_name}.{expert_name}" # Fused expert weights can be identified by their 3D tensors @@ -960,29 +925,19 @@ def load_weights( param_name = weight_name.removeprefix(f"{self.layer_name}.") param = getattr(self, param_name) if is_fused: - # PR #50137's hidden-axis heuristic cannot orient block - # scales because both dimensions are divided by the block - # size. Use the exact destination shape only for canonical, - # TP-sharded BLOCK parameters. Other quant formats may use - # transposed storage or replicated/full axes, for which - # shard_id alone does not describe the target layout. - use_block_scale_shape_fallback = ( - getattr(param, "quant_method", None) - == FusedMoeWeightScaleSupported.BLOCK.value - and not getattr(param, "is_transposed", False) - and not getattr(param, "load_full_w2", False) + quant_method = getattr(param, "quant_method", None) + # Block scales share the weight's two-dimensional layout. + # Other quantization metadata can use independent layouts. + uses_weight_layout = ( + "scale" not in weight_name + or quant_method == FusedMoeWeightScaleSupported.BLOCK.value ) # w1 and w3 share one fused tensor; use a local copy so the # transpose below doesn't mutate loaded_weight across # iterations (else w3 is transposed twice and wrongly chunked) fused_weight = self._orient_fused_weight( loaded_weight, - shard_id, - unpadded_hidden, - target_shape=( - param.shape if use_block_scale_shape_fallback else None - ), - tp_size=self.moe_config.tp_size, + self.is_fused_checkpoint_transposed and uses_weight_layout, ) if shard_id in {"w1", "w3"}: # Repurpose expert_id for deconcatenating w1 and w3 diff --git a/vllm/model_executor/models/qwen3_moe.py b/vllm/model_executor/models/qwen3_moe.py index 2c29f9cc616e..01da3308a3ab 100644 --- a/vllm/model_executor/models/qwen3_moe.py +++ b/vllm/model_executor/models/qwen3_moe.py @@ -132,6 +132,7 @@ def __init__( self, vllm_config: VllmConfig, prefix: str = "", + is_fused_checkpoint_transposed: bool = False, ): super().__init__() @@ -214,6 +215,7 @@ def __init__( enable_eplb=self.enable_eplb, num_redundant_experts=self.n_redundant_experts, is_sequence_parallel=self.is_sequence_parallel, + is_fused_checkpoint_transposed=is_fused_checkpoint_transposed, ) def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: @@ -355,7 +357,12 @@ def forward( class Qwen3MoeDecoderLayer(nn.Module): - def __init__(self, vllm_config: VllmConfig, prefix: str = "") -> None: + def __init__( + self, + vllm_config: VllmConfig, + prefix: str = "", + is_fused_checkpoint_transposed: bool = False, + ) -> None: super().__init__() config = vllm_config.model_config.hf_text_config @@ -391,7 +398,9 @@ def __init__(self, vllm_config: VllmConfig, prefix: str = "") -> None: config.num_experts > 0 and (layer_idx + 1) % config.decoder_sparse_step == 0 ): self.mlp = Qwen3MoeSparseMoeBlock( - vllm_config=vllm_config, prefix=f"{prefix}.mlp" + vllm_config=vllm_config, + prefix=f"{prefix}.mlp", + is_fused_checkpoint_transposed=is_fused_checkpoint_transposed, ) else: self.mlp = Qwen3MoeMLP( diff --git a/vllm/model_executor/models/qwen3_vl_moe.py b/vllm/model_executor/models/qwen3_vl_moe.py index f4a36f289796..f7cf2407d393 100644 --- a/vllm/model_executor/models/qwen3_vl_moe.py +++ b/vllm/model_executor/models/qwen3_vl_moe.py @@ -43,6 +43,7 @@ from .interfaces import MixtureOfExperts from .qwen3_moe import ( + Qwen3MoeDecoderLayer, Qwen3MoeForCausalLM, Qwen3MoeModel, Qwen3MoeSparseMoeBlock, @@ -64,6 +65,15 @@ def get_hf_config(self): return self.ctx.get_hf_config(Qwen3VLMoeConfig) +class Qwen3VLMoeDecoderLayer(Qwen3MoeDecoderLayer): + def __init__(self, vllm_config: VllmConfig, prefix: str = "") -> None: + super().__init__( + vllm_config=vllm_config, + prefix=prefix, + is_fused_checkpoint_transposed=True, + ) + + @support_torch_compile( dynamic_arg_dims={ "input_ids": 0, @@ -77,6 +87,13 @@ def get_hf_config(self): } ) class Qwen3MoeLLMModel(Qwen3MoeModel): + def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""): + super().__init__( + vllm_config=vllm_config, + prefix=prefix, + decoder_layer_type=Qwen3VLMoeDecoderLayer, + ) + def forward( self, input_ids: torch.Tensor | None, From fc82db14f0686d5709965f868c82d21f5b6b3e5b Mon Sep 17 00:00:00 2001 From: Andreas Karatzas Date: Fri, 7 Aug 2026 19:31:17 +0000 Subject: [PATCH 5/5] Fix Qwen3-VL decoder layer override compatibility Co-authored-by: OpenAI Codex Signed-off-by: Andreas Karatzas --- vllm/model_executor/models/qwen3_vl_moe.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/vllm/model_executor/models/qwen3_vl_moe.py b/vllm/model_executor/models/qwen3_vl_moe.py index f7cf2407d393..1360edfdd7b3 100644 --- a/vllm/model_executor/models/qwen3_vl_moe.py +++ b/vllm/model_executor/models/qwen3_vl_moe.py @@ -87,11 +87,17 @@ def __init__(self, vllm_config: VllmConfig, prefix: str = "") -> None: } ) class Qwen3MoeLLMModel(Qwen3MoeModel): - def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""): + def __init__( + self, + *, + vllm_config: VllmConfig, + prefix: str = "", + decoder_layer_type: type[torch.nn.Module] = Qwen3VLMoeDecoderLayer, + ): super().__init__( vllm_config=vllm_config, prefix=prefix, - decoder_layer_type=Qwen3VLMoeDecoderLayer, + decoder_layer_type=decoder_layer_type, ) def forward(