|
| 1 | +#include "common.cuh" |
| 2 | +#include "fwht.cuh" |
| 3 | + |
| 4 | +template <int N> |
| 5 | +__launch_bounds__(4*ggml_cuda_get_physical_warp_size(), 1) |
| 6 | +__global__ void fwht_cuda(const float * src, float * dst, const int64_t n_rows, const float scale) { |
| 7 | + constexpr int warp_size = ggml_cuda_get_physical_warp_size(); |
| 8 | + |
| 9 | + const int64_t r = (int64_t) blockIdx.x * blockDim.y + threadIdx.y; |
| 10 | + |
| 11 | + if (r >= n_rows) { |
| 12 | + return; |
| 13 | + } |
| 14 | + |
| 15 | + src += r * N; |
| 16 | + dst += r * N; |
| 17 | + |
| 18 | + static constexpr int el_w = N / warp_size; |
| 19 | + float reg[el_w]; |
| 20 | + const int lane = threadIdx.x; |
| 21 | + |
| 22 | +#pragma unroll |
| 23 | + for (int i = 0; i < el_w; ++i) { |
| 24 | + reg[i] = src[i * warp_size + lane] * scale; |
| 25 | + } |
| 26 | + |
| 27 | +#pragma unroll |
| 28 | + for (int h = 1; h < warp_size; h *= 2) { |
| 29 | +#pragma unroll |
| 30 | + for (int j = 0; j < el_w; j++) { |
| 31 | + const float val = reg[j]; |
| 32 | + const float val2 = __shfl_xor_sync(0xFFFFFFFF, val, h, warp_size); |
| 33 | + |
| 34 | + reg[j] = (lane & h) == 0 ? val + val2 : val2 - val; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | +#pragma unroll |
| 39 | + for (int h = warp_size; h < N; h *= 2) { |
| 40 | + const int step = h / warp_size; |
| 41 | +#pragma unroll |
| 42 | + for (int j = 0; j < el_w; j += 2 * step) { |
| 43 | +#pragma unroll |
| 44 | + for (int k = 0; k < step; k++) { |
| 45 | + const float x = reg[j + k]; |
| 46 | + const float y = reg[j + k + step]; |
| 47 | + |
| 48 | + reg[j + k] = x + y; |
| 49 | + reg[j + k + step] = x - y; |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | +#pragma unroll |
| 55 | + for (int i = 0; i < el_w; ++i) { |
| 56 | + dst[i * warp_size + lane] = reg[i]; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +void ggml_cuda_op_fwht(ggml_backend_cuda_context & ctx, const ggml_tensor * src, ggml_tensor * dst) { |
| 61 | + GGML_ASSERT(ggml_are_same_shape(src, dst)); |
| 62 | + GGML_ASSERT(ggml_is_contiguous(src)); |
| 63 | + GGML_ASSERT(ggml_is_contiguous(dst)); |
| 64 | + const int n = src->ne[0]; |
| 65 | + const int64_t rows = ggml_nrows(src); |
| 66 | + |
| 67 | + const float * src_d = (const float *) src->data; |
| 68 | + float * dst_d = (float *) dst->data; |
| 69 | + |
| 70 | + const int warp_size = ggml_cuda_info().devices[ggml_cuda_get_device()].warp_size; |
| 71 | + GGML_ASSERT(n % warp_size == 0); |
| 72 | + const int rows_per_block = 4; |
| 73 | + |
| 74 | + const int64_t num_blocks = (rows + rows_per_block - 1) / rows_per_block; |
| 75 | + |
| 76 | + cudaStream_t stream = ctx.stream(); |
| 77 | + dim3 grid_dims(num_blocks, 1, 1); |
| 78 | + dim3 block_dims(warp_size, rows_per_block, 1); |
| 79 | + const ggml_cuda_kernel_launch_params launch_params = |
| 80 | + ggml_cuda_kernel_launch_params(grid_dims, block_dims, 0, stream); |
| 81 | + |
| 82 | + const float scale = 1 / sqrtf(n); |
| 83 | + |
| 84 | + switch (n) { |
| 85 | + case 64: |
| 86 | + { |
| 87 | + ggml_cuda_kernel_launch(fwht_cuda<64>, launch_params, src_d, dst_d, rows, scale); |
| 88 | + break; |
| 89 | + } |
| 90 | + case 128: |
| 91 | + { |
| 92 | + ggml_cuda_kernel_launch(fwht_cuda<128>, launch_params, src_d, dst_d, rows, scale); |
| 93 | + break; |
| 94 | + } |
| 95 | + case 256: |
| 96 | + { |
| 97 | + ggml_cuda_kernel_launch(fwht_cuda<256>, launch_params, src_d, dst_d, rows, scale); |
| 98 | + break; |
| 99 | + } |
| 100 | + case 512: |
| 101 | + { |
| 102 | + ggml_cuda_kernel_launch(fwht_cuda<512>, launch_params, src_d, dst_d, rows, scale); |
| 103 | + break; |
| 104 | + } |
| 105 | + default: |
| 106 | + GGML_ABORT("fatal error"); |
| 107 | + } |
| 108 | +} |
0 commit comments