Skip to content

Conversation

cyang49
Copy link
Contributor

@cyang49 cyang49 commented Aug 15, 2025

…oE (#20762)

Purpose

Re-apply #20762 with changes needed for the latest main. The bug that was triggered by the original PR was fixed in #21426

Test Plan

Need some suggestions for tests. Might need to run with Maverick

Test Result

(Optional) Documentation Update


Essential Elements of an Effective PR Description Checklist
  • The purpose of the PR, such as "Fix some issue (link existing issues this PR will resolve)".
  • The test plan, such as providing test command.
  • The test results, such as pasting the results comparison before and after, or e2e results
  • (Optional) The necessary documentation update, such as updating supported_models.md and examples for a new model.

…oE (vllm-project#20762)

Signed-off-by: ElizaWszola <[email protected]>
Co-authored-by: Chih-Chieh-Yang <[email protected]>
Signed-off-by: Chih-Chieh-Yang <[email protected]>
Copy link

👋 Hi! Thank you for contributing to the vLLM project.

💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels.

Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your fastcheck build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping simon-mo or khluu to add you in our Buildkite org.

Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can either: Add ready label to the PR or enable auto-merge.

🚀

@mergify mergify bot added the performance Performance-related issues label Aug 15, 2025
Copy link

mergify bot commented Aug 15, 2025

This pull request has merge conflicts that must be resolved before it can be
merged. Please rebase the PR, @cyang49.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify bot added the needs-rebase label Aug 15, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request reapplies performance improvements for non-blockwise FP8 CUTLASS MoE by pre-computing stride tensors and introducing a custom CUDA kernel for permutation. The changes are well-structured and align with the performance goals. However, I've identified a critical issue in the new shuffle_rows CUDA operation within csrc/moe/moe_permute_unpermute_op.cu. The alignment check is incorrect, which could lead to memory access errors. I've also suggested a refactoring to reduce code duplication in the same function.

Comment on lines +197 to +214
if (num_cols % (128 / sizeof(input_tensor.scalar_type()) / 8)) {
// use slow kernel if num_cols can't be aligned to 128 bits
MOE_DISPATCH(input_tensor.scalar_type(), [&] {
shuffleInputRowsKernelSlow<scalar_t><<<blocks, threads, 0, stream>>>(
reinterpret_cast<scalar_t*>(input_tensor.data_ptr()),
dst2src_map.data_ptr<int32_t>(),
reinterpret_cast<scalar_t*>(output_tensor.data_ptr()), num_src_rows,
num_dest_rows, num_cols);
});
} else {
MOE_DISPATCH(input_tensor.scalar_type(), [&] {
shuffleInputRowsKernel<scalar_t><<<blocks, threads, 0, stream>>>(
reinterpret_cast<scalar_t*>(input_tensor.data_ptr()),
dst2src_map.data_ptr<int32_t>(),
reinterpret_cast<scalar_t*>(output_tensor.data_ptr()), num_src_rows,
num_dest_rows, num_cols);
});
}
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

There are two issues in this block:

  1. Critical Bug: The expression sizeof(input_tensor.scalar_type()) is incorrect for getting the size of the tensor's elements. input_tensor.scalar_type() returns a c10::ScalarType enum, and sizeof on it will return the size of the enum type itself (e.g., 1 or 4 bytes), not the size of the data type it represents. This will lead to an incorrect alignment check, which could cause the fast kernel path to be taken for unaligned inputs, leading to memory access errors or incorrect results. The correct way to get the element size is by using c10::elementSize(input_tensor.scalar_type()) or, inside the MOE_DISPATCH macro, sizeof(scalar_t).

  2. Code Duplication: The MOE_DISPATCH call is duplicated in the if and else branches. This can be simplified by moving the if/else logic inside the MOE_DISPATCH lambda, which improves maintainability and reduces code duplication.

Here is a suggested change that addresses both issues:

  MOE_DISPATCH(input_tensor.scalar_type(), [&] {
    if (num_cols % (128 / sizeof(scalar_t) / 8)) {
      // use slow kernel if num_cols can't be aligned to 128 bits
      shuffleInputRowsKernelSlow<scalar_t><<<blocks, threads, 0, stream>>>(
          reinterpret_cast<scalar_t*>(input_tensor.data_ptr()),
          dst2src_map.data_ptr<int32_t>(),
          reinterpret_cast<scalar_t*>(output_tensor.data_ptr()), num_src_rows,
          num_dest_rows, num_cols);
    } else {
      shuffleInputRowsKernel<scalar_t><<<blocks, threads, 0, stream>>>(
          reinterpret_cast<scalar_t*>(input_tensor.data_ptr()),
          dst2src_map.data_ptr<int32_t>(),
          reinterpret_cast<scalar_t*>(output_tensor.data_ptr()), num_src_rows,
          num_dest_rows, num_cols);
    }
  });

@cyang49
Copy link
Contributor Author

cyang49 commented Aug 19, 2025

closing in favor of #23045

@cyang49 cyang49 closed this Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-rebase performance Performance-related issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants