[model_runner_v2]optimize the performance of the _topk_log_softmax_kernel#7221
[model_runner_v2]optimize the performance of the _topk_log_softmax_kernel#7221wangxiyuan merged 6 commits intovllm-project:mainfrom
Conversation
…rnel Triton operator in model_runner_v2 to 7% of its original value. Signed-off-by: wangx700 <wangxin700@huawei.com>
Signed-off-by: wangx700 <wangxin700@huawei.com>
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the performance of log probability calculations within the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces an optimized Triton kernel _topk_log_softmax_kernel for calculating log probabilities, claiming a significant performance improvement. It also adds a new test to verify the kernel's correctness. My review includes suggestions to further improve the kernel's efficiency and to enhance the new test's implementation for better performance and readability. Additionally, I've provided an updated PR title and summary to align with the repository's contribution guidelines.
Suggested PR Title:
[Ops][Perf] Optimize the performance of _topk_log_softmax_kernelSuggested PR Summary:
### What this PR does / why we need it?
This PR optimizes the `_topk_log_softmax_kernel` Triton kernel, which is used for calculating log probabilities for specified tokens. The key improvements are within the kernel's implementation of the log-softmax operation, which are intended to reduce execution time significantly.
A new test is also added to verify the correctness of the optimized kernel by comparing its output against the standard PyTorch `log_softmax` implementation.
### Does this PR introduce _any_ user-facing change?
No, this is a backend performance optimization and does not introduce any user-facing changes.
### How was this patch tested?
A new test, `test_topk_log_softmax_kernel`, has been added to `tests/e2e/nightly/single_node/ops/singlecard_ops/triton/test_log_softmax.py`. This test covers various configurations of batch size, vocabulary size, and number of logprobs. It validates the Triton kernel's output against a reference PyTorch implementation, ensuring correctness.| for i in range(0, vocab_size, BLOCK_SIZE): | ||
| block = i + tl.arange(0, BLOCK_SIZE) | ||
| logits = tl.load(row_ptr + block, mask=block < vocab_size, other=0.0) | ||
| # NOTE(woosuk): Make sure that logits and all following operations use FP32. | ||
| logits = logits.to(tl.float32) | ||
| block = block.to(tl.float32) | ||
| e = tl.exp(logits - max_val) | ||
| e = tl.where(block < vocab_size, e, 0.0) | ||
| se += tl.sum(e) |
There was a problem hiding this comment.
The loop for calculating the sum of exponentials (se) can be made more efficient and readable. Currently, it loads values with other=0.0, computes exp, and then uses tl.where to mask out-of-bound elements. This performs unnecessary computations.
A better approach is to load with other=float("-inf"). This way, tl.exp on out-of-bound elements will naturally result in 0.0, removing the need for an explicit tl.where call. Additionally, the type conversion block.to(tl.float32) is unnecessary as block is only used for masking and pointer arithmetic.
| for i in range(0, vocab_size, BLOCK_SIZE): | |
| block = i + tl.arange(0, BLOCK_SIZE) | |
| logits = tl.load(row_ptr + block, mask=block < vocab_size, other=0.0) | |
| # NOTE(woosuk): Make sure that logits and all following operations use FP32. | |
| logits = logits.to(tl.float32) | |
| block = block.to(tl.float32) | |
| e = tl.exp(logits - max_val) | |
| e = tl.where(block < vocab_size, e, 0.0) | |
| se += tl.sum(e) | |
| for i in range(0, vocab_size, BLOCK_SIZE): | |
| block = i + tl.arange(0, BLOCK_SIZE) | |
| logits = tl.load(row_ptr + block, mask=block < vocab_size, other=float("-inf")) | |
| # NOTE(woosuk): Make sure that logits and all following operations use FP32. | |
| logits = logits.to(tl.float32) | |
| e = tl.exp(logits - max_val) | |
| se += tl.sum(e) |
| ref_output = torch.zeros_like(triton_output) | ||
| for i in range(batch_size): | ||
| for j in range(num_logprobs): | ||
| token_id = token_ids[i, j] | ||
| ref_output[i, j] = torch_logprobs[i, token_id] |
There was a problem hiding this comment.
The reference output calculation uses nested Python loops, which is inefficient and less readable than a vectorized approach. You can achieve the same result more efficiently and concisely by using torch.gather.
| ref_output = torch.zeros_like(triton_output) | |
| for i in range(batch_size): | |
| for j in range(num_logprobs): | |
| token_id = token_ids[i, j] | |
| ref_output[i, j] = torch_logprobs[i, token_id] | |
| ref_output = torch.gather(torch_logprobs, 1, token_ids) |
|
👋 Hi! Thank you for contributing to the vLLM Ascend project. The following points will speed up your PR merge:
If CI fails, you can run linting and testing checks locally according Contributing and Testing. |
Signed-off-by: wangx700 <wangxin700@huawei.com>
Signed-off-by: wangx700 <wangxin700@huawei.com>
|
please refer to #5208. |
| logits = logits.to(tl.float32) | ||
| block = block.to(tl.float32) | ||
| e = tl.exp(logits - max_val) | ||
| e = tl.where(block < vocab_size, e, 0.0) |
There was a problem hiding this comment.
add some comments to explain the difference between vllm and vllm-ascend.
There was a problem hiding this comment.
ok,I added some notes to explain the difference between vllm and vllm-ascend.
Signed-off-by: wangx700 <wangxin700@huawei.com>
Signed-off-by: wangx700 <wangxin700@huawei.com>
…rnel (vllm-project#7221) ### What this PR does / why we need it? Optimize the performance of the triton operator _topk_log_softmax_kernel in model_runner_v2 to 1.04xH100,which is 7% of its original value.(issue vllm-project#5208) - vLLM version: v0.16.0 - vLLM main: vllm-project/vllm@4034c3d --------- Signed-off-by: wangx700 <wangxin700@huawei.com>
…rnel (vllm-project#7221) ### What this PR does / why we need it? Optimize the performance of the triton operator _topk_log_softmax_kernel in model_runner_v2 to 1.04xH100,which is 7% of its original value.(issue vllm-project#5208) - vLLM version: v0.16.0 - vLLM main: vllm-project/vllm@4034c3d --------- Signed-off-by: wangx700 <wangxin700@huawei.com> Signed-off-by: xutianyi <xutianyi5@huawei.com>
…scend into qwen3next_graph * 'qwen3next_graph' of https://github.com/845473182/vllm-ascend: (62 commits) [doc] Refresh the documentation for DeepSeek-V3.2 (vllm-project#7403) [bugfix][accuracy] Fix ds indexer accuracy problem caused by k rope (vllm-project#7341) [P/D] LayerwiseConnector supports the virtual push functionality on node D. (vllm-project#7361) [CI] Add PAT_TOKEN when checkout (vllm-project#7400) [main2main] upgrade vllm to 0308 (vllm-project#7213) [CI] add scheduled stale issue management (vllm-project#7354) [CI] expand issue labeler rules for feature/model triage (vllm-project#7356) [Bugfix] Assertion error when decode prefix cache fully hits (vllm-project#7236) [doc] Refresh the documentation for GLM-4.7 (vllm-project#7292) [BugFix]A2 MOE method&& layerwise MTP bugfix && Mamba gdn_metadata bugfix (vllm-project#7364) [doc] Upload doc for qwen3.5-27B and qwen3.5-397B-A17B on Ascend (vllm-project#7313) [bugfix]Enable dispatch_ffn_combine feature for qwen3.5 (vllm-project#7066) [bugfix] fix unzip file path for fia operator (vllm-project#7367) [Perf] Optimize bias handling in AscendRMSNorm (vllm-project#7226) [eagle3][pcp] fix bug for eagle3 and cp enable (vllm-project#7309) [Bugfix] fix TransposeKvCacheByBlock op error report in plog (vllm-project#7235) [Feature]Supports DSv3.1 PD separation and C8 quantization (vllm-project#7222) [main][bugfix] Fixed the problem that eagle3 will crash in FULL_DECODE_ONLY (vllm-project#7290) [xlite][Bugfix] Support mrope and deepstack features in xlite backend (vllm-project#7295) [model_runner_v2]optimize the performance of the _topk_log_softmax_kernel (vllm-project#7221) ...
- Remove is_skipped flag from tests/e2e/singlecard/model_runner_v2/test_basic.py - Test was originally skipped due to get_cuda_view_from_cpu_tensor error (vllm-project#5752) - Recent model_runner_v2 improvements may have resolved the issue: - vllm-project#7110: Added aclgraph support - vllm-project#7496: Optimized post_update performance - vllm-project#7221: Optimized _topk_log_softmax_kernel performance - CI will verify if the test now passes successfully Signed-off-by: hejianping <hejianping7@huawei.com>
- Remove is_skipped flag from tests/e2e/singlecard/model_runner_v2/test_basic.py - Test was originally skipped due to get_cuda_view_from_cpu_tensor error (vllm-project#5752) - Recent model_runner_v2 improvements may have resolved the issue: - vllm-project#7110: Added aclgraph support - vllm-project#7496: Optimized post_update performance - vllm-project#7221: Optimized _topk_log_softmax_kernel performance - CI will verify if test now passes successfully Signed-off-by: hejianping <hejianping7@huawei.com>
- Remove is_skipped flag from tests/e2e/singlecard/model_runner_v2/test_basic.py - Test was originally skipped due to get_cuda_view_from_cpu_tensor error (vllm-project#5752) - Recent model_runner_v2 improvements may have resolved the issue: - vllm-project#7110: Added aclgraph support - vllm-project#7496: Optimized post_update performance - vllm-project#7221: Optimized _topk_log_softmax_kernel performance - CI will verify if the test now passes successfully Signed-off-by: hejianping <hejianping7@huawei.com>
…rnel (vllm-project#7221) ### What this PR does / why we need it? Optimize the performance of the triton operator _topk_log_softmax_kernel in model_runner_v2 to 1.04xH100,which is 7% of its original value.(issue vllm-project#5208) - vLLM version: v0.16.0 - vLLM main: vllm-project/vllm@4034c3d --------- Signed-off-by: wangx700 <wangxin700@huawei.com>
What this PR does / why we need it?
Optimize the performance of the triton operator _topk_log_softmax_kernel in model_runner_v2 to 1.04xH100,which is 7% of its original value.(issue #5208)
Does this PR introduce any user-facing change?
How was this patch tested?