-
Notifications
You must be signed in to change notification settings - Fork 208
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
feat: implement SM-Constrained GEMM API #744
Open
lanchongyizu
wants to merge
1
commit into
flashinfer-ai:main
Choose a base branch
from
lanchongyizu:sm_constrained_gemm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+162
−16
Open
Changes from all commits
Commits
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 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 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,27 @@ | ||
/* | ||
* Copyright (c) 2025 by FlashInfer team. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#include "pytorch_extension_utils.h" | ||
|
||
void CutlassSegmentGEMMSM90(at::Tensor float_workspace_buffer, at::Tensor int_workspace_buffer, | ||
at::Tensor all_problems, at::Tensor x_ptr, at::Tensor w_ptr, | ||
at::Tensor y_ptr, at::Tensor x_stride, at::Tensor weight_stride, | ||
at::Tensor y_stride, at::Tensor empty_x_data, bool weight_column_major, | ||
std::vector<int64_t> plan_info_vec, int64_t cuda_stream); | ||
|
||
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { | ||
m.def("cutlass_segment_gemm_sm90", &CutlassSegmentGEMMSM90, | ||
"Cutlass Segment GEMM operator for SM90"); | ||
} |
This file contains 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 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 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 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 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 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 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 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,64 @@ | ||
/* | ||
* Copyright (c) 2025 by FlashInfer team. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#ifndef FLASHINFER_GEMM_SCHEDULER_CUH_ | ||
#define FLASHINFER_GEMM_SCHEDULER_CUH_ | ||
|
||
#include <cuda_runtime_api.h> | ||
|
||
#include <algorithm> | ||
#include <cstddef> | ||
#include <cstdint> | ||
#include <sstream> | ||
#include <vector> | ||
|
||
#include "../utils.cuh" | ||
|
||
namespace flashinfer { | ||
|
||
struct GemmPlanInfo { | ||
int64_t num_ctas; | ||
|
||
GemmPlanInfo() : num_ctas(0) {} | ||
|
||
// convert GemmPlanInfo to std::vector<int64_t> | ||
std::vector<int64_t> ToVector() const { return {num_ctas}; } | ||
|
||
// From std::vector<int64_t> to GemmPlanInfo | ||
void FromVector(const std::vector<int64_t>& vec) { | ||
if (vec.size() != 1) { | ||
std::ostringstream err_msg; | ||
err_msg << "GemmPlanInfo::FromVector: vec.size() should be 1, but got " << vec.size(); | ||
FLASHINFER_ERROR(err_msg.str()); | ||
} | ||
num_ctas = vec[0]; | ||
} | ||
}; | ||
|
||
inline cudaError_t GemmPlan(uint32_t num_ctas, GemmPlanInfo& plan_info) { | ||
int dev_id = 0; | ||
int num_sms = 0; | ||
FLASHINFER_CUDA_CALL(cudaGetDevice(&dev_id)); | ||
FLASHINFER_CUDA_CALL(cudaDeviceGetAttribute(&num_sms, cudaDevAttrMultiProcessorCount, dev_id)); | ||
if (num_ctas > 0 && num_ctas < num_sms) { | ||
plan_info.num_ctas = num_ctas; | ||
} else { | ||
plan_info.num_ctas = num_sms; | ||
} | ||
return cudaSuccess; | ||
} | ||
|
||
} // namespace flashinfer | ||
#endif // FLASHINFER_GEMM_SCHEDULER_CUH_ |
This file contains 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 |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
@pytest.mark.parametrize("dtype", DTYPES) | ||
@pytest.mark.parametrize("device", CUDA_DEVICES) | ||
@pytest.mark.parametrize("backend", ["auto", "sm90", "sm80"]) | ||
@pytest.mark.parametrize("num_ctas", [0, 4, 16, 64]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the expected behavior of |
||
def test_segment_gemm( | ||
batch_size, | ||
num_rows_per_batch, | ||
|
@@ -43,6 +44,7 @@ def test_segment_gemm( | |
dtype, | ||
device, | ||
backend, | ||
num_ctas, | ||
): | ||
if batch_size * num_rows_per_batch > 8192: | ||
pytest.skip("batch_size * num_rows_per_batch too large for test.") | ||
|
@@ -64,6 +66,7 @@ def test_segment_gemm( | |
weight = torch.randn(batch_size, d_out, d_in, dtype=dtype).to(device) | ||
else: | ||
weight = torch.randn(batch_size, d_in, d_out, dtype=dtype).to(device) | ||
segment_gemm.plan(num_ctas) | ||
y = segment_gemm.run( | ||
x, | ||
weight, | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried nsys profiler and it turns out this value can't control the number of SMs this kernel used.
A more fundamental approach might be using green context.