2023-03-30 11:04:21 -07:00
|
|
|
#include <torch/extension.h>
|
|
|
|
#include <ATen/cuda/CUDAContext.h>
|
|
|
|
|
2023-09-02 14:59:47 +09:00
|
|
|
#include "dispatch_utils.h"
|
|
|
|
|
2023-06-17 03:07:40 -07:00
|
|
|
namespace vllm {
|
2023-03-30 11:04:21 -07:00
|
|
|
|
|
|
|
template<typename scalar_t>
|
|
|
|
__global__ void rotary_embedding_neox_kernel(
|
|
|
|
const int64_t* __restrict__ positions, // [num_tokens]
|
2023-04-02 00:30:17 -07:00
|
|
|
scalar_t* __restrict__ query, // [num_tokens, num_heads, head_size]
|
2023-07-20 11:38:27 -07:00
|
|
|
scalar_t* __restrict__ key, // [num_tokens, num_kv_heads, head_size]
|
2023-04-28 00:32:10 -07:00
|
|
|
const scalar_t* __restrict__ cos_sin_cache, // [max_position, 2, rot_dim // 2]
|
|
|
|
const int rot_dim,
|
2023-08-02 14:04:39 -07:00
|
|
|
const int query_stride,
|
|
|
|
const int key_stride,
|
2023-03-30 11:04:21 -07:00
|
|
|
const int num_heads,
|
2023-07-20 11:38:27 -07:00
|
|
|
const int num_kv_heads,
|
2023-03-30 11:04:21 -07:00
|
|
|
const int head_size) {
|
|
|
|
// Each thread block is responsible for one token.
|
|
|
|
const int token_idx = blockIdx.x;
|
|
|
|
int64_t pos = positions[token_idx];
|
2023-04-28 00:32:10 -07:00
|
|
|
const scalar_t* cache_ptr = cos_sin_cache + pos * rot_dim;
|
2023-03-30 11:04:21 -07:00
|
|
|
|
2023-04-28 00:32:10 -07:00
|
|
|
const int embed_dim = rot_dim / 2;
|
2023-07-20 11:38:27 -07:00
|
|
|
const int nq = num_heads * embed_dim;
|
|
|
|
for (int i = threadIdx.x; i < nq; i += blockDim.x) {
|
2023-04-02 00:30:17 -07:00
|
|
|
const int head_idx = i / embed_dim;
|
2023-08-02 14:04:39 -07:00
|
|
|
const int token_head = token_idx * query_stride + head_idx * head_size;
|
2023-03-30 11:04:21 -07:00
|
|
|
|
2023-04-02 00:30:17 -07:00
|
|
|
const int rot_offset = i % embed_dim;
|
2023-03-30 11:04:21 -07:00
|
|
|
const int x_index = rot_offset;
|
|
|
|
const int y_index = embed_dim + rot_offset;
|
|
|
|
|
2023-08-02 14:04:39 -07:00
|
|
|
const int out_x = token_idx * query_stride + head_idx * head_size + x_index;
|
|
|
|
const int out_y = token_idx * query_stride + head_idx * head_size + y_index;
|
2023-04-02 00:30:17 -07:00
|
|
|
|
2023-03-30 11:04:21 -07:00
|
|
|
const scalar_t cos = __ldg(cache_ptr + x_index);
|
|
|
|
const scalar_t sin = __ldg(cache_ptr + y_index);
|
|
|
|
|
2023-04-02 00:30:17 -07:00
|
|
|
const scalar_t q_x = query[token_head + x_index];
|
|
|
|
const scalar_t q_y = query[token_head + y_index];
|
|
|
|
query[out_x] = q_x * cos - q_y * sin;
|
|
|
|
query[out_y] = q_y * cos + q_x * sin;
|
2023-08-02 14:04:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const int nk = num_kv_heads * embed_dim;
|
|
|
|
for (int i = threadIdx.x; i < nk; i += blockDim.x) {
|
|
|
|
const int head_idx = i / embed_dim;
|
|
|
|
const int token_head = token_idx * key_stride + head_idx * head_size;
|
|
|
|
|
|
|
|
const int rot_offset = i % embed_dim;
|
|
|
|
const int x_index = rot_offset;
|
|
|
|
const int y_index = embed_dim + rot_offset;
|
|
|
|
|
|
|
|
const int out_x = token_idx * key_stride + head_idx * head_size + x_index;
|
|
|
|
const int out_y = token_idx * key_stride + head_idx * head_size + y_index;
|
|
|
|
|
|
|
|
const scalar_t cos = __ldg(cache_ptr + x_index);
|
|
|
|
const scalar_t sin = __ldg(cache_ptr + y_index);
|
2023-03-30 11:04:21 -07:00
|
|
|
|
2023-08-02 14:04:39 -07:00
|
|
|
const scalar_t k_x = key[token_head + x_index];
|
|
|
|
const scalar_t k_y = key[token_head + y_index];
|
|
|
|
key[out_x] = k_x * cos - k_y * sin;
|
|
|
|
key[out_y] = k_y * cos + k_x * sin;
|
2023-03-30 11:04:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-17 03:07:40 -07:00
|
|
|
} // namespace vllm
|
2023-03-30 11:04:21 -07:00
|
|
|
|
|
|
|
void rotary_embedding_neox(
|
|
|
|
torch::Tensor& positions, // [num_tokens]
|
|
|
|
torch::Tensor& query, // [num_tokens, num_heads * head_size]
|
2023-07-20 11:38:27 -07:00
|
|
|
torch::Tensor& key, // [num_tokens, num_kv_heads * head_size]
|
2023-04-28 00:32:10 -07:00
|
|
|
int head_size,
|
|
|
|
torch::Tensor& cos_sin_cache) // [max_position, rot_dim]
|
2023-03-30 11:04:21 -07:00
|
|
|
{
|
|
|
|
int num_tokens = query.size(0);
|
2023-04-28 00:32:10 -07:00
|
|
|
int rot_dim = cos_sin_cache.size(1);
|
2023-03-30 11:04:21 -07:00
|
|
|
int num_heads = query.size(1) / head_size;
|
2023-07-20 11:38:27 -07:00
|
|
|
int num_kv_heads = key.size(1) / head_size;
|
2023-08-02 14:04:39 -07:00
|
|
|
int query_stride = query.stride(0);
|
|
|
|
int key_stride = key.stride(0);
|
2023-03-30 11:04:21 -07:00
|
|
|
|
|
|
|
dim3 grid(num_tokens);
|
2023-04-28 00:32:10 -07:00
|
|
|
dim3 block(std::min(num_heads * rot_dim / 2, 512));
|
2023-03-30 11:04:21 -07:00
|
|
|
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
2023-09-02 14:59:47 +09:00
|
|
|
VLLM_DISPATCH_FLOATING_TYPES(
|
2023-03-30 11:04:21 -07:00
|
|
|
query.scalar_type(),
|
|
|
|
"rotary_embedding_neox",
|
|
|
|
[&] {
|
2023-06-17 03:07:40 -07:00
|
|
|
vllm::rotary_embedding_neox_kernel<scalar_t><<<grid, block, 0, stream>>>(
|
2023-03-30 11:04:21 -07:00
|
|
|
positions.data_ptr<int64_t>(),
|
|
|
|
query.data_ptr<scalar_t>(),
|
|
|
|
key.data_ptr<scalar_t>(),
|
|
|
|
cos_sin_cache.data_ptr<scalar_t>(),
|
2023-04-28 00:32:10 -07:00
|
|
|
rot_dim,
|
2023-08-02 14:04:39 -07:00
|
|
|
query_stride,
|
|
|
|
key_stride,
|
2023-03-30 11:04:21 -07:00
|
|
|
num_heads,
|
2023-07-20 11:38:27 -07:00
|
|
|
num_kv_heads,
|
2023-03-30 11:04:21 -07:00
|
|
|
head_size);
|
|
|
|
});
|
|
|
|
}
|