PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
# Copyright (c) 2026 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import torch
import torch.nn.functional as F
from transformers.models.qwen3_5_moe.modeling_qwen3_5_moe import Qwen3_5MoeMLP
from transformers.utils.versions import require_version
from auto_round.modeling.fused_moe.fusion_spec import build_standard_moe_fusion_spec, register_moe_fusion_spec
from auto_round.modeling.fused_moe.replace_modules import ReplacementModuleBase
from auto_round.modeling.fused_moe.utils import (
_update_parameter,
build_forced_routing,
force_all_experts_routing_enabled,
grouped_or_sequential_moe_forward,
)
from auto_round.utils import clear_memory, unsupported_meta_device
require_version("transformers>=5.2.0")
class LinearQwen3_5MoeSparseMoeBlock(ReplacementModuleBase):
supports_gguf_fused_moe = True
def __init__(self, original, config):
super().__init__(original)
self.gate = original.gate
text_config = config.get_text_config()
self.shared_expert = original.shared_expert
with torch.device("meta"):
self.experts = SequentialQwen3_5MoeExperts(text_config, original.experts)
self.shared_expert_gate = original.shared_expert_gate
self.num_experts = text_config.num_experts
@classmethod
def original_module_class(cls) -> str:
return "Qwen3_5MoeSparseMoeBlock"
def _materialize_weights(self) -> None:
original = self._get_original_module()
self.experts._materialize_weights(original.experts)
clear_memory()
def experts_forward(self, hidden_states, top_k_index, top_k_weights):
return grouped_or_sequential_moe_forward(
hidden_states, top_k_index, top_k_weights, self.experts, self.num_experts
)
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
batch_size, sequence_length, hidden_dim = hidden_states.shape
hidden_states_reshaped = hidden_states.view(-1, hidden_dim)
shared_expert_output = self.shared_expert(hidden_states_reshaped)
router_logits, routing_weights, selected_experts = self.gate(hidden_states_reshaped)
if force_all_experts_routing_enabled():
routing_scores = (
torch.softmax(router_logits, dim=-1, dtype=torch.float)
if router_logits is not None
else torch.full(
(hidden_states_reshaped.size(0), self.num_experts),
1.0 / float(self.num_experts),
device=hidden_states_reshaped.device,
dtype=torch.float,
)
)
selected_experts, routing_weights = build_forced_routing(
module=self,
routing_scores=routing_scores,
top_k=routing_weights.size(-1),
num_experts=self.num_experts,
dtype=hidden_states_reshaped.dtype,
normalize=True,
)
expert_output = self.experts_forward(hidden_states_reshaped, selected_experts, routing_weights)
shared_expert_output = F.sigmoid(self.shared_expert_gate(hidden_states_reshaped)) * shared_expert_output
expert_output += shared_expert_output
return expert_output.reshape(batch_size, sequence_length, hidden_dim)
@classmethod
def from_original(cls, original, config, **kwargs):
return cls(original, config)
class SequentialQwen3_5MoeExperts(torch.nn.ModuleList):
def __init__(self, config, original):
super().__init__()
self.num_experts = original.gate_up_proj.shape[0]
intermediate_size = config.moe_intermediate_size
with torch.device("meta"):
super().__init__([Qwen3_5MoeMLP(config, intermediate_size) for _ in range(self.num_experts)])
# The grouped experts forward applies gating at the container level, so it needs the
# activation here (all experts share the same stateless act_fn). Referencing the first
# expert's keeps it consistent with the per-expert MLPs.
# Store via ``object.__setattr__`` so the activation is NOT registered as a child module
# of this ``ModuleList``; otherwise it would appear as an extra expert (e.g. index 256)
# in iteration/len, breaking per-expert traversal, materialization and export.
object.__setattr__(self, "act_fn", self[0].act_fn)
register_moe_fusion_spec(
self,
build_standard_moe_fusion_spec(
detected_projections={
"gate_up_proj": {"split_into": ["gate_proj", "up_proj"]},
"down_proj": {},
},
num_experts=self.num_experts,
checkpoint_transposed=False,
module=original,
),
)
def _materialize_weights(self, original) -> None:
intermediate_size = original.down_proj.shape[-1]
if not unsupported_meta_device(original):
for expert_idx in range(self.num_experts):
gate_up = original.gate_up_proj[expert_idx]
down = original.down_proj[expert_idx]
gate_proj = gate_up[:intermediate_size, :]
up_proj = gate_up[intermediate_size:, :]
_update_parameter(self[expert_idx].gate_proj, "weight", gate_proj.contiguous())
_update_parameter(self[expert_idx].up_proj, "weight", up_proj.contiguous())
_update_parameter(self[expert_idx].down_proj, "weight", down.contiguous())
del gate_up, down, gate_proj, up_proj
original.to_empty(device="meta")
clear_memory()