Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Kernel] Adding fused bias add to cutlass_scaled_mm_dq kernel #5390

Closed
wants to merge 3 commits into from

Conversation

cyang49
Copy link
Contributor

@cyang49 cyang49 commented Jun 10, 2024

This PR adds support of bias add to cutlass_scaled_mm_dq op, which is required for optimized performance of W8A8 linear layer with bias.

I added the support for fp8, but not the int8 path yet, as my use case is fp8 only.

Microbenchmarking results show that it performs similarly to cutlass_scaled_mm_dq. Times are in microseconds (us). Columns represent different m (batch_size*seq_len). The kernels being compared are:

  • torch_mm:f16 : fp16 pytorch (using cublas)
  • cutlass_scaled_mm_bias0:f8e4: original cutlass_scaled_mm_dq fp8 without bias add support
  • cutlass_scaled_mm_bias1:f8e4: original cutlass_scaled_mm_dq fp8 with bias add support

cc @njhill


[-------------------------------------------------------- llama-3-8b_n=4096_k=4096 --------------------------------------------------------]
                                           |   1    |   2    |   4    |   8    |   16   |   32   |   64   |  128   |  256   |  512   |  1024
1 threads: ---------------------------------------------------------------------------------------------------------------------------------
      torch_mm:f16                         |  11.5  |  11.7  |  11.6  |  11.8  |  11.7  |  11.7  |  14.4  |  16.6  |  24.9  |  35.6  |  55.0
      cutlass_scaled_mm_bias0:f8e4         |   8.0  |   8.0  |   8.0  |   8.0  |   8.1  |   8.2  |   8.0  |  10.0  |  14.6  |  16.0  |  30.8
      cutlass_scaled_mm_bias1:f8e4         |   8.3  |   8.3  |   8.3  |   8.3  |   8.3  |   8.4  |   8.3  |  10.2  |  14.9  |  16.3  |  31.2

[-------------------------------------------------------- llama-3-8b_n=4096_k=6144 --------------------------------------------------------]
                                           |   1    |   2    |   4    |   8    |   16   |   32   |   64   |  128   |  256   |  512   |  1024
1 threads: ---------------------------------------------------------------------------------------------------------------------------------
      torch_mm:f16                         |  24.9  |  25.6  |  25.3  |  26.1  |  26.0  |  25.0  |  25.9  |  29.7  |  34.0  |  52.1  |  83.6
      cutlass_scaled_mm_bias0:f8e4         |   9.8  |   9.8  |  13.8  |   9.8  |   9.8  |   9.9  |   9.8  |  12.8  |  19.4  |  23.5  |  45.9
      cutlass_scaled_mm_bias1:f8e4         |  10.2  |  10.1  |  10.1  |  10.2  |  10.2  |  10.2  |  10.2  |  13.1  |  19.7  |  23.6  |  46.2

[------------------------------------------------------------ llama-3-8b_n=4096_k=28672 ------------------------------------------------------------]
                                           |    1    |    2    |    4    |    8    |    16   |   32   |   64   |   128   |   256   |   512   |   1024
1 threads: ------------------------------------------------------------------------------------------------------------------------------------------
      torch_mm:f16                         |   95.2  |   85.0  |   84.7  |   85.7  |   85.9  |  88.8  |  89.8  |  100.4  |  140.2  |  202.3  |  370.8
      cutlass_scaled_mm_bias0:f8e4         |   44.8  |   44.8  |   44.9  |   45.0  |   45.2  |  45.5  |  46.0  |   54.3  |   76.6  |   98.1  |  190.6
      cutlass_scaled_mm_bias1:f8e4         |   45.1  |   45.1  |   45.1  |   45.3  |   45.4  |  45.7  |  46.2  |   54.7  |   76.8  |   97.8  |  190.1

[-------------------------------------------------------- llama-3-8b_n=14336_k=4096 ---------------------------------------------------------]
                                           |   1    |   2    |   4    |   8    |   16   |   32   |   64   |  128   |  256   |   512   |   1024
1 threads: -----------------------------------------------------------------------------------------------------------------------------------
      torch_mm:f16                         |  45.3  |  45.4  |  45.6  |  45.7  |  48.0  |  48.5  |  46.6  |  48.8  |  59.3  |  124.2  |  199.9
      cutlass_scaled_mm_bias0:f8e4         |  24.6  |  24.8  |  25.2  |  25.2  |  25.7  |  25.7  |  25.8  |  27.0  |  33.9  |   51.8  |   98.5
      cutlass_scaled_mm_bias1:f8e4         |  25.0  |  25.0  |  25.5  |  25.5  |  25.9  |  25.9  |  26.0  |  27.3  |  34.3  |   52.8  |   98.8

It would be helpful for the vLLM team to provide feedback on getting this PR accepted. Thanks!


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!

@robertgshaw2-neuralmagic
Copy link
Collaborator

// Hopper
cutlass_scaled_mm_dq_bias_sm90(c, a, b, a_scales, b_scales, bias);
} else {
assert(0 && "cutlass_scaled_mm_dq_bias only supports Hopper for now");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you please support Ada as well for FP8? Your test seems like it would be running for Ada

// KernelSchedule, EpilogueSchedule>>(
// out, a, b, a_scales, b_scales);
// }
assert(0 && "kInt8 not supported in cutlass_scaled_mm_dq_bias yet");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use TORCH_CHECK with a message instead of assert

// Hopper
cutlass_scaled_mm_dq_bias_sm90(c, a, b, a_scales, b_scales, bias);
} else {
assert(0 && "cutlass_scaled_mm_dq_bias only supports Hopper for now");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use TORCH_CHECK with a message instead of assert

@tlrmchlsmth
Copy link
Collaborator

Hi @cyang49,

Thank you for your contribution. Could you take a look at #5391? I'm hoping we can develop the CUTLASS kernels in a way that minimizes code duplication, while reusing the same hyperparameters and base algorithms across various epilogues.

@cyang49
Copy link
Contributor Author

cyang49 commented Jun 10, 2024

Hi @cyang49,

Thank you for your contribution. Could you take a look at #5391? I'm hoping we can develop the CUTLASS kernels in a way that minimizes code duplication, while reusing the same hyperparameters and base algorithms across various epilogues.

Yeah, I agree that's the better way of doing things. This PR is a quick and slightly hacky way to test out the bias-add fusion. @tlrmchlsmth do you think it's better to add the bias add after merging #5391 ?

@tlrmchlsmth
Copy link
Collaborator

Yeah, hopefully we can get #5391 landed quickly so as to not block this one :)

CMakeLists.txt Outdated Show resolved Hide resolved
csrc/ops.h Outdated Show resolved Hide resolved
vllm/_custom_ops.py Outdated Show resolved Hide resolved
@cyang49
Copy link
Contributor Author

cyang49 commented Jun 11, 2024

@tlrmchlsmth

When #5391 is merged, would it make more sense for me to modify the C++ function signature of cutlass_scaled_mm to include bias, and add if/else condition for calling kernels with/without bias fusion? That should remove some redundancy at C++ level.

@tlrmchlsmth
Copy link
Collaborator

When #5391 is merged, would it make more sense for me to modify the C++ function signature of cutlass_scaled_mm to include bias, and add if/else condition for calling kernels with/without bias fusion? That should remove some redundancy at C++ level.

Yeah, I think that's a great idea. I'm supportive of pushing the dispatching for bias vs no bias down to lower levels. In particular, I think we should try to do it at a lower level than cutlass_scaled_mm_sm90_fp8_dispatch so that we use the same code that defines the hyperparameters for all different epilogues. This will really help to keep things sane.

@cyang49
Copy link
Contributor Author

cyang49 commented Jun 14, 2024

Rebased to the new refactored scaled_mm code and got to a working version. More refactoring to be done with the cutlass kernel code.

@ProExpertProg
Copy link
Contributor

Hey @cyang49, I've actually been working on adding bias/zero-point support to the epilogues for CUTLASS 2.x as well! I just posted the PR: #5560. I took some inspiration from yours as well (specifically using the multiply_add directly instead of 2 separate ops). I'd be happy to work together to get them both standardized and feel free to use the structure I used to avoid a bunch of code duplication. And I'll defer to @robertgshaw2-neuralmagic and @tlrmchlsmth for the final decision on how these should both look like but happy to give you a review once you finish your refactoring to incorporate the new epilogue stuff.

@cyang49
Copy link
Contributor Author

cyang49 commented Jun 15, 2024

Hey @cyang49, I've actually been working on adding bias/zero-point support to the epilogues for CUTLASS 2.x as well! I just posted the PR: #5560. I took some inspiration from yours as well (specifically using the multiply_add directly instead of 2 separate ops). I'd be happy to work together to get them both standardized and feel free to use the structure I used to avoid a bunch of code duplication. And I'll defer to @robertgshaw2-neuralmagic and @tlrmchlsmth for the final decision on how these should both look like but happy to give you a review once you finish your refactoring to incorporate the new epilogue stuff.

@ProExpertProg thanks, I will take a look. There are a few other things at work I need to attend to early next week. It's likely that #5660 will be merged when I get back to finalizing this.

@ProExpertProg
Copy link
Contributor

Okay thanks for letting me know, I'll go ahead with mine first then and then help you land this one once you get around to it

@cyang49
Copy link
Contributor Author

cyang49 commented Jun 18, 2024

Closing in favor of #5560

@cyang49 cyang49 closed this Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants