|
| 1 | +/* Copyright 2026 The xLLM Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + https://github.com/jd-opensource/xllm/blob/main/LICENSE |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +#include <c10/cuda/CUDAException.h> |
| 17 | +#include <cuda_runtime.h> |
| 18 | +#include <glog/logging.h> |
| 19 | + |
| 20 | +#include <algorithm> |
| 21 | + |
| 22 | +#include "core/kernels/cuda/llm_decode_metadata_update.h" |
| 23 | + |
| 24 | +namespace xllm::kernel::cuda { |
| 25 | +namespace { |
| 26 | + |
| 27 | +constexpr int32_t kThreadsPerBlock = 256; |
| 28 | + |
| 29 | +__global__ void llm_decode_metadata_update_kernel( |
| 30 | + LlmDecodeMetadataUpdateParams params, |
| 31 | + int64_t max_work_size) { |
| 32 | + const int64_t thread_idx = |
| 33 | + static_cast<int64_t>(blockIdx.x) * blockDim.x + threadIdx.x; |
| 34 | + const int64_t step = static_cast<int64_t>(blockDim.x) * gridDim.x; |
| 35 | + for (int64_t idx = thread_idx; idx < max_work_size; idx += step) { |
| 36 | + if (idx < params.actual_num_tokens) { |
| 37 | + params.dst_tokens[idx] = params.src_tokens[idx]; |
| 38 | + params.dst_positions[idx] = params.src_positions[idx]; |
| 39 | + params.dst_new_cache_slots[idx] = params.src_new_cache_slots[idx]; |
| 40 | + } |
| 41 | + if (idx >= params.actual_num_tokens && idx < params.padded_num_tokens) { |
| 42 | + params.dst_tokens[idx] = 0; |
| 43 | + params.dst_new_cache_slots[idx] = 0; |
| 44 | + } |
| 45 | + if (idx < params.actual_batch_size + 1) { |
| 46 | + params.dst_kv_cu_seq_lens[idx] = params.src_kv_cu_seq_lens[idx]; |
| 47 | + params.dst_paged_kv_indptr[idx] = params.src_paged_kv_indptr[idx]; |
| 48 | + } |
| 49 | + if (idx < params.actual_batch_size) { |
| 50 | + params.dst_kv_seq_lens_delta[idx] = |
| 51 | + params.src_kv_cu_seq_lens[idx + 1] - params.src_kv_cu_seq_lens[idx]; |
| 52 | + params.dst_paged_kv_last_page_len[idx] = |
| 53 | + params.src_paged_kv_last_page_len[idx]; |
| 54 | + } |
| 55 | + if (idx < params.actual_indices_size) { |
| 56 | + params.dst_paged_kv_indices[idx] = params.src_paged_kv_indices[idx]; |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +} // namespace |
| 62 | + |
| 63 | +void UpdateLlmDecodeMetadata(const LlmDecodeMetadataUpdateParams& params, |
| 64 | + cudaStream_t stream) { |
| 65 | + const int64_t max_work_size = std::max({params.actual_num_tokens, |
| 66 | + params.padded_num_tokens, |
| 67 | + params.actual_batch_size + 1, |
| 68 | + params.actual_indices_size}); |
| 69 | + if (max_work_size <= 0) { |
| 70 | + return; |
| 71 | + } |
| 72 | + const int64_t num_blocks = std::min<int64_t>( |
| 73 | + (max_work_size + kThreadsPerBlock - 1) / kThreadsPerBlock, 4096); |
| 74 | + llm_decode_metadata_update_kernel<<<static_cast<uint32_t>(num_blocks), |
| 75 | + kThreadsPerBlock, |
| 76 | + 0, |
| 77 | + stream>>>(params, max_work_size); |
| 78 | + const cudaError_t error = cudaGetLastError(); |
| 79 | + CHECK_EQ(error, cudaSuccess) |
| 80 | + << "llm_decode_metadata_update kernel launch failed: " |
| 81 | + << cudaGetErrorString(error); |
| 82 | +} |
| 83 | + |
| 84 | +} // namespace xllm::kernel::cuda |
0 commit comments