vllm
vllm copied to clipboard
[Kernel] Layernorm performance optimization
This PR primarily creates optimized specializations of fused_add_rms_norm_kernel, used in many layernorms. It also includes a slightly optimized version of blockReduceSum/warpReduceSum which slightly reduce the number of shuffles done when the max block size is <=512 and known at compile time.
It is observed that fused_add_rms_norm is memory latency bound under many scenarios. The optimized implementation primarily derives its benefits by
- Coalescing global memory transactions into larger operations, which reduces the number of stalls that need to be hidden. This is achieved by (implicitly) unrolling both of the for loops through the use of a vector struct.
- Using a smaller block size when the number of blocks dispatched is large, which allows more blocks to simultaneously fit onto execution units and hence improves latency hiding.
The same ideas contained here can be applied to other relatively simple kernels which should be memory bound (e.g. some activation kernels).
More performance numbers can be provided as they become available or if requested. The existing test suite appears sufficient, but additional tests can be created on request.
Some examples of the speed up, as obtained by profiling via benchmark_latency on Llama2-70B (hidden size 8192), FP16, TP = 1, on MI300X:
- (input_len = output_len = batch_size = 128): Prefill improves to 305 us from 440 us.
- (input_len = 2048, output_len = 128, batch_size = 1): Prefill improves to 41 us from 88 us.
- For both cases above, decode improves to 7 us from 11 us.
Another optimization attempted was the use of shared memory, which effectively converts a global memory load into a shared memory load/store pair per item. While this improves performance when applied to baseline, it was not observed to improve performance on top of the current optimizations.
BEFORE SUBMITTING, PLEASE READ THE CHECKLIST BELOW AND FILL IN THE DESCRIPTION ABOVE
PR Checklist (Click to Expand)
Thank you for your contribution to vLLM! Before submitting the pull request, please ensure the PR meets the following criteria. This helps vLLM maintain the code quality and improve the efficiency of the review process.
PR Title and Classification
Only specific types of PRs will be reviewed. The PR title is prefixed appropriately to indicate the type of change. Please use one of the following:
[Bugfix]
for bug fixes.[CI/Build]
for build or continuous integration improvements.[Doc]
for documentation fixes and improvements.[Model]
for adding a new model or improving an existing model. Model name should appear in the title.[Frontend]
For changes on the vLLM frontend (e.g., OpenAI API server,LLM
class, etc.)[Kernel]
for changes affecting CUDA kernels or other compute kernels.[Core]
for changes in the core vLLM logic (e.g.,LLMEngine
,AsyncLLMEngine
,Scheduler
, etc.)[Hardware][Vendor]
for hardware-specific changes. Vendor name should appear in the prefix (e.g.,[Hardware][AMD]
).[Misc]
for PRs that do not fit the above categories. Please use this sparingly.
Note: If the PR spans more than one category, please include all relevant prefixes.
Code Quality
The PR need to meet the following code quality standards:
- We adhere to Google Python style guide and Google C++ style guide.
- Pass all linter checks. Please use
format.sh
to format your code. - The code need to be well-documented to ensure future contributors can easily understand the code.
- Include sufficient tests to ensure the project to stay correct and robust. This includes both unit tests and integration tests.
- Please add documentation to
docs/source/
if the PR modifies the user-facing behaviors of vLLM. It helps vLLM user understand and utilize the new features or changes.
Notes for Large Changes
Please keep the changes as concise as possible. For major architectural changes (>500 LOC excluding kernel/data/config/test), we would expect a GitHub issue (RFC) discussing the technical design and justification. Otherwise, we will tag it with rfc-required
and might not go through the PR.
What to Expect for the Reviews
The goal of the vLLM team is to be a transparent reviewing machine. We would like to make the review process transparent and efficient and make sure no contributor feel confused or frustrated. However, the vLLM team is small, so we need to prioritize some PRs over others. Here is what you can expect from the review process:
- After the PR is submitted, the PR will be assigned to a reviewer. Every reviewer will pick up the PRs based on their expertise and availability.
- After the PR is assigned, the reviewer will provide status update every 2-3 days. If the PR is not reviewed within 7 days, please feel free to ping the reviewer or the vLLM team.
- After the review, the reviewer will put an
action-required
label on the PR if there are changes required. The contributor should address the comments and ping the reviewer to re-review the PR. - Please respond to all comments within a reasonable time frame. If a comment isn't clear or you disagree with a suggestion, feel free to ask for clarification or discuss the suggestion.
Thank You
Finally, thank you for taking the time to read these guidelines and for your interest in contributing to vLLM. Your contributions make vLLM a great tool for everyone!
@mawong-amd Thanks for submitting the PR! This optimization seems to be necessary for MI300x GPUs.
Unfortunately, I didn't see noticeable e2e performance boost for A100 GPUs. Is this expected? Also, I'm a bit worried about whether the new kernels keep the semantics of the current kernels. Could you double check?
Hi, I managed to run a few performance tests on H100 last night and also observed that there was no speed up. I looked at the PTX and SASS assembly and NVCC was not fusing the loads/stores as expected. It appears NVCC needs to know these global memory ops are aligned on a 16 byte boundary to unlock the full 128-bit coalesced op; I've added this alignment requirement to the vector struct and now I'm observing similar speedups on H100.
Preliminary numbers I'm seeing on H100 are:
- (input_len = output_len = batch_size = 128): Prefill improves to 92 ms from 178 ms.
- (input_len = 2048, output_len = 128, batch_size = 1): Prefill improves to 45 ms from 84 ms.
- For both cases above, decode improves to 3 ms from 8 ms.
One "drawback" of this change is we can now only enable optimizations when the hidden_size is a multiple of 8 and the tensor pointers are aligned on a 16 byte boundary. But these conditions should be met essentially all the time.
As for the changed semantics, I'll discuss it in the relevant review comment thread.
Thanks!
Quick update on end-to-end runtime numbers. With the latest changes, I'm seeing small but observable improvements on H100.
Specifically, on the latency benchmark (50 iters on each test):
- (input_len = output_len = batch_size = 128): Improves to 11.463s from 11.658s. [1.7% improvement]
- (input_len = 2048, output_len = 128, batch_size = 1): Improves to 4.261s from 4.362s. [2.3% improvement]
I realized that this pr breaks cuda 11.8 support because of the usage of __half2
etc.
I realized that this pr breaks cuda 11.8 support because of the usage of
__half2
etc.
I think we can hotfix in a define guard to enable these optimizations only when the cuda version is > 11.8. Let me prepare a diff that does that.
EDIT: Hotfix created as the following PR https://github.com/vllm-project/vllm/pull/3782
@mawong-amd Can you send a PR to land that patch?