ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-sycl / norm.cpp
| 1 | #include "norm.hpp" |
| 2 | #include "ggml-sycl/common.hpp" |
| 3 | #include "ggml-sycl/presets.hpp" |
| 4 | |
| 5 | static void norm_f32(const float* x, float* dst, const int ncols, |
| 6 | const int64_t src_stride_col, const int64_t src_stride_row, const int64_t src_stride_channel, const int64_t src_stride_sample, |
| 7 | const int64_t dst_stride_col, const int64_t dst_stride_row, const int64_t dst_stride_channel, const int64_t dst_stride_sample, |
| 8 | const float eps, const sycl::nd_item<3>& item_ct1, sycl::float2* s_sum, int block_size) { |
| 9 | |
| 10 | const int nthreads = item_ct1.get_local_range(2); |
| 11 | const int sample = item_ct1.get_group(0); |
| 12 | const int channel = item_ct1.get_group(1); |
| 13 | const int row = item_ct1.get_group(2); |
| 14 | |
| 15 | const int tid = item_ct1.get_local_id(2); |
| 16 | const int nwarps = nthreads / WARP_SIZE; |
| 17 | |
| 18 | const auto src_offset = calculate_offset<3>({src_stride_sample, src_stride_channel, src_stride_row}, {sample, channel, row}); |
| 19 | const auto dst_offset = calculate_offset<3>({dst_stride_sample, dst_stride_channel, dst_stride_row}, {sample, channel, row}); |
| 20 | |
| 21 | x += src_offset; |
| 22 | dst += dst_offset; |
| 23 | |
| 24 | sycl::float2 mean_var = sycl::float2(0.f, 0.f); |
| 25 | |
| 26 | for (int col = tid; col < ncols; col += block_size) { |
| 27 | const float xi = x[col * src_stride_col]; |
| 28 | mean_var.x() += xi; |
| 29 | mean_var.y() += xi * xi; |
| 30 | } |
| 31 | |
| 32 | // sum up partial sums |
| 33 | mean_var = warp_reduce_sum(mean_var, item_ct1); |
| 34 | if (block_size > WARP_SIZE) { |
| 35 | const auto sub_group = item_ct1.get_sub_group(); |
| 36 | const auto sg_id = sub_group.get_group_linear_id(); |
| 37 | const auto wi_in_sg = sub_group.get_local_linear_id(); |
| 38 | if (wi_in_sg == 0) { |
| 39 | s_sum[sg_id] = mean_var; |
| 40 | } |
| 41 | item_ct1.barrier(sycl::access::fence_space::local_space); |
| 42 | mean_var = 0.f; |
| 43 | const size_t nreduce = ceil_div(nwarps, WARP_SIZE); |
| 44 | for (size_t i = 0; i < nreduce; i += 1) |
| 45 | { |
| 46 | mean_var += s_sum[wi_in_sg + i * WARP_SIZE]; |
| 47 | } |
| 48 | mean_var = warp_reduce_sum(mean_var, item_ct1); |
| 49 | } |
| 50 | |
| 51 | const float mean = mean_var.x() / ncols; |
| 52 | const float var = mean_var.y() / ncols - mean * mean; |
| 53 | const float inv_std = sycl::rsqrt(var + eps); |
| 54 | |
| 55 | for (int col = tid; col < ncols; col += block_size) { |
| 56 | dst[col * dst_stride_col] = (x[col * src_stride_col] - mean) * inv_std; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | static void group_norm_f32(const float* x, float* dst, const int group_size, const int ne_elements, const float eps, |
| 61 | const sycl::nd_item<3>& item_ct1, float* s_sum, int block_size) { |
| 62 | int start = item_ct1.get_group(2) * group_size; |
| 63 | int end = start + group_size; |
| 64 | const int nthreads = item_ct1.get_local_range(2); |
| 65 | const int nwarps = nthreads / WARP_SIZE; |
| 66 | start += item_ct1.get_local_id(2); |
| 67 | size_t nreduce = nwarps / WARP_SIZE; |
| 68 | |
| 69 | if (end >= ne_elements) { |
| 70 | end = ne_elements; |
| 71 | } |
| 72 | |
| 73 | float tmp = 0.0f; // partial sum for thread in warp |
| 74 | |
| 75 | for (int j = start; j < end; j += block_size) { |
| 76 | tmp += x[j]; |
| 77 | } |
| 78 | |
| 79 | tmp = warp_reduce_sum(tmp, item_ct1); |
| 80 | if (block_size > WARP_SIZE) { |
| 81 | |
| 82 | int warp_id = item_ct1.get_local_id(2) / WARP_SIZE; |
| 83 | int lane_id = item_ct1.get_local_id(2) % WARP_SIZE; |
| 84 | if (lane_id == 0) { |
| 85 | s_sum[warp_id] = tmp; |
| 86 | } |
| 87 | /* |
| 88 | DPCT1118:1: SYCL group functions and algorithms must be encountered in |
| 89 | converged control flow. You may need to adjust the code. |
| 90 | */ |
| 91 | /* |
| 92 | DPCT1065:54: Consider replacing sycl::nd_item::barrier() with |
| 93 | sycl::nd_item::barrier(sycl::access::fence_space::local_space) for |
| 94 | better performance if there is no access to global memory. |
| 95 | */ |
| 96 | item_ct1.barrier(); |
| 97 | tmp = 0.f; |
| 98 | for (size_t i = 0; i < nreduce; i += 1) |
| 99 | { |
| 100 | tmp += s_sum[lane_id + i * WARP_SIZE]; |
| 101 | } |
| 102 | tmp = warp_reduce_sum(tmp, item_ct1); |
| 103 | } |
| 104 | |
| 105 | float mean = tmp / group_size; |
| 106 | tmp = 0.0f; |
| 107 | |
| 108 | for (int j = start; j < end; j += block_size) { |
| 109 | float xi = x[j] - mean; |
| 110 | dst[j] = xi; |
| 111 | tmp += xi * xi; |
| 112 | } |
| 113 | |
| 114 | tmp = warp_reduce_sum(tmp, item_ct1); |
| 115 | if (block_size > WARP_SIZE) { |
| 116 | |
| 117 | int warp_id = item_ct1.get_local_id(2) / WARP_SIZE; |
| 118 | int lane_id = item_ct1.get_local_id(2) % WARP_SIZE; |
| 119 | if (lane_id == 0) { |
| 120 | s_sum[warp_id] = tmp; |
| 121 | } |
| 122 | /* |
| 123 | DPCT1118:2: SYCL group functions and algorithms must be encountered in |
| 124 | converged control flow. You may need to adjust the code. |
| 125 | */ |
| 126 | /* |
| 127 | DPCT1065:55: Consider replacing sycl::nd_item::barrier() with |
| 128 | sycl::nd_item::barrier(sycl::access::fence_space::local_space) for |
| 129 | better performance if there is no access to global memory. |
| 130 | */ |
| 131 | item_ct1.barrier(); |
| 132 | tmp = 0.f; |
| 133 | for (size_t i = 0; i < nreduce; i += 1) |
| 134 | { |
| 135 | tmp += s_sum[lane_id + i * WARP_SIZE]; |
| 136 | } |
| 137 | tmp = warp_reduce_sum(tmp, item_ct1); |
| 138 | } |
| 139 | |
| 140 | float variance = tmp / group_size; |
| 141 | float scale = sycl::rsqrt(variance + eps); |
| 142 | for (int j = start; j < end; j += block_size) { |
| 143 | dst[j] *= scale; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | template <bool do_multiply = false, bool do_add = false> |
| 148 | static void rms_norm_f32(const float* x, float* dst, const int ncols, |
| 149 | const int64_t src_stride_col, const int64_t src_stride_row, const int64_t src_stride_channel, const int64_t src_stride_sample, |
| 150 | const int64_t dst_stride_col, const int64_t dst_stride_row, const int64_t dst_stride_channel, const int64_t dst_stride_sample, |
| 151 | const float eps, const sycl::nd_item<3>& item_ct1, float* s_sum, int block_size, |
| 152 | const float* mul = nullptr, const int64_t mul_stride_row = 0, const int64_t mul_stride_channel = 0, |
| 153 | const int64_t mul_stride_sample = 0, const int mul_nrows = 0, const int mul_nchannels = 0, const int mul_nsamples = 0, |
| 154 | const float* add = nullptr, const int64_t add_stride_row = 0, const int64_t add_stride_channel = 0, |
| 155 | const int64_t add_stride_sample = 0, const int add_nrows = 0, const int add_nchannels = 0, const int add_nsamples = 0, |
| 156 | const float scale_mul = 1.0f) { |
| 157 | |
| 158 | static_assert(!do_add || do_multiply, "fusing add is not supported without multiplying"); |
| 159 | |
| 160 | const int sample = item_ct1.get_group(0); |
| 161 | const int channel = item_ct1.get_group(1); |
| 162 | const int row = item_ct1.get_group(2); |
| 163 | |
| 164 | const int nthreads = item_ct1.get_local_range(2); |
| 165 | |
| 166 | const int tid = item_ct1.get_local_id(2); |
| 167 | const int nwarps = nthreads / WARP_SIZE; |
| 168 | |
| 169 | const auto src_offset = calculate_offset<3>({src_stride_sample, src_stride_channel, src_stride_row}, {sample, channel, row}); |
| 170 | const auto dst_offset = calculate_offset<3>({dst_stride_sample, dst_stride_channel, dst_stride_row}, {sample, channel, row}); |
| 171 | |
| 172 | x += src_offset; |
| 173 | dst += dst_offset; |
| 174 | |
| 175 | if constexpr (do_multiply) { |
| 176 | const int mul_row = row % mul_nrows; |
| 177 | const int mul_channel = channel % mul_nchannels; |
| 178 | const int mul_sample = sample % mul_nsamples; |
| 179 | mul += mul_sample * mul_stride_sample + mul_channel * mul_stride_channel + mul_row * mul_stride_row; |
| 180 | } |
| 181 | |
| 182 | if constexpr (do_add) { |
| 183 | const int add_row = row % add_nrows; |
| 184 | const int add_channel = channel % add_nchannels; |
| 185 | const int add_sample = sample % add_nsamples; |
| 186 | add += add_sample * add_stride_sample + add_channel * add_stride_channel + add_row * add_stride_row; |
| 187 | } |
| 188 | |
| 189 | float tmp = 0.0f; // partial sum for thread in warp |
| 190 | |
| 191 | for (int col = tid; col < ncols; col += block_size) { |
| 192 | const float xi = x[col * src_stride_col]; |
| 193 | tmp += xi * xi; |
| 194 | } |
| 195 | |
| 196 | // sum up partial sums |
| 197 | tmp = warp_reduce_sum(tmp, item_ct1); |
| 198 | if (block_size > WARP_SIZE) { |
| 199 | const auto sub_group = item_ct1.get_sub_group(); |
| 200 | const auto sg_id = sub_group.get_group_linear_id(); |
| 201 | const auto wi_in_sg = sub_group.get_local_linear_id(); |
| 202 | if (wi_in_sg == 0) { |
| 203 | s_sum[sg_id] = tmp; |
| 204 | } |
| 205 | |
| 206 | item_ct1.barrier(sycl::access::fence_space::local_space); |
| 207 | const size_t nreduce = ceil_div(nwarps, WARP_SIZE); |
| 208 | tmp = 0.f; |
| 209 | for (size_t i = 0; i < nreduce; i += 1) |
| 210 | { |
| 211 | tmp += s_sum[wi_in_sg + i * WARP_SIZE]; |
| 212 | } |
| 213 | tmp = warp_reduce_sum(tmp, item_ct1); |
| 214 | } |
| 215 | |
| 216 | const float mean = tmp / ncols; |
| 217 | const float scale = sycl::rsqrt(mean + eps); |
| 218 | |
| 219 | for (int col = tid; col < ncols; col += block_size) { |
| 220 | if constexpr (do_multiply && do_add) { |
| 221 | dst[col * dst_stride_col] = scale * x[col * src_stride_col] * mul[col] + add[col]; |
| 222 | } else if constexpr (do_multiply) { |
| 223 | dst[col * dst_stride_col] = scale * x[col * src_stride_col] * mul[col]; |
| 224 | } else { |
| 225 | // folded epilogue of a fused GGML_OP_SCALE consumer (qwen35 GDN l2 norms); |
| 226 | // the explicit temporary keeps the float evaluation order identical to |
| 227 | // running rms_norm and scale as two separate kernels |
| 228 | const float v = scale * x[col * src_stride_col]; |
| 229 | dst[col * dst_stride_col] = v * scale_mul; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | template<int warp_size> |
| 235 | static void l2_norm_f32(const float * x, float * dst, const int ncols, |
| 236 | const int64_t src_stride_col, const int64_t src_stride_row, const int64_t src_stride_channel, |
| 237 | const int64_t src_stride_sample, const int64_t dst_stride_col, const int64_t dst_stride_row, |
| 238 | const int64_t dst_stride_channel, const int64_t dst_stride_sample, const float eps, |
| 239 | const sycl::nd_item<3>& item_ct1, float* s_sum, const int block_size) { |
| 240 | |
| 241 | const int row = item_ct1.get_group(2); |
| 242 | const int channel = item_ct1.get_group(1); |
| 243 | const int sample = item_ct1.get_group(0); |
| 244 | const int tid = item_ct1.get_local_id(2); |
| 245 | |
| 246 | x += sample*src_stride_sample + channel*src_stride_channel + row*src_stride_row; |
| 247 | dst += sample*dst_stride_sample + channel*dst_stride_channel + row*dst_stride_row; |
| 248 | |
| 249 | float tmp = 0.0f; // partial sum for thread in warp |
| 250 | |
| 251 | for (int col = tid; col < ncols; col += block_size) { |
| 252 | const float xi = x[col * src_stride_col]; |
| 253 | tmp += xi * xi; |
| 254 | } |
| 255 | |
| 256 | tmp = block_reduce<block_reduce_method::SUM, warp_size>(tmp, s_sum, block_size); |
| 257 | const float scale = sycl::rsqrt(sycl::fmax(tmp, eps * eps)); |
| 258 | |
| 259 | for (int col = tid; col < ncols; col += block_size) { |
| 260 | dst[col * dst_stride_col] = scale * x[col * src_stride_col]; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | static void norm_f32_sycl(const float * x, float * dst, const int ncols, const int nrows, const int nchannels, const int nsamples, |
| 265 | const int64_t src_stride_col, const int64_t src_stride_row, const int64_t src_stride_channel, const int64_t src_stride_sample, |
| 266 | const int64_t dst_stride_col, const int64_t dst_stride_row, const int64_t dst_stride_channel, const int64_t dst_stride_sample, |
| 267 | const float eps, queue_ptr stream, int device) { |
| 268 | |
| 269 | const sycl::range<3> global_dims(nsamples, nchannels, nrows); |
| 270 | if (ncols < 1024) { |
| 271 | const sycl::range<3> block_dims(1, 1, WARP_SIZE); |
| 272 | stream->submit([&](sycl::handler& cgh) { |
| 273 | cgh.parallel_for( |
| 274 | sycl::nd_range<3>(global_dims * block_dims, block_dims), |
| 275 | [=](sycl::nd_item<3> item_ct1) |
| 276 | [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 277 | norm_f32(x, dst, ncols, |
| 278 | src_stride_col, src_stride_row, src_stride_channel, src_stride_sample, |
| 279 | dst_stride_col, dst_stride_row, dst_stride_channel, dst_stride_sample, |
| 280 | eps, item_ct1, nullptr, WARP_SIZE); |
| 281 | }); |
| 282 | }); |
| 283 | } |
| 284 | else { |
| 285 | const int work_group_size = ggml_sycl_info().max_work_group_sizes[device]; |
| 286 | assert(work_group_size % (WARP_SIZE * WARP_SIZE) == 0); |
| 287 | const sycl::range<3> block_dims(1, 1, work_group_size); |
| 288 | /* |
| 289 | DPCT1049:17: The work-group size passed to the SYCL kernel may exceed |
| 290 | the limit. To get the device limit, query |
| 291 | info::device::max_work_group_size. Adjust the work-group size if needed. |
| 292 | */ |
| 293 | stream->submit([&](sycl::handler& cgh) { |
| 294 | sycl::local_accessor<sycl::float2, 1> s_sum_acc_ct1( |
| 295 | sycl::range<1>(work_group_size / WARP_SIZE), cgh); |
| 296 | cgh.parallel_for( |
| 297 | sycl::nd_range<3>(global_dims * block_dims, block_dims), |
| 298 | [=](sycl::nd_item<3> item_ct1) |
| 299 | [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 300 | norm_f32(x, dst, ncols, |
| 301 | src_stride_col, src_stride_row, src_stride_channel, src_stride_sample, |
| 302 | dst_stride_col, dst_stride_row, dst_stride_channel, dst_stride_sample, |
| 303 | eps, item_ct1, get_pointer(s_sum_acc_ct1), work_group_size); |
| 304 | }); |
| 305 | }); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | static void group_norm_f32_sycl(const float* x, float* dst, |
| 310 | const int num_groups, const float eps, const int group_size, |
| 311 | const int ne_elements, queue_ptr stream, int device) { |
| 312 | if (group_size < 1024) { |
| 313 | const sycl::range<3> block_dims(1, 1, WARP_SIZE); |
| 314 | stream->submit([&](sycl::handler& cgh) { |
| 315 | const float eps_ct4 = eps; |
| 316 | cgh.parallel_for( |
| 317 | sycl::nd_range<3>(sycl::range<3>(1, 1, num_groups) * block_dims, |
| 318 | block_dims), |
| 319 | [=](sycl::nd_item<3> item_ct1) |
| 320 | [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 321 | group_norm_f32( |
| 322 | x, dst, group_size, ne_elements, eps_ct4, item_ct1, |
| 323 | nullptr, WARP_SIZE); |
| 324 | }); |
| 325 | }); |
| 326 | } |
| 327 | else { |
| 328 | const int work_group_size = ggml_sycl_info().max_work_group_sizes[device]; |
| 329 | assert(work_group_size % (WARP_SIZE * WARP_SIZE) == 0); |
| 330 | const sycl::range<3> block_dims(1, 1, work_group_size); |
| 331 | /* |
| 332 | DPCT1049:18: The work-group size passed to the SYCL kernel may exceed |
| 333 | the limit. To get the device limit, query |
| 334 | info::device::max_work_group_size. Adjust the work-group size if needed. |
| 335 | */ |
| 336 | |
| 337 | stream->submit([&](sycl::handler& cgh) { |
| 338 | sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(work_group_size / WARP_SIZE), |
| 339 | cgh); |
| 340 | |
| 341 | const float eps_ct4 = eps; |
| 342 | |
| 343 | cgh.parallel_for( |
| 344 | sycl::nd_range<3>(sycl::range<3>(1, 1, num_groups) * block_dims, |
| 345 | block_dims), |
| 346 | [=](sycl::nd_item<3> item_ct1) |
| 347 | [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 348 | group_norm_f32(x, dst, group_size, ne_elements, |
| 349 | eps_ct4, item_ct1, |
| 350 | get_pointer(s_sum_acc_ct1), work_group_size); |
| 351 | }); |
| 352 | }); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | static void rms_norm_f32_sycl(const float* x, float* dst, const int ncols, const int nrows, const int nchannels, const int nsamples, |
| 357 | const int64_t src_stride_col, const int64_t src_stride_row, const int64_t src_stride_channel, const int64_t src_stride_sample, |
| 358 | const int64_t dst_stride_col, const int64_t dst_stride_row, const int64_t dst_stride_channel, const int64_t dst_stride_sample, |
| 359 | const float eps, queue_ptr stream, int device) { |
| 360 | // printf("%s ncols=%d, nrows=%d, WARP_SIZE=%d\n", __func__, ncols, nrows, WARP_SIZE); |
| 361 | |
| 362 | const sycl::range<3> global_dims(nsamples, nchannels, nrows); |
| 363 | if (ncols < 1024) { |
| 364 | const sycl::range<3> block_dims(1, 1, WARP_SIZE); |
| 365 | stream->submit([&](sycl::handler& cgh) { |
| 366 | cgh.parallel_for( |
| 367 | sycl::nd_range<3>(global_dims * block_dims, block_dims), |
| 368 | [=](sycl::nd_item<3> item_ct1) |
| 369 | [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 370 | rms_norm_f32(x, dst, ncols, |
| 371 | src_stride_col, src_stride_row, src_stride_channel, src_stride_sample, |
| 372 | dst_stride_col, dst_stride_row, dst_stride_channel, dst_stride_sample, |
| 373 | eps, item_ct1, nullptr, WARP_SIZE); |
| 374 | }); |
| 375 | }); |
| 376 | } |
| 377 | else { |
| 378 | const int work_group_size = ggml_sycl_info().max_work_group_sizes[device]; |
| 379 | assert(work_group_size % (WARP_SIZE * WARP_SIZE) == 0); |
| 380 | const sycl::range<3> block_dims(1, 1, work_group_size); |
| 381 | /* |
| 382 | DPCT1049:19: The work-group size passed to the SYCL kernel may exceed |
| 383 | the limit. To get the device limit, query |
| 384 | info::device::max_work_group_size. Adjust the work-group size if needed. |
| 385 | */ |
| 386 | stream->submit([&](sycl::handler& cgh) { |
| 387 | sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(work_group_size / WARP_SIZE), |
| 388 | cgh); |
| 389 | cgh.parallel_for( |
| 390 | sycl::nd_range<3>(global_dims * block_dims, block_dims), |
| 391 | [=](sycl::nd_item<3> item_ct1) |
| 392 | [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 393 | rms_norm_f32(x, dst, ncols, |
| 394 | src_stride_col, src_stride_row, src_stride_channel, src_stride_sample, |
| 395 | dst_stride_col, dst_stride_row, dst_stride_channel, dst_stride_sample, |
| 396 | eps, item_ct1, get_pointer(s_sum_acc_ct1), work_group_size); |
| 397 | }); |
| 398 | }); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | static void rms_norm_scale_f32_sycl(const float* x, float* dst, const int ncols, const int nrows, |
| 403 | const int nchannels, const int nsamples, |
| 404 | const int64_t src_stride_col, const int64_t src_stride_row, const int64_t src_stride_channel, const int64_t src_stride_sample, |
| 405 | const int64_t dst_stride_col, const int64_t dst_stride_row, const int64_t dst_stride_channel, const int64_t dst_stride_sample, |
| 406 | const float eps, const float scale_mul, queue_ptr stream, int device) { |
| 407 | const sycl::range<3> global_dims(nsamples, nchannels, nrows); |
| 408 | if (ncols < 1024) { |
| 409 | const sycl::range<3> block_dims(1, 1, WARP_SIZE); |
| 410 | stream->submit([&](sycl::handler& cgh) { |
| 411 | cgh.parallel_for( |
| 412 | sycl::nd_range<3>(global_dims * block_dims, block_dims), |
| 413 | [=](sycl::nd_item<3> item_ct1) |
| 414 | [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 415 | rms_norm_f32(x, dst, ncols, |
| 416 | src_stride_col, src_stride_row, src_stride_channel, src_stride_sample, |
| 417 | dst_stride_col, dst_stride_row, dst_stride_channel, dst_stride_sample, |
| 418 | eps, item_ct1, nullptr, WARP_SIZE, |
| 419 | nullptr, 0, 0, 0, 0, 0, 0, |
| 420 | nullptr, 0, 0, 0, 0, 0, 0, |
| 421 | scale_mul); |
| 422 | }); |
| 423 | }); |
| 424 | } |
| 425 | else { |
| 426 | const int work_group_size = ggml_sycl_info().max_work_group_sizes[device]; |
| 427 | assert(work_group_size % (WARP_SIZE * WARP_SIZE) == 0); |
| 428 | const sycl::range<3> block_dims(1, 1, work_group_size); |
| 429 | stream->submit([&](sycl::handler& cgh) { |
| 430 | sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(work_group_size / WARP_SIZE), |
| 431 | cgh); |
| 432 | cgh.parallel_for( |
| 433 | sycl::nd_range<3>(global_dims * block_dims, block_dims), |
| 434 | [=](sycl::nd_item<3> item_ct1) |
| 435 | [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 436 | rms_norm_f32(x, dst, ncols, |
| 437 | src_stride_col, src_stride_row, src_stride_channel, src_stride_sample, |
| 438 | dst_stride_col, dst_stride_row, dst_stride_channel, dst_stride_sample, |
| 439 | eps, item_ct1, get_pointer(s_sum_acc_ct1), work_group_size, |
| 440 | nullptr, 0, 0, 0, 0, 0, 0, |
| 441 | nullptr, 0, 0, 0, 0, 0, 0, |
| 442 | scale_mul); |
| 443 | }); |
| 444 | }); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | static void rms_norm_mul_f32_sycl(const float* x, const float* mul, float* dst, const int ncols, const int nrows, |
| 449 | const int nchannels, const int nsamples, |
| 450 | const int64_t src_stride_col, const int64_t src_stride_row, const int64_t src_stride_channel, const int64_t src_stride_sample, |
| 451 | const int64_t dst_stride_col, const int64_t dst_stride_row, const int64_t dst_stride_channel, const int64_t dst_stride_sample, |
| 452 | const int64_t mul_stride_row, const int64_t mul_stride_channel, const int64_t mul_stride_sample, |
| 453 | const int mul_nrows, const int mul_nchannels, const int mul_nsamples, |
| 454 | const float eps, queue_ptr stream, int device) { |
| 455 | const sycl::range<3> global_dims(nsamples, nchannels, nrows); |
| 456 | if (ncols < 1024) { |
| 457 | const sycl::range<3> block_dims(1, 1, WARP_SIZE); |
| 458 | stream->submit([&](sycl::handler& cgh) { |
| 459 | cgh.parallel_for( |
| 460 | sycl::nd_range<3>(global_dims * block_dims, block_dims), |
| 461 | [=](sycl::nd_item<3> item_ct1) |
| 462 | [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 463 | rms_norm_f32<true>(x, dst, ncols, |
| 464 | src_stride_col, src_stride_row, src_stride_channel, src_stride_sample, |
| 465 | dst_stride_col, dst_stride_row, dst_stride_channel, dst_stride_sample, |
| 466 | eps, item_ct1, nullptr, WARP_SIZE, |
| 467 | mul, mul_stride_row, mul_stride_channel, mul_stride_sample, mul_nrows, mul_nchannels, mul_nsamples); |
| 468 | }); |
| 469 | }); |
| 470 | } |
| 471 | else { |
| 472 | const int work_group_size = ggml_sycl_info().max_work_group_sizes[device]; |
| 473 | assert(work_group_size % (WARP_SIZE * WARP_SIZE) == 0); |
| 474 | const sycl::range<3> block_dims(1, 1, work_group_size); |
| 475 | stream->submit([&](sycl::handler& cgh) { |
| 476 | sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(work_group_size / WARP_SIZE), cgh); |
| 477 | cgh.parallel_for( |
| 478 | sycl::nd_range<3>(global_dims * block_dims, block_dims), |
| 479 | [=](sycl::nd_item<3> item_ct1) |
| 480 | [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 481 | rms_norm_f32<true>(x, dst, ncols, |
| 482 | src_stride_col, src_stride_row, src_stride_channel, src_stride_sample, |
| 483 | dst_stride_col, dst_stride_row, dst_stride_channel, dst_stride_sample, |
| 484 | eps, item_ct1, get_pointer(s_sum_acc_ct1), work_group_size, |
| 485 | mul, mul_stride_row, mul_stride_channel, mul_stride_sample, mul_nrows, mul_nchannels, mul_nsamples); |
| 486 | }); |
| 487 | }); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | static void rms_norm_mul_add_f32_sycl(const float* x, const float* mul, const float* add, float* dst, |
| 492 | const int ncols, const int nrows, const int nchannels, const int nsamples, |
| 493 | const int64_t src_stride_col, const int64_t src_stride_row, const int64_t src_stride_channel, const int64_t src_stride_sample, |
| 494 | const int64_t dst_stride_col, const int64_t dst_stride_row, const int64_t dst_stride_channel, const int64_t dst_stride_sample, |
| 495 | const int64_t mul_stride_row, const int64_t mul_stride_channel, const int64_t mul_stride_sample, |
| 496 | const int mul_nrows, const int mul_nchannels, const int mul_nsamples, |
| 497 | const int64_t add_stride_row, const int64_t add_stride_channel, const int64_t add_stride_sample, |
| 498 | const int add_nrows, const int add_nchannels, const int add_nsamples, |
| 499 | const float eps, queue_ptr stream, int device) { |
| 500 | const sycl::range<3> global_dims(nsamples, nchannels, nrows); |
| 501 | if (ncols < 1024) { |
| 502 | const sycl::range<3> block_dims(1, 1, WARP_SIZE); |
| 503 | stream->submit([&](sycl::handler& cgh) { |
| 504 | cgh.parallel_for( |
| 505 | sycl::nd_range<3>(global_dims * block_dims, block_dims), |
| 506 | [=](sycl::nd_item<3> item_ct1) |
| 507 | [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 508 | rms_norm_f32<true, true>(x, dst, ncols, |
| 509 | src_stride_col, src_stride_row, src_stride_channel, src_stride_sample, |
| 510 | dst_stride_col, dst_stride_row, dst_stride_channel, dst_stride_sample, |
| 511 | eps, item_ct1, nullptr, WARP_SIZE, |
| 512 | mul, mul_stride_row, mul_stride_channel, mul_stride_sample, mul_nrows, mul_nchannels, mul_nsamples, |
| 513 | add, add_stride_row, add_stride_channel, add_stride_sample, add_nrows, add_nchannels, add_nsamples); |
| 514 | }); |
| 515 | }); |
| 516 | } |
| 517 | else { |
| 518 | const int work_group_size = ggml_sycl_info().max_work_group_sizes[device]; |
| 519 | assert(work_group_size % (WARP_SIZE * WARP_SIZE) == 0); |
| 520 | const sycl::range<3> block_dims(1, 1, work_group_size); |
| 521 | stream->submit([&](sycl::handler& cgh) { |
| 522 | sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(work_group_size / WARP_SIZE), cgh); |
| 523 | cgh.parallel_for( |
| 524 | sycl::nd_range<3>(global_dims * block_dims, block_dims), |
| 525 | [=](sycl::nd_item<3> item_ct1) |
| 526 | [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 527 | rms_norm_f32<true, true>(x, dst, ncols, |
| 528 | src_stride_col, src_stride_row, src_stride_channel, src_stride_sample, |
| 529 | dst_stride_col, dst_stride_row, dst_stride_channel, dst_stride_sample, |
| 530 | eps, item_ct1, get_pointer(s_sum_acc_ct1), work_group_size, |
| 531 | mul, mul_stride_row, mul_stride_channel, mul_stride_sample, mul_nrows, mul_nchannels, mul_nsamples, |
| 532 | add, add_stride_row, add_stride_channel, add_stride_sample, add_nrows, add_nchannels, add_nsamples); |
| 533 | }); |
| 534 | }); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | template<int warp_size> |
| 539 | static void l2_norm_f32_sycl(const float * x, |
| 540 | float * dst, |
| 541 | const int ncols, |
| 542 | const int nrows, |
| 543 | const int nchannels, |
| 544 | const int nsamples, |
| 545 | const int64_t src_stride_col, |
| 546 | const int64_t src_stride_row, |
| 547 | const int64_t src_stride_channel, |
| 548 | const int64_t src_stride_sample, |
| 549 | const int64_t dst_stride_col, |
| 550 | const int64_t dst_stride_row, |
| 551 | const int64_t dst_stride_channel, |
| 552 | const int64_t dst_stride_sample, |
| 553 | const float eps, |
| 554 | queue_ptr stream, |
| 555 | int device) { |
| 556 | const dpct::dim3 blocks_num(nrows, nchannels, nsamples); |
| 557 | |
| 558 | if (ncols < 1024) { |
| 559 | const dpct::dim3 block_dims(warp_size, 1, 1); |
| 560 | stream->submit([&](sycl::handler& cgh) { |
| 561 | cgh.parallel_for( |
| 562 | sycl::nd_range<3>(blocks_num * block_dims, |
| 563 | block_dims), |
| 564 | [=](sycl::nd_item<3> item_ct1) |
| 565 | [[sycl::reqd_sub_group_size(warp_size)]] { |
| 566 | l2_norm_f32<warp_size>(x, dst, ncols, |
| 567 | src_stride_col, src_stride_row, src_stride_channel, src_stride_sample, |
| 568 | dst_stride_col, dst_stride_row, dst_stride_channel, dst_stride_sample, |
| 569 | eps, item_ct1, |
| 570 | nullptr, warp_size); |
| 571 | }); |
| 572 | }); |
| 573 | } |
| 574 | else { |
| 575 | const int work_group_size = ggml_sycl_info().max_work_group_sizes[device]; |
| 576 | assert(work_group_size % (warp_size * warp_size) == 0); |
| 577 | const sycl::range<3> block_dims(1, 1, work_group_size); |
| 578 | int lsm_size = block_dims[2] > warp_size ? work_group_size / warp_size * sizeof(float): 0; |
| 579 | stream->submit([&](sycl::handler& cgh) { |
| 580 | sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(lsm_size), |
| 581 | cgh); |
| 582 | |
| 583 | cgh.parallel_for( |
| 584 | sycl::nd_range<3>(blocks_num * block_dims, |
| 585 | block_dims), |
| 586 | [=](sycl::nd_item<3> item_ct1) |
| 587 | [[sycl::reqd_sub_group_size(warp_size)]] { |
| 588 | l2_norm_f32<warp_size>(x, dst, ncols, |
| 589 | src_stride_col, src_stride_row, src_stride_channel, src_stride_sample, |
| 590 | dst_stride_col, dst_stride_row, dst_stride_channel, dst_stride_sample, |
| 591 | eps, item_ct1, get_pointer(s_sum_acc_ct1), work_group_size); |
| 592 | }); |
| 593 | }); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | // Batched L2 norm: N independent same-shape F32 tensors in one launch; the tensor |
| 598 | // index is folded into grid dim0 and each row's reduction is identical to the |
| 599 | // single-tensor kernel, so the result is bit-exact. |
| 600 | struct l2_batch_ptrs { |
| 601 | const float * src[GGML_SYCL_L2_BATCH_MAX]; |
| 602 | float * dst[GGML_SYCL_L2_BATCH_MAX]; |
| 603 | }; |
| 604 | |
| 605 | // One stride set shared by the whole batch: the caller only groups tensors whose nb[] |
| 606 | // all match, so per-tensor state stays two pointers. |
| 607 | struct l2_batch_strides { |
| 608 | int ne1, ne2; |
| 609 | int64_t ss0, ss1, ss2, ss3; |
| 610 | int64_t ds0, ds1, ds2, ds3; |
| 611 | }; |
| 612 | |
| 613 | template <int warp_size> |
| 614 | static void l2_norm_f32_batch(l2_batch_ptrs p, l2_batch_strides st, const int ncols, const float eps, |
| 615 | const sycl::nd_item<3> & item_ct1) { |
| 616 | const int t = item_ct1.get_group(0); // tensor index |
| 617 | const int r = item_ct1.get_group(2); // flattened row over ne1*ne2*ne3 |
| 618 | const int tid = item_ct1.get_local_id(2); |
| 619 | |
| 620 | const int i1 = r % st.ne1; |
| 621 | const int i2 = (r / st.ne1) % st.ne2; |
| 622 | const int i3 = r / (st.ne1 * st.ne2); |
| 623 | |
| 624 | const float * x = p.src[t] + i3 * st.ss3 + i2 * st.ss2 + i1 * st.ss1; |
| 625 | float * dst = p.dst[t] + i3 * st.ds3 + i2 * st.ds2 + i1 * st.ds1; |
| 626 | |
| 627 | float tmp = 0.0f; |
| 628 | for (int col = tid; col < ncols; col += warp_size) { |
| 629 | const float xi = x[col * st.ss0]; |
| 630 | tmp += xi * xi; |
| 631 | } |
| 632 | tmp = block_reduce<block_reduce_method::SUM, warp_size>(tmp, (float *) nullptr, warp_size); |
| 633 | const float scale = sycl::rsqrt(sycl::fmax(tmp, eps * eps)); |
| 634 | for (int col = tid; col < ncols; col += warp_size) { |
| 635 | dst[col * st.ds0] = scale * x[col * st.ss0]; |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | template <int warp_size> |
| 640 | static void l2_norm_f32_batch_sycl(l2_batch_ptrs p, l2_batch_strides st, const int n_tensors, |
| 641 | const int ncols, const int nrows_total, const float eps, |
| 642 | queue_ptr stream) { |
| 643 | const dpct::dim3 blocks_num(nrows_total, 1, n_tensors); |
| 644 | const dpct::dim3 block_dims(warp_size, 1, 1); |
| 645 | stream->submit([&](sycl::handler & cgh) { |
| 646 | cgh.parallel_for(sycl::nd_range<3>(blocks_num * block_dims, block_dims), |
| 647 | [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(warp_size)]] { |
| 648 | l2_norm_f32_batch<warp_size>(p, st, ncols, eps, item_ct1); |
| 649 | }); |
| 650 | }); |
| 651 | } |
| 652 | |
| 653 | void ggml_sycl_op_norm(ggml_backend_sycl_context& ctx, ggml_tensor* dst) { |
| 654 | const ggml_tensor * src0 = dst->src[0]; |
| 655 | |
| 656 | GGML_ASSERT(dst->src[0]->type == GGML_TYPE_F32); |
| 657 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 658 | |
| 659 | GGML_TENSOR_UNARY_OP_LOCALS |
| 660 | dpct::queue_ptr main_stream = ctx.stream(); |
| 661 | SYCL_CHECK(ggml_sycl_set_device(ctx.device)); |
| 662 | const float * src0_dd = static_cast<const float *>(dst->src[0]->data); |
| 663 | float * dst_dd = static_cast<float *>(dst->data); |
| 664 | |
| 665 | float eps; |
| 666 | memcpy(&eps, dst->op_params, sizeof(float)); |
| 667 | GGML_ASSERT(eps >= 0.0f); |
| 668 | const size_t ts0 = ggml_type_size(src0->type); |
| 669 | const size_t tdst = ggml_type_size(dst->type); |
| 670 | GGML_ASSERT(nb00 % ts0 == 0 && nb01 % ts0 == 0 && nb02 % ts0 == 0 && nb03 % ts0 == 0); |
| 671 | GGML_ASSERT(nb0 % tdst == 0 && nb1 % tdst == 0 && nb2 % tdst == 0 && nb3 % tdst == 0); |
| 672 | const int64_t ss0 = nb00 / ts0; |
| 673 | const int64_t ss1 = nb01 / ts0; |
| 674 | const int64_t ss2 = nb02 / ts0; |
| 675 | const int64_t ss3 = nb03 / ts0; |
| 676 | const int64_t ds0 = nb0 / tdst; |
| 677 | const int64_t ds1 = nb1 / tdst; |
| 678 | const int64_t ds2 = nb2 / tdst; |
| 679 | const int64_t ds3 = nb3 / tdst; |
| 680 | |
| 681 | norm_f32_sycl(src0_dd, dst_dd, ne00, ne01, ne02, ne03, |
| 682 | ss0, ss1, ss2, ss3, ds0, ds1, ds2, ds3, eps, main_stream, ctx.device); |
| 683 | } |
| 684 | |
| 685 | void ggml_sycl_op_group_norm(ggml_backend_sycl_context& ctx, ggml_tensor* dst) { |
| 686 | |
| 687 | GGML_ASSERT(dst->src[0]->type == GGML_TYPE_F32); |
| 688 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 689 | |
| 690 | int num_groups = dst->op_params[0]; |
| 691 | dpct::queue_ptr main_stream = ctx.stream(); |
| 692 | SYCL_CHECK(ggml_sycl_set_device(ctx.device)); |
| 693 | |
| 694 | const float * src0_dd = static_cast<const float *>(dst->src[0]->data); |
| 695 | float * dst_dd = static_cast<float *>(dst->data); |
| 696 | |
| 697 | float eps; |
| 698 | memcpy(&eps, dst->op_params + 1, sizeof(float)); |
| 699 | |
| 700 | int group_size = dst->src[0]->ne[0] * dst->src[0]->ne[1] * ((dst->src[0]->ne[2] + num_groups - 1) / num_groups); |
| 701 | group_norm_f32_sycl(src0_dd, dst_dd, num_groups, eps, group_size, dst->src[0]->ne[0] * dst->src[0]->ne[1] * dst->src[0]->ne[2], main_stream, ctx.device); |
| 702 | } |
| 703 | |
| 704 | void ggml_sycl_op_rms_norm(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 705 | |
| 706 | const ggml_tensor * src0 = dst->src[0]; |
| 707 | GGML_ASSERT(dst->src[0]->type == GGML_TYPE_F32); |
| 708 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 709 | |
| 710 | dpct::queue_ptr main_stream = ctx.stream(); |
| 711 | SYCL_CHECK(ggml_sycl_set_device(ctx.device)); |
| 712 | |
| 713 | const float * src0_dd = static_cast<const float *>(dst->src[0]->data); |
| 714 | float * dst_dd = static_cast<float *>(dst->data); |
| 715 | |
| 716 | float eps; |
| 717 | memcpy(&eps, dst->op_params, sizeof(float)); |
| 718 | |
| 719 | GGML_TENSOR_UNARY_OP_LOCALS |
| 720 | const size_t ts0 = ggml_type_size(src0->type); |
| 721 | const size_t tdst = ggml_type_size(dst->type); |
| 722 | GGML_ASSERT(nb00 % ts0 == 0 && nb01 % ts0 == 0 && nb02 % ts0 == 0 && nb03 % ts0 == 0); |
| 723 | GGML_ASSERT(nb0 % tdst == 0 && nb1 % tdst == 0 && nb2 % tdst == 0 && nb3 % tdst == 0); |
| 724 | const int64_t ss0 = nb00 / ts0; |
| 725 | const int64_t ss1 = nb01 / ts0; |
| 726 | const int64_t ss2 = nb02 / ts0; |
| 727 | const int64_t ss3 = nb03 / ts0; |
| 728 | const int64_t ds0 = nb0 / tdst; |
| 729 | const int64_t ds1 = nb1 / tdst; |
| 730 | const int64_t ds2 = nb2 / tdst; |
| 731 | const int64_t ds3 = nb3 / tdst; |
| 732 | rms_norm_f32_sycl(src0_dd, dst_dd, ne00, ne01, ne02, ne03, |
| 733 | ss0, ss1, ss2, ss3, ds0, ds1, ds2, ds3, eps, main_stream, ctx.device); |
| 734 | } |
| 735 | |
| 736 | // Fused rms_norm + scale (the qwen35 GDN l2-norm pair build_gdn_l2_norm emits): |
| 737 | // the scale factor is a host scalar in the GGML_OP_SCALE node's op_params, so |
| 738 | // unlike the mul variants there is no second device tensor to wire up. |
| 739 | void ggml_sycl_op_rms_norm_scale_fused(ggml_backend_sycl_context & ctx, ggml_tensor * dst, |
| 740 | ggml_tensor * scale_tensor) { |
| 741 | const ggml_tensor * src0 = dst->src[0]; |
| 742 | GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 743 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 744 | GGML_ASSERT(scale_tensor->type == GGML_TYPE_F32); |
| 745 | |
| 746 | dpct::queue_ptr main_stream = ctx.stream(); |
| 747 | SYCL_CHECK(ggml_sycl_set_device(ctx.device)); |
| 748 | |
| 749 | const float * src0_dd = static_cast<const float *>(src0->data); |
| 750 | float * dst_dd = static_cast<float *>(scale_tensor->data); |
| 751 | |
| 752 | float eps; |
| 753 | memcpy(&eps, dst->op_params, sizeof(float)); |
| 754 | float scale_mul; |
| 755 | memcpy(&scale_mul, scale_tensor->op_params, sizeof(float)); |
| 756 | GGML_ASSERT(scale_mul >= 0.0f); |
| 757 | |
| 758 | GGML_TENSOR_UNARY_OP_LOCALS |
| 759 | const size_t ts0 = ggml_type_size(src0->type); |
| 760 | const size_t tdst = ggml_type_size(scale_tensor->type); |
| 761 | GGML_ASSERT(nb00 % ts0 == 0 && nb01 % ts0 == 0 && nb02 % ts0 == 0 && nb03 % ts0 == 0); |
| 762 | GGML_ASSERT(scale_tensor->nb[0] % tdst == 0 && scale_tensor->nb[1] % tdst == 0 && |
| 763 | scale_tensor->nb[2] % tdst == 0 && scale_tensor->nb[3] % tdst == 0); |
| 764 | const int64_t ss0 = nb00 / ts0; |
| 765 | const int64_t ss1 = nb01 / ts0; |
| 766 | const int64_t ss2 = nb02 / ts0; |
| 767 | const int64_t ss3 = nb03 / ts0; |
| 768 | const int64_t ds0 = scale_tensor->nb[0] / tdst; |
| 769 | const int64_t ds1 = scale_tensor->nb[1] / tdst; |
| 770 | const int64_t ds2 = scale_tensor->nb[2] / tdst; |
| 771 | const int64_t ds3 = scale_tensor->nb[3] / tdst; |
| 772 | rms_norm_scale_f32_sycl(src0_dd, dst_dd, ne00, ne01, ne02, ne03, |
| 773 | ss0, ss1, ss2, ss3, ds0, ds1, ds2, ds3, eps, scale_mul, main_stream, ctx.device); |
| 774 | } |
| 775 | |
| 776 | void ggml_sycl_op_rms_norm_fused(ggml_backend_sycl_context & ctx, ggml_tensor * dst, ggml_tensor * mul_tensor) { |
| 777 | const ggml_tensor * rms_norm_src = dst->src[0]; |
| 778 | float eps = 0.0f; |
| 779 | memcpy(&eps, dst->op_params, sizeof(float)); |
| 780 | |
| 781 | const float * src0_dd = static_cast<const float *>(rms_norm_src->data); |
| 782 | const float * mul_dd = nullptr; |
| 783 | const ggml_tensor * mul_src = nullptr; |
| 784 | if (mul_tensor->src[0] == dst) { |
| 785 | mul_dd = static_cast<const float *>(mul_tensor->src[1]->data); |
| 786 | mul_src = mul_tensor->src[1]; |
| 787 | } else if (mul_tensor->src[1] == dst) { |
| 788 | mul_dd = static_cast<const float *>(mul_tensor->src[0]->data); |
| 789 | mul_src = mul_tensor->src[0]; |
| 790 | } else { |
| 791 | GGML_ASSERT(false); |
| 792 | } |
| 793 | float * dst_dd = static_cast<float *>(mul_tensor->data); |
| 794 | |
| 795 | dpct::queue_ptr main_stream = ctx.stream(); |
| 796 | SYCL_CHECK(ggml_sycl_set_device(ctx.device)); |
| 797 | |
| 798 | GGML_ASSERT(rms_norm_src->type == GGML_TYPE_F32); |
| 799 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 800 | GGML_ASSERT(mul_tensor->type == GGML_TYPE_F32); |
| 801 | GGML_ASSERT(eps >= 0.0f); |
| 802 | |
| 803 | const int64_t ne00 = rms_norm_src->ne[0]; |
| 804 | const int64_t ne01 = rms_norm_src->ne[1]; |
| 805 | const int64_t ne02 = rms_norm_src->ne[2]; |
| 806 | const int64_t ne03 = rms_norm_src->ne[3]; |
| 807 | |
| 808 | const size_t ts0 = ggml_type_size(rms_norm_src->type); |
| 809 | GGML_ASSERT(rms_norm_src->nb[0] == ts0); |
| 810 | const int64_t s00 = rms_norm_src->nb[0] / ts0; |
| 811 | const int64_t s01 = rms_norm_src->nb[1] / ts0; |
| 812 | const int64_t s02 = rms_norm_src->nb[2] / ts0; |
| 813 | const int64_t s03 = rms_norm_src->nb[3] / ts0; |
| 814 | |
| 815 | const size_t tdst = ggml_type_size(mul_tensor->type); |
| 816 | GGML_ASSERT(mul_tensor->nb[0] == tdst); |
| 817 | const int64_t d00 = mul_tensor->nb[0] / tdst; |
| 818 | const int64_t d01 = mul_tensor->nb[1] / tdst; |
| 819 | const int64_t d02 = mul_tensor->nb[2] / tdst; |
| 820 | const int64_t d03 = mul_tensor->nb[3] / tdst; |
| 821 | |
| 822 | const size_t ts_mul = ggml_type_size(mul_src->type); |
| 823 | GGML_ASSERT(mul_src->nb[0] == ts_mul); |
| 824 | const int64_t mul_s01 = mul_src->nb[1] / ts_mul; |
| 825 | const int64_t mul_s02 = mul_src->nb[2] / ts_mul; |
| 826 | const int64_t mul_s03 = mul_src->nb[3] / ts_mul; |
| 827 | const int mul_nrows = mul_src->ne[1]; |
| 828 | const int mul_nchannels = mul_src->ne[2]; |
| 829 | const int mul_nsamples = mul_src->ne[3]; |
| 830 | |
| 831 | rms_norm_mul_f32_sycl(src0_dd, mul_dd, dst_dd, ne00, ne01, ne02, ne03, |
| 832 | s00, s01, s02, s03, d00, d01, d02, d03, |
| 833 | mul_s01, mul_s02, mul_s03, mul_nrows, mul_nchannels, mul_nsamples, eps, main_stream, ctx.device); |
| 834 | } |
| 835 | |
| 836 | void ggml_sycl_op_rms_norm_fused_add(ggml_backend_sycl_context & ctx, ggml_tensor * dst, |
| 837 | ggml_tensor * mul_tensor, ggml_tensor * add_tensor) { |
| 838 | const ggml_tensor * rms_norm_src = dst->src[0]; |
| 839 | float eps = 0.0f; |
| 840 | memcpy(&eps, dst->op_params, sizeof(float)); |
| 841 | |
| 842 | const float * src0_dd = static_cast<const float *>(rms_norm_src->data); |
| 843 | const float * mul_dd = nullptr; |
| 844 | const ggml_tensor * mul_src = nullptr; |
| 845 | if (mul_tensor->src[0] == dst) { |
| 846 | mul_dd = static_cast<const float *>(mul_tensor->src[1]->data); |
| 847 | mul_src = mul_tensor->src[1]; |
| 848 | } else if (mul_tensor->src[1] == dst) { |
| 849 | mul_dd = static_cast<const float *>(mul_tensor->src[0]->data); |
| 850 | mul_src = mul_tensor->src[0]; |
| 851 | } else { |
| 852 | GGML_ASSERT(false); |
| 853 | } |
| 854 | |
| 855 | const float * add_dd = nullptr; |
| 856 | const ggml_tensor * add_src = nullptr; |
| 857 | if (add_tensor->src[0] == mul_tensor) { |
| 858 | add_dd = static_cast<const float *>(add_tensor->src[1]->data); |
| 859 | add_src = add_tensor->src[1]; |
| 860 | } else if (add_tensor->src[1] == mul_tensor) { |
| 861 | add_dd = static_cast<const float *>(add_tensor->src[0]->data); |
| 862 | add_src = add_tensor->src[0]; |
| 863 | } else { |
| 864 | GGML_ASSERT(false); |
| 865 | } |
| 866 | |
| 867 | float * dst_dd = static_cast<float *>(add_tensor->data); |
| 868 | |
| 869 | dpct::queue_ptr main_stream = ctx.stream(); |
| 870 | SYCL_CHECK(ggml_sycl_set_device(ctx.device)); |
| 871 | |
| 872 | GGML_ASSERT(rms_norm_src->type == GGML_TYPE_F32); |
| 873 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 874 | GGML_ASSERT(mul_tensor->type == GGML_TYPE_F32); |
| 875 | GGML_ASSERT(add_tensor->type == GGML_TYPE_F32); |
| 876 | GGML_ASSERT(eps >= 0.0f); |
| 877 | |
| 878 | const int64_t ne00 = rms_norm_src->ne[0]; |
| 879 | const int64_t ne01 = rms_norm_src->ne[1]; |
| 880 | const int64_t ne02 = rms_norm_src->ne[2]; |
| 881 | const int64_t ne03 = rms_norm_src->ne[3]; |
| 882 | |
| 883 | const size_t ts0 = ggml_type_size(rms_norm_src->type); |
| 884 | GGML_ASSERT(rms_norm_src->nb[0] == ts0); |
| 885 | const int64_t s00 = rms_norm_src->nb[0] / ts0; |
| 886 | const int64_t s01 = rms_norm_src->nb[1] / ts0; |
| 887 | const int64_t s02 = rms_norm_src->nb[2] / ts0; |
| 888 | const int64_t s03 = rms_norm_src->nb[3] / ts0; |
| 889 | |
| 890 | const size_t tdst = ggml_type_size(add_tensor->type); |
| 891 | GGML_ASSERT(add_tensor->nb[0] == tdst); |
| 892 | const int64_t d00 = add_tensor->nb[0] / tdst; |
| 893 | const int64_t d01 = add_tensor->nb[1] / tdst; |
| 894 | const int64_t d02 = add_tensor->nb[2] / tdst; |
| 895 | const int64_t d03 = add_tensor->nb[3] / tdst; |
| 896 | |
| 897 | const size_t ts_mul = ggml_type_size(mul_src->type); |
| 898 | GGML_ASSERT(mul_src->nb[0] == ts_mul); |
| 899 | const int64_t mul_s01 = mul_src->nb[1] / ts_mul; |
| 900 | const int64_t mul_s02 = mul_src->nb[2] / ts_mul; |
| 901 | const int64_t mul_s03 = mul_src->nb[3] / ts_mul; |
| 902 | const int mul_nrows = mul_src->ne[1]; |
| 903 | const int mul_nchannels = mul_src->ne[2]; |
| 904 | const int mul_nsamples = mul_src->ne[3]; |
| 905 | |
| 906 | const size_t ts_add = ggml_type_size(add_src->type); |
| 907 | GGML_ASSERT(add_src->nb[0] == ts_add); |
| 908 | const int64_t add_s01 = add_src->nb[1] / ts_add; |
| 909 | const int64_t add_s02 = add_src->nb[2] / ts_add; |
| 910 | const int64_t add_s03 = add_src->nb[3] / ts_add; |
| 911 | const int add_nrows = add_src->ne[1]; |
| 912 | const int add_nchannels = add_src->ne[2]; |
| 913 | const int add_nsamples = add_src->ne[3]; |
| 914 | |
| 915 | rms_norm_mul_add_f32_sycl(src0_dd, mul_dd, add_dd, dst_dd, ne00, ne01, ne02, ne03, |
| 916 | s00, s01, s02, s03, d00, d01, d02, d03, |
| 917 | mul_s01, mul_s02, mul_s03, mul_nrows, mul_nchannels, mul_nsamples, |
| 918 | add_s01, add_s02, add_s03, add_nrows, add_nchannels, add_nsamples, eps, main_stream, ctx.device); |
| 919 | } |
| 920 | |
| 921 | void ggml_sycl_op_rms_norm_back(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 922 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/2); |
| 923 | |
| 924 | GGML_ASSERT(dst->src[0]->type == GGML_TYPE_F32); // dz |
| 925 | GGML_ASSERT(dst->src[1]->type == GGML_TYPE_F32); // x |
| 926 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 927 | |
| 928 | float eps = 1e-5f; |
| 929 | std::memcpy(&eps, dst->op_params, sizeof(float)); |
| 930 | if (!(eps > 0.0f) || !std::isfinite(eps)) eps = 1e-5f; |
| 931 | |
| 932 | const float * g_base = static_cast<const float *>(dst->src[0]->data); // dz |
| 933 | const float * x_base = static_cast<const float *>(dst->src[1]->data); // x |
| 934 | float * dx_base = static_cast< float *>(dst->data); |
| 935 | |
| 936 | const int64_t D = dst->ne[0]; |
| 937 | const int64_t n1 = dst->ne[1], n2 = dst->ne[2], n3 = dst->ne[3]; (void) n3; |
| 938 | const int64_t N = ggml_nrows(dst); |
| 939 | if (D == 0 || N == 0) return; |
| 940 | |
| 941 | const ggml_tensor *G = dst->src[0]; |
| 942 | const ggml_tensor *X = dst->src[1]; |
| 943 | const int ts = (int) ggml_type_size(X->type); |
| 944 | GGML_ASSERT((size_t) X->nb[0] == (size_t) ts); |
| 945 | GGML_ASSERT((size_t) G->nb[0] == (size_t) ts); |
| 946 | GGML_ASSERT((size_t) dst->nb[0] == (size_t) ts); |
| 947 | |
| 948 | const int64_t xs1 = X->nb[1] / ts, xs2 = X->nb[2] / ts, xs3 = X->nb[3] / ts; |
| 949 | const int64_t gs1 = G->nb[1] / ts, gs2 = G->nb[2] / ts, gs3 = G->nb[3] / ts; |
| 950 | const int64_t ds1 = dst->nb[1] / ts, ds2 = dst->nb[2] / ts, ds3 = dst->nb[3] / ts; |
| 951 | |
| 952 | dpct::queue_ptr q = ctx.stream(); |
| 953 | |
| 954 | // work-group size: multiple of WARP_SIZE, capped by device and 256, and not larger than D |
| 955 | const int device_max_wg = ggml_sycl_info().max_work_group_sizes[ctx.device]; |
| 956 | auto roundup = [](int v, int m) { return ((v + m - 1) / m) * m; }; |
| 957 | int wg_cap = 256; |
| 958 | if (device_max_wg > 0) wg_cap = std::min(wg_cap, device_max_wg); |
| 959 | int WG = std::max(WARP_SIZE, std::min(roundup((int)std::min<int64_t>(D, wg_cap), WARP_SIZE), wg_cap)); |
| 960 | |
| 961 | // FP32 path: per-thread compensated accumulation + hierarchical reduction |
| 962 | q->submit([&](sycl::handler &cgh) { |
| 963 | const int nwarps_loc = std::max(1, WG / WARP_SIZE); |
| 964 | // store one partial value per warp (xx and xg) for cross-warp reduction |
| 965 | auto l_xx = sycl::local_accessor<sycl::float2, 1>(sycl::range<1>(nwarps_loc), cgh); |
| 966 | auto l_xg = sycl::local_accessor<sycl::float2, 1>(sycl::range<1>(nwarps_loc), cgh); |
| 967 | |
| 968 | cgh.parallel_for( |
| 969 | sycl::nd_range<3>(sycl::range<3>(1, 1, N) * sycl::range<3>(1, 1, WG), |
| 970 | sycl::range<3>(1, 1, WG)), |
| 971 | [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 972 | const int row = item_ct1.get_group(2); |
| 973 | const int tid = item_ct1.get_local_id(2); |
| 974 | |
| 975 | const int64_t i1 = row % n1; |
| 976 | const int64_t i2 = (row / n1) % n2; |
| 977 | const int64_t i3 = row / (n1 * n2); |
| 978 | |
| 979 | const float *__restrict x_row = x_base + i3 * xs3 + i2 * xs2 + i1 * xs1; |
| 980 | const float *__restrict g_row = g_base + i3 * gs3 + i2 * gs2 + i1 * gs1; |
| 981 | float *__restrict d_row = dx_base + i3 * ds3 + i2 * ds2 + i1 * ds1; |
| 982 | |
| 983 | // per-thread accumulation (compensated by default) |
| 984 | float sum_xx = 0.f, sum_xg = 0.f; |
| 985 | #ifndef GGML_SYCL_RMS_BACK_FAST |
| 986 | float c_xx = 0.f, c_xg = 0.f; |
| 987 | #endif |
| 988 | for (int64_t col = tid; col < D; col += WG) { |
| 989 | const float xv = x_row[col]; |
| 990 | const float gv = g_row[col]; |
| 991 | #ifdef GGML_SYCL_RMS_BACK_FAST |
| 992 | sum_xx += xv * xv; |
| 993 | sum_xg += xv * gv; |
| 994 | #else |
| 995 | float y1 = xv * xv - c_xx; |
| 996 | float t1 = sum_xx + y1; |
| 997 | c_xx = (t1 - sum_xx) - y1; |
| 998 | sum_xx = t1; |
| 999 | |
| 1000 | float y2 = xv * gv - c_xg; |
| 1001 | float t2 = sum_xg + y2; |
| 1002 | c_xg = (t2 - sum_xg) - y2; |
| 1003 | sum_xg = t2; |
| 1004 | #endif |
| 1005 | } |
| 1006 | |
| 1007 | // warp-level reduction |
| 1008 | sycl::float2 xx = sycl::float2(sum_xx, |
| 1009 | #ifndef GGML_SYCL_RMS_BACK_FAST |
| 1010 | c_xx |
| 1011 | #else |
| 1012 | 0.f |
| 1013 | #endif |
| 1014 | ); |
| 1015 | sycl::float2 xg = sycl::float2(sum_xg, |
| 1016 | #ifndef GGML_SYCL_RMS_BACK_FAST |
| 1017 | c_xg |
| 1018 | #else |
| 1019 | 0.f |
| 1020 | #endif |
| 1021 | ); |
| 1022 | xx = warp_reduce_sum(xx, item_ct1); |
| 1023 | xg = warp_reduce_sum(xg, item_ct1); |
| 1024 | |
| 1025 | // cross-warp reduction using local memory (single barrier) |
| 1026 | const auto sub_group = item_ct1.get_sub_group(); |
| 1027 | const auto sg_id = sub_group.get_group_linear_id(); |
| 1028 | const auto wi_in_sg = sub_group.get_local_linear_id(); |
| 1029 | const int nthreads = item_ct1.get_local_range(2); |
| 1030 | const int nwarps = nthreads / WARP_SIZE; |
| 1031 | |
| 1032 | sycl::float2 xx_total = xx; |
| 1033 | sycl::float2 xg_total = xg; |
| 1034 | if (nwarps > 1) { |
| 1035 | if (wi_in_sg == 0) { |
| 1036 | l_xx[sg_id] = xx; |
| 1037 | l_xg[sg_id] = xg; |
| 1038 | } |
| 1039 | item_ct1.barrier(sycl::access::fence_space::local_space); |
| 1040 | |
| 1041 | if (sg_id == 0) { |
| 1042 | const unsigned wi_u = wi_in_sg; |
| 1043 | sycl::float2 xx_first = (wi_u < static_cast<unsigned>(nwarps)) ? l_xx[wi_u] : sycl::float2(0.f, 0.f); |
| 1044 | sycl::float2 xg_first = (wi_u < static_cast<unsigned>(nwarps)) ? l_xg[wi_u] : sycl::float2(0.f, 0.f); |
| 1045 | xx_total = warp_reduce_sum(xx_first, item_ct1); |
| 1046 | xg_total = warp_reduce_sum(xg_first, item_ct1); |
| 1047 | } else { |
| 1048 | // other subgroups keep their local totals; they'll be ignored |
| 1049 | xx_total = xx; |
| 1050 | xg_total = xg; |
| 1051 | } |
| 1052 | // ensure all threads see the first-subgroup result via broadcast below |
| 1053 | } |
| 1054 | |
| 1055 | // compute inv_r and coeff once per row and broadcast to the whole work-group |
| 1056 | float inv_r = 0.f; |
| 1057 | float coeff = 0.f; |
| 1058 | if (tid == 0) { |
| 1059 | const float sum_xx_f = xx_total.x() + xx_total.y(); |
| 1060 | const float sum_xdz_f = xg_total.x() + xg_total.y(); |
| 1061 | const float mean_eps = sum_xx_f / (float) D + eps; |
| 1062 | const float sum_eps = sum_xx_f + eps * (float) D; |
| 1063 | inv_r = sycl::rsqrt(mean_eps); |
| 1064 | coeff = -sum_xdz_f / sum_eps; |
| 1065 | } |
| 1066 | inv_r = sycl::group_broadcast(item_ct1.get_group(), inv_r); |
| 1067 | coeff = sycl::group_broadcast(item_ct1.get_group(), coeff); |
| 1068 | |
| 1069 | for (int64_t col = tid; col < D; col += WG) { |
| 1070 | d_row[col] = (g_row[col] + coeff * x_row[col]) * inv_r; |
| 1071 | } |
| 1072 | }); |
| 1073 | }); |
| 1074 | |
| 1075 | } |
| 1076 | |
| 1077 | void ggml_sycl_op_l2_norm(ggml_backend_sycl_context& ctx, ggml_tensor* dst) { |
| 1078 | const ggml_tensor * src0 = dst->src[0]; |
| 1079 | const float * src0_d = (const float *) src0->data; |
| 1080 | float * dst_d = (float *) dst->data; |
| 1081 | dpct::queue_ptr stream = ctx.stream(); |
| 1082 | |
| 1083 | GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 1084 | GGML_ASSERT( dst->type == GGML_TYPE_F32); |
| 1085 | |
| 1086 | GGML_TENSOR_UNARY_OP_LOCALS; |
| 1087 | |
| 1088 | float eps; |
| 1089 | memcpy(&eps, dst->op_params, sizeof(float)); |
| 1090 | GGML_ASSERT(eps >= 0.0f); |
| 1091 | |
| 1092 | const size_t ts0 = ggml_type_size(src0->type); |
| 1093 | const size_t tdst = ggml_type_size(dst->type); |
| 1094 | GGML_ASSERT(nb00 % ts0 == 0 && nb01 % ts0 == 0 && nb02 % ts0 == 0 && nb03 % ts0 == 0); |
| 1095 | GGML_ASSERT(nb0 % tdst == 0 && nb1 % tdst == 0 && nb2 % tdst == 0 && nb3 % tdst == 0); |
| 1096 | const int64_t ss0 = nb00 / ts0; |
| 1097 | const int64_t ss1 = nb01 / ts0; |
| 1098 | const int64_t ss2 = nb02 / ts0; |
| 1099 | const int64_t ss3 = nb03 / ts0; |
| 1100 | const int64_t ds0 = nb0 / tdst; |
| 1101 | const int64_t ds1 = nb1 / tdst; |
| 1102 | const int64_t ds2 = nb2 / tdst; |
| 1103 | const int64_t ds3 = nb3 / tdst; |
| 1104 | |
| 1105 | /*support both WARP_SIZE or WARP_32_SIZE in code |
| 1106 | choose by hardware for better performance |
| 1107 | */ |
| 1108 | l2_norm_f32_sycl<WARP_SIZE>(src0_d, dst_d, ne00, ne01, ne02, ne03, |
| 1109 | ss0, ss1, ss2, ss3, ds0, ds1, ds2, ds3, eps, stream, ctx.device); |
| 1110 | } |
| 1111 | |
| 1112 | // nodes[0..count) are independent, same-shape, same-eps, same-nb L2_NORM ops validated |
| 1113 | // by the caller; requires ncols < 1024 (the warp reduction path). |
| 1114 | void ggml_sycl_l2_norm_batch(ggml_backend_sycl_context & ctx, ggml_tensor ** nodes, int count) { |
| 1115 | const ggml_tensor * s0 = nodes[0]->src[0]; |
| 1116 | const int ncols = (int) s0->ne[0]; |
| 1117 | const int nrows_total = (int) ggml_nrows(s0); |
| 1118 | float eps; |
| 1119 | memcpy(&eps, nodes[0]->op_params, sizeof(float)); |
| 1120 | GGML_ASSERT(eps >= 0.0f); |
| 1121 | |
| 1122 | l2_batch_ptrs p{}; |
| 1123 | for (int t = 0; t < count; ++t) { |
| 1124 | p.src[t] = (const float *) nodes[t]->src[0]->data; |
| 1125 | p.dst[t] = (float *) nodes[t]->data; |
| 1126 | } |
| 1127 | |
| 1128 | const ggml_tensor * d0 = nodes[0]; |
| 1129 | const size_t ts = ggml_type_size(GGML_TYPE_F32); |
| 1130 | l2_batch_strides st{}; |
| 1131 | st.ne1 = (int) s0->ne[1]; |
| 1132 | st.ne2 = (int) s0->ne[2]; |
| 1133 | st.ss0 = s0->nb[0] / ts; st.ss1 = s0->nb[1] / ts; st.ss2 = s0->nb[2] / ts; st.ss3 = s0->nb[3] / ts; |
| 1134 | st.ds0 = d0->nb[0] / ts; st.ds1 = d0->nb[1] / ts; st.ds2 = d0->nb[2] / ts; st.ds3 = d0->nb[3] / ts; |
| 1135 | |
| 1136 | l2_norm_f32_batch_sycl<WARP_SIZE>(p, st, count, ncols, nrows_total, eps, ctx.stream()); |
| 1137 | } |