pos_encoding_kernels.cu 3.6 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,
Zhuohan Li's avatar
Zhuohan Li committed
13
14
  const int query_stride,
  const int key_stride,
15
  const int num_heads,
Zhuohan Li's avatar
Zhuohan Li committed
16
  const int num_kv_heads,
17
18
19
20
  const int head_size) {
  // Each thread block is responsible for one token.
  const int token_idx = blockIdx.x;
  int64_t pos = positions[token_idx];
21
  const scalar_t* cache_ptr = cos_sin_cache + pos * rot_dim;
22

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

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

Zhuohan Li's avatar
Zhuohan Li committed
33
34
    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;
Woosuk Kwon's avatar
Woosuk Kwon committed
35

36
37
38
    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
39
40
41
42
    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;
Zhuohan Li's avatar
Zhuohan Li committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  }

  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);
59

Zhuohan Li's avatar
Zhuohan Li committed
60
61
62
63
    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;
64
65
66
  }
}

Woosuk Kwon's avatar
Woosuk Kwon committed
67
} // namespace vllm
68
69
70
71

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
72
  torch::Tensor& key,               // [num_tokens, num_kv_heads * head_size]
73
74
  int head_size,
  torch::Tensor& cos_sin_cache)     // [max_position, rot_dim]
75
76
{
  int num_tokens = query.size(0);
77
  int rot_dim = cos_sin_cache.size(1);
78
  int num_heads = query.size(1) / head_size;
Zhuohan Li's avatar
Zhuohan Li committed
79
  int num_kv_heads = key.size(1) / head_size;
Zhuohan Li's avatar
Zhuohan Li committed
80
81
  int query_stride = query.stride(0);
  int key_stride = key.stride(0);
82
83

  dim3 grid(num_tokens);
84
  dim3 block(std::min(num_heads * rot_dim / 2, 512));
85
  const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
Woosuk Kwon's avatar
Woosuk Kwon committed
86
87
88
  AT_DISPATCH_FLOATING_TYPES_AND2(
    at::ScalarType::Half,
    at::ScalarType::BFloat16,
89
90
91
    query.scalar_type(),
    "rotary_embedding_neox",
    [&] {
Woosuk Kwon's avatar
Woosuk Kwon committed
92
      vllm::rotary_embedding_neox_kernel<scalar_t><<<grid, block, 0, stream>>>(
93
94
95
96
        positions.data_ptr<int64_t>(),
        query.data_ptr<scalar_t>(),
        key.data_ptr<scalar_t>(),
        cos_sin_cache.data_ptr<scalar_t>(),
97
        rot_dim,
Zhuohan Li's avatar
Zhuohan Li committed
98
99
        query_stride,
        key_stride,
100
        num_heads,
Zhuohan Li's avatar
Zhuohan Li committed
101
        num_kv_heads,
102
103
104
        head_size);
    });
}