attention_utils.cuh 4.92 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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 ds_read_b128(uint4& a, uint32_t offset){
    asm volatile("ds_read_b128 %0 %1;": "=v" (a): "v" (offset));
}

inline __device__ void ds_read_b128_sync(uint4& a, uint32_t offset){
    asm volatile("ds_read_b128 %0 %1\ns_waitcnt lgkmcnt(1);": "=v" (a): "v" (offset));
}

inline __device__ void lgkmcnt0(){
    asm volatile("s_waitcnt lgkmcnt(0);");
}

__device__ inline size_t  __nv_cvta_generic_to_shared_impl(const void *__ptr) {
        return (size_t)(void __attribute__((address_space(3))) *)__ptr;
}

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
zhangshao's avatar
zhangshao committed
83
84
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>
// template <int THREAD_GROUP_SIZE, typename Vec, int N>
Woosuk Kwon's avatar
Woosuk Kwon committed
85
inline __device__ float qk_dot_(const Vec (&q)[N], const Vec (&k)[N]) {
zhangshao's avatar
zhangshao committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  
  float qk =0;
  // uint32_t offset = __nv_cvta_generic_to_shared_impl(q);
  // const uint4 *k_ptr= reinterpret_cast<const uint4 *>(k);
  // // Compute the parallel products for Q*K^T (treat vector lanes separately).

  // constexpr int loop=N*sizeof(Vec)/16/2;
  // uint4 qt[2];
  // #pragma unroll
  // for (int ii = 0; ii < loop; ++ii) {
  //   ds_read_b128(qt[0],offset+16*ii*2);
  //   ds_read_b128_sync(qt[1],offset+16*(ii*2+1));
  //   v_dot2_f32_f16(qk,qt[0],k_ptr[ii*2]);
  //   // v_pk_fma_f16x8(qk,qt[0],k_ptr[ii*2]);
  //   lgkmcnt0();
  //   v_dot2_f32_f16(qk,qt[1],k_ptr[ii*2+1]);
  //   // v_pk_fma_f16x8(qk,qt[1],k_ptr[ii*2+1]);
  // }
  #pragma unroll
  for (int ii = 0; ii < N; ++ii) {
    v_dot2_f32_f16(qk,q[ii],k[ii]);
  }
  // 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;
}

// Q*K^T operation. //bf16
zhangshao's avatar
zhangshao committed
117
118
119
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>
// template <int THREAD_GROUP_SIZE, typename Vec, int N>
inline __device__ float qk_dot_(const Vec (&q)[N], const Vec (&k)[N]) {
zhangshao's avatar
zhangshao committed
120

121
122
  using A_vec = typename FloatVec<Vec>::Type;
  A_vec qk_vec = mul<A_vec, Vec, Vec>(q[0], k[0]);
zhangshao's avatar
zhangshao committed
123
  #pragma unroll
124
  for (int ii = 1; ii < N; ++ii) {
125
    qk_vec = vllm::fma(q[ii], k[ii], qk_vec);
126
  }
zhangshao's avatar
zhangshao committed
127
  // Finalize the reduction across lanes.
zhuwenwen's avatar
zhuwenwen committed
128
  float qk = sum(qk_vec);
129
130
131
132
133
134
#pragma unroll
  for (int mask = THREAD_GROUP_SIZE / 2; mask >= 1; mask /= 2) {
    qk += VLLM_SHFL_XOR_SYNC(qk, mask);
  }
  return qk;
}
zhangshao's avatar
zhangshao committed
135

zhangshao's avatar
zhangshao committed
136

137
template <typename T, int THREAD_GROUP_SIZE>
Woosuk Kwon's avatar
Woosuk Kwon committed
138
struct Qk_dot {
139
  template <typename Vec, int N>
Woosuk Kwon's avatar
Woosuk Kwon committed
140
  static inline __device__ float dot(const Vec (&q)[N], const Vec (&k)[N]) {
zhangshao's avatar
zhangshao committed
141
    return qk_dot_<THREAD_GROUP_SIZE,Vec,N,T>(q, k);
Woosuk Kwon's avatar
Woosuk Kwon committed
142
  }
zhangshao's avatar
zhangshao committed
143
144
145
146
  // template <typename Vec, int N>
  // static inline __device__ float qk_dot_vpack(const Vec (&q)[N], const Vec (&k)[N]) {
  //   return qk_dot_vpack_<THREAD_GROUP_SIZE>(q, k);
  // }
Woosuk Kwon's avatar
Woosuk Kwon committed
147
148
};

zhangshao's avatar
zhangshao committed
149
}  // namespace vllm