ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-opencl / kernels / top_k_mask.cl
| 1 | // Integer storage preserves the mask bits without FP16 conversion. |
| 2 | #if MASK_F16 |
| 3 | #define MASK_T ushort |
| 4 | #define MASK_V ushort8 |
| 5 | #define MASK_NV 8 |
| 6 | #define MASK_NINF 0xfc00 |
| 7 | #define MASK_STORE vstore8 |
| 8 | #else |
| 9 | #define MASK_T uint |
| 10 | #define MASK_V uint4 |
| 11 | #define MASK_NV 4 |
| 12 | #define MASK_NINF 0xff800000u |
| 13 | #define MASK_STORE vstore4 |
| 14 | #endif |
| 15 | |
| 16 | kernel void kernel_top_k_mask_fill( |
| 17 | global char * dst, |
| 18 | ulong offsetd, |
| 19 | ulong n |
| 20 | ) { |
| 21 | global MASK_T * out = (global MASK_T *) (dst + offsetd); |
| 22 | const ulong c = get_global_id(0)*MASK_NV; |
| 23 | if (c + MASK_NV <= n) { |
| 24 | MASK_STORE((MASK_V) (MASK_NINF), 0, out + c); |
| 25 | } else { |
| 26 | for (ulong j = c; j < n; ++j) { |
| 27 | out[j] = MASK_NINF; |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | kernel void kernel_top_k_mask( |
| 33 | global const char * mask, |
| 34 | ulong offsetm, |
| 35 | global const char * top_k, |
| 36 | ulong offsetk, |
| 37 | global const char * score, |
| 38 | ulong offsets, |
| 39 | global const char * blocks, |
| 40 | ulong offsetb, |
| 41 | global const char * extra, |
| 42 | ulong offsete, |
| 43 | global char * dst, |
| 44 | ulong offsetd, |
| 45 | ulong nem0, |
| 46 | ulong nem1, |
| 47 | ulong nem2, |
| 48 | ulong nem3, |
| 49 | ulong nbm1, |
| 50 | ulong nbm2, |
| 51 | ulong nbm3, |
| 52 | ulong nek0, |
| 53 | ulong nbk0, |
| 54 | ulong nbk1, |
| 55 | ulong nbk2, |
| 56 | ulong nbk3, |
| 57 | ulong nes0, |
| 58 | ulong nbs0, |
| 59 | ulong nbs1, |
| 60 | ulong nbs2, |
| 61 | ulong nbs3, |
| 62 | ulong neb0, |
| 63 | ulong neb1, |
| 64 | ulong nbb0, |
| 65 | ulong nbb1, |
| 66 | ulong nee0, |
| 67 | ulong nbe0, |
| 68 | ulong nbe1, |
| 69 | ulong nbe2, |
| 70 | ulong nbe3, |
| 71 | ulong ned1, |
| 72 | ulong ned2, |
| 73 | ulong nbd1, |
| 74 | ulong nbd2, |
| 75 | ulong nbd3, |
| 76 | int fill |
| 77 | ) { |
| 78 | const ulong row = get_group_id(1); |
| 79 | const ulong i1 = row % ned1; |
| 80 | const ulong i2 = (row / ned1) % ned2; |
| 81 | const ulong i3 = row / (ned1*ned2); |
| 82 | const ulong tid = get_local_id(0); |
| 83 | const ulong nth = get_local_size(0); |
| 84 | const ulong n_pick = blocks ? neb1 : score ? nes0 : nem0; |
| 85 | |
| 86 | global const MASK_T * mask_row = (global const MASK_T *) (mask + offsetm |
| 87 | + (i1 % nem1)*nbm1 + (i2 % nem2)*nbm2 + (i3 % nem3)*nbm3); |
| 88 | global MASK_T * dst_row = (global MASK_T *) (dst + offsetd + i1*nbd1 + i2*nbd2 + i3*nbd3); |
| 89 | global const char * top_k_row = top_k + offsetk + i1*nbk1 + i2*nbk2 + i3*nbk3; |
| 90 | |
| 91 | if (fill) { |
| 92 | for (ulong c = tid; c < nem0/MASK_NV; c += nth) { |
| 93 | MASK_STORE((MASK_V) (MASK_NINF), c, dst_row); |
| 94 | } |
| 95 | for (ulong c = nem0/MASK_NV*MASK_NV + tid; c < nem0; c += nth) { |
| 96 | dst_row[c] = MASK_NINF; |
| 97 | } |
| 98 | barrier(CLK_GLOBAL_MEM_FENCE); |
| 99 | } |
| 100 | |
| 101 | // Repeated cells copy the same mask value, as in the CUDA and Metal kernels. |
| 102 | for (ulong j = tid; j < nek0 + nee0; j += nth) { |
| 103 | if (j >= nek0) { |
| 104 | const int c = *(global const int *) (extra + offsete + (j - nek0)*nbe0 + i1*nbe1 + i2*nbe2 + i3*nbe3); |
| 105 | if (c >= 0 && (ulong) c < nem0) { |
| 106 | dst_row[c] = mask_row[c]; |
| 107 | } |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | const int p = *(global const int *) (top_k_row + j*nbk0); |
| 112 | if (p < 0 || (ulong) p >= n_pick) { |
| 113 | continue; |
| 114 | } |
| 115 | if (score && !(*(global const float *) (score + offsets + p*nbs0 + i1*nbs1 + i2*nbs2 + i3*nbs3) > -INFINITY)) { |
| 116 | continue; |
| 117 | } |
| 118 | if (blocks) { |
| 119 | for (ulong k = 0; k < neb0; ++k) { |
| 120 | const int c = *(global const int *) (blocks + offsetb + k*nbb0 + p*nbb1); |
| 121 | if (c >= 0 && (ulong) c < nem0) { |
| 122 | dst_row[c] = mask_row[c]; |
| 123 | } |
| 124 | } |
| 125 | } else if ((ulong) p < nem0) { |
| 126 | dst_row[p] = mask_row[p]; |
| 127 | } |
| 128 | } |
| 129 | } |