pos_encoding_kernels.cu 2.98 KB
Newer Older
1
2
3
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>

Woosuk Kwon's avatar
Woosuk Kwon committed
4
namespace vllm {
5
6
7
8

template<typename scalar_t>
__global__ void rotary_embedding_neox_kernel(
  const int64_t* __restrict__ positions,        // [num_tokens]
Woosuk Kwon's avatar
Woosuk Kwon committed
9
  scalar_t* __restrict__ query,                 // [num_tokens, num_heads, head_size]
Zhuohan Li's avatar
Zhuohan Li committed
10
  scalar_t* __restrict__ key,                   // [num_tokens, num_kv_heads, head_size]
11
12
  const scalar_t* __restrict__ cos_sin_cache,   // [max_position, 2, rot_dim // 2]
  const int rot_dim,
Woosuk Kwon's avatar
Woosuk Kwon committed
13
  const int stride,
14
  const int num_heads,
Zhuohan Li's avatar
Zhuohan Li committed
15
  const int num_kv_heads,
16
17
18
19
  const int head_size) {
  // Each thread block is responsible for one token.
  const int token_idx = blockIdx.x;
  int64_t pos = positions[token_idx];
20
  const scalar_t* cache_ptr = cos_sin_cache + pos * rot_dim;
21

22
  const int embed_dim = rot_dim / 2;
Zhuohan Li's avatar
Zhuohan Li committed
23
24
  const int nq = num_heads * embed_dim;
  for (int i = threadIdx.x; i < nq; i += blockDim.x) {
Woosuk Kwon's avatar
Woosuk Kwon committed
25
26
    const int head_idx = i / embed_dim;
    const int token_head = token_idx * stride + head_idx * head_size;
27

Woosuk Kwon's avatar
Woosuk Kwon committed
28
    const int rot_offset = i % embed_dim;
29
30
31
    const int x_index = rot_offset;
    const int y_index = embed_dim + rot_offset;

Woosuk Kwon's avatar
Woosuk Kwon committed
32
33
34
    const int out_x = token_idx * stride + head_idx * head_size + x_index;
    const int out_y = token_idx * stride + head_idx * head_size + y_index;

35
36
37
    const scalar_t cos = __ldg(cache_ptr + x_index);
    const scalar_t sin = __ldg(cache_ptr + y_index);

Woosuk Kwon's avatar
Woosuk Kwon committed
38
39
40
41
    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;
42

Zhuohan Li's avatar
Zhuohan Li committed
43
44
45
46
47
48
    if (head_idx < num_kv_heads) {
      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;
    }
49
50
51
  }
}

Woosuk Kwon's avatar
Woosuk Kwon committed
52
} // namespace vllm
53
54
55
56

void rotary_embedding_neox(
  torch::Tensor& positions,         // [num_tokens]
  torch::Tensor& query,             // [num_tokens, num_heads * head_size]
Zhuohan Li's avatar
Zhuohan Li committed
57
  torch::Tensor& key,               // [num_tokens, num_kv_heads * head_size]
58
59
  int head_size,
  torch::Tensor& cos_sin_cache)     // [max_position, rot_dim]
60
61
{
  int num_tokens = query.size(0);
62
  int rot_dim = cos_sin_cache.size(1);
63
  int num_heads = query.size(1) / head_size;
Zhuohan Li's avatar
Zhuohan Li committed
64
  int num_kv_heads = key.size(1) / head_size;
Woosuk Kwon's avatar
Woosuk Kwon committed
65
66
  int stride = query.stride(0);
  TORCH_CHECK(stride == key.stride(0));
67
68

  dim3 grid(num_tokens);
69
  dim3 block(std::min(num_heads * rot_dim / 2, 512));
70
  const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
Woosuk Kwon's avatar
Woosuk Kwon committed
71
72
73
  AT_DISPATCH_FLOATING_TYPES_AND2(
    at::ScalarType::Half,
    at::ScalarType::BFloat16,
74
75
76
    query.scalar_type(),
    "rotary_embedding_neox",
    [&] {
Woosuk Kwon's avatar
Woosuk Kwon committed
77
      vllm::rotary_embedding_neox_kernel<scalar_t><<<grid, block, 0, stream>>>(
78
79
80
81
        positions.data_ptr<int64_t>(),
        query.data_ptr<scalar_t>(),
        key.data_ptr<scalar_t>(),
        cos_sin_cache.data_ptr<scalar_t>(),
82
        rot_dim,
Woosuk Kwon's avatar
Woosuk Kwon committed
83
        stride,
84
        num_heads,
Zhuohan Li's avatar
Zhuohan Li committed
85
        num_kv_heads,
86
87
88
        head_size);
    });
}