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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* coding=utf-8
|
| 2 |
+
* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
| 3 |
+
*
|
| 4 |
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
* you may not use this file except in compliance with the License.
|
| 6 |
+
* You may obtain a copy of the License at
|
| 7 |
+
*
|
| 8 |
+
* http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
*
|
| 10 |
+
* Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
* See the License for the specific language governing permissions and
|
| 14 |
+
* limitations under the License.
|
| 15 |
+
*/
|
| 16 |
+
|
| 17 |
+
#pragma once
|
| 18 |
+
|
| 19 |
+
#include <assert.h>
|
| 20 |
+
#include <cuda_fp16.h>
|
| 21 |
+
#include <cfloat>
|
| 22 |
+
#include <limits>
|
| 23 |
+
#include <stdint.h>
|
| 24 |
+
#include <c10/macros/Macros.h>
|
| 25 |
+
|
| 26 |
+
namespace {
|
| 27 |
+
|
| 28 |
+
template <typename Datatype, int ELEMENTS_PER_LDG>
|
| 29 |
+
__device__ __inline__ void copy_vector(Datatype *dst, const Datatype *src);
|
| 30 |
+
|
| 31 |
+
template <>
|
| 32 |
+
__device__ __inline__ void copy_vector<c10::BFloat16, 1>(c10::BFloat16 *dst, const c10::BFloat16 *src) { *dst = *src; }
|
| 33 |
+
|
| 34 |
+
template <>
|
| 35 |
+
__device__ __inline__ void copy_vector<c10::BFloat16, 4>(c10::BFloat16 *dst, const c10::BFloat16 *src) { *((float2*) dst) = *((float2*) src); }
|
| 36 |
+
|
| 37 |
+
template <>
|
| 38 |
+
__device__ __inline__ void copy_vector<c10::Half, 1>(c10::Half *dst, const c10::Half *src) { *dst = *src; }
|
| 39 |
+
|
| 40 |
+
template <>
|
| 41 |
+
__device__ __inline__ void copy_vector<c10::Half, 4>(c10::Half *dst, const c10::Half *src) { *((float2*) dst) = *((float2*) src); }
|
| 42 |
+
|
| 43 |
+
template <>
|
| 44 |
+
__device__ __inline__ void copy_vector<uint8_t, 1>(uint8_t *dst, const uint8_t *src) { *dst = *src; }
|
| 45 |
+
|
| 46 |
+
template <>
|
| 47 |
+
__device__ __inline__ void copy_vector<uint8_t, 4>(uint8_t *dst, const uint8_t *src) {*((half2*) dst) = *((half2*) src); }
|
| 48 |
+
|
| 49 |
+
template <typename Datatype, int ELEMENTS_PER_LDG>
|
| 50 |
+
__device__ __inline__ void copy_zero_vector(Datatype *dst);
|
| 51 |
+
|
| 52 |
+
template <>
|
| 53 |
+
__device__ __inline__ void copy_zero_vector<c10::BFloat16, 1>(c10::BFloat16 *dst) { *dst = 0.0; }
|
| 54 |
+
|
| 55 |
+
template <>
|
| 56 |
+
__device__ __inline__ void copy_zero_vector<c10::BFloat16, 4>(c10::BFloat16 *dst) { *((float2*) dst) = make_float2(0.0f, 0.0f); }
|
| 57 |
+
|
| 58 |
+
template <>
|
| 59 |
+
__device__ __inline__ void copy_zero_vector<c10::Half, 1>(c10::Half *dst) { *dst = 0.0; }
|
| 60 |
+
|
| 61 |
+
template <>
|
| 62 |
+
__device__ __inline__ void copy_zero_vector<c10::Half, 4>(c10::Half *dst) { *((float2*) dst) = make_float2(0.0f, 0.0f); }
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
int log2_ceil(int value) {
|
| 66 |
+
int log2_value = 0;
|
| 67 |
+
while ((1 << log2_value) < value) ++log2_value;
|
| 68 |
+
return log2_value;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
template<typename T>
|
| 72 |
+
struct Add {
|
| 73 |
+
__device__ __forceinline__ T operator()(T a, T b) const {
|
| 74 |
+
return a + b;
|
| 75 |
+
}
|
| 76 |
+
};
|
| 77 |
+
|
| 78 |
+
template<typename T>
|
| 79 |
+
struct Max {
|
| 80 |
+
__device__ __forceinline__ T operator()(T a, T b) const {
|
| 81 |
+
return a < b ? b : a;
|
| 82 |
+
}
|
| 83 |
+
};
|
| 84 |
+
|
| 85 |
+
template <typename T>
|
| 86 |
+
__device__ __forceinline__ T WARP_SHFL_XOR_NATIVE(T value, int laneMask, int width = warpSize, unsigned int mask = 0xffffffff)
|
| 87 |
+
{
|
| 88 |
+
#if CUDA_VERSION >= 9000
|
| 89 |
+
return __shfl_xor_sync(mask, value, laneMask, width);
|
| 90 |
+
#else
|
| 91 |
+
return __shfl_xor(value, laneMask, width);
|
| 92 |
+
#endif
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
template <typename acc_t, int WARP_BATCH, int WARP_SIZE, template<typename> class ReduceOp>
|
| 96 |
+
__device__ __forceinline__ void warp_reduce(acc_t* sum) {
|
| 97 |
+
ReduceOp<acc_t> r;
|
| 98 |
+
#pragma unroll
|
| 99 |
+
for (int offset = WARP_SIZE / 2; offset > 0; offset /= 2) {
|
| 100 |
+
#pragma unroll
|
| 101 |
+
for (int i = 0; i < WARP_BATCH; ++i) {
|
| 102 |
+
acc_t b = WARP_SHFL_XOR_NATIVE(sum[i], offset, WARP_SIZE);
|
| 103 |
+
sum[i] = r(sum[i], b);
|
| 104 |
+
}
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
/*
|
| 109 |
+
* Extended softmax (from native aten pytorch) with following additional features
|
| 110 |
+
* 1) input scaling
|
| 111 |
+
* 2) Implicit time (diagonal masking)
|
| 112 |
+
*/
|
| 113 |
+
template <typename input_t, typename output_t, typename acc_t, int log2_elements>
|
| 114 |
+
__global__ void scaled_upper_triang_masked_softmax_warp_forward(
|
| 115 |
+
output_t *dst,
|
| 116 |
+
const input_t *src,
|
| 117 |
+
const acc_t scale,
|
| 118 |
+
int micro_batch_size,
|
| 119 |
+
int stride,
|
| 120 |
+
int element_count)
|
| 121 |
+
{
|
| 122 |
+
// WARP_SIZE and WARP_BATCH must match the return values batches_per_warp and
|
| 123 |
+
// warp_size of method warp_softmax_forward_kernel.
|
| 124 |
+
constexpr int next_power_of_two = 1 << log2_elements;
|
| 125 |
+
constexpr int WARP_SIZE = (next_power_of_two < C10_WARP_SIZE) ? next_power_of_two : C10_WARP_SIZE;
|
| 126 |
+
constexpr int WARP_ITERATIONS = next_power_of_two / WARP_SIZE;
|
| 127 |
+
constexpr int WARP_BATCH = (next_power_of_two <= 128) ? 2 : 1;
|
| 128 |
+
constexpr int ELEMENTS_PER_LDG_STG = (WARP_ITERATIONS < 4) ? 1 : 4;
|
| 129 |
+
|
| 130 |
+
long int first_batch = (blockDim.y * blockIdx.y + threadIdx.y) * gridDim.x * WARP_BATCH + blockIdx.x;
|
| 131 |
+
int local_seq = blockIdx.x + 1;
|
| 132 |
+
int warp_iteration_limit = (local_seq + ELEMENTS_PER_LDG_STG * WARP_SIZE - 1)/ WARP_SIZE;
|
| 133 |
+
|
| 134 |
+
// micro_batch_size might not be a multiple of WARP_BATCH. Check how
|
| 135 |
+
// many batches have to computed within this WARP.
|
| 136 |
+
int local_batches = micro_batch_size - first_batch;
|
| 137 |
+
if (local_batches > WARP_BATCH)
|
| 138 |
+
local_batches = WARP_BATCH;
|
| 139 |
+
|
| 140 |
+
// there might be multiple batches per warp. compute the index within the batch
|
| 141 |
+
int local_idx = threadIdx.x;
|
| 142 |
+
|
| 143 |
+
long int thread_offset = first_batch * stride + ELEMENTS_PER_LDG_STG * local_idx;
|
| 144 |
+
src += thread_offset;
|
| 145 |
+
dst += thread_offset;
|
| 146 |
+
|
| 147 |
+
// load data from global memory
|
| 148 |
+
acc_t elements[WARP_BATCH][WARP_ITERATIONS];
|
| 149 |
+
input_t temp_data[ELEMENTS_PER_LDG_STG];
|
| 150 |
+
#pragma unroll
|
| 151 |
+
for (int i = 0; i < WARP_BATCH; ++i) {
|
| 152 |
+
int batch_element_count = (i >= local_batches) ? 0 : local_seq;
|
| 153 |
+
|
| 154 |
+
#pragma unroll
|
| 155 |
+
for (int it = 0; it < WARP_ITERATIONS; it+=ELEMENTS_PER_LDG_STG) {
|
| 156 |
+
int element_index = ELEMENTS_PER_LDG_STG * local_idx + it * WARP_SIZE;
|
| 157 |
+
|
| 158 |
+
if (element_index < batch_element_count) {
|
| 159 |
+
copy_vector<input_t, ELEMENTS_PER_LDG_STG>(temp_data, src + i*element_count*stride + it*WARP_SIZE);
|
| 160 |
+
|
| 161 |
+
#pragma unroll
|
| 162 |
+
for (int element = 0; element < ELEMENTS_PER_LDG_STG; ++element) {
|
| 163 |
+
if ((element_index + element) < batch_element_count) {
|
| 164 |
+
elements[i][it+element] = (acc_t)temp_data[element] * scale;
|
| 165 |
+
} else {
|
| 166 |
+
elements[i][it + element] = -std::numeric_limits<acc_t>::infinity();
|
| 167 |
+
}
|
| 168 |
+
}
|
| 169 |
+
} else {
|
| 170 |
+
#pragma unroll
|
| 171 |
+
for (int element = 0; element < ELEMENTS_PER_LDG_STG; ++element) {
|
| 172 |
+
elements[i][it + element] = -std::numeric_limits<acc_t>::infinity();
|
| 173 |
+
}
|
| 174 |
+
}
|
| 175 |
+
}
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
// compute max_value
|
| 179 |
+
acc_t max_value[WARP_BATCH];
|
| 180 |
+
#pragma unroll
|
| 181 |
+
for (int i = 0; i < WARP_BATCH; ++i) {
|
| 182 |
+
max_value[i] = elements[i][0];
|
| 183 |
+
#pragma unroll
|
| 184 |
+
for (int it = 1; it < WARP_ITERATIONS; ++it) {
|
| 185 |
+
max_value[i] = (max_value[i] > elements[i][it]) ? max_value[i] : elements[i][it];
|
| 186 |
+
}
|
| 187 |
+
}
|
| 188 |
+
warp_reduce<acc_t, WARP_BATCH, WARP_SIZE, Max>(max_value);
|
| 189 |
+
|
| 190 |
+
acc_t sum[WARP_BATCH] { 0.0f };
|
| 191 |
+
#pragma unroll
|
| 192 |
+
for (int i = 0; i < WARP_BATCH; ++i) {
|
| 193 |
+
#pragma unroll
|
| 194 |
+
for (int it = 0; it < WARP_ITERATIONS; ++it) {
|
| 195 |
+
if (it < warp_iteration_limit) {
|
| 196 |
+
elements[i][it] = std::exp((elements[i][it] - max_value[i]));
|
| 197 |
+
sum[i] += elements[i][it];
|
| 198 |
+
}
|
| 199 |
+
}
|
| 200 |
+
}
|
| 201 |
+
warp_reduce<acc_t, WARP_BATCH, WARP_SIZE, Add>(sum);
|
| 202 |
+
|
| 203 |
+
// store result
|
| 204 |
+
output_t out[ELEMENTS_PER_LDG_STG];
|
| 205 |
+
#pragma unroll
|
| 206 |
+
for (int i = 0; i < WARP_BATCH; ++i) {
|
| 207 |
+
if (i >= local_batches)
|
| 208 |
+
break;
|
| 209 |
+
#pragma unroll
|
| 210 |
+
for (int it = 0; it < WARP_ITERATIONS; it+=ELEMENTS_PER_LDG_STG) {
|
| 211 |
+
int element_index = ELEMENTS_PER_LDG_STG * local_idx + it * WARP_SIZE;
|
| 212 |
+
|
| 213 |
+
if (element_index < local_seq) {
|
| 214 |
+
|
| 215 |
+
#pragma unroll
|
| 216 |
+
for (int element = 0; element < ELEMENTS_PER_LDG_STG; ++element) {
|
| 217 |
+
if (element_index + element < local_seq) {
|
| 218 |
+
out[element] = elements[i][it + element] / sum[i];
|
| 219 |
+
} else {
|
| 220 |
+
out[element] = 0;
|
| 221 |
+
}
|
| 222 |
+
}
|
| 223 |
+
copy_vector<output_t, ELEMENTS_PER_LDG_STG>(dst + i * element_count * stride + it * WARP_SIZE, out);
|
| 224 |
+
} else if (element_index < element_count) {
|
| 225 |
+
copy_zero_vector<output_t, ELEMENTS_PER_LDG_STG>(dst + i * element_count * stride + it * WARP_SIZE);
|
| 226 |
+
} else {
|
| 227 |
+
break;
|
| 228 |
+
}
|
| 229 |
+
}
|
| 230 |
+
}
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
template <typename input_t, typename output_t, typename acc_t, int log2_elements>
|
| 234 |
+
__global__ void scaled_upper_triang_masked_softmax_warp_backward(
|
| 235 |
+
output_t *gradInput,
|
| 236 |
+
input_t *grad,
|
| 237 |
+
const input_t *output,
|
| 238 |
+
acc_t scale,
|
| 239 |
+
int micro_batch_size,
|
| 240 |
+
int stride,
|
| 241 |
+
int element_count)
|
| 242 |
+
{
|
| 243 |
+
// WARP_SIZE and WARP_BATCH must match the return values batches_per_warp and
|
| 244 |
+
// warp_size of method warp_softmax_backward_kernel.
|
| 245 |
+
constexpr int next_power_of_two = 1 << log2_elements;
|
| 246 |
+
constexpr int WARP_SIZE = (next_power_of_two < C10_WARP_SIZE) ? next_power_of_two : C10_WARP_SIZE;
|
| 247 |
+
constexpr int WARP_ITERATIONS = next_power_of_two / WARP_SIZE;
|
| 248 |
+
constexpr int WARP_BATCH = (next_power_of_two <= 128) ? 2 : 1;
|
| 249 |
+
constexpr int ELEMENTS_PER_LDG_STG = (WARP_ITERATIONS < 4) ? 1 : 4;
|
| 250 |
+
|
| 251 |
+
long int first_batch = (blockDim.y * blockIdx.y + threadIdx.y) * gridDim.x * WARP_BATCH + blockIdx.x;
|
| 252 |
+
int local_seq = blockIdx.x + 1;
|
| 253 |
+
|
| 254 |
+
// micro_batch_size might not be a multiple of WARP_BATCH. Check how
|
| 255 |
+
// many batches have to computed within this WARP.
|
| 256 |
+
int local_batches = micro_batch_size - first_batch;
|
| 257 |
+
if (local_batches > WARP_BATCH)
|
| 258 |
+
local_batches = WARP_BATCH;
|
| 259 |
+
|
| 260 |
+
// there might be multiple batches per warp. compute the index within the batch
|
| 261 |
+
int local_idx = threadIdx.x;
|
| 262 |
+
|
| 263 |
+
// the first element to process by the current thread
|
| 264 |
+
long int thread_offset = first_batch * stride + ELEMENTS_PER_LDG_STG * local_idx;
|
| 265 |
+
grad += thread_offset;
|
| 266 |
+
output += thread_offset;
|
| 267 |
+
gradInput += thread_offset;
|
| 268 |
+
|
| 269 |
+
// load data from global memory
|
| 270 |
+
acc_t grad_reg[WARP_BATCH][WARP_ITERATIONS] { 0.0f };
|
| 271 |
+
acc_t output_reg[WARP_BATCH][WARP_ITERATIONS] { 0.0f };
|
| 272 |
+
input_t temp_grad[ELEMENTS_PER_LDG_STG];
|
| 273 |
+
input_t temp_output[ELEMENTS_PER_LDG_STG];
|
| 274 |
+
#pragma unroll
|
| 275 |
+
for (int i = 0; i < WARP_BATCH; ++i) {
|
| 276 |
+
int batch_element_count = (i >= local_batches) ? 0 : local_seq;
|
| 277 |
+
|
| 278 |
+
#pragma unroll
|
| 279 |
+
for (int it = 0; it < WARP_ITERATIONS; it+=ELEMENTS_PER_LDG_STG) {
|
| 280 |
+
int element_index = ELEMENTS_PER_LDG_STG * local_idx + it * WARP_SIZE;
|
| 281 |
+
if (element_index < batch_element_count) {
|
| 282 |
+
copy_vector<input_t, ELEMENTS_PER_LDG_STG>(temp_grad, grad + i * element_count * stride + it * WARP_SIZE);
|
| 283 |
+
copy_vector<input_t, ELEMENTS_PER_LDG_STG>(temp_output, output + i * element_count * stride + it * WARP_SIZE);
|
| 284 |
+
|
| 285 |
+
#pragma unroll
|
| 286 |
+
for (int element = 0; element < ELEMENTS_PER_LDG_STG; ++element) {
|
| 287 |
+
if (element_index + element < batch_element_count) {
|
| 288 |
+
output_reg[i][it + element] = (acc_t)temp_output[element];
|
| 289 |
+
}
|
| 290 |
+
}
|
| 291 |
+
#pragma unroll
|
| 292 |
+
for (int element = 0; element < ELEMENTS_PER_LDG_STG; ++element) {
|
| 293 |
+
if (element_index + element < batch_element_count) {
|
| 294 |
+
grad_reg[i][it + element] = (acc_t)temp_grad[element] * output_reg[i][it + element];
|
| 295 |
+
}
|
| 296 |
+
}
|
| 297 |
+
}
|
| 298 |
+
}
|
| 299 |
+
}
|
| 300 |
+
|
| 301 |
+
acc_t sum[WARP_BATCH];
|
| 302 |
+
#pragma unroll
|
| 303 |
+
for (int i = 0; i < WARP_BATCH; ++i) {
|
| 304 |
+
sum[i] = grad_reg[i][0];
|
| 305 |
+
#pragma unroll
|
| 306 |
+
for (int it = 1; it < WARP_ITERATIONS; ++it) {
|
| 307 |
+
sum[i] += grad_reg[i][it];
|
| 308 |
+
}
|
| 309 |
+
}
|
| 310 |
+
warp_reduce<acc_t, WARP_BATCH, WARP_SIZE, Add>(sum);
|
| 311 |
+
|
| 312 |
+
// store result
|
| 313 |
+
#pragma unroll
|
| 314 |
+
for (int i = 0; i < WARP_BATCH; ++i) {
|
| 315 |
+
if (i >= local_batches)
|
| 316 |
+
break;
|
| 317 |
+
#pragma unroll
|
| 318 |
+
for (int it = 0; it < WARP_ITERATIONS; it+=ELEMENTS_PER_LDG_STG) {
|
| 319 |
+
int element_index = ELEMENTS_PER_LDG_STG * local_idx + it * WARP_SIZE;
|
| 320 |
+
if (element_index < element_count) {
|
| 321 |
+
// compute gradients
|
| 322 |
+
output_t out[ELEMENTS_PER_LDG_STG];
|
| 323 |
+
#pragma unroll
|
| 324 |
+
for (int element = 0; element < ELEMENTS_PER_LDG_STG; ++element) {
|
| 325 |
+
out[element] = (output_t)(scale * (grad_reg[i][it + element] - output_reg[i][it + element] * sum[i]));
|
| 326 |
+
}
|
| 327 |
+
copy_vector<output_t, ELEMENTS_PER_LDG_STG>(gradInput + i * element_count * stride + it * WARP_SIZE, out);
|
| 328 |
+
}
|
| 329 |
+
}
|
| 330 |
+
}
|
| 331 |
+
}
|
| 332 |
+
|
| 333 |
+
} // end of anonymous namespace
|
| 334 |
+
|
| 335 |
+
template<typename input_t, typename output_t, typename acc_t>
|
| 336 |
+
void dispatch_scaled_upper_triang_masked_softmax_forward(
|
| 337 |
+
output_t *dst,
|
| 338 |
+
const input_t *src,
|
| 339 |
+
const input_t scale,
|
| 340 |
+
int softmax_elements,
|
| 341 |
+
int softmax_elements_stride,
|
| 342 |
+
int attn_batches)
|
| 343 |
+
{
|
| 344 |
+
TORCH_INTERNAL_ASSERT(softmax_elements >= 0 && softmax_elements <= 16384 );
|
| 345 |
+
if (softmax_elements == 0) {
|
| 346 |
+
return;
|
| 347 |
+
} else {
|
| 348 |
+
int log2_elements = log2_ceil(softmax_elements);
|
| 349 |
+
const int next_power_of_two = 1 << log2_elements;
|
| 350 |
+
int seq_len = softmax_elements;
|
| 351 |
+
int batch_count = attn_batches * seq_len;
|
| 352 |
+
|
| 353 |
+
// This value must match the WARP_SIZE constexpr value computed inside softmax_warp_forward.
|
| 354 |
+
int warp_size = (next_power_of_two < C10_WARP_SIZE) ? next_power_of_two : C10_WARP_SIZE;
|
| 355 |
+
|
| 356 |
+
// This value must match the WARP_BATCH constexpr value computed inside softmax_warp_forward.
|
| 357 |
+
int batches_per_warp = (next_power_of_two <= 128) ? 2 : 1;
|
| 358 |
+
|
| 359 |
+
// use 128 threads per block to maximize gpu utilization
|
| 360 |
+
constexpr int threads_per_block = 128;
|
| 361 |
+
|
| 362 |
+
int warps_per_block = (threads_per_block / warp_size);
|
| 363 |
+
int batches_per_block = warps_per_block * batches_per_warp;
|
| 364 |
+
TORCH_INTERNAL_ASSERT(attn_batches % batches_per_block == 0);
|
| 365 |
+
|
| 366 |
+
int blocks_per_seq = attn_batches / batches_per_block;
|
| 367 |
+
dim3 blocks(seq_len, blocks_per_seq, 1);
|
| 368 |
+
dim3 threads(warp_size, warps_per_block, 1);
|
| 369 |
+
// Launch code would be more elegant if C++ supported FOR CONSTEXPR
|
| 370 |
+
switch (log2_elements) {
|
| 371 |
+
case 0: // 1
|
| 372 |
+
scaled_upper_triang_masked_softmax_warp_forward<input_t, output_t, acc_t, 0>
|
| 373 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(dst, src, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 374 |
+
break;
|
| 375 |
+
case 1: // 2
|
| 376 |
+
scaled_upper_triang_masked_softmax_warp_forward<input_t, output_t, acc_t, 1>
|
| 377 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(dst, src, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 378 |
+
break;
|
| 379 |
+
case 2: // 4
|
| 380 |
+
scaled_upper_triang_masked_softmax_warp_forward<input_t, output_t, acc_t, 2>
|
| 381 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(dst, src, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 382 |
+
break;
|
| 383 |
+
case 3: // 8
|
| 384 |
+
scaled_upper_triang_masked_softmax_warp_forward<input_t, output_t, acc_t, 3>
|
| 385 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(dst, src, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 386 |
+
break;
|
| 387 |
+
case 4: // 16
|
| 388 |
+
scaled_upper_triang_masked_softmax_warp_forward<input_t, output_t, acc_t, 4>
|
| 389 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(dst, src, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 390 |
+
break;
|
| 391 |
+
case 5: // 32
|
| 392 |
+
scaled_upper_triang_masked_softmax_warp_forward<input_t, output_t, acc_t, 5>
|
| 393 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(dst, src, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 394 |
+
break;
|
| 395 |
+
case 6: // 64
|
| 396 |
+
scaled_upper_triang_masked_softmax_warp_forward<input_t, output_t, acc_t, 6>
|
| 397 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(dst, src, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 398 |
+
break;
|
| 399 |
+
case 7: // 128
|
| 400 |
+
scaled_upper_triang_masked_softmax_warp_forward<input_t, output_t, acc_t, 7>
|
| 401 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(dst, src, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 402 |
+
break;
|
| 403 |
+
case 8: // 256
|
| 404 |
+
scaled_upper_triang_masked_softmax_warp_forward<input_t, output_t, acc_t, 8>
|
| 405 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(dst, src, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 406 |
+
break;
|
| 407 |
+
case 9: // 512
|
| 408 |
+
scaled_upper_triang_masked_softmax_warp_forward<input_t, output_t, acc_t, 9>
|
| 409 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(dst, src, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 410 |
+
break;
|
| 411 |
+
case 10: // 1024
|
| 412 |
+
scaled_upper_triang_masked_softmax_warp_forward<input_t, output_t, acc_t, 10>
|
| 413 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(dst, src, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 414 |
+
break;
|
| 415 |
+
case 11: // 2048
|
| 416 |
+
scaled_upper_triang_masked_softmax_warp_forward<input_t, output_t, acc_t, 11>
|
| 417 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(dst, src, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 418 |
+
break;
|
| 419 |
+
case 12: // 4096
|
| 420 |
+
scaled_upper_triang_masked_softmax_warp_forward<input_t, output_t, acc_t, 12>
|
| 421 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(dst, src, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 422 |
+
break;
|
| 423 |
+
case 13: // 8192
|
| 424 |
+
scaled_upper_triang_masked_softmax_warp_forward<input_t, output_t, acc_t, 13>
|
| 425 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(dst, src, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 426 |
+
break;
|
| 427 |
+
case 14: // 16384
|
| 428 |
+
scaled_upper_triang_masked_softmax_warp_forward<input_t, output_t, acc_t, 14>
|
| 429 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(dst, src, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 430 |
+
break;
|
| 431 |
+
default:
|
| 432 |
+
break;
|
| 433 |
+
}
|
| 434 |
+
}
|
| 435 |
+
}
|
| 436 |
+
|
| 437 |
+
template<typename input_t, typename output_t, typename acc_t>
|
| 438 |
+
void dispatch_scaled_upper_triang_masked_softmax_backward(
|
| 439 |
+
output_t *grad_input,
|
| 440 |
+
input_t *grad,
|
| 441 |
+
const input_t *output,
|
| 442 |
+
const acc_t scale,
|
| 443 |
+
int softmax_elements,
|
| 444 |
+
int softmax_elements_stride,
|
| 445 |
+
int attn_batches)
|
| 446 |
+
{
|
| 447 |
+
TORCH_INTERNAL_ASSERT( softmax_elements >= 0 && softmax_elements <= 16384 );
|
| 448 |
+
if (softmax_elements == 0) {
|
| 449 |
+
return;
|
| 450 |
+
} else {
|
| 451 |
+
int log2_elements = log2_ceil(softmax_elements);
|
| 452 |
+
const int next_power_of_two = 1 << log2_elements;
|
| 453 |
+
int seq_len = softmax_elements;
|
| 454 |
+
int batch_count = attn_batches * seq_len;
|
| 455 |
+
|
| 456 |
+
// This value must match the WARP_SIZE constexpr value computed inside softmax_warp_backward.
|
| 457 |
+
int warp_size = (next_power_of_two < C10_WARP_SIZE) ? next_power_of_two : C10_WARP_SIZE;
|
| 458 |
+
|
| 459 |
+
// This value must match the WARP_BATCH constexpr value computed inside softmax_warp_backward.
|
| 460 |
+
int batches_per_warp = (next_power_of_two <= 128) ? 2 : 1;
|
| 461 |
+
|
| 462 |
+
// use 128 threads per block to maximize gpu utilization
|
| 463 |
+
constexpr int threads_per_block = 128;
|
| 464 |
+
|
| 465 |
+
int warps_per_block = (threads_per_block / warp_size);
|
| 466 |
+
int batches_per_block = warps_per_block * batches_per_warp;
|
| 467 |
+
TORCH_INTERNAL_ASSERT(attn_batches % batches_per_block == 0);
|
| 468 |
+
|
| 469 |
+
int blocks_per_seq = attn_batches / batches_per_block;
|
| 470 |
+
dim3 blocks(seq_len, blocks_per_seq, 1);
|
| 471 |
+
dim3 threads(warp_size, warps_per_block, 1);
|
| 472 |
+
// Launch code would be more elegant if C++ supported FOR CONSTEXPR
|
| 473 |
+
switch (log2_elements) {
|
| 474 |
+
case 0: // 1
|
| 475 |
+
scaled_upper_triang_masked_softmax_warp_backward<input_t, output_t, acc_t, 0>
|
| 476 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 477 |
+
break;
|
| 478 |
+
case 1: // 2
|
| 479 |
+
scaled_upper_triang_masked_softmax_warp_backward<input_t, output_t, acc_t, 1>
|
| 480 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 481 |
+
break;
|
| 482 |
+
case 2: // 4
|
| 483 |
+
scaled_upper_triang_masked_softmax_warp_backward<input_t, output_t, acc_t, 2>
|
| 484 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 485 |
+
break;
|
| 486 |
+
case 3: // 8
|
| 487 |
+
scaled_upper_triang_masked_softmax_warp_backward<input_t, output_t, acc_t, 3>
|
| 488 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 489 |
+
break;
|
| 490 |
+
case 4: // 16
|
| 491 |
+
scaled_upper_triang_masked_softmax_warp_backward<input_t, output_t, acc_t, 4>
|
| 492 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 493 |
+
break;
|
| 494 |
+
case 5: // 32
|
| 495 |
+
scaled_upper_triang_masked_softmax_warp_backward<input_t, output_t, acc_t, 5>
|
| 496 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 497 |
+
break;
|
| 498 |
+
case 6: // 64
|
| 499 |
+
scaled_upper_triang_masked_softmax_warp_backward<input_t, output_t, acc_t, 6>
|
| 500 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 501 |
+
break;
|
| 502 |
+
case 7: // 128
|
| 503 |
+
scaled_upper_triang_masked_softmax_warp_backward<input_t, output_t, acc_t, 7>
|
| 504 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 505 |
+
break;
|
| 506 |
+
case 8: // 256
|
| 507 |
+
scaled_upper_triang_masked_softmax_warp_backward<input_t, output_t, acc_t, 8>
|
| 508 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 509 |
+
break;
|
| 510 |
+
case 9: // 512
|
| 511 |
+
scaled_upper_triang_masked_softmax_warp_backward<input_t, output_t, acc_t, 9>
|
| 512 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 513 |
+
break;
|
| 514 |
+
case 10: // 1024
|
| 515 |
+
scaled_upper_triang_masked_softmax_warp_backward<input_t, output_t, acc_t, 10>
|
| 516 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 517 |
+
break;
|
| 518 |
+
case 11: // 2048
|
| 519 |
+
scaled_upper_triang_masked_softmax_warp_backward<input_t, output_t, acc_t, 11>
|
| 520 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 521 |
+
break;
|
| 522 |
+
case 12: // 4096
|
| 523 |
+
scaled_upper_triang_masked_softmax_warp_backward<input_t, output_t, acc_t, 12>
|
| 524 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 525 |
+
break;
|
| 526 |
+
case 13: // 8192
|
| 527 |
+
scaled_upper_triang_masked_softmax_warp_backward<input_t, output_t, acc_t, 13>
|
| 528 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 529 |
+
break;
|
| 530 |
+
case 14: // 16384
|
| 531 |
+
scaled_upper_triang_masked_softmax_warp_backward<input_t, output_t, acc_t, 14>
|
| 532 |
+
<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, scale, batch_count, softmax_elements_stride, softmax_elements);
|
| 533 |
+
break;
|
| 534 |
+
default:
|
| 535 |
+
break;
|
| 536 |
+
}
|
| 537 |
+
}
|
| 538 |
+
}
|