fix: read input_scale from nn.Parameter before it's freed

This commit is contained in:
2026-05-16 22:23:24 +00:00
parent 152648789d
commit 139c9c37cd

View File

@@ -515,11 +515,12 @@ class DeepseekV4MegaMoEExperts(nn.Module):
# Set activation global scales from checkpoint input_scale
# The input_scale is the pre-computed activation normalization factor
# for each projection. Using hardcoded 1/2688 gives wrong scale for most layers.
# Read from the nn.Parameters BEFORE they're freed below.
# input_scale shape: (num_experts, 2) for w13 (gate, up), (num_experts, 1) for w2
l1_igs = self._w13_input_scale[:, 0] if self._w13_input_scale is not None else None # gate input_scale
l2_igs = self._w2_input_scale[:, 0] if self._w2_input_scale is not None else None # down input_scale
self._cutedsl_runner.l1_activation_global_scale = l1_igs.mean().item() if l1_igs is not None else 1.0 / 2688.0
self._cutedsl_runner.l2_activation_global_scale = l2_igs.mean().item() if l2_igs is not None else 1.0 / 2688.0
l1_igs = self.w13_input_scale.data[:, 0] # gate input_scale
l2_igs = self.w2_input_scale.data[:, 0] # down input_scale
self._cutedsl_runner.l1_activation_global_scale = l1_igs.mean().item()
self._cutedsl_runner.l2_activation_global_scale = l2_igs.mean().item()
# Drop the original loader-side parameters
self._w13_input_scale = self.w13_input_scale.data.clone()