attention_utils.cuh 3.66 KB
Newer Older
1
/*
2
3
 * Adapted from
 * https://github.com/NVIDIA/FasterTransformer/blob/release/v5.3_tag/src/fastertransformer/kernels/decoder_masked_multihead_attention/decoder_masked_multihead_attention_template.hpp
Woosuk Kwon's avatar
Woosuk Kwon committed
4
 * Copyright (c) 2023, The vLLM team.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 * Copyright (c) 2020-2023, NVIDIA CORPORATION.  All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Woosuk Kwon's avatar
Woosuk Kwon committed
19
20
#pragma once

21
#include "../cuda_compat.h"
Woosuk Kwon's avatar
Woosuk Kwon committed
22
#include "attention_dtypes.h"
Woosuk Kwon's avatar
Woosuk Kwon committed
23
24
25
26

#include <float.h>
#include <type_traits>

Woosuk Kwon's avatar
Woosuk Kwon committed
27
namespace vllm {
Woosuk Kwon's avatar
Woosuk Kwon committed
28

zhangshao's avatar
zhangshao committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
inline __device__ void v_dot2_f32_f16(float& a, const uint32_t &  b,const uint32_t &  c) {
  asm volatile("v_dot2_f32_f16 %0, %1, %2, %0;": "=v"(a): "v"(b), "v"(c), "0"(a));
}

inline __device__ void v_pk_fma_f16(uint32_t& a, const uint32_t &  b,const uint32_t &  c){
   asm volatile("v_pk_fma_f16 %0, %1, %2, %3;": "=v"(a) : "v"(b), "v"(c), "v"(a));
}

inline __device__ void v_dot2_f32_f16(float& a,const uint2 &  b,const uint2 &  c) {
  v_dot2_f32_f16(a, b.x, c.x);
  v_dot2_f32_f16(a, b.y, c.y);
}

inline __device__ void v_dot2_f32_f16(float& a,const uint4 &  b,const uint4 &  c) {
  v_dot2_f32_f16(a, b.x, c.x);
  v_dot2_f32_f16(a, b.y, c.y);
  v_dot2_f32_f16(a, b.z, c.z);
  v_dot2_f32_f16(a, b.w, c.w);
}

inline __device__ float add_half2(uint32_t a){
 union {
    uint32_t u32;
    half u16[2];
  } tmp;
  tmp.u32=a;
  return static_cast<float>(tmp.u16[0]+tmp.u16[1]);
}

inline __device__ void v_pk_fma_f16x8(float& a,const uint4 &  b,const uint4 &  c) {
  uint32_t tmp = mul<uint32_t, uint32_t, uint32_t>(b.x,c.x);
  v_pk_fma_f16(tmp,b.y,c.y);
  v_pk_fma_f16(tmp,b.z,c.z);
  v_pk_fma_f16(tmp,b.w,c.w);
  a+=add_half2(tmp);
}

// Q*K^T operation. fp16
// template <int THREAD_GROUP_SIZE, typename Vec, int N, typename scalar_t, std::enable_if_t<std::is_same<scalar_t, uint16_t>::value, int> = 0>
68
template <int THREAD_GROUP_SIZE, typename Vec, int N>
Woosuk Kwon's avatar
Woosuk Kwon committed
69
inline __device__ float qk_dot_(const Vec (&q)[N], const Vec (&k)[N]) {
zhangshao's avatar
zhangshao committed
70
71
  
  float qk =0;
Woosuk Kwon's avatar
Woosuk Kwon committed
72
  // Compute the parallel products for Q*K^T (treat vector lanes separately).
zhangshao's avatar
zhangshao committed
73
74
75
  #pragma unroll
  for (int ii = 0; ii < N; ++ii) {
    v_dot2_f32_f16(qk,q[ii],k[ii]);
Woosuk Kwon's avatar
Woosuk Kwon committed
76
77
78
79
  }
  // Finalize the reduction across lanes.
#pragma unroll
  for (int mask = THREAD_GROUP_SIZE / 2; mask >= 1; mask /= 2) {
80
    qk += VLLM_SHFL_XOR_SYNC(qk, mask);
Woosuk Kwon's avatar
Woosuk Kwon committed
81
82
83
84
  }
  return qk;
}

zhangshao's avatar
zhangshao committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Q*K^T operation. //bf16
// template <int THREAD_GROUP_SIZE, typename Vec, int N, typename scalar_t, std::enable_if_t<!std::is_same<scalar_t, uint16_t>::value, int> = 0>
// inline __device__ float qk_dot_(const Vec (&q)[N], const Vec (&k)[N]) {

//   using A_vec = typename FloatVec<Vec>::Type;
//   A_vec qk_vec = mul<A_vec, Vec, Vec>(q[0], k[0]);
//   #pragma unroll
//   for (int ii = 1; ii < N; ++ii) {
//     qk_vec = fma(q[ii], k[ii], qk_vec);
//   }
//   float qk = sum(qk_vec);
//   // Finalize the reduction across lanes.
// #pragma unroll
//   for (int mask = THREAD_GROUP_SIZE / 2; mask >= 1; mask /= 2) {
//     qk += VLLM_SHFL_XOR_SYNC(qk, mask);
//   }
//   return qk;
// }


105
template <typename T, int THREAD_GROUP_SIZE>
Woosuk Kwon's avatar
Woosuk Kwon committed
106
struct Qk_dot {
107
  template <typename Vec, int N>
Woosuk Kwon's avatar
Woosuk Kwon committed
108
109
110
111
112
  static inline __device__ float dot(const Vec (&q)[N], const Vec (&k)[N]) {
    return qk_dot_<THREAD_GROUP_SIZE>(q, k);
  }
};

113
}  // namespace vllm