diff --git a/tests/pytorch/test_permutation.py b/tests/pytorch/test_permutation.py index 53e76ebac1..b0ce16ba72 100644 --- a/tests/pytorch/test_permutation.py +++ b/tests/pytorch/test_permutation.py @@ -1799,6 +1799,33 @@ def test_permutation_mask_map_capacity_drop(torch_dtype, num_tokens): assert_close(unperm, ref_unperm) +@pytest.mark.parametrize("te_dtype", _te_dtypes) +@pytest.mark.parametrize("use_torch_compile", [False, True]) +def test_permutation_mask_map_zero_out_tokens(te_dtype, use_torch_compile): + """Non-empty input whose routing map routes no token (num_out_tokens == 0), e.g. an + expert-parallel rank whose local experts were not selected.""" + kwargs = dict( + te_dtype=te_dtype, + num_tokens=256, + num_expert=8, + hidden_size=4096, + topK=2, + num_out_tokens=0, + use_torch_compile=use_torch_compile, + ) + _test_permutation_mask_map(with_probs=True, **kwargs) + _test_permutation_mask_map_alongside_probs(tp_size=1, **kwargs) + _test_permutation_and_padding_mask_map(with_merging_probs=True, **kwargs) + + +def test_permutation_mask_map_rejects_negative_out_tokens(): + """A negative num_out_tokens (such as a -1 'infer' sentinel) is not a valid size.""" + routing_map = torch.ones(4, 2, dtype=torch.bool, device="cuda") + inp = torch.randn(4, 16, device="cuda") + with pytest.raises(AssertionError, match="num_out_tokens >= 0"): + te_permute(inp, routing_map, num_out_tokens=-1, map_type="mask") + + @pytest.mark.parametrize("te_dtype", _te_dtypes) @pytest.mark.parametrize("num_out_tokens", [None]) @pytest.mark.parametrize( @@ -1991,6 +2018,27 @@ def test_permutation_mask_map_fp8( ) +@pytest.mark.skipif(not fp8_available, reason=reason_for_no_fp8) +@pytest.mark.parametrize("te_dtype", [te.DType.kFloat8E4M3, te.DType.kFloat8E5M2]) +@pytest.mark.parametrize("recipe", fp8_recipes) +def test_permutation_mask_map_fp8_zero_out_tokens(te_dtype, recipe): + """Quantized non-empty input whose routing map routes no token (num_out_tokens == 0).""" + if recipe.mxfp8() and not mxfp8_available: + pytest.skip(reason_for_no_mxfp8) + if recipe.float8_block_scaling() and not fp8_block_scaling_available: + pytest.skip(reason_for_no_fp8_block_scaling) + + _test_permutation_mask_map_fp8( + te_dtype=te_dtype, + num_tokens=256, + num_expert=8, + hidden_size=4096, + topK=2, + num_out_tokens=0, + recipe=recipe, + ) + + @pytest.mark.parametrize("te_dtype", _te_dtypes) @pytest.mark.parametrize("num_tokens", [4096]) @pytest.mark.parametrize("num_expert", [7, 16]) diff --git a/transformer_engine/pytorch/permutation.py b/transformer_engine/pytorch/permutation.py index 0eb5ffe88d..3149f7c90b 100644 --- a/transformer_engine/pytorch/permutation.py +++ b/transformer_engine/pytorch/permutation.py @@ -329,8 +329,8 @@ def moe_permute_mask_map_forward( f"Permute not possible: inp.size(0) ({inp.size(0)}) must match " f"routing_map.size(0) ({routing_map.size(0)})." ) - assert num_out_tokens > 0, ( - f"moe_permute (mask map) requires num_out_tokens > 0, got {num_out_tokens}. " + assert num_out_tokens >= 0, ( + f"moe_permute (mask map) requires num_out_tokens >= 0, got {num_out_tokens}. " "Use int(routing_map.sum()) or num_tokens * top_k." ) num_tokens, hidden_size = inp.size() @@ -456,8 +456,8 @@ def _moe_permute_mask_map_forward_fake( # pylint: disable=unused-argument hidden_size = inp.shape[1] num_experts = routing_map.shape[1] if num_tokens > 0: - assert num_out_tokens > 0, ( - f"moe_permute (mask map) requires num_out_tokens > 0, got {num_out_tokens}. " + assert num_out_tokens >= 0, ( + f"moe_permute (mask map) requires num_out_tokens >= 0, got {num_out_tokens}. " "Use int(routing_map.sum()) or num_tokens * top_k." ) out_rows = num_out_tokens @@ -560,8 +560,9 @@ def _moe_permute_mask_map_backward_wrapper( row_id_map, pad_offsets = ctx.saved_tensors - # Pass permuted_probs_grad only if it has content - probs_grad_input = grad_permuted_probs if grad_permuted_probs.numel() > 0 else None + # Pass permuted_probs_grad only when probs need a gradient. Its size cannot tell: + # with num_out_tokens == 0 it is empty even though probs need a (zero) gradient. + probs_grad_input = grad_permuted_probs if ctx.needs_probs_grad else None act_grad, probs_grad = torch.ops.te_moe.permute_mask_map_bwd( grad_output, @@ -600,7 +601,9 @@ def moe_unpermute_mask_map_forward( ) -> torch.Tensor: """Forward pass for MoE unpermute with mask router map.""" if not inp.numel(): - return inp.clone() + # No permuted rows (e.g. no token was routed to these experts): every + # token receives nothing, so the output is zero. + return torch.zeros((num_tokens, hidden_size), dtype=inp.dtype, device=inp.device) assert not isinstance( inp, QuantizedTensor ), "The forward of moe_unpermute does not support FP8." @@ -828,11 +831,14 @@ def _moe_unpermute_mask_map_setup_context(ctx, inputs, output): # pylint: disab def _moe_unpermute_mask_map_backward_wrapper(ctx, unpermuted_act_grad): """Backward wrapper calling the appropriate custom backward op.""" if ctx.empty_input: + # The forward input has no rows, so its gradient has none either, even when + # the unpermuted output (and thus unpermuted_act_grad) has num_tokens rows. + act_grad = unpermuted_act_grad.new_zeros((0, ctx.hidden_size)) if ctx.with_probs: _, _, merging_probs, _ = ctx.saved_tensors probs_grad = torch.zeros_like(merging_probs) if ctx.needs_probs_grad else None - return unpermuted_act_grad, None, probs_grad, None, None, None, None - return unpermuted_act_grad, None, None, None, None, None, None + return act_grad, None, probs_grad, None, None, None, None + return act_grad, None, None, None, None, None, None act_grad = None probs_grad = None @@ -913,7 +919,9 @@ def moe_permute( The values in it are the routed expert indices. num_out_tokens : int Number of output tokens (rows in the permuted buffer). - mask map: must be > 0, e.g. int(routing_map.sum()) or num_tokens * top_k. + mask map: must be >= 0, e.g. int(routing_map.sum()) or num_tokens * top_k. + Routed entries beyond the first num_out_tokens rows are dropped, so 0 yields + an empty output, e.g. when routing_map routes no token to any expert. index map: must be >= 0; 0 means infer as num_tokens * top_k. max_token_num : int, default = -1 Workspace sizing hint, only used for map_type='index'. Ignored for 'mask'. @@ -962,8 +970,9 @@ def moe_permute_with_probs( The token to expert mapping tensor of shape [num_tokens, num_experts] and dtype 'int32'. The values in it: 1 means the token is routed to this expert and 0 means not. num_out_tokens : int - Number of output tokens (rows in the permuted buffer). Must be > 0, - e.g. int(routing_map.sum()) or num_tokens * top_k. + Number of output tokens (rows in the permuted buffer). Must be >= 0, + e.g. int(routing_map.sum()) or num_tokens * top_k. Routed entries beyond + the first num_out_tokens rows are dropped, so 0 yields an empty output. """ if isinstance(inp, QuantizedTensor) and torch.compiler.is_compiling(): raise RuntimeError(