ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-webgpu / ggml-webgpu-shader-lib.hpp
| 1 | #ifndef GGML_WEBGPU_SHADER_LIB_HPP |
| 2 | #define GGML_WEBGPU_SHADER_LIB_HPP |
| 3 | |
| 4 | #include "ggml-impl.h" |
| 5 | #include "ggml-wgsl-shaders.hpp" |
| 6 | #include "ggml.h" |
| 7 | #include "pre_wgsl.hpp" |
| 8 | |
| 9 | #include <webgpu/webgpu_cpp.h> |
| 10 | |
| 11 | #include <algorithm> |
| 12 | #include <memory> |
| 13 | #include <string> |
| 14 | #include <unordered_map> |
| 15 | #include <vector> |
| 16 | |
| 17 | #define GGML_WEBGPU_F16_SIZE_BYTES 2 |
| 18 | #define GGML_WEBGPU_F32_SIZE_BYTES 4 |
| 19 | #define GGML_WEBGPU_I32_SIZE_BYTES 4 |
| 20 | #define GGML_WEBGPU_FLASH_ATTN_PREFERRED_KV_SG_TILES 8u |
| 21 | #define GGML_WEBGPU_FLASH_ATTN_VEC_MAX_SEQ_LEN 20u |
| 22 | #define GGML_WEBGPU_MUL_MAT_VEC_MAX_ROWS 4 |
| 23 | #define GGML_WEBGPU_FLASH_ATTN_VEC_MAX_KV_TILE 32u |
| 24 | #define GGML_WEBGPU_FLASH_ATTN_TILE_MAX_KV_TILE 64u |
| 25 | #define GGML_WEBGPU_FLASH_ATTN_PREFERRED_WG_SIZE 128u |
| 26 | // Matches GGML_PAD(..., 256) in src/llama-context.cpp for KV cache sizing. |
| 27 | #define GGML_WEBGPU_KV_SEQ_PAD 256u |
| 28 | |
| 29 | #define GGML_WEBGPU_ARGSORT_MERGE_MAX_WG_SIZE 512u |
| 30 | |
| 31 | // Matrix multiplication parameters |
| 32 | |
| 33 | // Register tiling parameters |
| 34 | #define WEBGPU_MUL_MAT_TILE_M 4 |
| 35 | #define WEBGPU_MUL_MAT_TILE_N 4 |
| 36 | #define WEBGPU_MUL_MAT_WG_SIZE_M 8 |
| 37 | #define WEBGPU_MUL_MAT_WG_SIZE_N 8 |
| 38 | #define WEBGPU_MUL_MAT_REG_TILE_K_FLOAT 8 |
| 39 | #define WEBGPU_MUL_MAT_REG_TILE_K_QUANT 32 |
| 40 | |
| 41 | // Subgroup matrix parameters |
| 42 | // The number of subgroups in the M dimension |
| 43 | #define WEBGPU_MUL_MAT_SUBGROUP_M 2 |
| 44 | // The number of subgroups in the N dimension |
| 45 | #define WEBGPU_MUL_MAT_SUBGROUP_N 4 |
| 46 | // The number of subgroup matrices each subgroup accumulates over |
| 47 | #define WEBGPU_MUL_MAT_SUBGROUP_MATRIX_M 4 |
| 48 | #define WEBGPU_MUL_MAT_SUBGROUP_MATRIX_N 2 |
| 49 | #define WEBGPU_MUL_MAT_SUBGROUP_TILE_K_FLOAT 32 |
| 50 | #define WEBGPU_MUL_MAT_SUBGROUP_TILE_K_QUANT 32 |
| 51 | |
| 52 | // Matrix-vector multiplication parameters |
| 53 | #define WEBGPU_MUL_MAT_VEC_WG_SIZE 256 |
| 54 | |
| 55 | #define WEBGPU_MUL_MAT_VEC_FLOAT_OUTPUTS_PER_WG 4 |
| 56 | #define WEBGPU_MUL_MAT_VEC_LEGACY_Q_OUTPUTS_PER_WG 4 |
| 57 | #define WEBGPU_MUL_MAT_VEC_K_Q_OUTPUTS_PER_WG 4 |
| 58 | |
| 59 | // default size for reg-tile matrix multiplication |
| 60 | #define WEBGPU_MUL_MAT_WG_SIZE 256 |
| 61 | |
| 62 | // Same hash combine function as in boost |
| 63 | template <typename T> inline void ggml_webgpu_hash_combine(size_t & seed, const T & value) { |
| 64 | seed ^= std::hash<T>{}(value) + 0x9e3779b9 + (seed << 6) + (seed >> 2); |
| 65 | } |
| 66 | |
| 67 | // Calculates base address of a tensor ignoring the fake base pointer |
| 68 | inline uintptr_t ggml_webgpu_tensor_addr(const ggml_tensor * tensor) { |
| 69 | const ggml_tensor * base_tensor = tensor->view_src ? tensor->view_src : tensor; |
| 70 | return (uintptr_t) base_tensor->data + tensor->view_offs; |
| 71 | } |
| 72 | |
| 73 | inline bool ggml_webgpu_tensor_equal(const ggml_tensor * a, const ggml_tensor * b) { |
| 74 | return a->buffer == b->buffer && ggml_webgpu_tensor_addr(a) == ggml_webgpu_tensor_addr(b); |
| 75 | } |
| 76 | |
| 77 | struct ggml_webgpu_shader_lib_context { |
| 78 | ggml_tensor * src0; |
| 79 | ggml_tensor * src1; |
| 80 | ggml_tensor * src2; |
| 81 | ggml_tensor * src3; |
| 82 | ggml_tensor * src4; |
| 83 | ggml_tensor * src5; |
| 84 | ggml_tensor * dst; |
| 85 | ggml_tensor * dst_fuse; |
| 86 | |
| 87 | uint32_t max_wg_size; |
| 88 | size_t wg_mem_limit_bytes = 0; |
| 89 | bool supports_subgroups = false; |
| 90 | bool supports_subgroup_matrix = false; |
| 91 | uint32_t sg_mat_m = 0; |
| 92 | uint32_t sg_mat_n = 0; |
| 93 | uint32_t sg_mat_k = 0; |
| 94 | uint32_t min_subgroup_size = 0; |
| 95 | uint32_t max_subgroup_size = 0; |
| 96 | bool supports_dot_product = false; |
| 97 | std::string vendor; |
| 98 | }; |
| 99 | |
| 100 | struct webgpu_pipeline { |
| 101 | wgpu::ComputePipeline pipeline; |
| 102 | std::string name; |
| 103 | std::shared_ptr<void> context = nullptr; |
| 104 | }; |
| 105 | |
| 106 | struct ggml_webgpu_generic_shader_decisions { |
| 107 | uint32_t wg_size = 0; |
| 108 | bool inplace = false; |
| 109 | }; |
| 110 | |
| 111 | struct ggml_webgpu_get_rows_shader_decisions { |
| 112 | uint32_t wg_size = 0; |
| 113 | bool vectorized = false; |
| 114 | }; |
| 115 | |
| 116 | struct ggml_webgpu_binary_shader_decisions { |
| 117 | uint32_t wg_size = 0; |
| 118 | bool inplace = false; |
| 119 | bool overlap = false; |
| 120 | bool src_overlap = false; |
| 121 | }; |
| 122 | |
| 123 | struct ggml_webgpu_glu_shader_decisions { |
| 124 | uint32_t wg_size = 0; |
| 125 | bool src_overlap = false; |
| 126 | }; |
| 127 | |
| 128 | struct ggml_webgpu_processed_shader { |
| 129 | std::string wgsl; |
| 130 | std::string variant; |
| 131 | std::shared_ptr<void> decisions; |
| 132 | }; |
| 133 | |
| 134 | struct ggml_webgpu_ssm_conv_shader_decisions { |
| 135 | uint32_t block_size; |
| 136 | uint32_t tokens_per_wg; |
| 137 | }; |
| 138 | |
| 139 | struct ggml_webgpu_ssm_scan_pipeline_key { |
| 140 | int type; |
| 141 | int d_state; |
| 142 | uint8_t xbc_overlap; |
| 143 | bool a_overlap; |
| 144 | bool ids_overlap; |
| 145 | |
| 146 | bool operator==(const ggml_webgpu_ssm_scan_pipeline_key & other) const { |
| 147 | return type == other.type && d_state == other.d_state && xbc_overlap == other.xbc_overlap && |
| 148 | a_overlap == other.a_overlap && ids_overlap == other.ids_overlap; |
| 149 | } |
| 150 | }; |
| 151 | |
| 152 | struct ggml_webgpu_ssm_scan_pipeline_key_hash { |
| 153 | size_t operator()(const ggml_webgpu_ssm_scan_pipeline_key & key) const { |
| 154 | size_t seed = 0; |
| 155 | ggml_webgpu_hash_combine(seed, key.type); |
| 156 | ggml_webgpu_hash_combine(seed, key.d_state); |
| 157 | ggml_webgpu_hash_combine(seed, key.xbc_overlap); |
| 158 | ggml_webgpu_hash_combine(seed, key.a_overlap); |
| 159 | ggml_webgpu_hash_combine(seed, key.ids_overlap); |
| 160 | return seed; |
| 161 | } |
| 162 | }; |
| 163 | |
| 164 | struct ggml_webgpu_ssm_scan_shader_decisions { |
| 165 | uint32_t wg_size; |
| 166 | uint32_t tokens_per_tile; |
| 167 | uint8_t xbc_overlap = 0; |
| 168 | bool a_overlap = false; |
| 169 | bool ids_overlap = false; |
| 170 | }; |
| 171 | |
| 172 | /** Argsort **/ |
| 173 | |
| 174 | struct ggml_webgpu_argsort_shader_lib_context { |
| 175 | uint32_t max_wg_size; |
| 176 | size_t wg_mem_limit_bytes; |
| 177 | int32_t order; |
| 178 | }; |
| 179 | |
| 180 | /** Set Rows **/ |
| 181 | |
| 182 | struct ggml_webgpu_set_rows_pipeline_key { |
| 183 | int src0_type; |
| 184 | int dst_type; |
| 185 | int vec4; |
| 186 | int i64_idx; |
| 187 | int pair_blocks; |
| 188 | |
| 189 | bool operator==(const ggml_webgpu_set_rows_pipeline_key & other) const { |
| 190 | return src0_type == other.src0_type && dst_type == other.dst_type && vec4 == other.vec4 && |
| 191 | i64_idx == other.i64_idx && pair_blocks == other.pair_blocks; |
| 192 | } |
| 193 | }; |
| 194 | |
| 195 | struct ggml_webgpu_set_rows_pipeline_key_hash { |
| 196 | size_t operator()(const ggml_webgpu_set_rows_pipeline_key & key) const { |
| 197 | size_t seed = 0; |
| 198 | ggml_webgpu_hash_combine(seed, key.src0_type); |
| 199 | ggml_webgpu_hash_combine(seed, key.dst_type); |
| 200 | ggml_webgpu_hash_combine(seed, key.vec4); |
| 201 | ggml_webgpu_hash_combine(seed, key.i64_idx); |
| 202 | ggml_webgpu_hash_combine(seed, key.pair_blocks); |
| 203 | return seed; |
| 204 | } |
| 205 | }; |
| 206 | |
| 207 | struct ggml_webgpu_set_rows_shader_decisions { |
| 208 | bool vec4; |
| 209 | bool i64_idx; |
| 210 | bool pair_blocks; |
| 211 | uint32_t wg_size; |
| 212 | }; |
| 213 | |
| 214 | /** Set **/ |
| 215 | |
| 216 | struct ggml_webgpu_set_pipeline_key { |
| 217 | ggml_type type; |
| 218 | bool inplace; |
| 219 | |
| 220 | bool operator==(const ggml_webgpu_set_pipeline_key & other) const { |
| 221 | return type == other.type && inplace == other.inplace; |
| 222 | } |
| 223 | }; |
| 224 | |
| 225 | struct ggml_webgpu_set_pipeline_key_hash { |
| 226 | size_t operator()(const ggml_webgpu_set_pipeline_key & key) const { |
| 227 | size_t seed = 0; |
| 228 | ggml_webgpu_hash_combine(seed, key.type); |
| 229 | ggml_webgpu_hash_combine(seed, key.inplace); |
| 230 | return seed; |
| 231 | } |
| 232 | }; |
| 233 | |
| 234 | /** Get Rows **/ |
| 235 | |
| 236 | struct ggml_webgpu_get_rows_pipeline_key { |
| 237 | ggml_type src_type; |
| 238 | int vectorized; |
| 239 | |
| 240 | bool operator==(const ggml_webgpu_get_rows_pipeline_key & other) const { |
| 241 | return src_type == other.src_type && vectorized == other.vectorized; |
| 242 | } |
| 243 | }; |
| 244 | |
| 245 | struct ggml_webgpu_get_rows_pipeline_key_hash { |
| 246 | size_t operator()(const ggml_webgpu_get_rows_pipeline_key & key) const { |
| 247 | size_t seed = 0; |
| 248 | ggml_webgpu_hash_combine(seed, key.src_type); |
| 249 | ggml_webgpu_hash_combine(seed, key.vectorized); |
| 250 | return seed; |
| 251 | } |
| 252 | }; |
| 253 | |
| 254 | /** Row Norm **/ |
| 255 | |
| 256 | struct ggml_webgpu_row_norm_pipeline_key { |
| 257 | ggml_op op; |
| 258 | ggml_type src_type; |
| 259 | ggml_type dst_type; |
| 260 | bool inplace; |
| 261 | |
| 262 | bool operator==(const ggml_webgpu_row_norm_pipeline_key & other) const { |
| 263 | return op == other.op && src_type == other.src_type && dst_type == other.dst_type && inplace == other.inplace; |
| 264 | } |
| 265 | }; |
| 266 | |
| 267 | struct ggml_webgpu_row_norm_pipeline_key_hash { |
| 268 | size_t operator()(const ggml_webgpu_row_norm_pipeline_key & key) const { |
| 269 | size_t seed = 0; |
| 270 | ggml_webgpu_hash_combine(seed, key.op); |
| 271 | ggml_webgpu_hash_combine(seed, key.src_type); |
| 272 | ggml_webgpu_hash_combine(seed, key.dst_type); |
| 273 | ggml_webgpu_hash_combine(seed, key.inplace); |
| 274 | return seed; |
| 275 | } |
| 276 | }; |
| 277 | |
| 278 | /** RMS_NORM + MUL **/ |
| 279 | |
| 280 | struct ggml_webgpu_rms_norm_mul_pipeline_key { |
| 281 | bool inplace; // rn_src == dst |
| 282 | bool overlap; // mul_src == dst |
| 283 | bool src_overlap; // rn_src binding overlaps mul_src binding |
| 284 | |
| 285 | bool operator==(const ggml_webgpu_rms_norm_mul_pipeline_key & other) const { |
| 286 | return inplace == other.inplace && overlap == other.overlap && src_overlap == other.src_overlap; |
| 287 | } |
| 288 | }; |
| 289 | |
| 290 | struct ggml_webgpu_rms_norm_mul_pipeline_key_hash { |
| 291 | size_t operator()(const ggml_webgpu_rms_norm_mul_pipeline_key & key) const { |
| 292 | size_t seed = 0; |
| 293 | ggml_webgpu_hash_combine(seed, key.inplace); |
| 294 | ggml_webgpu_hash_combine(seed, key.overlap); |
| 295 | ggml_webgpu_hash_combine(seed, key.src_overlap); |
| 296 | return seed; |
| 297 | } |
| 298 | }; |
| 299 | |
| 300 | struct ggml_webgpu_rms_norm_mul_shader_decisions { |
| 301 | uint32_t wg_size = 0; |
| 302 | bool inplace = false; |
| 303 | bool overlap = false; |
| 304 | bool src_overlap = false; |
| 305 | }; |
| 306 | |
| 307 | /** Pad **/ |
| 308 | struct ggml_webgpu_pad_pipeline_key { |
| 309 | bool circular; |
| 310 | |
| 311 | bool operator==(const ggml_webgpu_pad_pipeline_key & other) const { return circular == other.circular; } |
| 312 | }; |
| 313 | |
| 314 | struct ggml_webgpu_pad_pipeline_key_hash { |
| 315 | size_t operator()(const ggml_webgpu_pad_pipeline_key & key) const { |
| 316 | size_t seed = 0; |
| 317 | ggml_webgpu_hash_combine(seed, key.circular); |
| 318 | return seed; |
| 319 | } |
| 320 | }; |
| 321 | |
| 322 | /** Solve Tri **/ |
| 323 | struct ggml_webgpu_solve_tri_pipeline_key { |
| 324 | int type; |
| 325 | int n; |
| 326 | int k; |
| 327 | |
| 328 | bool operator==(const ggml_webgpu_solve_tri_pipeline_key & other) const { |
| 329 | return type == other.type && n == other.n && k == other.k; |
| 330 | } |
| 331 | }; |
| 332 | |
| 333 | struct ggml_webgpu_solve_tri_pipeline_key_hash { |
| 334 | size_t operator()(const ggml_webgpu_solve_tri_pipeline_key & key) const { |
| 335 | size_t seed = 0; |
| 336 | ggml_webgpu_hash_combine(seed, key.type); |
| 337 | ggml_webgpu_hash_combine(seed, key.n); |
| 338 | ggml_webgpu_hash_combine(seed, key.k); |
| 339 | return seed; |
| 340 | } |
| 341 | }; |
| 342 | |
| 343 | /** SSM Conv **/ |
| 344 | struct ggml_webgpu_ssm_conv_pipeline_key { |
| 345 | int type; |
| 346 | int vectorized; |
| 347 | |
| 348 | bool operator==(const ggml_webgpu_ssm_conv_pipeline_key & other) const { |
| 349 | return type == other.type && vectorized == other.vectorized; |
| 350 | } |
| 351 | }; |
| 352 | |
| 353 | /** CONV 2D */ |
| 354 | struct ggml_webgpu_conv2d_pipeline_key { |
| 355 | ggml_type weight_type; |
| 356 | ggml_type input_type; |
| 357 | ggml_type output_type; |
| 358 | |
| 359 | bool operator==(const ggml_webgpu_conv2d_pipeline_key & other) const { |
| 360 | return weight_type == other.weight_type && input_type == other.input_type && output_type == other.output_type; |
| 361 | } |
| 362 | }; |
| 363 | |
| 364 | struct ggml_webgpu_conv2d_pipeline_key_hash { |
| 365 | size_t operator()(const ggml_webgpu_conv2d_pipeline_key & key) const { |
| 366 | size_t seed = 0; |
| 367 | ggml_webgpu_hash_combine(seed, key.weight_type); |
| 368 | ggml_webgpu_hash_combine(seed, key.input_type); |
| 369 | ggml_webgpu_hash_combine(seed, key.output_type); |
| 370 | return seed; |
| 371 | } |
| 372 | }; |
| 373 | |
| 374 | // Same type fields as conv2d plus the input layout (WHCN vs CWHN). |
| 375 | struct ggml_webgpu_conv2d_dw_pipeline_key { |
| 376 | ggml_type weight_type; |
| 377 | ggml_type input_type; |
| 378 | ggml_type output_type; |
| 379 | bool whcn; |
| 380 | |
| 381 | bool operator==(const ggml_webgpu_conv2d_dw_pipeline_key & other) const { |
| 382 | return weight_type == other.weight_type && input_type == other.input_type && output_type == other.output_type && |
| 383 | whcn == other.whcn; |
| 384 | } |
| 385 | }; |
| 386 | |
| 387 | struct ggml_webgpu_conv2d_dw_pipeline_key_hash { |
| 388 | size_t operator()(const ggml_webgpu_conv2d_dw_pipeline_key & key) const { |
| 389 | size_t seed = 0; |
| 390 | ggml_webgpu_hash_combine(seed, key.weight_type); |
| 391 | ggml_webgpu_hash_combine(seed, key.input_type); |
| 392 | ggml_webgpu_hash_combine(seed, key.output_type); |
| 393 | ggml_webgpu_hash_combine(seed, key.whcn); |
| 394 | return seed; |
| 395 | } |
| 396 | }; |
| 397 | |
| 398 | /** Im2Col **/ |
| 399 | struct ggml_webgpu_im2col_pipeline_key { |
| 400 | ggml_type input_type; |
| 401 | ggml_type output_type; |
| 402 | |
| 403 | bool operator==(const ggml_webgpu_im2col_pipeline_key & other) const { |
| 404 | return input_type == other.input_type && output_type == other.output_type; |
| 405 | } |
| 406 | }; |
| 407 | |
| 408 | struct ggml_webgpu_im2col_pipeline_key_hash { |
| 409 | size_t operator()(const ggml_webgpu_im2col_pipeline_key & key) const { |
| 410 | size_t seed = 0; |
| 411 | ggml_webgpu_hash_combine(seed, key.input_type); |
| 412 | ggml_webgpu_hash_combine(seed, key.output_type); |
| 413 | return seed; |
| 414 | } |
| 415 | }; |
| 416 | |
| 417 | /** Gated Delta Net **/ |
| 418 | struct ggml_webgpu_gated_delta_net_pipeline_key { |
| 419 | int type; |
| 420 | int s_v; |
| 421 | int kda; |
| 422 | bool fused_cache; |
| 423 | |
| 424 | bool operator==(const ggml_webgpu_gated_delta_net_pipeline_key & other) const { |
| 425 | return type == other.type && s_v == other.s_v && kda == other.kda && fused_cache == other.fused_cache; |
| 426 | } |
| 427 | }; |
| 428 | |
| 429 | struct ggml_webgpu_gated_delta_net_pipeline_key_hash { |
| 430 | size_t operator()(const ggml_webgpu_gated_delta_net_pipeline_key & key) const { |
| 431 | size_t seed = 0; |
| 432 | ggml_webgpu_hash_combine(seed, key.type); |
| 433 | ggml_webgpu_hash_combine(seed, key.s_v); |
| 434 | ggml_webgpu_hash_combine(seed, key.kda); |
| 435 | return seed; |
| 436 | } |
| 437 | }; |
| 438 | |
| 439 | struct ggml_webgpu_ssm_conv_pipeline_key_hash { |
| 440 | size_t operator()(const ggml_webgpu_ssm_conv_pipeline_key & key) const { |
| 441 | size_t seed = 0; |
| 442 | ggml_webgpu_hash_combine(seed, key.type); |
| 443 | ggml_webgpu_hash_combine(seed, key.vectorized); |
| 444 | return seed; |
| 445 | } |
| 446 | }; |
| 447 | |
| 448 | /** Scale **/ |
| 449 | |
| 450 | struct ggml_webgpu_scale_pipeline_key { |
| 451 | int inplace; |
| 452 | |
| 453 | bool operator==(const ggml_webgpu_scale_pipeline_key & other) const { return inplace == other.inplace; } |
| 454 | }; |
| 455 | |
| 456 | struct ggml_webgpu_scale_pipeline_key_hash { |
| 457 | size_t operator()(const ggml_webgpu_scale_pipeline_key & key) const { |
| 458 | size_t seed = 0; |
| 459 | ggml_webgpu_hash_combine(seed, key.inplace); |
| 460 | return seed; |
| 461 | } |
| 462 | }; |
| 463 | |
| 464 | /** Upscale **/ |
| 465 | |
| 466 | struct ggml_webgpu_upscale_pipeline_key { |
| 467 | ggml_type input_type; |
| 468 | ggml_type output_type; |
| 469 | uint32_t base_mode; |
| 470 | bool antialias; |
| 471 | |
| 472 | bool operator==(const ggml_webgpu_upscale_pipeline_key & other) const { |
| 473 | return input_type == other.input_type && output_type == other.output_type && base_mode == other.base_mode && |
| 474 | antialias == other.antialias; |
| 475 | } |
| 476 | }; |
| 477 | |
| 478 | struct ggml_webgpu_upscale_pipeline_key_hash { |
| 479 | size_t operator()(const ggml_webgpu_upscale_pipeline_key & key) const { |
| 480 | size_t seed = 0; |
| 481 | ggml_webgpu_hash_combine(seed, key.input_type); |
| 482 | ggml_webgpu_hash_combine(seed, key.output_type); |
| 483 | ggml_webgpu_hash_combine(seed, key.base_mode); |
| 484 | ggml_webgpu_hash_combine(seed, key.antialias); |
| 485 | return seed; |
| 486 | } |
| 487 | }; |
| 488 | |
| 489 | /** Concat **/ |
| 490 | |
| 491 | struct ggml_webgpu_concat_pipeline_key { |
| 492 | int type; |
| 493 | bool src_overlap; |
| 494 | |
| 495 | bool operator==(const ggml_webgpu_concat_pipeline_key & other) const { |
| 496 | return type == other.type && src_overlap == other.src_overlap; |
| 497 | } |
| 498 | }; |
| 499 | |
| 500 | struct ggml_webgpu_concat_pipeline_key_hash { |
| 501 | size_t operator()(const ggml_webgpu_concat_pipeline_key & key) const { |
| 502 | size_t seed = 0; |
| 503 | ggml_webgpu_hash_combine(seed, key.type); |
| 504 | ggml_webgpu_hash_combine(seed, key.src_overlap); |
| 505 | return seed; |
| 506 | } |
| 507 | }; |
| 508 | |
| 509 | /** Repeat **/ |
| 510 | |
| 511 | struct ggml_webgpu_repeat_pipeline_key { |
| 512 | int type; |
| 513 | |
| 514 | bool operator==(const ggml_webgpu_repeat_pipeline_key & other) const { return type == other.type; } |
| 515 | }; |
| 516 | |
| 517 | struct ggml_webgpu_repeat_pipeline_key_hash { |
| 518 | size_t operator()(const ggml_webgpu_repeat_pipeline_key & key) const { |
| 519 | size_t seed = 0; |
| 520 | ggml_webgpu_hash_combine(seed, key.type); |
| 521 | return seed; |
| 522 | } |
| 523 | }; |
| 524 | |
| 525 | /** Binary **/ |
| 526 | |
| 527 | struct ggml_webgpu_binary_pipeline_key { |
| 528 | int type; |
| 529 | int op; |
| 530 | bool inplace; |
| 531 | bool overlap; |
| 532 | bool src_overlap; |
| 533 | |
| 534 | bool operator==(const ggml_webgpu_binary_pipeline_key & other) const { |
| 535 | return type == other.type && op == other.op && inplace == other.inplace && overlap == other.overlap && |
| 536 | src_overlap == other.src_overlap; |
| 537 | } |
| 538 | }; |
| 539 | |
| 540 | struct ggml_webgpu_binary_pipeline_key_hash { |
| 541 | size_t operator()(const ggml_webgpu_binary_pipeline_key & key) const { |
| 542 | size_t seed = 0; |
| 543 | ggml_webgpu_hash_combine(seed, key.type); |
| 544 | ggml_webgpu_hash_combine(seed, key.op); |
| 545 | ggml_webgpu_hash_combine(seed, key.inplace); |
| 546 | ggml_webgpu_hash_combine(seed, key.overlap); |
| 547 | ggml_webgpu_hash_combine(seed, key.src_overlap); |
| 548 | return seed; |
| 549 | } |
| 550 | }; |
| 551 | |
| 552 | /* Add_Id */ |
| 553 | |
| 554 | struct ggml_webgpu_add_id_pipeline_key { |
| 555 | bool inplace; |
| 556 | |
| 557 | bool operator==(const ggml_webgpu_add_id_pipeline_key & other) const { return inplace == other.inplace; } |
| 558 | }; |
| 559 | |
| 560 | struct ggml_webgpu_add_id_pipeline_key_hash { |
| 561 | size_t operator()(const ggml_webgpu_add_id_pipeline_key & key) const { |
| 562 | size_t seed = 0; |
| 563 | ggml_webgpu_hash_combine(seed, key.inplace); |
| 564 | return seed; |
| 565 | } |
| 566 | }; |
| 567 | |
| 568 | /** Unary **/ |
| 569 | |
| 570 | struct ggml_webgpu_unary_pipeline_key { |
| 571 | int type; |
| 572 | int op; |
| 573 | bool is_unary; // many unary operators fall under the GGML_OP_UNARY umbrella |
| 574 | bool inplace; |
| 575 | ggml_tri_type ttype; // only used for GGML_OP_TRI |
| 576 | |
| 577 | bool operator==(const ggml_webgpu_unary_pipeline_key & other) const { |
| 578 | return type == other.type && op == other.op && is_unary == other.is_unary && inplace == other.inplace && |
| 579 | ttype == other.ttype; |
| 580 | } |
| 581 | }; |
| 582 | |
| 583 | struct ggml_webgpu_unary_pipeline_key_hash { |
| 584 | size_t operator()(const ggml_webgpu_unary_pipeline_key & key) const { |
| 585 | size_t seed = 0; |
| 586 | ggml_webgpu_hash_combine(seed, key.type); |
| 587 | ggml_webgpu_hash_combine(seed, key.op); |
| 588 | ggml_webgpu_hash_combine(seed, key.is_unary); |
| 589 | ggml_webgpu_hash_combine(seed, key.inplace); |
| 590 | ggml_webgpu_hash_combine(seed, key.ttype); |
| 591 | return seed; |
| 592 | } |
| 593 | }; |
| 594 | |
| 595 | /** FlashAttention */ |
| 596 | |
| 597 | struct ggml_webgpu_flash_attn_common_pipeline_key { |
| 598 | ggml_type q_type; |
| 599 | ggml_type k_type; |
| 600 | ggml_type v_type; |
| 601 | ggml_type dst_type; |
| 602 | uint32_t head_dim_qk; |
| 603 | uint32_t head_dim_v; |
| 604 | bool k_direct; |
| 605 | bool v_direct; |
| 606 | bool kv_overlap; |
| 607 | bool has_mask; |
| 608 | bool has_sinks; |
| 609 | bool uses_logit_softcap; |
| 610 | |
| 611 | bool operator==(const ggml_webgpu_flash_attn_common_pipeline_key & other) const { |
| 612 | return q_type == other.q_type && k_type == other.k_type && v_type == other.v_type && |
| 613 | dst_type == other.dst_type && head_dim_qk == other.head_dim_qk && head_dim_v == other.head_dim_v && |
| 614 | k_direct == other.k_direct && v_direct == other.v_direct && kv_overlap == other.kv_overlap && |
| 615 | has_mask == other.has_mask && has_sinks == other.has_sinks && |
| 616 | uses_logit_softcap == other.uses_logit_softcap; |
| 617 | } |
| 618 | }; |
| 619 | |
| 620 | inline void ggml_webgpu_flash_attn_hash_common_pipeline_key(size_t & seed, |
| 621 | const ggml_webgpu_flash_attn_common_pipeline_key & key) { |
| 622 | ggml_webgpu_hash_combine(seed, key.q_type); |
| 623 | ggml_webgpu_hash_combine(seed, key.k_type); |
| 624 | ggml_webgpu_hash_combine(seed, key.v_type); |
| 625 | ggml_webgpu_hash_combine(seed, key.dst_type); |
| 626 | ggml_webgpu_hash_combine(seed, key.head_dim_qk); |
| 627 | ggml_webgpu_hash_combine(seed, key.head_dim_v); |
| 628 | ggml_webgpu_hash_combine(seed, key.k_direct); |
| 629 | ggml_webgpu_hash_combine(seed, key.v_direct); |
| 630 | ggml_webgpu_hash_combine(seed, key.kv_overlap); |
| 631 | ggml_webgpu_hash_combine(seed, key.has_mask); |
| 632 | ggml_webgpu_hash_combine(seed, key.has_sinks); |
| 633 | ggml_webgpu_hash_combine(seed, key.uses_logit_softcap); |
| 634 | } |
| 635 | |
| 636 | struct ggml_webgpu_flash_attn_vec_pipeline_key { |
| 637 | ggml_webgpu_flash_attn_common_pipeline_key common; |
| 638 | |
| 639 | bool operator==(const ggml_webgpu_flash_attn_vec_pipeline_key & other) const { return common == other.common; } |
| 640 | }; |
| 641 | |
| 642 | struct ggml_webgpu_flash_attn_vec_pipeline_key_hash { |
| 643 | size_t operator()(const ggml_webgpu_flash_attn_vec_pipeline_key & key) const { |
| 644 | size_t seed = 0; |
| 645 | ggml_webgpu_flash_attn_hash_common_pipeline_key(seed, key.common); |
| 646 | return seed; |
| 647 | } |
| 648 | }; |
| 649 | |
| 650 | struct ggml_webgpu_flash_attn_pipeline_key { |
| 651 | ggml_webgpu_flash_attn_common_pipeline_key common; |
| 652 | bool use_sg_matrix; |
| 653 | |
| 654 | bool operator==(const ggml_webgpu_flash_attn_pipeline_key & other) const { |
| 655 | return common == other.common && use_sg_matrix == other.use_sg_matrix; |
| 656 | } |
| 657 | }; |
| 658 | |
| 659 | struct ggml_webgpu_flash_attn_pipeline_key_hash { |
| 660 | size_t operator()(const ggml_webgpu_flash_attn_pipeline_key & key) const { |
| 661 | size_t seed = 0; |
| 662 | ggml_webgpu_flash_attn_hash_common_pipeline_key(seed, key.common); |
| 663 | ggml_webgpu_hash_combine(seed, key.use_sg_matrix); |
| 664 | return seed; |
| 665 | } |
| 666 | }; |
| 667 | |
| 668 | struct ggml_webgpu_flash_attn_vec_decisions { |
| 669 | uint32_t kv_tile = 0; |
| 670 | uint32_t wg_size = 0; |
| 671 | }; |
| 672 | |
| 673 | struct ggml_webgpu_flash_attn_decisions { |
| 674 | bool use_sg_matrix = false; |
| 675 | uint32_t q_tile = 0; |
| 676 | uint32_t kv_tile = 0; |
| 677 | uint32_t wg_size = 0; |
| 678 | }; |
| 679 | |
| 680 | inline constexpr uint32_t GGML_WEBGPU_FLASH_ATTN_TILE_KV_VEC_WIDTH = 4u; |
| 681 | inline constexpr uint32_t GGML_WEBGPU_FLASH_ATTN_TILE_Q_TILE = 4u; |
| 682 | |
| 683 | inline size_t ggml_webgpu_flash_attn_tensor_offset(const ggml_tensor * tensor) { |
| 684 | constexpr uintptr_t ptr_base_addr = 0x1000u; |
| 685 | const ggml_tensor * base = tensor->view_src != nullptr ? tensor->view_src : tensor; |
| 686 | return reinterpret_cast<uintptr_t>(base->data) - ptr_base_addr + tensor->view_offs; |
| 687 | } |
| 688 | |
| 689 | inline bool ggml_webgpu_flash_attn_float_vec4_aligned(const ggml_tensor * K, size_t storage_offset_alignment) { |
| 690 | const uint32_t offset_elems = |
| 691 | (uint32_t) ((ggml_webgpu_flash_attn_tensor_offset(K) & (storage_offset_alignment - 1)) / |
| 692 | ggml_type_size(K->type)); |
| 693 | return offset_elems % GGML_WEBGPU_FLASH_ATTN_TILE_KV_VEC_WIDTH == 0u; |
| 694 | } |
| 695 | |
| 696 | inline bool ggml_webgpu_flash_attn_float_vec4_aligned(const ggml_tensor * K, |
| 697 | const ggml_tensor * V, |
| 698 | size_t storage_offset_alignment) { |
| 699 | return ggml_webgpu_flash_attn_float_vec4_aligned(K, storage_offset_alignment) && |
| 700 | ggml_webgpu_flash_attn_float_vec4_aligned(V, storage_offset_alignment); |
| 701 | } |
| 702 | |
| 703 | inline bool ggml_webgpu_flash_attn_k_direct(const ggml_tensor * Q, const ggml_tensor * K, uint32_t kv_direct_align) { |
| 704 | return (K->type == GGML_TYPE_F16 || K->type == GGML_TYPE_Q8_0 || K->type == GGML_TYPE_Q4_0) && |
| 705 | (Q->ne[0] % kv_direct_align == 0) && (K->ne[1] % GGML_WEBGPU_KV_SEQ_PAD == 0); |
| 706 | } |
| 707 | |
| 708 | inline bool ggml_webgpu_flash_attn_v_direct(const ggml_tensor * Q, const ggml_tensor * V, uint32_t kv_direct_align) { |
| 709 | return ggml_webgpu_flash_attn_k_direct(Q, V, kv_direct_align); |
| 710 | } |
| 711 | |
| 712 | inline ggml_webgpu_flash_attn_common_pipeline_key ggml_webgpu_flash_attn_make_common_pipeline_key( |
| 713 | const ggml_webgpu_shader_lib_context & context, |
| 714 | uint32_t kv_direct_align, |
| 715 | bool kv_overlap) { |
| 716 | ggml_webgpu_flash_attn_common_pipeline_key key = {}; |
| 717 | key.q_type = context.src0->type; |
| 718 | key.k_type = context.src1->type; |
| 719 | key.v_type = context.src2->type; |
| 720 | key.dst_type = context.dst->type; |
| 721 | key.head_dim_qk = (uint32_t) context.src0->ne[0]; |
| 722 | key.head_dim_v = (uint32_t) context.src2->ne[0]; |
| 723 | key.k_direct = ggml_webgpu_flash_attn_k_direct(context.src0, context.src1, kv_direct_align); |
| 724 | key.v_direct = ggml_webgpu_flash_attn_v_direct(context.src0, context.src2, kv_direct_align); |
| 725 | key.kv_overlap = kv_overlap; |
| 726 | key.has_mask = context.src3 != nullptr; |
| 727 | key.has_sinks = context.src4 != nullptr; |
| 728 | key.uses_logit_softcap = ggml_get_op_params_f32(context.dst, 2) != 0.0f; |
| 729 | return key; |
| 730 | } |
| 731 | |
| 732 | inline std::vector<std::string> ggml_webgpu_flash_attn_common_defines( |
| 733 | const ggml_webgpu_flash_attn_common_pipeline_key & key, |
| 734 | std::string & variant, |
| 735 | uint32_t q_tile, |
| 736 | uint32_t kv_tile, |
| 737 | uint32_t wg_size) { |
| 738 | std::vector<std::string> defines; |
| 739 | |
| 740 | switch (key.k_type) { |
| 741 | case GGML_TYPE_F32: |
| 742 | defines.push_back("K_F32"); |
| 743 | break; |
| 744 | case GGML_TYPE_F16: |
| 745 | defines.push_back("K_F16"); |
| 746 | break; |
| 747 | case GGML_TYPE_Q4_0: |
| 748 | defines.push_back("K_Q4_0"); |
| 749 | break; |
| 750 | case GGML_TYPE_Q8_0: |
| 751 | defines.push_back("K_Q8_0"); |
| 752 | break; |
| 753 | default: |
| 754 | GGML_ABORT("Unsupported K type for flash attention shader"); |
| 755 | } |
| 756 | variant += std::string("_k") + ggml_type_name(key.k_type); |
| 757 | |
| 758 | switch (key.v_type) { |
| 759 | case GGML_TYPE_F32: |
| 760 | defines.push_back("V_F32"); |
| 761 | break; |
| 762 | case GGML_TYPE_F16: |
| 763 | defines.push_back("V_F16"); |
| 764 | break; |
| 765 | case GGML_TYPE_Q4_0: |
| 766 | defines.push_back("V_Q4_0"); |
| 767 | break; |
| 768 | case GGML_TYPE_Q8_0: |
| 769 | defines.push_back("V_Q8_0"); |
| 770 | break; |
| 771 | default: |
| 772 | GGML_ABORT("Unsupported V type for flash attention shader"); |
| 773 | } |
| 774 | variant += std::string("_v") + ggml_type_name(key.v_type); |
| 775 | |
| 776 | switch (key.q_type) { |
| 777 | case GGML_TYPE_F32: |
| 778 | defines.push_back("Q_F32"); |
| 779 | break; |
| 780 | case GGML_TYPE_F16: |
| 781 | defines.push_back("Q_F16"); |
| 782 | break; |
| 783 | default: |
| 784 | GGML_ABORT("Unsupported Q type for flash attention shader"); |
| 785 | } |
| 786 | variant += std::string("_q") + ggml_type_name(key.q_type); |
| 787 | |
| 788 | switch (key.dst_type) { |
| 789 | case GGML_TYPE_F32: |
| 790 | defines.push_back("DST_F32"); |
| 791 | break; |
| 792 | case GGML_TYPE_F16: |
| 793 | defines.push_back("DST_F16"); |
| 794 | break; |
| 795 | default: |
| 796 | GGML_ABORT("Unsupported dst type for flash attention shader"); |
| 797 | } |
| 798 | variant += std::string("_dst") + ggml_type_name(key.dst_type); |
| 799 | |
| 800 | if (key.has_mask) { |
| 801 | defines.push_back("MASK"); |
| 802 | variant += "_mask"; |
| 803 | } |
| 804 | if (key.has_sinks) { |
| 805 | defines.push_back("SINKS"); |
| 806 | variant += "_sinks"; |
| 807 | } |
| 808 | if (key.uses_logit_softcap) { |
| 809 | defines.push_back("LOGIT_SOFTCAP"); |
| 810 | variant += "_lgsc"; |
| 811 | } |
| 812 | if (key.k_direct) { |
| 813 | defines.push_back("K_DIRECT"); |
| 814 | variant += "_k_direct"; |
| 815 | } |
| 816 | if (key.v_direct) { |
| 817 | defines.push_back("V_DIRECT"); |
| 818 | variant += "_v_direct"; |
| 819 | } |
| 820 | if (key.kv_overlap) { |
| 821 | defines.push_back("KV_OVERLAP"); |
| 822 | variant += "_kv_overlap"; |
| 823 | } |
| 824 | |
| 825 | defines.push_back(std::string("HEAD_DIM_QK=") + std::to_string(key.head_dim_qk)); |
| 826 | variant += std::string("_hsqk") + std::to_string(key.head_dim_qk); |
| 827 | |
| 828 | defines.push_back(std::string("HEAD_DIM_V=") + std::to_string(key.head_dim_v)); |
| 829 | variant += std::string("_hsv") + std::to_string(key.head_dim_v); |
| 830 | |
| 831 | defines.push_back(std::string("Q_TILE=") + std::to_string(q_tile)); |
| 832 | defines.push_back(std::string("KV_TILE=") + std::to_string(kv_tile)); |
| 833 | defines.push_back(std::string("WG_SIZE=") + std::to_string(wg_size)); |
| 834 | |
| 835 | if (ggml_is_quantized(key.k_type) || ggml_is_quantized(key.v_type)) { |
| 836 | defines.push_back("U32_DEQUANT_HELPERS"); |
| 837 | if (ggml_is_quantized(key.k_type)) { |
| 838 | defines.push_back("LOADERS_QUANTIZED_K"); |
| 839 | } |
| 840 | if (ggml_is_quantized(key.v_type)) { |
| 841 | defines.push_back("LOADERS_QUANTIZED_V"); |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | return defines; |
| 846 | } |
| 847 | |
| 848 | struct ggml_webgpu_flash_attn_vec_reduce_pipeline_key { |
| 849 | uint32_t head_dim_v; |
| 850 | uint32_t wg_size; |
| 851 | ggml_type dst_type; |
| 852 | }; |
| 853 | |
| 854 | struct ggml_webgpu_flash_attn_vec_reduce_pipeline_key_hash { |
| 855 | size_t operator()(const ggml_webgpu_flash_attn_vec_reduce_pipeline_key & key) const { |
| 856 | size_t seed = 0; |
| 857 | ggml_webgpu_hash_combine(seed, key.head_dim_v); |
| 858 | ggml_webgpu_hash_combine(seed, key.wg_size); |
| 859 | ggml_webgpu_hash_combine(seed, key.dst_type); |
| 860 | return seed; |
| 861 | } |
| 862 | }; |
| 863 | |
| 864 | inline bool operator==(const ggml_webgpu_flash_attn_vec_reduce_pipeline_key & lhs, |
| 865 | const ggml_webgpu_flash_attn_vec_reduce_pipeline_key & rhs) { |
| 866 | return lhs.head_dim_v == rhs.head_dim_v && lhs.wg_size == rhs.wg_size && lhs.dst_type == rhs.dst_type; |
| 867 | } |
| 868 | |
| 869 | struct ggml_webgpu_flash_attn_blk_pipeline_key { |
| 870 | uint32_t kv_tile; |
| 871 | |
| 872 | bool operator==(const ggml_webgpu_flash_attn_blk_pipeline_key & other) const { return kv_tile == other.kv_tile; } |
| 873 | }; |
| 874 | |
| 875 | struct ggml_webgpu_flash_attn_blk_pipeline_key_hash { |
| 876 | size_t operator()(const ggml_webgpu_flash_attn_blk_pipeline_key & key) const { |
| 877 | size_t seed = 0; |
| 878 | ggml_webgpu_hash_combine(seed, key.kv_tile); |
| 879 | return seed; |
| 880 | } |
| 881 | }; |
| 882 | |
| 883 | // Note: this will slightly overestimate memory usage for vec path |
| 884 | // since row_max and exp_sum shmem are not needed. |
| 885 | inline size_t ggml_webgpu_flash_attn_wg_mem_bytes(uint32_t q_tile, |
| 886 | uint32_t kv_tile, |
| 887 | uint32_t head_dim_qk, |
| 888 | uint32_t head_dim_v, |
| 889 | bool has_mask, |
| 890 | bool kv_direct) { |
| 891 | const uint32_t max_head_dim = std::max(head_dim_qk, head_dim_v); |
| 892 | size_t f16_elems = 0; |
| 893 | size_t f32_elems = 0; |
| 894 | |
| 895 | f32_elems += q_tile * head_dim_qk; // q_shmem |
| 896 | if (!kv_direct) { |
| 897 | f32_elems += kv_tile * max_head_dim; // kv_shmem |
| 898 | } |
| 899 | f32_elems += q_tile * head_dim_v; // o_shmem |
| 900 | if (has_mask) { |
| 901 | f32_elems += q_tile * kv_tile; // mask_shmem |
| 902 | } |
| 903 | f32_elems += q_tile * kv_tile; // inter_shmem |
| 904 | f32_elems += q_tile; // row_max_shmem |
| 905 | f32_elems += q_tile; // exp_sum_shmem |
| 906 | return f16_elems * GGML_WEBGPU_F16_SIZE_BYTES + f32_elems * GGML_WEBGPU_F32_SIZE_BYTES; |
| 907 | } |
| 908 | |
| 909 | inline uint32_t ggml_webgpu_flash_attn_max_kv_tile(size_t limit_bytes, |
| 910 | uint32_t q_tile, |
| 911 | uint32_t kv_granularity, |
| 912 | uint32_t head_dim_qk, |
| 913 | uint32_t head_dim_v, |
| 914 | bool has_mask, |
| 915 | bool kv_direct) { |
| 916 | const size_t base_q_bytes = |
| 917 | ggml_webgpu_flash_attn_wg_mem_bytes(q_tile, 0, head_dim_qk, head_dim_v, has_mask, kv_direct); |
| 918 | if (limit_bytes <= base_q_bytes) { |
| 919 | return 0; |
| 920 | } |
| 921 | const size_t one_kv_bytes = |
| 922 | ggml_webgpu_flash_attn_wg_mem_bytes(q_tile, 1, head_dim_qk, head_dim_v, has_mask, kv_direct); |
| 923 | const size_t bytes_per_kv = one_kv_bytes - base_q_bytes; |
| 924 | if (bytes_per_kv == 0) { |
| 925 | return 0; |
| 926 | } |
| 927 | const size_t max_kv_tile = (limit_bytes - base_q_bytes) / bytes_per_kv; |
| 928 | return (uint32_t) ((max_kv_tile / kv_granularity) * kv_granularity); |
| 929 | } |
| 930 | |
| 931 | inline uint32_t ggml_webgpu_flash_attn_get_vec_kv_tile(size_t wg_mem_limit_bytes, |
| 932 | uint32_t head_dim_qk, |
| 933 | uint32_t head_dim_v, |
| 934 | bool has_mask, |
| 935 | bool kv_direct) { |
| 936 | const uint32_t max_kv_tile = |
| 937 | ggml_webgpu_flash_attn_max_kv_tile(wg_mem_limit_bytes, 1u, 1u, head_dim_qk, head_dim_v, has_mask, kv_direct); |
| 938 | GGML_ASSERT(max_kv_tile > 0); |
| 939 | |
| 940 | uint32_t kv_tile = std::min(GGML_WEBGPU_FLASH_ATTN_VEC_MAX_KV_TILE, max_kv_tile); |
| 941 | if (kv_direct) { |
| 942 | kv_tile = std::min(kv_tile, GGML_WEBGPU_KV_SEQ_PAD); |
| 943 | while (GGML_WEBGPU_KV_SEQ_PAD % kv_tile != 0) { |
| 944 | kv_tile -= 1u; |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | return kv_tile; |
| 949 | } |
| 950 | |
| 951 | inline bool ggml_webgpu_flash_attn_can_use_subgroup_matrix_path(bool supports_subgroup_matrix, |
| 952 | uint32_t sg_mat_k, |
| 953 | uint32_t sg_mat_n, |
| 954 | const ggml_tensor * Q, |
| 955 | const ggml_tensor * V) { |
| 956 | return supports_subgroup_matrix && Q->ne[0] % sg_mat_k == 0 && V->ne[0] % sg_mat_n == 0; |
| 957 | } |
| 958 | |
| 959 | /** Matrix Multiplication **/ |
| 960 | |
| 961 | struct ggml_webgpu_mul_mat_vec_pipeline_key { |
| 962 | ggml_type src0_type; |
| 963 | ggml_type src1_type; |
| 964 | int vectorized; |
| 965 | uint32_t num_cols; |
| 966 | bool use_mmvq; |
| 967 | bool src_overlap; |
| 968 | |
| 969 | bool operator==(const ggml_webgpu_mul_mat_vec_pipeline_key & other) const { |
| 970 | return src0_type == other.src0_type && src1_type == other.src1_type && vectorized == other.vectorized && |
| 971 | num_cols == other.num_cols && use_mmvq == other.use_mmvq && src_overlap == other.src_overlap; |
| 972 | } |
| 973 | }; |
| 974 | |
| 975 | struct ggml_webgpu_mul_mat_vec_pipeline_key_hash { |
| 976 | size_t operator()(const ggml_webgpu_mul_mat_vec_pipeline_key & key) const { |
| 977 | size_t seed = 0; |
| 978 | ggml_webgpu_hash_combine(seed, key.src0_type); |
| 979 | ggml_webgpu_hash_combine(seed, key.src1_type); |
| 980 | ggml_webgpu_hash_combine(seed, key.vectorized); |
| 981 | ggml_webgpu_hash_combine(seed, key.num_cols); |
| 982 | ggml_webgpu_hash_combine(seed, key.use_mmvq); |
| 983 | ggml_webgpu_hash_combine(seed, key.src_overlap); |
| 984 | return seed; |
| 985 | } |
| 986 | }; |
| 987 | |
| 988 | struct ggml_webgpu_mul_mat_vec_shader_decisions { |
| 989 | uint32_t wg_size; |
| 990 | uint32_t outputs_per_wg; |
| 991 | uint32_t vec_size; |
| 992 | bool src_overlap = false; |
| 993 | }; |
| 994 | |
| 995 | struct ggml_webgpu_quantize_q8_pipeline_key { |
| 996 | ggml_type src0_type; |
| 997 | |
| 998 | bool operator==(const ggml_webgpu_quantize_q8_pipeline_key & other) const { return src0_type == other.src0_type; } |
| 999 | }; |
| 1000 | |
| 1001 | struct ggml_webgpu_quantize_q8_pipeline_key_hash { |
| 1002 | size_t operator()(const ggml_webgpu_quantize_q8_pipeline_key & key) const { |
| 1003 | size_t seed = 0; |
| 1004 | ggml_webgpu_hash_combine(seed, key.src0_type); |
| 1005 | return seed; |
| 1006 | } |
| 1007 | }; |
| 1008 | |
| 1009 | struct ggml_webgpu_mul_mat_pipeline_key { |
| 1010 | ggml_type src0_type; |
| 1011 | ggml_type src1_type; |
| 1012 | int vectorized; |
| 1013 | int use_subgroup_matrix; |
| 1014 | bool src_overlap; |
| 1015 | |
| 1016 | bool operator==(const ggml_webgpu_mul_mat_pipeline_key & other) const { |
| 1017 | return src0_type == other.src0_type && src1_type == other.src1_type && vectorized == other.vectorized && |
| 1018 | use_subgroup_matrix == other.use_subgroup_matrix && src_overlap == other.src_overlap; |
| 1019 | } |
| 1020 | }; |
| 1021 | |
| 1022 | struct ggml_webgpu_mul_mat_pipeline_key_hash { |
| 1023 | size_t operator()(const ggml_webgpu_mul_mat_pipeline_key & key) const { |
| 1024 | size_t seed = 0; |
| 1025 | ggml_webgpu_hash_combine(seed, key.src0_type); |
| 1026 | ggml_webgpu_hash_combine(seed, key.src1_type); |
| 1027 | ggml_webgpu_hash_combine(seed, key.vectorized); |
| 1028 | ggml_webgpu_hash_combine(seed, key.use_subgroup_matrix); |
| 1029 | ggml_webgpu_hash_combine(seed, key.src_overlap); |
| 1030 | return seed; |
| 1031 | } |
| 1032 | }; |
| 1033 | |
| 1034 | struct ggml_webgpu_mul_mat_shader_decisions { |
| 1035 | uint32_t tile_k; |
| 1036 | uint32_t wg_size_m; |
| 1037 | uint32_t wg_size_n; |
| 1038 | uint32_t wg_size; |
| 1039 | uint32_t outputs_per_wg; |
| 1040 | int use_subgroup_matrix; |
| 1041 | |
| 1042 | uint32_t tile_m; |
| 1043 | uint32_t tile_n; |
| 1044 | |
| 1045 | // Subgroup matrix parameters |
| 1046 | uint32_t subgroup_m; |
| 1047 | uint32_t subgroup_n; |
| 1048 | uint32_t subgroup_matrix_m; |
| 1049 | uint32_t subgroup_matrix_n; |
| 1050 | |
| 1051 | uint32_t mul_mat_wg_size; |
| 1052 | bool src_overlap = false; |
| 1053 | }; |
| 1054 | |
| 1055 | /** MUL_MAT_ID **/ |
| 1056 | |
| 1057 | struct ggml_webgpu_mul_mat_id_gather_pipeline_key { |
| 1058 | uint32_t n_experts; |
| 1059 | |
| 1060 | bool operator==(const ggml_webgpu_mul_mat_id_gather_pipeline_key & other) const { |
| 1061 | return n_experts == other.n_experts; |
| 1062 | } |
| 1063 | }; |
| 1064 | |
| 1065 | struct ggml_webgpu_mul_mat_id_gather_pipeline_key_hash { |
| 1066 | size_t operator()(const ggml_webgpu_mul_mat_id_gather_pipeline_key & key) const { |
| 1067 | size_t seed = 0; |
| 1068 | ggml_webgpu_hash_combine(seed, key.n_experts); |
| 1069 | return seed; |
| 1070 | } |
| 1071 | }; |
| 1072 | |
| 1073 | struct ggml_webgpu_mul_mat_id_pipeline_key { |
| 1074 | ggml_type src0_type; |
| 1075 | ggml_type src1_type; |
| 1076 | uint32_t n_experts; |
| 1077 | uint32_t num_cols; |
| 1078 | int vectorized; |
| 1079 | |
| 1080 | bool operator==(const ggml_webgpu_mul_mat_id_pipeline_key & other) const { |
| 1081 | return src0_type == other.src0_type && src1_type == other.src1_type && n_experts == other.n_experts && |
| 1082 | num_cols == other.num_cols && vectorized == other.vectorized; |
| 1083 | } |
| 1084 | }; |
| 1085 | |
| 1086 | struct ggml_webgpu_mul_mat_id_pipeline_key_hash { |
| 1087 | size_t operator()(const ggml_webgpu_mul_mat_id_pipeline_key & key) const { |
| 1088 | size_t seed = 0; |
| 1089 | ggml_webgpu_hash_combine(seed, key.src0_type); |
| 1090 | ggml_webgpu_hash_combine(seed, key.src1_type); |
| 1091 | ggml_webgpu_hash_combine(seed, key.n_experts); |
| 1092 | ggml_webgpu_hash_combine(seed, key.num_cols); |
| 1093 | ggml_webgpu_hash_combine(seed, key.vectorized); |
| 1094 | return seed; |
| 1095 | } |
| 1096 | }; |
| 1097 | |
| 1098 | /** Cpy **/ |
| 1099 | |
| 1100 | struct ggml_webgpu_cpy_pipeline_key { |
| 1101 | ggml_type src_type; |
| 1102 | ggml_type dst_type; |
| 1103 | |
| 1104 | bool operator==(const ggml_webgpu_cpy_pipeline_key & other) const { |
| 1105 | return src_type == other.src_type && dst_type == other.dst_type; |
| 1106 | } |
| 1107 | }; |
| 1108 | |
| 1109 | struct ggml_webgpu_cpy_pipeline_key_hash { |
| 1110 | size_t operator()(const ggml_webgpu_cpy_pipeline_key & key) const { |
| 1111 | size_t seed = 0; |
| 1112 | ggml_webgpu_hash_combine(seed, key.src_type); |
| 1113 | ggml_webgpu_hash_combine(seed, key.dst_type); |
| 1114 | return seed; |
| 1115 | } |
| 1116 | }; |
| 1117 | |
| 1118 | /** Glu **/ |
| 1119 | |
| 1120 | struct ggml_webgpu_glu_pipeline_key { |
| 1121 | ggml_glu_op glu_op; |
| 1122 | ggml_type type; |
| 1123 | bool split; |
| 1124 | bool src_overlap; |
| 1125 | |
| 1126 | bool operator==(const ggml_webgpu_glu_pipeline_key & other) const { |
| 1127 | return glu_op == other.glu_op && type == other.type && split == other.split && src_overlap == other.src_overlap; |
| 1128 | } |
| 1129 | }; |
| 1130 | |
| 1131 | struct ggml_webgpu_glu_pipeline_key_hash { |
| 1132 | size_t operator()(const ggml_webgpu_glu_pipeline_key & key) const { |
| 1133 | size_t seed = 0; |
| 1134 | ggml_webgpu_hash_combine(seed, key.glu_op); |
| 1135 | ggml_webgpu_hash_combine(seed, key.type); |
| 1136 | ggml_webgpu_hash_combine(seed, key.split); |
| 1137 | ggml_webgpu_hash_combine(seed, key.src_overlap); |
| 1138 | return seed; |
| 1139 | } |
| 1140 | }; |
| 1141 | |
| 1142 | /** Rope **/ |
| 1143 | |
| 1144 | struct ggml_webgpu_rope_pipeline_key { |
| 1145 | ggml_type type; |
| 1146 | bool inplace; |
| 1147 | bool has_ff; |
| 1148 | |
| 1149 | bool operator==(const ggml_webgpu_rope_pipeline_key & other) const { |
| 1150 | return type == other.type && inplace == other.inplace && has_ff == other.has_ff; |
| 1151 | } |
| 1152 | }; |
| 1153 | |
| 1154 | struct ggml_webgpu_rope_pipeline_key_hash { |
| 1155 | size_t operator()(const ggml_webgpu_rope_pipeline_key & key) const { |
| 1156 | size_t seed = 0; |
| 1157 | ggml_webgpu_hash_combine(seed, key.type); |
| 1158 | ggml_webgpu_hash_combine(seed, key.inplace); |
| 1159 | ggml_webgpu_hash_combine(seed, key.has_ff); |
| 1160 | return seed; |
| 1161 | } |
| 1162 | }; |
| 1163 | |
| 1164 | /** SoftMax **/ |
| 1165 | |
| 1166 | struct ggml_webgpu_soft_max_pipeline_key { |
| 1167 | ggml_type mask_type; |
| 1168 | bool has_mask; |
| 1169 | bool has_sink; |
| 1170 | bool inplace; |
| 1171 | |
| 1172 | bool operator==(const ggml_webgpu_soft_max_pipeline_key & other) const { |
| 1173 | return mask_type == other.mask_type && has_mask == other.has_mask && has_sink == other.has_sink && |
| 1174 | inplace == other.inplace; |
| 1175 | } |
| 1176 | }; |
| 1177 | |
| 1178 | struct ggml_webgpu_soft_max_pipeline_key_hash { |
| 1179 | size_t operator()(const ggml_webgpu_soft_max_pipeline_key & key) const { |
| 1180 | size_t seed = 0; |
| 1181 | ggml_webgpu_hash_combine(seed, key.mask_type); |
| 1182 | ggml_webgpu_hash_combine(seed, key.has_mask); |
| 1183 | ggml_webgpu_hash_combine(seed, key.has_sink); |
| 1184 | ggml_webgpu_hash_combine(seed, key.inplace); |
| 1185 | return seed; |
| 1186 | } |
| 1187 | }; |
| 1188 | |
| 1189 | /** MMVQ **/ |
| 1190 | |
| 1191 | inline bool ggml_webgpu_can_use_mmvq(const ggml_tensor * src0, |
| 1192 | const ggml_tensor * src1, |
| 1193 | int64_t n_rows, |
| 1194 | bool supports_dot_product, |
| 1195 | const std::string & vendor) { |
| 1196 | if (n_rows <= GGML_WEBGPU_MUL_MAT_VEC_MAX_ROWS) { |
| 1197 | bool supports_dp4a = vendor == "amd" || vendor == "intel" || vendor == "nvidia"; |
| 1198 | if (supports_dp4a && supports_dot_product) { |
| 1199 | switch (src1->type) { |
| 1200 | case GGML_TYPE_F32: |
| 1201 | switch (src0->type) { |
| 1202 | case GGML_TYPE_Q1_0: |
| 1203 | case GGML_TYPE_Q4_0: |
| 1204 | case GGML_TYPE_Q4_1: |
| 1205 | case GGML_TYPE_Q5_0: |
| 1206 | case GGML_TYPE_Q5_1: |
| 1207 | case GGML_TYPE_Q8_0: |
| 1208 | case GGML_TYPE_MXFP4: |
| 1209 | case GGML_TYPE_Q2_K: |
| 1210 | case GGML_TYPE_Q3_K: |
| 1211 | case GGML_TYPE_Q4_K: |
| 1212 | case GGML_TYPE_Q5_K: |
| 1213 | case GGML_TYPE_Q6_K: |
| 1214 | return src0->ne[0] % 4 == 0; |
| 1215 | default: |
| 1216 | break; |
| 1217 | } |
| 1218 | break; |
| 1219 | default: |
| 1220 | break; |
| 1221 | } |
| 1222 | } |
| 1223 | } |
| 1224 | return false; |
| 1225 | } |
| 1226 | |
| 1227 | class ggml_webgpu_shader_lib { |
| 1228 | wgpu::Device device; |
| 1229 | pre_wgsl::Preprocessor preprocessor; |
| 1230 | |
| 1231 | std::unordered_map<int, webgpu_pipeline> sum_rows_pipelines; // key is fixed, no variants yet |
| 1232 | std::unordered_map<int, webgpu_pipeline> argmax_pipelines; // key is vec4 |
| 1233 | std::unordered_map<int, webgpu_pipeline> argsort_pipelines; // key is order |
| 1234 | std::unordered_map<int, webgpu_pipeline> argsort_merge_pipelines; // key is order |
| 1235 | std::unordered_map<int, webgpu_pipeline> cumsum_pipelines; // key is fixed, no variants yet |
| 1236 | std::unordered_map<int, webgpu_pipeline> arange_pipelines; // key is fixed, no variants yet |
| 1237 | std::unordered_map<ggml_webgpu_row_norm_pipeline_key, webgpu_pipeline, ggml_webgpu_row_norm_pipeline_key_hash> |
| 1238 | row_norm_pipelines; // op/inplace |
| 1239 | |
| 1240 | std::unordered_map<ggml_webgpu_get_rows_pipeline_key, webgpu_pipeline, ggml_webgpu_get_rows_pipeline_key_hash> |
| 1241 | get_rows_pipelines; // src_type, vectorized |
| 1242 | std::unordered_map<ggml_webgpu_unary_pipeline_key, webgpu_pipeline, ggml_webgpu_unary_pipeline_key_hash> |
| 1243 | unary_pipelines; // type/op/inplace |
| 1244 | std::unordered_map<ggml_webgpu_scale_pipeline_key, webgpu_pipeline, ggml_webgpu_scale_pipeline_key_hash> |
| 1245 | scale_pipelines; // inplace |
| 1246 | std::unordered_map<ggml_webgpu_solve_tri_pipeline_key, webgpu_pipeline, ggml_webgpu_solve_tri_pipeline_key_hash> |
| 1247 | solve_tri_pipelines; // type |
| 1248 | std::unordered_map<ggml_webgpu_ssm_conv_pipeline_key, webgpu_pipeline, ggml_webgpu_ssm_conv_pipeline_key_hash> |
| 1249 | ssm_conv_pipelines; // type/vectorized |
| 1250 | std::unordered_map<ggml_webgpu_ssm_scan_pipeline_key, webgpu_pipeline, ggml_webgpu_ssm_scan_pipeline_key_hash> |
| 1251 | ssm_scan_pipelines; // type/d_state |
| 1252 | std::unordered_map<ggml_webgpu_gated_delta_net_pipeline_key, |
| 1253 | webgpu_pipeline, |
| 1254 | ggml_webgpu_gated_delta_net_pipeline_key_hash> |
| 1255 | gated_delta_net_pipelines; // type/S_v/kda |
| 1256 | std::unordered_map<ggml_webgpu_pad_pipeline_key, webgpu_pipeline, ggml_webgpu_pad_pipeline_key_hash> |
| 1257 | pad_pipelines; // circular/non-circular |
| 1258 | std::unordered_map<ggml_webgpu_binary_pipeline_key, webgpu_pipeline, ggml_webgpu_binary_pipeline_key_hash> |
| 1259 | binary_pipelines; // type/op/inplace/overlap/src_overlap |
| 1260 | std::unordered_map<ggml_webgpu_add_id_pipeline_key, webgpu_pipeline, ggml_webgpu_add_id_pipeline_key_hash> |
| 1261 | add_id_pipelines; // inplace |
| 1262 | std::unordered_map<ggml_webgpu_concat_pipeline_key, webgpu_pipeline, ggml_webgpu_concat_pipeline_key_hash> |
| 1263 | concat_pipelines; // type |
| 1264 | std::unordered_map<ggml_webgpu_repeat_pipeline_key, webgpu_pipeline, ggml_webgpu_repeat_pipeline_key_hash> |
| 1265 | repeat_pipelines; // type |
| 1266 | std::unordered_map<ggml_webgpu_flash_attn_vec_pipeline_key, |
| 1267 | webgpu_pipeline, |
| 1268 | ggml_webgpu_flash_attn_vec_pipeline_key_hash> |
| 1269 | flash_attn_vec_pipelines; |
| 1270 | std::unordered_map<ggml_webgpu_flash_attn_pipeline_key, webgpu_pipeline, ggml_webgpu_flash_attn_pipeline_key_hash> |
| 1271 | flash_attn_pipelines; |
| 1272 | std::unordered_map<ggml_webgpu_flash_attn_vec_reduce_pipeline_key, |
| 1273 | webgpu_pipeline, |
| 1274 | ggml_webgpu_flash_attn_vec_reduce_pipeline_key_hash> |
| 1275 | flash_attn_vec_reduce_pipelines; |
| 1276 | std::unordered_map<ggml_webgpu_flash_attn_blk_pipeline_key, |
| 1277 | webgpu_pipeline, |
| 1278 | ggml_webgpu_flash_attn_blk_pipeline_key_hash> |
| 1279 | flash_attn_blk_pipelines; |
| 1280 | std::unordered_map<ggml_webgpu_mul_mat_vec_pipeline_key, webgpu_pipeline, ggml_webgpu_mul_mat_vec_pipeline_key_hash> |
| 1281 | mul_mat_vec_pipelines; // fast mat-vec (n==1) |
| 1282 | std::unordered_map<ggml_webgpu_mul_mat_pipeline_key, webgpu_pipeline, ggml_webgpu_mul_mat_pipeline_key_hash> |
| 1283 | mul_mat_fast_pipelines; // fast mat-mat (reg-tile or subgroup) |
| 1284 | std::unordered_map<ggml_webgpu_quantize_q8_pipeline_key, webgpu_pipeline, ggml_webgpu_quantize_q8_pipeline_key_hash> |
| 1285 | quantize_q8_pipelines; |
| 1286 | std::unordered_map<ggml_webgpu_mul_mat_id_gather_pipeline_key, |
| 1287 | webgpu_pipeline, |
| 1288 | ggml_webgpu_mul_mat_id_gather_pipeline_key_hash> |
| 1289 | mul_mat_id_gather_pipelines; // n_experts |
| 1290 | std::unordered_map<ggml_webgpu_mul_mat_id_pipeline_key, webgpu_pipeline, ggml_webgpu_mul_mat_id_pipeline_key_hash> |
| 1291 | mul_mat_id_pipelines; // src0_type/src1_type |
| 1292 | std::unordered_map<ggml_webgpu_mul_mat_id_pipeline_key, webgpu_pipeline, ggml_webgpu_mul_mat_id_pipeline_key_hash> |
| 1293 | mul_mat_id_vec_pipelines; // src0_type/src1_type |
| 1294 | |
| 1295 | std::unordered_map<ggml_webgpu_set_rows_pipeline_key, webgpu_pipeline, ggml_webgpu_set_rows_pipeline_key_hash> |
| 1296 | set_rows_pipelines; |
| 1297 | std::unordered_map<ggml_webgpu_set_pipeline_key, webgpu_pipeline, ggml_webgpu_set_pipeline_key_hash> set_pipelines; |
| 1298 | std::unordered_map<ggml_webgpu_cpy_pipeline_key, webgpu_pipeline, ggml_webgpu_cpy_pipeline_key_hash> cpy_pipelines; |
| 1299 | std::unordered_map<ggml_webgpu_glu_pipeline_key, webgpu_pipeline, ggml_webgpu_glu_pipeline_key_hash> glu_pipelines; |
| 1300 | std::unordered_map<ggml_webgpu_rope_pipeline_key, webgpu_pipeline, ggml_webgpu_rope_pipeline_key_hash> |
| 1301 | rope_pipelines; |
| 1302 | std::unordered_map<ggml_webgpu_soft_max_pipeline_key, webgpu_pipeline, ggml_webgpu_soft_max_pipeline_key_hash> |
| 1303 | soft_max_pipelines; |
| 1304 | std::unordered_map<ggml_webgpu_conv2d_pipeline_key, webgpu_pipeline, ggml_webgpu_conv2d_pipeline_key_hash> |
| 1305 | conv2d_pipelines; |
| 1306 | std::unordered_map<ggml_webgpu_conv2d_dw_pipeline_key, webgpu_pipeline, ggml_webgpu_conv2d_dw_pipeline_key_hash> |
| 1307 | conv2d_dw_pipelines; |
| 1308 | std::unordered_map<ggml_webgpu_im2col_pipeline_key, webgpu_pipeline, ggml_webgpu_im2col_pipeline_key_hash> |
| 1309 | im2col_pipelines; |
| 1310 | |
| 1311 | std::unordered_map<ggml_webgpu_rms_norm_mul_pipeline_key, |
| 1312 | webgpu_pipeline, |
| 1313 | ggml_webgpu_rms_norm_mul_pipeline_key_hash> |
| 1314 | rms_norm_mul_pipelines; |
| 1315 | std::unordered_map<ggml_webgpu_upscale_pipeline_key, webgpu_pipeline, ggml_webgpu_upscale_pipeline_key_hash> |
| 1316 | upscale_pipelines; |
| 1317 | |
| 1318 | public: |
| 1319 | ggml_webgpu_shader_lib(wgpu::Device device) { this->device = device; } |
| 1320 | |
| 1321 | webgpu_pipeline get_sum_rows_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 1322 | auto it = sum_rows_pipelines.find(1); |
| 1323 | if (it != sum_rows_pipelines.end()) { |
| 1324 | return it->second; |
| 1325 | } |
| 1326 | std::vector<std::string> defines; |
| 1327 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size)); |
| 1328 | |
| 1329 | auto processed = preprocessor.preprocess(wgsl_sum_rows, defines); |
| 1330 | sum_rows_pipelines[1] = ggml_webgpu_create_pipeline(device, processed, "sum_rows"); |
| 1331 | return sum_rows_pipelines[1]; |
| 1332 | } |
| 1333 | |
| 1334 | webgpu_pipeline get_row_norm_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 1335 | ggml_webgpu_row_norm_pipeline_key key = {}; |
| 1336 | key.op = context.dst->op; |
| 1337 | key.src_type = context.src0->type; |
| 1338 | key.dst_type = context.dst->type; |
| 1339 | key.inplace = ggml_webgpu_tensor_equal(context.src0, context.dst); |
| 1340 | |
| 1341 | auto it = row_norm_pipelines.find(key); |
| 1342 | if (it != row_norm_pipelines.end()) { |
| 1343 | return it->second; |
| 1344 | } |
| 1345 | std::vector<std::string> defines; |
| 1346 | std::string variant; |
| 1347 | |
| 1348 | switch (key.op) { |
| 1349 | case GGML_OP_RMS_NORM: |
| 1350 | defines.push_back("RMS_NORM"); |
| 1351 | variant = "rms_norm"; |
| 1352 | break; |
| 1353 | case GGML_OP_NORM: |
| 1354 | defines.push_back("NORM"); |
| 1355 | variant = "norm"; |
| 1356 | break; |
| 1357 | case GGML_OP_L2_NORM: |
| 1358 | defines.push_back("L2_NORM"); |
| 1359 | variant = "l2_norm"; |
| 1360 | break; |
| 1361 | default: |
| 1362 | GGML_ABORT("Unsupported op for row_norm shader"); |
| 1363 | } |
| 1364 | |
| 1365 | if (key.inplace) { |
| 1366 | defines.push_back("INPLACE"); |
| 1367 | variant += "_inplace"; |
| 1368 | } |
| 1369 | |
| 1370 | if (key.src_type == GGML_TYPE_F32) { |
| 1371 | defines.push_back("SRC_F32"); |
| 1372 | variant += "_src_f32"; |
| 1373 | } else if (key.src_type == GGML_TYPE_F16) { |
| 1374 | defines.push_back("SRC_F16"); |
| 1375 | variant += "_src_f16"; |
| 1376 | } |
| 1377 | |
| 1378 | if (key.dst_type == GGML_TYPE_F32) { |
| 1379 | defines.push_back("DST_F32"); |
| 1380 | variant += "_dst_f32"; |
| 1381 | } else if (key.dst_type == GGML_TYPE_F16) { |
| 1382 | defines.push_back("DST_F16"); |
| 1383 | variant += "_dst_f16"; |
| 1384 | } |
| 1385 | |
| 1386 | const uint32_t row_norm_wg_size = 128u; |
| 1387 | uint32_t wg_size = std::min(context.max_wg_size, row_norm_wg_size); |
| 1388 | defines.push_back(std::string("WG_SIZE=") + std::to_string(wg_size)); |
| 1389 | auto processed = preprocessor.preprocess(wgsl_row_norm, defines); |
| 1390 | auto decisions = std::make_shared<ggml_webgpu_generic_shader_decisions>(); |
| 1391 | decisions->wg_size = wg_size; |
| 1392 | decisions->inplace = key.inplace; |
| 1393 | row_norm_pipelines[key] = ggml_webgpu_create_pipeline(device, processed, variant); |
| 1394 | row_norm_pipelines[key].context = decisions; |
| 1395 | return row_norm_pipelines[key]; |
| 1396 | } |
| 1397 | |
| 1398 | webgpu_pipeline get_argmax_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 1399 | bool vec4 = context.src0->ne[0] % 4 == 0; |
| 1400 | |
| 1401 | auto it = argmax_pipelines.find(vec4); |
| 1402 | if (it != argmax_pipelines.end()) { |
| 1403 | return it->second; |
| 1404 | } |
| 1405 | std::string variant = "argmax"; |
| 1406 | std::vector<std::string> defines; |
| 1407 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size)); |
| 1408 | if (vec4) { |
| 1409 | defines.push_back("VEC4"); |
| 1410 | variant += "_vec4"; |
| 1411 | } |
| 1412 | |
| 1413 | auto processed = preprocessor.preprocess(wgsl_argmax, defines); |
| 1414 | argmax_pipelines[vec4] = ggml_webgpu_create_pipeline(device, processed, variant); |
| 1415 | return argmax_pipelines.at(vec4); |
| 1416 | } |
| 1417 | |
| 1418 | webgpu_pipeline get_set_rows_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 1419 | const bool quantized = ggml_is_quantized(context.dst->type); |
| 1420 | ggml_webgpu_set_rows_pipeline_key key = {}; |
| 1421 | key.src0_type = context.src0->type; |
| 1422 | key.dst_type = context.dst->type; |
| 1423 | key.vec4 = (context.dst->type == GGML_TYPE_F32 || context.dst->type == GGML_TYPE_F16) && |
| 1424 | context.src0->type == GGML_TYPE_F32 && context.src0->ne[0] % 4 == 0; |
| 1425 | key.i64_idx = context.src1->type == GGML_TYPE_I64; |
| 1426 | key.pair_blocks = quantized && ((context.src0->ne[0] / ggml_blck_size(context.dst->type)) % 2 == 0); |
| 1427 | |
| 1428 | auto it = set_rows_pipelines.find(key); |
| 1429 | if (it != set_rows_pipelines.end()) { |
| 1430 | return it->second; |
| 1431 | } |
| 1432 | |
| 1433 | std::vector<std::string> defines; |
| 1434 | std::string variant = "set_rows"; |
| 1435 | |
| 1436 | switch (context.dst->type) { |
| 1437 | case GGML_TYPE_F32: |
| 1438 | defines.push_back("DST_F32"); |
| 1439 | variant += "_dstf32"; |
| 1440 | break; |
| 1441 | case GGML_TYPE_F16: |
| 1442 | defines.push_back("DST_F16"); |
| 1443 | variant += "_dstf16"; |
| 1444 | break; |
| 1445 | case GGML_TYPE_Q8_0: |
| 1446 | defines.push_back("DST_Q8_0"); |
| 1447 | variant += "_dstq8_0"; |
| 1448 | break; |
| 1449 | case GGML_TYPE_Q4_0: |
| 1450 | defines.push_back("DST_Q4_0"); |
| 1451 | variant += "_dstq4_0"; |
| 1452 | break; |
| 1453 | default: |
| 1454 | GGML_ABORT("Unsupported dst type for set_rows shader"); |
| 1455 | } |
| 1456 | |
| 1457 | if (context.src0->type == GGML_TYPE_F16) { |
| 1458 | defines.push_back("TYPE_F16"); |
| 1459 | variant += "_src0_f16"; |
| 1460 | } |
| 1461 | |
| 1462 | if (key.vec4) { |
| 1463 | defines.push_back("VEC4"); |
| 1464 | variant += "_vec4"; |
| 1465 | } |
| 1466 | if (key.i64_idx) { |
| 1467 | defines.push_back("I64_IDX"); |
| 1468 | variant += "_i64idx"; |
| 1469 | } |
| 1470 | if (key.pair_blocks) { |
| 1471 | defines.push_back("PAIR_BLOCKS"); |
| 1472 | variant += "_pair_blocks"; |
| 1473 | } |
| 1474 | |
| 1475 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size)); |
| 1476 | |
| 1477 | const auto & shader_source = quantized ? wgsl_set_rows_quant : wgsl_set_rows; |
| 1478 | auto processed = preprocessor.preprocess(shader_source, defines); |
| 1479 | auto decisions = std::make_shared<ggml_webgpu_set_rows_shader_decisions>(); |
| 1480 | decisions->vec4 = key.vec4; |
| 1481 | decisions->i64_idx = key.i64_idx; |
| 1482 | decisions->pair_blocks = key.pair_blocks; |
| 1483 | decisions->wg_size = context.max_wg_size; |
| 1484 | set_rows_pipelines[key] = ggml_webgpu_create_pipeline(device, processed, variant); |
| 1485 | set_rows_pipelines[key].context = decisions; |
| 1486 | return set_rows_pipelines[key]; |
| 1487 | } |
| 1488 | |
| 1489 | webgpu_pipeline get_set_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 1490 | ggml_webgpu_set_pipeline_key key = {}; |
| 1491 | key.type = context.dst->type; |
| 1492 | key.inplace = ggml_webgpu_tensor_equal(context.src0, context.dst); |
| 1493 | |
| 1494 | auto it = set_pipelines.find(key); |
| 1495 | if (it != set_pipelines.end()) { |
| 1496 | return it->second; |
| 1497 | } |
| 1498 | |
| 1499 | std::vector<std::string> defines; |
| 1500 | std::string variant = "set"; |
| 1501 | |
| 1502 | switch (key.type) { |
| 1503 | case GGML_TYPE_F32: |
| 1504 | defines.push_back("TYPE_F32"); |
| 1505 | variant += "_f32"; |
| 1506 | break; |
| 1507 | case GGML_TYPE_I32: |
| 1508 | defines.push_back("TYPE_I32"); |
| 1509 | variant += "_i32"; |
| 1510 | break; |
| 1511 | default: |
| 1512 | GGML_ABORT("Unsupported type for set shader"); |
| 1513 | } |
| 1514 | |
| 1515 | if (key.inplace) { |
| 1516 | defines.push_back("INPLACE"); |
| 1517 | variant += "_inplace"; |
| 1518 | } |
| 1519 | |
| 1520 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size)); |
| 1521 | |
| 1522 | auto processed = preprocessor.preprocess(wgsl_set, defines); |
| 1523 | auto decisions = std::make_shared<ggml_webgpu_generic_shader_decisions>(); |
| 1524 | decisions->wg_size = context.max_wg_size; |
| 1525 | decisions->inplace = key.inplace; |
| 1526 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 1527 | pipeline.context = decisions; |
| 1528 | set_pipelines[key] = pipeline; |
| 1529 | return set_pipelines[key]; |
| 1530 | } |
| 1531 | |
| 1532 | webgpu_pipeline get_cumsum_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 1533 | auto it = cumsum_pipelines.find(1); |
| 1534 | if (it != cumsum_pipelines.end()) { |
| 1535 | return it->second; |
| 1536 | } |
| 1537 | |
| 1538 | std::vector<std::string> defines; |
| 1539 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size)); |
| 1540 | |
| 1541 | auto processed = preprocessor.preprocess(wgsl_cumsum, defines); |
| 1542 | cumsum_pipelines[1] = ggml_webgpu_create_pipeline(device, processed, "cumsum"); |
| 1543 | return cumsum_pipelines[1]; |
| 1544 | } |
| 1545 | |
| 1546 | webgpu_pipeline get_argsort_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 1547 | bool is_top_k = context.dst->op == GGML_OP_TOP_K; |
| 1548 | // ascending order is 0, descending order is 1 |
| 1549 | const int32_t order = |
| 1550 | is_top_k ? (int32_t) GGML_SORT_ORDER_DESC : (int32_t) ggml_get_op_params_i32(context.dst, 0); |
| 1551 | |
| 1552 | auto it = argsort_pipelines.find(order); |
| 1553 | if (it != argsort_pipelines.end()) { |
| 1554 | return it->second; |
| 1555 | } |
| 1556 | |
| 1557 | std::vector<std::string> defines; |
| 1558 | std::string variant = "argsort"; |
| 1559 | defines.push_back(std::string("ORDER=") + std::to_string(order)); |
| 1560 | variant += std::string("_order") + std::to_string(order); |
| 1561 | uint32_t wg_size = 1; |
| 1562 | while (wg_size * 2 <= context.max_wg_size && |
| 1563 | wg_size * GGML_WEBGPU_I32_SIZE_BYTES <= context.wg_mem_limit_bytes / 2) { |
| 1564 | wg_size *= 2; |
| 1565 | } |
| 1566 | defines.push_back(std::string("WG_SIZE=") + std::to_string(wg_size)); |
| 1567 | auto processed = preprocessor.preprocess(wgsl_argsort, defines); |
| 1568 | auto decisions = std::make_shared<ggml_webgpu_generic_shader_decisions>(); |
| 1569 | decisions->wg_size = wg_size; |
| 1570 | argsort_pipelines[order] = ggml_webgpu_create_pipeline(device, processed, variant); |
| 1571 | argsort_pipelines[order].context = decisions; |
| 1572 | return argsort_pipelines[order]; |
| 1573 | } |
| 1574 | |
| 1575 | webgpu_pipeline get_argsort_merge_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 1576 | bool is_top_k = context.dst->op == GGML_OP_TOP_K; |
| 1577 | // ascending order is 0, descending order is 1 |
| 1578 | const int32_t order = |
| 1579 | is_top_k ? (int32_t) GGML_SORT_ORDER_DESC : (int32_t) ggml_get_op_params_i32(context.dst, 0); |
| 1580 | |
| 1581 | auto it = argsort_merge_pipelines.find(order); |
| 1582 | if (it != argsort_merge_pipelines.end()) { |
| 1583 | return it->second; |
| 1584 | } |
| 1585 | |
| 1586 | std::vector<std::string> defines; |
| 1587 | std::string variant = "argsort_merge"; |
| 1588 | defines.push_back(std::string("ORDER=") + std::to_string(order)); |
| 1589 | variant += std::string("_order") + std::to_string(order); |
| 1590 | uint32_t wg_size = std::min(GGML_WEBGPU_ARGSORT_MERGE_MAX_WG_SIZE, context.max_wg_size); |
| 1591 | defines.push_back(std::string("WG_SIZE=") + std::to_string(wg_size)); |
| 1592 | |
| 1593 | auto processed = preprocessor.preprocess(wgsl_argsort_merge, defines); |
| 1594 | argsort_merge_pipelines[order] = ggml_webgpu_create_pipeline(device, processed, variant); |
| 1595 | return argsort_merge_pipelines[order]; |
| 1596 | } |
| 1597 | |
| 1598 | webgpu_pipeline get_get_rows_pipeline(const ggml_webgpu_shader_lib_context & context, bool vec4_aligned) { |
| 1599 | const bool vectorized = context.src0->type == GGML_TYPE_F32 && context.dst->ne[0] % 4 == 0 && vec4_aligned; |
| 1600 | ggml_webgpu_get_rows_pipeline_key key = {}; |
| 1601 | key.src_type = context.src0->type; |
| 1602 | key.vectorized = (int) vectorized; |
| 1603 | |
| 1604 | auto it = get_rows_pipelines.find(key); |
| 1605 | if (it != get_rows_pipelines.end()) { |
| 1606 | return it->second; |
| 1607 | } |
| 1608 | |
| 1609 | std::vector<std::string> defines; |
| 1610 | std::string variant = "get_rows"; |
| 1611 | |
| 1612 | const struct ggml_type_traits * type_traits = ggml_get_type_traits(key.src_type); |
| 1613 | const char * type_str = type_traits->type_name; |
| 1614 | |
| 1615 | switch (key.src_type) { |
| 1616 | case GGML_TYPE_F32: |
| 1617 | defines.push_back("FLOAT_PARALLEL"); |
| 1618 | if (key.vectorized) { |
| 1619 | defines.push_back("F32_VEC"); |
| 1620 | defines.push_back("SRC_TYPE=vec4<f32>"); |
| 1621 | defines.push_back("DST_TYPE=vec4<f32>"); |
| 1622 | defines.push_back("BLOCK_SIZE=4u"); |
| 1623 | } else { |
| 1624 | defines.push_back("F32"); |
| 1625 | defines.push_back("SRC_TYPE=f32"); |
| 1626 | defines.push_back("DST_TYPE=f32"); |
| 1627 | defines.push_back("BLOCK_SIZE=1u"); |
| 1628 | } |
| 1629 | variant += "_f32"; |
| 1630 | break; |
| 1631 | case GGML_TYPE_F16: |
| 1632 | defines.push_back("FLOAT_PARALLEL"); |
| 1633 | defines.push_back("F16"); |
| 1634 | defines.push_back("SRC_TYPE=f16"); |
| 1635 | defines.push_back("DST_TYPE=f32"); |
| 1636 | defines.push_back("BLOCK_SIZE=1u"); |
| 1637 | variant += "_f16"; |
| 1638 | break; |
| 1639 | case GGML_TYPE_I32: |
| 1640 | defines.push_back("FLOAT_PARALLEL"); |
| 1641 | defines.push_back("I32"); |
| 1642 | defines.push_back("SRC_TYPE=i32"); |
| 1643 | defines.push_back("DST_TYPE=i32"); |
| 1644 | defines.push_back("BLOCK_SIZE=1u"); |
| 1645 | variant += "_i32"; |
| 1646 | break; |
| 1647 | case GGML_TYPE_BF16: |
| 1648 | defines.push_back("BF16"); |
| 1649 | defines.push_back("SRC_TYPE=u32"); |
| 1650 | defines.push_back("DST_TYPE=f32"); |
| 1651 | defines.push_back("BLOCK_SIZE=1u"); |
| 1652 | variant += "_bf16"; |
| 1653 | break; |
| 1654 | default: |
| 1655 | { |
| 1656 | std::string type_upper = type_str; |
| 1657 | std::transform(type_upper.begin(), type_upper.end(), type_upper.begin(), ::toupper); |
| 1658 | |
| 1659 | switch (key.src_type) { |
| 1660 | case GGML_TYPE_Q1_0: |
| 1661 | case GGML_TYPE_Q2_0: |
| 1662 | case GGML_TYPE_Q4_0: |
| 1663 | case GGML_TYPE_Q5_0: |
| 1664 | case GGML_TYPE_Q8_0: |
| 1665 | case GGML_TYPE_Q3_K: |
| 1666 | case GGML_TYPE_Q6_K: |
| 1667 | case GGML_TYPE_IQ2_XXS: |
| 1668 | case GGML_TYPE_IQ2_XS: |
| 1669 | case GGML_TYPE_IQ2_S: |
| 1670 | case GGML_TYPE_IQ3_XXS: |
| 1671 | case GGML_TYPE_IQ3_S: |
| 1672 | case GGML_TYPE_IQ1_S: |
| 1673 | case GGML_TYPE_IQ4_NL: |
| 1674 | case GGML_TYPE_MXFP4: |
| 1675 | case GGML_TYPE_NVFP4: |
| 1676 | { |
| 1677 | // Quantized types using u32 buffers for portability. |
| 1678 | defines.push_back("SRC_TYPE=u32"); |
| 1679 | defines.push_back("U32_DEQUANT_HELPERS"); |
| 1680 | break; |
| 1681 | } |
| 1682 | default: |
| 1683 | { |
| 1684 | defines.push_back(std::string("SRC_TYPE=") + type_str); |
| 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | defines.push_back("BYTE_HELPERS"); |
| 1689 | defines.push_back(type_upper + "_T"); |
| 1690 | defines.push_back(type_upper); |
| 1691 | defines.push_back(type_upper + "_SCALE_MIN"); |
| 1692 | defines.push_back(type_upper + "_TABLES"); |
| 1693 | defines.push_back(type_upper + "_GRID"); |
| 1694 | defines.push_back(type_upper + "_LUT"); |
| 1695 | |
| 1696 | variant += "_"; |
| 1697 | variant += type_str; |
| 1698 | |
| 1699 | defines.push_back("DST_TYPE=f32"); |
| 1700 | |
| 1701 | if (key.src_type == GGML_TYPE_Q1_0) { |
| 1702 | defines.push_back("BLOCK_SIZE=128u"); |
| 1703 | } else if ((key.src_type >= GGML_TYPE_Q4_0 && key.src_type <= GGML_TYPE_Q8_1) || |
| 1704 | key.src_type == GGML_TYPE_IQ4_NL || key.src_type == GGML_TYPE_MXFP4) { |
| 1705 | defines.push_back("BLOCK_SIZE=32u"); |
| 1706 | } else if (key.src_type == GGML_TYPE_Q2_0 || key.src_type == GGML_TYPE_NVFP4) { |
| 1707 | defines.push_back("BLOCK_SIZE=64u"); |
| 1708 | } else if (key.src_type >= GGML_TYPE_Q2_K) { |
| 1709 | defines.push_back("BLOCK_SIZE=256u"); |
| 1710 | } else { |
| 1711 | defines.push_back("BLOCK_SIZE=1u"); |
| 1712 | } |
| 1713 | break; |
| 1714 | } |
| 1715 | } |
| 1716 | |
| 1717 | if (key.vectorized) { |
| 1718 | variant += "_vec"; |
| 1719 | } |
| 1720 | |
| 1721 | defines.push_back("WG_SIZE=" + std::to_string(context.max_wg_size)); |
| 1722 | |
| 1723 | auto processed = preprocessor.preprocess(wgsl_get_rows, defines); |
| 1724 | auto decisions = std::make_shared<ggml_webgpu_get_rows_shader_decisions>(); |
| 1725 | decisions->wg_size = context.max_wg_size; |
| 1726 | decisions->vectorized = vectorized; |
| 1727 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 1728 | pipeline.context = decisions; |
| 1729 | get_rows_pipelines[key] = pipeline; |
| 1730 | return get_rows_pipelines[key]; |
| 1731 | } |
| 1732 | |
| 1733 | webgpu_pipeline get_scale_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 1734 | ggml_webgpu_scale_pipeline_key key = {}; |
| 1735 | key.inplace = ggml_webgpu_tensor_equal(context.src0, context.dst); |
| 1736 | |
| 1737 | auto it = scale_pipelines.find(key); |
| 1738 | if (it != scale_pipelines.end()) { |
| 1739 | return it->second; |
| 1740 | } |
| 1741 | |
| 1742 | std::vector<std::string> defines; |
| 1743 | std::string variant = "scale"; |
| 1744 | |
| 1745 | if (key.inplace) { |
| 1746 | defines.push_back("INPLACE"); |
| 1747 | variant += "_inplace"; |
| 1748 | } |
| 1749 | |
| 1750 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size)); |
| 1751 | |
| 1752 | auto processed = preprocessor.preprocess(wgsl_scale, defines); |
| 1753 | auto decisions = std::make_shared<ggml_webgpu_generic_shader_decisions>(); |
| 1754 | decisions->wg_size = context.max_wg_size; |
| 1755 | decisions->inplace = key.inplace; |
| 1756 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 1757 | pipeline.context = decisions; |
| 1758 | scale_pipelines[key] = pipeline; |
| 1759 | return scale_pipelines[key]; |
| 1760 | } |
| 1761 | |
| 1762 | webgpu_pipeline get_solve_tri_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 1763 | ggml_webgpu_solve_tri_pipeline_key key = {}; |
| 1764 | key.type = context.dst->type; |
| 1765 | key.n = (int) context.src0->ne[0]; |
| 1766 | key.k = (int) context.src1->ne[0]; |
| 1767 | |
| 1768 | auto it = solve_tri_pipelines.find(key); |
| 1769 | if (it != solve_tri_pipelines.end()) { |
| 1770 | return it->second; |
| 1771 | } |
| 1772 | |
| 1773 | std::vector<std::string> defines; |
| 1774 | std::string variant = "solve_tri"; |
| 1775 | |
| 1776 | switch (key.type) { |
| 1777 | case GGML_TYPE_F32: |
| 1778 | variant += "_f32"; |
| 1779 | break; |
| 1780 | default: |
| 1781 | GGML_ABORT("Unsupported type for solve_tri shader"); |
| 1782 | } |
| 1783 | |
| 1784 | const uint32_t wg_size = std::min((uint32_t) key.n, context.max_wg_size); |
| 1785 | const uint32_t k_tile = wg_size; |
| 1786 | const uint32_t bytes_per_row = ((uint32_t) key.n + wg_size) * GGML_WEBGPU_F32_SIZE_BYTES; |
| 1787 | const uint32_t batch_n = (uint32_t) (context.wg_mem_limit_bytes / bytes_per_row); |
| 1788 | |
| 1789 | defines.push_back(std::string("N=") + std::to_string(key.n)); |
| 1790 | defines.push_back(std::string("WG_SIZE=") + std::to_string(wg_size)); |
| 1791 | defines.push_back(std::string("K_TILE=") + std::to_string(k_tile)); |
| 1792 | defines.push_back(std::string("BATCH_N=") + std::to_string(batch_n)); |
| 1793 | |
| 1794 | auto processed = preprocessor.preprocess(wgsl_solve_tri, defines); |
| 1795 | auto decisions = std::make_shared<ggml_webgpu_generic_shader_decisions>(); |
| 1796 | decisions->wg_size = wg_size; |
| 1797 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 1798 | pipeline.context = decisions; |
| 1799 | solve_tri_pipelines[key] = pipeline; |
| 1800 | return solve_tri_pipelines[key]; |
| 1801 | } |
| 1802 | |
| 1803 | webgpu_pipeline get_ssm_conv_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 1804 | ggml_webgpu_ssm_conv_pipeline_key key = {}; |
| 1805 | key.type = context.dst->type; |
| 1806 | key.vectorized = context.src1->ne[0] == 4; |
| 1807 | |
| 1808 | auto it = ssm_conv_pipelines.find(key); |
| 1809 | if (it != ssm_conv_pipelines.end()) { |
| 1810 | return it->second; |
| 1811 | } |
| 1812 | |
| 1813 | std::vector<std::string> defines; |
| 1814 | std::string variant = "ssm_conv"; |
| 1815 | |
| 1816 | switch (key.type) { |
| 1817 | case GGML_TYPE_F32: |
| 1818 | variant += "_f32"; |
| 1819 | break; |
| 1820 | default: |
| 1821 | GGML_ABORT("Unsupported type for ssm_conv shader"); |
| 1822 | } |
| 1823 | |
| 1824 | if (key.vectorized) { |
| 1825 | defines.push_back("VECTORIZED"); |
| 1826 | variant += "_vec4"; |
| 1827 | } |
| 1828 | |
| 1829 | constexpr uint32_t block_size = 32u; |
| 1830 | constexpr uint32_t tokens_per_wg = 8u; |
| 1831 | |
| 1832 | defines.push_back("BLOCK_SIZE=" + std::to_string(block_size) + "u"); |
| 1833 | defines.push_back("TOKENS_PER_WG=" + std::to_string(tokens_per_wg) + "u"); |
| 1834 | |
| 1835 | auto processed = preprocessor.preprocess(wgsl_ssm_conv, defines); |
| 1836 | auto decisions = std::make_shared<ggml_webgpu_ssm_conv_shader_decisions>(); |
| 1837 | decisions->block_size = block_size; |
| 1838 | decisions->tokens_per_wg = tokens_per_wg; |
| 1839 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 1840 | pipeline.context = decisions; |
| 1841 | ssm_conv_pipelines[key] = pipeline; |
| 1842 | return ssm_conv_pipelines[key]; |
| 1843 | } |
| 1844 | |
| 1845 | webgpu_pipeline get_ssm_scan_pipeline(const ggml_webgpu_shader_lib_context & context, uint8_t xbc_overlap) { |
| 1846 | ggml_webgpu_ssm_scan_pipeline_key key = {}; |
| 1847 | key.type = context.dst->type; |
| 1848 | key.d_state = (int) context.src0->ne[0]; |
| 1849 | key.xbc_overlap = xbc_overlap; |
| 1850 | |
| 1851 | auto it = ssm_scan_pipelines.find(key); |
| 1852 | if (it != ssm_scan_pipelines.end()) { |
| 1853 | return it->second; |
| 1854 | } |
| 1855 | |
| 1856 | std::vector<std::string> defines; |
| 1857 | std::string variant = "ssm_scan"; |
| 1858 | |
| 1859 | switch (key.type) { |
| 1860 | case GGML_TYPE_F32: |
| 1861 | variant += "_f32"; |
| 1862 | break; |
| 1863 | default: |
| 1864 | GGML_ABORT("Unsupported type for ssm_scan shader"); |
| 1865 | } |
| 1866 | |
| 1867 | const uint32_t wg_size = (uint32_t) key.d_state; |
| 1868 | |
| 1869 | constexpr uint32_t tokens_per_tile = 4u; |
| 1870 | |
| 1871 | defines.push_back("WG_SIZE=" + std::to_string(wg_size) + "u"); |
| 1872 | defines.push_back("TOKENS_PER_TILE=" + std::to_string(tokens_per_tile) + "u"); |
| 1873 | |
| 1874 | if (context.supports_subgroups) { |
| 1875 | defines.push_back("USE_SUBGROUP_REDUCTION"); |
| 1876 | variant += "_sg_reduce"; |
| 1877 | } else { |
| 1878 | variant += "_wg_reduce"; |
| 1879 | } |
| 1880 | |
| 1881 | if (key.xbc_overlap == 0b110) { // x/B |
| 1882 | defines.push_back("XB_OVERLAP"); |
| 1883 | variant += "_xb_overlap"; |
| 1884 | } else if (key.xbc_overlap == 0b011) { // B/C |
| 1885 | defines.push_back("BC_OVERLAP"); |
| 1886 | variant += "_bc_overlap"; |
| 1887 | } else if (key.xbc_overlap == 0b111) { // x/B/C |
| 1888 | defines.push_back("XBC_OVERLAP"); |
| 1889 | variant += "_xbc_overlap"; |
| 1890 | } |
| 1891 | |
| 1892 | variant += "_d" + std::to_string(key.d_state); |
| 1893 | |
| 1894 | auto processed = preprocessor.preprocess(wgsl_ssm_scan, defines); |
| 1895 | auto decisions = std::make_shared<ggml_webgpu_ssm_scan_shader_decisions>(); |
| 1896 | decisions->wg_size = wg_size; |
| 1897 | decisions->tokens_per_tile = tokens_per_tile; |
| 1898 | decisions->xbc_overlap = key.xbc_overlap; |
| 1899 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 1900 | pipeline.context = decisions; |
| 1901 | ssm_scan_pipelines[key] = pipeline; |
| 1902 | return ssm_scan_pipelines[key]; |
| 1903 | } |
| 1904 | |
| 1905 | webgpu_pipeline get_gated_delta_net_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 1906 | ggml_webgpu_gated_delta_net_pipeline_key key = {}; |
| 1907 | key.type = context.dst->type; |
| 1908 | key.s_v = (int) context.src2->ne[0]; |
| 1909 | key.kda = context.src3->ne[0] == context.src2->ne[0]; |
| 1910 | key.fused_cache = context.dst_fuse != nullptr; |
| 1911 | |
| 1912 | auto it = gated_delta_net_pipelines.find(key); |
| 1913 | if (it != gated_delta_net_pipelines.end()) { |
| 1914 | return it->second; |
| 1915 | } |
| 1916 | |
| 1917 | std::vector<std::string> defines; |
| 1918 | std::string variant = "gated_delta_net"; |
| 1919 | |
| 1920 | switch (key.type) { |
| 1921 | case GGML_TYPE_F32: |
| 1922 | variant += "_f32"; |
| 1923 | break; |
| 1924 | default: |
| 1925 | GGML_ABORT("Unsupported type for gated_delta_net shader"); |
| 1926 | } |
| 1927 | |
| 1928 | if (key.kda) { |
| 1929 | defines.push_back("KDA"); |
| 1930 | variant += "_kda"; |
| 1931 | } |
| 1932 | |
| 1933 | if (key.fused_cache) { |
| 1934 | defines.push_back("FUSED_CACHE"); |
| 1935 | variant += "_fused_cache"; |
| 1936 | } |
| 1937 | |
| 1938 | defines.push_back("S_V=" + std::to_string(key.s_v) + "u"); |
| 1939 | defines.push_back("WG_SIZE=" + std::to_string(key.s_v) + "u"); |
| 1940 | |
| 1941 | auto processed = preprocessor.preprocess(wgsl_gated_delta_net, defines); |
| 1942 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 1943 | gated_delta_net_pipelines[key] = pipeline; |
| 1944 | return gated_delta_net_pipelines[key]; |
| 1945 | } |
| 1946 | |
| 1947 | webgpu_pipeline get_arange_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 1948 | auto it = arange_pipelines.find(1); |
| 1949 | if (it != arange_pipelines.end()) { |
| 1950 | return it->second; |
| 1951 | } |
| 1952 | |
| 1953 | std::vector<std::string> defines; |
| 1954 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size)); |
| 1955 | |
| 1956 | auto processed = preprocessor.preprocess(wgsl_arange, defines); |
| 1957 | auto decisions = std::make_shared<ggml_webgpu_generic_shader_decisions>(); |
| 1958 | decisions->wg_size = context.max_wg_size; |
| 1959 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, "arange"); |
| 1960 | pipeline.context = decisions; |
| 1961 | arange_pipelines[1] = pipeline; |
| 1962 | return arange_pipelines[1]; |
| 1963 | } |
| 1964 | |
| 1965 | webgpu_pipeline get_pad_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 1966 | ggml_webgpu_pad_pipeline_key key = {}; |
| 1967 | key.circular = ggml_get_op_params_i32(context.dst, 8) != 0; |
| 1968 | |
| 1969 | auto it = pad_pipelines.find(key); |
| 1970 | if (it != pad_pipelines.end()) { |
| 1971 | return it->second; |
| 1972 | } |
| 1973 | |
| 1974 | std::vector<std::string> defines; |
| 1975 | std::string variant = "pad"; |
| 1976 | |
| 1977 | if (key.circular) { |
| 1978 | defines.push_back("CIRCULAR"); |
| 1979 | variant += "_circular"; |
| 1980 | } |
| 1981 | |
| 1982 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size)); |
| 1983 | |
| 1984 | auto processed = preprocessor.preprocess(wgsl_pad, defines); |
| 1985 | auto decisions = std::make_shared<ggml_webgpu_generic_shader_decisions>(); |
| 1986 | decisions->wg_size = context.max_wg_size; |
| 1987 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 1988 | pipeline.context = decisions; |
| 1989 | pad_pipelines[key] = pipeline; |
| 1990 | return pad_pipelines[key]; |
| 1991 | } |
| 1992 | |
| 1993 | webgpu_pipeline get_quantize_q8_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 1994 | ggml_webgpu_quantize_q8_pipeline_key key = {}; |
| 1995 | key.src0_type = context.src0->type; |
| 1996 | |
| 1997 | auto it = quantize_q8_pipelines.find(key); |
| 1998 | if (it != quantize_q8_pipelines.end()) { |
| 1999 | return it->second; |
| 2000 | } |
| 2001 | const char * shader_src = wgsl_quantize_q8; |
| 2002 | std::vector<std::string> defines; |
| 2003 | std::string variant = "quantize_q8"; |
| 2004 | |
| 2005 | uint32_t wg_size = WEBGPU_MUL_MAT_VEC_WG_SIZE; |
| 2006 | |
| 2007 | defines.push_back("SRC1_INNER_TYPE=f32"); |
| 2008 | defines.push_back(std::string("WG_SIZE=") + std::to_string(wg_size)); |
| 2009 | |
| 2010 | const struct ggml_type_traits * src0_traits = ggml_get_type_traits(context.src0->type); |
| 2011 | std::string src0_name = src0_traits->type_name; |
| 2012 | std::string type_upper = src0_name; |
| 2013 | variant += "_" + src0_name; |
| 2014 | std::transform(type_upper.begin(), type_upper.end(), type_upper.begin(), ::toupper); |
| 2015 | |
| 2016 | defines.push_back("MUL_ACC_" + type_upper); |
| 2017 | defines.push_back("Q8_1_T"); |
| 2018 | |
| 2019 | defines.push_back(context.supports_subgroups ? "USE_SUBGROUP_REDUCTION" : "USE_WORKGROUP_REDUCTION"); |
| 2020 | variant += context.supports_subgroups ? "_sg_reduce" : "_wg_reduce"; |
| 2021 | |
| 2022 | auto processed = preprocessor.preprocess(shader_src, defines); |
| 2023 | auto decisions = std::make_shared<ggml_webgpu_generic_shader_decisions>(); |
| 2024 | decisions->wg_size = wg_size; |
| 2025 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 2026 | pipeline.context = decisions; |
| 2027 | quantize_q8_pipelines[key] = pipeline; |
| 2028 | return quantize_q8_pipelines[key]; |
| 2029 | } |
| 2030 | |
| 2031 | webgpu_pipeline get_mul_mat_vec_pipeline(const ggml_webgpu_shader_lib_context & context, bool src_overlap) { |
| 2032 | ggml_webgpu_mul_mat_vec_pipeline_key key = {}; |
| 2033 | key.src0_type = context.src0->type; |
| 2034 | key.src1_type = context.src1->type; |
| 2035 | key.vectorized = (context.src0->ne[0] % 4 == 0 && |
| 2036 | (context.src0->type == GGML_TYPE_F32 || context.src0->type == GGML_TYPE_F16)) ? |
| 2037 | 1 : |
| 2038 | 0; |
| 2039 | key.num_cols = context.dst->ne[1]; |
| 2040 | key.use_mmvq = ggml_webgpu_can_use_mmvq(context.src0, context.src1, context.src1->ne[1], |
| 2041 | context.supports_dot_product, context.vendor); |
| 2042 | key.src_overlap = src_overlap; |
| 2043 | |
| 2044 | auto it = mul_mat_vec_pipelines.find(key); |
| 2045 | if (it != mul_mat_vec_pipelines.end()) { |
| 2046 | return it->second; |
| 2047 | } |
| 2048 | |
| 2049 | std::vector<std::string> defines; |
| 2050 | std::string variant = "mul_mat_vec"; |
| 2051 | const char * shader_src = wgsl_mul_mat_vec; |
| 2052 | |
| 2053 | // src0 type (matrix row) |
| 2054 | switch (context.src0->type) { |
| 2055 | case GGML_TYPE_F32: |
| 2056 | defines.push_back("SRC0_INNER_TYPE=f32"); |
| 2057 | defines.push_back("MUL_ACC_FLOAT"); |
| 2058 | defines.push_back("TYPE_F32"); |
| 2059 | variant += "_f32"; |
| 2060 | break; |
| 2061 | case GGML_TYPE_F16: |
| 2062 | defines.push_back("SRC0_INNER_TYPE=f16"); |
| 2063 | defines.push_back("MUL_ACC_FLOAT"); |
| 2064 | defines.push_back("TYPE_F16"); |
| 2065 | variant += "_f16"; |
| 2066 | break; |
| 2067 | case GGML_TYPE_BF16: |
| 2068 | defines.push_back("SRC0_INNER_TYPE=u32"); |
| 2069 | defines.push_back("MUL_ACC_FLOAT"); |
| 2070 | defines.push_back("TYPE_BF16"); |
| 2071 | variant += "_bf16"; |
| 2072 | break; |
| 2073 | default: |
| 2074 | { |
| 2075 | // Quantized types: use helpers but accumulate in f16 |
| 2076 | const struct ggml_type_traits * src0_traits = ggml_get_type_traits(context.src0->type); |
| 2077 | std::string src0_name = src0_traits->type_name; |
| 2078 | std::string type_upper = src0_name; |
| 2079 | variant += "_" + src0_name; |
| 2080 | std::transform(type_upper.begin(), type_upper.end(), type_upper.begin(), ::toupper); |
| 2081 | |
| 2082 | defines.push_back("BYTE_HELPERS"); |
| 2083 | defines.push_back("MUL_ACC_" + type_upper); |
| 2084 | defines.push_back("U32_DEQUANT_HELPERS"); |
| 2085 | defines.push_back("SRC0_INNER_TYPE=u32"); |
| 2086 | switch (context.src0->type) { |
| 2087 | case GGML_TYPE_Q4_0: |
| 2088 | case GGML_TYPE_Q4_1: |
| 2089 | case GGML_TYPE_Q5_0: |
| 2090 | case GGML_TYPE_Q5_1: |
| 2091 | case GGML_TYPE_Q8_0: |
| 2092 | if (key.use_mmvq) { |
| 2093 | defines.push_back("LEGACY_QUANTS_HANDLING"); |
| 2094 | } |
| 2095 | break; |
| 2096 | case GGML_TYPE_Q1_0: |
| 2097 | case GGML_TYPE_Q2_K: |
| 2098 | case GGML_TYPE_Q3_K: |
| 2099 | case GGML_TYPE_Q4_K: |
| 2100 | case GGML_TYPE_Q5_K: |
| 2101 | case GGML_TYPE_Q6_K: |
| 2102 | if (key.use_mmvq) { |
| 2103 | defines.push_back("K_QUANTS_HANDLING"); |
| 2104 | } |
| 2105 | break; |
| 2106 | case GGML_TYPE_IQ1_S: |
| 2107 | case GGML_TYPE_IQ1_M: |
| 2108 | case GGML_TYPE_IQ2_S: |
| 2109 | case GGML_TYPE_IQ3_S: |
| 2110 | case GGML_TYPE_IQ4_NL: |
| 2111 | case GGML_TYPE_IQ4_XS: |
| 2112 | defines.push_back(type_upper + "_GRID"); |
| 2113 | break; |
| 2114 | case GGML_TYPE_IQ2_XXS: |
| 2115 | case GGML_TYPE_IQ2_XS: |
| 2116 | case GGML_TYPE_IQ3_XXS: |
| 2117 | defines.push_back(type_upper + "_GRID"); |
| 2118 | defines.push_back(type_upper + "_TABLES"); |
| 2119 | break; |
| 2120 | case GGML_TYPE_MXFP4: |
| 2121 | defines.push_back(type_upper + "_LUT"); |
| 2122 | if (key.use_mmvq) { |
| 2123 | defines.push_back("LEGACY_QUANTS_HANDLING"); |
| 2124 | } |
| 2125 | break; |
| 2126 | case GGML_TYPE_NVFP4: |
| 2127 | defines.push_back(type_upper + "_LUT"); |
| 2128 | break; |
| 2129 | default: |
| 2130 | break; |
| 2131 | } |
| 2132 | break; |
| 2133 | } |
| 2134 | } |
| 2135 | |
| 2136 | // src1 type (vector) |
| 2137 | switch (context.src1->type) { |
| 2138 | case GGML_TYPE_F32: |
| 2139 | defines.push_back("SRC1_INNER_TYPE=f32"); |
| 2140 | variant += "_f32"; |
| 2141 | break; |
| 2142 | case GGML_TYPE_F16: |
| 2143 | defines.push_back("SRC1_INNER_TYPE=f16"); |
| 2144 | variant += "_f16"; |
| 2145 | break; |
| 2146 | default: |
| 2147 | GGML_ABORT("Unsupported src1 type for mul_mat_vec shader"); |
| 2148 | } |
| 2149 | |
| 2150 | // VEC/SCALAR controls |
| 2151 | defines.push_back(key.vectorized ? "VEC" : "SCALAR"); |
| 2152 | |
| 2153 | uint32_t wg_size = WEBGPU_MUL_MAT_VEC_WG_SIZE; |
| 2154 | uint32_t outputs_per_wg = WEBGPU_MUL_MAT_VEC_FLOAT_OUTPUTS_PER_WG; |
| 2155 | |
| 2156 | if (key.src0_type == GGML_TYPE_Q1_0) { |
| 2157 | outputs_per_wg = WEBGPU_MUL_MAT_VEC_LEGACY_Q_OUTPUTS_PER_WG; |
| 2158 | } else if (key.src0_type >= GGML_TYPE_Q2_K) { |
| 2159 | outputs_per_wg = WEBGPU_MUL_MAT_VEC_K_Q_OUTPUTS_PER_WG; |
| 2160 | } else if (key.src0_type >= GGML_TYPE_Q4_0) { |
| 2161 | outputs_per_wg = WEBGPU_MUL_MAT_VEC_LEGACY_Q_OUTPUTS_PER_WG; |
| 2162 | } |
| 2163 | |
| 2164 | if (key.use_mmvq) { |
| 2165 | defines.push_back("MMVQ"); |
| 2166 | defines.push_back("Q8_1_T"); |
| 2167 | } |
| 2168 | |
| 2169 | if (key.src_overlap) { |
| 2170 | defines.push_back("SRC_OVERLAP"); |
| 2171 | variant += "_src_overlap"; |
| 2172 | } |
| 2173 | |
| 2174 | defines.push_back(std::string("WG_SIZE=") + std::to_string(wg_size)); |
| 2175 | defines.push_back(std::string("OUTPUTS_PER_WG=") + std::to_string(outputs_per_wg)); |
| 2176 | defines.push_back(context.supports_subgroups ? "USE_SUBGROUP_REDUCTION" : "USE_WORKGROUP_REDUCTION"); |
| 2177 | variant += context.supports_subgroups ? "_sg_reduce" : "_wg_reduce"; |
| 2178 | if (key.vectorized) { |
| 2179 | variant += "_vectorized"; |
| 2180 | } |
| 2181 | defines.push_back(std::string("NUM_COLS=") + std::to_string(key.num_cols)); |
| 2182 | |
| 2183 | auto processed = preprocessor.preprocess(shader_src, defines); |
| 2184 | auto decisions = std::make_shared<ggml_webgpu_mul_mat_vec_shader_decisions>(); |
| 2185 | decisions->wg_size = wg_size; |
| 2186 | decisions->outputs_per_wg = outputs_per_wg; |
| 2187 | decisions->vec_size = key.vectorized ? 4 : 1; |
| 2188 | |
| 2189 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 2190 | pipeline.context = decisions; |
| 2191 | mul_mat_vec_pipelines[key] = pipeline; |
| 2192 | return mul_mat_vec_pipelines[key]; |
| 2193 | } |
| 2194 | |
| 2195 | webgpu_pipeline get_mul_mat_fast_pipeline(const ggml_webgpu_shader_lib_context & context, bool src_overlap) { |
| 2196 | ggml_webgpu_mul_mat_pipeline_key key = {}; |
| 2197 | key.src0_type = context.src0->type; |
| 2198 | key.src1_type = context.src1->type; |
| 2199 | key.vectorized = (context.src0->ne[0] % 4 == 0 && context.dst->ne[0] % 4 == 0 && |
| 2200 | (context.src0->type == GGML_TYPE_F32 || context.src0->type == GGML_TYPE_F16)) ? |
| 2201 | 1 : |
| 2202 | 0; |
| 2203 | key.use_subgroup_matrix = context.supports_subgroup_matrix; |
| 2204 | key.src_overlap = src_overlap; |
| 2205 | |
| 2206 | auto it = mul_mat_fast_pipelines.find(key); |
| 2207 | if (it != mul_mat_fast_pipelines.end()) { |
| 2208 | return it->second; |
| 2209 | } |
| 2210 | |
| 2211 | const char * shader_src = key.use_subgroup_matrix ? wgsl_mul_mat_subgroup_matrix : wgsl_mul_mat_reg_tile; |
| 2212 | std::vector<std::string> defines; |
| 2213 | std::string variant = key.use_subgroup_matrix ? "mul_mat_subgroup_matrix" : "mul_mat_reg_tile"; |
| 2214 | |
| 2215 | // src1 type |
| 2216 | switch (context.src1->type) { |
| 2217 | case GGML_TYPE_F32: |
| 2218 | defines.push_back("SRC1_INNER_TYPE=f32"); |
| 2219 | break; |
| 2220 | case GGML_TYPE_F16: |
| 2221 | defines.push_back("SRC1_INNER_TYPE=f16"); |
| 2222 | break; |
| 2223 | default: |
| 2224 | GGML_ABORT("Unsupported src1 type for mul_mat fast shader"); |
| 2225 | } |
| 2226 | |
| 2227 | // src0 type |
| 2228 | const struct ggml_type_traits * src0_traits = ggml_get_type_traits(context.src0->type); |
| 2229 | const char * src0_name = src0_traits->type_name; |
| 2230 | |
| 2231 | switch (context.src0->type) { |
| 2232 | case GGML_TYPE_F32: |
| 2233 | defines.push_back("SRC0_INNER_TYPE=f32"); |
| 2234 | defines.push_back("TYPE_F32"); |
| 2235 | defines.push_back("MUL_ACC_FLOAT"); |
| 2236 | defines.push_back("INIT_SRC0_SHMEM_FLOAT"); |
| 2237 | defines.push_back("INIT_SRC1_SHMEM_FLOAT"); |
| 2238 | variant += "_f32"; |
| 2239 | break; |
| 2240 | case GGML_TYPE_F16: |
| 2241 | defines.push_back("SRC0_INNER_TYPE=f16"); |
| 2242 | defines.push_back("TYPE_F16"); |
| 2243 | defines.push_back("MUL_ACC_FLOAT"); |
| 2244 | defines.push_back("INIT_SRC0_SHMEM_FLOAT"); |
| 2245 | defines.push_back("INIT_SRC1_SHMEM_FLOAT"); |
| 2246 | variant += "_f16"; |
| 2247 | break; |
| 2248 | case GGML_TYPE_BF16: |
| 2249 | defines.push_back("SRC0_INNER_TYPE=u32"); |
| 2250 | defines.push_back("TYPE_BF16"); |
| 2251 | defines.push_back("MUL_ACC_FLOAT"); |
| 2252 | defines.push_back("INIT_SRC0_SHMEM_FLOAT"); |
| 2253 | defines.push_back("INIT_SRC1_SHMEM_FLOAT"); |
| 2254 | variant += "_bf16"; |
| 2255 | break; |
| 2256 | default: |
| 2257 | { |
| 2258 | std::string type_upper = src0_name; |
| 2259 | std::transform(type_upper.begin(), type_upper.end(), type_upper.begin(), ::toupper); |
| 2260 | |
| 2261 | defines.push_back("BYTE_HELPERS"); |
| 2262 | defines.push_back("MUL_ACC_" + type_upper); |
| 2263 | defines.push_back("INIT_SRC0_SHMEM_" + type_upper); |
| 2264 | defines.push_back("INIT_SRC1_SHMEM_FLOAT"); |
| 2265 | defines.push_back("U32_DEQUANT_HELPERS"); |
| 2266 | defines.push_back("SRC0_INNER_TYPE=u32"); |
| 2267 | |
| 2268 | switch (context.src0->type) { |
| 2269 | case GGML_TYPE_IQ1_S: |
| 2270 | case GGML_TYPE_IQ1_M: |
| 2271 | case GGML_TYPE_IQ4_NL: |
| 2272 | case GGML_TYPE_IQ4_XS: |
| 2273 | defines.push_back(type_upper + "_GRID"); |
| 2274 | break; |
| 2275 | case GGML_TYPE_IQ2_XXS: |
| 2276 | case GGML_TYPE_IQ2_XS: |
| 2277 | case GGML_TYPE_IQ2_S: |
| 2278 | case GGML_TYPE_IQ3_XXS: |
| 2279 | case GGML_TYPE_IQ3_S: |
| 2280 | defines.push_back(type_upper + "_GRID"); |
| 2281 | defines.push_back(type_upper + "_TABLES"); |
| 2282 | break; |
| 2283 | case GGML_TYPE_MXFP4: |
| 2284 | case GGML_TYPE_NVFP4: |
| 2285 | defines.push_back(type_upper + "_LUT"); |
| 2286 | break; |
| 2287 | default: |
| 2288 | break; |
| 2289 | } |
| 2290 | |
| 2291 | variant += std::string("_") + src0_name; |
| 2292 | break; |
| 2293 | } |
| 2294 | } |
| 2295 | |
| 2296 | // VEC/SCALAR controls |
| 2297 | defines.push_back(key.vectorized ? "VEC" : "SCALAR"); |
| 2298 | |
| 2299 | const bool is_quant = ggml_is_quantized(context.src0->type); |
| 2300 | |
| 2301 | uint32_t tile_k; |
| 2302 | if (key.use_subgroup_matrix) { |
| 2303 | tile_k = is_quant ? WEBGPU_MUL_MAT_SUBGROUP_TILE_K_QUANT : WEBGPU_MUL_MAT_SUBGROUP_TILE_K_FLOAT; |
| 2304 | } else { |
| 2305 | tile_k = is_quant ? WEBGPU_MUL_MAT_REG_TILE_K_QUANT : WEBGPU_MUL_MAT_REG_TILE_K_FLOAT; |
| 2306 | } |
| 2307 | |
| 2308 | // Tiles |
| 2309 | defines.push_back("TILE_M=" + std::to_string(WEBGPU_MUL_MAT_TILE_M) + "u"); |
| 2310 | defines.push_back("TILE_N=" + std::to_string(WEBGPU_MUL_MAT_TILE_N) + "u"); |
| 2311 | |
| 2312 | // Subgroup matrix specifics |
| 2313 | if (key.use_subgroup_matrix) { |
| 2314 | defines.push_back("TILE_K=" + std::to_string(tile_k) + "u"); |
| 2315 | defines.push_back("MAX_SUBGROUP_SIZE=" + std::to_string(context.max_subgroup_size) + "u"); |
| 2316 | defines.push_back("SUBGROUP_M=" + std::to_string(WEBGPU_MUL_MAT_SUBGROUP_M) + "u"); |
| 2317 | defines.push_back("SUBGROUP_N=" + std::to_string(WEBGPU_MUL_MAT_SUBGROUP_N) + "u"); |
| 2318 | defines.push_back("SUBGROUP_MATRIX_M=" + std::to_string(WEBGPU_MUL_MAT_SUBGROUP_MATRIX_M) + "u"); |
| 2319 | defines.push_back("SUBGROUP_MATRIX_N=" + std::to_string(WEBGPU_MUL_MAT_SUBGROUP_MATRIX_N) + "u"); |
| 2320 | defines.push_back("SUBGROUP_MATRIX_M_SIZE=" + std::to_string(context.sg_mat_m) + "u"); |
| 2321 | defines.push_back("SUBGROUP_MATRIX_N_SIZE=" + std::to_string(context.sg_mat_n) + "u"); |
| 2322 | defines.push_back("SUBGROUP_MATRIX_K_SIZE=" + std::to_string(context.sg_mat_k) + "u"); |
| 2323 | } |
| 2324 | |
| 2325 | // variant suffix for src1 type |
| 2326 | variant += std::string("_") + (context.src1->type == GGML_TYPE_F32 ? "f32" : "f16"); |
| 2327 | if (key.vectorized) { |
| 2328 | variant += "_vectorized"; |
| 2329 | } |
| 2330 | |
| 2331 | if (key.src_overlap) { |
| 2332 | defines.push_back("SRC_OVERLAP"); |
| 2333 | variant += "_src_overlap"; |
| 2334 | } |
| 2335 | |
| 2336 | if (!key.use_subgroup_matrix) { |
| 2337 | defines.push_back("WORKGROUP_SIZE_M=" + std::to_string(WEBGPU_MUL_MAT_WG_SIZE_M) + "u"); |
| 2338 | defines.push_back("WORKGROUP_SIZE_N=" + std::to_string(WEBGPU_MUL_MAT_WG_SIZE_N) + "u"); |
| 2339 | defines.push_back("TILE_K=" + std::to_string(tile_k) + "u"); |
| 2340 | } |
| 2341 | |
| 2342 | auto processed = preprocessor.preprocess(shader_src, defines); |
| 2343 | |
| 2344 | auto decisions = std::make_shared<ggml_webgpu_mul_mat_shader_decisions>(); |
| 2345 | decisions->tile_k = tile_k; |
| 2346 | decisions->tile_m = WEBGPU_MUL_MAT_TILE_M; |
| 2347 | decisions->tile_n = WEBGPU_MUL_MAT_TILE_N; |
| 2348 | decisions->use_subgroup_matrix = key.use_subgroup_matrix; |
| 2349 | if (key.use_subgroup_matrix) { |
| 2350 | decisions->subgroup_m = WEBGPU_MUL_MAT_SUBGROUP_M; |
| 2351 | decisions->subgroup_n = WEBGPU_MUL_MAT_SUBGROUP_N; |
| 2352 | decisions->subgroup_matrix_m = WEBGPU_MUL_MAT_SUBGROUP_MATRIX_M; |
| 2353 | decisions->subgroup_matrix_n = WEBGPU_MUL_MAT_SUBGROUP_MATRIX_N; |
| 2354 | decisions->wg_size = context.max_subgroup_size; |
| 2355 | } else { |
| 2356 | decisions->wg_size_m = WEBGPU_MUL_MAT_WG_SIZE_M; |
| 2357 | decisions->wg_size_n = WEBGPU_MUL_MAT_WG_SIZE_N; |
| 2358 | decisions->wg_size = WEBGPU_MUL_MAT_WG_SIZE_M * WEBGPU_MUL_MAT_WG_SIZE_N; |
| 2359 | decisions->mul_mat_wg_size = WEBGPU_MUL_MAT_WG_SIZE; |
| 2360 | } |
| 2361 | |
| 2362 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 2363 | pipeline.context = decisions; |
| 2364 | mul_mat_fast_pipelines[key] = pipeline; |
| 2365 | return mul_mat_fast_pipelines[key]; |
| 2366 | } |
| 2367 | |
| 2368 | webgpu_pipeline get_mul_mat_id_gather_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 2369 | ggml_webgpu_mul_mat_id_gather_pipeline_key key = {}; |
| 2370 | key.n_experts = (uint32_t) context.src0->ne[2]; |
| 2371 | |
| 2372 | auto it = mul_mat_id_gather_pipelines.find(key); |
| 2373 | if (it != mul_mat_id_gather_pipelines.end()) { |
| 2374 | return it->second; |
| 2375 | } |
| 2376 | std::vector<std::string> defines; |
| 2377 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size) + "u"); |
| 2378 | defines.push_back(std::string("N_EXPERT=") + std::to_string(key.n_experts) + "u"); |
| 2379 | defines.push_back(std::string("TILE_N_SLOTS=") + |
| 2380 | std::to_string(WEBGPU_MUL_MAT_TILE_N * WEBGPU_MUL_MAT_WG_SIZE_N) + "u"); |
| 2381 | |
| 2382 | auto processed = preprocessor.preprocess(wgsl_mul_mat_id_gather, defines); |
| 2383 | auto decisions = std::make_shared<ggml_webgpu_generic_shader_decisions>(); |
| 2384 | decisions->wg_size = context.max_wg_size; |
| 2385 | |
| 2386 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, "mul_mat_id_gather"); |
| 2387 | pipeline.context = decisions; |
| 2388 | mul_mat_id_gather_pipelines[key] = pipeline; |
| 2389 | return pipeline; |
| 2390 | } |
| 2391 | |
| 2392 | webgpu_pipeline get_mul_mat_id_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 2393 | ggml_webgpu_mul_mat_id_pipeline_key key = {}; |
| 2394 | key.src0_type = context.src0->type; |
| 2395 | key.src1_type = context.src1->type; |
| 2396 | key.n_experts = context.src0->ne[2]; |
| 2397 | key.vectorized = (context.src0->ne[0] % 4 == 0 && context.src0->ne[1] % 4 == 0 && |
| 2398 | (context.src0->type == GGML_TYPE_F32 || context.src0->type == GGML_TYPE_F16)) ? |
| 2399 | 1 : |
| 2400 | 0; |
| 2401 | |
| 2402 | auto it = mul_mat_id_pipelines.find(key); |
| 2403 | if (it != mul_mat_id_pipelines.end()) { |
| 2404 | return it->second; |
| 2405 | } |
| 2406 | |
| 2407 | std::vector<std::string> defines; |
| 2408 | std::string variant = "mul_mat_id"; |
| 2409 | defines.push_back("MUL_MAT_ID"); |
| 2410 | |
| 2411 | // src1 type |
| 2412 | switch (context.src1->type) { |
| 2413 | case GGML_TYPE_F32: |
| 2414 | defines.push_back("SRC1_INNER_TYPE=f32"); |
| 2415 | break; |
| 2416 | case GGML_TYPE_F16: |
| 2417 | defines.push_back("SRC1_INNER_TYPE=f16"); |
| 2418 | break; |
| 2419 | default: |
| 2420 | GGML_ABORT("Unsupported src1 type for mul_mat fast shader"); |
| 2421 | } |
| 2422 | |
| 2423 | // src0 type |
| 2424 | const struct ggml_type_traits * src0_traits = ggml_get_type_traits(context.src0->type); |
| 2425 | const char * src0_name = src0_traits->type_name; |
| 2426 | |
| 2427 | switch (context.src0->type) { |
| 2428 | case GGML_TYPE_F32: |
| 2429 | defines.push_back("SRC0_INNER_TYPE=f32"); |
| 2430 | defines.push_back("INIT_SRC0_SHMEM_FLOAT"); |
| 2431 | defines.push_back("INIT_SRC1_SHMEM_FLOAT"); |
| 2432 | defines.push_back("TYPE_F32"); |
| 2433 | variant += "_f32"; |
| 2434 | break; |
| 2435 | case GGML_TYPE_F16: |
| 2436 | defines.push_back("SRC0_INNER_TYPE=f16"); |
| 2437 | defines.push_back("INIT_SRC0_SHMEM_FLOAT"); |
| 2438 | defines.push_back("INIT_SRC1_SHMEM_FLOAT"); |
| 2439 | defines.push_back("TYPE_F16"); |
| 2440 | variant += "_f16"; |
| 2441 | break; |
| 2442 | case GGML_TYPE_BF16: |
| 2443 | defines.push_back("SRC0_INNER_TYPE=u32"); |
| 2444 | defines.push_back("INIT_SRC0_SHMEM_FLOAT"); |
| 2445 | defines.push_back("INIT_SRC1_SHMEM_FLOAT"); |
| 2446 | defines.push_back("TYPE_BF16"); |
| 2447 | variant += "_bf16"; |
| 2448 | break; |
| 2449 | default: |
| 2450 | { |
| 2451 | std::string type_upper = src0_name; |
| 2452 | std::transform(type_upper.begin(), type_upper.end(), type_upper.begin(), ::toupper); |
| 2453 | |
| 2454 | defines.push_back("BYTE_HELPERS"); |
| 2455 | defines.push_back("INIT_SRC0_SHMEM_" + type_upper); |
| 2456 | defines.push_back("INIT_SRC1_SHMEM_FLOAT"); |
| 2457 | defines.push_back("U32_DEQUANT_HELPERS"); |
| 2458 | defines.push_back("SRC0_INNER_TYPE=u32"); |
| 2459 | |
| 2460 | switch (context.src0->type) { |
| 2461 | case GGML_TYPE_IQ1_S: |
| 2462 | case GGML_TYPE_IQ1_M: |
| 2463 | case GGML_TYPE_IQ4_NL: |
| 2464 | case GGML_TYPE_IQ4_XS: |
| 2465 | defines.push_back(type_upper + "_GRID"); |
| 2466 | break; |
| 2467 | case GGML_TYPE_IQ2_XXS: |
| 2468 | case GGML_TYPE_IQ2_XS: |
| 2469 | case GGML_TYPE_IQ2_S: |
| 2470 | case GGML_TYPE_IQ3_XXS: |
| 2471 | case GGML_TYPE_IQ3_S: |
| 2472 | defines.push_back(type_upper + "_GRID"); |
| 2473 | defines.push_back(type_upper + "_TABLES"); |
| 2474 | break; |
| 2475 | case GGML_TYPE_MXFP4: |
| 2476 | case GGML_TYPE_NVFP4: |
| 2477 | defines.push_back(type_upper + "_LUT"); |
| 2478 | break; |
| 2479 | default: |
| 2480 | break; |
| 2481 | } |
| 2482 | |
| 2483 | variant += std::string("_") + src0_name; |
| 2484 | break; |
| 2485 | } |
| 2486 | } |
| 2487 | |
| 2488 | // VEC/SCALAR controls |
| 2489 | defines.push_back(key.vectorized ? "VEC" : "SCALAR"); |
| 2490 | |
| 2491 | // mul_mat_id is register-tile only. |
| 2492 | const uint32_t tile_k = |
| 2493 | ggml_is_quantized(context.src0->type) ? WEBGPU_MUL_MAT_REG_TILE_K_QUANT : WEBGPU_MUL_MAT_REG_TILE_K_FLOAT; |
| 2494 | |
| 2495 | // Tiles |
| 2496 | defines.push_back("TILE_M=" + std::to_string(WEBGPU_MUL_MAT_TILE_M) + "u"); |
| 2497 | defines.push_back("TILE_N=" + std::to_string(WEBGPU_MUL_MAT_TILE_N) + "u"); |
| 2498 | defines.push_back("TILE_K=" + std::to_string(tile_k) + "u"); |
| 2499 | |
| 2500 | defines.push_back("WORKGROUP_SIZE_M=" + std::to_string(WEBGPU_MUL_MAT_WG_SIZE_M) + "u"); |
| 2501 | defines.push_back("WORKGROUP_SIZE_N=" + std::to_string(WEBGPU_MUL_MAT_WG_SIZE_N) + "u"); |
| 2502 | |
| 2503 | // variant suffix for src1 type |
| 2504 | variant += std::string("_") + (context.src1->type == GGML_TYPE_F32 ? "f32" : "f16"); |
| 2505 | if (key.vectorized) { |
| 2506 | variant += "_vectorized"; |
| 2507 | } |
| 2508 | |
| 2509 | auto processed = preprocessor.preprocess(wgsl_mul_mat_id, defines); |
| 2510 | |
| 2511 | auto decisions = std::make_shared<ggml_webgpu_mul_mat_shader_decisions>(); |
| 2512 | decisions->tile_k = tile_k; |
| 2513 | decisions->tile_m = WEBGPU_MUL_MAT_TILE_M; |
| 2514 | decisions->tile_n = WEBGPU_MUL_MAT_TILE_N; |
| 2515 | decisions->wg_size_m = WEBGPU_MUL_MAT_WG_SIZE_M; |
| 2516 | decisions->wg_size_n = WEBGPU_MUL_MAT_WG_SIZE_N; |
| 2517 | decisions->wg_size = WEBGPU_MUL_MAT_WG_SIZE_M * WEBGPU_MUL_MAT_WG_SIZE_N; |
| 2518 | |
| 2519 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 2520 | pipeline.context = decisions; |
| 2521 | mul_mat_id_pipelines[key] = pipeline; |
| 2522 | return mul_mat_id_pipelines[key]; |
| 2523 | } |
| 2524 | |
| 2525 | webgpu_pipeline get_mul_mat_id_vec_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 2526 | ggml_webgpu_mul_mat_id_pipeline_key key = {}; |
| 2527 | key.src0_type = context.src0->type; |
| 2528 | key.src1_type = context.src1->type; |
| 2529 | key.n_experts = context.src0->ne[2]; |
| 2530 | key.vectorized = (context.src0->ne[0] % 4 == 0 && |
| 2531 | (context.src0->type == GGML_TYPE_F32 || context.src0->type == GGML_TYPE_F16)) ? |
| 2532 | 1 : |
| 2533 | 0; |
| 2534 | |
| 2535 | auto it = mul_mat_id_vec_pipelines.find(key); |
| 2536 | if (it != mul_mat_id_vec_pipelines.end()) { |
| 2537 | return it->second; |
| 2538 | } |
| 2539 | |
| 2540 | std::vector<std::string> defines; |
| 2541 | std::string variant = "mul_mat_id_vec"; |
| 2542 | const char * shader_src = wgsl_mul_mat_id_vec; |
| 2543 | |
| 2544 | // src1 type |
| 2545 | switch (context.src1->type) { |
| 2546 | case GGML_TYPE_F32: |
| 2547 | defines.push_back("SRC1_INNER_TYPE=f32"); |
| 2548 | break; |
| 2549 | case GGML_TYPE_F16: |
| 2550 | defines.push_back("SRC1_INNER_TYPE=f16"); |
| 2551 | break; |
| 2552 | default: |
| 2553 | GGML_ABORT("Unsupported src1 type for mul_mat fast shader"); |
| 2554 | } |
| 2555 | |
| 2556 | // src0 type |
| 2557 | switch (context.src0->type) { |
| 2558 | case GGML_TYPE_F32: |
| 2559 | defines.push_back("SRC0_INNER_TYPE=f32"); |
| 2560 | defines.push_back("MUL_ACC_FLOAT"); |
| 2561 | defines.push_back("TYPE_F32"); |
| 2562 | variant += "_f32"; |
| 2563 | break; |
| 2564 | case GGML_TYPE_F16: |
| 2565 | defines.push_back("SRC0_INNER_TYPE=f16"); |
| 2566 | defines.push_back("MUL_ACC_FLOAT"); |
| 2567 | defines.push_back("TYPE_F16"); |
| 2568 | variant += "_f16"; |
| 2569 | break; |
| 2570 | case GGML_TYPE_BF16: |
| 2571 | defines.push_back("SRC0_INNER_TYPE=u32"); |
| 2572 | defines.push_back("MUL_ACC_FLOAT"); |
| 2573 | defines.push_back("TYPE_BF16"); |
| 2574 | variant += "_bf16"; |
| 2575 | break; |
| 2576 | default: |
| 2577 | { |
| 2578 | // Quantized types: use helpers but accumulate in f16 |
| 2579 | const struct ggml_type_traits * src0_traits = ggml_get_type_traits(context.src0->type); |
| 2580 | std::string src0_name = src0_traits->type_name; |
| 2581 | std::string type_upper = src0_name; |
| 2582 | variant += "_" + src0_name; |
| 2583 | std::transform(type_upper.begin(), type_upper.end(), type_upper.begin(), ::toupper); |
| 2584 | |
| 2585 | defines.push_back("BYTE_HELPERS"); |
| 2586 | defines.push_back("MUL_ACC_" + type_upper); |
| 2587 | defines.push_back("U32_DEQUANT_HELPERS"); |
| 2588 | defines.push_back("SRC0_INNER_TYPE=u32"); |
| 2589 | switch (context.src0->type) { |
| 2590 | case GGML_TYPE_IQ1_S: |
| 2591 | case GGML_TYPE_IQ1_M: |
| 2592 | case GGML_TYPE_IQ2_S: |
| 2593 | case GGML_TYPE_IQ3_S: |
| 2594 | case GGML_TYPE_IQ4_NL: |
| 2595 | case GGML_TYPE_IQ4_XS: |
| 2596 | defines.push_back(type_upper + "_GRID"); |
| 2597 | break; |
| 2598 | case GGML_TYPE_IQ2_XXS: |
| 2599 | case GGML_TYPE_IQ2_XS: |
| 2600 | case GGML_TYPE_IQ3_XXS: |
| 2601 | defines.push_back(type_upper + "_GRID"); |
| 2602 | defines.push_back(type_upper + "_TABLES"); |
| 2603 | break; |
| 2604 | case GGML_TYPE_MXFP4: |
| 2605 | case GGML_TYPE_NVFP4: |
| 2606 | defines.push_back(type_upper + "_LUT"); |
| 2607 | break; |
| 2608 | default: |
| 2609 | break; |
| 2610 | } |
| 2611 | break; |
| 2612 | } |
| 2613 | } |
| 2614 | |
| 2615 | // VEC/SCALAR controls |
| 2616 | defines.push_back(key.vectorized ? "VEC" : "SCALAR"); |
| 2617 | |
| 2618 | uint32_t wg_size = WEBGPU_MUL_MAT_VEC_WG_SIZE; |
| 2619 | uint32_t outputs_per_wg = WEBGPU_MUL_MAT_VEC_FLOAT_OUTPUTS_PER_WG; |
| 2620 | |
| 2621 | if (key.src0_type == GGML_TYPE_Q1_0) { |
| 2622 | outputs_per_wg = WEBGPU_MUL_MAT_VEC_LEGACY_Q_OUTPUTS_PER_WG; |
| 2623 | } else if (key.src0_type >= GGML_TYPE_Q2_K) { |
| 2624 | outputs_per_wg = WEBGPU_MUL_MAT_VEC_K_Q_OUTPUTS_PER_WG; |
| 2625 | } else if (key.src0_type >= GGML_TYPE_Q4_0) { |
| 2626 | outputs_per_wg = WEBGPU_MUL_MAT_VEC_LEGACY_Q_OUTPUTS_PER_WG; |
| 2627 | } |
| 2628 | |
| 2629 | // variant suffix for src1 type |
| 2630 | variant += std::string("_") + (context.src1->type == GGML_TYPE_F32 ? "f32" : "f16"); |
| 2631 | |
| 2632 | defines.push_back(std::string("WG_SIZE=") + std::to_string(wg_size)); |
| 2633 | defines.push_back(std::string("OUTPUTS_PER_WG=") + std::to_string(outputs_per_wg)); |
| 2634 | defines.push_back(context.supports_subgroups ? "USE_SUBGROUP_REDUCTION" : "USE_WORKGROUP_REDUCTION"); |
| 2635 | variant += context.supports_subgroups ? "_sg_reduce" : "_wg_reduce"; |
| 2636 | if (key.vectorized) { |
| 2637 | variant += "_vectorized"; |
| 2638 | } |
| 2639 | defines.push_back(std::string("NUM_COLS=1")); |
| 2640 | |
| 2641 | defines.push_back(std::string("N_EXPERTS=") + std::to_string(key.n_experts)); |
| 2642 | |
| 2643 | auto processed = preprocessor.preprocess(shader_src, defines); |
| 2644 | |
| 2645 | auto decisions = std::make_shared<ggml_webgpu_mul_mat_vec_shader_decisions>(); |
| 2646 | decisions->wg_size = wg_size; |
| 2647 | decisions->outputs_per_wg = outputs_per_wg; |
| 2648 | |
| 2649 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 2650 | pipeline.context = decisions; |
| 2651 | mul_mat_id_vec_pipelines[key] = pipeline; |
| 2652 | return mul_mat_id_vec_pipelines[key]; |
| 2653 | } |
| 2654 | |
| 2655 | webgpu_pipeline get_unary_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 2656 | const bool is_unary = context.dst->op == GGML_OP_UNARY; |
| 2657 | const int op = is_unary ? (int) ggml_get_unary_op(context.dst) : context.dst->op; |
| 2658 | ggml_webgpu_unary_pipeline_key key = {}; |
| 2659 | key.type = context.dst->type; |
| 2660 | key.op = op; |
| 2661 | key.is_unary = is_unary; |
| 2662 | key.inplace = ggml_webgpu_tensor_equal(context.src0, context.dst) || context.dst->op == GGML_OP_FILL; |
| 2663 | key.ttype = (ggml_tri_type) ggml_get_op_params_i32(context.dst, 0); |
| 2664 | |
| 2665 | auto it = unary_pipelines.find(key); |
| 2666 | if (it != unary_pipelines.end()) { |
| 2667 | return it->second; |
| 2668 | } |
| 2669 | |
| 2670 | std::vector<std::string> defines; |
| 2671 | std::string variant = |
| 2672 | key.is_unary ? ggml_unary_op_name((ggml_unary_op) key.op) : ggml_op_name((ggml_op) key.op); |
| 2673 | defines.push_back(variant); |
| 2674 | |
| 2675 | switch (key.type) { |
| 2676 | case GGML_TYPE_F32: |
| 2677 | defines.push_back("TYPE_F32"); |
| 2678 | variant += "_f32"; |
| 2679 | break; |
| 2680 | case GGML_TYPE_F16: |
| 2681 | defines.push_back("TYPE_F16"); |
| 2682 | variant += "_f16"; |
| 2683 | break; |
| 2684 | default: |
| 2685 | GGML_ABORT("Unsupported type for unary shader"); |
| 2686 | } |
| 2687 | |
| 2688 | if (key.inplace) { |
| 2689 | defines.push_back("INPLACE"); |
| 2690 | variant += "_inplace"; |
| 2691 | } |
| 2692 | |
| 2693 | if (op == GGML_OP_TRI) { |
| 2694 | switch (key.ttype) { |
| 2695 | case GGML_TRI_TYPE_LOWER: |
| 2696 | defines.push_back("TRI_TYPE_LOWER"); |
| 2697 | variant += "_tri_type_lower"; |
| 2698 | break; |
| 2699 | case GGML_TRI_TYPE_LOWER_DIAG: |
| 2700 | defines.push_back("TRI_TYPE_LOWER_DIAG"); |
| 2701 | variant += "_tri_type_lower_diag"; |
| 2702 | break; |
| 2703 | case GGML_TRI_TYPE_UPPER: |
| 2704 | defines.push_back("TRI_TYPE_UPPER"); |
| 2705 | variant += "_tri_type_upper"; |
| 2706 | break; |
| 2707 | case GGML_TRI_TYPE_UPPER_DIAG: |
| 2708 | defines.push_back("TRI_TYPE_UPPER_DIAG"); |
| 2709 | variant += "_tri_upper_diag"; |
| 2710 | break; |
| 2711 | default: |
| 2712 | GGML_ABORT("Unsupported ggml_tri_type for unary shader"); |
| 2713 | } |
| 2714 | } |
| 2715 | |
| 2716 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size)); |
| 2717 | |
| 2718 | auto processed = preprocessor.preprocess(wgsl_unary, defines); |
| 2719 | auto decisions = std::make_shared<ggml_webgpu_generic_shader_decisions>(); |
| 2720 | decisions->wg_size = context.max_wg_size; |
| 2721 | decisions->inplace = key.inplace; |
| 2722 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 2723 | pipeline.context = decisions; |
| 2724 | unary_pipelines[key] = pipeline; |
| 2725 | return unary_pipelines[key]; |
| 2726 | } |
| 2727 | |
| 2728 | webgpu_pipeline get_rms_norm_mul_pipeline(const ggml_webgpu_shader_lib_context & context, bool src_overlap) { |
| 2729 | ggml_webgpu_rms_norm_mul_pipeline_key key = {}; |
| 2730 | key.inplace = ggml_webgpu_tensor_equal(context.src0, context.dst); |
| 2731 | key.overlap = ggml_webgpu_tensor_equal(context.src1, context.dst); |
| 2732 | key.src_overlap = src_overlap; |
| 2733 | |
| 2734 | auto it = rms_norm_mul_pipelines.find(key); |
| 2735 | if (it != rms_norm_mul_pipelines.end()) { |
| 2736 | return it->second; |
| 2737 | } |
| 2738 | |
| 2739 | std::vector<std::string> defines; |
| 2740 | std::string op_name = "RMS_NORM_MUL"; |
| 2741 | std::string variant = op_name; |
| 2742 | |
| 2743 | if (key.inplace) { |
| 2744 | defines.push_back("INPLACE"); |
| 2745 | variant += "_inplace"; |
| 2746 | } else if (key.overlap) { |
| 2747 | defines.push_back("OVERLAP"); |
| 2748 | variant += "_overlap"; |
| 2749 | } else if (key.src_overlap) { |
| 2750 | defines.push_back("SRC_OVERLAP"); |
| 2751 | variant += "_src_overlap"; |
| 2752 | } |
| 2753 | |
| 2754 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size)); |
| 2755 | |
| 2756 | auto processed = preprocessor.preprocess(wgsl_rms_norm_mul, defines); |
| 2757 | auto pipeline_decisions = std::make_shared<ggml_webgpu_rms_norm_mul_shader_decisions>(); |
| 2758 | pipeline_decisions->wg_size = context.max_wg_size; |
| 2759 | pipeline_decisions->inplace = key.inplace; |
| 2760 | pipeline_decisions->overlap = key.overlap; |
| 2761 | pipeline_decisions->src_overlap = key.src_overlap; |
| 2762 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 2763 | pipeline.context = pipeline_decisions; |
| 2764 | rms_norm_mul_pipelines[key] = pipeline; |
| 2765 | return rms_norm_mul_pipelines[key]; |
| 2766 | } |
| 2767 | |
| 2768 | webgpu_pipeline get_binary_pipeline(const ggml_webgpu_shader_lib_context & context, bool src_overlap) { |
| 2769 | ggml_webgpu_binary_pipeline_key key = {}; |
| 2770 | key.type = context.dst->type; |
| 2771 | key.op = context.dst->op; |
| 2772 | key.inplace = ggml_webgpu_tensor_equal(context.src0, context.dst); |
| 2773 | key.overlap = ggml_webgpu_tensor_equal(context.src1, context.dst); |
| 2774 | key.src_overlap = src_overlap; |
| 2775 | |
| 2776 | auto it = binary_pipelines.find(key); |
| 2777 | if (it != binary_pipelines.end()) { |
| 2778 | return it->second; |
| 2779 | } |
| 2780 | |
| 2781 | std::vector<std::string> defines; |
| 2782 | std::string op_name = ggml_op_name((ggml_op) key.op); |
| 2783 | std::string variant = op_name; |
| 2784 | |
| 2785 | defines.push_back(std::string("OP_") + op_name); |
| 2786 | |
| 2787 | switch (key.type) { |
| 2788 | case GGML_TYPE_F32: |
| 2789 | defines.push_back("TYPE_F32"); |
| 2790 | variant += "_f32"; |
| 2791 | break; |
| 2792 | case GGML_TYPE_F16: |
| 2793 | defines.push_back("TYPE_F16"); |
| 2794 | variant += "_f16"; |
| 2795 | break; |
| 2796 | default: |
| 2797 | GGML_ABORT("Unsupported type for binary shader"); |
| 2798 | } |
| 2799 | |
| 2800 | if (key.inplace) { |
| 2801 | defines.push_back("INPLACE"); |
| 2802 | variant += "_inplace"; |
| 2803 | } else if (key.overlap) { |
| 2804 | defines.push_back("OVERLAP"); |
| 2805 | variant += "_overlap"; |
| 2806 | } else if (key.src_overlap) { |
| 2807 | defines.push_back("SRC_OVERLAP"); |
| 2808 | variant += "_src_overlap"; |
| 2809 | } |
| 2810 | |
| 2811 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size)); |
| 2812 | |
| 2813 | auto processed = preprocessor.preprocess(wgsl_binary, defines); |
| 2814 | auto pipeline_decisions = std::make_shared<ggml_webgpu_binary_shader_decisions>(); |
| 2815 | pipeline_decisions->wg_size = context.max_wg_size; |
| 2816 | pipeline_decisions->inplace = key.inplace; |
| 2817 | pipeline_decisions->overlap = key.overlap; |
| 2818 | pipeline_decisions->src_overlap = key.src_overlap; |
| 2819 | |
| 2820 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 2821 | pipeline.context = pipeline_decisions; |
| 2822 | binary_pipelines[key] = pipeline; |
| 2823 | return binary_pipelines[key]; |
| 2824 | } |
| 2825 | |
| 2826 | webgpu_pipeline get_add_id_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 2827 | ggml_webgpu_add_id_pipeline_key key = {}; |
| 2828 | key.inplace = ggml_webgpu_tensor_equal(context.src0, context.dst); |
| 2829 | |
| 2830 | auto it = add_id_pipelines.find(key); |
| 2831 | if (it != add_id_pipelines.end()) { |
| 2832 | return it->second; |
| 2833 | } |
| 2834 | |
| 2835 | std::vector<std::string> defines; |
| 2836 | std::string variant = "add_id"; |
| 2837 | const char * shader_src = wgsl_add_id; |
| 2838 | |
| 2839 | if (key.inplace) { |
| 2840 | defines.push_back("INPLACE"); |
| 2841 | variant += "_inplace"; |
| 2842 | } |
| 2843 | |
| 2844 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size)); |
| 2845 | |
| 2846 | auto processed = preprocessor.preprocess(shader_src, defines); |
| 2847 | auto pipeline_decisions = std::make_shared<ggml_webgpu_generic_shader_decisions>(); |
| 2848 | pipeline_decisions->wg_size = context.max_wg_size; |
| 2849 | pipeline_decisions->inplace = key.inplace; |
| 2850 | |
| 2851 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 2852 | pipeline.context = pipeline_decisions; |
| 2853 | add_id_pipelines[key] = pipeline; |
| 2854 | return pipeline; |
| 2855 | } |
| 2856 | |
| 2857 | webgpu_pipeline get_concat_pipeline(const ggml_webgpu_shader_lib_context & context, bool src_overlap) { |
| 2858 | ggml_webgpu_concat_pipeline_key key = {}; |
| 2859 | key.type = context.dst->type; |
| 2860 | key.src_overlap = src_overlap; |
| 2861 | |
| 2862 | auto it = concat_pipelines.find(key); |
| 2863 | if (it != concat_pipelines.end()) { |
| 2864 | return it->second; |
| 2865 | } |
| 2866 | |
| 2867 | std::vector<std::string> defines; |
| 2868 | std::string variant = "concat"; |
| 2869 | |
| 2870 | switch (key.type) { |
| 2871 | case GGML_TYPE_F32: |
| 2872 | defines.push_back("TYPE_F32"); |
| 2873 | variant += "_f32"; |
| 2874 | break; |
| 2875 | case GGML_TYPE_I32: |
| 2876 | defines.push_back("TYPE_I32"); |
| 2877 | variant += "_i32"; |
| 2878 | break; |
| 2879 | default: |
| 2880 | GGML_ABORT("Unsupported type for concat shader"); |
| 2881 | } |
| 2882 | |
| 2883 | if (key.src_overlap) { |
| 2884 | defines.push_back("SRC_OVERLAP"); |
| 2885 | variant += "_src_overlap"; |
| 2886 | } |
| 2887 | |
| 2888 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size)); |
| 2889 | |
| 2890 | auto processed = preprocessor.preprocess(wgsl_concat, defines); |
| 2891 | auto decisions = std::make_shared<ggml_webgpu_binary_shader_decisions>(); |
| 2892 | decisions->wg_size = context.max_wg_size; |
| 2893 | decisions->src_overlap = key.src_overlap; |
| 2894 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 2895 | pipeline.context = decisions; |
| 2896 | concat_pipelines[key] = pipeline; |
| 2897 | return concat_pipelines[key]; |
| 2898 | } |
| 2899 | |
| 2900 | webgpu_pipeline get_repeat_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 2901 | ggml_webgpu_repeat_pipeline_key key = {}; |
| 2902 | key.type = context.dst->type; |
| 2903 | |
| 2904 | auto it = repeat_pipelines.find(key); |
| 2905 | if (it != repeat_pipelines.end()) { |
| 2906 | return it->second; |
| 2907 | } |
| 2908 | |
| 2909 | std::vector<std::string> defines; |
| 2910 | std::string variant = "repeat"; |
| 2911 | |
| 2912 | switch (key.type) { |
| 2913 | case GGML_TYPE_F32: |
| 2914 | defines.push_back("TYPE_F32"); |
| 2915 | variant += "_f32"; |
| 2916 | break; |
| 2917 | case GGML_TYPE_F16: |
| 2918 | defines.push_back("TYPE_F16"); |
| 2919 | variant += "_f16"; |
| 2920 | break; |
| 2921 | case GGML_TYPE_I32: |
| 2922 | defines.push_back("TYPE_I32"); |
| 2923 | variant += "_i32"; |
| 2924 | break; |
| 2925 | case GGML_TYPE_I16: |
| 2926 | defines.push_back("TYPE_I16"); |
| 2927 | variant += "_i16"; |
| 2928 | break; |
| 2929 | default: |
| 2930 | GGML_ABORT("Unsupported type for repeat shader"); |
| 2931 | } |
| 2932 | |
| 2933 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size)); |
| 2934 | |
| 2935 | auto processed = preprocessor.preprocess(wgsl_repeat, defines); |
| 2936 | auto decisions = std::make_shared<ggml_webgpu_generic_shader_decisions>(); |
| 2937 | decisions->wg_size = context.max_wg_size; |
| 2938 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 2939 | pipeline.context = decisions; |
| 2940 | repeat_pipelines[key] = pipeline; |
| 2941 | return repeat_pipelines[key]; |
| 2942 | } |
| 2943 | |
| 2944 | webgpu_pipeline get_flash_attn_pipeline(const ggml_webgpu_shader_lib_context & context, bool kv_overlap) { |
| 2945 | const bool can_use_subgroup_matrix = ggml_webgpu_flash_attn_can_use_subgroup_matrix_path( |
| 2946 | context.supports_subgroup_matrix, context.sg_mat_k, context.sg_mat_n, context.src0, context.src2); |
| 2947 | ggml_webgpu_flash_attn_decisions decisions = {}; |
| 2948 | decisions.use_sg_matrix = can_use_subgroup_matrix; |
| 2949 | decisions.q_tile = decisions.use_sg_matrix ? context.sg_mat_m : GGML_WEBGPU_FLASH_ATTN_TILE_Q_TILE; |
| 2950 | |
| 2951 | ggml_webgpu_flash_attn_pipeline_key key = {}; |
| 2952 | key.common = ggml_webgpu_flash_attn_make_common_pipeline_key( |
| 2953 | context, decisions.use_sg_matrix ? context.sg_mat_k : 1u, kv_overlap); |
| 2954 | key.common.k_direct &= decisions.use_sg_matrix && key.common.k_type == GGML_TYPE_F16; |
| 2955 | key.common.v_direct &= decisions.use_sg_matrix && key.common.v_type == GGML_TYPE_F16; |
| 2956 | key.use_sg_matrix = decisions.use_sg_matrix; |
| 2957 | |
| 2958 | uint32_t max_kv_tile = ggml_webgpu_flash_attn_max_kv_tile( |
| 2959 | context.wg_mem_limit_bytes, decisions.q_tile, decisions.use_sg_matrix ? context.sg_mat_n : 1u, |
| 2960 | key.common.head_dim_qk, key.common.head_dim_v, key.common.has_mask, |
| 2961 | key.common.k_direct || key.common.v_direct); |
| 2962 | |
| 2963 | // WorkGroup storage size isn't enough for some params with subgroup matrices path (ref. https://github.com/ggml-org/llama.cpp/pull/26566) |
| 2964 | if (max_kv_tile == 0) { |
| 2965 | GGML_ASSERT(decisions.use_sg_matrix); |
| 2966 | // switch to flash_attn_reg_tile path |
| 2967 | decisions.use_sg_matrix = false; |
| 2968 | decisions.q_tile = GGML_WEBGPU_FLASH_ATTN_TILE_Q_TILE; |
| 2969 | key.common.k_direct = false; |
| 2970 | key.common.v_direct = false; |
| 2971 | key.use_sg_matrix = false; |
| 2972 | max_kv_tile = ggml_webgpu_flash_attn_max_kv_tile( |
| 2973 | context.wg_mem_limit_bytes, decisions.q_tile, 1u, key.common.head_dim_qk, key.common.head_dim_v, |
| 2974 | key.common.has_mask, key.common.k_direct || key.common.v_direct); |
| 2975 | GGML_ASSERT(max_kv_tile > 0); |
| 2976 | } |
| 2977 | |
| 2978 | decisions.kv_tile = decisions.use_sg_matrix ? |
| 2979 | std::min(max_kv_tile, context.sg_mat_n * GGML_WEBGPU_FLASH_ATTN_PREFERRED_KV_SG_TILES) : |
| 2980 | std::min(GGML_WEBGPU_FLASH_ATTN_TILE_MAX_KV_TILE, max_kv_tile); |
| 2981 | decisions.wg_size = |
| 2982 | decisions.use_sg_matrix ? |
| 2983 | std::max(context.max_subgroup_size, GGML_WEBGPU_FLASH_ATTN_PREFERRED_WG_SIZE) : |
| 2984 | std::min(context.max_wg_size, std::max(GGML_WEBGPU_FLASH_ATTN_PREFERRED_WG_SIZE, |
| 2985 | GGML_WEBGPU_FLASH_ATTN_TILE_Q_TILE * context.max_subgroup_size)); |
| 2986 | |
| 2987 | if (key.common.k_direct || key.common.v_direct) { |
| 2988 | decisions.kv_tile = std::min(decisions.kv_tile, GGML_WEBGPU_KV_SEQ_PAD); |
| 2989 | while (GGML_WEBGPU_KV_SEQ_PAD % decisions.kv_tile != 0) { |
| 2990 | decisions.kv_tile -= decisions.use_sg_matrix ? context.sg_mat_n : context.min_subgroup_size; |
| 2991 | } |
| 2992 | } |
| 2993 | |
| 2994 | auto it = flash_attn_pipelines.find(key); |
| 2995 | if (it != flash_attn_pipelines.end()) { |
| 2996 | return it->second; |
| 2997 | } |
| 2998 | |
| 2999 | std::string variant = decisions.use_sg_matrix ? "flash_attn" : "flash_attn_tile"; |
| 3000 | std::vector<std::string> defines = ggml_webgpu_flash_attn_common_defines(key.common, variant, decisions.q_tile, |
| 3001 | decisions.kv_tile, decisions.wg_size); |
| 3002 | const char * shader_src = nullptr; |
| 3003 | if (!key.use_sg_matrix) { |
| 3004 | shader_src = wgsl_flash_attn_tile; |
| 3005 | defines.push_back("MIN_SUBGROUP_SIZE=" + std::to_string(context.min_subgroup_size) + "u"); |
| 3006 | defines.push_back("MAX_SUBGROUP_SIZE=" + std::to_string(context.max_subgroup_size) + "u"); |
| 3007 | variant += "_tile_sg" + std::to_string(context.min_subgroup_size) + "_" + |
| 3008 | std::to_string(context.max_subgroup_size); |
| 3009 | } else { |
| 3010 | shader_src = wgsl_flash_attn; |
| 3011 | defines.push_back(std::string("SG_MAT_M=") + std::to_string(context.sg_mat_m)); |
| 3012 | defines.push_back(std::string("SG_MAT_N=") + std::to_string(context.sg_mat_n)); |
| 3013 | defines.push_back(std::string("SG_MAT_K=") + std::to_string(context.sg_mat_k)); |
| 3014 | } |
| 3015 | auto pipeline_decisions = std::make_shared<ggml_webgpu_flash_attn_decisions>(decisions); |
| 3016 | webgpu_pipeline pipeline = |
| 3017 | ggml_webgpu_create_pipeline(device, preprocessor.preprocess(shader_src, defines), variant); |
| 3018 | pipeline.context = pipeline_decisions; |
| 3019 | flash_attn_pipelines[key] = pipeline; |
| 3020 | return flash_attn_pipelines[key]; |
| 3021 | } |
| 3022 | |
| 3023 | webgpu_pipeline get_flash_attn_vec_pipeline(const ggml_webgpu_shader_lib_context & context, bool kv_overlap) { |
| 3024 | ggml_webgpu_flash_attn_vec_pipeline_key key = {}; |
| 3025 | key.common = ggml_webgpu_flash_attn_make_common_pipeline_key(context, GGML_WEBGPU_FLASH_ATTN_TILE_KV_VEC_WIDTH, |
| 3026 | kv_overlap); |
| 3027 | |
| 3028 | auto it = flash_attn_vec_pipelines.find(key); |
| 3029 | if (it != flash_attn_vec_pipelines.end()) { |
| 3030 | return it->second; |
| 3031 | } |
| 3032 | |
| 3033 | ggml_webgpu_flash_attn_vec_decisions decisions = {}; |
| 3034 | decisions.kv_tile = ggml_webgpu_flash_attn_get_vec_kv_tile(context.wg_mem_limit_bytes, key.common.head_dim_qk, |
| 3035 | key.common.head_dim_v, key.common.has_mask, |
| 3036 | key.common.k_direct || key.common.v_direct); |
| 3037 | decisions.wg_size = context.max_subgroup_size; |
| 3038 | |
| 3039 | std::string variant = "flash_attn_vec"; |
| 3040 | std::vector<std::string> defines = |
| 3041 | ggml_webgpu_flash_attn_common_defines(key.common, variant, 1u, decisions.kv_tile, decisions.wg_size); |
| 3042 | if (key.common.has_mask) { |
| 3043 | defines.push_back("BLK"); |
| 3044 | variant.resize(variant.size() - (sizeof("_mask") - 1)); |
| 3045 | variant += "_mask_blk"; |
| 3046 | } |
| 3047 | |
| 3048 | uint32_t d_split = context.min_subgroup_size; |
| 3049 | const uint32_t D = key.common.head_dim_qk | key.common.head_dim_v; |
| 3050 | const uint32_t D_lsb = D & (~(D - 1u)); |
| 3051 | d_split = std::min(std::min(context.min_subgroup_size, 4u), std::max(D_lsb / 4u, 1u)); |
| 3052 | |
| 3053 | defines.push_back(std::string("D_SPLIT=") + std::to_string(d_split)); |
| 3054 | variant += "_dsplit" + std::to_string(d_split); |
| 3055 | |
| 3056 | auto pipeline_decisions = std::make_shared<ggml_webgpu_flash_attn_vec_decisions>(decisions); |
| 3057 | webgpu_pipeline pipeline = |
| 3058 | ggml_webgpu_create_pipeline(device, preprocessor.preprocess(wgsl_flash_attn_vec_split, defines), variant); |
| 3059 | pipeline.context = pipeline_decisions; |
| 3060 | flash_attn_vec_pipelines[key] = pipeline; |
| 3061 | return flash_attn_vec_pipelines[key]; |
| 3062 | } |
| 3063 | |
| 3064 | webgpu_pipeline get_flash_attn_blk_pipeline(const ggml_webgpu_shader_lib_context & context, uint32_t kv_tile) { |
| 3065 | ggml_webgpu_flash_attn_blk_pipeline_key key = {}; |
| 3066 | key.kv_tile = kv_tile; |
| 3067 | auto it = flash_attn_blk_pipelines.find(key); |
| 3068 | if (it != flash_attn_blk_pipelines.end()) { |
| 3069 | return it->second; |
| 3070 | } |
| 3071 | |
| 3072 | std::vector<std::string> defines; |
| 3073 | std::string variant = "flash_attn_vec_blk"; |
| 3074 | |
| 3075 | defines.push_back(std::string("KV_TILE=") + std::to_string(key.kv_tile)); |
| 3076 | variant += std::string("_kvt") + std::to_string(key.kv_tile); |
| 3077 | |
| 3078 | uint32_t wg_size = 1; |
| 3079 | while ((wg_size << 1) <= context.max_wg_size) { |
| 3080 | wg_size <<= 1; |
| 3081 | } |
| 3082 | defines.push_back(std::string("WG_SIZE=") + std::to_string(wg_size)); |
| 3083 | variant += std::string("_wg") + std::to_string(wg_size); |
| 3084 | |
| 3085 | webgpu_pipeline pipeline = |
| 3086 | ggml_webgpu_create_pipeline(device, preprocessor.preprocess(wgsl_flash_attn_vec_blk, defines), variant); |
| 3087 | flash_attn_blk_pipelines[key] = pipeline; |
| 3088 | return flash_attn_blk_pipelines[key]; |
| 3089 | } |
| 3090 | |
| 3091 | webgpu_pipeline get_flash_attn_vec_reduce_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 3092 | ggml_webgpu_flash_attn_vec_reduce_pipeline_key key = {}; |
| 3093 | key.head_dim_v = (uint32_t) context.src2->ne[0]; |
| 3094 | key.dst_type = context.dst->type; |
| 3095 | key.wg_size = context.max_wg_size; |
| 3096 | auto it = flash_attn_vec_reduce_pipelines.find(key); |
| 3097 | if (it != flash_attn_vec_reduce_pipelines.end()) { |
| 3098 | return it->second; |
| 3099 | } |
| 3100 | |
| 3101 | std::vector<std::string> defines; |
| 3102 | std::string variant = "flash_attn_vec_reduce"; |
| 3103 | |
| 3104 | switch (key.dst_type) { |
| 3105 | case GGML_TYPE_F32: |
| 3106 | defines.push_back("DST_F32"); |
| 3107 | break; |
| 3108 | case GGML_TYPE_F16: |
| 3109 | defines.push_back("DST_F16"); |
| 3110 | break; |
| 3111 | default: |
| 3112 | GGML_ABORT("Unsupported dst type for flash attention vec reduce shader"); |
| 3113 | } |
| 3114 | variant += std::string("_dst") + ggml_type_name(key.dst_type); |
| 3115 | |
| 3116 | defines.push_back(std::string("HEAD_DIM_V=") + std::to_string(key.head_dim_v)); |
| 3117 | variant += std::string("_hsv") + std::to_string(key.head_dim_v); |
| 3118 | |
| 3119 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size)); |
| 3120 | variant += std::string("_wg") + std::to_string(context.max_wg_size); |
| 3121 | |
| 3122 | webgpu_pipeline pipeline = |
| 3123 | ggml_webgpu_create_pipeline(device, preprocessor.preprocess(wgsl_flash_attn_vec_reduce, defines), variant); |
| 3124 | flash_attn_vec_reduce_pipelines[key] = pipeline; |
| 3125 | return flash_attn_vec_reduce_pipelines[key]; |
| 3126 | } |
| 3127 | |
| 3128 | webgpu_pipeline get_cpy_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 3129 | ggml_webgpu_cpy_pipeline_key key = {}; |
| 3130 | key.src_type = context.src0->type; |
| 3131 | key.dst_type = context.dst->type; |
| 3132 | |
| 3133 | auto it = cpy_pipelines.find(key); |
| 3134 | if (it != cpy_pipelines.end()) { |
| 3135 | return it->second; |
| 3136 | } |
| 3137 | |
| 3138 | std::vector<std::string> defines; |
| 3139 | std::string variant = "cpy"; |
| 3140 | |
| 3141 | switch (key.src_type) { |
| 3142 | case GGML_TYPE_F32: |
| 3143 | defines.push_back("SRC_F32"); |
| 3144 | variant += "_f32"; |
| 3145 | break; |
| 3146 | case GGML_TYPE_F16: |
| 3147 | defines.push_back("SRC_F16"); |
| 3148 | variant += "_f16"; |
| 3149 | break; |
| 3150 | case GGML_TYPE_I32: |
| 3151 | defines.push_back("SRC_I32"); |
| 3152 | variant += "_i32"; |
| 3153 | break; |
| 3154 | default: |
| 3155 | GGML_ABORT("Unsupported src type for cpy shader"); |
| 3156 | } |
| 3157 | |
| 3158 | switch (key.dst_type) { |
| 3159 | case GGML_TYPE_F32: |
| 3160 | defines.push_back("DST_F32"); |
| 3161 | variant += "_f32"; |
| 3162 | break; |
| 3163 | case GGML_TYPE_F16: |
| 3164 | defines.push_back("DST_F16"); |
| 3165 | variant += "_f16"; |
| 3166 | break; |
| 3167 | case GGML_TYPE_I32: |
| 3168 | defines.push_back("DST_I32"); |
| 3169 | variant += "_i32"; |
| 3170 | break; |
| 3171 | default: |
| 3172 | GGML_ABORT("Unsupported dst type for cpy shader"); |
| 3173 | } |
| 3174 | |
| 3175 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size)); |
| 3176 | |
| 3177 | auto processed = preprocessor.preprocess(wgsl_cpy, defines); |
| 3178 | auto decisions = std::make_shared<ggml_webgpu_generic_shader_decisions>(); |
| 3179 | decisions->wg_size = context.max_wg_size; |
| 3180 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 3181 | pipeline.context = decisions; |
| 3182 | cpy_pipelines[key] = pipeline; |
| 3183 | return cpy_pipelines[key]; |
| 3184 | } |
| 3185 | |
| 3186 | webgpu_pipeline get_glu_pipeline(const ggml_webgpu_shader_lib_context & context, bool src_overlap) { |
| 3187 | ggml_webgpu_glu_pipeline_key key = {}; |
| 3188 | key.glu_op = ggml_get_glu_op(context.dst); |
| 3189 | key.type = context.dst->type; |
| 3190 | key.split = (context.src1 != nullptr); |
| 3191 | key.src_overlap = src_overlap; |
| 3192 | |
| 3193 | auto it = glu_pipelines.find(key); |
| 3194 | if (it != glu_pipelines.end()) { |
| 3195 | return it->second; |
| 3196 | } |
| 3197 | |
| 3198 | std::vector<std::string> defines; |
| 3199 | std::string variant = "glu"; |
| 3200 | |
| 3201 | switch (key.glu_op) { |
| 3202 | case GGML_GLU_OP_REGLU: |
| 3203 | defines.push_back("OP_REGLU"); |
| 3204 | variant += "_reglu"; |
| 3205 | break; |
| 3206 | case GGML_GLU_OP_GEGLU: |
| 3207 | defines.push_back("OP_GEGLU"); |
| 3208 | variant += "_geglu"; |
| 3209 | break; |
| 3210 | case GGML_GLU_OP_SWIGLU: |
| 3211 | defines.push_back("OP_SWIGLU"); |
| 3212 | variant += "_swiglu"; |
| 3213 | break; |
| 3214 | case GGML_GLU_OP_SWIGLU_OAI: |
| 3215 | defines.push_back("OP_SWIGLU_OAI"); |
| 3216 | variant += "_swiglu_oai"; |
| 3217 | break; |
| 3218 | case GGML_GLU_OP_GEGLU_ERF: |
| 3219 | defines.push_back("OP_GEGLU_ERF"); |
| 3220 | variant += "_geglu_erf"; |
| 3221 | break; |
| 3222 | case GGML_GLU_OP_GEGLU_QUICK: |
| 3223 | defines.push_back("OP_GEGLU_QUICK"); |
| 3224 | variant += "_geglu_quick"; |
| 3225 | break; |
| 3226 | case GGML_GLU_OP_SWIGLU_CLAMP: |
| 3227 | defines.push_back("OP_SWIGLU_CLAMP"); |
| 3228 | variant += "_swiglu_clamp"; |
| 3229 | break; |
| 3230 | default: |
| 3231 | GGML_ABORT("Unsupported GLU op"); |
| 3232 | } |
| 3233 | switch (key.type) { |
| 3234 | case GGML_TYPE_F32: |
| 3235 | defines.push_back("TYPE_F32"); |
| 3236 | variant += "_f32"; |
| 3237 | break; |
| 3238 | case GGML_TYPE_F16: |
| 3239 | defines.push_back("TYPE_F16"); |
| 3240 | variant += "_f16"; |
| 3241 | break; |
| 3242 | default: |
| 3243 | GGML_ABORT("Unsupported type for GLU shader"); |
| 3244 | } |
| 3245 | |
| 3246 | if (key.src_overlap) { |
| 3247 | defines.push_back("SRC_OVERLAP"); |
| 3248 | variant += "_src_overlap"; |
| 3249 | } else if (key.split) { |
| 3250 | variant += "_split"; |
| 3251 | } else { |
| 3252 | defines.push_back("NO_SPLIT"); |
| 3253 | } |
| 3254 | |
| 3255 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size)); |
| 3256 | |
| 3257 | auto processed = preprocessor.preprocess(wgsl_glu, defines); |
| 3258 | auto decisions = std::make_shared<ggml_webgpu_glu_shader_decisions>(); |
| 3259 | decisions->wg_size = context.max_wg_size; |
| 3260 | decisions->src_overlap = key.src_overlap; |
| 3261 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 3262 | pipeline.context = decisions; |
| 3263 | glu_pipelines[key] = pipeline; |
| 3264 | return glu_pipelines[key]; |
| 3265 | } |
| 3266 | |
| 3267 | webgpu_pipeline get_rope_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 3268 | ggml_webgpu_rope_pipeline_key key = {}; |
| 3269 | key.type = context.dst->type; |
| 3270 | key.inplace = ggml_webgpu_tensor_equal(context.src0, context.dst); |
| 3271 | key.has_ff = (context.src2 != nullptr); |
| 3272 | |
| 3273 | auto it = rope_pipelines.find(key); |
| 3274 | if (it != rope_pipelines.end()) { |
| 3275 | return it->second; |
| 3276 | } |
| 3277 | |
| 3278 | std::vector<std::string> defines; |
| 3279 | std::string variant = "rope"; |
| 3280 | |
| 3281 | switch (key.type) { |
| 3282 | case GGML_TYPE_F32: |
| 3283 | defines.push_back("TYPE_F32"); |
| 3284 | variant += "_f32"; |
| 3285 | break; |
| 3286 | case GGML_TYPE_F16: |
| 3287 | defines.push_back("TYPE_F16"); |
| 3288 | variant += "_f16"; |
| 3289 | break; |
| 3290 | default: |
| 3291 | GGML_ABORT("Unsupported type for ROPE shader"); |
| 3292 | } |
| 3293 | |
| 3294 | if (key.inplace) { |
| 3295 | defines.push_back("INPLACE"); |
| 3296 | variant += "_inplace"; |
| 3297 | } |
| 3298 | |
| 3299 | if (key.has_ff) { |
| 3300 | defines.push_back("FF_FUNC"); |
| 3301 | variant += "_ff"; |
| 3302 | } |
| 3303 | |
| 3304 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size)); |
| 3305 | |
| 3306 | auto processed = preprocessor.preprocess(wgsl_rope, defines); |
| 3307 | auto decisions = std::make_shared<ggml_webgpu_generic_shader_decisions>(); |
| 3308 | decisions->wg_size = context.max_wg_size; |
| 3309 | decisions->inplace = key.inplace; |
| 3310 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 3311 | pipeline.context = decisions; |
| 3312 | rope_pipelines[key] = pipeline; |
| 3313 | return rope_pipelines[key]; |
| 3314 | } |
| 3315 | |
| 3316 | webgpu_pipeline get_soft_max_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 3317 | ggml_webgpu_soft_max_pipeline_key key = {}; |
| 3318 | key.mask_type = context.src1 ? context.src1->type : GGML_TYPE_F32; |
| 3319 | key.has_mask = (context.src1 != nullptr); |
| 3320 | key.has_sink = (context.src2 != nullptr); |
| 3321 | key.inplace = ggml_webgpu_tensor_equal(context.src0, context.dst); |
| 3322 | |
| 3323 | auto it = soft_max_pipelines.find(key); |
| 3324 | if (it != soft_max_pipelines.end()) { |
| 3325 | return it->second; |
| 3326 | } |
| 3327 | |
| 3328 | std::vector<std::string> defines; |
| 3329 | std::string variant = "soft_max"; |
| 3330 | |
| 3331 | if (key.has_mask) { |
| 3332 | defines.push_back("HAS_MASK"); |
| 3333 | switch (key.mask_type) { |
| 3334 | case GGML_TYPE_F32: |
| 3335 | defines.push_back("MASK_F32"); |
| 3336 | variant += "_mask_f32"; |
| 3337 | break; |
| 3338 | case GGML_TYPE_F16: |
| 3339 | defines.push_back("MASK_F16"); |
| 3340 | variant += "_mask_f16"; |
| 3341 | break; |
| 3342 | default: |
| 3343 | GGML_ABORT("Unsupported type for SOFT_MAX shader"); |
| 3344 | } |
| 3345 | } |
| 3346 | |
| 3347 | if (key.has_sink) { |
| 3348 | defines.push_back("HAS_SINK"); |
| 3349 | variant += "_sink"; |
| 3350 | } |
| 3351 | |
| 3352 | if (key.inplace) { |
| 3353 | defines.push_back("INPLACE"); |
| 3354 | variant += "_inplace"; |
| 3355 | } |
| 3356 | |
| 3357 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size)); |
| 3358 | |
| 3359 | auto processed = preprocessor.preprocess(wgsl_soft_max, defines); |
| 3360 | auto decisions = std::make_shared<ggml_webgpu_generic_shader_decisions>(); |
| 3361 | decisions->wg_size = context.max_wg_size; |
| 3362 | decisions->inplace = key.inplace; |
| 3363 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 3364 | pipeline.context = decisions; |
| 3365 | soft_max_pipelines[key] = pipeline; |
| 3366 | return soft_max_pipelines[key]; |
| 3367 | } |
| 3368 | |
| 3369 | webgpu_pipeline get_conv2d_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 3370 | ggml_webgpu_conv2d_pipeline_key key = {}; |
| 3371 | key.weight_type = context.src0->type; |
| 3372 | key.input_type = context.src1->type; |
| 3373 | key.output_type = context.dst->type; |
| 3374 | |
| 3375 | auto it = conv2d_pipelines.find(key); |
| 3376 | if (it != conv2d_pipelines.end()) { |
| 3377 | return it->second; |
| 3378 | } |
| 3379 | |
| 3380 | std::vector<std::string> defines; |
| 3381 | std::string variant = "conv_2d"; |
| 3382 | |
| 3383 | auto push_type_defines = [&](const char * prefix, ggml_type type) { |
| 3384 | std::string s_prefix = prefix; |
| 3385 | if (type == GGML_TYPE_F32) { |
| 3386 | defines.push_back(s_prefix + "=f32"); |
| 3387 | } else if (type == GGML_TYPE_F16) { |
| 3388 | defines.push_back(s_prefix + "=f16"); |
| 3389 | } else { |
| 3390 | GGML_ABORT("Unsupported type for CONV_2D shader"); |
| 3391 | } |
| 3392 | }; |
| 3393 | |
| 3394 | push_type_defines("WEIGHT_TYPE", key.weight_type); |
| 3395 | push_type_defines("INPUT_TYPE", key.input_type); |
| 3396 | push_type_defines("OUTPUT_TYPE", key.output_type); |
| 3397 | |
| 3398 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size)); |
| 3399 | |
| 3400 | auto processed = preprocessor.preprocess(wgsl_conv2d, defines); |
| 3401 | auto decisions = std::make_shared<ggml_webgpu_generic_shader_decisions>(); |
| 3402 | decisions->wg_size = context.max_wg_size; |
| 3403 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 3404 | pipeline.context = decisions; |
| 3405 | conv2d_pipelines[key] = pipeline; |
| 3406 | return conv2d_pipelines[key]; |
| 3407 | } |
| 3408 | |
| 3409 | // whcn selects the input layout: contiguous WHCN vs contiguous-channels CWHN |
| 3410 | webgpu_pipeline get_conv2d_dw_pipeline(const ggml_webgpu_shader_lib_context & context, bool whcn) { |
| 3411 | ggml_webgpu_conv2d_dw_pipeline_key key = {}; |
| 3412 | key.weight_type = context.src0->type; |
| 3413 | key.input_type = context.src1->type; |
| 3414 | key.output_type = context.dst->type; |
| 3415 | key.whcn = whcn; |
| 3416 | |
| 3417 | auto it = conv2d_dw_pipelines.find(key); |
| 3418 | if (it != conv2d_dw_pipelines.end()) { |
| 3419 | return it->second; |
| 3420 | } |
| 3421 | |
| 3422 | std::vector<std::string> defines; |
| 3423 | std::string variant = whcn ? "conv_2d_dw_whcn" : "conv_2d_dw_cwhn"; |
| 3424 | |
| 3425 | auto push_type_defines = [&](const char * prefix, ggml_type type) { |
| 3426 | std::string s_prefix = prefix; |
| 3427 | if (type == GGML_TYPE_F32) { |
| 3428 | defines.push_back(s_prefix + "=f32"); |
| 3429 | } else if (type == GGML_TYPE_F16) { |
| 3430 | defines.push_back(s_prefix + "=f16"); |
| 3431 | } else { |
| 3432 | GGML_ABORT("Unsupported type for CONV_2D shader"); |
| 3433 | } |
| 3434 | }; |
| 3435 | |
| 3436 | push_type_defines("WEIGHT_TYPE", key.weight_type); |
| 3437 | push_type_defines("INPUT_TYPE", key.input_type); |
| 3438 | push_type_defines("OUTPUT_TYPE", key.output_type); |
| 3439 | |
| 3440 | if (whcn) { |
| 3441 | defines.push_back("WHCN"); |
| 3442 | } |
| 3443 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size)); |
| 3444 | |
| 3445 | auto processed = preprocessor.preprocess(wgsl_conv2d_dw, defines); |
| 3446 | auto decisions = std::make_shared<ggml_webgpu_generic_shader_decisions>(); |
| 3447 | decisions->wg_size = context.max_wg_size; |
| 3448 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 3449 | pipeline.context = decisions; |
| 3450 | conv2d_dw_pipelines[key] = pipeline; |
| 3451 | return conv2d_dw_pipelines[key]; |
| 3452 | } |
| 3453 | |
| 3454 | webgpu_pipeline get_im2col_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 3455 | ggml_webgpu_im2col_pipeline_key key = {}; |
| 3456 | key.input_type = context.src1->type; |
| 3457 | key.output_type = context.dst->type; |
| 3458 | |
| 3459 | auto it = im2col_pipelines.find(key); |
| 3460 | if (it != im2col_pipelines.end()) { |
| 3461 | return it->second; |
| 3462 | } |
| 3463 | |
| 3464 | std::vector<std::string> defines; |
| 3465 | std::string variant = "im2col"; |
| 3466 | |
| 3467 | auto push_type_defines = [&](const char * prefix, ggml_type type) { |
| 3468 | std::string s_prefix = prefix; |
| 3469 | if (type == GGML_TYPE_F32) { |
| 3470 | defines.push_back(s_prefix + "=f32"); |
| 3471 | } else if (type == GGML_TYPE_F16) { |
| 3472 | defines.push_back(s_prefix + "=f16"); |
| 3473 | } else { |
| 3474 | GGML_ABORT("Unsupported type for IM2COL shader"); |
| 3475 | } |
| 3476 | }; |
| 3477 | |
| 3478 | push_type_defines("INPUT_TYPE", key.input_type); |
| 3479 | push_type_defines("OUTPUT_TYPE", key.output_type); |
| 3480 | |
| 3481 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size)); |
| 3482 | |
| 3483 | auto processed = preprocessor.preprocess(wgsl_im2col, defines); |
| 3484 | auto decisions = std::make_shared<ggml_webgpu_generic_shader_decisions>(); |
| 3485 | decisions->wg_size = context.max_wg_size; |
| 3486 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 3487 | pipeline.context = decisions; |
| 3488 | im2col_pipelines[key] = pipeline; |
| 3489 | return im2col_pipelines[key]; |
| 3490 | } |
| 3491 | |
| 3492 | webgpu_pipeline get_upscale_pipeline(const ggml_webgpu_shader_lib_context & context) { |
| 3493 | const uint32_t mode_flags = (uint32_t) ggml_get_op_params_i32(context.dst, 0); |
| 3494 | const uint32_t base_mode = mode_flags & 0xFFu; |
| 3495 | const bool antialias = (mode_flags & GGML_SCALE_FLAG_ANTIALIAS) != 0u; |
| 3496 | |
| 3497 | ggml_webgpu_upscale_pipeline_key key = {}; |
| 3498 | key.input_type = context.src0->type; |
| 3499 | key.output_type = context.dst->type; |
| 3500 | key.base_mode = base_mode; |
| 3501 | key.antialias = antialias; |
| 3502 | |
| 3503 | auto it = upscale_pipelines.find(key); |
| 3504 | if (it != upscale_pipelines.end()) { |
| 3505 | return it->second; |
| 3506 | } |
| 3507 | |
| 3508 | std::vector<std::string> defines; |
| 3509 | std::string variant = "upscale"; |
| 3510 | |
| 3511 | if (key.input_type == GGML_TYPE_F16) { |
| 3512 | defines.push_back("SRC_F16"); |
| 3513 | variant += "_src_f16"; |
| 3514 | } else { |
| 3515 | variant += "_src_f32"; |
| 3516 | } |
| 3517 | |
| 3518 | if (key.output_type == GGML_TYPE_F16) { |
| 3519 | defines.push_back("DST_F16"); |
| 3520 | variant += "_dst_f16"; |
| 3521 | } else { |
| 3522 | variant += "_dst_f32"; |
| 3523 | } |
| 3524 | |
| 3525 | switch (base_mode) { |
| 3526 | case GGML_SCALE_MODE_NEAREST: |
| 3527 | defines.push_back("NEAREST"); |
| 3528 | variant += "_nearest"; |
| 3529 | break; |
| 3530 | case GGML_SCALE_MODE_BILINEAR: |
| 3531 | defines.push_back("BILINEAR"); |
| 3532 | variant += "_bilinear"; |
| 3533 | break; |
| 3534 | case GGML_SCALE_MODE_BICUBIC: |
| 3535 | defines.push_back("BICUBIC"); |
| 3536 | variant += "_bicubic"; |
| 3537 | break; |
| 3538 | default: |
| 3539 | GGML_ABORT("Unsupported upscale mode"); |
| 3540 | } |
| 3541 | |
| 3542 | if (antialias) { |
| 3543 | defines.push_back("ANTIALIAS"); |
| 3544 | variant += "_aa"; |
| 3545 | } |
| 3546 | |
| 3547 | defines.push_back(std::string("WG_SIZE=") + std::to_string(context.max_wg_size)); |
| 3548 | |
| 3549 | auto processed = preprocessor.preprocess(wgsl_upscale, defines); |
| 3550 | auto decisions = std::make_shared<ggml_webgpu_generic_shader_decisions>(); |
| 3551 | decisions->wg_size = context.max_wg_size; |
| 3552 | webgpu_pipeline pipeline = ggml_webgpu_create_pipeline(device, processed, variant); |
| 3553 | pipeline.context = decisions; |
| 3554 | upscale_pipelines[key] = pipeline; |
| 3555 | return upscale_pipelines[key]; |
| 3556 | } |
| 3557 | |
| 3558 | private: |
| 3559 | static webgpu_pipeline ggml_webgpu_create_pipeline(wgpu::Device & device, |
| 3560 | std::string shader_code, |
| 3561 | std::string label) { |
| 3562 | wgpu::ShaderSourceWGSL shader_source; |
| 3563 | shader_source.code = shader_code.c_str(); |
| 3564 | |
| 3565 | wgpu::ShaderModuleDescriptor shader_desc; |
| 3566 | shader_desc.nextInChain = &shader_source; |
| 3567 | |
| 3568 | wgpu::ShaderModule shader_module = device.CreateShaderModule(&shader_desc); |
| 3569 | |
| 3570 | wgpu::ComputePipelineDescriptor pipeline_desc; |
| 3571 | pipeline_desc.label = label.c_str(); |
| 3572 | pipeline_desc.compute.module = shader_module; |
| 3573 | pipeline_desc.compute.entryPoint = "main"; // Entry point in the WGSL code |
| 3574 | pipeline_desc.layout = nullptr; // nullptr means auto layout |
| 3575 | return { device.CreateComputePipeline(&pipeline_desc), label }; |
| 3576 | } |
| 3577 | }; |
| 3578 | |
| 3579 | #endif // GGML_WEBGPU_SHADER_LIB_HPP |