|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +import torch |
| 6 | +import torch.nn.functional as F |
| 7 | + |
| 8 | +from llmcompressor.modeling.moe_context import MoECalibrationModule |
| 9 | +from llmcompressor.utils.dev import skip_weights_initialize |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from transformers.models.qwen3_5_moe.modeling_qwen3_5_moe import ( |
| 13 | + Qwen3_5MoeSparseMoeBlock, |
| 14 | + ) |
| 15 | + |
| 16 | + |
| 17 | +@MoECalibrationModule.register("Qwen3_5MoeSparseMoeBlock") |
| 18 | +class CalibrationQwen3_5MoeSparseMoeBlock(MoECalibrationModule): |
| 19 | + """ |
| 20 | + Calibration version of Qwen3_5MoeSparseMoeBlock that unfuses 3D expert |
| 21 | + parameters into individual MLP modules (nn.Linear) so they can be |
| 22 | + individually quantized. Sends all tokens to all experts during calibration. |
| 23 | +
|
| 24 | + is_permanent = True because the unfused structure must persist for |
| 25 | + quantization to target the individual nn.Linear expert weights. |
| 26 | + """ |
| 27 | + |
| 28 | + is_permanent = True |
| 29 | + |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + original: Qwen3_5MoeSparseMoeBlock, |
| 33 | + config, |
| 34 | + calibrate_all_experts: bool = True, |
| 35 | + ): |
| 36 | + super().__init__() |
| 37 | + text_config = getattr(config, "text_config", config) |
| 38 | + |
| 39 | + self.num_experts = text_config.num_experts |
| 40 | + self.top_k = text_config.num_experts_per_tok |
| 41 | + self.hidden_size = text_config.hidden_size |
| 42 | + |
| 43 | + self.calibrate_all_experts = calibrate_all_experts |
| 44 | + self.gate = original.gate |
| 45 | + self.shared_expert = original.shared_expert |
| 46 | + self.shared_expert_gate = original.shared_expert_gate |
| 47 | + self.experts = SequentialQwen3_5MoeExperts(text_config, original.experts) |
| 48 | + |
| 49 | + def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: |
| 50 | + batch_size, sequence_length, hidden_dim = hidden_states.shape |
| 51 | + hidden_states_reshaped = hidden_states.view(-1, hidden_dim) |
| 52 | + |
| 53 | + # router: returns (router_logits, router_scores, router_indices) |
| 54 | + _, routing_weights, selected_experts = self.gate(hidden_states_reshaped) |
| 55 | + |
| 56 | + # expert mask: (num_experts, top_k, num_tokens) |
| 57 | + expert_mask = F.one_hot( |
| 58 | + selected_experts, num_classes=self.num_experts |
| 59 | + ).permute(2, 1, 0) |
| 60 | + |
| 61 | + final_hidden_states = torch.zeros( |
| 62 | + (batch_size * sequence_length, hidden_dim), |
| 63 | + dtype=hidden_states.dtype, |
| 64 | + device=hidden_states.device, |
| 65 | + ) |
| 66 | + |
| 67 | + for expert_idx, expert_layer in enumerate(self.experts): |
| 68 | + idx, token_idx = torch.where(expert_mask[expert_idx].squeeze(0)) |
| 69 | + |
| 70 | + if self.calibrate_all_experts: |
| 71 | + expert_out = expert_layer(hidden_states_reshaped)[token_idx] |
| 72 | + else: |
| 73 | + expert_out = expert_layer(hidden_states_reshaped[token_idx]) |
| 74 | + |
| 75 | + if len(token_idx) > 0: |
| 76 | + current_hidden_states = ( |
| 77 | + expert_out * routing_weights[token_idx, idx, None] |
| 78 | + ) |
| 79 | + final_hidden_states.index_add_( |
| 80 | + 0, |
| 81 | + token_idx, |
| 82 | + current_hidden_states.to(hidden_states.dtype), |
| 83 | + ) |
| 84 | + |
| 85 | + # shared expert |
| 86 | + shared_expert_output = self.shared_expert(hidden_states_reshaped) |
| 87 | + shared_expert_output = ( |
| 88 | + F.sigmoid(self.shared_expert_gate(hidden_states_reshaped)) |
| 89 | + * shared_expert_output |
| 90 | + ) |
| 91 | + final_hidden_states = final_hidden_states + shared_expert_output |
| 92 | + |
| 93 | + final_hidden_states = final_hidden_states.reshape( |
| 94 | + batch_size, sequence_length, hidden_dim |
| 95 | + ) |
| 96 | + return final_hidden_states |
| 97 | + |
| 98 | + def restore(self, original: torch.nn.Module) -> torch.nn.Module: |
| 99 | + return self |
| 100 | + |
| 101 | + |
| 102 | +class SequentialQwen3_5MoeExperts(torch.nn.ModuleList): |
| 103 | + """ |
| 104 | + Unfuses 3D expert parameter tensors into individual Qwen3_5MoeMLP modules |
| 105 | + so that each expert's weights are nn.Linear and can be targeted by |
| 106 | + quantization with targets="Linear". |
| 107 | + """ |
| 108 | + |
| 109 | + def __init__(self, config, original): |
| 110 | + from compressed_tensors.offload import disable_onloading |
| 111 | + from transformers.models.qwen3_5_moe.modeling_qwen3_5_moe import ( |
| 112 | + Qwen3_5MoeMLP, |
| 113 | + ) |
| 114 | + |
| 115 | + self.num_experts = config.num_experts |
| 116 | + intermediate_size = config.moe_intermediate_size |
| 117 | + |
| 118 | + with skip_weights_initialize(): |
| 119 | + super().__init__( |
| 120 | + [ |
| 121 | + Qwen3_5MoeMLP(config, intermediate_size=intermediate_size) |
| 122 | + for _ in range(self.num_experts) |
| 123 | + ] |
| 124 | + ) |
| 125 | + |
| 126 | + # Access expert weights on CPU to avoid GPU OOM. |
| 127 | + # disable_onloading() makes OffloadCache return the offloaded (CPU) |
| 128 | + # values directly instead of onloading to GPU. |
| 129 | + with disable_onloading(): |
| 130 | + gate_up_data = original.gate_up_proj.data # [num_experts, 2*inter, hidden] |
| 131 | + down_data = original.down_proj.data # [num_experts, hidden, inter] |
| 132 | + |
| 133 | + for i in range(self.num_experts): |
| 134 | + gate_up = gate_up_data[i] # [2*intermediate, hidden] |
| 135 | + down = down_data[i] # [hidden, intermediate] |
| 136 | + |
| 137 | + # gate_up_proj stores [gate; up] stacked along dim 0 |
| 138 | + # nn.Linear weight is [out_features, in_features] |
| 139 | + self[i].gate_proj.weight.data = ( |
| 140 | + gate_up[:intermediate_size, :].clone().contiguous() |
| 141 | + ) |
| 142 | + self[i].up_proj.weight.data = ( |
| 143 | + gate_up[intermediate_size:, :].clone().contiguous() |
| 144 | + ) |
| 145 | + self[i].down_proj.weight.data = down.clone().contiguous() |
0 commit comments