char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
749f688fc
char/aliceai-t5-moe
1enable f16; 2 3#ifdef TYPE_F32 4#define DataType f32 5#endif 6#ifdef TYPE_F16 7#define DataType f16 8#endif 9 10struct Params { 11 offset_src0: u32, 12 offset_src1: u32, 13 offset_src2: u32, 14 offset_dst: u32, 15 16 // Strides (in elements) 17 stride_src01: u32, 18 stride_src02: u32, 19 stride_src03: u32, 20 21 stride_dst1: u32, 22 stride_dst2: u32, 23 stride_dst3: u32, 24 25 n_threads: u32, 26 ne0: u32, 27 ne1: u32, 28 ne2: u32, 29 30 n_dims: u32, 31 mode: u32, 32 theta_scale: f32, 33 attn_factor: f32, 34 freq_scale: f32, 35 ext_factor: f32, 36 corr_dim0: f32, 37 corr_dim1: f32, 38 sections0: u32, 39 sections1: u32, 40 sections2: u32, 41 sections3: u32, 42 n_offs: u32 43}; 44 45@group(0) @binding(0) 46var<storage, read_write> src0: array<DataType>; 47@group(0) @binding(1) 48var<storage, read_write> src1: array<i32>; 49 50#ifdef INPLACE 51 52#ifdef FF_FUNC 53 54@group(0) @binding(2) 55var<storage, read_write> src2: array<f32>; 56 57@group(0) @binding(3) 58var<uniform> params: Params; 59 60#else 61 62@group(0) @binding(2) 63var<uniform> params: Params; 64 65#endif 66 67#else 68 69#ifdef FF_FUNC 70@group(0) @binding(2) 71var<storage, read_write> src2: array<f32>; 72 73@group(0) @binding(3) 74var<storage, read_write> dst: array<DataType>; 75 76@group(0) @binding(4) 77var<uniform> params: Params; 78 79#else 80@group(0) @binding(2) 81var<storage, read_write> dst: array<DataType>; 82 83@group(0) @binding(3) 84var<uniform> params: Params; 85#endif 86#endif 87 88#ifdef FF_FUNC 89fn freq_factor(i: u32) -> f32 { 90 return src2[params.offset_src2 + i/2]; 91} 92 93#else 94fn freq_factor(i: u32) -> f32 { 95 return 1.0f; 96} 97#endif 98#ifdef INPLACE 99fn rotate(i_dst0: u32, i_dst1: u32, out0: f32, out1: f32) { 100 src0[i_dst0] = DataType(out0); 101 src0[i_dst1] = DataType(out1); 102} 103#else 104fn rotate(i_dst0: u32, i_dst1: u32, out0: f32, out1: f32) { 105 dst[i_dst0] = DataType(out0); 106 dst[i_dst1] = DataType(out1); 107} 108#endif 109 110fn rope_yarn_ramp(low: f32, high: f32, i: u32) -> f32 { 111 let y = (f32(i / 2) - low) / max(0.001f, high - low); 112 return 1.0f - min(1.0f, max(0.0f, y)); 113} 114 115// returns vector of (cos_theta, sin_theta) 116// TODO: check performance of instantiating once on the CPU and passed as buffer, since it's repeated per-row 117fn rope_yarn(theta_extrap: f32, i: u32) -> vec2<f32> { 118 var mscale = params.attn_factor; 119 var theta = params.freq_scale * theta_extrap; 120 if (params.ext_factor != 0.0f) { 121 let ramp_mix = rope_yarn_ramp(params.corr_dim0, params.corr_dim1, i) * params.ext_factor; 122 theta = theta * (1 - ramp_mix) + theta_extrap * ramp_mix; 123 mscale *= 1.0f + 0.1f * log(1.0f / params.freq_scale); 124 } 125 return vec2<f32>(cos(theta) * mscale, sin(theta) * mscale); 126} 127 128fn pair_base(i0: u32, div_2: bool) -> u32 { 129 if (div_2) { 130 // first channel of the rotated pair: n_offs + (i0 - n_offs)/2 131 return i0 / 2 + params.n_offs / 2; 132 } else { 133 return i0; 134 } 135} 136 137fn pair_offset(is_neox: bool, is_mrope: bool, is_vision: bool) -> u32 { 138 if (is_vision) { 139 return params.n_dims; 140 } else if (is_neox || is_mrope) { 141 return params.n_dims / 2; 142 } else { 143 return 1; 144 } 145} 146 147@compute @workgroup_size(WG_SIZE) 148fn main(@builtin(global_invocation_id) gid: vec3<u32>) { 149 // two elements per n_threads 150 if (gid.x >= params.n_threads) { 151 return; 152 } 153 154 let is_neox = bool(params.mode & 2); 155 let is_mrope = bool(params.mode & 8); 156 let is_imrope = params.mode == 40; 157 let is_vision = params.mode == 24; 158 159 var i = gid.x * 2; // start index for this thread 160 let i3 = i / (params.ne2 * params.ne1 * params.ne0); 161 i = i % (params.ne2 * params.ne1 * params.ne0); 162 let i2 = i / (params.ne1 * params.ne0); 163 i = i % (params.ne1 * params.ne0); 164 let i1 = i / params.ne0; 165 let i0 = i % params.ne0; 166 167 let i_src_row = params.offset_src0 + i3 * params.stride_src03 + i2 * params.stride_src02 + i1 * params.stride_src01; 168 let i_dst_row = params.offset_dst + i3 * params.stride_dst3 + i2 * params.stride_dst2 + i1 * params.stride_dst1; 169 170 if ((i0 < params.n_offs || i0 >= params.n_offs + params.n_dims) && !is_vision) { 171 let i_src = i_src_row + i0; 172 let i_dst = i_dst_row + i0; 173 rotate(i_dst, i_dst + 1, f32(src0[i_src]), f32(src0[i_src + 1])); 174 return; 175 } 176 177 let iw = i0 - params.n_offs; // relative idx 178 179 var theta_base_mult: u32 = 0; 180 var theta_scale_pwr: u32 = iw / 2; 181 if (is_mrope) { 182 let sect_dims = params.sections0 + params.sections1 + params.sections2 + params.sections3; 183 let sec_w = params.sections1 + params.sections0; 184 let sec_e = params.sections2 + sec_w; 185 let sector = (iw / 2) % sect_dims; 186 if (is_imrope) { 187 if (sector % 3 == 1 && sector < 3 * params.sections1) { 188 theta_base_mult = 1; 189 } else if (sector % 3 == 2 && sector < 3 * params.sections2) { 190 theta_base_mult = 2; 191 } else if (sector % 3 == 0 && sector < 3 * params.sections0) { 192 theta_base_mult = 0; 193 } else { 194 theta_base_mult = 3; 195 } 196 } else { 197 if (sector >= params.sections0 && sector < sec_w) { 198 theta_base_mult = 1; 199 if (is_vision) { 200 theta_scale_pwr = sector - params.sections0; 201 } 202 } else if (sector >= sec_w && sector < sec_e) { 203 theta_base_mult = 2; 204 if (is_vision) { 205 theta_scale_pwr = sector - sec_w; 206 } 207 } else if (sector >= sec_e) { 208 if (is_vision) { 209 theta_scale_pwr = sector - sec_e; 210 theta_scale_pwr = (iw / 2) % sec_e; 211 } 212 theta_base_mult = 3; 213 } else if (is_vision) { 214 theta_scale_pwr = sector; 215 } 216 } 217 } 218 let theta_base = f32(src1[params.offset_src1 + i2 + params.ne2 * theta_base_mult]) * pow(params.theta_scale, f32(theta_scale_pwr)); 219 let thetas = rope_yarn(theta_base/freq_factor(iw), iw); 220 221 let i_src = i_src_row + pair_base(i0, is_neox || is_mrope || is_vision); 222 let i_dst = i_dst_row + pair_base(i0, is_neox || is_mrope || is_vision); 223 224 let x0 = f32(src0[i_src]); 225 let x1 = f32(src0[i_src + pair_offset(is_neox, is_mrope, is_vision)]); 226 rotate(i_dst, i_dst + pair_offset(is_neox, is_mrope, is_vision), x0 * thetas.x - x1 * thetas.y, x0 * thetas.y + x1 * thetas.x); 227 228}