layernorm_kernels.cu 10.6 KB
Newer Older
1
2
#include "type_convert.cuh"
#include "dispatch_utils.h"
Aidyn-A's avatar
Aidyn-A committed
3
#include "cub_helpers.h"
4
#include "core/batch_invariant.hpp"
5
#include "quantization/vectorization_utils.cuh"
6
7

#include <torch/cuda.h>
8
#include <c10/cuda/CUDAGuard.h>
9

Woosuk Kwon's avatar
Woosuk Kwon committed
10
namespace vllm {
11
12

// TODO(woosuk): Further optimize this kernel.
13
template <typename scalar_t, int VEC_SIZE>
14
__global__ void rms_norm_kernel(
15
16
17
    scalar_t* __restrict__ out,          // [..., hidden_size]
    const scalar_t* __restrict__ input,  // [..., hidden_size]
    const int64_t input_stride,
18
19
    const scalar_t* __restrict__ weight,  // [hidden_size]
    const float epsilon, const int num_tokens, const int hidden_size) {
20
21
  __shared__ float s_variance;
  float variance = 0.0f;
22
  const scalar_t* input_row = input + blockIdx.x * input_stride;
23

24
25
26
27
28
29
30
31
32
  auto vec_op = [&variance](const vec_n_t<scalar_t, VEC_SIZE>& vec) {
#pragma unroll
    for (int i = 0; i < VEC_SIZE; ++i) {
      float x = static_cast<float>(vec.val[i]);
      variance += x * x;
    }
  };
  auto scalar_op = [&variance](const scalar_t& val) {
    float x = static_cast<float>(val);
33
    variance += x * x;
34
35
36
  };
  vllm::vectorize_read_with_alignment<VEC_SIZE>(
      input_row, hidden_size, threadIdx.x, blockDim.x, vec_op, scalar_op);
37
38
39

  using BlockReduce = cub::BlockReduce<float, 1024>;
  __shared__ typename BlockReduce::TempStorage reduceStore;
Aidyn-A's avatar
Aidyn-A committed
40
  variance = BlockReduce(reduceStore).Reduce(variance, CubAddOp{}, blockDim.x);
41

42
43
44
45
46
  if (threadIdx.x == 0) {
    s_variance = rsqrtf(variance / hidden_size + epsilon);
  }
  __syncthreads();

47
48
49
50
51
52
53
54
55
56
57
58
59
60
  scalar_t* out_row = out + blockIdx.x * hidden_size;
  auto* v_in = reinterpret_cast<const vec_n_t<scalar_t, VEC_SIZE>*>(input_row);
  auto* v_w = reinterpret_cast<const vec_n_t<scalar_t, VEC_SIZE>*>(weight);
  auto* v_out = reinterpret_cast<vec_n_t<scalar_t, VEC_SIZE>*>(out_row);
  for (int i = threadIdx.x; i < hidden_size / VEC_SIZE; i += blockDim.x) {
    vec_n_t<scalar_t, VEC_SIZE> dst;
    vec_n_t<scalar_t, VEC_SIZE> src1 = v_in[i];
    vec_n_t<scalar_t, VEC_SIZE> src2 = v_w[i];
#pragma unroll
    for (int j = 0; j < VEC_SIZE; j++) {
      float x = static_cast<float>(src1.val[j]);
      dst.val[j] = ((scalar_t)(x * s_variance)) * src2.val[j];
    }
    v_out[i] = dst;
61
62
63
  }
}

64
65
66
67
/* Function specialization in the case of FP16/BF16 tensors.
   Additional optimizations we can make in this case are
   packed and vectorized operations, which help with the
   memory latency bottleneck. */
68
69
70
template <typename scalar_t, int width>
__global__ std::enable_if_t<(width > 0) && _typeConvert<scalar_t>::exists>
fused_add_rms_norm_kernel(
71
72
    scalar_t* __restrict__ input,  // [..., hidden_size]
    const int64_t input_stride,
73
74
75
    scalar_t* __restrict__ residual,      // [..., hidden_size]
    const scalar_t* __restrict__ weight,  // [hidden_size]
    const float epsilon, const int num_tokens, const int hidden_size) {
76
77
78
79
80
  // Sanity checks on our vector struct and type-punned pointer arithmetic
  static_assert(std::is_pod_v<_f16Vec<scalar_t, width>>);
  static_assert(sizeof(_f16Vec<scalar_t, width>) == sizeof(scalar_t) * width);

  const int vec_hidden_size = hidden_size / width;
81
  const int64_t vec_input_stride = input_stride / width;
82
83
84
85
86
  __shared__ float s_variance;
  float variance = 0.0f;
  /* These and the argument pointers are all declared `restrict` as they are
     not aliased in practice. Argument pointers should not be dereferenced
     in this kernel as that would be undefined behavior */
87
88
89
90
91
92
  auto* __restrict__ input_v =
      reinterpret_cast<_f16Vec<scalar_t, width>*>(input);
  auto* __restrict__ residual_v =
      reinterpret_cast<_f16Vec<scalar_t, width>*>(residual);
  auto* __restrict__ weight_v =
      reinterpret_cast<const _f16Vec<scalar_t, width>*>(weight);
93
94
95

  for (int idx = threadIdx.x; idx < vec_hidden_size; idx += blockDim.x) {
    int id = blockIdx.x * vec_hidden_size + idx;
96
97
    int64_t strided_id = blockIdx.x * vec_input_stride + idx;
    _f16Vec<scalar_t, width> temp = input_v[strided_id];
98
99
100
101
    temp += residual_v[id];
    variance += temp.sum_squares();
    residual_v[id] = temp;
  }
102
103
104

  using BlockReduce = cub::BlockReduce<float, 1024>;
  __shared__ typename BlockReduce::TempStorage reduceStore;
Aidyn-A's avatar
Aidyn-A committed
105
  variance = BlockReduce(reduceStore).Reduce(variance, CubAddOp{}, blockDim.x);
106

107
108
109
110
111
112
113
  if (threadIdx.x == 0) {
    s_variance = rsqrtf(variance / hidden_size + epsilon);
  }
  __syncthreads();

  for (int idx = threadIdx.x; idx < vec_hidden_size; idx += blockDim.x) {
    int id = blockIdx.x * vec_hidden_size + idx;
114
    int64_t strided_id = blockIdx.x * vec_input_stride + idx;
115
116
117
    _f16Vec<scalar_t, width> temp = residual_v[id];
    temp *= s_variance;
    temp *= weight_v[idx];
118
    input_v[strided_id] = temp;
119
120
121
122
123
124
  }
}

/* Generic fused_add_rms_norm_kernel
   The width field is not used here but necessary for other specializations.
 */
125
126
127
template <typename scalar_t, int width>
__global__ std::enable_if_t<(width == 0) || !_typeConvert<scalar_t>::exists>
fused_add_rms_norm_kernel(
128
129
    scalar_t* __restrict__ input,  // [..., hidden_size]
    const int64_t input_stride,
130
131
132
    scalar_t* __restrict__ residual,      // [..., hidden_size]
    const scalar_t* __restrict__ weight,  // [hidden_size]
    const float epsilon, const int num_tokens, const int hidden_size) {
133
134
135
136
  __shared__ float s_variance;
  float variance = 0.0f;

  for (int idx = threadIdx.x; idx < hidden_size; idx += blockDim.x) {
137
    scalar_t z = input[blockIdx.x * input_stride + idx];
138
    z += residual[blockIdx.x * hidden_size + idx];
139
    float x = (float)z;
140
    variance += x * x;
141
    residual[blockIdx.x * hidden_size + idx] = z;
142
  }
143
144
145

  using BlockReduce = cub::BlockReduce<float, 1024>;
  __shared__ typename BlockReduce::TempStorage reduceStore;
Aidyn-A's avatar
Aidyn-A committed
146
  variance = BlockReduce(reduceStore).Reduce(variance, CubAddOp{}, blockDim.x);
147

148
149
150
151
152
153
  if (threadIdx.x == 0) {
    s_variance = rsqrtf(variance / hidden_size + epsilon);
  }
  __syncthreads();

  for (int idx = threadIdx.x; idx < hidden_size; idx += blockDim.x) {
154
    float x = (float)residual[blockIdx.x * hidden_size + idx];
155
    input[blockIdx.x * input_stride + idx] =
156
        ((scalar_t)(x * s_variance)) * weight[idx];
157
158
159
  }
}

160
}  // namespace vllm
161

162
163
164
void rms_norm(torch::Tensor& out,     // [..., hidden_size]
              torch::Tensor& input,   // [..., hidden_size]
              torch::Tensor& weight,  // [hidden_size]
165
              double epsilon) {
166
  TORCH_CHECK(out.is_contiguous());
167
  TORCH_CHECK(input.stride(-1) == 1);
168
169
  TORCH_CHECK(weight.is_contiguous());

170
  int hidden_size = input.size(-1);
171
172
173
174
175
176
177
178

  // We cannot just use `input.stride(-2)` if the tensor is not row-major.
  // Instead, we use a 2d view to get the second-innermost stride.
  // That way the dimensions (except the last one) can be arbitrarily permuted.
  torch::Tensor input_view = input.view({-1, hidden_size});

  int num_tokens = input_view.numel() / hidden_size;
  int64_t input_stride = input_view.stride(-2);
179

180
181
  // For large num_tokens, use smaller blocks to increase SM concurrency.
  const int max_block_size = (num_tokens < 256) ? 1024 : 256;
182
  dim3 grid(num_tokens);
183
  const at::cuda::OptionalCUDAGuard device_guard(device_of(input_view));
184
  const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
185
186
  VLLM_DISPATCH_FLOATING_TYPES(
      input_view.scalar_type(), "rms_norm_kernel", [&] {
187
188
189
190
191
192
193
194
195
196
197
        const int calculated_vec_size =
            std::gcd(16 / sizeof(scalar_t), hidden_size);
        const int block_size =
            std::min(hidden_size / calculated_vec_size, max_block_size);
        dim3 block(block_size);
        VLLM_DISPATCH_VEC_SIZE(calculated_vec_size, [&] {
          vllm::rms_norm_kernel<scalar_t, vec_size><<<grid, block, 0, stream>>>(
              out.data_ptr<scalar_t>(), input_view.data_ptr<scalar_t>(),
              input_stride, weight.data_ptr<scalar_t>(), epsilon, num_tokens,
              hidden_size);
        });
198
      });
199
}
200

201
202
203
204
205
206
207
208
#define LAUNCH_FUSED_ADD_RMS_NORM(width)                                    \
  VLLM_DISPATCH_FLOATING_TYPES(                                             \
      input.scalar_type(), "fused_add_rms_norm_kernel", [&] {               \
        vllm::fused_add_rms_norm_kernel<scalar_t, width>                    \
            <<<grid, block, 0, stream>>>(                                   \
                input.data_ptr<scalar_t>(), input_stride,                   \
                residual.data_ptr<scalar_t>(), weight.data_ptr<scalar_t>(), \
                epsilon, num_tokens, hidden_size);                          \
209
210
211
212
213
      });

void fused_add_rms_norm(torch::Tensor& input,     // [..., hidden_size]
                        torch::Tensor& residual,  // [..., hidden_size]
                        torch::Tensor& weight,    // [hidden_size]
214
                        double epsilon) {
215
216
  TORCH_CHECK(weight.scalar_type() == input.scalar_type());
  TORCH_CHECK(input.scalar_type() == residual.scalar_type());
217
218
  TORCH_CHECK(residual.is_contiguous());
  TORCH_CHECK(weight.is_contiguous());
219
  int hidden_size = input.size(-1);
220
  int64_t input_stride = input.stride(-2);
221
222
223
  int num_tokens = input.numel() / hidden_size;

  dim3 grid(num_tokens);
224
225
226
227
228
229
  /* This kernel is memory-latency bound in many scenarios.
     When num_tokens is large, a smaller block size allows
     for increased block occupancy on CUs and better latency
     hiding on global mem ops. */
  const int max_block_size = (num_tokens < 256) ? 1024 : 256;
  dim3 block(std::min(hidden_size, max_block_size));
230
  const at::cuda::OptionalCUDAGuard device_guard(device_of(input));
231
  const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
232
233
234
235
236
237
238
239
240
241
  /*If the tensor types are FP16/BF16, try to use the optimized kernel
    with packed + vectorized ops.
    Max optimization is achieved with a width-8 vector of FP16/BF16s
    since we can load at most 128 bits at once in a global memory op.
    However, this requires each tensor's data to be aligned to 16
    bytes.
   */
  auto inp_ptr = reinterpret_cast<std::uintptr_t>(input.data_ptr());
  auto res_ptr = reinterpret_cast<std::uintptr_t>(residual.data_ptr());
  auto wt_ptr = reinterpret_cast<std::uintptr_t>(weight.data_ptr());
242
243
244
245
246
247
248
249
250
  constexpr int vector_width = 8;
  constexpr int req_alignment_bytes =
      vector_width * 2;  // vector_width * sizeof(bfloat16 or float16) (float32
                         // falls back to non-vectorized version anyway)
  bool ptrs_are_aligned = inp_ptr % req_alignment_bytes == 0 &&
                          res_ptr % req_alignment_bytes == 0 &&
                          wt_ptr % req_alignment_bytes == 0;
  bool offsets_are_multiple_of_vector_width =
      hidden_size % vector_width == 0 && input_stride % vector_width == 0;
251
  bool batch_invariant_launch = vllm::vllm_is_batch_invariant();
252
253
  if (ptrs_are_aligned && offsets_are_multiple_of_vector_width &&
      !batch_invariant_launch) {
254
255
256
257
    LAUNCH_FUSED_ADD_RMS_NORM(8);
  } else {
    LAUNCH_FUSED_ADD_RMS_NORM(0);
  }
258
}