Update modeling_densebackward_olmoe0125.py
Browse files
modeling_densebackward_olmoe0125.py
CHANGED
|
@@ -25,32 +25,41 @@ class DenseBackwardOlmoeSparseMoeBlock(OlmoeSparseMoeBlock):
|
|
| 25 |
"""
|
| 26 |
def forward(self, hidden_states: torch.Tensor):
|
| 27 |
batch_size, seq_length, hidden_dim = hidden_states.shape
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
flat_hidden = hidden_states.view(-1, hidden_dim) # (B*seq_len, hidden_dim)
|
| 29 |
N_tokens = flat_hidden.size(0)
|
| 30 |
|
| 31 |
# 计算路由逻辑
|
| 32 |
router_logits = self.gate(flat_hidden) # (B*seq_len, num_experts)
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# 选择top-k专家
|
| 36 |
routing_weights_topk, selected_experts = torch.topk(routing_weights, self.top_k, dim=-1)
|
| 37 |
if self.norm_topk_prob:
|
| 38 |
routing_weights_topk = routing_weights_topk / routing_weights_topk.sum(dim=-1, keepdim=True)
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
# ---------- 真实计算所有专家输出(密集计算)----------
|
| 42 |
all_expert_outputs = torch.zeros((N_tokens, self.num_experts, hidden_dim),
|
| 43 |
-
dtype=
|
| 44 |
|
| 45 |
for expert_idx in range(self.num_experts):
|
| 46 |
expert_layer = self.experts[expert_idx]
|
| 47 |
# 对所有token都计算当前专家的输出
|
| 48 |
expert_output = expert_layer(flat_hidden) # (N_tokens, hidden_dim)
|
|
|
|
|
|
|
| 49 |
all_expert_outputs[:, expert_idx, :] = expert_output
|
| 50 |
|
| 51 |
# ---------- 提取激活专家输出(稀疏前向)- 使用张量批处理 ----------
|
| 52 |
# 创建索引张量,第一维是token索引,第二维是专家索引
|
| 53 |
-
token_indices = torch.arange(N_tokens, device=
|
| 54 |
batch_indices = token_indices.reshape(-1)
|
| 55 |
expert_indices = selected_experts.reshape(-1)
|
| 56 |
|
|
@@ -59,6 +68,7 @@ class DenseBackwardOlmoeSparseMoeBlock(OlmoeSparseMoeBlock):
|
|
| 59 |
|
| 60 |
# 扩展权重以便批量相乘
|
| 61 |
expanded_weights = routing_weights_topk.unsqueeze(-1) # (N_tokens, top_k, 1)
|
|
|
|
| 62 |
|
| 63 |
# 权重乘以专家输出并求和
|
| 64 |
sparse_output = (selected_outputs * expanded_weights).sum(dim=1) # (N_tokens, hidden_dim)
|
|
@@ -66,12 +76,14 @@ class DenseBackwardOlmoeSparseMoeBlock(OlmoeSparseMoeBlock):
|
|
| 66 |
# ---------- 密集计算聚合(用于反向传播)----------
|
| 67 |
# 使用所有专家的输出和路由权重计算密集输出
|
| 68 |
routing_weights_expanded = routing_weights.unsqueeze(-1) # (N_tokens, num_experts, 1)
|
|
|
|
| 69 |
dense_outputs = (all_expert_outputs * routing_weights_expanded).sum(dim=1) # (N_tokens, hidden_dim)
|
| 70 |
|
| 71 |
# ---------- 组合稀疏前向和密集反向 ----------
|
| 72 |
# sparse_output.detach()保留稀疏前向计算图
|
| 73 |
# (dense_outputs - dense_outputs.detach())只保留密集反向梯度
|
| 74 |
final_flat = sparse_output.detach() + (dense_outputs - dense_outputs.detach())
|
|
|
|
| 75 |
final_output = final_flat.view(batch_size, seq_length, hidden_dim)
|
| 76 |
|
| 77 |
return final_output, router_logits
|
|
|
|
| 25 |
"""
|
| 26 |
def forward(self, hidden_states: torch.Tensor):
|
| 27 |
batch_size, seq_length, hidden_dim = hidden_states.shape
|
| 28 |
+
# 记录输入张量的数据类型,确保所有计算保持一致
|
| 29 |
+
dtype = hidden_states.dtype
|
| 30 |
+
device = hidden_states.device
|
| 31 |
+
|
| 32 |
flat_hidden = hidden_states.view(-1, hidden_dim) # (B*seq_len, hidden_dim)
|
| 33 |
N_tokens = flat_hidden.size(0)
|
| 34 |
|
| 35 |
# 计算路由逻辑
|
| 36 |
router_logits = self.gate(flat_hidden) # (B*seq_len, num_experts)
|
| 37 |
+
# 确保router_logits和flat_hidden数据类型一致
|
| 38 |
+
router_logits = router_logits.to(dtype=dtype)
|
| 39 |
+
routing_weights = F.softmax(router_logits, dim=1, dtype=dtype) # (B*seq_len, num_experts)
|
| 40 |
|
| 41 |
# 选择top-k专家
|
| 42 |
routing_weights_topk, selected_experts = torch.topk(routing_weights, self.top_k, dim=-1)
|
| 43 |
if self.norm_topk_prob:
|
| 44 |
routing_weights_topk = routing_weights_topk / routing_weights_topk.sum(dim=-1, keepdim=True)
|
| 45 |
+
# 确保归一化后类型一致
|
| 46 |
+
routing_weights_topk = routing_weights_topk.to(dtype=dtype)
|
| 47 |
+
|
| 48 |
# ---------- 真实计算所有专家输出(密集计算)----------
|
| 49 |
all_expert_outputs = torch.zeros((N_tokens, self.num_experts, hidden_dim),
|
| 50 |
+
dtype=dtype, device=device)
|
| 51 |
|
| 52 |
for expert_idx in range(self.num_experts):
|
| 53 |
expert_layer = self.experts[expert_idx]
|
| 54 |
# 对所有token都计算当前专家的输出
|
| 55 |
expert_output = expert_layer(flat_hidden) # (N_tokens, hidden_dim)
|
| 56 |
+
# 确保专家输出与预期类型一致
|
| 57 |
+
expert_output = expert_output.to(dtype=dtype)
|
| 58 |
all_expert_outputs[:, expert_idx, :] = expert_output
|
| 59 |
|
| 60 |
# ---------- 提取激活专家输出(稀疏前向)- 使用张量批处理 ----------
|
| 61 |
# 创建索引张量,第一维是token索引,第二维是专家索引
|
| 62 |
+
token_indices = torch.arange(N_tokens, device=device).unsqueeze(1).expand(-1, self.top_k)
|
| 63 |
batch_indices = token_indices.reshape(-1)
|
| 64 |
expert_indices = selected_experts.reshape(-1)
|
| 65 |
|
|
|
|
| 68 |
|
| 69 |
# 扩展权重以便批量相乘
|
| 70 |
expanded_weights = routing_weights_topk.unsqueeze(-1) # (N_tokens, top_k, 1)
|
| 71 |
+
expanded_weights = expanded_weights.to(dtype=dtype)
|
| 72 |
|
| 73 |
# 权重乘以专家输出并求和
|
| 74 |
sparse_output = (selected_outputs * expanded_weights).sum(dim=1) # (N_tokens, hidden_dim)
|
|
|
|
| 76 |
# ---------- 密集计算聚合(用于反向传播)----------
|
| 77 |
# 使用所有专家的输出和路由权重计算密集输出
|
| 78 |
routing_weights_expanded = routing_weights.unsqueeze(-1) # (N_tokens, num_experts, 1)
|
| 79 |
+
routing_weights_expanded = routing_weights_expanded.to(dtype=dtype)
|
| 80 |
dense_outputs = (all_expert_outputs * routing_weights_expanded).sum(dim=1) # (N_tokens, hidden_dim)
|
| 81 |
|
| 82 |
# ---------- 组合稀疏前向和密集反向 ----------
|
| 83 |
# sparse_output.detach()保留稀疏前向计算图
|
| 84 |
# (dense_outputs - dense_outputs.detach())只保留密集反向梯度
|
| 85 |
final_flat = sparse_output.detach() + (dense_outputs - dense_outputs.detach())
|
| 86 |
+
final_flat = final_flat.to(dtype=dtype) # 确保最终输出类型一致
|
| 87 |
final_output = final_flat.view(batch_size, seq_length, hidden_dim)
|
| 88 |
|
| 89 |
return final_output, router_logits
|