-
Notifications
You must be signed in to change notification settings - Fork 130
LoRA: Implementing kernels using CUBE computation unit #432
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
ping1jing2
merged 8 commits into
sgl-project:main
from
vlserov:vlserov/lora_kernels_cube
May 6, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
27a056c
LoRA: Implementing kernels using CUBE computation unit
vlserov 04c5fa2
Merge branch 'origin/main'
vlserov a7455c6
Resolving comments
vlserov 9b22313
Merge branch 'origin/main'
vlserov a103bd6
Update pre-commit after merge
vlserov e26f9b8
Fix for CANN 8.3
vlserov fb82059
Merge branch 'origin/main'
vlserov a25f41c
Resolve gemini comments
vlserov 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,76 @@ | ||
| /* | ||
| * Copyright (c) Huawei Technologies Co., Ltd. 2026. 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. | ||
| * | ||
| */ | ||
|
|
||
| #include "defines.h" | ||
| #include "tiling/sgemmc_tiling.h" | ||
| #include "torch_helper.h" | ||
|
|
||
| #include "aclrtlaunch_sgemmc_expand.h" | ||
|
|
||
| namespace sglang { | ||
| namespace npu_kernel { | ||
|
|
||
| HOST_API at::Tensor sgemmc_expand(at::Tensor &x, at::Tensor &weight, at::Tensor &lora_indices, at::Tensor &seq_len, | ||
| at::Tensor &lora_ranks, at::Tensor &slice_offsets, at::Tensor &y) | ||
| { | ||
| at::ScalarType scalar_type = y.scalar_type(); | ||
| TORCH_CHECK(scalar_type == at::kHalf || scalar_type == at::kBFloat16, "only support half and bf16"); | ||
| TORCH_CHECK(x.dim() == 2, "x should be [batch_size, hidden_in]"); | ||
| TORCH_CHECK(weight.dim() == 3 || weight.dim() == 4, | ||
| "weight should be [num_loras, hidden_out, hidden_in] or [num_loras, 1, hidden_out, hidden_in]"); | ||
| TORCH_CHECK(y.dim() == 2, "y should be [batch_size, hidden_out]"); | ||
| TORCH_CHECK(slice_offsets.dim() == 1 && slice_offsets.size(0) > 1, | ||
| "slice_offsets should be a vector of size 2 and more."); | ||
| TORCH_CHECK(lora_ranks.dim() == 1, "lora_ranks should be a vector."); | ||
|
|
||
| at::Tensor y_out = y; | ||
| void *x_ptr = x.data_ptr(); | ||
| void *weight_ptr = weight.data_ptr(); | ||
| void *y_ptr = y.data_ptr(); | ||
| void *y_out_ptr = y_out.data_ptr(); | ||
|
|
||
| void *lora_indices_ptr = lora_indices.data_ptr(); | ||
| int lora_indices_size = lora_indices.size(0); | ||
| void *seq_len_ptr = seq_len.data_ptr(); | ||
| int seq_len_size = seq_len.size(0); | ||
| void *lora_ranks_ptr = lora_ranks.data_ptr(); | ||
| int lora_ranks_size = lora_ranks.size(0); | ||
| void *slice_offsets_ptr = slice_offsets.data_ptr(); | ||
| int slice_offsets_size = slice_offsets.size(0); | ||
| int slice_count = slice_offsets_size - 1; | ||
| int batch_size = x.size(0); | ||
| int max_lora_rank = x.size(1) / slice_count; | ||
| int output_full_dim = y.size(1); | ||
|
|
||
| uint32_t block_dim; | ||
| uint32_t workspace_size; | ||
|
|
||
| at::Tensor tiling_tensor = GenerateTiling(block_dim, workspace_size, batch_size, max_lora_rank, output_full_dim, | ||
| slice_count, TorchNpuHelper::ConvertDataType(scalar_type)); | ||
| auto workspace_tensor = | ||
| at::empty({workspace_size}, at::TensorOptions().dtype(at::kByte).device(x.options().device())); | ||
|
|
||
| /* launch the kernel function via torch */ | ||
| EXEC_KERNEL_CMD(sgemmc_expand, block_dim, x_ptr, weight_ptr, lora_indices_ptr, lora_indices_size, seq_len_ptr, | ||
| seq_len_size, lora_ranks_ptr, lora_ranks_size, slice_offsets_ptr, slice_offsets_size, y_ptr, | ||
| y_out_ptr, batch_size, max_lora_rank, output_full_dim, workspace_tensor, tiling_tensor); | ||
|
|
||
| return y_out; | ||
| } | ||
|
|
||
| } // namespace npu_kernel | ||
| } // namespace sglang | ||
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,71 @@ | ||
| /* | ||
| * Copyright (c) Huawei Technologies Co., Ltd. 2026. 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. | ||
| * | ||
| */ | ||
|
|
||
| #include "defines.h" | ||
| #include "tiling/sgemmc_tiling.h" | ||
| #include "torch_helper.h" | ||
|
|
||
| #include "aclrtlaunch_sgemmc_shrink.h" | ||
|
|
||
| namespace sglang { | ||
| namespace npu_kernel { | ||
|
|
||
| HOST_API void sgemmc_shrink(at::Tensor &x, at::Tensor &weight, at::Tensor &lora_indices, at::Tensor &seq_len, | ||
| at::Tensor &lora_ranks, at::Tensor &lora_scales, at::Tensor &y, int64_t slices) | ||
| { | ||
| at::ScalarType scalar_type = x.scalar_type(); | ||
| TORCH_CHECK(scalar_type == at::kHalf || scalar_type == at::kBFloat16, "only support half and bf16"); | ||
| TORCH_CHECK(x.dim() == 2, "x should be [batch_size, hidden_in]"); | ||
| TORCH_CHECK(weight.dim() == 3 || weight.dim() == 4, | ||
| "weight should be [num_loras, hidden_out, hidden_in] or [num_loras, 1, hidden_out, hidden_in]"); | ||
| TORCH_CHECK(y.dim() == 2, "y should be [batch_size, hidden_out]"); | ||
| TORCH_CHECK(x.size(1) > y.size(1), "hidden in should be greater than hidden out"); | ||
|
|
||
| void *x_ptr = x.data_ptr(); | ||
| void *weight_ptr = weight.data_ptr(); | ||
| void *lora_indices_ptr = lora_indices.data_ptr(); | ||
| int lora_indices_size = lora_indices.size(0); | ||
| void *seq_len_ptr = seq_len.data_ptr(); | ||
| int seq_len_size = seq_len.size(0); | ||
| void *lora_ranks_ptr = lora_ranks.data_ptr(); | ||
| int lora_ranks_size = lora_ranks.size(0); | ||
| void *lora_scales_ptr = lora_scales.data_ptr(); | ||
| int lora_scales_size = lora_scales.size(0); | ||
|
|
||
| void *y_ptr = y.data_ptr(); | ||
| int batch_size = x.size(0); | ||
| int input_hidden_token = x.size(1); | ||
| uint32_t max_lora_rank = y.size(1) / slices; | ||
| uint32_t slice_count = slices; | ||
|
|
||
| uint32_t block_dim; | ||
| uint32_t workspace_size; | ||
|
|
||
| at::Tensor tiling_tensor = GenerateTiling(block_dim, workspace_size, batch_size, input_hidden_token, max_lora_rank, | ||
| slice_count, TorchNpuHelper::ConvertDataType(scalar_type)); | ||
|
|
||
| auto workspace_tensor = | ||
| at::empty({workspace_size}, at::TensorOptions().dtype(at::kByte).device(x.options().device())); | ||
| /* launch the kernel function via torch */ | ||
| EXEC_KERNEL_CMD(sgemmc_shrink, block_dim, x_ptr, weight_ptr, lora_indices_ptr, lora_indices_size, seq_len_ptr, | ||
| seq_len_size, lora_ranks_ptr, lora_ranks_size, lora_scales_ptr, lora_scales_size, y_ptr, batch_size, | ||
| input_hidden_token, max_lora_rank, slice_count, workspace_tensor, tiling_tensor); | ||
| return; | ||
| } | ||
|
|
||
| } // namespace npu_kernel | ||
| } // namespace sglang |
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,80 @@ | ||
| /* | ||
| * Copyright (c) Huawei Technologies Co., Ltd. 2026. 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. | ||
| * | ||
| */ | ||
|
|
||
| #include "common.h" | ||
| #include "sgemmc_tiling.h" | ||
|
|
||
| namespace sglang { | ||
| namespace npu_kernel { | ||
|
|
||
| matmul_tiling::DataType ConvertToMatMulTypes(host_utils::DataType data_type) | ||
| { | ||
| switch (data_type) { | ||
| case host_utils::DataType::DT_BFLOAT16: | ||
| return matmul_tiling::DataType::DT_BFLOAT16; | ||
| case host_utils::DataType::DT_FLOAT: | ||
| return matmul_tiling::DataType::DT_FLOAT; | ||
| case host_utils::DataType::DT_FLOAT16: | ||
| return matmul_tiling::DataType::DT_FLOAT16; | ||
| } | ||
|
|
||
| return matmul_tiling::DataType::DT_FLOAT16; | ||
| } | ||
|
|
||
| at::Tensor GenerateTiling(uint32_t &block_dim, uint32_t &workspace_size, uint32_t batch_size, uint32_t inner_size, | ||
| uint32_t output_size, uint32_t slice_count, const host_utils::DataType type) | ||
| { | ||
| auto ascendc_platform = *platform_ascendc::PlatformAscendCManager::GetInstance(); | ||
| uint32_t aiv_num = ascendc_platform.GetCoreNumAiv(); | ||
| uint32_t aic_num = ascendc_platform.GetCoreNumAic(); | ||
| workspace_size = ascendc_platform.GetLibApiWorkSpaceSize(); | ||
|
|
||
| auto tilingBuffer = at::empty({sizeof(SGEMMCTilingData)}, at::TensorOptions().dtype(at::kByte).device(at::kCPU)); | ||
| SGEMMCTilingData *tiling_data = reinterpret_cast<SGEMMCTilingData *>(tilingBuffer.data_ptr()); | ||
|
|
||
| matmul_tiling::MatmulApiTiling cubeTiling(ascendc_platform); | ||
|
|
||
| const matmul_tiling::DataType data_type = ConvertToMatMulTypes(type); | ||
|
|
||
| cubeTiling.EnableBias(false); | ||
| cubeTiling.SetAType(matmul_tiling::TPosition::GM, matmul_tiling::CubeFormat::VECTOR, data_type, false); | ||
| cubeTiling.SetBType(matmul_tiling::TPosition::GM, matmul_tiling::CubeFormat::ND, data_type, true); | ||
| cubeTiling.SetCType(matmul_tiling::TPosition::VECIN, matmul_tiling::CubeFormat::ND, | ||
| matmul_tiling::DataType::DT_FLOAT); | ||
| cubeTiling.SetBiasType(matmul_tiling::TPosition::GM, matmul_tiling::CubeFormat::ND, data_type); | ||
|
|
||
| cubeTiling.SetOrgShape(1, output_size, inner_size); | ||
| cubeTiling.SetShape(1, output_size, inner_size); | ||
| cubeTiling.SetBufferSpace(-1, -1, -1); | ||
|
|
||
| if (cubeTiling.GetTiling(tiling_data->cubeTiling) == -1) { | ||
| TORCH_CHECK(false, "Generate tiling failed."); | ||
| return {}; | ||
| } | ||
|
|
||
| tiling_data->tilingKey = (type == host_utils::DataType::DT_BFLOAT16); | ||
|
|
||
| block_dim = batch_size * slice_count; | ||
| workspace_size = ascendc_platform.GetLibApiWorkSpaceSize() + | ||
| static_cast<uint32_t>(batch_size * tiling_data->cubeTiling.baseM * tiling_data->cubeTiling.baseN * | ||
| sizeof(float)); | ||
|
|
||
| return TorchNpuHelper::CopyTensorHostToDevice(tilingBuffer); | ||
| } | ||
|
|
||
| } // namespace npu_kernel | ||
| } // namespace sglang |
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,37 @@ | ||
| /* | ||
| * Copyright (c) Huawei Technologies Co., Ltd. 2026. 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. | ||
| * | ||
| */ | ||
|
|
||
| #ifndef SGEMMC_TILING_H | ||
| #define SGEMMC_TILING_H | ||
|
|
||
| #include <register/tilingdata_base.h> | ||
| #include <tiling/tiling_api.h> | ||
|
|
||
| #include "torch_helper.h" | ||
| #include "common_tiling.h" | ||
| #include "sgemmc_tiling_data.h" | ||
|
|
||
| namespace sglang { | ||
| namespace npu_kernel { | ||
|
|
||
| at::Tensor GenerateTiling(uint32_t &blockDim, uint32_t &workspace, uint32_t batch, uint32_t hidden_size, uint32_t k, | ||
| uint32_t slice_count, const host_utils::DataType type); | ||
|
|
||
| } // namespace npu_kernel | ||
| } // namespace sglang | ||
|
|
||
| #endif // SGEMMC_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,44 @@ | ||
| /* | ||
| * Copyright (c) Huawei Technologies Co., Ltd. 2026. 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. | ||
| * | ||
| */ | ||
|
|
||
| #ifndef SGEMMC_TILING_DATA_H | ||
| #define SGEMMC_TILING_DATA_H | ||
|
|
||
| #include <cstdint> | ||
|
|
||
| namespace AscendC { | ||
| namespace tiling { | ||
|
|
||
| struct TCubeTiling; | ||
|
|
||
| } // namespace tiling | ||
| } // namespace AscendC | ||
|
|
||
| namespace sglang { | ||
| namespace npu_kernel { | ||
|
|
||
| #pragma pack(push, 1) | ||
| struct SGEMMCTilingData { | ||
| uint32_t tilingKey; | ||
| AscendC::tiling::TCubeTiling cubeTiling; | ||
| }; | ||
| #pragma pack(pop) | ||
|
|
||
| } // namespace npu_kernel | ||
| } // namespace sglang | ||
|
|
||
| #endif // SGEMMC_TILING_DATA_H |
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.