Add greedy verification kernel (#4383)
This commit is contained in:
@@ -17,6 +17,8 @@
|
||||
#include <ATen/ATen.h>
|
||||
#include <ATen/cuda/CUDAContext.h>
|
||||
|
||||
#include "pytorch_extension_utils.h"
|
||||
|
||||
// parent_list [bs, topk * (depth - 1) + 1)]
|
||||
// selected_index [bs, draft_token_num - 1]
|
||||
// verified_seq_len [bs]
|
||||
@@ -72,8 +74,8 @@ __global__ void build_tree_efficient(
|
||||
}
|
||||
if (parent_position == draft_token_num) {
|
||||
printf(
|
||||
"ERROR: invalid eagle tree!!! Detected a token with no parent token selected. Check the logprob. The token "
|
||||
"will be dropped.");
|
||||
"WARNING: invalid eagle tree!!! Detected a token with no parent token selected. "
|
||||
"Please check if the logprob has nan. The token will be ignored to keep proceeding.\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -140,112 +142,141 @@ void build_tree_kernel_efficient(
|
||||
int32_t(draft_token_num));
|
||||
}
|
||||
|
||||
// parent_list [bs, topk * (depth - 1) + 1)]
|
||||
// selected_index [bs, draft_token_num - 1]
|
||||
// verified_seq_len [bs]
|
||||
// tree_mask [draft_token*(seq_len[0]+draft_token) | draft_token*(seq_len[1]+draft_token) | ..] =
|
||||
// [sum(verified_seq_len)*draft_token+bs*draft_token*draft_token] positions [bs * draft_token] retrive_index [b,
|
||||
// draft_token, depth + 2]
|
||||
__global__ void build_tree(
|
||||
int64_t* parent_list,
|
||||
int64_t* selected_index,
|
||||
int32_t* verified_seq_len,
|
||||
bool* tree_mask,
|
||||
int64_t* positions,
|
||||
int64_t* retrive_index,
|
||||
int topk,
|
||||
int depth,
|
||||
int draft_token_num) {
|
||||
int bid = blockIdx.x;
|
||||
int tid = threadIdx.x;
|
||||
template <typename IdType>
|
||||
__global__ void VerifyTreeGreedy(
|
||||
IdType* predicts,
|
||||
IdType* accept_index,
|
||||
IdType* accept_token_num, // mutable
|
||||
IdType* candidates,
|
||||
IdType* retrive_index,
|
||||
IdType* retrive_next_token,
|
||||
IdType* retrive_next_sibling,
|
||||
IdType* target_predict,
|
||||
uint32_t batch_size,
|
||||
uint32_t num_speculative_tokens,
|
||||
uint32_t num_draft_tokens) {
|
||||
uint32_t bx = blockIdx.x;
|
||||
|
||||
if (tid >= draft_token_num) {
|
||||
return;
|
||||
}
|
||||
int seq_tree_idx = draft_token_num * draft_token_num * bid;
|
||||
for (int i = 0; i < bid; i++) {
|
||||
seq_tree_idx += verified_seq_len[i] * draft_token_num;
|
||||
}
|
||||
int seq_len = verified_seq_len[bid];
|
||||
int token_tree_idx = seq_tree_idx + (seq_len + draft_token_num) * tid + seq_len + 1;
|
||||
for (int i = 0; i < draft_token_num - 1; i++) {
|
||||
tree_mask[token_tree_idx + i] = false;
|
||||
}
|
||||
IdType last_accepted_retrive_idx = retrive_index[bx * num_draft_tokens];
|
||||
accept_index[bx * num_speculative_tokens] = last_accepted_retrive_idx;
|
||||
uint32_t num_accepted_tokens = 0;
|
||||
IdType cur_index = 0;
|
||||
|
||||
int position = 0;
|
||||
if (tid == 0) {
|
||||
positions[bid * draft_token_num] = seq_len;
|
||||
retrive_index[bid * draft_token_num * (depth + 2)] = bid * draft_token_num;
|
||||
return;
|
||||
}
|
||||
for (uint32_t j = 1; j < num_speculative_tokens; ++j) {
|
||||
cur_index = retrive_next_token[bx * num_draft_tokens + cur_index];
|
||||
while (cur_index != -1) {
|
||||
IdType draft_index = retrive_index[bx * num_draft_tokens + cur_index];
|
||||
IdType draft_token_id = candidates[bx * num_draft_tokens + cur_index];
|
||||
IdType target_token_id = target_predict[last_accepted_retrive_idx];
|
||||
|
||||
int depends_order[10];
|
||||
|
||||
int cur_position = tid - 1;
|
||||
while (true) {
|
||||
depends_order[position] = cur_position + 1;
|
||||
position += 1;
|
||||
tree_mask[token_tree_idx + cur_position] = true;
|
||||
int parent_tb_idx = selected_index[bid * (draft_token_num - 1) + cur_position] / topk;
|
||||
if (parent_tb_idx == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
int token_idx = parent_list[bid * (topk * (depth - 1) + 1) + parent_tb_idx];
|
||||
for (cur_position = 0; cur_position < draft_token_num; cur_position++) {
|
||||
if (selected_index[bid * (draft_token_num - 1) + cur_position] == token_idx) {
|
||||
if (draft_token_id == target_token_id) {
|
||||
// accept token
|
||||
predicts[last_accepted_retrive_idx] = target_token_id;
|
||||
++num_accepted_tokens;
|
||||
accept_index[bx * num_speculative_tokens + num_accepted_tokens] = draft_index;
|
||||
last_accepted_retrive_idx = draft_index;
|
||||
break;
|
||||
} else {
|
||||
cur_index = retrive_next_sibling[bx * num_draft_tokens + cur_index];
|
||||
}
|
||||
}
|
||||
if (cur_position == draft_token_num) {
|
||||
printf(
|
||||
"ERROR: invalid eagle tree!!! Detected a token with no parent token selected. Check the logprob. The token "
|
||||
"will be dropped.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
positions[bid * draft_token_num + tid] = position + seq_len;
|
||||
|
||||
int is_leaf = 0;
|
||||
for (int i = 1; i < draft_token_num; i++) {
|
||||
if (tree_mask[seq_tree_idx + i * (draft_token_num + seq_len) + seq_len + tid]) {
|
||||
is_leaf++;
|
||||
}
|
||||
}
|
||||
if (is_leaf == 1) {
|
||||
for (int i = 0; i < position; i++) {
|
||||
retrive_index[(bid * (draft_token_num) + tid) * (depth + 2) + position - i] =
|
||||
depends_order[i] + bid * draft_token_num;
|
||||
}
|
||||
retrive_index[(bid * (draft_token_num) + tid) * (depth + 2)] = bid * draft_token_num;
|
||||
if (cur_index == -1) break;
|
||||
}
|
||||
accept_token_num[bx] = num_accepted_tokens;
|
||||
predicts[last_accepted_retrive_idx] = target_predict[last_accepted_retrive_idx];
|
||||
}
|
||||
|
||||
void build_tree_kernel(
|
||||
at::Tensor parent_list,
|
||||
at::Tensor selected_index,
|
||||
at::Tensor verified_seq_len,
|
||||
at::Tensor tree_mask,
|
||||
at::Tensor positions,
|
||||
// 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,
|
||||
int64_t topk,
|
||||
int64_t depth,
|
||||
int64_t draft_token_num) {
|
||||
// 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();
|
||||
at::Tensor retrive_next_token,
|
||||
at::Tensor retrive_next_sibling,
|
||||
at::Tensor target_predict,
|
||||
int64_t cuda_stream = 0) {
|
||||
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::kInt) {
|
||||
throw std::runtime_error("Expected 'candidates' to be of type int (torch.int32).");
|
||||
}
|
||||
if (retrive_index.scalar_type() != at::kInt) {
|
||||
throw std::runtime_error("Expected 'retrive_index' to be of type int (torch.int32).");
|
||||
}
|
||||
if (retrive_next_token.scalar_type() != at::kInt) {
|
||||
throw std::runtime_error("Expected 'retrive_next_token' to be of type int (torch.int32).");
|
||||
}
|
||||
if (retrive_next_sibling.scalar_type() != at::kInt) {
|
||||
throw std::runtime_error("Expected 'retrive_next_sibling' to be of type int (torch.int32).");
|
||||
}
|
||||
if (target_predict.scalar_type() != at::kInt) {
|
||||
throw std::runtime_error("Expected 'target_predict' to be of type int (torch.int32).");
|
||||
}
|
||||
|
||||
build_tree<<<grid, block, 0, stream>>>(
|
||||
static_cast<int64_t*>(parent_list.data_ptr()),
|
||||
static_cast<int64_t*>(selected_index.data_ptr()),
|
||||
static_cast<int32_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()),
|
||||
int32_t(topk),
|
||||
int32_t(depth),
|
||||
int32_t(draft_token_num));
|
||||
cudaStream_t stream = reinterpret_cast<cudaStream_t>(cuda_stream);
|
||||
dim3 grid(batch_size);
|
||||
dim3 block(1);
|
||||
|
||||
VerifyTreeGreedy<int><<<grid, block, 0, stream>>>(
|
||||
static_cast<int*>(predicts.data_ptr()),
|
||||
static_cast<int*>(accept_index.data_ptr()),
|
||||
static_cast<int*>(accept_token_num.data_ptr()),
|
||||
static_cast<int*>(candidates.data_ptr()),
|
||||
static_cast<int*>(retrive_index.data_ptr()),
|
||||
static_cast<int*>(retrive_next_token.data_ptr()),
|
||||
static_cast<int*>(retrive_next_sibling.data_ptr()),
|
||||
static_cast<int*>(target_predict.data_ptr()),
|
||||
batch_size,
|
||||
num_spec_step,
|
||||
num_draft_tokens);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user