Spaces:
Sleeping
Sleeping
File size: 7,024 Bytes
0fdcb79 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 83 84 85 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
// Copyright 2021 AlQuraishi Laboratory
//
// 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.
// modified from fastfold/model/fastnn/kernel/cuda_native/csrc/softmax_cuda_kernel.cu
#include <math_constants.h>
#include <torch/extension.h>
#include <c10/cuda/CUDAGuard.h>
#include <iostream>
#include "ATen/ATen.h"
#include "ATen/cuda/CUDAContext.h"
#include "compat.h"
#define CHECK_CUDA(x) TORCH_CHECK(x.is_cuda(), #x " must be a CUDA tensor")
#define CHECK_CONTIGUOUS(x) TORCH_CHECK(x.is_contiguous(), #x " must be contiguous")
#define CHECK_INPUT(x) \
CHECK_CUDA(x); \
CHECK_CONTIGUOUS(x)
__inline__ __device__ float WarpAllReduceMax(float val) {
for (int mask = 1; mask < 32; mask *= 2) {
val = max(val, __shfl_xor_sync(0xffffffff, val, mask));
}
return val;
}
__inline__ __device__ float WarpAllReduceSum(float val) {
for (int mask = 1; mask < 32; mask *= 2) {
val += __shfl_xor_sync(0xffffffff, val, mask);
}
return val;
}
template<typename T>
__global__ void attn_softmax_inplace_(
T *input,
long long rows, int cols
) {
int threadidx_x = threadIdx.x / 32;
int threadidx_y = threadIdx.x % 32;
long long row_offset = (long long)(blockIdx.x * 4 + threadidx_x);
int cols_per_thread = (cols + 31) / 32;
int cols_this_thread = cols_per_thread;
int last_y = (cols / cols_per_thread);
if (threadidx_y == last_y) {
cols_this_thread = cols - cols_per_thread * last_y;
}
else if (threadidx_y > last_y) {
cols_this_thread = 0;
}
float buf[32];
int lane_id = threadidx_y;
if (row_offset < rows) {
T *row_input = input + row_offset * cols;
T *row_output = row_input;
#pragma unroll
for (int i = 0; i < cols_this_thread; i++) {
int idx = lane_id * cols_per_thread + i;
buf[i] = static_cast<float>(row_input[idx]);
}
float thread_max = -1 * CUDART_INF_F;
#pragma unroll
for (int i = 0; i < cols_this_thread; i++) {
thread_max = max(thread_max, buf[i]);
}
float warp_max = WarpAllReduceMax(thread_max);
float thread_sum = 0.f;
#pragma unroll
for (int i = 0; i < cols_this_thread; i++) {
buf[i] = __expf(buf[i] - warp_max);
thread_sum += buf[i];
}
float warp_sum = WarpAllReduceSum(thread_sum);
#pragma unroll
for (int i = 0; i < cols_this_thread; i++) {
row_output[lane_id * cols_per_thread + i] =
static_cast<T>(__fdividef(buf[i], warp_sum));
}
}
}
void attn_softmax_inplace_forward_(
at::Tensor input,
long long rows, int cols
) {
CHECK_INPUT(input);
const at::cuda::OptionalCUDAGuard device_guard(device_of(input));
int grid = (rows + 3) / 4;
dim3 block(128);
if (input.dtype() == torch::kFloat32) {
attn_softmax_inplace_<float><<<grid, block>>>(
(float *)input.data_ptr(),
rows, cols
);
}
else {
attn_softmax_inplace_<at::BFloat16><<<grid, block>>>(
(at::BFloat16 *)input.data_ptr(),
rows, cols
);
}
}
template<typename T>
__global__ void attn_softmax_inplace_grad_(
T *output,
T *d_ov,
T *values,
long long rows,
int cols_output,
int cols_values
) {
int threadidx_x = threadIdx.x / 32;
int threadidx_y = threadIdx.x % 32;
long long row_offset = (long long)(blockIdx.x * 4 + threadidx_x);
int cols_per_thread = (cols_output + 31) / 32;
int cols_this_thread = cols_per_thread;
int rows_values = cols_output;
// values are set to the beginning of the current
// rows_values x cols_values leaf matrix
long long value_row_offset = row_offset - row_offset % rows_values;
int last_y = (cols_output / cols_per_thread);
if (threadidx_y == last_y) {
cols_this_thread = cols_output - cols_per_thread * last_y;
}
else if (threadidx_y > last_y) {
cols_this_thread = 0;
}
float y_buf[32];
float dy_buf[32];
int lane_id = threadidx_y;
if (row_offset < rows) {
T *row_output = output + row_offset * cols_output;
T *row_d_ov = d_ov + row_offset * cols_values;
T *row_values = values + value_row_offset * cols_values;
float thread_max = -1 * CUDART_INF_F;
// Compute a chunk of the output gradient on the fly
int value_row_idx = 0;
int value_idx = 0;
#pragma unroll
for (int i = 0; i < cols_this_thread; i++) {
T sum = 0.;
#pragma unroll
for (int j = 0; j < cols_values; j++) {
value_row_idx = ((lane_id * cols_per_thread) + i);
value_idx = value_row_idx * cols_values + j;
sum += row_d_ov[j] * row_values[value_idx];
}
dy_buf[i] = static_cast<float>(sum);
}
#pragma unroll
for (int i = 0; i < cols_this_thread; i++) {
y_buf[i] = static_cast<float>(row_output[lane_id * cols_per_thread + i]);
}
float thread_sum = 0.;
#pragma unroll
for (int i = 0; i < cols_this_thread; i++) {
thread_sum += y_buf[i] * dy_buf[i];
}
float warp_sum = WarpAllReduceSum(thread_sum);
#pragma unroll
for (int i = 0; i < cols_this_thread; i++) {
row_output[lane_id * cols_per_thread + i] = static_cast<T>(
(dy_buf[i] - warp_sum) * y_buf[i]
);
}
}
}
void attn_softmax_inplace_backward_(
at::Tensor output,
at::Tensor d_ov,
at::Tensor values,
long long rows,
int cols_output,
int cols_values
) {
CHECK_INPUT(output);
CHECK_INPUT(d_ov);
CHECK_INPUT(values);
const at::cuda::OptionalCUDAGuard device_guard(device_of(output));
int grid = (rows + 3) / 4;
dim3 block(128);
if (output.dtype() == torch::kFloat32) {
attn_softmax_inplace_grad_<float><<<grid, block>>>(
(float *)output.data_ptr(),
(float *)d_ov.data_ptr(),
(float *)values.data_ptr(),
rows, cols_output, cols_values
);
} else {
attn_softmax_inplace_grad_<at::BFloat16><<<grid, block>>>(
(at::BFloat16 *)output.data_ptr(),
(at::BFloat16 *)d_ov.data_ptr(),
(at::BFloat16 *)values.data_ptr(),
rows, cols_output, cols_values
);
}
}
|