Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-cuda/gated-delta-net-mma.cu # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-cuda / gated-delta-net-mma.cu` 443 lines ยท 18834 bytes Raw Blame History 1 #include "gated-delta-net-mma.cuh" 2 #include "mma.cuh" 3 4 #include 5 #include 6 7 namespace ggml_cuda_gated_delta_net_mma { 8 constexpr int D = 128; 9 using bf16 = nv_bfloat16; 10 11 template struct matrix { 12 bf16 hi[R * C], lo[R * C]; 13 14 static __device__ __forceinline__ int index(int r, int c) { 15 #ifdef GGML_USE_HIP 16 return r * C + c; 17 #else 18 return 2 * ggml_cuda_mma::swizzle(r * (C / 2) + c / 2, r) + c % 2; 19 #endif // GGML_USE_HIP 20 } 21 22 __device__ __forceinline__ void store2(int r, int c, float2 x) { 23 #ifdef GGML_USE_MUSA 24 store(r, c, x.x); 25 store(r, c + 1, x.y); 26 #else 27 const int i = index(r, c); 28 const auto h = __float22bfloat162_rn(x); 29 const float2 rounded = __bfloat1622float2(h); 30 *reinterpret_cast(hi + i) = h; 31 *reinterpret_cast(lo + i) = 32 __float22bfloat162_rn(make_float2(x.x - rounded.x, x.y - rounded.y)); 33 #endif // GGML_USE_MUSA 34 } 35 36 __device__ __forceinline__ void store(int r, int c, float x) { 37 const int i = index(r, c); 38 const bf16 h = __float2bfloat16(x); 39 hi[i] = h; 40 lo[i] = __float2bfloat16(x - __bfloat162float(h)); 41 } 42 }; 43 44 template struct alignas(128) shared { 45 matrix q, k; 46 matrix p, inverse; 47 matrix delta; 48 49 union { 50 matrix state; 51 matrix solved; 52 } scratch; 53 54 float lower[C * C], prefix[C], beta[C]; 55 56 static __device__ __forceinline__ int lower_index(int r, int c) { return r * C + c; } 57 }; 58 59 #if defined(AMPERE_MMA_AVAILABLE) || (defined(AMD_WMMA_AVAILABLE) && defined(RDNA3)) 60 using namespace ggml_cuda_mma; 61 #ifdef GGML_USE_HIP 62 using acc = tile<16, 16, float, DATA_LAYOUT_J_MAJOR>; 63 using a_tile = tile<16, 8, nv_bfloat162, DATA_LAYOUT_I_MAJOR_MIRRORED>; 64 using b_tile = a_tile; 65 #else 66 using acc = tile<16, 8, float>; 67 using a_tile = tile<16, 8, nv_bfloat162>; 68 using b_tile = tile<8, 8, nv_bfloat162>; 69 #endif // GGML_USE_HIP 70 71 template 72 __device__ __forceinline__ tile_t load(const matrix & m, int row, int col, bool low) { 73 tile_t t; 74 const bf16 * data = low ? m.lo : m.hi; 75 #ifdef GGML_USE_HIP 76 #pragma unroll 77 for (int i = 0; i < tile_t::ne; ++i) { 78 const int r = row + tile_t::get_i(i), c = col + 2 * tile_t::get_j(i); 79 const bf16 x = data[TRANS ? m.index(c, r) : m.index(r, c)]; 80 const bf16 y = data[TRANS ? m.index(c + 1, r) : m.index(r, c + 1)]; 81 t.x[i] = __float22bfloat162_rn(make_float2(__bfloat162float(x), __bfloat162float(y))); 82 } 83 #else 84 const auto * packed = reinterpret_cast(data); 85 if constexpr (TRANS) { 86 load_ldmatrix_trans_swizzled(t, packed, col * (C / 2) + row / 2); 87 } else { 88 load_ldmatrix_swizzled(t, packed, row * (C / 2) + col / 2); 89 } 90 #endif // GGML_USE_HIP 91 return t; 92 } 93 94 // B is addressed as B-transpose. Preserve both BF16 first-order residual terms. 95 template 96 __device__ __forceinline__ void product(acc & c, const matrix & a, const matrix & b, int row, int col) { 97 #pragma unroll 98 for (int k = 0; k < K; k += 16) { 99 const auto ah = load(a, row, k, false); 100 const auto al = load(a, row, k, true); 101 const auto bh = load(b, col, k, false); 102 const auto bl = load(b, col, k, true); 103 mma(c, al, bh); 104 mma(c, ah, bl); 105 mma(c, ah, bh); 106 } 107 } 108 109 template __device__ __forceinline__ void store(matrix & m, const acc & x, int r, int c) { 110 #pragma unroll 111 #ifdef GGML_USE_HIP 112 for (int i = 0; i < acc::ne; ++i) { 113 m.store(r + acc::get_i(i), c + acc::get_j(i), x.x[i]); 114 } 115 #else 116 for (int i = 0; i < acc::ne; i += 2) { 117 m.store2(r + acc::get_i(i), c + acc::get_j(i), make_float2(x.x[i], x.x[i + 1])); 118 } 119 #endif // GGML_USE_HIP 120 } 121 #endif // defined(AMPERE_MMA_AVAILABLE) || (defined(AMD_WMMA_AVAILABLE) && defined(RDNA3)) 122 123 // Each block owns V independent value columns and advances by C tokens. 124 // Keep recurrent state in FP32; pack high/residual BF16 operands inside the block. 125 template 126 static __global__ __launch_bounds__(256, BLOCKS) void gdn_single(ggml_cuda_gdn_mma_args a) { 127 #if defined(AMPERE_MMA_AVAILABLE) || (defined(AMD_WMMA_AVAILABLE) && defined(RDNA3)) 128 static_assert(ggml_cuda_get_physical_warp_size() == 32, "GDN MMA requires 32-lane warps"); 129 constexpr int WARPS = 8; 130 #ifdef GGML_USE_HIP 131 constexpr int N = 16; 132 #else 133 constexpr int N = 8; 134 #endif // GGML_USE_HIP 135 static_assert(C >= 16 && (C & (C - 1)) == 0 && V % N == 0 && D % V == 0, "invalid GDN tile"); 136 extern __shared__ __align__(128) unsigned char bytes[]; 137 auto & s = *reinterpret_cast *>(bytes); 138 const int lane = threadIdx.x, warp = threadIdx.y, tid = warp * 32 + lane; 139 const int slice = blockIdx.x % (D / V); 140 const int h = (blockIdx.x / (D / V)) % a.H; 141 const int seq = blockIdx.x / ((D / V) * a.H); 142 const int v0 = slice * V; 143 const int64_t soff = ((int64_t) seq * a.H + h) * D * D; 144 const int64_t qoff = (seq / a.rq3) * a.sq3 + (h % a.H_k) * a.sq1; 145 const int64_t voff = seq * a.sv3 + h * a.sv1 + v0; 146 const int64_t goff = seq * a.sb3 + h * a.sb1; 147 float * out = a.dst + ((int64_t) seq * a.n_tokens * a.H + h) * D + v0; 148 constexpr int STATE_TILES = (D / 16) * (V / N); 149 constexpr int PER_WARP = STATE_TILES / WARPS; 150 static_assert(STATE_TILES % WARPS == 0, "state must divide across warps"); 151 acc state[PER_WARP]; 152 #pragma unroll 153 for (int j = 0; j < PER_WARP; ++j) { 154 const int tile_index = warp + j * WARPS, r = (tile_index / (V / N)) * 16, c = (tile_index % (V / N)) * N; 155 #pragma unroll 156 for (int i = 0; i < acc::ne; ++i) { 157 state[j].x[i] = a.state[soff + (v0 + c + acc::get_j(i)) * D + r + acc::get_i(i)]; 158 } 159 } 160 auto read_qk = [&](int64_t t, float4 & q, float4 & k) { 161 t += warp; 162 q = k = make_float4(0, 0, 0, 0); 163 if (t < a.n_tokens) { 164 const int64_t off = qoff + t * a.sq2 + 4 * lane; 165 if ((((uintptr_t) (a.q + off) | (uintptr_t) (a.k + off)) & 15) == 0) { 166 q = *reinterpret_cast(a.q + off); 167 k = *reinterpret_cast(a.k + off); 168 } else { 169 q = make_float4(a.q[off], a.q[off + 1], a.q[off + 2], a.q[off + 3]); 170 k = make_float4(a.k[off], a.k[off + 1], a.k[off + 2], a.k[off + 3]); 171 } 172 } 173 }; 174 // Carry the first eight input rows across chunks to hide global-load latency. 175 constexpr bool PREFETCH = C == 16 && V == 32; 176 float4 next_q, next_k; 177 if constexpr (PREFETCH) { 178 read_qk(0, next_q, next_k); 179 } 180 for (int64_t t0 = 0; t0 < a.n_tokens; t0 += C) { 181 const int valid = min((int64_t) C, a.n_tokens - t0); 182 #pragma unroll 183 for (int j = 0; j < C / 8; ++j) { 184 const int t = warp + 8 * j, d = 4 * lane; 185 float4 q, k; 186 if (PREFETCH && j == 0) { 187 q = next_q; 188 k = next_k; 189 } else { 190 read_qk(t0 + 8 * j, q, k); 191 } 192 s.q.store2(t, d, make_float2(q.x * a.scale, q.y * a.scale)); 193 s.q.store2(t, d + 2, make_float2(q.z * a.scale, q.w * a.scale)); 194 s.k.store2(t, d, make_float2(k.x, k.y)); 195 s.k.store2(t, d + 2, make_float2(k.z, k.w)); 196 } 197 // Load gates in parallel before the FP32 prefix scan. 198 if (warp == 0) { 199 float carry = 0.f; 200 for (int base = 0; base < C; base += 32) { 201 const int t = base + lane; 202 float g = t < valid ? a.g[goff + (t0 + t) * a.sb2] : 0.f; 203 const float beta = t < valid ? a.beta[goff + (t0 + t) * a.sb2] : 0.f; 204 #pragma unroll 205 for (int offset = 1; offset < 32; offset *= 2) { 206 const float previous = __shfl_up_sync(0xffffffff, g, offset, 32); 207 if (lane >= offset) { 208 g += previous; 209 } 210 } 211 g += carry; 212 if (t < C) { 213 s.prefix[t] = g; 214 s.beta[t] = beta; 215 } 216 carry = __shfl_sync(0xffffffff, g, 31, 32); 217 } 218 } 219 #pragma unroll 220 for (int j = 0; j < PER_WARP; ++j) { 221 const int ti = warp + j * WARPS; 222 store(s.scratch.state, state[j], (ti / (V / N)) * 16, (ti % (V / N)) * N); 223 } 224 __syncthreads(); 225 if constexpr (PREFETCH) { 226 if (t0 + C < a.n_tokens) { 227 read_qk(t0 + C, next_q, next_k); 228 } 229 } 230 // V32 overlaps Gram work with one output tile per projection warp. 231 constexpr bool SPLIT_PROJECTION = C == 16 && V == 32; 232 acc base_output[((C / 16) * (V / N) + WARPS - 1) / WARPS]; 233 // delta = beta * (v - decay * K S), base output = decay * Q S 234 auto project = [&]() { 235 if constexpr (V == 32) { 236 if (warp < 4) { 237 return; 238 } 239 } 240 for (int ti = (V == 32 ? warp - 4 : warp); ti < (C / 16) * (V / N); ti += (V == 32 ? 4 : WARPS)) { 241 const int r = (ti / (V / N)) * 16, c = (ti % (V / N)) * N; 242 acc qs, ks; 243 product(qs, s.q, s.scratch.state, r, c); 244 product(ks, s.k, s.scratch.state, r, c); 245 #pragma unroll 246 for (int i = 0; i < acc::ne; ++i) { 247 const int t = r + acc::get_i(i), v = c + acc::get_j(i); 248 const float decay = expf(s.prefix[t]); 249 const float vv = t < valid ? a.v[voff + (t0 + t) * a.sv2 + v] : 0.f; 250 s.delta.store(t, v, s.beta[t] * (vv - decay * ks.x[i])); 251 base_output[ti / WARPS].x[i] = decay * qs.x[i]; 252 } 253 } 254 }; 255 // A = beta * strict_tril(K K^T * decay), P = tril(Q K^T * decay) 256 for (int ti = warp; ti < 2 * (C / 16) * (C / N) && (V != 32 || warp < 4); ti += (V == 32 ? 4 : WARPS)) { 257 const bool gram = ti < (C / 16) * (C / N); 258 const int t = ti % ((C / 16) * (C / N)), r = (t / (C / N)) * 16, c = (t % (C / N)) * N; 259 acc x; 260 if (gram) { 261 product(x, s.k, s.k, r, c); 262 } else { 263 product(x, s.q, s.k, r, c); 264 } 265 #pragma unroll 266 for (int i = 0; i < acc::ne; ++i) { 267 const int rr = r + acc::get_i(i), cc = c + acc::get_j(i); 268 const float v = rr < valid && cc <= rr ? x.x[i] * expf(s.prefix[rr] - s.prefix[cc]) : 0.f; 269 if (gram) { 270 if (cc < rr) { 271 s.lower[s.lower_index(rr, cc)] = v * s.beta[rr]; 272 } 273 } else { 274 s.p.store(rr, cc, v); 275 } 276 } 277 } 278 if constexpr (V == 32) { 279 project(); 280 } 281 __syncthreads(); 282 // inverse = (I + A)^-1 by forward substitution 283 for (int row = warp; row < C; row += WARPS) { 284 float x[(C + 31) / 32]; 285 #pragma unroll 286 for (int j = 0; j < (C + 31) / 32; ++j) { 287 const int col = lane + 32 * j; 288 x[j] = col == row ? 1.f : (col < row ? -s.lower[s.lower_index(row, col)] : 0.f); 289 } 290 for (int pivot = row - 1; pivot > 0; --pivot) { 291 const float xp = __shfl_sync(0xffffffff, x[pivot / 32], pivot % 32, 32); 292 #pragma unroll 293 for (int j = 0; j < (C + 31) / 32; ++j) { 294 const int col = lane + 32 * j; 295 if (col < pivot) { 296 x[j] -= xp * s.lower[s.lower_index(pivot, col)]; 297 } 298 } 299 } 300 #pragma unroll 301 for (int j = 0; j < (C + 31) / 32; ++j) { 302 if (lane + 32 * j < C) { 303 s.inverse.store(row, lane + 32 * j, x[j]); 304 } 305 } 306 } 307 if constexpr (V != 32) { 308 project(); 309 } 310 __syncthreads(); 311 // solved = inverse * delta 312 for (int ti = warp; ti < (C / 16) * (V / N); ti += WARPS) { 313 const int r = (ti / (V / N)) * 16, c = (ti % (V / N)) * N; 314 acc x; 315 product(x, s.inverse, s.delta, r, c); 316 store(s.scratch.solved, x, r, c); 317 } 318 __syncthreads(); 319 // out = base output + P * solved 320 for (int ti = (SPLIT_PROJECTION ? warp - 4 : warp); ti < (C / 16) * (V / N) && (!SPLIT_PROJECTION || warp >= 4); 321 ti += WARPS) { 322 const int r = (ti / (V / N)) * 16, c = (ti % (V / N)) * N; 323 acc x; 324 product(x, s.p, s.scratch.solved, r, c); 325 #pragma unroll 326 for (int i = 0; i < acc::ne; ++i) { 327 const int t = r + acc::get_i(i), v = c + acc::get_j(i); 328 if (t < valid) { 329 out[(t0 + t) * a.H * D + v] = base_output[ti / WARPS].x[i] + x.x[i]; 330 } 331 } 332 } 333 // S = decay(chunk) * S + K^T * (solved * decay to chunk end) 334 for (int i = tid; i < C * V; i += 256) { 335 const int t = i / V, v = i % V, j = s.scratch.solved.index(t, v); 336 const float x = __bfloat162float(s.scratch.solved.hi[j]) + __bfloat162float(s.scratch.solved.lo[j]); 337 s.delta.store(t, v, x * expf(s.prefix[valid - 1] - s.prefix[t])); 338 } 339 __syncthreads(); 340 const float decay = expf(s.prefix[valid - 1]); 341 #pragma unroll 342 for (int j = 0; j < PER_WARP; ++j) { 343 const int ti = warp + j * WARPS, r = (ti / (V / N)) * 16, c = (ti % (V / N)) * N; 344 #pragma unroll 345 for (int i = 0; i < acc::ne; ++i) { 346 state[j].x[i] *= decay; 347 } 348 product(state[j], s.k, s.delta, r, c); 349 } 350 __syncthreads(); 351 } 352 #pragma unroll 353 for (int j = 0; j < PER_WARP; ++j) { 354 const int ti = warp + j * WARPS, r = (ti / (V / N)) * 16, c = (ti % (V / N)) * N; 355 #pragma unroll 356 for (int i = 0; i < acc::ne; ++i) { 357 a.state_out[soff + (v0 + c + acc::get_j(i)) * D + r + acc::get_i(i)] = state[j].x[i]; 358 } 359 } 360 #else 361 GGML_UNUSED_VARS(a); 362 NO_DEVICE_CODE; 363 #endif // defined(AMPERE_MMA_AVAILABLE) || (defined(AMD_WMMA_AVAILABLE) && defined(RDNA3)) 364 } 365 366 template bool init(int device) { 367 static std::once_flag once[GGML_CUDA_MAX_DEVICES]; 368 static bool available[GGML_CUDA_MAX_DEVICES] = {}; 369 std::call_once(once[device], [device] { 370 ggml_cuda_set_device(device); 371 if (sizeof(shared) > ggml_cuda_info().devices[device].smpbo) { 372 return; 373 } 374 const auto status = cudaFuncSetAttribute((const void *) gdn_single, 375 cudaFuncAttributeMaxDynamicSharedMemorySize, sizeof(shared)); 376 available[device] = status == cudaSuccess; 377 if (status != cudaSuccess) { 378 (void) cudaGetLastError(); 379 } 380 }); 381 return available[device]; 382 } 383 384 enum class gdn_path { ar, slice32_one_block, slice32_two_blocks, slice64, whole_head }; 385 386 static gdn_path select_gdn_mma_path(int device, const ggml_cuda_gdn_mma_args & a) { 387 GGML_ASSERT(device >= 0 && device < GGML_CUDA_MAX_DEVICES); 388 if (!a.eligible || a.H_k != 16 || (a.H != 16 && a.H != 32 && a.H != 48 && a.H != 64) || a.n_tokens < 64 || 389 a.n_tokens > INT64_MAX - 32 || a.rq3 <= 0 || a.n_seqs <= 0 || a.n_seqs > INT_MAX / (a.H * 4)) { 390 return gdn_path::ar; 391 } 392 const auto & info = ggml_cuda_info().devices[device]; 393 #ifdef GGML_USE_HIP 394 if (!GGML_CUDA_CC_IS_RDNA3_5(info.cc) || a.n_tokens < 2048) { 395 return gdn_path::ar; 396 } 397 return init<16, 64>(device) ? gdn_path::slice64 : gdn_path::ar; 398 #elif defined(GGML_USE_MUSA) 399 GGML_UNUSED_VARS(info); 400 return gdn_path::ar; 401 #else 402 // Enable only architectures benchmarked for this schedule. MMA support alone does not imply a speedup. 403 if (!ampere_mma_available(info.cc) || 404 (info.cc != GGML_CUDA_CC_AMPERE_GA10X && info.cc != GGML_CUDA_CC_ADA_LOVELACE && 405 info.cc != GGML_CUDA_CC_BLACKWELL && info.cc != GGML_CUDA_CC_DGX_SPARK)) { 406 return gdn_path::ar; 407 } 408 // Value splitting pays off when whole heads leave at least half the SMs idle. 409 if (a.H * a.n_seqs * 2 <= info.nsm) { 410 // A grid that already fits in one wave benefits from a larger register budget. 411 if (a.H * a.n_seqs * 4 <= info.nsm) { 412 return init<16, 32, 1>(device) ? gdn_path::slice32_one_block : gdn_path::ar; 413 } 414 return init<16, 32, 2>(device) ? gdn_path::slice32_two_blocks : gdn_path::ar; 415 } 416 return init<16, 128>(device) ? gdn_path::whole_head : gdn_path::ar; 417 #endif // GGML_USE_HIP 418 } 419 } // namespace 420 using namespace ggml_cuda_gated_delta_net_mma; 421 422 bool ggml_cuda_gdn_mma_launch(int device, const ggml_cuda_gdn_mma_args & a, cudaStream_t stream) { 423 if (!a.state_out) { 424 return false; 425 } 426 const auto path = select_gdn_mma_path(device, a); 427 if (path == gdn_path::ar) { 428 return false; 429 } 430 #ifdef GGML_USE_HIP 431 gdn_single<16, 64><<), stream>>>(a); 432 #else 433 if (path == gdn_path::slice32_two_blocks) { 434 gdn_single<16, 32, 2><<), stream>>>(a); 435 } else if (path == gdn_path::slice32_one_block) { 436 gdn_single<16, 32, 1><<), stream>>>(a); 437 } else { 438 gdn_single<16, 128><<), stream>>>(a); 439 } 440 #endif // GGML_USE_HIP 441 CUDA_CHECK(cudaGetLastError()); 442 return true; 443 }