PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
Fix MoE for llama/gpt-oss (#1557)
Yi Liu555052e7759d
diff --git a/auto_round/modeling/fused_moe/gpt_oss.py b/auto_round/modeling/fused_moe/gpt_oss.pyindex 998d2dbda..959e01356 100644--- a/auto_round/modeling/fused_moe/gpt_oss.py+++ b/auto_round/modeling/fused_moe/gpt_oss.py@@ -114,16 +114,19 @@ class SequentialGPTOSSMoE(ReplacementModuleBase):mask_weights = mask_weights[:num_all_tokens, :total_num_experts]mask_weights = mask_weights.transpose(0, 1)- experts_mask = experts_mask[:num_all_tokens, :total_num_experts]- experts_mask = experts_mask.transpose(0, 1)- num_experts = total_num_experts- for expert_index in range(num_experts):- mask_weight = mask_weights[expert_index].unsqueeze(1)++ # Only process experts that actually received tokens (expert_hit pattern),+ # skipping experts with zero routing weight to save compute during calibration.+ with torch.no_grad():+ expert_hit = torch.greater(mask_weights.sum(dim=-1), 0).nonzero()+ for expert_idx in expert_hit:+ expert_idx = expert_idx[0]+ mask_weight = mask_weights[expert_idx].unsqueeze(1)current_state_static = x * mask_weight- expert = self.experts[expert_index]- expert_output = expert(current_state_static)- expert_output = expert_output * experts_mask[expert_index].unsqueeze(1)+ expert_output = self.experts[expert_idx](current_state_static)+ expert_output = expert_output * experts_mask[expert_idx].unsqueeze(1)final_hidden_states += expert_output+return final_hidden_states.view(B, T, H), router_scores.view(B * T, -1)@classmethoddiff --git a/auto_round/modeling/fused_moe/llama4.py b/auto_round/modeling/fused_moe/llama4.pyindex 863db80f7..488acdaac 100644--- a/auto_round/modeling/fused_moe/llama4.py+++ b/auto_round/modeling/fused_moe/llama4.py@@ -82,8 +82,14 @@ class SequentialLlama4TextMoe(ReplacementModuleBase):router_scores = torch.sigmoid(router_scores.float()).to(hidden_states.dtype)out = self.shared_expert(hidden_states)- for i in range(self.num_experts):- out += self.experts[i](hidden_states) * router_scores[i].reshape(-1, 1)++ # Only process experts that actually received tokens (expert_hit pattern),+ # skipping experts with zero routing weight to save compute during calibration.+ with torch.no_grad():+ expert_hit = torch.greater(router_scores.sum(dim=-1), 0).nonzero()+ for expert_idx in expert_hit:+ expert_idx = expert_idx[0]+ out += self.experts[expert_idx](hidden_states) * router_scores[expert_idx].reshape(-1, 1)return out, router_logits