[Refactor] Leave decoder stacks through final_norm(skip_empty=True) (#41751)

This commit is contained in:
Cheng Wan
2026-09-29 21:08:19 -07:00
committed by GitHub
parent 01b5f572b7
commit ea855f0daf
13 changed files with 50 additions and 89 deletions
+3 -6
View File
@@ -581,12 +581,9 @@ class GigaChat35Model(nn.Module):
if not self.pp_group.is_last_rank:
return residual_batch.to_pp(hidden_states, forward_batch)
hidden_states = residual_batch.complete_output(hidden_states, forward_batch)
if hidden_states.shape[0] != 0:
hidden_states = residual_batch.final_norm(
hidden_states, forward_batch, self.norm
)
hidden_states = residual_batch.final_norm(
hidden_states, forward_batch, self.norm, skip_empty=True
)
return hidden_states
+3 -6
View File
@@ -791,12 +791,9 @@ class InternS2MobiusForCausalLM(Qwen3_5ForCausalLM):
input_deepstack_embeds[:, start : start + self.hidden_size],
)
hidden_states = residual_batch.complete_output(hidden_states, forward_batch)
if hidden_states.shape[0] != 0:
hidden_states = residual_batch.final_norm(
hidden_states, forward_batch, self.norm
)
hidden_states = residual_batch.final_norm(
hidden_states, forward_batch, self.norm, skip_empty=True
)
return (
hidden_states
if not aux_hidden_states
+9 -11
View File
@@ -607,17 +607,15 @@ class LagunaModel(nn.Module):
if not self.pp_group.is_last_rank:
return residual_batch.to_pp(hidden_states, forward_batch)
hidden_states = residual_batch.complete_output(hidden_states, forward_batch)
if hidden_states.shape[0] != 0:
hidden_states = residual_batch.final_norm(
hidden_states,
forward_batch,
self.norm,
capture=aux_hidden_states.append
if self.end_layer in self.layers_to_capture
else None,
)
hidden_states = residual_batch.final_norm(
hidden_states,
forward_batch,
self.norm,
capture=aux_hidden_states.append
if self.end_layer in self.layers_to_capture
else None,
skip_empty=True,
)
if len(aux_hidden_states) == 0:
return hidden_states
return hidden_states, aux_hidden_states
+3 -6
View File
@@ -1177,12 +1177,9 @@ class MiniMaxM2Model(nn.Module):
if not self.pp_group.is_last_rank:
return residual_batch.to_pp(hidden_states, forward_batch)
hidden_states = residual_batch.complete_output(hidden_states, forward_batch)
if hidden_states.shape[0] != 0:
hidden_states = residual_batch.final_norm(
hidden_states, forward_batch, self.norm
)
hidden_states = residual_batch.final_norm(
hidden_states, forward_batch, self.norm, skip_empty=True
)
if len(aux_hidden_states) == 0:
return hidden_states
+3 -5
View File
@@ -1517,11 +1517,9 @@ class MiniMaxM3Model(nn.Module):
if not self.pp_group.is_last_rank:
return residual_batch.to_pp(hidden_states, forward_batch)
hidden_states = residual_batch.complete_output(hidden_states, forward_batch)
if hidden_states.shape[0] != 0:
hidden_states = residual_batch.final_norm(
hidden_states, forward_batch, self.norm
)
hidden_states = residual_batch.final_norm(
hidden_states, forward_batch, self.norm, skip_empty=True
)
if len(aux_hidden_states) == 0:
return hidden_states
+3 -8
View File
@@ -1214,11 +1214,9 @@ class Qwen2MoeModel(nn.Module):
if not self.pp_group.is_last_rank:
return residual_batch.to_pp(hidden_states, forward_batch)
else:
hidden_states = residual_batch.complete_output(hidden_states, forward_batch)
if hidden_states.shape[0] != 0:
hidden_states = residual_batch.final_norm(
hidden_states, forward_batch, self.norm
)
hidden_states = residual_batch.final_norm(
hidden_states, forward_batch, self.norm, skip_empty=True
)
if len(aux_hidden_states) == 0:
return hidden_states
@@ -1310,9 +1308,6 @@ class Qwen2MoeForCausalLM(nn.Module):
)
if end == self.model.config.num_hidden_layers:
forward_batch.hidden_states = residual_batch.complete_output(
forward_batch.hidden_states, forward_batch
)
# norm
hidden_states = residual_batch.final_norm(
forward_batch.hidden_states, forward_batch, self.model.norm
+3 -5
View File
@@ -477,11 +477,9 @@ class Qwen3Model(Qwen2Model):
)
if not self.pp_group.is_last_rank:
return residual_batch.to_pp(hidden_states, forward_batch)
hidden_states = residual_batch.complete_output(hidden_states, forward_batch)
if hidden_states.shape[0] != 0:
hidden_states = residual_batch.final_norm(
hidden_states, forward_batch, self.norm
)
hidden_states = residual_batch.final_norm(
hidden_states, forward_batch, self.norm, skip_empty=True
)
return (
(hidden_states, aux_hidden_states) if aux_hidden_states else hidden_states
)
-3
View File
@@ -1099,9 +1099,6 @@ class Qwen3MoeForCausalLM(nn.Module):
)
if end == self.model.config.num_hidden_layers:
forward_batch.hidden_states = residual_batch.complete_output(
forward_batch.hidden_states, forward_batch
)
# norm
hidden_states = residual_batch.final_norm(
forward_batch.hidden_states, forward_batch, self.model.norm
+7 -8
View File
@@ -1267,14 +1267,13 @@ class Qwen3LLMModel(Qwen3Model):
if not self.pp_group.is_last_rank:
return residual_batch.to_pp(hidden_states, forward_batch)
hidden_states = residual_batch.complete_output(hidden_states, forward_batch)
if hidden_states.shape[0] != 0:
hidden_states = residual_batch.final_norm(
hidden_states,
forward_batch,
self.norm,
post_residual_addition=last_deepstack,
)
hidden_states = residual_batch.final_norm(
hidden_states,
forward_batch,
self.norm,
post_residual_addition=last_deepstack,
skip_empty=True,
)
if len(aux_hidden_states) == 0:
return hidden_states
+7 -8
View File
@@ -157,14 +157,13 @@ class Qwen3MoeLLMModel(Qwen3MoeModel):
if not self.pp_group.is_last_rank:
return residual_batch.to_pp(hidden_states, forward_batch)
else:
hidden_states = residual_batch.complete_output(hidden_states, forward_batch)
if hidden_states.shape[0] != 0:
hidden_states = residual_batch.final_norm(
hidden_states,
forward_batch,
self.norm,
post_residual_addition=last_deepstack,
)
hidden_states = residual_batch.final_norm(
hidden_states,
forward_batch,
self.norm,
post_residual_addition=last_deepstack,
skip_empty=True,
)
if len(aux_hidden_states) == 0:
return hidden_states
+3 -12
View File
@@ -1169,12 +1169,9 @@ class SarvamMLAModel(nn.Module):
if not self.pp_group.is_last_rank:
return residual_batch.to_pp(hidden_states, forward_batch)
hidden_states = residual_batch.complete_output(hidden_states, forward_batch)
if hidden_states.shape[0] != 0:
hidden_states = residual_batch.final_norm(
hidden_states, forward_batch, self.norm
)
hidden_states = residual_batch.final_norm(
hidden_states, forward_batch, self.norm, skip_empty=True
)
return hidden_states
@@ -1274,9 +1271,6 @@ class SarvamMLAForCausalLM(nn.Module):
)
if end == self.model.config.num_hidden_layers:
forward_batch.hidden_states = residual_batch.complete_output(
forward_batch.hidden_states, forward_batch
)
forward_batch.hidden_states = residual_batch.final_norm(
forward_batch.hidden_states, forward_batch, self.model.norm
)
@@ -1453,9 +1447,6 @@ class SarvamMoEForCausalLM(BailingMoEForCausalLM):
)
if end == self.model.config.num_hidden_layers:
forward_batch.hidden_states = residual_batch.complete_output(
forward_batch.hidden_states, forward_batch
)
forward_batch.hidden_states = residual_batch.final_norm(
forward_batch.hidden_states, forward_batch, self.model.norm
)
+3 -6
View File
@@ -479,12 +479,9 @@ class Step3TextModel(nn.Module):
layer = self.layers[i]
hidden_states = layer(positions, hidden_states, forward_batch)
hidden_states = residual_batch.complete_output(hidden_states, forward_batch)
if hidden_states.shape[0] != 0:
hidden_states = residual_batch.final_norm(
hidden_states, forward_batch, self.norm
)
hidden_states = residual_batch.final_norm(
hidden_states, forward_batch, self.norm, skip_empty=True
)
return hidden_states
+3 -5
View File
@@ -1646,11 +1646,9 @@ class XllmModel(nn.Module):
if not self.pp_group.is_last_rank:
return residual_batch.to_pp(hidden_states, forward_batch)
else:
hidden_states = residual_batch.complete_output(hidden_states, forward_batch)
if hidden_states.shape[0] != 0:
hidden_states = residual_batch.final_norm(
hidden_states, forward_batch, self.norm
)
hidden_states = residual_batch.final_norm(
hidden_states, forward_batch, self.norm, skip_empty=True
)
return hidden_states