|
| 1 | +/* |
| 2 | + * Copyright (c) Huawei Technologies Co., Ltd. 2026. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +#include "defines.h" |
| 19 | +#include "tiling/sgemmc_tiling.h" |
| 20 | +#include "torch_helper.h" |
| 21 | + |
| 22 | +#include "aclrtlaunch_sgemmc_expand.h" |
| 23 | + |
| 24 | +namespace sglang { |
| 25 | +namespace npu_kernel { |
| 26 | + |
| 27 | +HOST_API at::Tensor sgemmc_expand(at::Tensor &x, at::Tensor &weight, at::Tensor &lora_indices, at::Tensor &seq_len, |
| 28 | + at::Tensor &lora_ranks, at::Tensor &slice_offsets, at::Tensor &y) |
| 29 | +{ |
| 30 | + at::ScalarType scalar_type = y.scalar_type(); |
| 31 | + TORCH_CHECK(scalar_type == at::kHalf || scalar_type == at::kBFloat16, "only support half and bf16"); |
| 32 | + TORCH_CHECK(x.dim() == 2, "x should be [batch_size, hidden_in]"); |
| 33 | + TORCH_CHECK(weight.dim() == 3 || weight.dim() == 4, |
| 34 | + "weight should be [num_loras, hidden_out, hidden_in] or [num_loras, 1, hidden_out, hidden_in]"); |
| 35 | + TORCH_CHECK(y.dim() == 2, "y should be [batch_size, hidden_out]"); |
| 36 | + |
| 37 | + at::Tensor y_out = y; |
| 38 | + void *x_ptr = x.data_ptr(); |
| 39 | + void *weight_ptr = weight.data_ptr(); |
| 40 | + void *y_ptr = y.data_ptr(); |
| 41 | + void *y_out_ptr = y_out.data_ptr(); |
| 42 | + |
| 43 | + void *lora_indices_ptr = lora_indices.data_ptr(); |
| 44 | + int lora_indices_size = lora_indices.size(0); |
| 45 | + void *seq_len_ptr = seq_len.data_ptr(); |
| 46 | + int seq_len_size = seq_len.size(0); |
| 47 | + void *lora_ranks_ptr = lora_ranks.data_ptr(); |
| 48 | + int lora_ranks_size = lora_ranks.size(0); |
| 49 | + void *slice_offsets_ptr = slice_offsets.data_ptr(); |
| 50 | + int slice_offsets_size = slice_offsets.size(0); |
| 51 | + int slice_count = slice_offsets_size - 1; |
| 52 | + int batch_size = x.size(0); |
| 53 | + int max_lora_rank = x.size(1) / slice_count; |
| 54 | + int output_full_dim = y.size(1); |
| 55 | + |
| 56 | + uint32_t block_dim; |
| 57 | + uint32_t workspace_size; |
| 58 | + |
| 59 | + at::Tensor tiling_tensor = GenerateTiling(block_dim, workspace_size, batch_size, max_lora_rank, output_full_dim, |
| 60 | + TorchNpuHelper::ConvertDataType(scalar_type)); |
| 61 | + auto workspace_tensor = |
| 62 | + at::empty({workspace_size}, at::TensorOptions().dtype(at::kByte).device(x.options().device())); |
| 63 | + |
| 64 | + /* launch the kernel function via torch */ |
| 65 | + EXEC_KERNEL_CMD(sgemmc_expand, block_dim, x_ptr, weight_ptr, lora_indices_ptr, lora_indices_size, seq_len_ptr, |
| 66 | + seq_len_size, lora_ranks_ptr, lora_ranks_size, slice_offsets_ptr, slice_offsets_size, y_ptr, |
| 67 | + y_out_ptr, batch_size, max_lora_rank, output_full_dim, workspace_tensor, tiling_tensor); |
| 68 | + |
| 69 | + return y_out; |
| 70 | +} |
| 71 | + |
| 72 | +} // namespace npu_kernel |
| 73 | +} // namespace sglang |
0 commit comments