|
19 | 19 | fused_compute_score_for_moe_aux_loss, |
20 | 20 | fused_moe_aux_loss, |
21 | 21 | fused_permute, |
| 22 | + fused_permute_and_pad_with_probs, |
22 | 23 | fused_permute_with_probs, |
23 | 24 | fused_sort_chunks_by_index, |
24 | 25 | fused_sort_chunks_by_index_with_probs, |
@@ -227,6 +228,8 @@ def permute( |
227 | 228 | num_out_tokens: Optional[int] = None, |
228 | 229 | fused: bool = False, |
229 | 230 | drop_and_pad: bool = False, |
| 231 | + tokens_per_expert: Optional[torch.Tensor] = None, |
| 232 | + align_size: int = -1, |
230 | 233 | ): |
231 | 234 | """Permute the tokens and probs based on the mask. |
232 | 235 | Tokens with the same designated expert will be grouped together. |
@@ -257,11 +260,18 @@ def permute( |
257 | 260 | return permuted_input, None, sorted_indices |
258 | 261 |
|
259 | 262 | if fused and probs is not None: |
260 | | - if not HAVE_TE or fused_permute_with_probs is None: |
261 | | - raise ValueError( |
262 | | - "fused_permute_with_probs is not available. Please install TE >= 2.1.0." |
| 263 | + if tokens_per_expert is not None and align_size > 0: |
| 264 | + return fused_permute_and_pad_with_probs( |
| 265 | + tokens, probs, routing_map, tokens_per_expert, align_size |
| 266 | + ) |
| 267 | + else: |
| 268 | + if not HAVE_TE or fused_permute_with_probs is None: |
| 269 | + raise ValueError( |
| 270 | + "fused_permute_with_probs is not available. Please install TE >= 2.1.0." |
| 271 | + ) |
| 272 | + return fused_permute_with_probs( |
| 273 | + tokens, probs, routing_map, num_out_tokens=num_out_tokens |
263 | 274 | ) |
264 | | - return fused_permute_with_probs(tokens, probs, routing_map, num_out_tokens=num_out_tokens) |
265 | 275 |
|
266 | 276 | num_tokens, hidden = tokens.shape |
267 | 277 | num_experts = routing_map.shape[1] |
@@ -315,6 +325,7 @@ def unpermute( |
315 | 325 | routing_map: torch.Tensor = None, |
316 | 326 | fused: bool = False, |
317 | 327 | drop_and_pad: bool = False, |
| 328 | + pad_offsets: Optional[torch.Tensor] = None, |
318 | 329 | ): |
319 | 330 | """ |
320 | 331 | Restore the original order of tokens after permutation. If probs are provided, it |
@@ -344,7 +355,11 @@ def unpermute( |
344 | 355 | if not HAVE_TE or fused_unpermute is None: |
345 | 356 | raise ValueError("fused_unpermute is not available. Please install TE >= 2.1.0.") |
346 | 357 | return fused_unpermute( |
347 | | - permuted_tokens, sorted_indices, merging_probs=probs, restore_shape=restore_shape |
| 358 | + permuted_tokens, |
| 359 | + sorted_indices, |
| 360 | + merging_probs=probs, |
| 361 | + restore_shape=restore_shape, |
| 362 | + pad_offsets=pad_offsets, |
348 | 363 | ) |
349 | 364 |
|
350 | 365 | _, hidden = restore_shape |
|
0 commit comments