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/top-k-mask.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 / top-k-mask.cu` 141 lines ยท 5677 bytes Raw Blame History 1 #include "top-k-mask.cuh" 2 #include "convert.cuh" 3 4 #define CUDA_TOP_K_MASK_BLOCK_SIZE 512 5 6 // one block per row: fill the row with -inf, then copy the mask cells of the live picks and the extra cells 7 template 8 static __global__ void top_k_mask_kernel( 9 const T * mask, const int32_t * top_k, const float * score, const int32_t * blocks, const int32_t * extra, T * dst, 10 const int64_t n_kv, const int64_t ne1, const int64_t ne2, 11 const int64_t m1, const int64_t m2, const int64_t m3, 12 const int64_t n_top, const int64_t n_pick, const int64_t n_blk, const int64_t n_extra, 13 const int64_t sm1, const int64_t sm2, const int64_t sm3, 14 const int64_t sd1, const int64_t sd2, const int64_t sd3, 15 const int64_t sk0, const int64_t sk1, const int64_t sk2, const int64_t sk3, 16 const int64_t ss0, const int64_t ss1, const int64_t ss2, const int64_t ss3, 17 const int64_t sb0, const int64_t sb1, 18 const int64_t se0, const int64_t se1, const int64_t se2, const int64_t se3, 19 const bool vec) { 20 ggml_cuda_pdl_lc(); 21 22 const int64_t ir = blockIdx.x; 23 const int64_t i1 = ir % ne1; 24 const int64_t i2 = (ir / ne1) % ne2; 25 const int64_t i3 = ir / (ne1*ne2); 26 27 const T * mask_row = mask + (i1 % m1)*sm1 + (i2 % m2)*sm2 + (i3 % m3)*sm3; 28 T * dst_row = dst + i1*sd1 + i2*sd2 + i3*sd3; 29 const int32_t * top_k_row = top_k + i1*sk1 + i2*sk2 + i3*sk3; 30 31 ggml_cuda_pdl_sync(); 32 33 const T ninf = ggml_cuda_cast(-INFINITY); 34 if (vec) { 35 constexpr int nv = sizeof(int4)/sizeof(T); 36 T fill[nv]; 37 #pragma unroll 38 for (int j = 0; j < nv; ++j) { 39 fill[j] = ninf; 40 } 41 const int4 fill4 = *(const int4 *) fill; 42 for (int64_t i = threadIdx.x; i < n_kv/nv; i += blockDim.x) { 43 ((int4 *) dst_row)[i] = fill4; 44 } 45 } else { 46 for (int64_t i = threadIdx.x; i < n_kv; i += blockDim.x) { 47 dst_row[i] = ninf; 48 } 49 } 50 51 __syncthreads(); 52 53 // one thread per pick or extra cell; cells kept twice get the same value 54 for (int64_t i = threadIdx.x; i < n_top + n_extra; i += blockDim.x) { 55 if (i >= n_top) { 56 const int32_t c = extra[(i - n_top)*se0 + i1*se1 + i2*se2 + i3*se3]; 57 if (c >= 0 && c < n_kv) { 58 dst_row[c] = mask_row[c]; 59 } 60 continue; 61 } 62 63 const int32_t p = top_k_row[i*sk0]; 64 if (p < 0 || p >= n_pick) { 65 continue; 66 } 67 if (score && !(score[p*ss0 + i1*ss1 + i2*ss2 + i3*ss3] > -INFINITY)) { 68 continue; 69 } 70 if (!blocks) { 71 if (p < n_kv) { 72 dst_row[p] = mask_row[p]; 73 } 74 continue; 75 } 76 for (int64_t k = 0; k < n_blk; ++k) { 77 const int32_t c = blocks[k*sb0 + p*sb1]; 78 if (c >= 0 && c < n_kv) { 79 dst_row[c] = mask_row[c]; 80 } 81 } 82 } 83 } 84 85 template 86 static void top_k_mask_cuda(ggml_backend_cuda_context & ctx, ggml_tensor * dst) { 87 const ggml_tensor * mask = dst->src[0]; 88 const ggml_tensor * top_k = dst->src[1]; 89 const ggml_tensor * score = dst->src[2]; 90 const ggml_tensor * blocks = dst->src[3]; 91 const ggml_tensor * extra = dst->src[4]; 92 93 const int64_t n_kv = mask->ne[0]; 94 95 GGML_ASSERT(dst->nb[0] == sizeof(T) && mask->nb[0] == sizeof(T)); 96 GGML_ASSERT(dst->nb[1] % sizeof(T) == 0 && mask->nb[1] % sizeof(T) == 0); 97 98 // 16-byte stores need every dst row aligned 99 const bool vec = (n_kv*sizeof(T)) % sizeof(int4) == 0 && 100 dst->nb[1] % sizeof(int4) == 0 && dst->nb[2] % sizeof(int4) == 0 && dst->nb[3] % sizeof(int4) == 0 && 101 (uintptr_t) dst->data % sizeof(int4) == 0; 102 103 const int64_t n_pick = blocks ? blocks->ne[1] : score ? score->ne[0] : n_kv; 104 105 const size_t si = sizeof(int32_t); 106 const size_t sf = sizeof(float); 107 108 const dim3 block_dims(CUDA_TOP_K_MASK_BLOCK_SIZE, 1, 1); 109 const dim3 grid_dims(ggml_nrows(dst), 1, 1); 110 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(grid_dims, block_dims, 0, ctx.stream()); 111 112 ggml_cuda_kernel_launch(top_k_mask_kernel, launch_params, 113 (const T *) mask->data, (const int32_t *) top_k->data, 114 score ? (const float *) score->data : nullptr, 115 blocks ? (const int32_t *) blocks->data : nullptr, 116 extra ? (const int32_t *) extra->data : nullptr, 117 (T *) dst->data, 118 n_kv, dst->ne[1], dst->ne[2], 119 mask->ne[1], mask->ne[2], mask->ne[3], 120 top_k->ne[0], n_pick, blocks ? blocks->ne[0] : 1, extra ? extra->ne[0] : 0, 121 mask->nb[1]/sizeof(T), mask->nb[2]/sizeof(T), mask->nb[3]/sizeof(T), 122 dst->nb[1]/sizeof(T), dst->nb[2]/sizeof(T), dst->nb[3]/sizeof(T), 123 top_k->nb[0]/si, top_k->nb[1]/si, top_k->nb[2]/si, top_k->nb[3]/si, 124 score ? score->nb[0]/sf : 0, score ? score->nb[1]/sf : 0, score ? score->nb[2]/sf : 0, score ? score->nb[3]/sf : 0, 125 blocks ? blocks->nb[0]/si : 0, blocks ? blocks->nb[1]/si : 0, 126 extra ? extra->nb[0]/si : 0, extra ? extra->nb[1]/si : 0, extra ? extra->nb[2]/si : 0, extra ? extra->nb[3]/si : 0, 127 vec); 128 } 129 130 void ggml_cuda_op_top_k_mask(ggml_backend_cuda_context & ctx, ggml_tensor * dst) { 131 switch (dst->type) { 132 case GGML_TYPE_F16: 133 top_k_mask_cuda(ctx, dst); 134 break; 135 case GGML_TYPE_F32: 136 top_k_mask_cuda(ctx, dst); 137 break; 138 default: 139 GGML_ABORT("unsupported type"); 140 } 141 }