Skip to content
Open
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
2 changes: 2 additions & 0 deletions tests/ut/ops/test_token_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ def test_token_combine_with_expert_map(self):
self.dispatcher.original_shape = (3, 128)
self.dispatcher.mask = torch.tensor([0, 1, 1, 0])
hidden_states = torch.randn(6, 128)
self.dispatcher.expanded_row_idx = torch.tensor([0, 1, 1, 1, 1, 1],
dtype=torch.int32)

final_hidden_states = self.dispatcher.token_combine(hidden_states)
self.assertEqual(final_hidden_states.shape, (6, 128))
Expand Down
2 changes: 2 additions & 0 deletions vllm_ascend/ops/moe/token_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ def token_combine(self,
hidden_states: torch.Tensor,
bias: torch.Tensor = None):
assert self.original_shape is not None

self.expanded_row_idx = torch.abs(self.expanded_row_idx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

Using torch.abs() on indices can be risky and might hide underlying issues. If self.expanded_row_idx contains negative values to signify special tokens (e.g., padding tokens with index -1), taking the absolute value will map them to valid indices (e.g., index 1), which could lead to data corruption by incorrectly mixing token data.

Could you please clarify the reason for expanded_row_idx containing negative values?

If negative values are indeed used for padding or invalid tokens, it would be safer to handle them with masking rather than torch.abs().

Additionally, it would be beneficial to add a unit test case with negative values in expanded_row_idx to verify that this change behaves as expected and prevents regressions.

final_hidden_states = torch_npu.npu_moe_token_unpermute(
permuted_tokens=hidden_states,
sorted_indices=self.expanded_row_idx,
Expand Down
Loading