-
Notifications
You must be signed in to change notification settings - Fork 7.5k
CUTLASS NVFP4 GEMM improvement of SM120 #21314
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
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
34812e3
more
b8zhong d887c0d
upd
b8zhong 1340169
review by bbuf
b8zhong d53dc61
fix accidentaly push
b8zhong 2a35489
Merge branch 'main' into brayden/fp4-sm120
b8zhong b38f8c6
upd
b8zhong 8b72a40
ziyi comment
b8zhong 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
102 changes: 102 additions & 0 deletions
102
python/sglang/jit_kernel/csrc/gemm/nvfp4/nvfp4_scaled_mm_common.cuh
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 @@ | ||
| /* Copyright 2026 SGLang Team. All Rights Reserved. | ||
|
|
||
| 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. | ||
| ==============================================================================*/ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <sgl_kernel/tensor.h> | ||
| #include <sgl_kernel/utils.h> | ||
|
|
||
| #include <sgl_kernel/runtime.cuh> | ||
| #include <sgl_kernel/utils.cuh> | ||
|
|
||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <cuda_runtime.h> | ||
| #include <unordered_map> | ||
|
|
||
| using namespace host; | ||
|
|
||
| // clang-format off | ||
| #include "cutlass/cutlass.h" | ||
| #include "cutlass/gemm/collective/collective_builder.hpp" | ||
| #include "cutlass/epilogue/collective/collective_builder.hpp" | ||
| #include "cutlass/gemm/device/gemm_universal_adapter.h" | ||
| #include "cutlass/gemm/kernel/gemm_universal.hpp" | ||
| #include "cutlass/util/packed_stride.hpp" | ||
| // clang-format on | ||
|
|
||
| #define CUTLASS_CHECK(status) \ | ||
| { \ | ||
| cutlass::Status error = status; \ | ||
| RuntimeCheck(error == cutlass::Status::kSuccess, cutlassGetStatusString(error)); \ | ||
| } | ||
|
|
||
| using namespace cute; | ||
|
|
||
| inline uint32_t next_pow_2(uint32_t x) noexcept { | ||
| if (x <= 1) return 1; | ||
| return 1u << (32 - __builtin_clz(x - 1)); | ||
| } | ||
|
|
||
| struct WorkspaceKey { | ||
| int device_id; | ||
| uintptr_t stream; | ||
| auto operator==(const WorkspaceKey&) const -> bool = default; | ||
| }; | ||
|
|
||
| struct WorkspaceKeyHash { | ||
| auto operator()(const WorkspaceKey& key) const -> size_t { | ||
| size_t h1 = std::hash<int>{}(key.device_id); | ||
| size_t h2 = std::hash<uintptr_t>{}(key.stream); | ||
| return h1 ^ (h2 + 0x9e3779b97f4a7c15ULL + (h1 << 6) + (h1 >> 2)); | ||
| } | ||
| }; | ||
|
|
||
| struct WorkspaceState { | ||
| void* ptr = nullptr; | ||
| size_t bytes = 0; | ||
| }; | ||
|
|
||
| inline auto get_cached_workspace(size_t required_bytes, int device_id, cudaStream_t stream) -> void* { | ||
| if (required_bytes == 0) { | ||
| return nullptr; | ||
| } | ||
|
|
||
| thread_local std::unordered_map<WorkspaceKey, WorkspaceState, WorkspaceKeyHash> cache; | ||
| WorkspaceKey key{device_id, reinterpret_cast<uintptr_t>(stream)}; | ||
| auto& ws = cache[key]; | ||
|
|
||
| if (ws.ptr != nullptr && ws.bytes >= required_bytes) { | ||
| return ws.ptr; | ||
| } | ||
|
|
||
| RuntimeDeviceCheck(cudaSetDevice(device_id)); | ||
| if (ws.ptr != nullptr) { | ||
| RuntimeDeviceCheck(cudaFreeAsync(ws.ptr, stream)); | ||
| ws.ptr = nullptr; | ||
| ws.bytes = 0; | ||
| } | ||
| RuntimeDeviceCheck(cudaMallocAsync(&ws.ptr, required_bytes, stream)); | ||
| ws.bytes = required_bytes; | ||
| return ws.ptr; | ||
| } | ||
|
|
||
| inline int getSMVersion(int device_id) { | ||
| int sm_major = 0; | ||
| int sm_minor = 0; | ||
| RuntimeDeviceCheck(cudaDeviceGetAttribute(&sm_major, cudaDevAttrComputeCapabilityMajor, device_id)); | ||
| RuntimeDeviceCheck(cudaDeviceGetAttribute(&sm_minor, cudaDevAttrComputeCapabilityMinor, device_id)); | ||
| return sm_major * 10 + sm_minor; | ||
| } | ||
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.
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.
@DarkSharpness It appears that the workspace required by the CUTLASS Kernel is requested by itself via
cudaMallocAsync.@b8zhong As far as I know, we may need to avoid such operations as much as possible; memory allocation should be done through PyTorch.
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 would recommend allocating the workspace from Python Side. As an alternative, you may also try some utility functions here for C++ allocation https://github.com/sgl-project/sglang/blob/f5c225eeba3c6f027e5e7691784249d6a667026f/python/sglang/jit_kernel/include/sgl_kernel/ffi.h
Uh oh!
There was an error while loading. Please reload this page.
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 see. Could I use
ffi::empty? Because, otherwise we need to bring the workspace size up to Python level (it could be useful when doing autotuning infra I think but is a bit more refactor required), this way it should still be using Torch allocator underneath.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.
@HydraQYH @DarkSharpness I do it in https://github.com/sgl-project/sglang/pull/21314/changes/2a354896f510e08658007523153c7fdfc9daf7e4..b38f8c60048eb0297be5336ebea79a48d115b68e. Let me know if it looks alright.