|
| 1 | +#include "sinkhorn-norm.cuh" |
| 2 | + |
| 3 | +#define SINKHORN_NORM_MAX_N 8 |
| 4 | + |
| 5 | +template <int MAXN> |
| 6 | +static __global__ void sinkhorn_norm_f32( |
| 7 | + const float * __restrict__ x, |
| 8 | + float * __restrict__ y, |
| 9 | + const int n, |
| 10 | + const int n_iters, |
| 11 | + const float eps, |
| 12 | + const int64_t n_slices, |
| 13 | + const int64_t slice_stride) { |
| 14 | + |
| 15 | + const int64_t s = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; |
| 16 | + if (s >= n_slices) { |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + const float * xs = x + s * slice_stride; |
| 21 | + float * ys = y + s * slice_stride; |
| 22 | + |
| 23 | + float m[MAXN * MAXN]; |
| 24 | + const int nn = n * n; |
| 25 | + |
| 26 | + for (int i = 0; i < nn; ++i) { |
| 27 | + m[i] = xs[i]; |
| 28 | + } |
| 29 | + |
| 30 | + for (int b = 0; b < n; ++b) { |
| 31 | + float mx = -INFINITY; |
| 32 | + for (int a = 0; a < n; ++a) { |
| 33 | + mx = fmaxf(mx, m[b*n + a]); |
| 34 | + } |
| 35 | + float sum = 0.0f; |
| 36 | + for (int a = 0; a < n; ++a) { |
| 37 | + const float e = expf(m[b*n + a] - mx); |
| 38 | + m[b*n + a] = e; |
| 39 | + sum += e; |
| 40 | + } |
| 41 | + for (int a = 0; a < n; ++a) { |
| 42 | + m[b*n + a] /= sum; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + for (int i = 0; i < nn; ++i) { |
| 47 | + m[i] += eps; |
| 48 | + } |
| 49 | + |
| 50 | + #define NORM_COLS() do { \ |
| 51 | + for (int a = 0; a < n; ++a) { \ |
| 52 | + float r = 0.0f; \ |
| 53 | + for (int b = 0; b < n; ++b) r += m[b*n + a]; \ |
| 54 | + r += eps; \ |
| 55 | + for (int b = 0; b < n; ++b) m[b*n + a] /= r; \ |
| 56 | + } \ |
| 57 | + } while (0) |
| 58 | + |
| 59 | + #define NORM_ROWS() do { \ |
| 60 | + for (int b = 0; b < n; ++b) { \ |
| 61 | + float c = 0.0f; \ |
| 62 | + for (int a = 0; a < n; ++a) c += m[b*n + a]; \ |
| 63 | + c += eps; \ |
| 64 | + for (int a = 0; a < n; ++a) m[b*n + a] /= c; \ |
| 65 | + } \ |
| 66 | + } while (0) |
| 67 | + |
| 68 | + NORM_COLS(); |
| 69 | + for (int it = 1; it < n_iters; ++it) { |
| 70 | + NORM_ROWS(); |
| 71 | + NORM_COLS(); |
| 72 | + } |
| 73 | + |
| 74 | + #undef NORM_COLS |
| 75 | + #undef NORM_ROWS |
| 76 | + |
| 77 | + for (int i = 0; i < nn; ++i) { |
| 78 | + ys[i] = m[i]; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +template <int N> |
| 83 | +static __global__ void sinkhorn_norm_warp_f32( |
| 84 | + const float * __restrict__ x, |
| 85 | + float * __restrict__ y, |
| 86 | + const int n_iters, |
| 87 | + const float eps, |
| 88 | + const int64_t n_slices) { |
| 89 | + |
| 90 | + constexpr int NN = N * N; |
| 91 | + |
| 92 | + const int64_t g = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; |
| 93 | + const int64_t token = g / NN; |
| 94 | + if (token >= n_slices) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + const int e = threadIdx.x % NN; |
| 99 | + |
| 100 | + float v = x[token * NN + e]; |
| 101 | + |
| 102 | + float mx = v; |
| 103 | +#pragma unroll |
| 104 | + for (int off = 1; off < N; off <<= 1) { |
| 105 | + mx = fmaxf(mx, __shfl_xor_sync(0xffffffff, mx, off, NN)); |
| 106 | + } |
| 107 | + float ex = expf(v - mx); |
| 108 | + float sum = ex; |
| 109 | +#pragma unroll |
| 110 | + for (int off = 1; off < N; off <<= 1) { |
| 111 | + sum += __shfl_xor_sync(0xffffffff, sum, off, NN); |
| 112 | + } |
| 113 | + v = ex / sum + eps; |
| 114 | + |
| 115 | + { |
| 116 | + float r = v; |
| 117 | +#pragma unroll |
| 118 | + for (int off = N; off < NN; off <<= 1) { |
| 119 | + r += __shfl_xor_sync(0xffffffff, r, off, NN); |
| 120 | + } |
| 121 | + v /= (r + eps); |
| 122 | + } |
| 123 | + |
| 124 | + for (int it = 1; it < n_iters; ++it) { |
| 125 | + { |
| 126 | + float c = v; |
| 127 | +#pragma unroll |
| 128 | + for (int off = 1; off < N; off <<= 1) { |
| 129 | + c += __shfl_xor_sync(0xffffffff, c, off, NN); |
| 130 | + } |
| 131 | + v /= (c + eps); |
| 132 | + } |
| 133 | + { |
| 134 | + float r = v; |
| 135 | +#pragma unroll |
| 136 | + for (int off = N; off < NN; off <<= 1) { |
| 137 | + r += __shfl_xor_sync(0xffffffff, r, off, NN); |
| 138 | + } |
| 139 | + v /= (r + eps); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + y[token * NN + e] = v; |
| 144 | +} |
| 145 | + |
| 146 | +static bool sinkhorn_norm_is_pow2(int n) { |
| 147 | + return n > 0 && (n & (n - 1)) == 0; |
| 148 | +} |
| 149 | + |
| 150 | +void ggml_cuda_op_sinkhorn_norm(ggml_backend_cuda_context & ctx, ggml_tensor * dst) { |
| 151 | + const ggml_tensor * src0 = dst->src[0]; |
| 152 | + const float * src0_d = (const float *) src0->data; |
| 153 | + float * dst_d = (float *) dst->data; |
| 154 | + cudaStream_t stream = ctx.stream(); |
| 155 | + |
| 156 | + GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 157 | + GGML_ASSERT( dst->type == GGML_TYPE_F32); |
| 158 | + GGML_ASSERT(ggml_is_contiguous(src0)); |
| 159 | + |
| 160 | + GGML_TENSOR_UNARY_OP_LOCALS; |
| 161 | + |
| 162 | + const int32_t n_iters = ggml_get_op_params_i32(dst, 0); |
| 163 | + const float eps = ggml_get_op_params_f32(dst, 1); |
| 164 | + |
| 165 | + const int n = (int) ne00; |
| 166 | + GGML_ASSERT(ne01 == ne00); |
| 167 | + GGML_ASSERT(n <= SINKHORN_NORM_MAX_N); |
| 168 | + GGML_ASSERT(n_iters >= 1); |
| 169 | + |
| 170 | + const int64_t n_slices = ne02 * ne03; |
| 171 | + const int64_t slice_stride = (int64_t) n * n; |
| 172 | + |
| 173 | + const int block_size = 256; |
| 174 | + |
| 175 | + const int warp_size = ggml_cuda_info().devices[ggml_cuda_get_device()].warp_size; |
| 176 | + |
| 177 | + if (sinkhorn_norm_is_pow2(n) && n*n <= warp_size) { |
| 178 | + const int64_t total_threads = n_slices * (int64_t) (n*n); |
| 179 | + const int64_t num_blocks = (total_threads + block_size - 1) / block_size; |
| 180 | + switch (n) { |
| 181 | + case 2: sinkhorn_norm_warp_f32<2><<<num_blocks, block_size, 0, stream>>>(src0_d, dst_d, n_iters, eps, n_slices); break; |
| 182 | + case 4: sinkhorn_norm_warp_f32<4><<<num_blocks, block_size, 0, stream>>>(src0_d, dst_d, n_iters, eps, n_slices); break; |
| 183 | + case 8: sinkhorn_norm_warp_f32<8><<<num_blocks, block_size, 0, stream>>>(src0_d, dst_d, n_iters, eps, n_slices); break; |
| 184 | + default: GGML_ABORT("sinkhorn_norm: unsupported warp N=%d", n); |
| 185 | + } |
| 186 | + return; |
| 187 | + } |
| 188 | + |
| 189 | + const int64_t num_blocks = (n_slices + block_size - 1) / block_size; |
| 190 | + sinkhorn_norm_f32<SINKHORN_NORM_MAX_N><<<num_blocks, block_size, 0, stream>>>( |
| 191 | + src0_d, dst_d, n, n_iters, eps, n_slices, slice_stride); |
| 192 | +} |
0 commit comments