|
| 1 | +#include "causal_softmax_sdaa.h" |
| 2 | + |
| 3 | +__local__ halfv16 h_local; |
| 4 | +__local__ floatv16 f_local; |
| 5 | + |
| 6 | +infiniopStatus_t tecoCreateCausalSoftmaxDescriptor(TecoHandle_t handle, |
| 7 | + CausalSoftmaxTecoDescriptor_t *desc_ptr, |
| 8 | + infiniopTensorDescriptor_t y_desc){ |
| 9 | + if (y_desc->ndim < 2 || y_desc->shape[y_desc->ndim - 1] < y_desc->shape[y_desc->ndim - 2]) { |
| 10 | + return STATUS_BAD_TENSOR_SHAPE; |
| 11 | + } |
| 12 | + |
| 13 | + int ndim = y_desc->ndim; |
| 14 | + int *shape = (int *)malloc(ndim * sizeof(int)); |
| 15 | + int *stride = (int *)malloc(ndim * sizeof(int)); |
| 16 | + |
| 17 | + |
| 18 | + for (int i = 0; i < ndim; i++) { |
| 19 | + stride[i] = static_cast<int>(y_desc->strides[i]); |
| 20 | + shape[i] = static_cast<int>(y_desc->shape[i]); |
| 21 | + } |
| 22 | + |
| 23 | + *desc_ptr = new CausalSoftmaxTecoDescriptor{ |
| 24 | + handle->device, |
| 25 | + handle->device_id, |
| 26 | + y_desc->dt, |
| 27 | + ndim, |
| 28 | + stride, |
| 29 | + shape}; |
| 30 | + return STATUS_SUCCESS; |
| 31 | +} |
| 32 | + |
| 33 | +infiniopStatus_t tecoGetCausalSoftmaxWorkspaceSize(CausalSoftmaxTecoDescriptor_t desc, uint64_t *size) { |
| 34 | + *size = desc->ndim * sizeof(int) * 2; |
| 35 | + return STATUS_SUCCESS; |
| 36 | +} |
| 37 | + |
| 38 | +template<typename T> |
| 39 | +__global__ void causalSoftmax(T *destination, int *shape, int *stride, int ndim, int mask){ |
| 40 | + int othersize = 1; |
| 41 | + for(int i = 0; i < ndim - 1; i++){ |
| 42 | + othersize *= shape[i]; |
| 43 | + } |
| 44 | + int remain = othersize % threadDim; |
| 45 | + int step_easy = (othersize - remain) / threadDim; |
| 46 | + int step_hard = step_easy + 1; |
| 47 | + int step = (threadIdx < remain ? step_hard : step_easy); |
| 48 | + int ind_start = (threadIdx < remain ? threadIdx * step_hard : (remain * step_hard + (threadIdx - remain) * step_easy)); |
| 49 | + |
| 50 | + int dimsize = shape[ndim - 1]; |
| 51 | + int buf_size = 16; |
| 52 | + |
| 53 | + for (int i = ind_start; i < ind_start + step; i++) { |
| 54 | + int ind_d = 0; |
| 55 | + int ind_i = i; |
| 56 | + int lastI = ind_i % shape[ndim - 2]; |
| 57 | + |
| 58 | + int remain_dhead = (lastI + mask + 1) % buf_size; |
| 59 | + int repeat = (lastI + mask + 1 - remain_dhead) / buf_size;//针对前面这部分做softmax |
| 60 | + |
| 61 | + int length = dimsize - (lastI + mask + 1); |
| 62 | + int remainI = length % buf_size; |
| 63 | + int rI = (length - remainI) / buf_size;//把后面这部分赋值为0 |
| 64 | + |
| 65 | + for (int j = ndim - 2; j >= 0; --j) { |
| 66 | + ind_d += (ind_i % shape[j]) * stride[j]; |
| 67 | + ind_i /= shape[j]; |
| 68 | + } |
| 69 | + //下面开始计算max,sum |
| 70 | + |
| 71 | + float new_max = destination[ind_d]; |
| 72 | + float old_max = new_max; |
| 73 | + float sum_value = 0.0f; |
| 74 | + for(int r = 0; r < repeat; r++){ |
| 75 | + int start = ind_d + r * buf_size; |
| 76 | + if constexpr (std::is_same<T, half>::value){ |
| 77 | + simd_load(h_local, destination + start); |
| 78 | + f_local = simd_cvt_h2f(h_local); |
| 79 | + } |
| 80 | + else if constexpr (std::is_same<T, float>::value){ |
| 81 | + simd_load(f_local, destination + start); |
| 82 | + } |
| 83 | + for(int k = 0; k < buf_size; k++){ |
| 84 | + if(new_max < f_local[k]){ |
| 85 | + new_max = f_local[k]; |
| 86 | + } |
| 87 | + } |
| 88 | + for(int k = 0; k < buf_size; k++){ |
| 89 | + f_local[k] = expf(f_local[k] - new_max); |
| 90 | + } |
| 91 | + if(r > 0){ |
| 92 | + sum_value = sum_value * expf(old_max - new_max); |
| 93 | + } |
| 94 | + sum_value += simd_redsum(f_local); |
| 95 | + old_max = new_max; |
| 96 | + } |
| 97 | + if(remain_dhead){ |
| 98 | + int start = ind_d + repeat * buf_size; |
| 99 | + for(int k = 0; k < remain_dhead; k++){ |
| 100 | + if constexpr (std::is_same<T, half>::value){ |
| 101 | + if (new_max < static_cast<float>(destination[start + k])){ |
| 102 | + new_max = static_cast<float>(destination[start + k]); |
| 103 | + } |
| 104 | + } |
| 105 | + else if constexpr (std::is_same<T, float>::value){ |
| 106 | + if (new_max < destination[start + k]){ |
| 107 | + new_max = destination[start + k]; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + if (repeat > 0){ |
| 112 | + sum_value = sum_value * expf(old_max - new_max); |
| 113 | + } |
| 114 | + for(int k = 0; k < remain_dhead; k++){ |
| 115 | + if constexpr (std::is_same<T, half>::value){ |
| 116 | + sum_value += expf(static_cast<float>(destination[start + k]) - new_max); |
| 117 | + } |
| 118 | + else if constexpr (std::is_same<T, float>::value){ |
| 119 | + sum_value += expf(destination[start + k] - new_max); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + float sum_inv = 1.0f / sum_value; |
| 125 | + //下面开始做softmax变换 |
| 126 | + for(int r = 0; r < repeat; r++){ |
| 127 | + int start = ind_d + r * buf_size; |
| 128 | + if constexpr (std::is_same<T, half>::value){ |
| 129 | + simd_load(h_local, destination + start); |
| 130 | + f_local = simd_cvt_h2f(h_local); |
| 131 | + } |
| 132 | + else if constexpr (std::is_same<T, float>::value){ |
| 133 | + simd_load(f_local, destination + start); |
| 134 | + } |
| 135 | + |
| 136 | + for(int k = 0; k < buf_size; k++){ |
| 137 | + f_local[k] = expf(f_local[k] - new_max) * sum_inv; |
| 138 | + } |
| 139 | + if constexpr (std::is_same<T, half>::value){ |
| 140 | + h_local = simd_cvt_f2h(f_local); |
| 141 | + simd_store(h_local, destination + start); |
| 142 | + } |
| 143 | + else if constexpr (std::is_same<T, float>::value){ |
| 144 | + simd_store(f_local, destination + start); |
| 145 | + } |
| 146 | + } |
| 147 | + if(remain_dhead){ |
| 148 | + int start = ind_d + repeat * buf_size; |
| 149 | + for(int k = 0; k < remain_dhead; k++){ |
| 150 | + if constexpr (std::is_same<T, half>::value){ |
| 151 | + destination[start + k] = static_cast<half>(expf(static_cast<float>(destination[start + k]) - new_max) * sum_inv); |
| 152 | + } |
| 153 | + else if constexpr (std::is_same<T, float>::value){ |
| 154 | + destination[start + k] = expf(destination[start + k] - new_max) * sum_inv; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | + //针对剩下部分赋值为0 |
| 161 | + for(int r = 0; r < rI; r++){ |
| 162 | + int start = ind_d + mask + 1 + lastI + r * buf_size; |
| 163 | + if constexpr (std::is_same<T, half>::value){ |
| 164 | + for(int k = 0; k < buf_size; k++){ |
| 165 | + destination[start + k] = static_cast<half>(0.0f); |
| 166 | + } |
| 167 | + } |
| 168 | + else if constexpr (std::is_same<T, float>::value){ |
| 169 | + for(int k = 0; k < buf_size; k++){ |
| 170 | + destination[start + k] = 0.0f; |
| 171 | + } |
| 172 | + } |
| 173 | + /*** |
| 174 | + if constexpr (std::is_same<T, half>::value){ |
| 175 | + simd_load(h_local, destination + start); |
| 176 | + for(int k = 0; k < buf_size; k++){ |
| 177 | + h_local[k] = static_cast<half>(0.0f); |
| 178 | + } |
| 179 | + simd_store(h_local, destination + start); |
| 180 | + } |
| 181 | + else if constexpr (std::is_same<T, float>::value){ |
| 182 | + simd_load(f_local, destination + start); |
| 183 | + for(int k = 0; k < buf_size; k++){ |
| 184 | + f_local[k] = 0.0f; |
| 185 | + } |
| 186 | + simd_store(f_local, destination + start); |
| 187 | + } |
| 188 | + ***/ |
| 189 | + } |
| 190 | + |
| 191 | + if (remainI){ |
| 192 | + int start = ind_d + mask + 1 + lastI + rI * buf_size; |
| 193 | + if constexpr (std::is_same<T, half>::value){ |
| 194 | + for(int k = 0; k < remainI; k++){ |
| 195 | + destination[start + k] = static_cast<half>(0.0f); |
| 196 | + } |
| 197 | + } |
| 198 | + else if constexpr (std::is_same<T, float>::value){ |
| 199 | + for(int k = 0; k < remainI; k++){ |
| 200 | + destination[start + k] = 0.0f; |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +infiniopStatus_t tecoCausalSoftmax(CausalSoftmaxTecoDescriptor_t desc, |
| 209 | + void *workspace, |
| 210 | + uint64_t workspace_size, |
| 211 | + void *data, |
| 212 | + void *stream){ |
| 213 | + int ndim = desc->ndim; |
| 214 | + int mask = desc->shape[ndim - 1] - desc->shape[ndim - 2]; |
| 215 | + |
| 216 | + int *teco_stride = reinterpret_cast<int *>(workspace); |
| 217 | + int *teco_shape = teco_stride + ndim; |
| 218 | + |
| 219 | + sdaaMemcpy(teco_stride, desc->stride, ndim * sizeof(int), sdaaMemcpyHostToDevice); |
| 220 | + sdaaMemcpy(teco_shape, desc->shape, ndim * sizeof(int), sdaaMemcpyHostToDevice); |
| 221 | + sdaaDeviceSynchronize(); |
| 222 | + if(dtype_eq(desc->dtype, F16)){ |
| 223 | + auto destination = reinterpret_cast<half *>(data); |
| 224 | + causalSoftmax<half><<<1, (sdaaStream_t)stream>>>(destination, teco_shape, teco_stride, ndim, mask); |
| 225 | + sdaaDeviceSynchronize(); |
| 226 | + return STATUS_SUCCESS; |
| 227 | + } |
| 228 | + else if(dtype_eq(desc->dtype, F32)){ |
| 229 | + auto destination = reinterpret_cast<float *>(data); |
| 230 | + causalSoftmax<float><<<1, (sdaaStream_t)stream>>>(destination, teco_shape, teco_stride, ndim, mask); |
| 231 | + sdaaDeviceSynchronize(); |
| 232 | + return STATUS_SUCCESS; |
| 233 | + } |
| 234 | + |
| 235 | + return STATUS_BAD_TENSOR_DTYPE; |
| 236 | +} |
| 237 | + |
| 238 | +infiniopStatus_t tecoDestroyCausalSoftmaxDescriptor(CausalSoftmaxTecoDescriptor_t desc){ |
| 239 | + //free(desc->stride); |
| 240 | + //free(desc->shape); |
| 241 | + delete desc; |
| 242 | + return STATUS_SUCCESS; |
| 243 | +} |
0 commit comments