Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions tests/ut/ops/test_fused_moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,12 +676,13 @@ def test_routing_replay_disabled_keeps_ascend_routing_unchanged(monkeypatch):


def test_hash_router_uses_explicit_input_ids(monkeypatch):
input_ids = torch.tensor([11, 22], dtype=torch.int32)
input_ids = torch.tensor([-1, 22], dtype=torch.int32)
gathered_input_ids = torch.tensor([22, -1], dtype=torch.int64)
hidden_states = torch.randn(2, 4)
router_logits = torch.randn(2, 4)
topk_weights = torch.randn(2, 2)
topk_ids = torch.zeros(2, 2, dtype=torch.int32)
prepare_finalize = SimpleNamespace(all_gather_input_id_with_dp_group=MagicMock(side_effect=lambda value: value))
prepare_finalize = SimpleNamespace(all_gather_input_ids=MagicMock(return_value=gathered_input_ids))
monkeypatch.setattr(
fused_topk_router_module,
"_EXTRA_CTX",
Expand Down Expand Up @@ -715,8 +716,10 @@ def test_hash_router_uses_explicit_input_ids(monkeypatch):

assert weights is topk_weights
assert ids is topk_ids
torch.testing.assert_close(hash_op.call_args.kwargs["input_ids"], input_ids.to(torch.int64))
prepare_finalize.all_gather_input_id_with_dp_group.assert_called_once()
torch.testing.assert_close(hash_op.call_args.kwargs["input_ids"], torch.tensor([22, 0], dtype=torch.int64))
prepare_finalize.all_gather_input_ids.assert_called_once()
actual_input_ids = prepare_finalize.all_gather_input_ids.call_args.args[0]
torch.testing.assert_close(actual_input_ids, input_ids.to(torch.int64))

with pytest.raises(ValueError, match="hash MoE routing requires input_ids"):
router._compute_routing(hidden_states, router_logits, torch.int32)
Expand Down
141 changes: 88 additions & 53 deletions tests/ut/ops/test_prepare_finalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,86 +204,121 @@ def mock_all_gather_func(tensor_list, tensor, group=None):
# Should concat back
self.assertEqual(final_result.shape[0], 2)

@patch("vllm_ascend.ops.fused_moe.prepare_finalize.get_pcp_group")
@patch("vllm_ascend.ops.fused_moe.prepare_finalize.get_dp_group")
@patch("vllm_ascend.ascend_forward_context.get_forward_context")
def test_allgather_prepare_finalize(self, mock_get_forward_context, mock_get_dp_group):
# Mock forward context
mock_context = MagicMock()
mock_context.max_tokens_across_dp = 6
mock_get_forward_context.return_value = mock_context

# Create a proper mock for DP group with working all_gather
mock_dp_group = MagicMock()

def mock_all_gather_func(tensor, dim):
# Simulate DP=2: repeat the tensor along the specified dimension
return torch.cat([tensor, tensor], dim=dim)

mock_dp_group.all_gather = mock_all_gather_func
mock_get_dp_group.return_value = mock_dp_group

self.moe_config.dp_size = 2
self.moe_config.tp_size = 1
self.moe_config.pcp_size = 1
self.moe_config.ep_size = 1
self.moe_config.is_sequence_parallel = False
self.moe_config.dp_group = mock_dp_group

layer = PrepareAndFinalizeWithAllGather(self.moe_config)

hidden_states = torch.randn(3, 8)
router_logits = torch.randn(3, 2)

prepare_output = layer.prepare(hidden_states, router_logits)
h_out = prepare_output.hidden_states
r_out = prepare_output.router_logits
padded_hidden_states_shape = prepare_output.padded_hidden_states_shape

# After all-gather with DP=2, should double the batch size
self.assertEqual(h_out.shape[0], 12)
self.assertEqual(r_out.shape[0], 12)
self.assertIsNone(padded_hidden_states_shape)

# Finalize with reduce_scatter
def mock_reduce_scatter_func(tensor, dim):
# Simulate reduce_scatter: take first half
return tensor[:3]

mock_dp_group.reduce_scatter = mock_reduce_scatter_func
result = layer.finalize(h_out, reduce_results=False, padded_hidden_states_shape=padded_hidden_states_shape)

self.assertEqual(result.shape[0], 3)

result_with_tp = layer.finalize(h_out, reduce_results=True)
self.assertEqual(result_with_tp.shape[0], 3)
def test_allgather_prepare_finalize(
self,
mock_get_forward_context,
mock_get_dp_group,
mock_get_pcp_group,
):
hidden_states = torch.arange(12, dtype=torch.float32).view(3, 4)
router_logits = torch.arange(6, dtype=torch.float32).view(3, 2)
input_ids = torch.tensor([11, 22, 33])

cases = (
("none", 1, 1, 3, 0),
("dp", 2, 1, 4, 0),
("pcp", 1, 2, 3, 4),
("dp_pcp", 2, 2, 4, 8),
)
for name, dp_size, pcp_size, max_tokens_dp, max_tokens_pcp in cases:
with self.subTest(name=name):
mock_context = MagicMock()
mock_context.max_tokens_across_dp = max_tokens_dp
mock_context.max_tokens_across_pcp = max_tokens_pcp
mock_get_forward_context.return_value = mock_context

mock_dp_group = MagicMock()
mock_dp_group.all_gather.side_effect = lambda tensor, dim: torch.cat([tensor, tensor + 100], dim=dim)
mock_dp_group.reduce_scatter.side_effect = lambda tensor, dim, group_size=dp_size: tensor.chunk(
group_size, dim=dim
)[0]
mock_get_dp_group.return_value = mock_dp_group

mock_pcp_group = MagicMock()
mock_pcp_group.all_gather.side_effect = lambda tensor, dim: torch.cat([tensor, tensor + 1000], dim=dim)
mock_pcp_group.reduce_scatter.side_effect = lambda tensor, dim, group_size=pcp_size: tensor.chunk(
group_size, dim=dim
)[0]
mock_get_pcp_group.return_value = mock_pcp_group

self.moe_config.dp_size = dp_size
self.moe_config.pcp_size = pcp_size
self.moe_config.is_sequence_parallel = False
self.moe_config.dp_group = mock_dp_group
layer = PrepareAndFinalizeWithAllGather(self.moe_config)

prepared = layer.prepare(hidden_states, router_logits)
gathered_input_ids = layer.all_gather_input_ids(input_ids)

expected_input_ids = input_ids
if dp_size > 1:
expected_input_ids = torch.nn.functional.pad(
expected_input_ids,
(0, max_tokens_dp - expected_input_ids.numel()),
)
expected_input_ids = torch.cat([expected_input_ids, expected_input_ids + 100])
if pcp_size > 1:
expected_input_ids = torch.nn.functional.pad(
expected_input_ids,
(0, max_tokens_pcp - expected_input_ids.numel()),
)
expected_input_ids = torch.cat([expected_input_ids, expected_input_ids + 1000])

self.assertEqual(prepared.hidden_states.shape[0], expected_input_ids.numel())
self.assertEqual(prepared.router_logits.shape[0], expected_input_ids.numel())
self.assertIsNone(prepared.padded_hidden_states_shape)
torch.testing.assert_close(gathered_input_ids, expected_input_ids)

finalized = layer.finalize(
prepared.hidden_states,
reduce_results=False,
padded_hidden_states_shape=prepared.padded_hidden_states_shape,
)
torch.testing.assert_close(finalized, hidden_states)
self.assertEqual(mock_dp_group.all_gather.call_count, 3 if dp_size > 1 else 0)
self.assertEqual(mock_dp_group.reduce_scatter.call_count, 1 if dp_size > 1 else 0)
self.assertEqual(mock_pcp_group.all_gather.call_count, 3 if pcp_size > 1 else 0)
self.assertEqual(mock_pcp_group.reduce_scatter.call_count, 1 if pcp_size > 1 else 0)


class TestSequenceParallelPCP(unittest.TestCase):
def test_ep_path_does_not_repeat_pcp_collectives(self):
def test_ep_path_gathers_inputs_once_without_dp_pcp_collectives(self):
config = MagicMock()
config.is_sequence_parallel = True
config.pcp_size = 2
config.dp_size = 2
inputs = torch.arange(12).view(3, 4).float()
logits = torch.arange(6).view(3, 2).float()
input_ids = torch.tensor([11, 22, 33])
gathered_inputs = inputs.repeat(8, 1)
gathered_logits = logits.repeat(8, 1)
gathered_input_ids = input_ids.repeat(8)
with (
patch("vllm_ascend.ops.fused_moe.prepare_finalize.get_dynamic_mx_quant_scale_alg", return_value=0),
patch("vllm_ascend.ops.fused_moe.prepare_finalize.get_pcp_group") as pcp_group,
patch("vllm_ascend.ops.fused_moe.prepare_finalize._EXTRA_CTX", max_tokens_across_pcp=0),
patch(
"torch.ops.vllm.maybe_all_gather_and_maybe_unpad", side_effect=[gathered_inputs, gathered_logits]
"torch.ops.vllm.maybe_all_gather_and_maybe_unpad",
side_effect=[gathered_inputs, gathered_logits, gathered_input_ids],
) as gather,
patch("torch.ops.vllm.maybe_pad_and_reduce", return_value=inputs) as reduce,
):
pcp_group.return_value.all_gather.side_effect = lambda x, dim: x.repeat(2, 1)
layer = PrepareAndFinalizeWithAllGather(config)
result = layer.prepare(inputs, logits)
result_input_ids = layer.all_gather_input_ids(input_ids)
self.assertTrue(torch.equal(result.hidden_states, gathered_inputs))
self.assertTrue(torch.equal(result.router_logits, gathered_logits))
self.assertTrue(torch.equal(result_input_ids, gathered_input_ids))
output = layer.finalize(result.hidden_states, reduce_results=False)
self.assertTrue(torch.equal(output, inputs))
self.assertEqual(gather.call_count, 2)
self.assertEqual(gather.call_count, 3)
torch.testing.assert_close(gather.call_args_list[0].args[0], inputs)
torch.testing.assert_close(gather.call_args_list[1].args[0], logits)
torch.testing.assert_close(gather.call_args_list[2].args[0], input_ids)
reduce.assert_called_once_with(gathered_inputs)
config.dp_group.all_gather.assert_not_called()
pcp_group.assert_not_called()
21 changes: 20 additions & 1 deletion vllm_ascend/ops/fused_moe/prepare_finalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,14 +488,33 @@ def _prepare_with_dp_group(
pertoken_scale=None,
)

def all_gather_input_id_with_dp_group(self, input_ids: torch.Tensor) -> torch.Tensor:
def all_gather_input_ids(self, input_ids: torch.Tensor) -> torch.Tensor:
"""Align token ids with the rows produced by :meth:`prepare`.

Hash-based routing consumes ``input_ids`` alongside the gathered router
logits. Sequence-parallel inputs use the same EP gather-and-unpad path
as hidden states and router logits. Other inputs follow the DP-then-PCP
communication layout.
"""
if self._use_ep_sequence_parallel():
return torch.ops.vllm.maybe_all_gather_and_maybe_unpad(input_ids)

if self.moe_config.dp_size > 1:
max_tokens_across_dp = _EXTRA_CTX.max_tokens_across_dp
pad_size = max_tokens_across_dp - self.num_tokens
if pad_size > 0:
input_ids = nn.functional.pad(input_ids, (0, pad_size))

input_ids = self.moe_config.dp_group.all_gather(input_ids, 0)

if self.moe_config.pcp_size > 1:
max_tokens_across_pcp = _EXTRA_CTX.max_tokens_across_pcp
pad_size = max_tokens_across_pcp - self.num_tokens_pcp
if pad_size > 0:
input_ids = nn.functional.pad(input_ids, (0, pad_size))
Comment thread
li1how marked this conversation as resolved.
Resolved

input_ids = get_pcp_group().all_gather(input_ids, 0)

return input_ids
Comment thread
li1how marked this conversation as resolved.
Resolved

def finalize(
Expand Down
2 changes: 1 addition & 1 deletion vllm_ascend/ops/fused_moe/router/fused_topk_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def _compute_routing(
tid2eid_ones = self.tid2eid.to(torch.int32) if self.tid2eid is not None else None
if _EXTRA_CTX.moe_comm_type == MoECommType.ALLGATHER:
prepare_finalize = _EXTRA_CTX.moe_comm_method.prepare_finalize
input_ids = prepare_finalize.all_gather_input_id_with_dp_group(input_ids)
input_ids = prepare_finalize.all_gather_input_ids(input_ids)
else:
input_ids = _EXTRA_CTX.moe_comm_method.pad_and_split_input_ids(input_ids)
if _EXTRA_CTX.moe_comm_type != MoECommType.ALLGATHER and input_ids.numel() != router_logits.shape[0]:
Expand Down
Loading