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 / mul_mv_f32_f32.cl
| 1 | #pragma OPENCL EXTENSION cl_khr_fp16 : enable |
| 2 | |
| 3 | #ifdef cl_intel_subgroups |
| 4 | #pragma OPENCL EXTENSION cl_intel_subgroups : enable |
| 5 | #else |
| 6 | #pragma OPENCL EXTENSION cl_khr_subgroups : enable |
| 7 | #endif |
| 8 | |
| 9 | #ifdef cl_intel_required_subgroup_size |
| 10 | #pragma OPENCL EXTENSION cl_intel_required_subgroup_size : enable |
| 11 | #define INTEL_GPU 1 |
| 12 | #define REQD_SUBGROUP_SIZE_16 __attribute__((intel_reqd_sub_group_size(16))) |
| 13 | #define REQD_SUBGROUP_SIZE_32 __attribute__((intel_reqd_sub_group_size(32))) |
| 14 | #elif defined(cl_qcom_reqd_sub_group_size) |
| 15 | #pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable |
| 16 | #define ADRENO_GPU 1 |
| 17 | #define REQD_SUBGROUP_SIZE_64 __attribute__((qcom_reqd_sub_group_size("half"))) |
| 18 | #define REQD_SUBGROUP_SIZE_128 __attribute__((qcom_reqd_sub_group_size("full"))) |
| 19 | #endif |
| 20 | |
| 21 | #define N_F32_F32 4 |
| 22 | |
| 23 | #ifdef ADRENO_GPU |
| 24 | REQD_SUBGROUP_SIZE_64 |
| 25 | #endif |
| 26 | kernel void kernel_mul_mat_f32_f32( |
| 27 | global char * src0, |
| 28 | ulong offset0, |
| 29 | global char * src1, |
| 30 | ulong offset1, |
| 31 | global float * dst, |
| 32 | ulong offsetd, |
| 33 | int ne00, |
| 34 | int ne01, |
| 35 | int ne02, |
| 36 | ulong nb00, |
| 37 | ulong nb01, |
| 38 | ulong nb02, |
| 39 | ulong nb03, |
| 40 | int ne10, |
| 41 | int ne11, |
| 42 | int ne12, |
| 43 | ulong nb10, |
| 44 | ulong nb11, |
| 45 | ulong nb12, |
| 46 | ulong nb13, |
| 47 | int ne0, |
| 48 | int ne1, |
| 49 | int r2, |
| 50 | int r3 |
| 51 | ) { |
| 52 | src0 = (global char*)((global char*)src0 + offset0); |
| 53 | src1 = (global char*)((global char*)src1 + offset1); |
| 54 | dst = (global float*)((global char*)dst + offsetd); |
| 55 | |
| 56 | int r0 = get_group_id(0); |
| 57 | int rb = get_group_id(1)*N_F32_F32; |
| 58 | int im = get_group_id(2); |
| 59 | |
| 60 | int i12 = im%ne12; |
| 61 | int i13 = im/ne12; |
| 62 | |
| 63 | ulong offset_src0 = r0*nb01 + (i12/r2)*nb02 + (i13/r3)*nb03; |
| 64 | |
| 65 | global float * x = (global float *) (src0 + offset_src0); |
| 66 | |
| 67 | if (ne00 < 128) { |
| 68 | for (int row = 0; row < N_F32_F32; ++row) { |
| 69 | int r1 = rb + row; |
| 70 | if (r1 >= ne11) { |
| 71 | break; |
| 72 | } |
| 73 | |
| 74 | ulong offset_src1 = r1*nb11 + (i12 )*nb12 + (i13 )*nb13; |
| 75 | |
| 76 | global float * y = (global float *) (src1 + offset_src1); |
| 77 | |
| 78 | float sumf = 0; |
| 79 | for (int i = get_sub_group_local_id(); i < ne00; i += get_max_sub_group_size()) { |
| 80 | sumf += (float) x[i] * (float) y[i]; |
| 81 | } |
| 82 | |
| 83 | float all_sum = sub_group_reduce_add(sumf); |
| 84 | if (get_sub_group_local_id() == 0) { |
| 85 | dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum; |
| 86 | } |
| 87 | } |
| 88 | } else { |
| 89 | global float4 * x4 = (global float4 *)x; |
| 90 | for (int row = 0; row < N_F32_F32; ++row) { |
| 91 | int r1 = rb + row; |
| 92 | if (r1 >= ne11) { |
| 93 | break; |
| 94 | } |
| 95 | |
| 96 | ulong offset_src1 = r1*nb11 + (i12 )*nb12 + (i13 )*nb13; |
| 97 | |
| 98 | global float * y = (global float *) (src1 + offset_src1); |
| 99 | global float4 * y4 = (global float4 *) y; |
| 100 | |
| 101 | float sumf = 0; |
| 102 | for (int i = get_sub_group_local_id(); i < ne00/4; i += get_max_sub_group_size()) { |
| 103 | sumf += (float) x4[i].s0 * y4[i].s0; |
| 104 | sumf += (float) x4[i].s1 * y4[i].s1; |
| 105 | sumf += (float) x4[i].s2 * y4[i].s2; |
| 106 | sumf += (float) x4[i].s3 * y4[i].s3; |
| 107 | } |
| 108 | |
| 109 | float all_sum = sub_group_reduce_add(sumf); |
| 110 | if (get_sub_group_local_id() == 0) { |
| 111 | for (int i = 4*(ne00/4); i < ne00; ++i) { |
| 112 | all_sum += (float) x[i] * y[i]; |
| 113 | } |
| 114 | dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // Each group computes two rows and four columns, with 32 lanes per row. |
| 121 | kernel void kernel_mul_mv_f32_f32_r2( |
| 122 | global const char * src0, |
| 123 | ulong offset0, |
| 124 | global const char * src1, |
| 125 | ulong offset1, |
| 126 | global float * dst, |
| 127 | ulong offsetd, |
| 128 | int ne00, |
| 129 | int ne01, |
| 130 | int ne02, |
| 131 | ulong nb00, |
| 132 | ulong nb01, |
| 133 | ulong nb02, |
| 134 | ulong nb03, |
| 135 | int ne10, |
| 136 | int ne11, |
| 137 | int ne12, |
| 138 | ulong nb10, |
| 139 | ulong nb11, |
| 140 | ulong nb12, |
| 141 | ulong nb13, |
| 142 | int ne0, |
| 143 | int ne1, |
| 144 | int r2, |
| 145 | int r3 |
| 146 | ) { |
| 147 | src0 += offset0; |
| 148 | src1 += offset1; |
| 149 | dst = (global float *)((global char *)dst + offsetd); |
| 150 | |
| 151 | const int lid = get_local_id(0); |
| 152 | const int lane = lid % 32; |
| 153 | const int row = get_group_id(0)*2 + lid/32; |
| 154 | const int col = get_group_id(1)*4; |
| 155 | const int im = get_group_id(2); |
| 156 | const int i12 = im % ne12; |
| 157 | const int i13 = im / ne12; |
| 158 | global const float4 * x = (global const float4 *)(src0 + min(row, ne01 - 1)*nb01 + (i12/r2)*nb02 + (i13/r3)*nb03); |
| 159 | global const char * y = src1 + col*nb11 + i12*nb12 + i13*nb13; |
| 160 | |
| 161 | float4 sum = 0.0f; |
| 162 | for (int i = lane; i < ne00/4; i += 32) { |
| 163 | const float4 xv = x[i]; |
| 164 | if (col + 0 < ne11) sum.s0 += dot(xv, ((global const float4 *)(y + 0*nb11))[i]); |
| 165 | if (col + 1 < ne11) sum.s1 += dot(xv, ((global const float4 *)(y + 1*nb11))[i]); |
| 166 | if (col + 2 < ne11) sum.s2 += dot(xv, ((global const float4 *)(y + 2*nb11))[i]); |
| 167 | if (col + 3 < ne11) sum.s3 += dot(xv, ((global const float4 *)(y + 3*nb11))[i]); |
| 168 | } |
| 169 | |
| 170 | local float4 part[64]; |
| 171 | part[lid] = sum; |
| 172 | barrier(CLK_LOCAL_MEM_FENCE); |
| 173 | for (int s = 16; s > 0; s /= 2) { |
| 174 | if (lane < s) { |
| 175 | part[lid] += part[lid + s]; |
| 176 | } |
| 177 | barrier(CLK_LOCAL_MEM_FENCE); |
| 178 | } |
| 179 | if (lane == 0 && row < ne01) { |
| 180 | if (col + 0 < ne11) dst[im*ne1*ne0 + (col + 0)*ne0 + row] = part[lid].s0; |
| 181 | if (col + 1 < ne11) dst[im*ne1*ne0 + (col + 1)*ne0 + row] = part[lid].s1; |
| 182 | if (col + 2 < ne11) dst[im*ne1*ne0 + (col + 2)*ne0 + row] = part[lid].s2; |
| 183 | if (col + 3 < ne11) dst[im*ne1*ne0 + (col + 3)*ne0 + row] = part[lid].s3; |
| 184 | } |
| 185 | } |