Repository navigation
[GG] fix MLA query BMM cuBLAS read-ahead without query copies #173
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
Merged
lukealonso
merged 3 commits into
dev/gilded-gnosis
from
fix/gg-mla-query-bmm-pedantic-standalone-20260723
Jul 24, 2026
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| #include <torch/csrc/stable/library.h> | ||
| #include <torch/csrc/stable/tensor.h> | ||
| #include <torch/headeronly/core/ScalarType.h> | ||
|
|
||
| #include "core/registration.h" | ||
| #include "libtorch_stable/torch_utils.h" | ||
|
|
||
| #include <cublas_v2.h> | ||
| #include <cuda_bf16.h> | ||
|
|
||
| #include <limits> | ||
| #include <sstream> | ||
|
|
||
| namespace { | ||
|
|
||
| void check_cublas(cublasStatus_t status, const char* operation) { | ||
| if (status == CUBLAS_STATUS_SUCCESS) { | ||
| return; | ||
| } | ||
| std::ostringstream error; | ||
| error << operation << " failed with cuBLAS status " | ||
| << static_cast<int>(status); | ||
| STD_TORCH_CHECK(false, error.str()); | ||
| } | ||
|
|
||
| int checked_int(int64_t value, const char* name) { | ||
| STD_TORCH_CHECK(value > 0 && value <= std::numeric_limits<int>::max(), name, | ||
| " is outside cuBLAS int range: ", value); | ||
| return static_cast<int>(value); | ||
| } | ||
|
|
||
| void check_bf16_cuda_3d(torch::stable::Tensor const& tensor, | ||
| const char* name) { | ||
| STD_TORCH_CHECK(tensor.device().is_cuda(), name, " must be on CUDA"); | ||
| STD_TORCH_CHECK(tensor.dim() == 3, name, " must be a 3D tensor"); | ||
| STD_TORCH_CHECK( | ||
| tensor.scalar_type() == torch::headeronly::ScalarType::BFloat16, name, | ||
| " must be BF16"); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| void safe_mla_query_bmm(torch::stable::Tensor const& query, | ||
| torch::stable::Tensor const& weight, | ||
| torch::stable::Tensor& output) { | ||
| check_bf16_cuda_3d(query, "query"); | ||
| check_bf16_cuda_3d(weight, "weight"); | ||
| check_bf16_cuda_3d(output, "output"); | ||
| STD_TORCH_CHECK(query.get_device_index() == weight.get_device_index() && | ||
| query.get_device_index() == output.get_device_index(), | ||
| "query, weight, and output must be on the same CUDA device"); | ||
|
|
||
| const int64_t heads_i64 = query.size(0); | ||
| const int64_t tokens_i64 = query.size(1); | ||
| const int64_t q_dim_i64 = query.size(2); | ||
| const int64_t latent_dim_i64 = weight.size(2); | ||
|
|
||
| STD_TORCH_CHECK(weight.size(0) == heads_i64 && weight.size(1) == q_dim_i64, | ||
| "weight must have shape [heads, q_dim, latent_dim]"); | ||
| STD_TORCH_CHECK(output.size(0) == heads_i64 && | ||
| output.size(1) == tokens_i64 && | ||
| output.size(2) == latent_dim_i64, | ||
| "output must have shape [heads, tokens, latent_dim]"); | ||
| STD_TORCH_CHECK(query.stride(2) == 1, | ||
| "query q_dim must be contiguous for safe_mla_query_bmm"); | ||
| STD_TORCH_CHECK(weight.stride(2) == 1, | ||
| "weight latent_dim must be contiguous for safe_mla_query_bmm"); | ||
| STD_TORCH_CHECK(output.stride(2) == 1, | ||
| "output latent_dim must be contiguous for safe_mla_query_bmm"); | ||
|
|
||
| const int heads = checked_int(heads_i64, "heads"); | ||
| const int tokens = checked_int(tokens_i64, "tokens"); | ||
| const int q_dim = checked_int(q_dim_i64, "q_dim"); | ||
| const int latent_dim = checked_int(latent_dim_i64, "latent_dim"); | ||
| const int query_ld = checked_int(query.stride(1), "query.stride(1)"); | ||
| const int weight_ld = checked_int(weight.stride(1), "weight.stride(1)"); | ||
| const int output_ld = checked_int(output.stride(1), "output.stride(1)"); | ||
|
|
||
| const torch::stable::accelerator::DeviceGuard device_guard( | ||
| query.get_device_index()); | ||
| cublasHandle_t handle = get_current_cuda_blas_handle(); | ||
| check_cublas(cublasSetStream(handle, get_current_cuda_stream( | ||
| query.get_device_index())), | ||
| "cublasSetStream"); | ||
|
|
||
| const float alpha = 1.0f; | ||
| const float beta = 0.0f; | ||
| check_cublas( | ||
| cublasGemmStridedBatchedEx( | ||
| handle, CUBLAS_OP_N, CUBLAS_OP_N, latent_dim, tokens, q_dim, &alpha, | ||
| weight.const_data_ptr(), CUDA_R_16BF, weight_ld, | ||
| static_cast<long long>(weight.stride(0)), query.const_data_ptr(), | ||
| CUDA_R_16BF, query_ld, static_cast<long long>(query.stride(0)), | ||
| &beta, output.mutable_data_ptr(), CUDA_R_16BF, output_ld, | ||
| static_cast<long long>(output.stride(0)), heads, | ||
| CUBLAS_COMPUTE_32F_PEDANTIC, CUBLAS_GEMM_DEFAULT), | ||
| "cublasGemmStridedBatchedEx"); | ||
| } | ||
|
|
||
| STABLE_TORCH_LIBRARY_IMPL(_C, CUDA, m) { | ||
| m.impl("safe_mla_query_bmm", TORCH_BOX(&safe_mla_query_bmm)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA required") | ||
|
|
||
|
|
||
| def _require_safe_mla_query_bmm(): | ||
| if not hasattr(torch.ops._C, "safe_mla_query_bmm"): | ||
| pytest.skip("safe_mla_query_bmm is not built") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("heads,tokens", [(8, 1), (8, 6), (8, 11), (11, 6), (16, 6)]) | ||
| def test_safe_mla_query_bmm_matches_torch_bmm(heads: int, tokens: int): | ||
| _require_safe_mla_query_bmm() | ||
| device = torch.device("cuda") | ||
| q_dim = 512 | ||
| rope_dim = 64 | ||
| latent_dim = 512 | ||
| torch.manual_seed(0) | ||
|
|
||
| query_storage = torch.randn( | ||
| tokens, heads, q_dim + rope_dim, dtype=torch.bfloat16, device=device | ||
| ) | ||
| query = query_storage[..., :q_dim].transpose(0, 1) | ||
| weight = torch.randn(heads, q_dim, latent_dim, dtype=torch.bfloat16, device=device) | ||
| output = torch.empty(heads, tokens, latent_dim, dtype=torch.bfloat16, device=device) | ||
|
|
||
| assert not query.is_contiguous() | ||
| torch.ops._C.safe_mla_query_bmm(query, weight, output) | ||
| expected = torch.bmm(query.contiguous(), weight) | ||
|
|
||
| torch.testing.assert_close(output.float(), expected.float(), rtol=5e-2, atol=5e-2) | ||
|
|
||
|
|
||
| def test_safe_mla_query_bmm_cuda_graph_replay(): | ||
| _require_safe_mla_query_bmm() | ||
| device = torch.device("cuda") | ||
| heads = 8 | ||
| tokens = 6 | ||
| q_dim = 512 | ||
| latent_dim = 512 | ||
| torch.manual_seed(1) | ||
|
|
||
| query_storage = torch.randn( | ||
| tokens, heads, q_dim + 64, dtype=torch.bfloat16, device=device | ||
| ) | ||
| query = query_storage[..., :q_dim].transpose(0, 1) | ||
| weight = torch.randn(heads, q_dim, latent_dim, dtype=torch.bfloat16, device=device) | ||
| output = torch.empty(heads, tokens, latent_dim, dtype=torch.bfloat16, device=device) | ||
|
|
||
| stream = torch.cuda.Stream() | ||
| stream.wait_stream(torch.cuda.current_stream()) | ||
| with torch.cuda.stream(stream): | ||
| torch.ops._C.safe_mla_query_bmm(query, weight, output) | ||
| torch.cuda.current_stream().wait_stream(stream) | ||
|
|
||
| graph = torch.cuda.CUDAGraph() | ||
| with torch.cuda.graph(graph): | ||
| torch.ops._C.safe_mla_query_bmm(query, weight, output) | ||
|
|
||
| graph.replay() | ||
| graph.replay() | ||
| expected = torch.bmm(query.contiguous(), weight) | ||
|
|
||
| torch.testing.assert_close(output.float(), expected.float(), rtol=5e-2, atol=5e-2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.