Datasets:
File size: 9,108 Bytes
167e081 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | import torch
import torch.nn.functional as F
from typing import Optional
from tile_kernels.moe.scoring import ScoringFunc
def stable_topk(scores: torch.Tensor, num_topk: int) -> torch.Tensor:
_, sorted_indices = torch.sort(scores, dim=1, descending=True, stable=True)
return sorted_indices[:, :num_topk].contiguous()
def topk_sum_and_topk_group_idx(
scores: torch.Tensor,
num_group_sum_topk: int,
num_topk_groups: int,
) -> torch.Tensor:
group_scores_ref = scores.topk(num_group_sum_topk, dim=-1, sorted=False).values.sum(-1)
return stable_topk(group_scores_ref, num_topk_groups)
def top2_sum_gate(
logits: torch.Tensor,
bias: torch.Tensor,
num_topk: int,
num_topk_groups: int,
num_groups: int,
use_shared_as_routed: bool,
num_shared_experts: int,
routed_scaling_factor: float,
ep_rank: int,
num_ep_ranks: int,
tp_rank: int,
num_tp_ranks: int,
scoring_func: str,
mask: Optional[torch.Tensor] = None,
fix_routing_mask: Optional[torch.Tensor] = None,
to_physical_map: Optional[torch.Tensor] = None,
logical_count: Optional[torch.Tensor] = None,
unmapped_topk_idx: Optional[torch.Tensor] = None,
) -> tuple[torch.Tensor, torch.Tensor]:
"""PyTorch reference for top-k expert routing with top-2 sum grouping.
Args:
logits: Raw token-expert logits, shape ``(num_tokens, num_routed_experts)``,
``float32``.
bias: Per-expert bias added to scores before ranking, shape
``(num_routed_experts,)``, ``float32``.
num_topk: Number of routed experts to select per token.
num_topk_groups: Number of expert groups to keep (0 means no grouping).
num_groups: Total number of expert groups (0 means no grouping).
use_shared_as_routed: Whether shared experts are appended as extra routed
slots in the output.
num_shared_experts: Number of shared experts.
routed_scaling_factor: Multiplicative scaling applied to normalised weights.
ep_rank: Expert-parallelism rank of this process.
num_ep_ranks: Total number of expert-parallelism ranks.
tp_rank: Tensor-parallelism rank of this process.
num_tp_ranks: Total number of tensor-parallelism ranks.
scoring_func: One of ``'sigmoid'``, ``'sqrtsoftplus'``, ``'softmax'``.
mask: Boolean mask, shape ``(num_tokens,)``. ``True`` → route the token,
``False`` → fill outputs with ``-1`` / ``0``.
fix_routing_mask: Boolean mask, shape ``(num_tokens,)``. When ``True`` for
a token, use the indices already stored in *unmapped_topk_idx* instead of
running the selection algorithm.
to_physical_map: Logical-to-physical expert map, shape
``(num_logical_experts, num_duplicate_experts + 1)``, ``int32``.
logical_count: Number of active duplicates per logical expert, shape
``(num_logical_experts,)``, ``int32``.
unmapped_topk_idx: Output tensor (updated in-place) for unmasked expert
indices, shape ``(num_tokens, num_topk)``, ``int64``.
Returns:
topk_idx: Selected (post-EP/TP masking) expert indices, shape
``(num_tokens, num_topk + num_shared_experts)``, ``int64``.
topk_weights: Normalised expert weights, same shape, ``float32``.
"""
num_tokens_full, num_routed_experts = logits.shape
scoring = ScoringFunc.from_str(scoring_func)
if not use_shared_as_routed:
num_shared_experts = 0
num_physical_topk = num_topk + num_shared_experts
num_logical_experts = num_routed_experts + num_shared_experts
device = logits.device
topk_idx_out = torch.full((num_tokens_full, num_physical_topk), -1, dtype=torch.int64, device=device)
topk_weights_out = torch.zeros((num_tokens_full, num_physical_topk), dtype=torch.float32, device=device)
if num_tokens_full == 0:
return topk_idx_out, topk_weights_out
active = mask if mask is not None else torch.ones(num_tokens_full, dtype=torch.bool, device=device)
active_indices = active.nonzero(as_tuple=False).squeeze(1)
num_tokens = active_indices.numel()
if num_tokens == 0:
if unmapped_topk_idx is not None:
unmapped_topk_idx[~active] = -1
return topk_idx_out, topk_weights_out
logits_a = logits[active_indices]
bias_b = bias.unsqueeze(0)
# 1. Apply scoring function
if scoring == ScoringFunc.SIGMOID:
scores_wo_bias = torch.sigmoid(logits_a)
elif scoring == ScoringFunc.SQRTSOFTPLUS:
scores_wo_bias = F.softplus(logits_a).sqrt()
else: # SOFTMAX
scores_wo_bias = torch.softmax(logits_a, dim=-1)
# 2. Biased scores for ranking (softmax uses raw logits + bias)
scores_biased = (logits_a + bias_b) if scoring == ScoringFunc.SOFTMAX else (scores_wo_bias + bias_b)
# 3. Split tokens into normal routing and fix_routing
fix_mask = torch.zeros(num_tokens, dtype=torch.bool, device=device)
if fix_routing_mask is not None and unmapped_topk_idx is not None:
fix_mask = fix_routing_mask[active_indices]
topk_idx_local = torch.full((num_tokens, num_topk), -1, dtype=torch.int64, device=device)
topk_score_local = torch.zeros((num_tokens, num_topk), dtype=torch.float32, device=device)
# 4. Normal routing: select top-k experts
normal_mask = ~fix_mask
if normal_mask.any():
normal_indices = normal_mask.nonzero(as_tuple=False).squeeze(1)
sb = scores_biased[normal_indices]
if num_groups != num_topk_groups:
num_per_group = num_routed_experts // num_groups
top_group_idx = topk_sum_and_topk_group_idx(sb.view(-1, num_groups, num_per_group), 2, num_topk_groups)
group_mask = torch.ones((normal_indices.numel(), num_groups), dtype=torch.bool, device=device)
group_mask.scatter_(1, top_group_idx, False)
sb = sb.masked_fill(
group_mask.unsqueeze(-1).expand(-1, num_groups, num_per_group).reshape(-1, num_routed_experts),
float('-inf'),
)
selected = stable_topk(sb, num_topk)
topk_idx_local[normal_indices] = selected
topk_score_local[normal_indices] = scores_wo_bias[normal_indices].gather(1, selected)
# 5. Fix routing: use pre-stored indices
if fix_mask.any() and unmapped_topk_idx is not None:
fix_indices = fix_mask.nonzero(as_tuple=False).squeeze(1)
pre_idx = unmapped_topk_idx[active_indices[fix_indices]]
topk_idx_local[fix_indices] = pre_idx
topk_score_local[fix_indices] = scores_wo_bias[fix_indices].gather(1, pre_idx.clamp(min=0))
# 6. Write unmapped_topk_idx
if unmapped_topk_idx is not None:
unmapped_topk_idx[active_indices] = topk_idx_local
if mask is not None:
unmapped_topk_idx[~active] = -1
# 7. Normalise weights (top-sum normalisation)
topk_sum = topk_score_local.sum(dim=-1, keepdim=True).clamp(min=1e-20)
topk_weights_routed = topk_score_local / topk_sum * routed_scaling_factor
# 8. Append shared-expert slots
if num_shared_experts > 0:
shared_idx = torch.arange(num_routed_experts, num_logical_experts, dtype=torch.int64, device=device)
topk_idx_all = torch.cat([topk_idx_local, shared_idx.expand(num_tokens, -1)], dim=1)
topk_weights_all = torch.cat(
[
topk_weights_routed,
torch.ones((num_tokens, num_shared_experts), dtype=torch.float32, device=device),
],
dim=1,
)
else:
topk_idx_all, topk_weights_all = topk_idx_local, topk_weights_routed
# 9. Map logical → physical experts
if to_physical_map is not None and logical_count is not None:
for lane in range(num_physical_topk):
logical = topk_idx_all[:, lane]
valid = logical >= 0
if valid.any():
global_idx = active_indices[valid].to(torch.int64)
dup_idx = (ep_rank + global_idx * 23333) % logical_count[logical[valid]].to(torch.int64)
topk_idx_all[valid, lane] = to_physical_map[logical[valid], dup_idx].to(torch.int64)
# 10. EP / TP masking
num_extra = to_physical_map.shape[1] - 1 if to_physical_map is not None else 0
experts_per_rank = (num_routed_experts + num_extra) // num_ep_ranks
experts_per_dp = experts_per_rank * num_tp_ranks
idx = topk_idx_all
valid = idx >= 0
ep_of = torch.where(valid, idx // experts_per_rank, torch.zeros_like(idx))
idx = torch.where(valid & (ep_of % num_tp_ranks != tp_rank), -1, idx)
valid = idx >= 0
local = idx - tp_rank * experts_per_rank
dp_of = torch.where(valid, local // experts_per_dp, torch.zeros_like(local))
remapped = local - dp_of * (experts_per_dp - experts_per_rank)
idx = torch.where(valid & (remapped >= 0), remapped, torch.where(valid, -1, idx))
# 11. Write outputs
topk_idx_out[active_indices] = idx
topk_weights_out[active_indices] = topk_weights_all
return topk_idx_out, topk_weights_out
|