main
cpp 366 lines 13.7 KB
Raw
1 #include "cpu_types.hpp"
2
3 namespace {
4 template <typename scalar_t>
5 void rotary_embedding_impl(
6 const int64_t* __restrict__ positions, // [batch_size, seq_len] or
7 // [num_tokens]
8 scalar_t* __restrict__ query, /// [batch_size, seq_len, num_heads,
9 /// head_size] or [num_tokens, num_heads,
10 /// head_size]
11 scalar_t* __restrict__ key, // nullptr (optional) or
12 // [batch_size, seq_len, num_kv_heads,
13 // head_size] or [num_tokens, num_kv_heads,
14 // head_size]
15 const scalar_t* __restrict__ cos_sin_cache, // [max_position, 2, rot_dim //
16 // 2]
17 const int rot_dim, const int64_t query_stride, const int64_t key_stride,
18 const int num_heads, const int num_kv_heads, const int head_size,
19 const int num_tokens) {
20 using scalar_vec_t = vec_op::vec_t<scalar_t>;
21 constexpr int VEC_ELEM_NUM = scalar_vec_t::get_elem_num();
22
23 const int embed_dim = rot_dim / 2;
24 bool flag = (embed_dim % VEC_ELEM_NUM == 0);
25 const int loop_upper = flag ? embed_dim : embed_dim - VEC_ELEM_NUM;
26
27 auto compute_loop = [&](const int64_t token_head, const scalar_t* cache_ptr,
28 scalar_t* qk) {
29 int j = 0;
30 for (; j < loop_upper; j += VEC_ELEM_NUM) {
31 const int rot_offset = j;
32 const int x_index = rot_offset;
33 const int y_index = embed_dim + rot_offset;
34
35 const int64_t out_x = token_head + x_index;
36 const int64_t out_y = token_head + y_index;
37
38 const scalar_vec_t cos(cache_ptr + x_index);
39 const scalar_vec_t sin(cache_ptr + y_index);
40
41 const scalar_vec_t q_x(qk + out_x);
42 const scalar_vec_t q_y(qk + out_y);
43
44 vec_op::FP32Vec8 fp32_cos(cos);
45 vec_op::FP32Vec8 fp32_sin(sin);
46
47 vec_op::FP32Vec8 fp32_q_x(q_x);
48 vec_op::FP32Vec8 fp32_q_y(q_y);
49
50 auto out1 = fp32_q_x * fp32_cos - fp32_q_y * fp32_sin;
51 scalar_vec_t(out1).save(qk + out_x);
52
53 auto out2 = fp32_q_y * fp32_cos + fp32_q_x * fp32_sin;
54 scalar_vec_t(out2).save(qk + out_y);
55 }
56 if (!flag) {
57 for (; j < embed_dim; ++j) {
58 const int x_index = j;
59 const int y_index = embed_dim + j;
60
61 const int64_t out_x = token_head + x_index;
62 const int64_t out_y = token_head + y_index;
63
64 const float fp32_cos = cache_ptr[x_index];
65 const float fp32_sin = cache_ptr[y_index];
66
67 const float fp32_q_x = qk[out_x];
68 const float fp32_q_y = qk[out_y];
69
70 qk[out_x] = fp32_q_x * fp32_cos - fp32_q_y * fp32_sin;
71 qk[out_y] = fp32_q_y * fp32_cos + fp32_q_x * fp32_sin;
72 }
73 }
74 };
75
76 #pragma omp parallel for
77 for (int token_idx = 0; token_idx < num_tokens; ++token_idx) {
78 int64_t pos = positions[token_idx];
79 const scalar_t* cache_ptr = cos_sin_cache + pos * rot_dim;
80
81 for (int i = 0; i < num_heads; ++i) {
82 const int head_idx = i;
83 const int64_t token_head =
84 token_idx * query_stride + head_idx * head_size;
85 compute_loop(token_head, cache_ptr, query);
86 }
87
88 if (key != nullptr) {
89 for (int i = 0; i < num_kv_heads; ++i) {
90 const int head_idx = i;
91 const int64_t token_head =
92 token_idx * key_stride + head_idx * head_size;
93 compute_loop(token_head, cache_ptr, key);
94 }
95 }
96 }
97 }
98
99 template <>
100 void rotary_embedding_impl<c10::Half>(
101 const int64_t* __restrict__ positions, c10::Half* __restrict__ query,
102 c10::Half* __restrict__ key, const c10::Half* __restrict__ cos_sin_cache,
103 const int rot_dim, const int64_t query_stride, const int64_t key_stride,
104 const int num_heads, const int num_kv_heads, const int head_size,
105 const int num_tokens) {
106 using scalar_vec_t = vec_op::FP16Vec8;
107 constexpr int VEC_ELEM_NUM = scalar_vec_t::get_elem_num();
108
109 const int embed_dim = rot_dim / 2;
110 bool flag = (embed_dim % VEC_ELEM_NUM == 0);
111 const int loop_upper = flag ? embed_dim : embed_dim - VEC_ELEM_NUM;
112
113 auto compute_loop = [&](const int64_t token_head, const c10::Half* cache_ptr,
114 c10::Half* qk) {
115 int j = 0;
116 for (; j < loop_upper; j += VEC_ELEM_NUM) {
117 const int rot_offset = j;
118 const int x_index = rot_offset;
119 const int y_index = embed_dim + rot_offset;
120
121 const int64_t out_x = token_head + x_index;
122 const int64_t out_y = token_head + y_index;
123
124 const vec_op::FP16Vec8 cos_fp16(cache_ptr + x_index);
125 const vec_op::FP16Vec8 sin_fp16(cache_ptr + y_index);
126 const vec_op::FP16Vec8 q_x_fp16(qk + out_x);
127 const vec_op::FP16Vec8 q_y_fp16(qk + out_y);
128
129 const vec_op::FP32Vec8 fp32_cos(cos_fp16);
130 const vec_op::FP32Vec8 fp32_sin(sin_fp16);
131 const vec_op::FP32Vec8 fp32_q_x(q_x_fp16);
132 const vec_op::FP32Vec8 fp32_q_y(q_y_fp16);
133
134 auto out1 = fp32_q_x * fp32_cos - fp32_q_y * fp32_sin;
135 auto out2 = fp32_q_y * fp32_cos + fp32_q_x * fp32_sin;
136
137 vec_op::FP16Vec8(out1).save(qk + out_x);
138 vec_op::FP16Vec8(out2).save(qk + out_y);
139 }
140 if (!flag) {
141 for (; j < embed_dim; ++j) {
142 const int x_index = j;
143 const int y_index = embed_dim + j;
144
145 const int64_t out_x = token_head + x_index;
146 const int64_t out_y = token_head + y_index;
147
148 const float fp32_cos = static_cast<float>(cache_ptr[x_index]);
149 const float fp32_sin = static_cast<float>(cache_ptr[y_index]);
150 const float fp32_q_x = static_cast<float>(qk[out_x]);
151 const float fp32_q_y = static_cast<float>(qk[out_y]);
152
153 qk[out_x] =
154 static_cast<c10::Half>(fp32_q_x * fp32_cos - fp32_q_y * fp32_sin);
155 qk[out_y] =
156 static_cast<c10::Half>(fp32_q_y * fp32_cos + fp32_q_x * fp32_sin);
157 }
158 }
159 };
160
161 #pragma omp parallel for
162 for (int token_idx = 0; token_idx < num_tokens; ++token_idx) {
163 int64_t pos = positions[token_idx];
164 const c10::Half* cache_ptr = cos_sin_cache + pos * rot_dim;
165
166 for (int i = 0; i < num_heads; ++i) {
167 const int head_idx = i;
168 const int64_t token_head =
169 token_idx * query_stride + head_idx * head_size;
170 compute_loop(token_head, cache_ptr, query);
171 }
172
173 if (key != nullptr) {
174 for (int i = 0; i < num_kv_heads; ++i) {
175 const int head_idx = i;
176 const int64_t token_head =
177 token_idx * key_stride + head_idx * head_size;
178 compute_loop(token_head, cache_ptr, key);
179 }
180 }
181 }
182 }
183
184 template <typename scalar_t>
185 void rotary_embedding_gptj_impl(
186 const int64_t* __restrict__ positions, // [batch_size, seq_len] or
187 // [num_tokens]
188 scalar_t* __restrict__ query, /// [batch_size, seq_len, num_heads,
189 /// head_size] or [num_tokens, num_heads,
190 /// head_size]
191 scalar_t* __restrict__ key, // nullptr (optional) or
192 // [batch_size, seq_len, num_kv_heads,
193 // head_size] or [num_tokens, num_kv_heads,
194 // head_size]
195 const scalar_t* __restrict__ cos_sin_cache, // [max_position, 2, rot_dim //
196 // 2]
197 const int rot_dim, const int64_t query_stride, const int64_t key_stride,
198 const int num_heads, const int num_kv_heads, const int head_size,
199 const int num_tokens) {
200 const int embed_dim = rot_dim / 2;
201
202 #pragma omp parallel for collapse(2)
203 for (int token_idx = 0; token_idx < num_tokens; ++token_idx) {
204 for (int i = 0; i < num_heads; ++i) {
205 int64_t pos = positions[token_idx];
206 const scalar_t* cache_ptr = cos_sin_cache + pos * rot_dim;
207 const scalar_t* cos_cache_ptr = cache_ptr;
208 const scalar_t* sin_cache_ptr = cache_ptr + embed_dim;
209 const int head_idx = i;
210 const int64_t token_head =
211 token_idx * query_stride + head_idx * head_size;
212 scalar_t* head_query = token_head + query;
213 for (int j = 0; j < embed_dim; j += 1) {
214 const int rot_offset = j;
215 const int x_index = 2 * rot_offset;
216 const int y_index = 2 * rot_offset + 1;
217
218 const float cos = cos_cache_ptr[rot_offset];
219 const float sin = sin_cache_ptr[rot_offset];
220
221 const float x = head_query[x_index];
222 const float y = head_query[y_index];
223
224 head_query[x_index] = x * cos - y * sin;
225 head_query[y_index] = y * cos + x * sin;
226 }
227 }
228 }
229
230 if (key == nullptr) {
231 return;
232 }
233
234 #pragma omp parallel for collapse(2)
235 for (int token_idx = 0; token_idx < num_tokens; ++token_idx) {
236 for (int i = 0; i < num_kv_heads; ++i) {
237 int64_t pos = positions[token_idx];
238 const scalar_t* cache_ptr = cos_sin_cache + pos * rot_dim;
239 const scalar_t* cos_cache_ptr = cache_ptr;
240 const scalar_t* sin_cache_ptr = cache_ptr + embed_dim;
241 const int head_idx = i;
242 const int64_t token_head = token_idx * key_stride + head_idx * head_size;
243 scalar_t* head_key = key + token_head;
244 for (int j = 0; j < embed_dim; j += 1) {
245 const int rot_offset = j;
246 const int x_index = 2 * rot_offset;
247 const int y_index = 2 * rot_offset + 1;
248
249 const float cos = cos_cache_ptr[rot_offset];
250 const float sin = sin_cache_ptr[rot_offset];
251
252 const float x = head_key[x_index];
253 const float y = head_key[y_index];
254
255 head_key[x_index] = x * cos - y * sin;
256 head_key[y_index] = y * cos + x * sin;
257 }
258 }
259 }
260 }
261
262 template <>
263 void rotary_embedding_gptj_impl<c10::Half>(
264 const int64_t* __restrict__ positions, c10::Half* __restrict__ query,
265 c10::Half* __restrict__ key, const c10::Half* __restrict__ cos_sin_cache,
266 const int rot_dim, const int64_t query_stride, const int64_t key_stride,
267 const int num_heads, const int num_kv_heads, const int head_size,
268 const int num_tokens) {
269 const int embed_dim = rot_dim / 2;
270
271 #pragma omp parallel for collapse(2)
272 for (int token_idx = 0; token_idx < num_tokens; ++token_idx) {
273 for (int i = 0; i < num_heads; ++i) {
274 int64_t pos = positions[token_idx];
275 const c10::Half* cache_ptr = cos_sin_cache + pos * rot_dim;
276 const c10::Half* cos_cache_ptr = cache_ptr;
277 const c10::Half* sin_cache_ptr = cache_ptr + embed_dim;
278 const int head_idx = i;
279 const int64_t token_head =
280 token_idx * query_stride + head_idx * head_size;
281 c10::Half* head_query = token_head + query;
282 for (int j = 0; j < embed_dim; j += 1) {
283 const int rot_offset = j;
284 const int x_index = 2 * rot_offset;
285 const int y_index = 2 * rot_offset + 1;
286
287 const float cos = static_cast<float>(cos_cache_ptr[rot_offset]);
288 const float sin = static_cast<float>(sin_cache_ptr[rot_offset]);
289
290 const float x = static_cast<float>(head_query[x_index]);
291 const float y = static_cast<float>(head_query[y_index]);
292
293 head_query[x_index] = static_cast<c10::Half>(x * cos - y * sin);
294 head_query[y_index] = static_cast<c10::Half>(y * cos + x * sin);
295 }
296 }
297 }
298
299 if (key == nullptr) {
300 return;
301 }
302
303 #pragma omp parallel for collapse(2)
304 for (int token_idx = 0; token_idx < num_tokens; ++token_idx) {
305 for (int i = 0; i < num_kv_heads; ++i) {
306 int64_t pos = positions[token_idx];
307 const c10::Half* cache_ptr = cos_sin_cache + pos * rot_dim;
308 const c10::Half* cos_cache_ptr = cache_ptr;
309 const c10::Half* sin_cache_ptr = cache_ptr + embed_dim;
310 const int head_idx = i;
311 const int64_t token_head = token_idx * key_stride + head_idx * head_size;
312 c10::Half* head_key = key + token_head;
313 for (int j = 0; j < embed_dim; j += 1) {
314 const int rot_offset = j;
315 const int x_index = 2 * rot_offset;
316 const int y_index = 2 * rot_offset + 1;
317
318 const float cos = static_cast<float>(cos_cache_ptr[rot_offset]);
319 const float sin = static_cast<float>(sin_cache_ptr[rot_offset]);
320
321 const float x = static_cast<float>(head_key[x_index]);
322 const float y = static_cast<float>(head_key[y_index]);
323
324 head_key[x_index] = static_cast<c10::Half>(x * cos - y * sin);
325 head_key[y_index] = static_cast<c10::Half>(y * cos + x * sin);
326 }
327 }
328 }
329 }
330 }; // namespace
331
332 void rotary_embedding(torch::Tensor& positions, torch::Tensor& query,
333 std::optional<torch::Tensor> key, int64_t head_size,
334 torch::Tensor& cos_sin_cache, bool is_neox,
335 int64_t rope_dim_offset, bool inverse) {
336 TORCH_CHECK(rope_dim_offset == 0,
337 "rope_dim_offset != 0 is not supported on CPU");
338 TORCH_CHECK(!inverse, "inverse rotary embedding is not supported on CPU");
339
340 int num_tokens = positions.numel();
341 int rot_dim = cos_sin_cache.size(1);
342 int num_heads = query.size(-1) / head_size;
343 int num_kv_heads = key.has_value() ? key->size(-1) / head_size : num_heads;
344 int64_t key_stride = key.has_value() ? key->stride(-2) : 0;
345 int64_t query_stride = query.stride(-2);
346
347 VLLM_DISPATCH_FLOATING_TYPES(
348 query.scalar_type(), "rotary_embedding_impl", [&] {
349 CPU_KERNEL_GUARD_IN(rotary_embedding_impl)
350 if (is_neox) {
351 rotary_embedding_impl(
352 positions.data_ptr<int64_t>(), query.data_ptr<scalar_t>(),
353 key.has_value() ? key->data_ptr<scalar_t>() : nullptr,
354 cos_sin_cache.data_ptr<scalar_t>(), rot_dim, query_stride,
355 key_stride, num_heads, num_kv_heads, head_size, num_tokens);
356 } else {
357 rotary_embedding_gptj_impl(
358 positions.data_ptr<int64_t>(), query.data_ptr<scalar_t>(),
359 key.has_value() ? key->data_ptr<scalar_t>() : nullptr,
360 cos_sin_cache.data_ptr<scalar_t>(), rot_dim, query_stride,
361 key_stride, num_heads, num_kv_heads, head_size, num_tokens);
362 }
363
364 CPU_KERNEL_GUARD_OUT(rotary_embedding_impl)
365 });
366 }