ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-cuda / fwht.cu
| 1 | #include "common.cuh" |
| 2 | #include "convert.cuh" |
| 3 | #include "fwht.cuh" |
| 4 | |
| 5 | // wide FWHT blocks use one row per thread block with this many threads |
| 6 | #define GGML_CUDA_FWHT_BLOCK_NT 256 |
| 7 | |
| 8 | template <int N, typename T> |
| 9 | __launch_bounds__(4*ggml_cuda_get_physical_warp_size(), 1) |
| 10 | __global__ void fwht_cuda(const T * src, float * dst, const int64_t n_rows, const float scale) { |
| 11 | constexpr int warp_size = ggml_cuda_get_physical_warp_size(); |
| 12 | |
| 13 | const int64_t r = (int64_t) blockIdx.x * blockDim.y + threadIdx.y; |
| 14 | |
| 15 | if (r >= n_rows) { |
| 16 | return; |
| 17 | } |
| 18 | |
| 19 | src += r * N; |
| 20 | dst += r * N; |
| 21 | |
| 22 | static constexpr int el_w = N / warp_size; |
| 23 | float reg[el_w]; |
| 24 | const int lane = threadIdx.x; |
| 25 | |
| 26 | ggml_cuda_pdl_sync(); |
| 27 | #pragma unroll |
| 28 | for (int i = 0; i < el_w; ++i) { |
| 29 | reg[i] = ggml_cuda_cast<float>(src[i * warp_size + lane]) * scale; |
| 30 | } |
| 31 | |
| 32 | #pragma unroll |
| 33 | for (int h = 1; h < warp_size; h *= 2) { |
| 34 | #pragma unroll |
| 35 | for (int j = 0; j < el_w; j++) { |
| 36 | const float val = reg[j]; |
| 37 | const float val2 = __shfl_xor_sync(0xFFFFFFFF, val, h, warp_size); |
| 38 | |
| 39 | reg[j] = (lane & h) == 0 ? val + val2 : val2 - val; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | #pragma unroll |
| 44 | for (int h = warp_size; h < N; h *= 2) { |
| 45 | const int step = h / warp_size; |
| 46 | #pragma unroll |
| 47 | for (int j = 0; j < el_w; j += 2 * step) { |
| 48 | #pragma unroll |
| 49 | for (int k = 0; k < step; k++) { |
| 50 | const float x = reg[j + k]; |
| 51 | const float y = reg[j + k + step]; |
| 52 | |
| 53 | reg[j + k] = x + y; |
| 54 | reg[j + k + step] = x - y; |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | #pragma unroll |
| 60 | for (int i = 0; i < el_w; ++i) { |
| 61 | dst[i * warp_size + lane] = reg[i]; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // Wide blocks: one row per thread block instead of per warp, so each thread keeps N/NT |
| 66 | // values rather than N/32. Stages below the warp width still shuffle, those up to the |
| 67 | // block width go through shared memory, and the rest stay in registers. |
| 68 | template <int N, int NT, typename T> |
| 69 | __launch_bounds__(NT, 1) |
| 70 | __global__ void fwht_cuda_block(const T * src, float * dst, const int64_t n_rows, const float scale) { |
| 71 | constexpr int warp_size = ggml_cuda_get_physical_warp_size(); |
| 72 | constexpr int NE = N / NT; |
| 73 | static_assert(NE >= 1 && N % NT == 0 && NT % warp_size == 0, "bad FWHT block shape"); |
| 74 | |
| 75 | __shared__ float s[N]; |
| 76 | |
| 77 | const int64_t r = blockIdx.x; |
| 78 | if (r >= n_rows) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | src += r * N; |
| 83 | dst += r * N; |
| 84 | |
| 85 | const int tid = threadIdx.x; |
| 86 | const int lane = tid % warp_size; |
| 87 | |
| 88 | ggml_cuda_pdl_sync(); |
| 89 | |
| 90 | float reg[NE]; |
| 91 | #pragma unroll |
| 92 | for (int i = 0; i < NE; ++i) { |
| 93 | reg[i] = ggml_cuda_cast<float>(src[i * NT + tid]) * scale; |
| 94 | } |
| 95 | |
| 96 | // stages within a warp: partner differs in the lane bits |
| 97 | #pragma unroll |
| 98 | for (int h = 1; h < warp_size; h *= 2) { |
| 99 | #pragma unroll |
| 100 | for (int j = 0; j < NE; j++) { |
| 101 | const float val = reg[j]; |
| 102 | const float val2 = __shfl_xor_sync(0xFFFFFFFF, val, h, warp_size); |
| 103 | reg[j] = (lane & h) == 0 ? val + val2 : val2 - val; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // stages across warps: partner differs in the thread-index bits above the lane |
| 108 | #pragma unroll |
| 109 | for (int h = warp_size; h < NT; h *= 2) { |
| 110 | #pragma unroll |
| 111 | for (int j = 0; j < NE; j++) { |
| 112 | s[j * NT + tid] = reg[j]; |
| 113 | } |
| 114 | __syncthreads(); |
| 115 | #pragma unroll |
| 116 | for (int j = 0; j < NE; j++) { |
| 117 | const float val = reg[j]; |
| 118 | const float val2 = s[j * NT + (tid ^ h)]; |
| 119 | reg[j] = (tid & h) == 0 ? val + val2 : val2 - val; |
| 120 | } |
| 121 | __syncthreads(); |
| 122 | } |
| 123 | |
| 124 | // stages above the block width: partner is another register of the same thread |
| 125 | #pragma unroll |
| 126 | for (int h = NT; h < N; h *= 2) { |
| 127 | const int step = h / NT; |
| 128 | #pragma unroll |
| 129 | for (int j = 0; j < NE; j += 2 * step) { |
| 130 | #pragma unroll |
| 131 | for (int k = 0; k < step; k++) { |
| 132 | const float x = reg[j + k]; |
| 133 | const float y = reg[j + k + step]; |
| 134 | reg[j + k] = x + y; |
| 135 | reg[j + k + step] = x - y; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | #pragma unroll |
| 141 | for (int i = 0; i < NE; ++i) { |
| 142 | dst[i * NT + tid] = reg[i]; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | template <typename T> |
| 147 | static bool ggml_cuda_op_fwht_impl(ggml_backend_cuda_context & ctx, const ggml_tensor * src, ggml_tensor * dst) { |
| 148 | const int n = src->ne[0]; |
| 149 | const int64_t rows = ggml_nrows(src); |
| 150 | |
| 151 | const T * src_d = (const T *) src->data; |
| 152 | float * dst_d = (float *) dst->data; |
| 153 | |
| 154 | const int warp_size = ggml_cuda_info().devices[ggml_cuda_get_device()].warp_size; |
| 155 | const int rows_per_block = 4; |
| 156 | |
| 157 | const int64_t num_blocks = (rows + rows_per_block - 1) / rows_per_block; |
| 158 | |
| 159 | cudaStream_t stream = ctx.stream(); |
| 160 | dim3 grid_dims(num_blocks, 1, 1); |
| 161 | dim3 block_dims(warp_size, rows_per_block, 1); |
| 162 | const ggml_cuda_kernel_launch_params launch_params = |
| 163 | ggml_cuda_kernel_launch_params(grid_dims, block_dims, 0, stream); |
| 164 | |
| 165 | const float scale = 1 / sqrtf(n); |
| 166 | |
| 167 | switch (n) { |
| 168 | case 64: |
| 169 | ggml_cuda_kernel_launch(fwht_cuda<64, T>, launch_params, src_d, dst_d, rows, scale); |
| 170 | return true; |
| 171 | case 128: |
| 172 | ggml_cuda_kernel_launch(fwht_cuda<128, T>, launch_params, src_d, dst_d, rows, scale); |
| 173 | return true; |
| 174 | case 256: |
| 175 | ggml_cuda_kernel_launch(fwht_cuda<256, T>, launch_params, src_d, dst_d, rows, scale); |
| 176 | return true; |
| 177 | case 512: |
| 178 | ggml_cuda_kernel_launch(fwht_cuda<512, T>, launch_params, src_d, dst_d, rows, scale); |
| 179 | return true; |
| 180 | default: |
| 181 | break; |
| 182 | } |
| 183 | |
| 184 | // wide blocks: one row per thread block |
| 185 | { |
| 186 | constexpr int nt = GGML_CUDA_FWHT_BLOCK_NT; |
| 187 | |
| 188 | dim3 grid_dims_w(rows, 1, 1); |
| 189 | dim3 block_dims_w(nt, 1, 1); |
| 190 | const ggml_cuda_kernel_launch_params launch_params_w = |
| 191 | ggml_cuda_kernel_launch_params(grid_dims_w, block_dims_w, 0, stream); |
| 192 | |
| 193 | switch (n) { |
| 194 | case 1024: |
| 195 | ggml_cuda_kernel_launch(fwht_cuda_block<1024, nt, T>, launch_params_w, src_d, dst_d, rows, scale); |
| 196 | return true; |
| 197 | case 2048: |
| 198 | ggml_cuda_kernel_launch(fwht_cuda_block<2048, nt, T>, launch_params_w, src_d, dst_d, rows, scale); |
| 199 | return true; |
| 200 | case 4096: |
| 201 | ggml_cuda_kernel_launch(fwht_cuda_block<4096, nt, T>, launch_params_w, src_d, dst_d, rows, scale); |
| 202 | return true; |
| 203 | #if !defined(GGML_USE_MUSA) |
| 204 | // 32 KB of shared memory, above the MUSA limit; falls back there |
| 205 | case 8192: |
| 206 | ggml_cuda_kernel_launch(fwht_cuda_block<8192, nt, T>, launch_params_w, src_d, dst_d, rows, scale); |
| 207 | return true; |
| 208 | #endif // !defined(GGML_USE_MUSA) |
| 209 | default: |
| 210 | return false; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | bool ggml_cuda_op_mul_mat_use_fwht(const struct ggml_tensor * op) { |
| 216 | const struct ggml_tensor * a = op->src[0]; |
| 217 | const struct ggml_tensor * b = op->src[1]; |
| 218 | |
| 219 | return op->op == GGML_OP_MUL_MAT && ggml_get_op_params_i32(op, 1) == GGML_HINT_SRC0_IS_HADAMARD && |
| 220 | a->type == GGML_TYPE_F32 && op->type == GGML_TYPE_F32 && |
| 221 | (b->type == GGML_TYPE_F32 || b->type == GGML_TYPE_F16) && ggml_is_contiguous(b) && ggml_is_contiguous(op) && |
| 222 | ggml_are_same_shape(b, op); |
| 223 | } |
| 224 | |
| 225 | bool ggml_cuda_op_fwht(ggml_backend_cuda_context & ctx, const ggml_tensor * src, ggml_tensor * dst) { |
| 226 | GGML_ASSERT(ggml_are_same_shape(src, dst)); |
| 227 | if (!ggml_is_contiguous(src) || !ggml_is_contiguous(dst)) { |
| 228 | return false; |
| 229 | } |
| 230 | if (dst->type != GGML_TYPE_F32) { |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | switch (src->type) { |
| 235 | case GGML_TYPE_F32: |
| 236 | return ggml_cuda_op_fwht_impl<float>(ctx, src, dst); |
| 237 | case GGML_TYPE_F16: |
| 238 | return ggml_cuda_op_fwht_impl<half>(ctx, src, dst); |
| 239 | default: |
| 240 | return false; |
| 241 | } |
| 242 | } |