183 lines
6.9 KiB
Plaintext
183 lines
6.9 KiB
Plaintext
/*
|
|
* Copyright (c) 2025 by SGLang team.
|
|
*
|
|
* 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 <ATen/ATen.h>
|
|
#include <ATen/cuda/CUDAContext.h>
|
|
|
|
#if !defined(USE_ROCM) && !defined(USE_MUSA)
|
|
#include "pytorch_extension_utils.h"
|
|
#else
|
|
#include "pytorch_extension_utils_rocm.h"
|
|
#endif
|
|
|
|
#include <sgl_kernel/speculative/eagle.cuh>
|
|
|
|
using sglang::speculative::build_tree_efficient;
|
|
using sglang::speculative::build_tree_efficient_partial_packed;
|
|
using sglang::speculative::QLEN_ONLY_BITPACKING;
|
|
using sglang::speculative::VerifyTreeGreedy;
|
|
|
|
void build_tree_kernel_efficient(
|
|
at::Tensor parent_list,
|
|
at::Tensor selected_index,
|
|
at::Tensor verified_seq_len,
|
|
at::Tensor tree_mask,
|
|
at::Tensor positions,
|
|
at::Tensor retrive_index,
|
|
at::Tensor retrive_next_token,
|
|
at::Tensor retrive_next_sibling,
|
|
int64_t topk,
|
|
int64_t depth,
|
|
int64_t draft_token_num,
|
|
int64_t tree_mask_mode) {
|
|
// TODO (ying) check shape
|
|
// TODO (ying) check type
|
|
int bs = parent_list.size(0);
|
|
dim3 grid(bs);
|
|
dim3 block(draft_token_num);
|
|
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
|
|
|
if (tree_mask_mode == QLEN_ONLY_BITPACKING) {
|
|
size_t num_bytes_per_item = 1;
|
|
if (draft_token_num > 16) {
|
|
num_bytes_per_item = 4;
|
|
} else if (draft_token_num > 8) {
|
|
num_bytes_per_item = 2;
|
|
}
|
|
build_tree_efficient_partial_packed<<<grid, block, 0, stream>>>(
|
|
static_cast<int64_t*>(parent_list.data_ptr()),
|
|
static_cast<int64_t*>(selected_index.data_ptr()),
|
|
static_cast<int64_t*>(verified_seq_len.data_ptr()),
|
|
static_cast<uint8_t*>(tree_mask.data_ptr()),
|
|
static_cast<int64_t*>(positions.data_ptr()),
|
|
static_cast<int64_t*>(retrive_index.data_ptr()),
|
|
static_cast<int64_t*>(retrive_next_token.data_ptr()),
|
|
static_cast<int64_t*>(retrive_next_sibling.data_ptr()),
|
|
int32_t(topk),
|
|
int32_t(depth),
|
|
int32_t(draft_token_num),
|
|
num_bytes_per_item);
|
|
} else {
|
|
build_tree_efficient<<<grid, block, 0, stream>>>(
|
|
static_cast<int64_t*>(parent_list.data_ptr()),
|
|
static_cast<int64_t*>(selected_index.data_ptr()),
|
|
static_cast<int64_t*>(verified_seq_len.data_ptr()),
|
|
static_cast<bool*>(tree_mask.data_ptr()),
|
|
static_cast<int64_t*>(positions.data_ptr()),
|
|
static_cast<int64_t*>(retrive_index.data_ptr()),
|
|
static_cast<int64_t*>(retrive_next_token.data_ptr()),
|
|
static_cast<int64_t*>(retrive_next_sibling.data_ptr()),
|
|
int32_t(topk),
|
|
int32_t(depth),
|
|
int32_t(draft_token_num),
|
|
int32_t(tree_mask_mode));
|
|
}
|
|
}
|
|
|
|
// predicts: [tot_num_draft_tokens]
|
|
// accept_index: [bs, num_spec_step]
|
|
// accept_token_num: [bs]
|
|
// candidates: [bs, num_draft_tokens]
|
|
// retrive_index: [bs, num_draft_tokens]
|
|
// retrive_next_token: [bs, num_draft_tokens]
|
|
// retrive_next_sibling: [bs, num_draft_tokens]
|
|
// target_predict: [bs, num_draft_tokens]
|
|
void verify_tree_greedy(
|
|
at::Tensor predicts,
|
|
at::Tensor accept_index,
|
|
at::Tensor accept_token_num, // mutable
|
|
at::Tensor candidates,
|
|
at::Tensor retrive_index,
|
|
at::Tensor retrive_next_token,
|
|
at::Tensor retrive_next_sibling,
|
|
at::Tensor target_predict) {
|
|
CHECK_INPUT(candidates);
|
|
CHECK_INPUT(retrive_index);
|
|
CHECK_INPUT(retrive_next_token);
|
|
CHECK_INPUT(retrive_next_sibling);
|
|
CHECK_INPUT(target_predict);
|
|
auto device = target_predict.device();
|
|
CHECK_EQ(candidates.device(), device);
|
|
CHECK_EQ(retrive_index.device(), device);
|
|
CHECK_EQ(retrive_next_token.device(), device);
|
|
CHECK_EQ(retrive_next_sibling.device(), device);
|
|
CHECK_EQ(target_predict.device(), device);
|
|
CHECK_DIM(1, predicts);
|
|
CHECK_DIM(2, accept_index);
|
|
CHECK_DIM(1, accept_token_num);
|
|
CHECK_DIM(2, candidates);
|
|
CHECK_DIM(2, retrive_index);
|
|
CHECK_DIM(2, retrive_next_token);
|
|
CHECK_DIM(2, retrive_next_sibling);
|
|
CHECK_DIM(2, target_predict);
|
|
unsigned int batch_size = candidates.size(0);
|
|
unsigned int num_spec_step = accept_index.size(1);
|
|
unsigned int num_draft_tokens = candidates.size(1);
|
|
CHECK_EQ(batch_size, accept_index.size(0));
|
|
CHECK_EQ(batch_size, accept_token_num.size(0));
|
|
CHECK_EQ(batch_size, retrive_index.size(0));
|
|
CHECK_EQ(batch_size, retrive_next_token.size(0));
|
|
CHECK_EQ(batch_size, retrive_next_sibling.size(0));
|
|
CHECK_EQ(batch_size, target_predict.size(0));
|
|
CHECK_EQ(num_draft_tokens, retrive_index.size(1));
|
|
CHECK_EQ(num_draft_tokens, retrive_next_token.size(1));
|
|
CHECK_EQ(num_draft_tokens, retrive_next_sibling.size(1));
|
|
CHECK_EQ(num_draft_tokens, target_predict.size(1));
|
|
CHECK_EQ(batch_size, accept_index.size(0));
|
|
CHECK_EQ(batch_size, accept_token_num.size(0));
|
|
if (predicts.scalar_type() != at::kInt) {
|
|
throw std::runtime_error("Expected 'predicts' to be of type int (torch.int32).");
|
|
}
|
|
if (accept_index.scalar_type() != at::kInt) {
|
|
throw std::runtime_error("Expected 'accept_index' to be of type int (torch.int32).");
|
|
}
|
|
if (accept_token_num.scalar_type() != at::kInt) {
|
|
throw std::runtime_error("Expected 'accept_token_num' to be of type int (torch.int32).");
|
|
}
|
|
if (candidates.scalar_type() != at::kLong) {
|
|
throw std::runtime_error("Expected 'candidates' to be of type long (torch.int64).");
|
|
}
|
|
if (retrive_index.scalar_type() != at::kLong) {
|
|
throw std::runtime_error("Expected 'retrive_index' to be of type long (torch.int64).");
|
|
}
|
|
if (retrive_next_token.scalar_type() != at::kLong) {
|
|
throw std::runtime_error("Expected 'retrive_next_token' to be of type long (torch.int64).");
|
|
}
|
|
if (retrive_next_sibling.scalar_type() != at::kLong) {
|
|
throw std::runtime_error("Expected 'retrive_next_sibling' to be of type long (torch.int64).");
|
|
}
|
|
if (target_predict.scalar_type() != at::kLong) {
|
|
throw std::runtime_error("Expected 'target_predict' to be of type long (torch.int64).");
|
|
}
|
|
|
|
cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
|
dim3 grid(batch_size);
|
|
dim3 block(1);
|
|
|
|
VerifyTreeGreedy<int32_t, int64_t><<<grid, block, 0, stream>>>(
|
|
static_cast<int32_t*>(predicts.data_ptr()),
|
|
static_cast<int32_t*>(accept_index.data_ptr()),
|
|
static_cast<int32_t*>(accept_token_num.data_ptr()),
|
|
static_cast<int64_t*>(candidates.data_ptr()),
|
|
static_cast<int64_t*>(retrive_index.data_ptr()),
|
|
static_cast<int64_t*>(retrive_next_token.data_ptr()),
|
|
static_cast<int64_t*>(retrive_next_sibling.data_ptr()),
|
|
static_cast<int64_t*>(target_predict.data_ptr()),
|
|
batch_size,
|
|
num_spec_step,
|
|
num_draft_tokens);
|
|
}
|