Skip to content

Commit c1f1e28

Browse files
CUDA: add fast walsh-hadamard transform (#23615)
* CUDA: add fast walsh-hadamard transform * review: add unrolls + change size_t -> int * warp size 64 --------- Co-authored-by: Johannes Gäßler <johannesg@5d6.de>
1 parent 5a4126a commit c1f1e28

4 files changed

Lines changed: 120 additions & 0 deletions

File tree

ggml/src/ggml-cuda/fwht.cu

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
}

ggml/src/ggml-cuda/fwht.cuh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "common.cuh"
2+
3+
void ggml_cuda_op_fwht(ggml_backend_cuda_context & ctx, const ggml_tensor * src, ggml_tensor * dst);

ggml/src/ggml-cuda/ggml-cuda.cu

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "ggml-cuda/diagmask.cuh"
2525
#include "ggml-cuda/diag.cuh"
2626
#include "ggml-cuda/fattn.cuh"
27+
#include "ggml-cuda/fwht.cuh"
2728
#include "ggml-cuda/getrows.cuh"
2829
#include "ggml-cuda/im2col.cuh"
2930
#include "ggml-cuda/mmf.cuh"
@@ -2594,6 +2595,13 @@ static void ggml_cuda_mul_mat(ggml_backend_cuda_context & ctx, const ggml_tensor
25942595
bool use_batched_cublas_bf16 = src0->type == GGML_TYPE_BF16 && bf16_mma_hardware_available(cc);
25952596
bool use_batched_cublas_f32 = src0->type == GGML_TYPE_F32;
25962597

2598+
const int32_t hint = ggml_get_op_params_i32(dst, 1);
2599+
if (hint == GGML_HINT_SRC0_IS_HADAMARD) {
2600+
GGML_ASSERT(!split);
2601+
ggml_cuda_op_fwht(ctx, src1, dst);
2602+
return;
2603+
}
2604+
25972605
if (!split && use_mul_mat_vec_f) {
25982606
// the custom F16 vector kernel can be used over batched cuBLAS GEMM
25992607
// but this is only faster for GPUs without tensor cores or with a thin src0 matrix (particularly KQV in attention)

tests/test-backend-ops.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8308,6 +8308,7 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
83088308
test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 64, 1, 64));
83098309
test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 256, 1, 256));
83108310
test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 128, 32, 128));
8311+
test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 128, 4, 128, {2, 3}));
83118312

83128313
#if 0
83138314
// > 4GB A matrix. Too slow to be enabled by default.

0 commit comments

Comments
 (0)