ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-metal / kernels / binbcast.metal
| 1 | #include "common.h" |
| 2 | |
| 3 | // OP: 0 - add, 1 - sub, 2 - mul, 3 - div |
| 4 | constant short FC_bin_op [[function_constant(FC_BIN + 0)]]; |
| 5 | constant short FC_bin_f [[function_constant(FC_BIN + 1)]]; |
| 6 | constant bool FC_bin_rb [[function_constant(FC_BIN + 2)]]; |
| 7 | constant bool FC_bin_cb [[function_constant(FC_BIN + 3)]]; |
| 8 | |
| 9 | template <typename T0, typename T1, typename T> |
| 10 | kernel void kernel_bin_fuse_impl( |
| 11 | constant ggml_metal_kargs_bin & args, |
| 12 | device const char * src0, |
| 13 | device const char * src1, |
| 14 | device char * dst, |
| 15 | uint3 tgpig[[threadgroup_position_in_grid]], |
| 16 | ushort3 tpitg[[thread_position_in_threadgroup]], |
| 17 | ushort3 ntg[[threads_per_threadgroup]]) { |
| 18 | #define FC_OP FC_bin_op |
| 19 | #define FC_F FC_bin_f |
| 20 | #define FC_RB FC_bin_rb |
| 21 | #define FC_CB FC_bin_cb |
| 22 | |
| 23 | if (FC_RB) { |
| 24 | // row broadcast |
| 25 | const uint i = tgpig.x*ntg.x + tpitg.x; |
| 26 | if (i >= (uint) args.ne0) { |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | const uint i0 = tgpig.y*args.ne00 + i; |
| 31 | const uint i1 = FC_CB ? i%args.ne10 : i; |
| 32 | |
| 33 | device const T0 * src0_row = (device const T0 *) (src0); |
| 34 | device T * dst_row = (device T *) (dst); |
| 35 | |
| 36 | if (FC_F == 1) { |
| 37 | device const T1 * src1_row = (device const T1 *) (src1 + args.o1[0]); |
| 38 | |
| 39 | if (FC_OP == 0) { |
| 40 | dst_row[i0] = src0_row[i0] + src1_row[i1]; |
| 41 | } |
| 42 | |
| 43 | if (FC_OP == 1) { |
| 44 | dst_row[i0] = src0_row[i0] - src1_row[i1]; |
| 45 | } |
| 46 | |
| 47 | if (FC_OP == 2) { |
| 48 | dst_row[i0] = src0_row[i0] * src1_row[i1]; |
| 49 | } |
| 50 | |
| 51 | if (FC_OP == 3) { |
| 52 | dst_row[i0] = src0_row[i0] / src1_row[i1]; |
| 53 | } |
| 54 | } else { |
| 55 | T0 res = src0_row[i0]; |
| 56 | |
| 57 | if (FC_OP == 0) { |
| 58 | FOR_UNROLL (short j = 0; j < FC_F; ++j) { |
| 59 | res += ((device const T1 *) (src1 + args.o1[j]))[i1]; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if (FC_OP == 1) { |
| 64 | FOR_UNROLL (short j = 0; j < FC_F; ++j) { |
| 65 | res -= ((device const T1 *) (src1 + args.o1[j]))[i1]; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if (FC_OP == 2) { |
| 70 | FOR_UNROLL (short j = 0; j < FC_F; ++j) { |
| 71 | res *= ((device const T1 *) (src1 + args.o1[j]))[i1]; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (FC_OP == 3) { |
| 76 | FOR_UNROLL (short j = 0; j < FC_F; ++j) { |
| 77 | res /= ((device const T1 *) (src1 + args.o1[j]))[i1]; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | dst_row[i0] = res; |
| 82 | } |
| 83 | } else { |
| 84 | const int i03 = tgpig.z; |
| 85 | const int i02 = tgpig.y; |
| 86 | const int i01 = tgpig.x; |
| 87 | |
| 88 | if (i01 >= args.ne01) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | const int i13 = i03%args.ne13; |
| 93 | const int i12 = i02%args.ne12; |
| 94 | const int i11 = i01%args.ne11; |
| 95 | |
| 96 | device const T0 * src0_ptr = (device const T0 *) (src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01 + args.offs); |
| 97 | device T * dst_ptr = (device T *) (dst + i03*args.nb3 + i02*args.nb2 + i01*args.nb1 + args.offs); |
| 98 | |
| 99 | if (FC_F == 1) { |
| 100 | device const T1 * src1_ptr = (device const T1 *) (src1 + args.o1[0] + i13*args.nb13 + i12*args.nb12 + i11*args.nb11); |
| 101 | |
| 102 | for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) { |
| 103 | const int i10 = FC_CB ? i0%args.ne10 : i0; |
| 104 | |
| 105 | if (FC_OP == 0) { |
| 106 | dst_ptr[i0] = src0_ptr[i0] + src1_ptr[i10]; |
| 107 | } |
| 108 | |
| 109 | if (FC_OP == 1) { |
| 110 | dst_ptr[i0] = src0_ptr[i0] - src1_ptr[i10]; |
| 111 | } |
| 112 | |
| 113 | if (FC_OP == 2) { |
| 114 | dst_ptr[i0] = src0_ptr[i0] * src1_ptr[i10]; |
| 115 | } |
| 116 | |
| 117 | if (FC_OP == 3) { |
| 118 | dst_ptr[i0] = src0_ptr[i0] / src1_ptr[i10]; |
| 119 | } |
| 120 | } |
| 121 | } else { |
| 122 | device const T1 * src1_ptr[8]; |
| 123 | FOR_UNROLL (short j = 0; j < FC_F; ++j) { |
| 124 | src1_ptr[j] = (device const T1 *) (src1 + args.o1[j] + i13*args.nb13 + i12*args.nb12 + i11*args.nb11); |
| 125 | } |
| 126 | |
| 127 | for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) { |
| 128 | const int i10 = FC_CB ? i0%args.ne10 : i0; |
| 129 | |
| 130 | T res = src0_ptr[i0]; |
| 131 | |
| 132 | if (FC_OP == 0) { |
| 133 | FOR_UNROLL (short j = 0; j < FC_F; ++j) { |
| 134 | res += src1_ptr[j][i10]; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if (FC_OP == 1) { |
| 139 | FOR_UNROLL (short j = 0; j < FC_F; ++j) { |
| 140 | res -= src1_ptr[j][i10]; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if (FC_OP == 2) { |
| 145 | FOR_UNROLL (short j = 0; j < FC_F; ++j) { |
| 146 | res *= src1_ptr[j][i10]; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | if (FC_OP == 3) { |
| 151 | FOR_UNROLL (short j = 0; j < FC_F; ++j) { |
| 152 | res /= src1_ptr[j][i10]; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | dst_ptr[i0] = res; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | #undef FC_OP |
| 162 | #undef FC_F |
| 163 | #undef FC_RB |
| 164 | #undef FC_CB |
| 165 | } |
| 166 | |
| 167 | typedef decltype(kernel_bin_fuse_impl<float, float, float>) kernel_bin_fuse_t; |
| 168 | |
| 169 | template [[host_name("kernel_bin_fuse_f32_f32_f32")]] kernel kernel_bin_fuse_t kernel_bin_fuse_impl<float, float, float>; |
| 170 | template [[host_name("kernel_bin_fuse_f32_f32_f32_4")]] kernel kernel_bin_fuse_t kernel_bin_fuse_impl<float4, float4, float4>; |
| 171 | template [[host_name("kernel_bin_fuse_f16_f16_f16")]] kernel kernel_bin_fuse_t kernel_bin_fuse_impl<half, half, half>; |
| 172 | template [[host_name("kernel_bin_fuse_f16_f16_f16_4")]] kernel kernel_bin_fuse_t kernel_bin_fuse_impl<half4, half4, half4>; |
| 173 | |
| 174 | kernel void kernel_add_id( |
| 175 | constant ggml_metal_kargs_add_id & args, |
| 176 | device const char * src0, |
| 177 | device const char * src1, |
| 178 | device const char * src2, |
| 179 | device char * dst, |
| 180 | uint3 tgpig[[threadgroup_position_in_grid]], |
| 181 | ushort3 tpitg[[thread_position_in_threadgroup]], |
| 182 | ushort3 ntg[[threads_per_threadgroup]]) { |
| 183 | const int i1 = tgpig.x; |
| 184 | const int i2 = tgpig.y; |
| 185 | |
| 186 | const int i11 = *((device const int32_t *) (src2 + i1*sizeof(int32_t) + i2*args.nb21)); |
| 187 | |
| 188 | const size_t nb1 = args.ne0 * sizeof(float); |
| 189 | const size_t nb2 = args.ne1 * nb1; |
| 190 | |
| 191 | device float * dst_row = (device float *)((device char *)dst + i1*nb1 + i2*nb2); |
| 192 | device const float * src0_row = (device const float *)((device char *)src0 + i1*args.nb01 + i2*args.nb02); |
| 193 | |
| 194 | if (i11 == -1) { |
| 195 | for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) { |
| 196 | dst_row[i0] = src0_row[i0]; |
| 197 | } |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | device const float * src1_row = (device const float *)((device char *)src1 + i11*args.nb11); |
| 202 | |
| 203 | for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) { |
| 204 | dst_row[i0] = src0_row[i0] + src1_row[i0]; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | template<typename T> |
| 209 | kernel void kernel_repeat( |
| 210 | constant ggml_metal_kargs_repeat & args, |
| 211 | device const char * src0, |
| 212 | device char * dst, |
| 213 | uint3 tgpig[[threadgroup_position_in_grid]], |
| 214 | ushort3 tpitg[[thread_position_in_threadgroup]], |
| 215 | ushort3 ntg[[threads_per_threadgroup]]) { |
| 216 | const int i3 = tgpig.z; |
| 217 | const int i2 = tgpig.y; |
| 218 | const int i1 = tgpig.x; |
| 219 | |
| 220 | const int i03 = i3%args.ne03; |
| 221 | const int i02 = i2%args.ne02; |
| 222 | const int i01 = i1%args.ne01; |
| 223 | |
| 224 | device const char * src0_ptr = src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01; |
| 225 | device char * dst_ptr = dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1; |
| 226 | |
| 227 | for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) { |
| 228 | const int i00 = i0%args.ne00; |
| 229 | *((device T *)(dst_ptr + i0*args.nb0)) = *((device T *)(src0_ptr + i00*args.nb00)); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | typedef decltype(kernel_repeat<float>) kernel_repeat_t; |
| 234 | |
| 235 | template [[host_name("kernel_repeat_f32")]] kernel kernel_repeat_t kernel_repeat<float>; |
| 236 | template [[host_name("kernel_repeat_f16")]] kernel kernel_repeat_t kernel_repeat<half>; |
| 237 | #if defined(GGML_METAL_HAS_BF16) |
| 238 | template [[host_name("kernel_repeat_bf16")]] kernel kernel_repeat_t kernel_repeat<bfloat>; |
| 239 | #endif |
| 240 | template [[host_name("kernel_repeat_i32")]] kernel kernel_repeat_t kernel_repeat<int>; |
| 241 | template [[host_name("kernel_repeat_i16")]] kernel kernel_repeat_t kernel_repeat<short>; |