ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-sycl / upscale.cpp
| 1 | #include "upscale.hpp" |
| 2 | |
| 3 | static void upscale_f32(const float * x, float * dst, |
| 4 | const int nb00, const int nb01, const int nb02, const int nb03, |
| 5 | const int ne10, const int ne11, const int ne12, const int ne13, |
| 6 | const float sf0, const float sf1, const float sf2, const float sf3) { |
| 7 | auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); |
| 8 | int index = item_ct1.get_local_id(2) + item_ct1.get_group(2) * item_ct1.get_local_range(2); |
| 9 | if (index >= ne10 * ne11 * ne12 * ne13) { |
| 10 | return; |
| 11 | } |
| 12 | |
| 13 | int i10 = index % ne10; |
| 14 | int i11 = (index / ne10) % ne11; |
| 15 | int i12 = (index / (ne10 * ne11)) % ne12; |
| 16 | int i13 = (index / (ne10 * ne11 * ne12)) % ne13; |
| 17 | |
| 18 | int i00 = i10 / sf0; |
| 19 | int i01 = i11 / sf1; |
| 20 | int i02 = i12 / sf2; |
| 21 | int i03 = i13 / sf3; |
| 22 | |
| 23 | dst[index] = *((const float*)((const char*)x + i03 * nb03 + i02 * nb02 + |
| 24 | i01 * nb01 + i00 * nb00)); |
| 25 | } |
| 26 | |
| 27 | static void upscale_f32_bilinear(const float * x, float * dst, |
| 28 | const int nb00, const int nb01, const int nb02, const int nb03, |
| 29 | const int ne00_src, const int ne01_src, |
| 30 | const int ne10_dst, const int ne11_dst, const int ne12_dst, const int ne13_dst, |
| 31 | const float sf0, const float sf1, const float sf2, const float sf3, |
| 32 | const float pixel_offset) { |
| 33 | auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); |
| 34 | const int64_t index = item_ct1.get_local_id(2) + |
| 35 | item_ct1.get_group(2) * item_ct1.get_local_range(2); |
| 36 | const int64_t dst_total_elements = ne10_dst * ne11_dst * ne12_dst * ne13_dst; |
| 37 | |
| 38 | if (index >= dst_total_elements) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | const int i10_dst = index % ne10_dst; |
| 43 | const int i11_dst = (index / ne10_dst) % ne11_dst; |
| 44 | const int i12_dst = (index / (ne10_dst * ne11_dst)) % ne12_dst; |
| 45 | const int i13_dst = index / (ne10_dst * ne11_dst * ne12_dst); |
| 46 | |
| 47 | const int i02_src = (int)(i12_dst / sf2); |
| 48 | const int i03_src = (int)(i13_dst / sf3); |
| 49 | |
| 50 | const float y_src_f = ((float)i11_dst + pixel_offset) / sf1 - pixel_offset; |
| 51 | int y0_src = (int) sycl::floor((float) y_src_f); |
| 52 | int y1_src = y0_src + 1; |
| 53 | |
| 54 | y0_src = sycl::max(0, sycl::min(y0_src, ne01_src - 1)); |
| 55 | y1_src = sycl::max(0, sycl::min(y1_src, ne01_src - 1)); |
| 56 | |
| 57 | float dy = y_src_f - (float)y0_src; |
| 58 | dy = sycl::max(0.0f, sycl::min(dy, 1.0f)); |
| 59 | |
| 60 | float x_src_f = ((float)i10_dst + pixel_offset) / sf0 - pixel_offset; |
| 61 | int x0_src = (int) sycl::floor(x_src_f); |
| 62 | int x1_src = x0_src + 1; |
| 63 | |
| 64 | x0_src = sycl::max(0, sycl::min(x0_src, ne00_src - 1)); |
| 65 | x1_src = sycl::max(0, sycl::min(x1_src, ne00_src - 1)); |
| 66 | |
| 67 | float dx = x_src_f - (float)x0_src; |
| 68 | dx = sycl::max(0.0f, sycl::min(dx, 1.0f)); |
| 69 | |
| 70 | const float* p_a = |
| 71 | (const float*)((const char*)x + (int64_t)x0_src * nb00 + |
| 72 | (int64_t)y0_src * nb01 + (int64_t)i02_src * nb02 + |
| 73 | (int64_t)i03_src * nb03); |
| 74 | const float* p_b = |
| 75 | (const float*)((const char*)x + (int64_t)x1_src * nb00 + |
| 76 | (int64_t)y0_src * nb01 + (int64_t)i02_src * nb02 + |
| 77 | (int64_t)i03_src * nb03); |
| 78 | const float* p_c = |
| 79 | (const float*)((const char*)x + (int64_t)x0_src * nb00 + |
| 80 | (int64_t)y1_src * nb01 + (int64_t)i02_src * nb02 + |
| 81 | (int64_t)i03_src * nb03); |
| 82 | const float* p_d = |
| 83 | (const float*)((const char*)x + (int64_t)x1_src * nb00 + |
| 84 | (int64_t)y1_src * nb01 + (int64_t)i02_src * nb02 + |
| 85 | (int64_t)i03_src * nb03); |
| 86 | |
| 87 | const float val_a = *p_a; |
| 88 | const float val_b = *p_b; |
| 89 | const float val_c = *p_c; |
| 90 | const float val_d = *p_d; |
| 91 | |
| 92 | float result = val_a * (1.0f - dx) * (1.0f - dy) + |
| 93 | val_b * dx * (1.0f - dy) + |
| 94 | val_c * (1.0f - dx) * dy + |
| 95 | val_d * dx * dy; |
| 96 | |
| 97 | dst[index] = result; |
| 98 | } |
| 99 | |
| 100 | // Similar to F.interpolate(..., mode="bilinear", align_corners=False, antialias=True) |
| 101 | // https://github.com/pytorch/pytorch/blob/8871ff29b743948d1225389d5b7068f37b22750b/aten/src/ATen/native/cpu/UpSampleKernel.cpp |
| 102 | static void upscale_f32_bilinear_antialias(const float * src0, |
| 103 | float * dst, |
| 104 | const int nb00, |
| 105 | const int nb01, |
| 106 | const int nb02, |
| 107 | const int nb03, |
| 108 | const int ne00_src, |
| 109 | const int ne01_src, |
| 110 | const int ne10_dst, |
| 111 | const int ne11_dst, |
| 112 | const int ne12_dst, |
| 113 | const int ne13_dst, |
| 114 | const float sf0, |
| 115 | const float sf1, |
| 116 | const float sf2, |
| 117 | const float sf3, |
| 118 | const float pixel_offset) { |
| 119 | auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); |
| 120 | const int64_t index = item_ct1.get_local_id(2) + |
| 121 | item_ct1.get_group(2) * item_ct1.get_local_range(2); |
| 122 | const int64_t dst_total_elements = ne10_dst * ne11_dst * ne12_dst * ne13_dst; |
| 123 | |
| 124 | if (index >= dst_total_elements) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | const int i10_dst = index % ne10_dst; |
| 129 | const int i11_dst = (index / ne10_dst) % ne11_dst; |
| 130 | const int i12_dst = (index / (ne10_dst * ne11_dst)) % ne12_dst; |
| 131 | const int i13_dst = index / (ne10_dst * ne11_dst * ne12_dst); |
| 132 | |
| 133 | const int i02_src = (int)(i12_dst / sf2); |
| 134 | const int i03_src = (int)(i13_dst / sf3); |
| 135 | |
| 136 | const float y = ((float)i11_dst + pixel_offset) / sf1; |
| 137 | const float x = ((float)i10_dst + pixel_offset) / sf0; |
| 138 | |
| 139 | // support and invscale, minimum 1 pixel for bilinear |
| 140 | const float support1 = sycl::max(1.0f / sf1, 1.0f); |
| 141 | const float invscale1 = 1.0f / support1; |
| 142 | const float support0 = sycl::max(1.0f / sf0, 1.0f); |
| 143 | const float invscale0 = 1.0f / support0; |
| 144 | |
| 145 | // the range of source pixels that contribute |
| 146 | const int64_t x_min = sycl::max(int64_t(0), int64_t(x - support0 + pixel_offset)); |
| 147 | const int64_t x_max = sycl::min(int64_t(ne00_src), int64_t(x + support0 + pixel_offset)); |
| 148 | const int64_t y_min = sycl::max(int64_t(0), int64_t(y - support1 + pixel_offset)); |
| 149 | const int64_t y_max = sycl::min(int64_t(ne01_src), int64_t(y + support1 + pixel_offset)); |
| 150 | |
| 151 | // bilinear filter with antialiasing |
| 152 | float val = 0.0f; |
| 153 | float total_weight = 0.0f; |
| 154 | |
| 155 | auto triangle_filter = [](float x) -> float { |
| 156 | return sycl::max(1.0f - sycl::fabs(x), 0.0f); |
| 157 | }; |
| 158 | |
| 159 | for (int64_t sy = y_min; sy < y_max; sy++) { |
| 160 | const float weight_y = triangle_filter((sy - y + pixel_offset) * invscale1); |
| 161 | |
| 162 | for (int64_t sx = x_min; sx < x_max; sx++) { |
| 163 | const float weight_x = triangle_filter((sx - x + pixel_offset) * invscale0); |
| 164 | const float weight = weight_x * weight_y; |
| 165 | |
| 166 | if (weight <= 0.0f) { |
| 167 | continue; |
| 168 | } |
| 169 | |
| 170 | const float pixel = |
| 171 | *(const float*)((const char*)src0 + sx * nb00 + sy * nb01 + |
| 172 | i02_src * nb02 + i03_src * nb03); |
| 173 | val += pixel * weight; |
| 174 | total_weight += weight; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | if (total_weight > 0.0f) { |
| 179 | val /= total_weight; |
| 180 | } |
| 181 | |
| 182 | dst[index] = val; |
| 183 | } |
| 184 | |
| 185 | namespace bicubic_interpolation { |
| 186 | static float weight1(float x, const float &a) { return ((a + 2) * x - (a + 3)) * x * x + 1; } |
| 187 | static float weight2(float x, const float &a) { return ((a * x - 5 * a) * x + 8 * a) * x - 4 * a; } |
| 188 | |
| 189 | static float bicubic(float p0, float p1, float p2, float p3, float x, float a) { |
| 190 | const float w0 = weight2(x + 1, a); |
| 191 | const float w1 = weight1(x + 0, a); |
| 192 | const float w2 = weight1(1 - x, a); |
| 193 | const float w3 = weight2(2 - x, a); |
| 194 | return p0 * w0 + p1 * w1 + p2 * w2 + p3 * w3; |
| 195 | } |
| 196 | |
| 197 | } |
| 198 | |
| 199 | static void upscale_f32_bicubic(const float * x, float * dst, |
| 200 | const int nb00, const int nb01, const int nb02, const int nb03, |
| 201 | const int ne00_src, const int ne01_src, |
| 202 | const int ne10_dst, const int ne11_dst, const int ne12_dst, const int ne13_dst, |
| 203 | const float sf0, const float sf1, const float sf2, const float sf3, |
| 204 | const float pixel_offset) { |
| 205 | auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); |
| 206 | const float a = -0.75f; |
| 207 | using bicubic_interpolation::bicubic; |
| 208 | |
| 209 | const int64_t index = item_ct1.get_local_id(2) + |
| 210 | item_ct1.get_group(2) * item_ct1.get_local_range(2); |
| 211 | const int64_t dst_total_elements = |
| 212 | ne10_dst * ne11_dst * ne12_dst * ne13_dst; |
| 213 | |
| 214 | if (index >= dst_total_elements) { |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | const int i10_dst = index % ne10_dst; |
| 219 | const int i11_dst = (index / ne10_dst) % ne11_dst; |
| 220 | const int i12_dst = (index / (ne10_dst * ne11_dst)) % ne12_dst; |
| 221 | const int i13_dst = index / (ne10_dst * ne11_dst * ne12_dst); |
| 222 | |
| 223 | const int i02_src = (int)(i12_dst / sf2); |
| 224 | const int i03_src = (int)(i13_dst / sf3); |
| 225 | |
| 226 | const float y_src_f = ((float)i11_dst + pixel_offset) / sf1 - pixel_offset; |
| 227 | const int y0_src = (int) sycl::floor((float) y_src_f); |
| 228 | const float dy = y_src_f - (float)y0_src; |
| 229 | |
| 230 | const float x_src_f = ((float)i10_dst + pixel_offset) / sf0 - pixel_offset; |
| 231 | const int x0_src = (int) sycl::floor((float) x_src_f); |
| 232 | const float dx = x_src_f - (float)x0_src; |
| 233 | |
| 234 | const char * x_base = (const char *)x + (int64_t)i02_src * nb02 + (int64_t)i03_src * nb03; |
| 235 | |
| 236 | auto load = [=](int x_off, int y_off) -> float { |
| 237 | int i00_src = sycl::max(0, sycl::min(x0_src + x_off, ne00_src - 1)); |
| 238 | int i01_src = sycl::max(0, sycl::min(y0_src + y_off, ne01_src - 1)); |
| 239 | return *(const float *)(x_base + (int64_t)i00_src * nb00 + (int64_t)i01_src * nb01); |
| 240 | }; |
| 241 | |
| 242 | const float result = bicubic( |
| 243 | bicubic(load(-1, -1), load(0, -1), load(1, -1), load(2, -1), dx, a), |
| 244 | bicubic(load(-1, 0), load(0, 0), load(1, 0), load(2, 0), dx, a), |
| 245 | bicubic(load(-1, 1), load(0, 1), load(1, 1), load(2, 1), dx, a), |
| 246 | bicubic(load(-1, 2), load(0, 2), load(1, 2), load(2, 2), dx, a), |
| 247 | dy, |
| 248 | a); |
| 249 | |
| 250 | dst[index] = result; |
| 251 | } |
| 252 | |
| 253 | static void upscale_f32_sycl(const float * x, |
| 254 | float * dst, |
| 255 | const int nb00, |
| 256 | const int nb01, |
| 257 | const int nb02, |
| 258 | const int nb03, |
| 259 | const int ne10, |
| 260 | const int ne11, |
| 261 | const int ne12, |
| 262 | const int ne13, |
| 263 | const float sf0, |
| 264 | const float sf1, |
| 265 | const float sf2, |
| 266 | const float sf3, |
| 267 | dpct::queue_ptr stream) { |
| 268 | const int64_t dst_size = ne10 * ne11 * ne12 * ne13; |
| 269 | const int64_t num_blocks = (dst_size + SYCL_UPSCALE_BLOCK_SIZE - 1) / SYCL_UPSCALE_BLOCK_SIZE; |
| 270 | |
| 271 | stream->parallel_for( |
| 272 | sycl::nd_range<3>( |
| 273 | sycl::range<3>(1, 1, num_blocks) * sycl::range<3>(1, 1, SYCL_UPSCALE_BLOCK_SIZE), |
| 274 | sycl::range<3>(1, 1, SYCL_UPSCALE_BLOCK_SIZE)), |
| 275 | [=](sycl::nd_item<3> /*item_ct1*/) { |
| 276 | upscale_f32(x, dst, nb00, nb01, nb02, nb03, ne10, ne11, ne12, ne13, sf0, sf1, sf2, sf3); |
| 277 | }); |
| 278 | } |
| 279 | |
| 280 | static void upscale_f32_bilinear_sycl(const float * x, |
| 281 | float * dst, |
| 282 | const int nb00, |
| 283 | const int nb01, |
| 284 | const int nb02, |
| 285 | const int nb03, |
| 286 | const int ne00_src, |
| 287 | const int ne01_src, |
| 288 | const int ne10_dst, |
| 289 | const int ne11_dst, |
| 290 | const int ne12_dst, |
| 291 | const int ne13_dst, |
| 292 | const float sf0, |
| 293 | const float sf1, |
| 294 | const float sf2, |
| 295 | const float sf3, |
| 296 | const float pixel_offset, |
| 297 | bool antialias, |
| 298 | dpct::queue_ptr stream) { |
| 299 | const int64_t dst_size = ne10_dst * ne11_dst * ne12_dst * ne13_dst; |
| 300 | const int64_t num_blocks = (dst_size + SYCL_UPSCALE_BLOCK_SIZE - 1) / SYCL_UPSCALE_BLOCK_SIZE; |
| 301 | |
| 302 | if (antialias) { |
| 303 | stream->parallel_for( |
| 304 | sycl::nd_range<3>( |
| 305 | sycl::range<3>(1, 1, num_blocks) * sycl::range<3>(1, 1, SYCL_UPSCALE_BLOCK_SIZE), |
| 306 | sycl::range<3>(1, 1, SYCL_UPSCALE_BLOCK_SIZE)), |
| 307 | [=](sycl::nd_item<3> /*item_ct1*/) { |
| 308 | upscale_f32_bilinear_antialias( |
| 309 | x, dst, nb00, nb01, nb02, nb03, ne00_src, ne01_src, ne10_dst, ne11_dst, |
| 310 | ne12_dst, ne13_dst, sf0, sf1, sf2, sf3, pixel_offset); |
| 311 | }); |
| 312 | } else { |
| 313 | stream->parallel_for( |
| 314 | sycl::nd_range<3>( |
| 315 | sycl::range<3>(1, 1, num_blocks) * sycl::range<3>(1, 1, SYCL_UPSCALE_BLOCK_SIZE), |
| 316 | sycl::range<3>(1, 1, SYCL_UPSCALE_BLOCK_SIZE)), |
| 317 | [=](sycl::nd_item<3> /*item_ct1*/) { |
| 318 | upscale_f32_bilinear( |
| 319 | x, dst, nb00, nb01, nb02, nb03, ne00_src, ne01_src, ne10_dst, ne11_dst, ne12_dst, |
| 320 | ne13_dst, sf0, sf1, sf2, sf3, pixel_offset); |
| 321 | }); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | static void upscale_f32_bicubic_sycl(const float * x, |
| 326 | float * dst, |
| 327 | const int nb00, |
| 328 | const int nb01, |
| 329 | const int nb02, |
| 330 | const int nb03, |
| 331 | const int ne00_src, |
| 332 | const int ne01_src, |
| 333 | const int ne10_dst, |
| 334 | const int ne11_dst, |
| 335 | const int ne12_dst, |
| 336 | const int ne13_dst, |
| 337 | const float sf0, |
| 338 | const float sf1, |
| 339 | const float sf2, |
| 340 | const float sf3, |
| 341 | const float pixel_offset, |
| 342 | dpct::queue_ptr stream) { |
| 343 | const int64_t dst_size = ne10_dst * ne11_dst * ne12_dst * ne13_dst; |
| 344 | const int64_t num_blocks = (dst_size + SYCL_UPSCALE_BLOCK_SIZE - 1) / SYCL_UPSCALE_BLOCK_SIZE; |
| 345 | |
| 346 | { |
| 347 | stream->submit([&](sycl::handler & cgh) { |
| 348 | cgh.parallel_for( |
| 349 | sycl::nd_range<3>( |
| 350 | sycl::range<3>(1, 1, num_blocks) * sycl::range<3>(1, 1, SYCL_UPSCALE_BLOCK_SIZE), |
| 351 | sycl::range<3>(1, 1, SYCL_UPSCALE_BLOCK_SIZE)), |
| 352 | [=](sycl::nd_item<3> /*item_ct1*/) { |
| 353 | upscale_f32_bicubic( |
| 354 | x, dst, nb00, nb01, nb02, nb03, ne00_src, ne01_src, ne10_dst, ne11_dst, |
| 355 | ne12_dst, ne13_dst, sf0, sf1, sf2, sf3, pixel_offset); |
| 356 | }); |
| 357 | }); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | static void ggml_sycl_op_upscale(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 362 | const ggml_tensor * src0 = dst->src[0]; |
| 363 | const float * src0_d = (const float *)src0->data; |
| 364 | float * dst_d = (float *)dst->data; |
| 365 | dpct::queue_ptr stream = ctx.stream(); |
| 366 | |
| 367 | GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 368 | GGML_ASSERT( dst->type == GGML_TYPE_F32); |
| 369 | |
| 370 | const int mode_flags = dst->op_params[0]; |
| 371 | const ggml_scale_mode mode = (ggml_scale_mode)(mode_flags & 0xFF); |
| 372 | |
| 373 | float sf0 = (float)dst->ne[0]/src0->ne[0]; |
| 374 | float sf1 = (float)dst->ne[1]/src0->ne[1]; |
| 375 | float sf2 = (float)dst->ne[2]/src0->ne[2]; |
| 376 | const float sf3 = (float)dst->ne[3]/src0->ne[3]; |
| 377 | |
| 378 | float pixel_offset = 0.5f; |
| 379 | if (mode_flags & GGML_SCALE_FLAG_ALIGN_CORNERS) { |
| 380 | sf0 = dst->ne[0] > 1 && src0->ne[0] > 1 |
| 381 | ? (float)(dst->ne[0] - 1) / (src0->ne[0] - 1) |
| 382 | : sf0; |
| 383 | sf1 = dst->ne[1] > 1 && src0->ne[1] > 1 |
| 384 | ? (float)(dst->ne[1] - 1) / (src0->ne[1] - 1) |
| 385 | : sf1; |
| 386 | pixel_offset = 0.0f; |
| 387 | } |
| 388 | |
| 389 | if (mode == GGML_SCALE_MODE_NEAREST) { |
| 390 | upscale_f32_sycl( |
| 391 | src0_d, dst_d, src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3], |
| 392 | dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], sf0, sf1, sf2, sf3, stream); |
| 393 | } else if (mode == GGML_SCALE_MODE_BILINEAR) { |
| 394 | const bool antialias = (mode_flags & GGML_SCALE_FLAG_ANTIALIAS); |
| 395 | upscale_f32_bilinear_sycl( |
| 396 | src0_d, dst_d, src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3], |
| 397 | src0->ne[0], src0->ne[1], dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], |
| 398 | sf0, sf1, sf2, sf3, pixel_offset, antialias, stream); |
| 399 | } else if (mode == GGML_SCALE_MODE_BICUBIC) { |
| 400 | upscale_f32_bicubic_sycl( |
| 401 | src0_d, dst_d, src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3], |
| 402 | src0->ne[0], src0->ne[1], dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], |
| 403 | sf0, sf1, sf2, sf3, pixel_offset, stream); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | void ggml_sycl_upscale(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 408 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/1); |
| 409 | ggml_sycl_op_upscale(ctx, dst); |
| 410 | } |