-
Notifications
You must be signed in to change notification settings - Fork 77
[feat] add catcoc mm+ar & ag+mm #271
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
Open
ltcs11
wants to merge
15
commits into
sgl-project:main
Choose a base branch
from
ltcs11:main_251223
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.
Open
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4023c78
[WIP] add catcoc demo
ltcs11 effd7e3
add tiling fix
ltcs11 32812cd
[test] move shmem init into cpp
ltcs11 f53c067
[refactor] using 2 stage compile (bisheng on kernel instead of ascendc)
ltcs11 767d54d
Merge branch 'sgl-project:main' into main_251223
ltcs11 f38fcc3
[catcoc] add matmull_allreduce
ltcs11 64aa3ce
[catcoc] add bf16 & weight nz support
ltcs11 221a312
apply pre-commit
ltcs11 6a4e319
optimize for Qwen-30B-A3B
ltcs11 2e45ffe
[refactor] using template to apply dType/formatType with different cases
ltcs11 ba98af8
apply pre-commit
ltcs11 d3775f1
split tiling into batched & single
ltcs11 7b5f98b
add readme & remove unused comment
fa1a8e2
Merge branch 'main' into main_251223
ltcs11 f6b9003
apply for suffix decoding(step > 5)
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
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,12 @@ | ||
| # torch.ops.catcoc | ||
|
|
||
|
|
||
| ## Function Description | 功能描述 | ||
|
|
||
| ### English: | ||
| This is the catcoc(based on catlass) version matmul+comm/comm+matmul fused kernel | ||
|
|
||
| ### 中文: | ||
| 这是调用catcoc模板库(基于catlass)实现的矩阵乘法和通讯融合运算算子 | ||
|
|
||
| 参考/Refs: [CATLSS](https://gitcode.com/cann/catlass) [CATCOC](https://open.codehub.huawei.com/OpenBaize/Ascend/CATCoC) |
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,47 @@ | ||
| // Licensed under the BSD 3-Clause License (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // 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 KERNEL_CATCOC_HOST_TILING_H | ||
| #define KERNEL_CATCOC_HOST_TILING_H | ||
|
|
||
| #include <cstdint> | ||
| #include <map> | ||
|
|
||
| namespace sglang { | ||
| namespace npu_kernel { | ||
|
|
||
| typedef enum { WEIGHT_ND = 0, WEIGHT_NZ = 1 } WeightFormatMode; | ||
|
|
||
| typedef enum { BF16 = 0, FP16 = 1, FP32 = 2 } DataFormatMode; | ||
ltcs11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| struct KernelCATCOCHostTilingData { | ||
| uint32_t m; // get from matmul M | ||
| uint32_t n; // get from matmul N | ||
| uint32_t k; // get from matmul K | ||
|
|
||
| uint32_t m0 = 128; | ||
| uint32_t k0 = 256; | ||
| uint32_t n0 = 256; | ||
| uint32_t swizzleDirect = 1; | ||
| uint32_t swizzleOffset = 7; | ||
| uint32_t ubMoveNum = 16 * 1024; | ||
| uint32_t pValue = 3; | ||
| uint32_t commNpuSplit = 2; | ||
| uint32_t commDataSplit = 1; | ||
| uint32_t lenPerLoop = 128 * 256 / 2; | ||
|
|
||
| int64_t weight_format_mode = WEIGHT_ND; | ||
| int64_t data_format_mode = BF16; | ||
| }; | ||
|
|
||
| } // namespace npu_kernel | ||
| } // namespace sglang | ||
|
|
||
| #endif // KERNEL_CATCOC_HOST_TILING_H | ||
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,67 @@ | ||
| // Licensed under the BSD 3-Clause License (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // 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 KERNEL_CATCOC_HOST_UTILS_H | ||
| #define KERNEL_CATCOC_HOST_UTILS_H | ||
|
|
||
| #include <cstdint> | ||
| #include "catcoc_host_tiling.h" | ||
|
|
||
| namespace sglang { | ||
| namespace npu_kernel { | ||
|
|
||
| constexpr uint32_t PADDING_BYTE = 32U; | ||
|
|
||
| inline std::map<c10::ScalarType, DataFormatMode> dTypeMap = {{at::ScalarType::Half, DataFormatMode::FP16}, | ||
| {at::ScalarType::BFloat16, DataFormatMode::BF16}}; | ||
|
|
||
| inline std::unordered_map<c10::string_view, uint16_t> weightFormatMap = {{"ND", WeightFormatMode::WEIGHT_ND}, | ||
| {"NZ", WeightFormatMode::WEIGHT_NZ}}; | ||
|
|
||
| // batch size -> memory index | ||
| constexpr uint32_t MAX_CAPTURE_NUM = 512; | ||
|
|
||
| template <typename MapType> | ||
| inline int GetModeVal(const MapType &mode_map, c10::optional<c10::string_view> mode_opt, c10::string_view default_mode, | ||
| const char *mode_name) | ||
| { | ||
| std::string modeStr(mode_name); | ||
| c10::string_view mode_str = mode_opt.value_or(default_mode); | ||
| auto it = mode_map.find(mode_str); | ||
| // if input mode is unsupported, use default value | ||
| TORCH_CHECK(it != mode_map.end(), modeStr, c10::str(": Unsupported mode value ", mode_str)); | ||
| return it->second; | ||
| } | ||
|
|
||
| inline at::Tensor get_tiling_tensor(uint32_t &m, uint32_t &n, uint32_t &k, int64_t weight_format_mode, | ||
| int64_t data_format_mode, uint32_t &blockDim) | ||
| { | ||
| auto ascendc_platform = platform_ascendc::PlatformAscendCManager::GetInstance(); | ||
| blockDim = static_cast<uint32_t>(ascendc_platform->GetCoreNumAiv()); | ||
|
|
||
| // align to 32 bytes | ||
| int32_t tiling_size = (sizeof(KernelCATCOCHostTilingData) + PADDING_BYTE - 1) / PADDING_BYTE * PADDING_BYTE; | ||
| auto tiling_buffer = at::empty({tiling_size}, at::TensorOptions().dtype(at::kByte).device(at::kCPU)); | ||
|
|
||
| KernelCATCOCHostTilingData *tiling_data = reinterpret_cast<KernelCATCOCHostTilingData *>(tiling_buffer.data_ptr()); | ||
| tiling_data->m = m; | ||
| tiling_data->n = n; | ||
| tiling_data->k = k; | ||
| tiling_data->weight_format_mode = weight_format_mode; | ||
| tiling_data->data_format_mode = data_format_mode; | ||
|
|
||
| // auto tiling_tensor = TorchNpuHelper::CopyTensorHostToDevice(tiling_buffer); | ||
| return tiling_buffer; | ||
| } | ||
|
|
||
| } // namespace npu_kernel | ||
| } // namespace sglang | ||
|
|
||
| #endif // KERNEL_CATCOC_HOST_UTILS_H |
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,48 @@ | ||
| // Licensed under the BSD 3-Clause License (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // 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 KERNEL_CATCOC_KERNEL_H | ||
| #define KERNEL_CATCOC_KERNEL_H | ||
|
|
||
| #include <acl/acl.h> | ||
| #include "catcoc_host_tiling.h" | ||
|
|
||
| void catcoc_allgather_matmul_bf16_wnd_kernel(uint32_t blockNum, aclrtStream stream, uint64_t fftsAddr, uint64_t teamIdx, | ||
| uint8_t *gmA, uint8_t *gmB, uint8_t *gmC, uint8_t *gmSymmetric, | ||
| uint8_t *gmWorkspace, uint8_t *gmTiling); | ||
|
|
||
| void catcoc_allgather_matmul_fp16_wnd_kernel(uint32_t blockNum, aclrtStream stream, uint64_t fftsAddr, uint64_t teamIdx, | ||
| uint8_t *gmA, uint8_t *gmB, uint8_t *gmC, uint8_t *gmSymmetric, | ||
| uint8_t *gmWorkspace, uint8_t *gmTiling); | ||
|
|
||
| void catcoc_allgather_matmul_bf16_wnz_kernel(uint32_t blockNum, aclrtStream stream, uint64_t fftsAddr, uint64_t teamIdx, | ||
| uint8_t *gmA, uint8_t *gmB, uint8_t *gmC, uint8_t *gmSymmetric, | ||
| uint8_t *gmWorkspace, uint8_t *gmTiling); | ||
|
|
||
| void catcoc_allgather_matmul_fp16_wnz_kernel(uint32_t blockNum, aclrtStream stream, uint64_t fftsAddr, uint64_t teamIdx, | ||
| uint8_t *gmA, uint8_t *gmB, uint8_t *gmC, uint8_t *gmSymmetric, | ||
| uint8_t *gmWorkspace, uint8_t *gmTiling); | ||
|
|
||
| void catcoc_matmul_allreduce_bf16_wnd_kernel(uint32_t blockNum, aclrtStream stream, uint64_t fftsAddr, uint64_t teamIdx, | ||
| uint8_t *gmA, uint8_t *gmB, uint8_t *gmC, uint8_t *gmSymmetric, | ||
| uint8_t *gmWorkspace, uint8_t *gmTiling); | ||
|
|
||
| void catcoc_matmul_allreduce_fp16_wnd_kernel(uint32_t blockNum, aclrtStream stream, uint64_t fftsAddr, uint64_t teamIdx, | ||
| uint8_t *gmA, uint8_t *gmB, uint8_t *gmC, uint8_t *gmSymmetric, | ||
| uint8_t *gmWorkspace, uint8_t *gmTiling); | ||
|
|
||
| void catcoc_matmul_allreduce_bf16_wnz_kernel(uint32_t blockNum, aclrtStream stream, uint64_t fftsAddr, uint64_t teamIdx, | ||
| uint8_t *gmA, uint8_t *gmB, uint8_t *gmC, uint8_t *gmSymmetric, | ||
| uint8_t *gmWorkspace, uint8_t *gmTiling); | ||
|
|
||
| void catcoc_matmul_allreduce_fp16_wnz_kernel(uint32_t blockNum, aclrtStream stream, uint64_t fftsAddr, uint64_t teamIdx, | ||
| uint8_t *gmA, uint8_t *gmB, uint8_t *gmC, uint8_t *gmSymmetric, | ||
| uint8_t *gmWorkspace, uint8_t *gmTiling); | ||
| #endif // KERNEL_CATCOC_KERNEL_H |
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.