whispercpp 1.3.2 → 1.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +6 -3
- data/README.md +71 -14
- data/Rakefile +20 -7
- data/ext/.gitignore +4 -6
- data/ext/dependencies.rb +36 -24
- data/ext/extconf.rb +1 -1
- data/ext/options.rb +48 -184
- data/ext/ruby_whisper.c +18 -0
- data/ext/ruby_whisper_context.c +43 -12
- data/ext/ruby_whisper_model.c +1 -1
- data/ext/ruby_whisper_params.c +59 -27
- data/ext/ruby_whisper_segment.c +81 -4
- data/ext/ruby_whisper_transcribe.cpp +13 -7
- data/ext/ruby_whisper_vad_params.c +1 -1
- data/ext/sources/CMakeLists.txt +5 -1
- data/ext/sources/bindings/javascript/package.json +1 -1
- data/ext/sources/build-xcframework.sh +24 -0
- data/ext/sources/examples/CMakeLists.txt +1 -0
- data/ext/sources/examples/addon.node/__test__/whisper.spec.js +120 -24
- data/ext/sources/examples/addon.node/addon.cpp +154 -35
- data/ext/sources/examples/addon.node/index.js +10 -5
- data/ext/sources/examples/addon.node/vad-example.js +132 -0
- data/ext/sources/examples/bench/bench.cpp +29 -18
- data/ext/sources/examples/bench.wasm/index-tmpl.html +10 -9
- data/ext/sources/examples/cli/cli.cpp +7 -4
- data/ext/sources/examples/command/command.cpp +58 -32
- data/ext/sources/examples/command.wasm/index-tmpl.html +5 -4
- data/ext/sources/examples/common-ggml.cpp +2 -0
- data/ext/sources/examples/common-whisper.cpp +14 -7
- data/ext/sources/examples/lsp/lsp.cpp +21 -17
- data/ext/sources/examples/quantize/quantize.cpp +3 -0
- data/ext/sources/examples/server/CMakeLists.txt +3 -0
- data/ext/sources/examples/server/server.cpp +193 -35
- data/ext/sources/examples/server.py +6 -1
- data/ext/sources/examples/stream/stream.cpp +10 -2
- data/ext/sources/examples/stream.wasm/emscripten.cpp +6 -6
- data/ext/sources/examples/stream.wasm/index-tmpl.html +82 -5
- data/ext/sources/examples/talk-llama/CMakeLists.txt +3 -0
- data/ext/sources/examples/talk-llama/llama-adapter.cpp +101 -4
- data/ext/sources/examples/talk-llama/llama-adapter.h +6 -0
- data/ext/sources/examples/talk-llama/llama-arch.cpp +756 -15
- data/ext/sources/examples/talk-llama/llama-arch.h +85 -1
- data/ext/sources/examples/talk-llama/llama-batch.cpp +773 -272
- data/ext/sources/examples/talk-llama/llama-batch.h +126 -55
- data/ext/sources/examples/talk-llama/llama-chat.cpp +150 -13
- data/ext/sources/examples/talk-llama/llama-chat.h +8 -0
- data/ext/sources/examples/talk-llama/llama-context.cpp +814 -542
- data/ext/sources/examples/talk-llama/llama-context.h +68 -32
- data/ext/sources/examples/talk-llama/llama-cparams.cpp +1 -1
- data/ext/sources/examples/talk-llama/llama-cparams.h +4 -4
- data/ext/sources/examples/talk-llama/llama-graph.cpp +787 -440
- data/ext/sources/examples/talk-llama/llama-graph.h +333 -153
- data/ext/sources/examples/talk-llama/llama-hparams.cpp +128 -6
- data/ext/sources/examples/talk-llama/llama-hparams.h +80 -17
- data/ext/sources/examples/talk-llama/llama-impl.h +2 -0
- data/ext/sources/examples/talk-llama/llama-kv-cache-iswa.cpp +326 -0
- data/ext/sources/examples/talk-llama/llama-kv-cache-iswa.h +137 -0
- data/ext/sources/examples/talk-llama/llama-kv-cache.cpp +1248 -1967
- data/ext/sources/examples/talk-llama/llama-kv-cache.h +218 -345
- data/ext/sources/examples/talk-llama/llama-kv-cells.h +164 -52
- data/ext/sources/examples/talk-llama/llama-memory-hybrid.cpp +266 -0
- data/ext/sources/examples/talk-llama/llama-memory-hybrid.h +139 -0
- data/ext/sources/examples/talk-llama/llama-memory-recurrent.cpp +1154 -0
- data/ext/sources/examples/talk-llama/llama-memory-recurrent.h +182 -0
- data/ext/sources/examples/talk-llama/llama-memory.cpp +58 -0
- data/ext/sources/examples/talk-llama/llama-memory.h +94 -4
- data/ext/sources/examples/talk-llama/llama-mmap.cpp +1 -1
- data/ext/sources/examples/talk-llama/llama-model-loader.cpp +44 -17
- data/ext/sources/examples/talk-llama/llama-model-loader.h +3 -2
- data/ext/sources/examples/talk-llama/llama-model-saver.cpp +1 -0
- data/ext/sources/examples/talk-llama/llama-model.cpp +11377 -5248
- data/ext/sources/examples/talk-llama/llama-model.h +87 -9
- data/ext/sources/examples/talk-llama/llama-quant.cpp +137 -16
- data/ext/sources/examples/talk-llama/llama-sampling.cpp +226 -126
- data/ext/sources/examples/talk-llama/llama-vocab.cpp +502 -38
- data/ext/sources/examples/talk-llama/llama-vocab.h +46 -0
- data/ext/sources/examples/talk-llama/llama.cpp +76 -17
- data/ext/sources/examples/talk-llama/llama.h +176 -151
- data/ext/sources/examples/talk-llama/talk-llama.cpp +11 -6
- data/ext/sources/examples/talk-llama/unicode.cpp +212 -0
- data/ext/sources/examples/talk-llama/unicode.h +45 -0
- data/ext/sources/examples/vad-speech-segments/speech.cpp +6 -0
- data/ext/sources/examples/wchess/wchess.cmd/wchess.cmd.cpp +6 -2
- data/ext/sources/examples/whisper.wasm/index-tmpl.html +17 -16
- data/ext/sources/ggml/CMakeLists.txt +106 -33
- data/ext/sources/ggml/cmake/common.cmake +24 -0
- data/ext/sources/ggml/cmake/ggml-config.cmake.in +132 -93
- data/ext/sources/ggml/include/ggml-backend.h +18 -2
- data/ext/sources/ggml/include/ggml-cpu.h +2 -0
- data/ext/sources/ggml/include/ggml-metal.h +1 -6
- data/ext/sources/ggml/include/ggml-opt.h +25 -6
- data/ext/sources/ggml/include/ggml-webgpu.h +19 -0
- data/ext/sources/ggml/include/ggml-zdnn.h +17 -0
- data/ext/sources/ggml/include/ggml.h +365 -21
- data/ext/sources/ggml/src/CMakeLists.txt +98 -25
- data/ext/sources/ggml/src/ggml-alloc.c +265 -141
- data/ext/sources/ggml/src/ggml-backend-impl.h +4 -1
- data/ext/sources/ggml/src/ggml-backend-reg.cpp +35 -13
- data/ext/sources/ggml/src/ggml-backend.cpp +266 -60
- data/ext/sources/ggml/src/ggml-blas/CMakeLists.txt +4 -4
- data/ext/sources/ggml/src/ggml-blas/ggml-blas.cpp +5 -4
- data/ext/sources/ggml/src/ggml-cann/CMakeLists.txt +15 -0
- data/ext/sources/ggml/src/ggml-cann/acl_tensor.cpp +3 -1
- data/ext/sources/ggml/src/ggml-cann/aclnn_ops.cpp +903 -717
- data/ext/sources/ggml/src/ggml-cann/aclnn_ops.h +143 -25
- data/ext/sources/ggml/src/ggml-cann/common.h +149 -2
- data/ext/sources/ggml/src/ggml-cann/ggml-cann.cpp +521 -78
- data/ext/sources/ggml/src/ggml-common.h +21 -0
- data/ext/sources/ggml/src/ggml-cpu/CMakeLists.txt +165 -50
- data/ext/sources/ggml/src/ggml-cpu/amx/amx.cpp +5 -3
- data/ext/sources/ggml/src/ggml-cpu/amx/mmq.cpp +11 -10
- data/ext/sources/ggml/src/ggml-cpu/arch/arm/cpu-feats.cpp +94 -0
- data/ext/sources/ggml/src/ggml-cpu/arch/arm/quants.c +3650 -0
- data/ext/sources/ggml/src/ggml-cpu/arch/arm/repack.cpp +1891 -0
- data/ext/sources/ggml/src/ggml-cpu/arch/loongarch/quants.c +2160 -0
- data/ext/sources/ggml/src/ggml-cpu/arch/powerpc/cpu-feats.cpp +82 -0
- data/ext/sources/ggml/src/ggml-cpu/arch/powerpc/quants.c +2305 -0
- data/ext/sources/ggml/src/ggml-cpu/arch/riscv/quants.c +1897 -0
- data/ext/sources/ggml/src/ggml-cpu/arch/riscv/repack.cpp +342 -0
- data/ext/sources/ggml/src/ggml-cpu/arch/s390/quants.c +1468 -0
- data/ext/sources/ggml/src/ggml-cpu/arch/wasm/quants.c +1221 -0
- data/ext/sources/ggml/src/ggml-cpu/arch/x86/quants.c +3820 -0
- data/ext/sources/ggml/src/ggml-cpu/arch/x86/repack.cpp +6307 -0
- data/ext/sources/ggml/src/ggml-cpu/arch-fallback.h +214 -0
- data/ext/sources/ggml/src/ggml-cpu/common.h +18 -3
- data/ext/sources/ggml/src/ggml-cpu/ggml-cpu-impl.h +23 -7
- data/ext/sources/ggml/src/ggml-cpu/ggml-cpu.c +179 -110
- data/ext/sources/ggml/src/ggml-cpu/ggml-cpu.cpp +44 -33
- data/ext/sources/ggml/src/ggml-cpu/{ggml-cpu-hbm.cpp → hbm.cpp} +1 -1
- data/ext/sources/ggml/src/ggml-cpu/kleidiai/kernels.cpp +152 -18
- data/ext/sources/ggml/src/ggml-cpu/kleidiai/kernels.h +7 -1
- data/ext/sources/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +228 -98
- data/ext/sources/ggml/src/ggml-cpu/llamafile/sgemm.cpp +532 -1124
- data/ext/sources/ggml/src/ggml-cpu/llamafile/sgemm.h +5 -0
- data/ext/sources/ggml/src/ggml-cpu/ops.cpp +3374 -2081
- data/ext/sources/ggml/src/ggml-cpu/ops.h +13 -8
- data/ext/sources/ggml/src/ggml-cpu/quants.c +1193 -0
- data/ext/sources/ggml/src/ggml-cpu/{ggml-cpu-quants.h → quants.h} +34 -0
- data/ext/sources/ggml/src/ggml-cpu/repack.cpp +1982 -0
- data/ext/sources/ggml/src/ggml-cpu/repack.h +120 -0
- data/ext/sources/ggml/src/ggml-cpu/simd-mappings.h +367 -46
- data/ext/sources/ggml/src/ggml-cpu/spacemit/ime.cpp +1024 -0
- data/ext/sources/ggml/src/ggml-cpu/spacemit/ime.h +13 -0
- data/ext/sources/ggml/src/ggml-cpu/spacemit/ime1_kernels.cpp +3196 -0
- data/ext/sources/ggml/src/ggml-cpu/spacemit/ime_kernels.h +26 -0
- data/ext/sources/ggml/src/ggml-cpu/{ggml-cpu-traits.cpp → traits.cpp} +3 -3
- data/ext/sources/ggml/src/ggml-cpu/{ggml-cpu-traits.h → traits.h} +1 -1
- data/ext/sources/ggml/src/ggml-cpu/vec.cpp +272 -35
- data/ext/sources/ggml/src/ggml-cpu/vec.h +794 -142
- data/ext/sources/ggml/src/ggml-cuda/CMakeLists.txt +20 -16
- data/ext/sources/ggml/src/ggml-cuda/add-id.cu +58 -0
- data/ext/sources/ggml/src/ggml-cuda/add-id.cuh +3 -0
- data/ext/sources/ggml/src/ggml-cuda/binbcast.cu +330 -191
- data/ext/sources/ggml/src/ggml-cuda/binbcast.cuh +2 -0
- data/ext/sources/ggml/src/ggml-cuda/common.cuh +291 -81
- data/ext/sources/ggml/src/ggml-cuda/conv-transpose-1d.cu +1 -4
- data/ext/sources/ggml/src/ggml-cuda/conv2d-dw.cu +161 -0
- data/ext/sources/ggml/src/ggml-cuda/conv2d-dw.cuh +5 -0
- data/ext/sources/ggml/src/ggml-cuda/conv2d-transpose.cu +91 -0
- data/ext/sources/ggml/src/ggml-cuda/conv2d-transpose.cuh +4 -0
- data/ext/sources/ggml/src/ggml-cuda/conv2d.cu +166 -0
- data/ext/sources/ggml/src/ggml-cuda/conv2d.cuh +5 -0
- data/ext/sources/ggml/src/ggml-cuda/convert.cu +117 -22
- data/ext/sources/ggml/src/ggml-cuda/convert.cuh +20 -0
- data/ext/sources/ggml/src/ggml-cuda/cpy-utils.cuh +217 -0
- data/ext/sources/ggml/src/ggml-cuda/cpy.cu +64 -307
- data/ext/sources/ggml/src/ggml-cuda/cross-entropy-loss.cu +2 -14
- data/ext/sources/ggml/src/ggml-cuda/dequantize.cuh +14 -40
- data/ext/sources/ggml/src/ggml-cuda/fattn-common.cuh +499 -368
- data/ext/sources/ggml/src/ggml-cuda/fattn-mma-f16.cuh +142 -93
- data/ext/sources/ggml/src/ggml-cuda/fattn-tile.cu +755 -0
- data/ext/sources/ggml/src/ggml-cuda/fattn-tile.cuh +3 -0
- data/ext/sources/ggml/src/ggml-cuda/fattn-vec.cuh +593 -0
- data/ext/sources/ggml/src/ggml-cuda/fattn-wmma-f16.cu +90 -50
- data/ext/sources/ggml/src/ggml-cuda/fattn.cu +185 -198
- data/ext/sources/ggml/src/ggml-cuda/fattn.cuh +2 -0
- data/ext/sources/ggml/src/ggml-cuda/getrows.cu +50 -39
- data/ext/sources/ggml/src/ggml-cuda/ggml-cuda.cu +636 -222
- data/ext/sources/ggml/src/ggml-cuda/im2col.cu +196 -35
- data/ext/sources/ggml/src/ggml-cuda/im2col.cuh +1 -0
- data/ext/sources/ggml/src/ggml-cuda/mean.cu +73 -0
- data/ext/sources/ggml/src/ggml-cuda/mean.cuh +3 -0
- data/ext/sources/ggml/src/ggml-cuda/mma.cuh +198 -45
- data/ext/sources/ggml/src/ggml-cuda/mmf.cu +123 -0
- data/ext/sources/ggml/src/ggml-cuda/mmf.cuh +496 -0
- data/ext/sources/ggml/src/ggml-cuda/mmq.cu +206 -57
- data/ext/sources/ggml/src/ggml-cuda/mmq.cuh +1262 -721
- data/ext/sources/ggml/src/ggml-cuda/mmvf.cu +506 -0
- data/ext/sources/ggml/src/ggml-cuda/{mmv.cuh → mmvf.cuh} +4 -5
- data/ext/sources/ggml/src/ggml-cuda/mmvq.cu +64 -73
- data/ext/sources/ggml/src/ggml-cuda/norm.cu +284 -12
- data/ext/sources/ggml/src/ggml-cuda/norm.cuh +7 -0
- data/ext/sources/ggml/src/ggml-cuda/opt-step-sgd.cu +49 -0
- data/ext/sources/ggml/src/ggml-cuda/opt-step-sgd.cuh +5 -0
- data/ext/sources/ggml/src/ggml-cuda/pad.cu +46 -23
- data/ext/sources/ggml/src/ggml-cuda/pad_reflect_1d.cu +91 -0
- data/ext/sources/ggml/src/ggml-cuda/pad_reflect_1d.cuh +5 -0
- data/ext/sources/ggml/src/ggml-cuda/quantize.cu +12 -10
- data/ext/sources/ggml/src/ggml-cuda/reduce_rows.cuh +53 -0
- data/ext/sources/ggml/src/ggml-cuda/roll.cu +67 -0
- data/ext/sources/ggml/src/ggml-cuda/roll.cuh +5 -0
- data/ext/sources/ggml/src/ggml-cuda/rope.cu +21 -27
- data/ext/sources/ggml/src/ggml-cuda/scale.cu +14 -11
- data/ext/sources/ggml/src/ggml-cuda/set-rows.cu +276 -0
- data/ext/sources/ggml/src/ggml-cuda/set-rows.cuh +7 -0
- data/ext/sources/ggml/src/ggml-cuda/softcap.cu +34 -0
- data/ext/sources/ggml/src/ggml-cuda/softcap.cuh +5 -0
- data/ext/sources/ggml/src/ggml-cuda/softmax.cu +126 -59
- data/ext/sources/ggml/src/ggml-cuda/ssm-conv.cu +10 -2
- data/ext/sources/ggml/src/ggml-cuda/ssm-scan.cu +322 -98
- data/ext/sources/ggml/src/ggml-cuda/sum.cu +6 -10
- data/ext/sources/ggml/src/ggml-cuda/sumrows.cu +23 -19
- data/ext/sources/ggml/src/ggml-cuda/sumrows.cuh +0 -1
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-f16-f16.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-f16-q4_0.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-f16-q4_1.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-f16-q5_0.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-f16-q5_1.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-f16-q8_0.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_0-f16.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_0-q4_0.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_0-q4_1.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_0-q5_0.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_0-q5_1.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_0-q8_0.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_1-f16.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_1-q4_0.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_1-q4_1.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_1-q5_0.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_1-q5_1.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_1-q8_0.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_0-f16.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_0-q4_0.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_0-q4_1.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_0-q5_0.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_0-q5_1.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_0-q8_0.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_1-f16.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_1-q4_0.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_1-q4_1.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_1-q5_0.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_1-q5_1.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_1-q8_0.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q8_0-f16.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q8_0-q4_0.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q8_0-q4_1.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q8_0-q5_0.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q8_0-q5_1.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q8_0-q8_0.cu +7 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/generate_cu_files.py +21 -18
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_10.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_11.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_12.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_13.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_14.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_15.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_16.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_2.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_3.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_4.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_5.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_6.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_7.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_8.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_9.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-mxfp4.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/topk-moe.cu +259 -0
- data/ext/sources/ggml/src/ggml-cuda/topk-moe.cuh +14 -0
- data/ext/sources/ggml/src/ggml-cuda/tsembd.cu +3 -3
- data/ext/sources/ggml/src/ggml-cuda/unary.cu +179 -0
- data/ext/sources/ggml/src/ggml-cuda/unary.cuh +15 -0
- data/ext/sources/ggml/src/ggml-cuda/upscale.cu +92 -6
- data/ext/sources/ggml/src/ggml-cuda/vecdotq.cuh +110 -22
- data/ext/sources/ggml/src/ggml-cuda/vendors/cuda.h +4 -0
- data/ext/sources/ggml/src/ggml-cuda/vendors/hip.h +58 -36
- data/ext/sources/ggml/src/ggml-cuda/vendors/musa.h +4 -3
- data/ext/sources/ggml/src/ggml-hip/CMakeLists.txt +14 -2
- data/ext/sources/ggml/src/ggml-impl.h +229 -175
- data/ext/sources/ggml/src/ggml-metal/CMakeLists.txt +21 -17
- data/ext/sources/ggml/src/ggml-metal/ggml-metal-common.cpp +446 -0
- data/ext/sources/ggml/src/ggml-metal/ggml-metal-common.h +52 -0
- data/ext/sources/ggml/src/ggml-metal/ggml-metal-context.h +33 -0
- data/ext/sources/ggml/src/ggml-metal/ggml-metal-context.m +600 -0
- data/ext/sources/ggml/src/ggml-metal/ggml-metal-device.cpp +1376 -0
- data/ext/sources/ggml/src/ggml-metal/ggml-metal-device.h +226 -0
- data/ext/sources/ggml/src/ggml-metal/ggml-metal-device.m +1308 -0
- data/ext/sources/ggml/src/ggml-metal/ggml-metal-impl.h +163 -63
- data/ext/sources/ggml/src/ggml-metal/ggml-metal-ops.cpp +3158 -0
- data/ext/sources/ggml/src/ggml-metal/ggml-metal-ops.h +82 -0
- data/ext/sources/ggml/src/ggml-metal/ggml-metal.cpp +718 -0
- data/ext/sources/ggml/src/ggml-metal/ggml-metal.metal +3208 -1575
- data/ext/sources/ggml/src/ggml-musa/CMakeLists.txt +18 -8
- data/ext/sources/ggml/src/ggml-musa/mudnn.cuh +2 -2
- data/ext/sources/ggml/src/ggml-opencl/CMakeLists.txt +32 -0
- data/ext/sources/ggml/src/ggml-opencl/ggml-opencl.cpp +4430 -792
- data/ext/sources/ggml/src/ggml-opencl/kernels/add.cl +107 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/add_id.cl +42 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/argsort.cl +86 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/concat.cl +109 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/conv2d.cl +185 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/conv2d_f16_f32.cl +176 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/cvt.cl +84 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/div.cl +138 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/flash_attn_f16.cl +370 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/flash_attn_f32.cl +370 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/flash_attn_f32_f16.cl +373 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/gelu.cl +27 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/glu.cl +378 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/group_norm.cl +121 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/im2col_f16.cl +1 -1
- data/ext/sources/ggml/src/ggml-opencl/kernels/im2col_f32.cl +1 -1
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul.cl +73 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mat_f16_f32.cl +130 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mm_f16_f32_l4_lm.cl +132 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mm_f32_f32_l4_lm.cl +133 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_id_mxfp4_f32.cl +189 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_id_mxfp4_f32_flat.cl +176 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_id_q4_0_f32_8x_flat.cl +283 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_id_q8_0_f32.cl +140 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_id_q8_0_f32_flat.cl +222 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_mxfp4_f32.cl +144 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_mxfp4_f32_flat.cl +167 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q8_0_f32.cl +125 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q8_0_f32_flat.cl +202 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/norm.cl +80 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/pad.cl +30 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/repeat.cl +39 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/rms_norm.cl +79 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/scale.cl +3 -2
- data/ext/sources/ggml/src/ggml-opencl/kernels/set_rows.cl +189 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/sigmoid.cl +29 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/softmax_4_f16.cl +34 -13
- data/ext/sources/ggml/src/ggml-opencl/kernels/softmax_4_f32.cl +34 -13
- data/ext/sources/ggml/src/ggml-opencl/kernels/softmax_f16.cl +34 -13
- data/ext/sources/ggml/src/ggml-opencl/kernels/softmax_f32.cl +34 -13
- data/ext/sources/ggml/src/ggml-opencl/kernels/sub.cl +138 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/sum_rows.cl +39 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/tanh.cl +63 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/transpose.cl +20 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/tsembd.cl +48 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/upscale.cl +120 -0
- data/ext/sources/ggml/src/ggml-opt.cpp +97 -41
- data/ext/sources/ggml/src/ggml-quants.c +117 -24
- data/ext/sources/ggml/src/ggml-quants.h +6 -0
- data/ext/sources/ggml/src/ggml-rpc/ggml-rpc.cpp +85 -62
- data/ext/sources/ggml/src/ggml-sycl/CMakeLists.txt +3 -3
- data/ext/sources/ggml/src/ggml-sycl/backend.hpp +2 -0
- data/ext/sources/ggml/src/ggml-sycl/binbcast.cpp +9 -0
- data/ext/sources/ggml/src/ggml-sycl/binbcast.hpp +6 -0
- data/ext/sources/ggml/src/ggml-sycl/common.hpp +20 -48
- data/ext/sources/ggml/src/ggml-sycl/concat.cpp +13 -17
- data/ext/sources/ggml/src/ggml-sycl/convert.cpp +21 -2
- data/ext/sources/ggml/src/ggml-sycl/cpy.cpp +116 -211
- data/ext/sources/ggml/src/ggml-sycl/cpy.hpp +213 -1
- data/ext/sources/ggml/src/ggml-sycl/dequantize.hpp +32 -0
- data/ext/sources/ggml/src/ggml-sycl/element_wise.cpp +700 -1041
- data/ext/sources/ggml/src/ggml-sycl/element_wise.hpp +20 -9
- data/ext/sources/ggml/src/ggml-sycl/gemm.hpp +17 -26
- data/ext/sources/ggml/src/ggml-sycl/getrows.cpp +2 -96
- data/ext/sources/ggml/src/ggml-sycl/ggml-sycl.cpp +393 -250
- data/ext/sources/ggml/src/ggml-sycl/im2col.cpp +1 -1
- data/ext/sources/ggml/src/ggml-sycl/mmvq.cpp +32 -8
- data/ext/sources/ggml/src/ggml-sycl/quantize.hpp +133 -0
- data/ext/sources/ggml/src/ggml-sycl/quants.hpp +38 -11
- data/ext/sources/ggml/src/ggml-sycl/rope.cpp +125 -21
- data/ext/sources/ggml/src/ggml-sycl/set_rows.cpp +234 -0
- data/ext/sources/ggml/src/ggml-sycl/set_rows.hpp +8 -0
- data/ext/sources/ggml/src/ggml-sycl/sycl_hw.cpp +3 -1
- data/ext/sources/ggml/src/ggml-sycl/sycl_hw.hpp +3 -0
- data/ext/sources/ggml/src/ggml-sycl/tsembd.cpp +4 -3
- data/ext/sources/ggml/src/ggml-sycl/vecdotq.hpp +105 -17
- data/ext/sources/ggml/src/ggml-vulkan/CMakeLists.txt +36 -32
- data/ext/sources/ggml/src/ggml-vulkan/ggml-vulkan.cpp +4198 -1145
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt +4 -12
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/add.comp +41 -1
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/add_id.comp +42 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/argmax.comp +13 -4
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/argsort.comp +39 -29
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/conv2d_mm.comp +349 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/conv_transpose_1d.comp +98 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/copy_from_quant.comp +2 -2
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/copy_to_quant.comp +66 -12
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_funcs.comp +154 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_funcs_cm2.comp +21 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq2_s.comp +1 -1
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq2_xxs.comp +2 -1
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq3_s.comp +6 -5
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq3_xxs.comp +4 -2
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_mxfp4.comp +32 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q2_k.comp +1 -1
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q3_k.comp +1 -1
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q4_k.comp +1 -1
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q5_k.comp +1 -1
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q6_k.comp +1 -1
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/exp.comp +21 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn.comp +69 -24
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_base.comp +60 -20
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm1.comp +98 -42
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm2.comp +64 -27
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_split_k_reduce.comp +74 -13
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/geglu.comp +13 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/geglu_erf.comp +27 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/geglu_quick.comp +11 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/gelu_erf.comp +39 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/generic_binary_head.comp +4 -17
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/get_rows.comp +19 -10
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/get_rows_quant.comp +25 -15
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/glu_head.comp +19 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/glu_main.comp +29 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/hardsigmoid.comp +22 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/hardswish.comp +22 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/im2col.comp +18 -14
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/im2col_3d.comp +126 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_base.comp +65 -1
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_nc.comp +11 -7
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vecq.comp +140 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm.comp +144 -531
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm_cm2.comp +206 -38
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm_funcs.comp +556 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mmq.comp +12 -5
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mmq_funcs.comp +15 -9
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/multi_add.comp +111 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/opt_step_sgd.comp +22 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/pad.comp +24 -3
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/quantize_q8_1.comp +53 -3
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/reglu.comp +9 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rms_norm.comp +64 -11
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rms_norm_partials.comp +65 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/roll.comp +46 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_head.comp +1 -4
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_multi.comp +7 -9
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_neox.comp +7 -9
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_norm.comp +7 -9
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rte.comp +5 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/scale.comp +1 -1
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/soft_max.comp +29 -7
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/soft_max_back.comp +4 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/sqrt.comp +17 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/sum_rows.comp +38 -5
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/swiglu.comp +9 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/swiglu_oai.comp +14 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/timestep_embedding.comp +4 -3
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/types.comp +101 -9
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/upscale.comp +69 -5
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/utils.comp +25 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +338 -71
- data/ext/sources/ggml/src/ggml-webgpu/CMakeLists.txt +54 -0
- data/ext/sources/ggml/src/ggml-webgpu/ggml-webgpu.cpp +1558 -0
- data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/add.tmpl.wgsl +44 -0
- data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/add_in_place.tmpl.wgsl +41 -0
- data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/binary_head.tmpl +45 -0
- data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/common_decls.tmpl +930 -0
- data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/cpy.wgsl +60 -0
- data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/embed_wgsl.py +124 -0
- data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/get_rows.tmpl.wgsl +874 -0
- data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/memset.wgsl +40 -0
- data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul.tmpl.wgsl +44 -0
- data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_in_place.tmpl.wgsl +41 -0
- data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat.tmpl.wgsl +907 -0
- data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/rms_norm.wgsl +57 -0
- data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/rms_norm_in_place.wgsl +48 -0
- data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/set_rows.wgsl +81 -0
- data/ext/sources/ggml/src/ggml-zdnn/CMakeLists.txt +36 -0
- data/ext/sources/ggml/src/ggml-zdnn/common.hpp +59 -0
- data/ext/sources/ggml/src/ggml-zdnn/ggml-zdnn.cpp +628 -0
- data/ext/sources/ggml/src/ggml-zdnn/mmf.cpp +80 -0
- data/ext/sources/ggml/src/ggml-zdnn/mmf.hpp +12 -0
- data/ext/sources/ggml/src/ggml-zdnn/utils.cpp +79 -0
- data/ext/sources/ggml/src/ggml-zdnn/utils.hpp +19 -0
- data/ext/sources/ggml/src/ggml.c +802 -142
- data/ext/sources/ggml/src/ggml.cpp +26 -0
- data/ext/sources/ggml/src/gguf.cpp +32 -4
- data/ext/sources/include/whisper.h +2 -0
- data/ext/sources/src/CMakeLists.txt +2 -0
- data/ext/sources/src/coreml/whisper-compat.h +10 -0
- data/ext/sources/src/coreml/whisper-compat.m +35 -0
- data/ext/sources/src/coreml/whisper-decoder-impl.m +1 -0
- data/ext/sources/src/coreml/whisper-encoder-impl.m +1 -0
- data/ext/sources/src/whisper.cpp +241 -215
- data/ext/sources/tests/CMakeLists.txt +8 -1
- data/ext/sources/tests/test-vad-full.cpp +3 -3
- data/ext/sources/tests/test-vad.cpp +2 -2
- data/extsources.rb +15 -9
- data/lib/whisper/context.rb +15 -0
- data/lib/whisper/model/uri.rb +57 -2
- data/lib/whisper/segment.rb +58 -0
- data/sig/whisper.rbs +75 -38
- data/{tests → test}/helper.rb +1 -12
- data/{tests → test}/test_model.rb +9 -0
- data/test/test_package.rb +51 -0
- data/{tests → test}/test_params.rb +8 -0
- data/test/test_segment.rb +146 -0
- data/{tests → test}/test_whisper.rb +70 -0
- data/whispercpp.gemspec +2 -3
- metadata +246 -191
- data/ext/sources/.dockerignore +0 -3
- data/ext/sources/.github/workflows/bindings-ruby.yml +0 -21
- data/ext/sources/ci/run.sh +0 -336
- data/ext/sources/close-issue.yml +0 -28
- data/ext/sources/ggml/include/ggml-kompute.h +0 -50
- data/ext/sources/ggml/src/ggml-amx/CMakeLists.txt +0 -107
- data/ext/sources/ggml/src/ggml-amx/common.h +0 -94
- data/ext/sources/ggml/src/ggml-amx/ggml-amx.cpp +0 -446
- data/ext/sources/ggml/src/ggml-amx/mmq.cpp +0 -2510
- data/ext/sources/ggml/src/ggml-amx/mmq.h +0 -17
- data/ext/sources/ggml/src/ggml-cann/kernels/CMakeLists.txt +0 -30
- data/ext/sources/ggml/src/ggml-cann/kernels/ascendc_kernels.h +0 -19
- data/ext/sources/ggml/src/ggml-cann/kernels/dup.cpp +0 -234
- data/ext/sources/ggml/src/ggml-cann/kernels/get_row_f16.cpp +0 -197
- data/ext/sources/ggml/src/ggml-cann/kernels/get_row_f32.cpp +0 -190
- data/ext/sources/ggml/src/ggml-cann/kernels/get_row_q4_0.cpp +0 -204
- data/ext/sources/ggml/src/ggml-cann/kernels/get_row_q8_0.cpp +0 -191
- data/ext/sources/ggml/src/ggml-cann/kernels/quantize_f16_q8_0.cpp +0 -218
- data/ext/sources/ggml/src/ggml-cann/kernels/quantize_f32_q8_0.cpp +0 -216
- data/ext/sources/ggml/src/ggml-cann/kernels/quantize_float_to_q4_0.cpp +0 -295
- data/ext/sources/ggml/src/ggml-cpu/ggml-cpu-aarch64.cpp +0 -6431
- data/ext/sources/ggml/src/ggml-cpu/ggml-cpu-aarch64.h +0 -8
- data/ext/sources/ggml/src/ggml-cpu/ggml-cpu-quants.c +0 -13747
- data/ext/sources/ggml/src/ggml-cuda/fattn-tile-f16.cu +0 -357
- data/ext/sources/ggml/src/ggml-cuda/fattn-tile-f16.cuh +0 -3
- data/ext/sources/ggml/src/ggml-cuda/fattn-tile-f32.cu +0 -365
- data/ext/sources/ggml/src/ggml-cuda/fattn-tile-f32.cuh +0 -3
- data/ext/sources/ggml/src/ggml-cuda/fattn-vec-f16.cuh +0 -482
- data/ext/sources/ggml/src/ggml-cuda/fattn-vec-f32.cuh +0 -472
- data/ext/sources/ggml/src/ggml-cuda/mmv.cu +0 -336
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-f16.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q4_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q4_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q5_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q5_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q8_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-f16.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q4_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q4_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q5_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q5_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q8_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-f16.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q4_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q4_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q5_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q5_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q8_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-f16.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q4_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q4_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q5_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q5_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q8_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-f16.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q4_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q4_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q5_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q5_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q8_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-f16.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q4_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q4_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q5_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q5_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q8_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs256-f16-f16.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-f16.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q4_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q4_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q5_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q5_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q8_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-f16.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q4_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q4_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q5_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q5_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q8_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-f16.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q4_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q4_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q5_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q5_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q8_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-f16.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q4_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q4_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q5_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q5_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q8_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-f16.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q4_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q4_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q5_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q5_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q8_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-f16.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q4_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q4_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q5_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q5_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q8_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-f16.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q4_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q4_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q5_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q5_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q8_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs256-f16-f16.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-f16.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q4_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q4_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q5_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q5_1.cu +0 -5
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q8_0.cu +0 -5
- data/ext/sources/ggml/src/ggml-kompute/CMakeLists.txt +0 -166
- data/ext/sources/ggml/src/ggml-kompute/ggml-kompute.cpp +0 -2251
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/common.comp +0 -112
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_add.comp +0 -58
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_addrow.comp +0 -25
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f16_f16.comp +0 -52
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f16_f32.comp +0 -52
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f32_f16.comp +0 -52
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f32_f32.comp +0 -52
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_diagmask.comp +0 -30
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_gelu.comp +0 -22
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows.comp +0 -17
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows_f16.comp +0 -31
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows_f32.comp +0 -31
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows_q4_0.comp +0 -38
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows_q4_1.comp +0 -39
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows_q6_k.comp +0 -44
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul.comp +0 -52
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_f16.comp +0 -69
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_mat_f32.comp +0 -51
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q4_0.comp +0 -33
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q4_1.comp +0 -35
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q4_k.comp +0 -140
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q6_k.comp +0 -106
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q8_0.comp +0 -73
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mv_q_n.comp +0 -52
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mv_q_n_pre.comp +0 -28
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_norm.comp +0 -84
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_relu.comp +0 -21
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_rmsnorm.comp +0 -53
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_rope_neox_f16.comp +0 -52
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_rope_neox_f32.comp +0 -52
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_rope_norm_f16.comp +0 -52
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_rope_norm_f32.comp +0 -52
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_scale.comp +0 -19
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_scale_8.comp +0 -23
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_silu.comp +0 -22
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_softmax.comp +0 -72
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/rope_common.comp +0 -71
- data/ext/sources/ggml/src/ggml-metal/ggml-metal.m +0 -5998
- data/tests/test_package.rb +0 -46
- data/tests/test_segment.rb +0 -74
- /data/ext/sources/ggml/src/ggml-cpu/{cpu-feats-x86.cpp → arch/x86/cpu-feats.cpp} +0 -0
- /data/ext/sources/ggml/src/ggml-cpu/{ggml-cpu-hbm.h → hbm.h} +0 -0
- /data/{tests → test}/jfk_reader/.gitignore +0 -0
- /data/{tests → test}/jfk_reader/extconf.rb +0 -0
- /data/{tests → test}/jfk_reader/jfk_reader.c +0 -0
- /data/{tests → test}/test_callback.rb +0 -0
- /data/{tests → test}/test_error.rb +0 -0
- /data/{tests → test}/test_vad.rb +0 -0
- /data/{tests → test}/test_vad_params.rb +0 -0
@@ -0,0 +1,1558 @@
|
|
1
|
+
/*
|
2
|
+
WebGPU backend implementation.
|
3
|
+
Note: Use ClangFormat to format this file.
|
4
|
+
*/
|
5
|
+
|
6
|
+
#include "ggml-webgpu.h"
|
7
|
+
|
8
|
+
#include "ggml-backend-impl.h"
|
9
|
+
#include "ggml-impl.h"
|
10
|
+
#include "ggml-wgsl-shaders.hpp"
|
11
|
+
|
12
|
+
#include <webgpu/webgpu_cpp.h>
|
13
|
+
|
14
|
+
#include <condition_variable>
|
15
|
+
#include <cstring>
|
16
|
+
#include <iostream>
|
17
|
+
#include <mutex>
|
18
|
+
#include <string>
|
19
|
+
#include <vector>
|
20
|
+
|
21
|
+
#ifdef GGML_WEBGPU_DEBUG
|
22
|
+
# define WEBGPU_LOG_DEBUG(msg) std::cout << msg << std::endl
|
23
|
+
# define WEBGPU_DEBUG_BUF_ELEMS 32
|
24
|
+
#else
|
25
|
+
# define WEBGPU_LOG_DEBUG(msg) ((void) 0)
|
26
|
+
#endif // GGML_WEBGPU_DEBUG
|
27
|
+
|
28
|
+
/* Constants */
|
29
|
+
|
30
|
+
#define WEBGPU_COMMAND_SUBMIT_BATCH_SIZE 16
|
31
|
+
#define WEBGPU_MUL_MAT_WG_SIZE 64
|
32
|
+
#define WEBGPU_NUM_PARAM_BUFS 100
|
33
|
+
#define WEBGPU_PARAMS_BUF_SIZE_BYTES 128 // enough for 32 parameters
|
34
|
+
#define WEBGPU_NUM_SET_ROWS_ERROR_BUFS 32
|
35
|
+
#define WEBGPU_SET_ROWS_ERROR_BUF_SIZE_BYTES 4
|
36
|
+
#define WEBGPU_STORAGE_BUF_BINDING_MULT 4 // a storage buffer binding size must be a multiple of 4
|
37
|
+
|
38
|
+
/* End Constants */
|
39
|
+
|
40
|
+
// This is a "fake" base pointer, since WebGPU buffers do not have pointers to their locations.
|
41
|
+
static void * const webgpu_ptr_base = (void *) (uintptr_t) 0x1000; // NOLINT
|
42
|
+
|
43
|
+
// Always returns the base offset of a tensor, regardless of views.
|
44
|
+
static uint64_t webgpu_tensor_offset(const ggml_tensor * tensor) {
|
45
|
+
if (tensor->view_src) {
|
46
|
+
return (uint8_t *) tensor->view_src->data - (uint8_t *) webgpu_ptr_base;
|
47
|
+
}
|
48
|
+
return (uint8_t *) tensor->data - (uint8_t *) webgpu_ptr_base;
|
49
|
+
}
|
50
|
+
|
51
|
+
/* Struct definitions */
|
52
|
+
|
53
|
+
// Forward reference
|
54
|
+
static void ggml_webgpu_create_buffer(wgpu::Device & device,
|
55
|
+
wgpu::Buffer & buffer,
|
56
|
+
size_t size,
|
57
|
+
wgpu::BufferUsage usage,
|
58
|
+
const char * label);
|
59
|
+
|
60
|
+
struct webgpu_pool_bufs {
|
61
|
+
wgpu::Buffer host_buf;
|
62
|
+
wgpu::Buffer dev_buf;
|
63
|
+
};
|
64
|
+
|
65
|
+
// Holds a pool of parameter buffers for WebGPU operations
|
66
|
+
struct webgpu_buf_pool {
|
67
|
+
std::vector<webgpu_pool_bufs> free;
|
68
|
+
|
69
|
+
std::mutex mutex;
|
70
|
+
|
71
|
+
std::condition_variable cv;
|
72
|
+
|
73
|
+
void init(wgpu::Device device,
|
74
|
+
int num_bufs,
|
75
|
+
size_t buf_size,
|
76
|
+
wgpu::BufferUsage dev_buf_usage,
|
77
|
+
wgpu::BufferUsage host_buf_usage) {
|
78
|
+
for (int i = 0; i < num_bufs; i++) {
|
79
|
+
wgpu::Buffer host_buf;
|
80
|
+
wgpu::Buffer dev_buf;
|
81
|
+
ggml_webgpu_create_buffer(device, host_buf, buf_size, host_buf_usage, "ggml_webgpu_host_pool_buf");
|
82
|
+
ggml_webgpu_create_buffer(device, dev_buf, buf_size, dev_buf_usage, "ggml_webgpu_dev_pool_buf");
|
83
|
+
free.push_back({ host_buf, dev_buf });
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
webgpu_pool_bufs alloc_bufs() {
|
88
|
+
std::unique_lock<std::mutex> lock(mutex);
|
89
|
+
cv.wait(lock, [this] { return !free.empty(); });
|
90
|
+
webgpu_pool_bufs bufs = free.back();
|
91
|
+
free.pop_back();
|
92
|
+
return bufs;
|
93
|
+
}
|
94
|
+
|
95
|
+
void free_bufs(std::vector<webgpu_pool_bufs> bufs) {
|
96
|
+
std::lock_guard<std::mutex> lock(mutex);
|
97
|
+
free.insert(free.end(), bufs.begin(), bufs.end());
|
98
|
+
cv.notify_all();
|
99
|
+
}
|
100
|
+
|
101
|
+
void cleanup() {
|
102
|
+
std::lock_guard<std::mutex> lock(mutex);
|
103
|
+
for (auto & bufs : free) {
|
104
|
+
bufs.host_buf.Destroy();
|
105
|
+
bufs.dev_buf.Destroy();
|
106
|
+
}
|
107
|
+
free.clear();
|
108
|
+
}
|
109
|
+
};
|
110
|
+
|
111
|
+
// All the base objects needed to run operations on a WebGPU device
|
112
|
+
struct webgpu_context_struct {
|
113
|
+
wgpu::Instance instance;
|
114
|
+
wgpu::Adapter adapter;
|
115
|
+
wgpu::Device device;
|
116
|
+
wgpu::Queue queue;
|
117
|
+
wgpu::Limits limits;
|
118
|
+
|
119
|
+
// Separate this out from limits since on some Metal systems, the limit returned by
|
120
|
+
// querying the limits is higher than the actual allowed maximum.
|
121
|
+
uint32_t max_wg_size_x;
|
122
|
+
|
123
|
+
std::recursive_mutex mutex;
|
124
|
+
|
125
|
+
webgpu_buf_pool param_buf_pool;
|
126
|
+
webgpu_buf_pool set_rows_error_buf_pool;
|
127
|
+
|
128
|
+
wgpu::ComputePipeline memset_pipeline;
|
129
|
+
wgpu::ComputePipeline mul_mat_pipeline[30][2];
|
130
|
+
wgpu::ComputePipeline set_rows_pipeline;
|
131
|
+
wgpu::ComputePipeline get_rows_pipeline[30];
|
132
|
+
wgpu::ComputePipeline get_rows_f32_no_vec_pipeline;
|
133
|
+
wgpu::ComputePipeline cpy_pipeline;
|
134
|
+
wgpu::ComputePipeline add_pipeline[2];
|
135
|
+
wgpu::ComputePipeline add_ip_pipeline[2];
|
136
|
+
wgpu::ComputePipeline mul_pipeline[2];
|
137
|
+
wgpu::ComputePipeline mul_ip_pipeline[2];
|
138
|
+
wgpu::ComputePipeline rms_norm_pipeline;
|
139
|
+
wgpu::ComputePipeline rms_norm_ip_pipeline;
|
140
|
+
|
141
|
+
size_t memset_bytes_per_thread;
|
142
|
+
|
143
|
+
// Staging buffer for reading data from the GPU
|
144
|
+
wgpu::Buffer get_tensor_staging_buf;
|
145
|
+
|
146
|
+
// Command buffers which need to be submitted
|
147
|
+
std::vector<wgpu::CommandBuffer> staged_command_bufs;
|
148
|
+
|
149
|
+
// Parameter buffers associated with the staged command buffers
|
150
|
+
std::vector<webgpu_pool_bufs> staged_param_bufs;
|
151
|
+
// Buffers associated with set_rows operations, used to store potential errors
|
152
|
+
std::vector<webgpu_pool_bufs> staged_set_row_error_bufs;
|
153
|
+
|
154
|
+
std::vector<wgpu::FutureWaitInfo> callback_futures;
|
155
|
+
|
156
|
+
#ifdef GGML_WEBGPU_DEBUG
|
157
|
+
wgpu::Buffer debug_host_buf;
|
158
|
+
wgpu::Buffer debug_dev_buf;
|
159
|
+
#endif
|
160
|
+
};
|
161
|
+
|
162
|
+
typedef std::shared_ptr<webgpu_context_struct> webgpu_context;
|
163
|
+
|
164
|
+
struct ggml_backend_webgpu_reg_context {
|
165
|
+
webgpu_context webgpu_ctx;
|
166
|
+
size_t device_count;
|
167
|
+
const char * name;
|
168
|
+
};
|
169
|
+
|
170
|
+
struct ggml_backend_webgpu_device_context {
|
171
|
+
webgpu_context webgpu_ctx;
|
172
|
+
std::string device_name;
|
173
|
+
std::string device_desc;
|
174
|
+
};
|
175
|
+
|
176
|
+
struct ggml_backend_webgpu_context {
|
177
|
+
webgpu_context webgpu_ctx;
|
178
|
+
std::string name;
|
179
|
+
};
|
180
|
+
|
181
|
+
struct ggml_backend_webgpu_buffer_context {
|
182
|
+
webgpu_context webgpu_ctx;
|
183
|
+
wgpu::Buffer buffer;
|
184
|
+
|
185
|
+
ggml_backend_webgpu_buffer_context(webgpu_context ctx, wgpu::Buffer buf) :
|
186
|
+
webgpu_ctx(std::move(ctx)),
|
187
|
+
buffer(std::move(buf)) {}
|
188
|
+
};
|
189
|
+
|
190
|
+
/* End struct definitions */
|
191
|
+
|
192
|
+
/* WebGPU object initializations */
|
193
|
+
|
194
|
+
static void ggml_webgpu_create_pipeline(wgpu::Device & device,
|
195
|
+
wgpu::ComputePipeline & pipeline,
|
196
|
+
const char * shader_code,
|
197
|
+
const char * label,
|
198
|
+
const std::vector<wgpu::ConstantEntry> & constants = {}) {
|
199
|
+
WEBGPU_LOG_DEBUG("ggml_webgpu_create_pipeline()");
|
200
|
+
|
201
|
+
wgpu::ShaderSourceWGSL shader_source;
|
202
|
+
shader_source.code = shader_code;
|
203
|
+
|
204
|
+
wgpu::ShaderModuleDescriptor shader_desc;
|
205
|
+
shader_desc.nextInChain = &shader_source;
|
206
|
+
|
207
|
+
wgpu::ShaderModule shader_module = device.CreateShaderModule(&shader_desc);
|
208
|
+
|
209
|
+
wgpu::ComputePipelineDescriptor pipeline_desc;
|
210
|
+
pipeline_desc.label = label;
|
211
|
+
pipeline_desc.compute.module = shader_module;
|
212
|
+
pipeline_desc.compute.entryPoint = "main"; // Entry point in the WGSL code
|
213
|
+
pipeline_desc.layout = nullptr; // nullptr means auto layout
|
214
|
+
if (constants.size() > 0) {
|
215
|
+
pipeline_desc.compute.constants = constants.data();
|
216
|
+
pipeline_desc.compute.constantCount = constants.size();
|
217
|
+
}
|
218
|
+
pipeline = device.CreateComputePipeline(&pipeline_desc);
|
219
|
+
}
|
220
|
+
|
221
|
+
static void ggml_webgpu_create_buffer(wgpu::Device & device,
|
222
|
+
wgpu::Buffer & buffer,
|
223
|
+
size_t size,
|
224
|
+
wgpu::BufferUsage usage,
|
225
|
+
const char * label) {
|
226
|
+
WEBGPU_LOG_DEBUG("ggml_webgpu_create_buffer()");
|
227
|
+
|
228
|
+
wgpu::BufferDescriptor buffer_desc;
|
229
|
+
buffer_desc.size = size;
|
230
|
+
buffer_desc.usage = usage;
|
231
|
+
buffer_desc.label = label;
|
232
|
+
buffer_desc.mappedAtCreation = false;
|
233
|
+
|
234
|
+
// TODO: error handling
|
235
|
+
buffer = device.CreateBuffer(&buffer_desc);
|
236
|
+
}
|
237
|
+
|
238
|
+
/** End WebGPU object initializations */
|
239
|
+
|
240
|
+
/** WebGPU Actions */
|
241
|
+
|
242
|
+
// Wait for the queue to finish processing all submitted work
|
243
|
+
static void ggml_backend_webgpu_wait_on_submission(webgpu_context & ctx) {
|
244
|
+
std::lock_guard<std::recursive_mutex> lock(ctx->mutex);
|
245
|
+
if (ctx->callback_futures.empty()) {
|
246
|
+
// no existing callbacks, wait on queue submission
|
247
|
+
ctx->instance.WaitAny(
|
248
|
+
ctx->queue.OnSubmittedWorkDone(wgpu::CallbackMode::AllowSpontaneous,
|
249
|
+
[](wgpu::QueueWorkDoneStatus status, wgpu::StringView message) {
|
250
|
+
if (status != wgpu::QueueWorkDoneStatus::Success) {
|
251
|
+
GGML_LOG_ERROR("ggml_webgpu: Failed to submit commands: %s\n",
|
252
|
+
std::string(message).c_str());
|
253
|
+
}
|
254
|
+
}),
|
255
|
+
UINT64_MAX);
|
256
|
+
} else {
|
257
|
+
// existing callbacks, wait on them
|
258
|
+
ctx->instance.WaitAny(ctx->callback_futures.size(), ctx->callback_futures.data(), UINT64_MAX);
|
259
|
+
ctx->callback_futures.clear();
|
260
|
+
}
|
261
|
+
}
|
262
|
+
|
263
|
+
static void ggml_backend_webgpu_submit_queue(webgpu_context & ctx) {
|
264
|
+
std::lock_guard<std::recursive_mutex> lock(ctx->mutex);
|
265
|
+
WEBGPU_LOG_DEBUG("ggml_backend_webgpu_submit_queue()");
|
266
|
+
if (ctx->staged_command_bufs.empty()) {
|
267
|
+
// Nothing to submit
|
268
|
+
return;
|
269
|
+
}
|
270
|
+
ctx->queue.Submit(ctx->staged_command_bufs.size(), ctx->staged_command_bufs.data());
|
271
|
+
|
272
|
+
// If there are SET_ROWS operations in this submission, copy their error buffers to the host.
|
273
|
+
if (ctx->staged_set_row_error_bufs.size() > 0) {
|
274
|
+
wgpu::CommandEncoder encoder = ctx->device.CreateCommandEncoder();
|
275
|
+
for (auto & error_bufs : ctx->staged_set_row_error_bufs) {
|
276
|
+
// Copy the error buffer to the host buffer
|
277
|
+
encoder.CopyBufferToBuffer(error_bufs.dev_buf, 0, error_bufs.host_buf, 0, error_bufs.host_buf.GetSize());
|
278
|
+
}
|
279
|
+
wgpu::CommandBuffer commands = encoder.Finish();
|
280
|
+
ctx->queue.Submit(1, &commands);
|
281
|
+
}
|
282
|
+
|
283
|
+
ctx->staged_command_bufs.clear();
|
284
|
+
std::vector<webgpu_pool_bufs> staged_param_bufs = std::move(ctx->staged_param_bufs);
|
285
|
+
std::vector<webgpu_pool_bufs> staged_set_row_error_bufs = std::move(ctx->staged_set_row_error_bufs);
|
286
|
+
|
287
|
+
// Free the staged parameter buffers once the submission completes
|
288
|
+
wgpu::Future p_f = ctx->queue.OnSubmittedWorkDone(
|
289
|
+
wgpu::CallbackMode::AllowSpontaneous,
|
290
|
+
[ctx, staged_param_bufs](wgpu::QueueWorkDoneStatus status, wgpu::StringView message) {
|
291
|
+
if (status != wgpu::QueueWorkDoneStatus::Success) {
|
292
|
+
GGML_LOG_ERROR("ggml_webgpu: Failed to submit commands: %s\n", std::string(message).c_str());
|
293
|
+
}
|
294
|
+
// Free the staged buffers
|
295
|
+
ctx->param_buf_pool.free_bufs(staged_param_bufs);
|
296
|
+
});
|
297
|
+
ctx->callback_futures.push_back({ p_f });
|
298
|
+
|
299
|
+
// Check for errrors in SET_ROWS operations
|
300
|
+
for (auto & error_bufs : staged_set_row_error_bufs) {
|
301
|
+
wgpu::Future f = error_bufs.host_buf.MapAsync(
|
302
|
+
wgpu::MapMode::Read, 0, error_bufs.host_buf.GetSize(), wgpu::CallbackMode::AllowSpontaneous,
|
303
|
+
[ctx, error_bufs](wgpu::MapAsyncStatus status, wgpu::StringView message) {
|
304
|
+
if (status != wgpu::MapAsyncStatus::Success) {
|
305
|
+
GGML_LOG_ERROR("ggml_webgpu: Failed to map error buffer: %s\n", std::string(message).c_str());
|
306
|
+
} else {
|
307
|
+
const uint32_t * error_data = (const uint32_t *) error_bufs.host_buf.GetConstMappedRange();
|
308
|
+
if (*error_data) {
|
309
|
+
GGML_ABORT("ggml_webgpu: SET_ROWS index > 2^32, unsupported.");
|
310
|
+
}
|
311
|
+
// We can't unmap in here due to WebGPU reentrancy limitations.
|
312
|
+
ctx->set_rows_error_buf_pool.free_bufs({ error_bufs });
|
313
|
+
}
|
314
|
+
});
|
315
|
+
ctx->callback_futures.push_back({ f });
|
316
|
+
}
|
317
|
+
}
|
318
|
+
|
319
|
+
static void ggml_backend_webgpu_map_buffer(webgpu_context & ctx,
|
320
|
+
wgpu::Buffer & buffer,
|
321
|
+
wgpu::MapMode mode,
|
322
|
+
size_t offset,
|
323
|
+
size_t size) {
|
324
|
+
ctx->instance.WaitAny(buffer.MapAsync(mode, offset, size, wgpu::CallbackMode::AllowSpontaneous,
|
325
|
+
[](wgpu::MapAsyncStatus status, wgpu::StringView message) {
|
326
|
+
if (status != wgpu::MapAsyncStatus::Success) {
|
327
|
+
GGML_LOG_ERROR("ggml_webgpu: Failed to map buffer: %s\n",
|
328
|
+
message.data);
|
329
|
+
}
|
330
|
+
}),
|
331
|
+
UINT64_MAX);
|
332
|
+
}
|
333
|
+
|
334
|
+
#ifdef GGML_WEBGPU_DEBUG
|
335
|
+
// This function adds debugging information to shaders, as WebGPU does not support printing directly.
|
336
|
+
// To use, add a bind group entry to the setup for the shader you are debugging, add the buffer and
|
337
|
+
// debug statements in the shader, and then call this function after encoding the commands and submitting them.
|
338
|
+
static void ggml_backend_webgpu_debug(webgpu_context & ctx) {
|
339
|
+
ggml_backend_webgpu_submit_queue(ctx);
|
340
|
+
wgpu::CommandEncoder encoder = ctx->device.CreateCommandEncoder();
|
341
|
+
encoder.CopyBufferToBuffer(ctx->debug_dev_buf, 0, ctx->debug_host_buf, 0, ctx->debug_host_buf.GetSize());
|
342
|
+
wgpu::CommandBuffer commands = encoder.Finish();
|
343
|
+
ctx->queue.Submit(1, &commands);
|
344
|
+
|
345
|
+
ggml_backend_webgpu_map_buffer(ctx, ctx->debug_host_buf, wgpu::MapMode::Read, 0, ctx->debug_host_buf.GetSize());
|
346
|
+
const uint32_t * debug_data = (const uint32_t *) ctx->debug_host_buf.GetConstMappedRange();
|
347
|
+
std::cout << "debug data:";
|
348
|
+
for (size_t i = 0; i < WEBGPU_DEBUG_BUF_ELEMS; i++) {
|
349
|
+
std::cout << " " << i << ": " << debug_data[i];
|
350
|
+
}
|
351
|
+
std::cout << "\n";
|
352
|
+
ctx->debug_host_buf.Unmap();
|
353
|
+
}
|
354
|
+
#endif
|
355
|
+
|
356
|
+
static void ggml_backend_webgpu_build_and_enqueue(webgpu_context & ctx,
|
357
|
+
wgpu::ComputePipeline & pipeline,
|
358
|
+
std::vector<uint32_t> params,
|
359
|
+
std::vector<wgpu::BindGroupEntry> bind_group_entries,
|
360
|
+
uint32_t wg_x,
|
361
|
+
const char * bind_group_label = nullptr,
|
362
|
+
bool submit_and_wait = false) {
|
363
|
+
webgpu_pool_bufs params_bufs = ctx->param_buf_pool.alloc_bufs();
|
364
|
+
|
365
|
+
ggml_backend_webgpu_map_buffer(ctx, params_bufs.host_buf, wgpu::MapMode::Write, 0, params_bufs.host_buf.GetSize());
|
366
|
+
uint32_t * _params = (uint32_t *) params_bufs.host_buf.GetMappedRange();
|
367
|
+
for (size_t i = 0; i < params.size(); i++) {
|
368
|
+
_params[i] = params[i];
|
369
|
+
};
|
370
|
+
|
371
|
+
params_bufs.host_buf.Unmap();
|
372
|
+
|
373
|
+
uint32_t params_bufs_binding_num = bind_group_entries.size();
|
374
|
+
bind_group_entries.push_back({ .binding = params_bufs_binding_num,
|
375
|
+
.buffer = params_bufs.dev_buf,
|
376
|
+
.offset = 0,
|
377
|
+
.size = params_bufs.dev_buf.GetSize() });
|
378
|
+
|
379
|
+
wgpu::BindGroupDescriptor bind_group_desc;
|
380
|
+
bind_group_desc.layout = pipeline.GetBindGroupLayout(0);
|
381
|
+
bind_group_desc.entryCount = bind_group_entries.size();
|
382
|
+
bind_group_desc.entries = bind_group_entries.data();
|
383
|
+
if (bind_group_label) {
|
384
|
+
bind_group_desc.label = bind_group_label;
|
385
|
+
}
|
386
|
+
wgpu::BindGroup bind_group = ctx->device.CreateBindGroup(&bind_group_desc);
|
387
|
+
|
388
|
+
wgpu::CommandEncoder encoder = ctx->device.CreateCommandEncoder();
|
389
|
+
encoder.CopyBufferToBuffer(params_bufs.host_buf, 0, params_bufs.dev_buf, 0, params_bufs.dev_buf.GetSize());
|
390
|
+
wgpu::ComputePassEncoder pass = encoder.BeginComputePass();
|
391
|
+
pass.SetPipeline(pipeline);
|
392
|
+
pass.SetBindGroup(0, bind_group);
|
393
|
+
pass.DispatchWorkgroups(wg_x, 1, 1);
|
394
|
+
pass.End();
|
395
|
+
wgpu::CommandBuffer commands = encoder.Finish();
|
396
|
+
if (submit_and_wait) {
|
397
|
+
// Submit and wait immediately
|
398
|
+
ctx->queue.Submit(1, &commands);
|
399
|
+
ctx->instance.WaitAny(ctx->queue.OnSubmittedWorkDone(
|
400
|
+
wgpu::CallbackMode::AllowSpontaneous,
|
401
|
+
[ctx, params_bufs](wgpu::QueueWorkDoneStatus status, wgpu::StringView message) {
|
402
|
+
if (status != wgpu::QueueWorkDoneStatus::Success) {
|
403
|
+
GGML_LOG_ERROR("ggml_webgpu: Failed to submit commands: %s\n", message.data);
|
404
|
+
}
|
405
|
+
ctx->param_buf_pool.free_bufs({ params_bufs });
|
406
|
+
}),
|
407
|
+
UINT64_MAX);
|
408
|
+
} else {
|
409
|
+
// Lock the context mutex when pushing to the staging vectors.
|
410
|
+
std::lock_guard<std::recursive_mutex> lock(ctx->mutex);
|
411
|
+
// Enqueue commands and only submit if we have enough staged commands
|
412
|
+
ctx->staged_command_bufs.push_back(commands);
|
413
|
+
ctx->staged_param_bufs.push_back(params_bufs);
|
414
|
+
if (ctx->staged_command_bufs.size() == WEBGPU_COMMAND_SUBMIT_BATCH_SIZE) {
|
415
|
+
ggml_backend_webgpu_submit_queue(ctx);
|
416
|
+
}
|
417
|
+
}
|
418
|
+
}
|
419
|
+
|
420
|
+
static void ggml_backend_webgpu_buffer_memset(webgpu_context & ctx,
|
421
|
+
wgpu::Buffer & buf,
|
422
|
+
uint32_t value,
|
423
|
+
size_t offset,
|
424
|
+
size_t size) {
|
425
|
+
std::vector<uint32_t> params = { (uint32_t) offset, (uint32_t) size, value };
|
426
|
+
std::vector<wgpu::BindGroupEntry> entries = {
|
427
|
+
{ .binding = 0, .buffer = buf, .offset = 0, .size = buf.GetSize() }
|
428
|
+
};
|
429
|
+
size_t bytes_per_wg = ctx->max_wg_size_x * ctx->memset_bytes_per_thread;
|
430
|
+
uint32_t wg_x = ((size + 3) + bytes_per_wg - 1) / bytes_per_wg;
|
431
|
+
ggml_backend_webgpu_build_and_enqueue(ctx, ctx->memset_pipeline, params, entries, wg_x, "MEMSET", true);
|
432
|
+
}
|
433
|
+
|
434
|
+
/** End WebGPU Actions */
|
435
|
+
|
436
|
+
/** GGML Backend Interface */
|
437
|
+
|
438
|
+
static const char * ggml_backend_webgpu_name(ggml_backend_t backend) {
|
439
|
+
ggml_backend_webgpu_context * ctx = (ggml_backend_webgpu_context *) backend->context;
|
440
|
+
return ctx->name.c_str();
|
441
|
+
}
|
442
|
+
|
443
|
+
static void ggml_backend_webgpu_free(ggml_backend_t backend) {
|
444
|
+
ggml_backend_webgpu_context * ctx = (ggml_backend_webgpu_context *) backend->context;
|
445
|
+
WEBGPU_LOG_DEBUG("ggml_backend_webgpu_free(" << ctx->name << ")");
|
446
|
+
|
447
|
+
// TODO: cleanup
|
448
|
+
GGML_UNUSED(ctx);
|
449
|
+
}
|
450
|
+
|
451
|
+
static size_t ggml_webgpu_tensor_offset(const ggml_tensor * tensor) {
|
452
|
+
return webgpu_tensor_offset(tensor) + tensor->view_offs;
|
453
|
+
}
|
454
|
+
|
455
|
+
static wgpu::Buffer ggml_webgpu_tensor_buf(const ggml_tensor * tensor) {
|
456
|
+
ggml_backend_webgpu_buffer_context * ctx = (ggml_backend_webgpu_buffer_context *) tensor->buffer->context;
|
457
|
+
return ctx->buffer;
|
458
|
+
}
|
459
|
+
|
460
|
+
static size_t ggml_webgpu_tensor_misalignment(webgpu_context & ctx, ggml_tensor * t) {
|
461
|
+
size_t offset = ggml_webgpu_tensor_offset(t);
|
462
|
+
return offset & (ctx->limits.minStorageBufferOffsetAlignment - 1);
|
463
|
+
}
|
464
|
+
|
465
|
+
static size_t ggml_webgpu_tensor_align_offset(webgpu_context & ctx, ggml_tensor * t) {
|
466
|
+
size_t offset = ggml_webgpu_tensor_offset(t);
|
467
|
+
return offset & ~(ctx->limits.minStorageBufferOffsetAlignment - 1);
|
468
|
+
}
|
469
|
+
|
470
|
+
static size_t ggml_webgpu_tensor_binding_size(webgpu_context & ctx, ggml_tensor * t) {
|
471
|
+
return (ggml_nbytes(t) + ggml_webgpu_tensor_misalignment(ctx, t) + WEBGPU_STORAGE_BUF_BINDING_MULT - 1) &
|
472
|
+
~(WEBGPU_STORAGE_BUF_BINDING_MULT - 1);
|
473
|
+
}
|
474
|
+
|
475
|
+
// Used to determine if two tensors are the same for in-place operations
|
476
|
+
static bool ggml_webgpu_tensor_equal(ggml_tensor * a, ggml_tensor * b) {
|
477
|
+
return (ggml_webgpu_tensor_buf(a).Get() == ggml_webgpu_tensor_buf(b).Get()) &&
|
478
|
+
(ggml_webgpu_tensor_offset(a) == ggml_webgpu_tensor_offset(b));
|
479
|
+
}
|
480
|
+
|
481
|
+
static void ggml_webgpu_cpy(webgpu_context & ctx, ggml_tensor * src, ggml_tensor * dst) {
|
482
|
+
uint32_t ne = (uint32_t) ggml_nelements(dst);
|
483
|
+
|
484
|
+
std::vector<uint32_t> params = {
|
485
|
+
ne, (uint32_t) (ggml_webgpu_tensor_misalignment(ctx, src) / ggml_type_size(src->type)),
|
486
|
+
(uint32_t) (ggml_webgpu_tensor_misalignment(ctx, dst) / ggml_type_size(dst->type)),
|
487
|
+
// Convert byte-strides to element-strides
|
488
|
+
(uint32_t) (src->nb[0] / ggml_type_size(src->type)), (uint32_t) (src->nb[1] / ggml_type_size(src->type)),
|
489
|
+
(uint32_t) (src->nb[2] / ggml_type_size(src->type)), (uint32_t) (src->nb[3] / ggml_type_size(src->type)),
|
490
|
+
(uint32_t) (dst->nb[0] / ggml_type_size(dst->type)), (uint32_t) (dst->nb[1] / ggml_type_size(dst->type)),
|
491
|
+
(uint32_t) (dst->nb[2] / ggml_type_size(dst->type)), (uint32_t) (dst->nb[3] / ggml_type_size(dst->type)),
|
492
|
+
// Logical shape — same for both tensors even if permuted
|
493
|
+
(uint32_t) src->ne[0], (uint32_t) src->ne[1], (uint32_t) src->ne[2], (uint32_t) src->ne[3]
|
494
|
+
};
|
495
|
+
|
496
|
+
std::vector<wgpu::BindGroupEntry> entries = {
|
497
|
+
{ .binding = 0,
|
498
|
+
.buffer = ggml_webgpu_tensor_buf(src),
|
499
|
+
.offset = ggml_webgpu_tensor_align_offset(ctx, src),
|
500
|
+
.size = ggml_webgpu_tensor_binding_size(ctx, src) },
|
501
|
+
{ .binding = 1,
|
502
|
+
.buffer = ggml_webgpu_tensor_buf(dst),
|
503
|
+
.offset = ggml_webgpu_tensor_align_offset(ctx, dst),
|
504
|
+
.size = ggml_webgpu_tensor_binding_size(ctx, dst) }
|
505
|
+
};
|
506
|
+
|
507
|
+
size_t max_wg_size = ctx->max_wg_size_x;
|
508
|
+
uint32_t wg_x = (ne + max_wg_size - 1) / max_wg_size;
|
509
|
+
ggml_backend_webgpu_build_and_enqueue(ctx, ctx->cpy_pipeline, params, entries, wg_x, ggml_op_name(dst->op));
|
510
|
+
}
|
511
|
+
|
512
|
+
static void ggml_webgpu_set_rows(webgpu_context & ctx, ggml_tensor * src, ggml_tensor * idx, ggml_tensor * dst) {
|
513
|
+
// For set rows specifically, we need to check if src and idx are empty tensors.
|
514
|
+
if (ggml_is_empty(src) || ggml_is_empty(idx)) {
|
515
|
+
return;
|
516
|
+
}
|
517
|
+
|
518
|
+
webgpu_pool_bufs error_bufs = ctx->set_rows_error_buf_pool.alloc_bufs();
|
519
|
+
if (error_bufs.host_buf.GetMapState() == wgpu::BufferMapState::Mapped) {
|
520
|
+
error_bufs.host_buf.Unmap();
|
521
|
+
}
|
522
|
+
|
523
|
+
std::vector<uint32_t> params = {
|
524
|
+
(uint32_t) (ggml_webgpu_tensor_misalignment(ctx, src) / ggml_type_size(src->type)),
|
525
|
+
(uint32_t) (ggml_webgpu_tensor_misalignment(ctx, idx) / ggml_type_size(idx->type)),
|
526
|
+
(uint32_t) (ggml_webgpu_tensor_misalignment(ctx, dst) / ggml_type_size(dst->type)),
|
527
|
+
// Convert byte-strides to element-strides
|
528
|
+
(uint32_t) (src->nb[1] / ggml_type_size(src->type)), (uint32_t) (src->nb[2] / ggml_type_size(src->type)),
|
529
|
+
(uint32_t) (src->nb[3] / ggml_type_size(src->type)), (uint32_t) (idx->nb[0] / ggml_type_size(idx->type)),
|
530
|
+
(uint32_t) (idx->nb[1] / ggml_type_size(idx->type)), (uint32_t) (idx->nb[2] / ggml_type_size(idx->type)),
|
531
|
+
(uint32_t) (dst->nb[1] / ggml_type_size(dst->type)), (uint32_t) (dst->nb[2] / ggml_type_size(dst->type)),
|
532
|
+
(uint32_t) (dst->nb[3] / ggml_type_size(dst->type)),
|
533
|
+
// Shape of src
|
534
|
+
(uint32_t) src->ne[0], (uint32_t) src->ne[1], (uint32_t) src->ne[2], (uint32_t) src->ne[3],
|
535
|
+
// Shape of idx
|
536
|
+
(uint32_t) (idx->ne[1]), (uint32_t) (idx->ne[2])
|
537
|
+
};
|
538
|
+
|
539
|
+
std::vector<wgpu::BindGroupEntry> entries = {
|
540
|
+
{ .binding = 0,
|
541
|
+
.buffer = ggml_webgpu_tensor_buf(src),
|
542
|
+
.offset = ggml_webgpu_tensor_align_offset(ctx, src),
|
543
|
+
.size = ggml_webgpu_tensor_binding_size(ctx, src) },
|
544
|
+
{ .binding = 1,
|
545
|
+
.buffer = ggml_webgpu_tensor_buf(idx),
|
546
|
+
.offset = ggml_webgpu_tensor_align_offset(ctx, idx),
|
547
|
+
.size = ggml_webgpu_tensor_binding_size(ctx, idx) },
|
548
|
+
{ .binding = 2,
|
549
|
+
.buffer = ggml_webgpu_tensor_buf(dst),
|
550
|
+
.offset = ggml_webgpu_tensor_align_offset(ctx, dst),
|
551
|
+
.size = ggml_webgpu_tensor_binding_size(ctx, dst) },
|
552
|
+
{ .binding = 3, .buffer = error_bufs.dev_buf, .offset = 0, .size = error_bufs.dev_buf.GetSize() }
|
553
|
+
};
|
554
|
+
|
555
|
+
size_t max_wg_size = ctx->max_wg_size_x;
|
556
|
+
uint32_t wg_x = (src->ne[1] * src->ne[2] * src->ne[3] + max_wg_size - 1) / max_wg_size;
|
557
|
+
|
558
|
+
std::lock_guard<std::recursive_mutex> lock(ctx->mutex);
|
559
|
+
ctx->staged_set_row_error_bufs.push_back(error_bufs);
|
560
|
+
|
561
|
+
ggml_backend_webgpu_build_and_enqueue(ctx, ctx->set_rows_pipeline, params, entries, wg_x, ggml_op_name(dst->op));
|
562
|
+
}
|
563
|
+
|
564
|
+
static void ggml_webgpu_get_rows(webgpu_context & ctx, ggml_tensor * src, ggml_tensor * idx, ggml_tensor * dst) {
|
565
|
+
std::vector<uint32_t> params = {
|
566
|
+
(uint32_t) (ggml_webgpu_tensor_misalignment(ctx, src) / ggml_type_size(src->type)),
|
567
|
+
(uint32_t) (ggml_webgpu_tensor_misalignment(ctx, idx) / ggml_type_size(idx->type)),
|
568
|
+
(uint32_t) (ggml_webgpu_tensor_misalignment(ctx, dst) / ggml_type_size(dst->type)),
|
569
|
+
// Convert byte-strides to element-strides
|
570
|
+
(uint32_t) (src->nb[1] / ggml_type_size(src->type)), (uint32_t) (src->nb[2] / ggml_type_size(src->type)),
|
571
|
+
(uint32_t) (src->nb[3] / ggml_type_size(src->type)), (uint32_t) (idx->nb[0] / ggml_type_size(idx->type)),
|
572
|
+
(uint32_t) (idx->nb[1] / ggml_type_size(idx->type)), (uint32_t) (idx->nb[2] / ggml_type_size(idx->type)),
|
573
|
+
(uint32_t) (dst->nb[1] / ggml_type_size(dst->type)), (uint32_t) (dst->nb[2] / ggml_type_size(dst->type)),
|
574
|
+
(uint32_t) (dst->nb[3] / ggml_type_size(dst->type)),
|
575
|
+
// Shape of dst
|
576
|
+
(uint32_t) dst->ne[0], (uint32_t) dst->ne[1], (uint32_t) dst->ne[2], (uint32_t) dst->ne[3],
|
577
|
+
// Shape of idx
|
578
|
+
(uint32_t) (idx->ne[1]), (uint32_t) (idx->ne[2])
|
579
|
+
};
|
580
|
+
|
581
|
+
std::vector<wgpu::BindGroupEntry> entries = {
|
582
|
+
{ .binding = 0,
|
583
|
+
.buffer = ggml_webgpu_tensor_buf(src),
|
584
|
+
.offset = ggml_webgpu_tensor_align_offset(ctx, src),
|
585
|
+
.size = ggml_webgpu_tensor_binding_size(ctx, src) },
|
586
|
+
{ .binding = 1,
|
587
|
+
.buffer = ggml_webgpu_tensor_buf(idx),
|
588
|
+
.offset = ggml_webgpu_tensor_align_offset(ctx, idx),
|
589
|
+
.size = ggml_webgpu_tensor_binding_size(ctx, idx) },
|
590
|
+
{ .binding = 2,
|
591
|
+
.buffer = ggml_webgpu_tensor_buf(dst),
|
592
|
+
.offset = ggml_webgpu_tensor_align_offset(ctx, dst),
|
593
|
+
.size = ggml_webgpu_tensor_binding_size(ctx, dst) }
|
594
|
+
};
|
595
|
+
|
596
|
+
size_t max_wg_size = ctx->max_wg_size_x;
|
597
|
+
uint32_t wg_x = (dst->ne[1] * dst->ne[2] * dst->ne[3] + max_wg_size - 1) / max_wg_size;
|
598
|
+
|
599
|
+
wgpu::ComputePipeline pipeline = ctx->get_rows_pipeline[src->type];
|
600
|
+
if (src->type == GGML_TYPE_F32 && dst->ne[0] % 4 != 0) {
|
601
|
+
pipeline = ctx->get_rows_f32_no_vec_pipeline;
|
602
|
+
}
|
603
|
+
ggml_backend_webgpu_build_and_enqueue(ctx, pipeline, params, entries, wg_x, ggml_op_name(dst->op));
|
604
|
+
}
|
605
|
+
|
606
|
+
static void ggml_webgpu_mul_mat(webgpu_context & ctx, ggml_tensor * src0, ggml_tensor * src1, ggml_tensor * dst) {
|
607
|
+
std::vector<uint32_t> params = {
|
608
|
+
(uint32_t) (ggml_webgpu_tensor_misalignment(ctx, src0) / ggml_type_size(src0->type)),
|
609
|
+
(uint32_t) (ggml_webgpu_tensor_misalignment(ctx, src1) / ggml_type_size(src1->type)),
|
610
|
+
(uint32_t) (ggml_webgpu_tensor_misalignment(ctx, dst) / ggml_type_size(dst->type)),
|
611
|
+
(uint32_t) dst->ne[1], // number of rows in result (M)
|
612
|
+
(uint32_t) dst->ne[0], // number of columns in result (N)
|
613
|
+
(uint32_t) src0->ne[0], // number of columns in src0/src1 (K)
|
614
|
+
(uint32_t) (src0->nb[1] / ggml_type_size(src0->type)), // stride (elements/blocks) of src0 in dimension 1
|
615
|
+
(uint32_t) (src1->nb[1] / ggml_type_size(src1->type)), // stride (elements/blocks) of src1 in dimension 1
|
616
|
+
(uint32_t) (src0->nb[2] / ggml_type_size(src0->type)), // stride (elements/blocks) of src0 in dimension 2
|
617
|
+
(uint32_t) (src1->nb[2] / ggml_type_size(src1->type)), // stride (elements/blocks) of src1 in dimension 2
|
618
|
+
(uint32_t) (src0->nb[3] / ggml_type_size(src0->type)), // stride (elements/blocks) of src0 in dimension 3
|
619
|
+
(uint32_t) (src1->nb[3] / ggml_type_size(src1->type)), // stride (elements/blocks) of src1 in dimension 3
|
620
|
+
(uint32_t) src0->ne[2], // batch size in dimension 2
|
621
|
+
(uint32_t) src0->ne[3], // batch size in dimension 3
|
622
|
+
(uint32_t) (src1->ne[2] / src0->ne[2]), // broadcast in dimension 2
|
623
|
+
(uint32_t) (src1->ne[3] / src0->ne[3]) // broadcast in dimension 3
|
624
|
+
};
|
625
|
+
|
626
|
+
std::vector<wgpu::BindGroupEntry> entries = {
|
627
|
+
{ .binding = 0,
|
628
|
+
.buffer = ggml_webgpu_tensor_buf(src0),
|
629
|
+
.offset = ggml_webgpu_tensor_align_offset(ctx, src0),
|
630
|
+
.size = ggml_webgpu_tensor_binding_size(ctx, src0) },
|
631
|
+
{ .binding = 1,
|
632
|
+
.buffer = ggml_webgpu_tensor_buf(src1),
|
633
|
+
.offset = ggml_webgpu_tensor_align_offset(ctx, src1),
|
634
|
+
.size = ggml_webgpu_tensor_binding_size(ctx, src1) },
|
635
|
+
{ .binding = 2,
|
636
|
+
.buffer = ggml_webgpu_tensor_buf(dst),
|
637
|
+
.offset = ggml_webgpu_tensor_align_offset(ctx, dst),
|
638
|
+
.size = ggml_webgpu_tensor_binding_size(ctx, dst) },
|
639
|
+
};
|
640
|
+
|
641
|
+
uint32_t wg_x =
|
642
|
+
(dst->ne[0] * dst->ne[1] * dst->ne[2] * dst->ne[3] + WEBGPU_MUL_MAT_WG_SIZE - 1) / WEBGPU_MUL_MAT_WG_SIZE;
|
643
|
+
ggml_backend_webgpu_build_and_enqueue(ctx, ctx->mul_mat_pipeline[src0->type][src1->type], params, entries, wg_x,
|
644
|
+
ggml_op_name(dst->op));
|
645
|
+
}
|
646
|
+
|
647
|
+
static void ggml_webgpu_binary_op(webgpu_context & ctx,
|
648
|
+
ggml_tensor * src0,
|
649
|
+
ggml_tensor * src1,
|
650
|
+
ggml_tensor * dst,
|
651
|
+
wgpu::ComputePipeline & pipeline,
|
652
|
+
bool in_place) {
|
653
|
+
std::vector<uint32_t> params = {
|
654
|
+
(uint32_t) ggml_nelements(dst),
|
655
|
+
(uint32_t) (ggml_webgpu_tensor_misalignment(ctx, src0) / ggml_type_size(src0->type)),
|
656
|
+
(uint32_t) (ggml_webgpu_tensor_misalignment(ctx, src1) / ggml_type_size(src1->type)),
|
657
|
+
(uint32_t) (ggml_webgpu_tensor_misalignment(ctx, dst) / ggml_type_size(dst->type)),
|
658
|
+
(uint32_t) (src1->nb[0] / ggml_type_size(src1->type)),
|
659
|
+
(uint32_t) (src1->nb[1] / ggml_type_size(src1->type)),
|
660
|
+
(uint32_t) (src1->nb[2] / ggml_type_size(src1->type)),
|
661
|
+
(uint32_t) (src1->nb[3] / ggml_type_size(src1->type)),
|
662
|
+
(uint32_t) src0->ne[0],
|
663
|
+
(uint32_t) src0->ne[1],
|
664
|
+
(uint32_t) src0->ne[2],
|
665
|
+
(uint32_t) src1->ne[0],
|
666
|
+
(uint32_t) src1->ne[1],
|
667
|
+
(uint32_t) src1->ne[2],
|
668
|
+
(uint32_t) src1->ne[3],
|
669
|
+
};
|
670
|
+
|
671
|
+
std::vector<wgpu::BindGroupEntry> entries = {
|
672
|
+
{ .binding = 0,
|
673
|
+
.buffer = ggml_webgpu_tensor_buf(src0),
|
674
|
+
.offset = ggml_webgpu_tensor_align_offset(ctx, src0),
|
675
|
+
.size = ggml_webgpu_tensor_binding_size(ctx, src0) },
|
676
|
+
{ .binding = 1,
|
677
|
+
.buffer = ggml_webgpu_tensor_buf(src1),
|
678
|
+
.offset = ggml_webgpu_tensor_align_offset(ctx, src1),
|
679
|
+
.size = ggml_webgpu_tensor_binding_size(ctx, src1) }
|
680
|
+
};
|
681
|
+
if (!in_place) {
|
682
|
+
entries.push_back({ .binding = 2,
|
683
|
+
.buffer = ggml_webgpu_tensor_buf(dst),
|
684
|
+
.offset = ggml_webgpu_tensor_align_offset(ctx, dst),
|
685
|
+
.size = ggml_webgpu_tensor_binding_size(ctx, dst) });
|
686
|
+
}
|
687
|
+
|
688
|
+
size_t max_wg_size = ctx->max_wg_size_x;
|
689
|
+
uint32_t wg_x = (ggml_nelements(dst) + max_wg_size - 1) / max_wg_size;
|
690
|
+
ggml_backend_webgpu_build_and_enqueue(ctx, pipeline, params, entries, wg_x, ggml_op_name(dst->op));
|
691
|
+
}
|
692
|
+
|
693
|
+
static void ggml_webgpu_rms_norm(webgpu_context & ctx, ggml_tensor * src, ggml_tensor * dst) {
|
694
|
+
bool in_place = ggml_webgpu_tensor_equal(src, dst);
|
695
|
+
|
696
|
+
uint32_t eps;
|
697
|
+
memcpy(&eps, dst->op_params, sizeof(float));
|
698
|
+
|
699
|
+
std::vector<uint32_t> params = {
|
700
|
+
(uint32_t) (ggml_webgpu_tensor_misalignment(ctx, src) / ggml_type_size(src->type)),
|
701
|
+
};
|
702
|
+
if (!in_place) {
|
703
|
+
params.push_back((uint32_t) (ggml_webgpu_tensor_misalignment(ctx, dst) / ggml_type_size(dst->type)));
|
704
|
+
}
|
705
|
+
params.push_back((uint32_t) (src->nb[1] / ggml_type_size(src->type)));
|
706
|
+
params.push_back((uint32_t) (src->nb[2] / ggml_type_size(src->type)));
|
707
|
+
params.push_back((uint32_t) (src->nb[3] / ggml_type_size(src->type)));
|
708
|
+
if (!in_place) {
|
709
|
+
params.push_back((uint32_t) (dst->nb[1] / ggml_type_size(dst->type)));
|
710
|
+
params.push_back((uint32_t) (dst->nb[2] / ggml_type_size(dst->type)));
|
711
|
+
params.push_back((uint32_t) (dst->nb[3] / ggml_type_size(dst->type)));
|
712
|
+
}
|
713
|
+
params.push_back((uint32_t) src->ne[0]);
|
714
|
+
params.push_back((uint32_t) src->ne[1]);
|
715
|
+
params.push_back((uint32_t) src->ne[2]);
|
716
|
+
params.push_back((uint32_t) src->ne[3]);
|
717
|
+
params.push_back(eps); // epsilon, will be bitcast to float in shader
|
718
|
+
|
719
|
+
std::vector<wgpu::BindGroupEntry> entries = {
|
720
|
+
{ .binding = 0,
|
721
|
+
.buffer = ggml_webgpu_tensor_buf(src),
|
722
|
+
.offset = ggml_webgpu_tensor_align_offset(ctx, src),
|
723
|
+
.size = ggml_webgpu_tensor_binding_size(ctx, src) }
|
724
|
+
};
|
725
|
+
if (!in_place) {
|
726
|
+
entries.push_back({ .binding = 1,
|
727
|
+
.buffer = ggml_webgpu_tensor_buf(dst),
|
728
|
+
.offset = ggml_webgpu_tensor_align_offset(ctx, dst),
|
729
|
+
.size = ggml_webgpu_tensor_binding_size(ctx, dst) });
|
730
|
+
}
|
731
|
+
|
732
|
+
wgpu::ComputePipeline pipeline;
|
733
|
+
if (in_place) {
|
734
|
+
pipeline = ctx->rms_norm_ip_pipeline;
|
735
|
+
} else {
|
736
|
+
pipeline = ctx->rms_norm_pipeline;
|
737
|
+
}
|
738
|
+
size_t max_wg_size = ctx->max_wg_size_x;
|
739
|
+
uint32_t wg_x = (src->ne[1] * src->ne[2] * src->ne[3] + max_wg_size - 1) / max_wg_size;
|
740
|
+
ggml_backend_webgpu_build_and_enqueue(ctx, pipeline, params, entries, wg_x, ggml_op_name(dst->op));
|
741
|
+
}
|
742
|
+
|
743
|
+
// Returns true if node has enqueued work into the queue, false otherwise
|
744
|
+
static bool ggml_webgpu_encode_node(webgpu_context ctx, ggml_tensor * node) {
|
745
|
+
if (ggml_is_empty(node)) {
|
746
|
+
return false;
|
747
|
+
}
|
748
|
+
WEBGPU_LOG_DEBUG("ggml_webgpu_encode_node(" << node << ", " << ggml_op_name(node->op) << ")");
|
749
|
+
|
750
|
+
ggml_tensor * src0 = node->src[0];
|
751
|
+
ggml_tensor * src1 = node->src[1];
|
752
|
+
|
753
|
+
switch (node->op) {
|
754
|
+
// no-ops
|
755
|
+
case GGML_OP_NONE:
|
756
|
+
case GGML_OP_VIEW:
|
757
|
+
case GGML_OP_PERMUTE:
|
758
|
+
case GGML_OP_TRANSPOSE:
|
759
|
+
case GGML_OP_RESHAPE:
|
760
|
+
return false;
|
761
|
+
case GGML_OP_CPY:
|
762
|
+
ggml_webgpu_cpy(ctx, src0, node);
|
763
|
+
break;
|
764
|
+
case GGML_OP_SET_ROWS:
|
765
|
+
ggml_webgpu_set_rows(ctx, src0, src1, node);
|
766
|
+
break;
|
767
|
+
case GGML_OP_GET_ROWS:
|
768
|
+
ggml_webgpu_get_rows(ctx, src0, src1, node);
|
769
|
+
break;
|
770
|
+
case GGML_OP_MUL_MAT:
|
771
|
+
ggml_webgpu_mul_mat(ctx, src0, src1, node);
|
772
|
+
break;
|
773
|
+
case GGML_OP_ADD:
|
774
|
+
if (ggml_webgpu_tensor_equal(src0, node)) {
|
775
|
+
ggml_webgpu_binary_op(ctx, src0, src1, node, ctx->add_ip_pipeline[node->type], true);
|
776
|
+
} else {
|
777
|
+
ggml_webgpu_binary_op(ctx, src0, src1, node, ctx->add_pipeline[node->type], false);
|
778
|
+
}
|
779
|
+
break;
|
780
|
+
case GGML_OP_MUL:
|
781
|
+
if (ggml_webgpu_tensor_equal(src0, node)) {
|
782
|
+
ggml_webgpu_binary_op(ctx, src0, src1, node, ctx->mul_ip_pipeline[node->type], true);
|
783
|
+
} else {
|
784
|
+
ggml_webgpu_binary_op(ctx, src0, src1, node, ctx->mul_pipeline[node->type], false);
|
785
|
+
}
|
786
|
+
break;
|
787
|
+
case GGML_OP_RMS_NORM:
|
788
|
+
ggml_webgpu_rms_norm(ctx, src0, node);
|
789
|
+
break;
|
790
|
+
default:
|
791
|
+
return false;
|
792
|
+
}
|
793
|
+
return true;
|
794
|
+
}
|
795
|
+
|
796
|
+
static ggml_status ggml_backend_webgpu_graph_compute(ggml_backend_t backend, struct ggml_cgraph * cgraph) {
|
797
|
+
WEBGPU_LOG_DEBUG("ggml_backend_webgpu_graph_compute(" << cgraph->n_nodes << " nodes)");
|
798
|
+
|
799
|
+
ggml_backend_webgpu_context * backend_ctx = static_cast<ggml_backend_webgpu_context *>(backend->context);
|
800
|
+
webgpu_context ctx = backend_ctx->webgpu_ctx;
|
801
|
+
|
802
|
+
for (int i = 0; i < cgraph->n_nodes; i++) {
|
803
|
+
ggml_webgpu_encode_node(ctx, cgraph->nodes[i]);
|
804
|
+
}
|
805
|
+
|
806
|
+
ggml_backend_webgpu_submit_queue(ctx);
|
807
|
+
ggml_backend_webgpu_wait_on_submission(ctx);
|
808
|
+
|
809
|
+
return GGML_STATUS_SUCCESS;
|
810
|
+
}
|
811
|
+
|
812
|
+
static ggml_backend_i ggml_backend_webgpu_i = {
|
813
|
+
/* .get_name = */ ggml_backend_webgpu_name,
|
814
|
+
/* .free = */ ggml_backend_webgpu_free,
|
815
|
+
/* .set_tensor_async = */ NULL,
|
816
|
+
/* .get_tensor_async = */ NULL,
|
817
|
+
/* .cpy_tensor_async = */ NULL,
|
818
|
+
/* .synchronize = */ NULL,
|
819
|
+
/* .graph_plan_create = */ NULL,
|
820
|
+
/* .graph_plan_free = */ NULL,
|
821
|
+
/* .graph_plan_update = */ NULL,
|
822
|
+
/* .graph_plan_compute = */ NULL,
|
823
|
+
/* .graph_compute = */ ggml_backend_webgpu_graph_compute,
|
824
|
+
/* .event_record = */ NULL,
|
825
|
+
/* .event_wait = */ NULL,
|
826
|
+
/* .graph_optimize = */ NULL,
|
827
|
+
};
|
828
|
+
|
829
|
+
/* End GGML Backend Interface */
|
830
|
+
|
831
|
+
/* GGML Backend Buffer Interface */
|
832
|
+
|
833
|
+
static void ggml_backend_webgpu_buffer_free_buffer(ggml_backend_buffer_t buffer) {
|
834
|
+
WEBGPU_LOG_DEBUG("ggml_backend_webgpu_buffer_free_buffer()");
|
835
|
+
ggml_backend_webgpu_buffer_context * ctx = static_cast<ggml_backend_webgpu_buffer_context *>(buffer->context);
|
836
|
+
ctx->buffer.Destroy();
|
837
|
+
}
|
838
|
+
|
839
|
+
// Returns the "fake" base pointer.
|
840
|
+
static void * ggml_backend_webgpu_buffer_get_base(ggml_backend_buffer_t buffer) {
|
841
|
+
GGML_UNUSED(buffer);
|
842
|
+
return webgpu_ptr_base;
|
843
|
+
}
|
844
|
+
|
845
|
+
static void ggml_backend_webgpu_buffer_memset_tensor(ggml_backend_buffer_t buffer,
|
846
|
+
ggml_tensor * tensor,
|
847
|
+
uint8_t value,
|
848
|
+
size_t offset,
|
849
|
+
size_t size) {
|
850
|
+
if (size == 0) {
|
851
|
+
WEBGPU_LOG_DEBUG("ggml_backend_webgpu_buffer_memset_tensor: size is zero, nothing to do.");
|
852
|
+
return;
|
853
|
+
}
|
854
|
+
|
855
|
+
WEBGPU_LOG_DEBUG("ggml_backend_webgpu_buffer_memset_tensor(" << buffer << ", " << tensor << ", " << value << ", "
|
856
|
+
<< offset << ", " << size << ")");
|
857
|
+
|
858
|
+
ggml_backend_webgpu_buffer_context * buf_ctx = (ggml_backend_webgpu_buffer_context *) buffer->context;
|
859
|
+
|
860
|
+
size_t total_offset = webgpu_tensor_offset(tensor) + tensor->view_offs + offset;
|
861
|
+
|
862
|
+
// This is a trick to set all bytes of a u32 to the same 1 byte value.
|
863
|
+
uint32_t val32 = (uint32_t) value * 0x01010101;
|
864
|
+
ggml_backend_webgpu_buffer_memset(buf_ctx->webgpu_ctx, buf_ctx->buffer, val32, total_offset, size);
|
865
|
+
}
|
866
|
+
|
867
|
+
static void ggml_backend_webgpu_buffer_set_tensor(ggml_backend_buffer_t buffer,
|
868
|
+
ggml_tensor * tensor,
|
869
|
+
const void * data,
|
870
|
+
size_t offset,
|
871
|
+
size_t size) {
|
872
|
+
WEBGPU_LOG_DEBUG("ggml_backend_webgpu_buffer_set_tensor(" << buffer << ", " << tensor << ", " << data << ", "
|
873
|
+
<< offset << ", " << size << ")");
|
874
|
+
ggml_backend_webgpu_buffer_context * buf_ctx = (ggml_backend_webgpu_buffer_context *) buffer->context;
|
875
|
+
webgpu_context webgpu_ctx = buf_ctx->webgpu_ctx;
|
876
|
+
|
877
|
+
size_t total_offset = webgpu_tensor_offset(tensor) + tensor->view_offs + offset;
|
878
|
+
|
879
|
+
webgpu_ctx->queue.WriteBuffer(buf_ctx->buffer, total_offset, data, (size / 4) * 4);
|
880
|
+
|
881
|
+
if (size % 4 != 0) {
|
882
|
+
// If size is not a multiple of 4, we need to memset the remaining bytes
|
883
|
+
size_t remaining_size = size % 4;
|
884
|
+
|
885
|
+
// pack the remaining bytes into a uint32_t
|
886
|
+
uint32_t val32 = 0;
|
887
|
+
|
888
|
+
for (size_t i = 0; i < remaining_size; i++) {
|
889
|
+
((uint8_t *) &val32)[i] = ((const uint8_t *) data)[size - remaining_size + i];
|
890
|
+
}
|
891
|
+
// memset the remaining bytes
|
892
|
+
ggml_backend_webgpu_buffer_memset(webgpu_ctx, buf_ctx->buffer, val32, total_offset + (size - remaining_size),
|
893
|
+
remaining_size);
|
894
|
+
} else {
|
895
|
+
// wait for WriteBuffer to complete
|
896
|
+
ggml_backend_webgpu_wait_on_submission(webgpu_ctx);
|
897
|
+
}
|
898
|
+
}
|
899
|
+
|
900
|
+
static void ggml_backend_webgpu_buffer_get_tensor(ggml_backend_buffer_t buffer,
|
901
|
+
const ggml_tensor * tensor,
|
902
|
+
void * data,
|
903
|
+
size_t offset,
|
904
|
+
size_t size) {
|
905
|
+
WEBGPU_LOG_DEBUG("ggml_backend_webgpu_buffer_get_tensor(" << buffer << ", " << tensor << ", " << data << ", "
|
906
|
+
<< offset << ", " << size << ")");
|
907
|
+
|
908
|
+
ggml_backend_webgpu_buffer_context * buf_ctx = (ggml_backend_webgpu_buffer_context *) buffer->context;
|
909
|
+
webgpu_context webgpu_ctx = buf_ctx->webgpu_ctx;
|
910
|
+
wgpu::Device device = webgpu_ctx->device;
|
911
|
+
|
912
|
+
size_t total_offset = webgpu_tensor_offset(tensor) + tensor->view_offs + offset;
|
913
|
+
|
914
|
+
size_t final_size = size;
|
915
|
+
if (size % 4 != 0) {
|
916
|
+
// If size is not a multiple of 4, we need to round it up to the next multiple of 4
|
917
|
+
final_size = size + (4 - (size % 4));
|
918
|
+
}
|
919
|
+
|
920
|
+
std::lock_guard<std::recursive_mutex> lock(webgpu_ctx->mutex);
|
921
|
+
|
922
|
+
if (webgpu_ctx->get_tensor_staging_buf == nullptr || webgpu_ctx->get_tensor_staging_buf.GetSize() < final_size) {
|
923
|
+
// Create a new staging buffer if it doesn't exist or is too small
|
924
|
+
if (webgpu_ctx->get_tensor_staging_buf) {
|
925
|
+
webgpu_ctx->get_tensor_staging_buf.Destroy();
|
926
|
+
}
|
927
|
+
ggml_webgpu_create_buffer(device, webgpu_ctx->get_tensor_staging_buf, final_size,
|
928
|
+
wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::MapRead, "get_tensor_staging_buf");
|
929
|
+
}
|
930
|
+
|
931
|
+
// Copy the data from the buffer to the staging buffer
|
932
|
+
wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
|
933
|
+
encoder.CopyBufferToBuffer(buf_ctx->buffer, total_offset, webgpu_ctx->get_tensor_staging_buf, 0, final_size);
|
934
|
+
wgpu::CommandBuffer commands = encoder.Finish();
|
935
|
+
|
936
|
+
// Submit the command buffer to the queue
|
937
|
+
webgpu_ctx->queue.Submit(1, &commands);
|
938
|
+
|
939
|
+
// Map the staging buffer to read the data
|
940
|
+
ggml_backend_webgpu_map_buffer(webgpu_ctx, webgpu_ctx->get_tensor_staging_buf, wgpu::MapMode::Read, 0, final_size);
|
941
|
+
// Must specify size here since the staging buffer might be larger than the tensor size
|
942
|
+
const void * mapped_range = webgpu_ctx->get_tensor_staging_buf.GetConstMappedRange(0, final_size);
|
943
|
+
|
944
|
+
// Copy the data from the mapped range to the output buffer
|
945
|
+
std::memcpy(data, mapped_range, size);
|
946
|
+
webgpu_ctx->get_tensor_staging_buf.Unmap();
|
947
|
+
}
|
948
|
+
|
949
|
+
static void ggml_backend_webgpu_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
|
950
|
+
WEBGPU_LOG_DEBUG("ggml_backend_webgpu_buffer_clear(" << buffer << ", " << (uint32_t) value << ")");
|
951
|
+
ggml_backend_webgpu_buffer_context * buf_ctx = (ggml_backend_webgpu_buffer_context *) buffer->context;
|
952
|
+
ggml_backend_webgpu_buffer_memset(buf_ctx->webgpu_ctx, buf_ctx->buffer, value, 0, buffer->size);
|
953
|
+
}
|
954
|
+
|
955
|
+
static ggml_backend_buffer_i ggml_backend_webgpu_buffer_interface = {
|
956
|
+
/* .free_buffer = */ ggml_backend_webgpu_buffer_free_buffer,
|
957
|
+
/* .get_base = */ ggml_backend_webgpu_buffer_get_base,
|
958
|
+
/* .init_tensor = */ NULL, // TODO: optional, needed?
|
959
|
+
/* .memset_tensor = */ ggml_backend_webgpu_buffer_memset_tensor,
|
960
|
+
/* .set_tensor = */ ggml_backend_webgpu_buffer_set_tensor,
|
961
|
+
/* .get_tensor = */ ggml_backend_webgpu_buffer_get_tensor,
|
962
|
+
/* .cpy_tensor = */ NULL, // TODO: optional, implement this
|
963
|
+
/* .clear = */ ggml_backend_webgpu_buffer_clear,
|
964
|
+
/* .reset = */ NULL, // TODO: optional, think it coordinates with .init_tensor
|
965
|
+
};
|
966
|
+
|
967
|
+
/* End GGML Backend Buffer Interface */
|
968
|
+
|
969
|
+
/* GGML Backend Buffer Type Interface */
|
970
|
+
|
971
|
+
static const char * ggml_backend_webgpu_buffer_type_get_name(ggml_backend_buffer_type_t buft) {
|
972
|
+
ggml_backend_webgpu_device_context * ctx = static_cast<ggml_backend_webgpu_device_context *>(buft->device->context);
|
973
|
+
return ctx->device_name.c_str();
|
974
|
+
}
|
975
|
+
|
976
|
+
static ggml_backend_buffer_t ggml_backend_webgpu_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft,
|
977
|
+
size_t size) {
|
978
|
+
WEBGPU_LOG_DEBUG("ggml_backend_webgpu_buffer_type_alloc_buffer(" << size << ")");
|
979
|
+
ggml_backend_webgpu_device_context * ctx = static_cast<ggml_backend_webgpu_device_context *>(buft->device->context);
|
980
|
+
|
981
|
+
wgpu::Buffer buf;
|
982
|
+
ggml_webgpu_create_buffer(ctx->webgpu_ctx->device, buf,
|
983
|
+
(size + WEBGPU_STORAGE_BUF_BINDING_MULT - 1) & ~(WEBGPU_STORAGE_BUF_BINDING_MULT - 1),
|
984
|
+
wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::CopyDst,
|
985
|
+
"allocated_buffer");
|
986
|
+
|
987
|
+
ggml_backend_webgpu_buffer_context * buf_ctx = new ggml_backend_webgpu_buffer_context(ctx->webgpu_ctx, buf);
|
988
|
+
|
989
|
+
return ggml_backend_buffer_init(buft, ggml_backend_webgpu_buffer_interface, buf_ctx, size);
|
990
|
+
}
|
991
|
+
|
992
|
+
static size_t ggml_backend_webgpu_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
|
993
|
+
ggml_backend_webgpu_device_context * ctx = static_cast<ggml_backend_webgpu_device_context *>(buft->device->context);
|
994
|
+
return ctx->webgpu_ctx->limits.minStorageBufferOffsetAlignment;
|
995
|
+
}
|
996
|
+
|
997
|
+
// maxBufferSize might be larger, but you can't bind more than maxStorageBufferBindingSize to a single binding.
|
998
|
+
static size_t ggml_backend_webgpu_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
|
999
|
+
ggml_backend_webgpu_device_context * ctx = static_cast<ggml_backend_webgpu_device_context *>(buft->device->context);
|
1000
|
+
return ctx->webgpu_ctx->limits.maxStorageBufferBindingSize;
|
1001
|
+
}
|
1002
|
+
|
1003
|
+
/* End GGML Backend Buffer Type Interface */
|
1004
|
+
|
1005
|
+
/* GGML Backend Device Interface */
|
1006
|
+
|
1007
|
+
static const char * ggml_backend_webgpu_device_get_name(ggml_backend_dev_t dev) {
|
1008
|
+
ggml_backend_webgpu_device_context * ctx = static_cast<ggml_backend_webgpu_device_context *>(dev->context);
|
1009
|
+
return ctx->device_name.c_str();
|
1010
|
+
}
|
1011
|
+
|
1012
|
+
static const char * ggml_backend_webgpu_device_get_description(ggml_backend_dev_t dev) {
|
1013
|
+
ggml_backend_webgpu_device_context * ctx = static_cast<ggml_backend_webgpu_device_context *>(dev->context);
|
1014
|
+
return ctx->device_desc.c_str();
|
1015
|
+
}
|
1016
|
+
|
1017
|
+
static void ggml_backend_webgpu_device_get_memory(ggml_backend_dev_t dev, size_t * free, size_t * total) {
|
1018
|
+
ggml_backend_webgpu_device_context * ctx = static_cast<ggml_backend_webgpu_device_context *>(dev->context);
|
1019
|
+
// TODO: what do we actually want to return here? maxBufferSize might not be the full available memory.
|
1020
|
+
*free = ctx->webgpu_ctx->limits.maxBufferSize;
|
1021
|
+
*total = ctx->webgpu_ctx->limits.maxBufferSize;
|
1022
|
+
}
|
1023
|
+
|
1024
|
+
static enum ggml_backend_dev_type ggml_backend_webgpu_device_get_type(ggml_backend_dev_t dev) {
|
1025
|
+
GGML_UNUSED(dev);
|
1026
|
+
return GGML_BACKEND_DEVICE_TYPE_GPU;
|
1027
|
+
}
|
1028
|
+
|
1029
|
+
static void ggml_backend_webgpu_device_get_props(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props) {
|
1030
|
+
props->name = ggml_backend_webgpu_device_get_name(dev);
|
1031
|
+
props->description = ggml_backend_webgpu_device_get_description(dev);
|
1032
|
+
props->type = ggml_backend_webgpu_device_get_type(dev);
|
1033
|
+
ggml_backend_webgpu_device_get_memory(dev, &props->memory_free, &props->memory_total);
|
1034
|
+
props->caps = {
|
1035
|
+
/* .async = */ false,
|
1036
|
+
/* .host_buffer = */ false,
|
1037
|
+
/* .buffer_from_host_ptr = */ false,
|
1038
|
+
/* .events = */ false,
|
1039
|
+
};
|
1040
|
+
}
|
1041
|
+
|
1042
|
+
static ggml_guid_t ggml_backend_webgpu_guid(void) {
|
1043
|
+
static const char * guid_str = "__ggml_webgpu :)";
|
1044
|
+
return reinterpret_cast<ggml_guid_t>((void *) guid_str);
|
1045
|
+
}
|
1046
|
+
|
1047
|
+
// The max workgroup size is a common constant
|
1048
|
+
static std::vector<wgpu::ConstantEntry> ggml_webgpu_max_wg_size_entry(webgpu_context & webgpu_ctx) {
|
1049
|
+
std::vector<wgpu::ConstantEntry> constants(1);
|
1050
|
+
constants[0].key = "wg_size";
|
1051
|
+
constants[0].value = webgpu_ctx->max_wg_size_x;
|
1052
|
+
return constants;
|
1053
|
+
}
|
1054
|
+
|
1055
|
+
static void ggml_webgpu_init_memset_pipeline(webgpu_context & webgpu_ctx) {
|
1056
|
+
// we use the maximum workgroup size for the memset pipeline
|
1057
|
+
size_t max_wg_size = webgpu_ctx->max_wg_size_x;
|
1058
|
+
size_t max_threads = max_wg_size * webgpu_ctx->limits.maxComputeWorkgroupsPerDimension;
|
1059
|
+
// Size the bytes_per_thread so that the largest buffer size can be handled
|
1060
|
+
webgpu_ctx->memset_bytes_per_thread =
|
1061
|
+
(webgpu_ctx->limits.maxStorageBufferBindingSize + max_threads - 1) / max_threads;
|
1062
|
+
std::vector<wgpu::ConstantEntry> constants(2);
|
1063
|
+
constants[0].key = "wg_size";
|
1064
|
+
constants[0].value = max_wg_size;
|
1065
|
+
constants[1].key = "bytes_per_thread";
|
1066
|
+
constants[1].value = webgpu_ctx->memset_bytes_per_thread;
|
1067
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->memset_pipeline, wgsl_memset, "memset", constants);
|
1068
|
+
}
|
1069
|
+
|
1070
|
+
static void ggml_webgpu_init_mul_mat_pipeline(webgpu_context & webgpu_ctx) {
|
1071
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_mat_pipeline[GGML_TYPE_F32][GGML_TYPE_F32],
|
1072
|
+
wgsl_mul_mat_f32_f32, "mul_mat_f32_f32");
|
1073
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_mat_pipeline[GGML_TYPE_F16][GGML_TYPE_F16],
|
1074
|
+
wgsl_mul_mat_f16_f16, "mul_mat_f16_f16");
|
1075
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_mat_pipeline[GGML_TYPE_F16][GGML_TYPE_F32],
|
1076
|
+
wgsl_mul_mat_f16_f32, "mul_mat_f16_f32");
|
1077
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_mat_pipeline[GGML_TYPE_Q4_0][GGML_TYPE_F32],
|
1078
|
+
wgsl_mul_mat_q4_0_f32, "mul_mat_q4_0_f32");
|
1079
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_mat_pipeline[GGML_TYPE_Q4_1][GGML_TYPE_F32],
|
1080
|
+
wgsl_mul_mat_q4_1_f32, "mul_mat_q4_1_f32");
|
1081
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_mat_pipeline[GGML_TYPE_Q5_0][GGML_TYPE_F32],
|
1082
|
+
wgsl_mul_mat_q5_0_f32, "mul_mat_q5_0_f32");
|
1083
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_mat_pipeline[GGML_TYPE_Q5_1][GGML_TYPE_F32],
|
1084
|
+
wgsl_mul_mat_q5_1_f32, "mul_mat_q5_1_f32");
|
1085
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_mat_pipeline[GGML_TYPE_Q8_0][GGML_TYPE_F32],
|
1086
|
+
wgsl_mul_mat_q8_0_f32, "mul_mat_q8_0_f32");
|
1087
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_mat_pipeline[GGML_TYPE_Q2_K][GGML_TYPE_F32],
|
1088
|
+
wgsl_mul_mat_q2_k_f32, "mul_mat_q2_k_f32");
|
1089
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_mat_pipeline[GGML_TYPE_Q3_K][GGML_TYPE_F32],
|
1090
|
+
wgsl_mul_mat_q3_k_f32, "mul_mat_q3_k_f32");
|
1091
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_mat_pipeline[GGML_TYPE_Q4_K][GGML_TYPE_F32],
|
1092
|
+
wgsl_mul_mat_q4_k_f32, "mul_mat_q4_k_f32");
|
1093
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_mat_pipeline[GGML_TYPE_Q5_K][GGML_TYPE_F32],
|
1094
|
+
wgsl_mul_mat_q5_k_f32, "mul_mat_q5_k_f32");
|
1095
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_mat_pipeline[GGML_TYPE_Q6_K][GGML_TYPE_F32],
|
1096
|
+
wgsl_mul_mat_q6_k_f32, "mul_mat_q6_k_f32");
|
1097
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_mat_pipeline[GGML_TYPE_IQ2_XXS][GGML_TYPE_F32],
|
1098
|
+
wgsl_mul_mat_iq2_xxs_f32, "mul_mat_iq2_xxs_f32");
|
1099
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_mat_pipeline[GGML_TYPE_IQ2_XS][GGML_TYPE_F32],
|
1100
|
+
wgsl_mul_mat_iq2_xs_f32, "mul_mat_iq2_xs_f32");
|
1101
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_mat_pipeline[GGML_TYPE_IQ2_S][GGML_TYPE_F32],
|
1102
|
+
wgsl_mul_mat_iq2_s_f32, "mul_mat_iq2_s_f32");
|
1103
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_mat_pipeline[GGML_TYPE_IQ3_XXS][GGML_TYPE_F32],
|
1104
|
+
wgsl_mul_mat_iq3_xxs_f32, "mul_mat_iq3_xxs_f32");
|
1105
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_mat_pipeline[GGML_TYPE_IQ3_S][GGML_TYPE_F32],
|
1106
|
+
wgsl_mul_mat_iq3_s_f32, "mul_mat_iq3_s_f32");
|
1107
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_mat_pipeline[GGML_TYPE_IQ1_S][GGML_TYPE_F32],
|
1108
|
+
wgsl_mul_mat_iq1_s_f32, "mul_mat_iq1_s_f32");
|
1109
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_mat_pipeline[GGML_TYPE_IQ1_M][GGML_TYPE_F32],
|
1110
|
+
wgsl_mul_mat_iq1_m_f32, "mul_mat_iq1_m_f32");
|
1111
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_mat_pipeline[GGML_TYPE_IQ4_NL][GGML_TYPE_F32],
|
1112
|
+
wgsl_mul_mat_iq4_nl_f32, "mul_mat_iq4_nl_f32");
|
1113
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_mat_pipeline[GGML_TYPE_IQ4_XS][GGML_TYPE_F32],
|
1114
|
+
wgsl_mul_mat_iq4_xs_f32, "mul_mat_iq4_xs_f32");
|
1115
|
+
}
|
1116
|
+
|
1117
|
+
static void ggml_webgpu_init_set_rows_pipeline(webgpu_context & webgpu_ctx) {
|
1118
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->set_rows_pipeline, wgsl_set_rows, "set_rows",
|
1119
|
+
ggml_webgpu_max_wg_size_entry(webgpu_ctx));
|
1120
|
+
}
|
1121
|
+
|
1122
|
+
static void ggml_webgpu_init_get_rows_pipeline(webgpu_context & webgpu_ctx) {
|
1123
|
+
std::vector<wgpu::ConstantEntry> constants = ggml_webgpu_max_wg_size_entry(webgpu_ctx);
|
1124
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->get_rows_pipeline[GGML_TYPE_F32], wgsl_get_rows_f32_vec,
|
1125
|
+
"get_rows_f32_vec", constants);
|
1126
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->get_rows_f32_no_vec_pipeline, wgsl_get_rows_f32,
|
1127
|
+
"get_rows_f32", constants);
|
1128
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->get_rows_pipeline[GGML_TYPE_F16], wgsl_get_rows_f16,
|
1129
|
+
"get_rows_f16", constants);
|
1130
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->get_rows_pipeline[GGML_TYPE_I32], wgsl_get_rows_i32,
|
1131
|
+
"get_rows_i32", constants);
|
1132
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->get_rows_pipeline[GGML_TYPE_Q4_0], wgsl_get_rows_q4_0,
|
1133
|
+
"get_rows_q4_0", constants);
|
1134
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->get_rows_pipeline[GGML_TYPE_Q4_1], wgsl_get_rows_q4_1,
|
1135
|
+
"get_rows_q4_1", constants);
|
1136
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->get_rows_pipeline[GGML_TYPE_Q5_0], wgsl_get_rows_q5_0,
|
1137
|
+
"get_rows_q5_0", constants);
|
1138
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->get_rows_pipeline[GGML_TYPE_Q5_1], wgsl_get_rows_q5_1,
|
1139
|
+
"get_rows_q5_1", constants);
|
1140
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->get_rows_pipeline[GGML_TYPE_Q8_0], wgsl_get_rows_q8_0,
|
1141
|
+
"get_rows_q8_0", constants);
|
1142
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->get_rows_pipeline[GGML_TYPE_Q2_K], wgsl_get_rows_q2_k,
|
1143
|
+
"get_rows_q2_k", constants);
|
1144
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->get_rows_pipeline[GGML_TYPE_Q3_K], wgsl_get_rows_q3_k,
|
1145
|
+
"get_rows_q3_k", constants);
|
1146
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->get_rows_pipeline[GGML_TYPE_Q4_K], wgsl_get_rows_q4_k,
|
1147
|
+
"get_rows_q4_k", constants);
|
1148
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->get_rows_pipeline[GGML_TYPE_Q5_K], wgsl_get_rows_q5_k,
|
1149
|
+
"get_rows_q5_k", constants);
|
1150
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->get_rows_pipeline[GGML_TYPE_Q6_K], wgsl_get_rows_q6_k,
|
1151
|
+
"get_rows_q6_k", constants);
|
1152
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->get_rows_pipeline[GGML_TYPE_IQ2_XXS],
|
1153
|
+
wgsl_get_rows_iq2_xxs, "get_rows_iq2_xxs", constants);
|
1154
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->get_rows_pipeline[GGML_TYPE_IQ2_XS],
|
1155
|
+
wgsl_get_rows_iq2_xs, "get_rows_iq2_xs", constants);
|
1156
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->get_rows_pipeline[GGML_TYPE_IQ2_S], wgsl_get_rows_iq2_s,
|
1157
|
+
"get_rows_iq2_s", constants);
|
1158
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->get_rows_pipeline[GGML_TYPE_IQ3_XXS],
|
1159
|
+
wgsl_get_rows_iq3_xxs, "get_rows_iq3_xxs", constants);
|
1160
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->get_rows_pipeline[GGML_TYPE_IQ3_S], wgsl_get_rows_iq3_s,
|
1161
|
+
"get_rows_iq3_s", constants);
|
1162
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->get_rows_pipeline[GGML_TYPE_IQ1_S], wgsl_get_rows_iq1_s,
|
1163
|
+
"get_rows_iq1_s", constants);
|
1164
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->get_rows_pipeline[GGML_TYPE_IQ1_M], wgsl_get_rows_iq1_m,
|
1165
|
+
"get_rows_iq1_m", constants);
|
1166
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->get_rows_pipeline[GGML_TYPE_IQ4_NL],
|
1167
|
+
wgsl_get_rows_iq4_nl, "get_rows_iq4_nl", constants);
|
1168
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->get_rows_pipeline[GGML_TYPE_IQ4_XS],
|
1169
|
+
wgsl_get_rows_iq4_xs, "get_rows_iq4_xs", constants);
|
1170
|
+
}
|
1171
|
+
|
1172
|
+
static void ggml_webgpu_init_cpy_pipeline(webgpu_context & webgpu_ctx) {
|
1173
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->cpy_pipeline, wgsl_cpy, "cpy",
|
1174
|
+
ggml_webgpu_max_wg_size_entry(webgpu_ctx));
|
1175
|
+
}
|
1176
|
+
|
1177
|
+
static void ggml_webgpu_init_add_pipeline(webgpu_context & webgpu_ctx) {
|
1178
|
+
std::vector<wgpu::ConstantEntry> constants = ggml_webgpu_max_wg_size_entry(webgpu_ctx);
|
1179
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->add_pipeline[GGML_TYPE_F32], wgsl_add_f32, "add_f32",
|
1180
|
+
constants);
|
1181
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->add_pipeline[GGML_TYPE_F16], wgsl_add_f16, "add_f16",
|
1182
|
+
constants);
|
1183
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->add_ip_pipeline[GGML_TYPE_F32], wgsl_add_in_place_f32,
|
1184
|
+
"add_in_place_f32", constants);
|
1185
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->add_ip_pipeline[GGML_TYPE_F16], wgsl_add_in_place_f16,
|
1186
|
+
"add_in_place_f16", constants);
|
1187
|
+
}
|
1188
|
+
|
1189
|
+
static void ggml_webgpu_init_mul_pipeline(webgpu_context & webgpu_ctx) {
|
1190
|
+
std::vector<wgpu::ConstantEntry> constants = ggml_webgpu_max_wg_size_entry(webgpu_ctx);
|
1191
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_pipeline[GGML_TYPE_F32], wgsl_mul_f32, "mul_f32",
|
1192
|
+
constants);
|
1193
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_pipeline[GGML_TYPE_F16], wgsl_mul_f16, "mul_f16",
|
1194
|
+
constants);
|
1195
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_ip_pipeline[GGML_TYPE_F32], wgsl_mul_in_place_f32,
|
1196
|
+
"mul_in_place_f32", constants);
|
1197
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_ip_pipeline[GGML_TYPE_F16], wgsl_mul_in_place_f16,
|
1198
|
+
"mul_in_place_f16", constants);
|
1199
|
+
}
|
1200
|
+
|
1201
|
+
static void ggml_webgpu_init_rms_norm_pipeline(webgpu_context & webgpu_ctx) {
|
1202
|
+
std::vector<wgpu::ConstantEntry> constants = ggml_webgpu_max_wg_size_entry(webgpu_ctx);
|
1203
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->rms_norm_pipeline, wgsl_rms_norm, "rms_norm",
|
1204
|
+
constants);
|
1205
|
+
ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->rms_norm_ip_pipeline, wgsl_rms_norm_in_place,
|
1206
|
+
"rms_norm_in_place", constants);
|
1207
|
+
}
|
1208
|
+
|
1209
|
+
static ggml_backend_t ggml_backend_webgpu_device_init(ggml_backend_dev_t dev, const char * params) {
|
1210
|
+
GGML_UNUSED(params);
|
1211
|
+
|
1212
|
+
WEBGPU_LOG_DEBUG("ggml_backend_webgpu_device_init()");
|
1213
|
+
|
1214
|
+
ggml_backend_webgpu_device_context * dev_ctx = static_cast<ggml_backend_webgpu_device_context *>(dev->context);
|
1215
|
+
webgpu_context webgpu_ctx = dev_ctx->webgpu_ctx;
|
1216
|
+
|
1217
|
+
static ggml_backend_webgpu_context backend_ctx;
|
1218
|
+
backend_ctx.name = GGML_WEBGPU_NAME + std::string(": ") + dev_ctx->device_name;
|
1219
|
+
backend_ctx.webgpu_ctx = webgpu_ctx;
|
1220
|
+
|
1221
|
+
// See GGML Backend Interface section
|
1222
|
+
static ggml_backend backend = {
|
1223
|
+
/* .guid = */ ggml_backend_webgpu_guid(),
|
1224
|
+
/* .interface = */ ggml_backend_webgpu_i,
|
1225
|
+
/* .device = */ dev,
|
1226
|
+
/* .context = */ &backend_ctx,
|
1227
|
+
};
|
1228
|
+
|
1229
|
+
return &backend;
|
1230
|
+
}
|
1231
|
+
|
1232
|
+
static ggml_backend_buffer_type_t ggml_backend_webgpu_device_get_buffer_type(ggml_backend_dev_t dev) {
|
1233
|
+
// See GGML Backend Buffer Type Interface section
|
1234
|
+
static struct ggml_backend_buffer_type ggml_backend_webgpu_buffer_type = {
|
1235
|
+
/* .iface = */ {
|
1236
|
+
/* .get_name = */ ggml_backend_webgpu_buffer_type_get_name,
|
1237
|
+
/* .alloc_buffer = */ ggml_backend_webgpu_buffer_type_alloc_buffer,
|
1238
|
+
/* .get_alignment = */ ggml_backend_webgpu_buffer_type_get_alignment,
|
1239
|
+
/* .get_max_size = */ ggml_backend_webgpu_buffer_type_get_max_size,
|
1240
|
+
/* .get_alloc_size = */ NULL, // defaults to ggml_nbytes
|
1241
|
+
/* .is_host = */ NULL, // defaults to false
|
1242
|
+
},
|
1243
|
+
/* .device = */
|
1244
|
+
dev,
|
1245
|
+
/* .context = */ NULL,
|
1246
|
+
};
|
1247
|
+
|
1248
|
+
return &ggml_backend_webgpu_buffer_type;
|
1249
|
+
}
|
1250
|
+
|
1251
|
+
static bool ggml_backend_webgpu_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
|
1252
|
+
GGML_UNUSED(dev);
|
1253
|
+
return buft->iface.get_name == ggml_backend_webgpu_buffer_type_get_name;
|
1254
|
+
}
|
1255
|
+
|
1256
|
+
static bool ggml_webgpu_supported_qtype(ggml_type type) {
|
1257
|
+
switch (type) {
|
1258
|
+
case GGML_TYPE_Q4_0:
|
1259
|
+
case GGML_TYPE_Q4_1:
|
1260
|
+
case GGML_TYPE_Q5_0:
|
1261
|
+
case GGML_TYPE_Q5_1:
|
1262
|
+
case GGML_TYPE_Q8_0:
|
1263
|
+
case GGML_TYPE_Q2_K:
|
1264
|
+
case GGML_TYPE_Q3_K:
|
1265
|
+
case GGML_TYPE_Q4_K:
|
1266
|
+
case GGML_TYPE_Q5_K:
|
1267
|
+
case GGML_TYPE_Q6_K:
|
1268
|
+
case GGML_TYPE_IQ2_XXS:
|
1269
|
+
case GGML_TYPE_IQ2_XS:
|
1270
|
+
case GGML_TYPE_IQ2_S:
|
1271
|
+
case GGML_TYPE_IQ3_XXS:
|
1272
|
+
case GGML_TYPE_IQ3_S:
|
1273
|
+
case GGML_TYPE_IQ1_S:
|
1274
|
+
case GGML_TYPE_IQ1_M:
|
1275
|
+
case GGML_TYPE_IQ4_NL:
|
1276
|
+
case GGML_TYPE_IQ4_XS:
|
1277
|
+
return true;
|
1278
|
+
default:
|
1279
|
+
return false;
|
1280
|
+
}
|
1281
|
+
}
|
1282
|
+
|
1283
|
+
static bool ggml_backend_webgpu_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
|
1284
|
+
ggml_backend_webgpu_device_context * ctx = static_cast<ggml_backend_webgpu_device_context *>(dev->context);
|
1285
|
+
|
1286
|
+
webgpu_context webgpu_ctx = ctx->webgpu_ctx;
|
1287
|
+
|
1288
|
+
ggml_tensor * src0 = op->src[0];
|
1289
|
+
ggml_tensor * src1 = op->src[1];
|
1290
|
+
// on smaller devices (or CI), tensors may be larger than the max storage buffer size
|
1291
|
+
if (ggml_nbytes(op) > webgpu_ctx->limits.maxStorageBufferBindingSize ||
|
1292
|
+
(src0 != nullptr && ggml_nbytes(src0) > webgpu_ctx->limits.maxStorageBufferBindingSize) ||
|
1293
|
+
(src1 != nullptr && ggml_nbytes(src1) > webgpu_ctx->limits.maxStorageBufferBindingSize)) {
|
1294
|
+
return false;
|
1295
|
+
}
|
1296
|
+
|
1297
|
+
bool supports_op = false;
|
1298
|
+
switch (op->op) {
|
1299
|
+
case GGML_OP_NONE:
|
1300
|
+
case GGML_OP_VIEW:
|
1301
|
+
case GGML_OP_PERMUTE:
|
1302
|
+
case GGML_OP_TRANSPOSE:
|
1303
|
+
case GGML_OP_RESHAPE:
|
1304
|
+
supports_op = true;
|
1305
|
+
break;
|
1306
|
+
case GGML_OP_ADD:
|
1307
|
+
case GGML_OP_MUL:
|
1308
|
+
supports_op = (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) && (op->src[0]->type == op->type) &&
|
1309
|
+
(op->src[1]->type == op->type);
|
1310
|
+
break;
|
1311
|
+
case GGML_OP_CPY:
|
1312
|
+
case GGML_OP_SET_ROWS:
|
1313
|
+
supports_op = (op->type == GGML_TYPE_F16 && op->src[0]->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_I64);
|
1314
|
+
break;
|
1315
|
+
case GGML_OP_GET_ROWS:
|
1316
|
+
if (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16 ||
|
1317
|
+
op->src[0]->type == GGML_TYPE_I32 || ggml_webgpu_supported_qtype(op->src[0]->type)) {
|
1318
|
+
supports_op = (op->type == GGML_TYPE_F32);
|
1319
|
+
}
|
1320
|
+
break;
|
1321
|
+
case GGML_OP_MUL_MAT:
|
1322
|
+
{
|
1323
|
+
switch (op->src[1]->type) {
|
1324
|
+
case GGML_TYPE_F16:
|
1325
|
+
supports_op = (op->src[0]->type == GGML_TYPE_F16);
|
1326
|
+
break;
|
1327
|
+
case GGML_TYPE_F32:
|
1328
|
+
switch (op->src[0]->type) {
|
1329
|
+
case GGML_TYPE_F32:
|
1330
|
+
case GGML_TYPE_F16:
|
1331
|
+
case GGML_TYPE_Q4_0:
|
1332
|
+
case GGML_TYPE_Q4_1:
|
1333
|
+
case GGML_TYPE_Q5_0:
|
1334
|
+
case GGML_TYPE_Q5_1:
|
1335
|
+
case GGML_TYPE_Q8_0:
|
1336
|
+
case GGML_TYPE_Q2_K:
|
1337
|
+
case GGML_TYPE_Q3_K:
|
1338
|
+
case GGML_TYPE_Q4_K:
|
1339
|
+
case GGML_TYPE_Q5_K:
|
1340
|
+
case GGML_TYPE_Q6_K:
|
1341
|
+
case GGML_TYPE_IQ2_XXS:
|
1342
|
+
case GGML_TYPE_IQ2_XS:
|
1343
|
+
case GGML_TYPE_IQ2_S:
|
1344
|
+
case GGML_TYPE_IQ3_XXS:
|
1345
|
+
case GGML_TYPE_IQ3_S:
|
1346
|
+
case GGML_TYPE_IQ1_S:
|
1347
|
+
case GGML_TYPE_IQ1_M:
|
1348
|
+
case GGML_TYPE_IQ4_NL:
|
1349
|
+
case GGML_TYPE_IQ4_XS:
|
1350
|
+
supports_op = true;
|
1351
|
+
break;
|
1352
|
+
default:
|
1353
|
+
break;
|
1354
|
+
}
|
1355
|
+
default:
|
1356
|
+
break;
|
1357
|
+
}
|
1358
|
+
break;
|
1359
|
+
}
|
1360
|
+
case GGML_OP_RMS_NORM:
|
1361
|
+
supports_op = op->type == GGML_TYPE_F32 && op->src[0]->type == GGML_TYPE_F32;
|
1362
|
+
break;
|
1363
|
+
default:
|
1364
|
+
break;
|
1365
|
+
}
|
1366
|
+
#ifdef GGML_WEBGPU_DEBUG
|
1367
|
+
if (!supports_op) {
|
1368
|
+
WEBGPU_LOG_DEBUG("not supported: " << ggml_op_name(op->op) << " with types dst: " << ggml_type_name(op->type)
|
1369
|
+
<< ", src0: " << (op->src[0] ? ggml_type_name(op->src[0]->type) : "null")
|
1370
|
+
<< ", src1: " << (op->src[1] ? ggml_type_name(op->src[1]->type) : "null"));
|
1371
|
+
}
|
1372
|
+
#endif
|
1373
|
+
return supports_op;
|
1374
|
+
}
|
1375
|
+
|
1376
|
+
static struct ggml_backend_device_i ggml_backend_webgpu_device_i = {
|
1377
|
+
/* .get_name = */ ggml_backend_webgpu_device_get_name,
|
1378
|
+
/* .get_description = */ ggml_backend_webgpu_device_get_description,
|
1379
|
+
/* .get_memory = */ ggml_backend_webgpu_device_get_memory,
|
1380
|
+
/* .get_type = */ ggml_backend_webgpu_device_get_type,
|
1381
|
+
/* .get_props = */ ggml_backend_webgpu_device_get_props,
|
1382
|
+
/* .init_backend = */ ggml_backend_webgpu_device_init,
|
1383
|
+
/* .get_buffer_type = */ ggml_backend_webgpu_device_get_buffer_type,
|
1384
|
+
/* .get_host_buffer_type = */ NULL,
|
1385
|
+
/* .buffer_from_host_ptr = */ NULL,
|
1386
|
+
/* .supports_op = */ ggml_backend_webgpu_device_supports_op,
|
1387
|
+
/* .supports_buft = */ ggml_backend_webgpu_device_supports_buft,
|
1388
|
+
/* .offload_op = */ NULL,
|
1389
|
+
/* .event_new = */ NULL,
|
1390
|
+
/* .event_free = */ NULL,
|
1391
|
+
/* .event_synchronize = */ NULL,
|
1392
|
+
};
|
1393
|
+
|
1394
|
+
/* End GGML Backend Device Interface */
|
1395
|
+
|
1396
|
+
/* GGML Backend Registration Interface */
|
1397
|
+
|
1398
|
+
static const char * ggml_backend_webgpu_reg_get_name(ggml_backend_reg_t reg) {
|
1399
|
+
ggml_backend_webgpu_reg_context * ctx = static_cast<ggml_backend_webgpu_reg_context *>(reg->context);
|
1400
|
+
return ctx->name;
|
1401
|
+
}
|
1402
|
+
|
1403
|
+
static size_t ggml_backend_webgpu_reg_get_device_count(ggml_backend_reg_t reg) {
|
1404
|
+
ggml_backend_webgpu_reg_context * ctx = static_cast<ggml_backend_webgpu_reg_context *>(reg->context);
|
1405
|
+
return ctx->device_count;
|
1406
|
+
}
|
1407
|
+
|
1408
|
+
// TODO: Does this need to be thread safe? Is it only called once?
|
1409
|
+
// Only one device is supported for now
|
1410
|
+
static ggml_backend_dev_t ggml_backend_webgpu_reg_get_device(ggml_backend_reg_t reg, size_t index) {
|
1411
|
+
GGML_ASSERT(index == 0);
|
1412
|
+
WEBGPU_LOG_DEBUG("ggml_backend_reg_get_device()");
|
1413
|
+
|
1414
|
+
ggml_backend_webgpu_reg_context * reg_ctx = static_cast<ggml_backend_webgpu_reg_context *>(reg->context);
|
1415
|
+
|
1416
|
+
webgpu_context ctx = reg_ctx->webgpu_ctx;
|
1417
|
+
|
1418
|
+
wgpu::RequestAdapterOptions options = {};
|
1419
|
+
ctx->instance.WaitAny(ctx->instance.RequestAdapter(
|
1420
|
+
&options, wgpu::CallbackMode::AllowSpontaneous,
|
1421
|
+
[&ctx](wgpu::RequestAdapterStatus status, wgpu::Adapter adapter, const char * message) {
|
1422
|
+
if (status != wgpu::RequestAdapterStatus::Success) {
|
1423
|
+
GGML_LOG_ERROR("ggml_webgpu: Failed to get an adapter: %s\n", message);
|
1424
|
+
return;
|
1425
|
+
}
|
1426
|
+
ctx->adapter = std::move(adapter);
|
1427
|
+
}),
|
1428
|
+
UINT64_MAX);
|
1429
|
+
GGML_ASSERT(ctx->adapter != nullptr);
|
1430
|
+
|
1431
|
+
ctx->adapter.GetLimits(&ctx->limits);
|
1432
|
+
ctx->max_wg_size_x = 288; // default value
|
1433
|
+
|
1434
|
+
wgpu::AdapterInfo info{};
|
1435
|
+
ctx->adapter.GetInfo(&info);
|
1436
|
+
|
1437
|
+
// Initialize device
|
1438
|
+
std::vector<wgpu::FeatureName> required_features = { wgpu::FeatureName::ShaderF16,
|
1439
|
+
wgpu::FeatureName::ImplicitDeviceSynchronization };
|
1440
|
+
wgpu::DeviceDescriptor dev_desc;
|
1441
|
+
dev_desc.requiredLimits = &ctx->limits;
|
1442
|
+
dev_desc.requiredFeatures = required_features.data();
|
1443
|
+
dev_desc.requiredFeatureCount = required_features.size();
|
1444
|
+
dev_desc.SetDeviceLostCallback(
|
1445
|
+
wgpu::CallbackMode::AllowSpontaneous,
|
1446
|
+
[](const wgpu::Device & device, wgpu::DeviceLostReason reason, wgpu::StringView message) {
|
1447
|
+
GGML_UNUSED(device);
|
1448
|
+
GGML_LOG_ERROR("ggml_webgpu: Device lost! Reason: %d, Message: %s\n", static_cast<int>(reason),
|
1449
|
+
std::string(message).c_str());
|
1450
|
+
});
|
1451
|
+
dev_desc.SetUncapturedErrorCallback(
|
1452
|
+
[](const wgpu::Device & device, wgpu::ErrorType reason, wgpu::StringView message) {
|
1453
|
+
GGML_UNUSED(device);
|
1454
|
+
GGML_LOG_ERROR("ggml_webgpu: Device error! Reason: %d, Message: %s\n", static_cast<int>(reason),
|
1455
|
+
std::string(message).c_str());
|
1456
|
+
});
|
1457
|
+
ctx->instance.WaitAny(ctx->adapter.RequestDevice(
|
1458
|
+
&dev_desc, wgpu::CallbackMode::AllowSpontaneous,
|
1459
|
+
[ctx](wgpu::RequestDeviceStatus status, wgpu::Device device, wgpu::StringView message) {
|
1460
|
+
if (status != wgpu::RequestDeviceStatus::Success) {
|
1461
|
+
GGML_LOG_ERROR("ggml_webgpu: Failed to get a device: %s\n",
|
1462
|
+
std::string(message).c_str());
|
1463
|
+
return;
|
1464
|
+
}
|
1465
|
+
ctx->device = std::move(device);
|
1466
|
+
}),
|
1467
|
+
UINT64_MAX);
|
1468
|
+
GGML_ASSERT(ctx->device != nullptr);
|
1469
|
+
|
1470
|
+
// Initialize (compute) queue
|
1471
|
+
ctx->queue = ctx->device.GetQueue();
|
1472
|
+
|
1473
|
+
// Create buffer pool for shader parameters
|
1474
|
+
ctx->param_buf_pool.init(ctx->device, WEBGPU_NUM_PARAM_BUFS, WEBGPU_PARAMS_BUF_SIZE_BYTES,
|
1475
|
+
wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Uniform,
|
1476
|
+
wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::MapWrite);
|
1477
|
+
ctx->set_rows_error_buf_pool.init(ctx->device, WEBGPU_NUM_SET_ROWS_ERROR_BUFS, WEBGPU_SET_ROWS_ERROR_BUF_SIZE_BYTES,
|
1478
|
+
wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::Storage,
|
1479
|
+
wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::MapRead);
|
1480
|
+
|
1481
|
+
ggml_webgpu_init_memset_pipeline(ctx);
|
1482
|
+
ggml_webgpu_init_mul_mat_pipeline(ctx);
|
1483
|
+
ggml_webgpu_init_set_rows_pipeline(ctx);
|
1484
|
+
ggml_webgpu_init_get_rows_pipeline(ctx);
|
1485
|
+
ggml_webgpu_init_cpy_pipeline(ctx);
|
1486
|
+
ggml_webgpu_init_add_pipeline(ctx);
|
1487
|
+
ggml_webgpu_init_mul_pipeline(ctx);
|
1488
|
+
ggml_webgpu_init_rms_norm_pipeline(ctx);
|
1489
|
+
|
1490
|
+
#ifdef GGML_WEBGPU_DEBUG
|
1491
|
+
// Initialize debug buffers
|
1492
|
+
ggml_webgpu_create_buffer(ctx->device, ctx->debug_host_buf, WEBGPU_DEBUG_BUF_ELEMS * sizeof(uint32_t),
|
1493
|
+
wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::MapRead, "debug_host_buf");
|
1494
|
+
ggml_webgpu_create_buffer(ctx->device, ctx->debug_dev_buf, WEBGPU_DEBUG_BUF_ELEMS * sizeof(uint32_t),
|
1495
|
+
wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopySrc, "debug_dev_buf");
|
1496
|
+
#endif
|
1497
|
+
|
1498
|
+
static ggml_backend_webgpu_device_context device_ctx;
|
1499
|
+
device_ctx.webgpu_ctx = ctx;
|
1500
|
+
device_ctx.device_name = GGML_WEBGPU_NAME;
|
1501
|
+
device_ctx.device_desc = info.description;
|
1502
|
+
|
1503
|
+
GGML_LOG_INFO(
|
1504
|
+
"ggml_webgpu: adapter_info: vendor_id: %u | vendor: %s | architecture: %s | device_id: %u | name: %s | "
|
1505
|
+
"device_desc: %s\n",
|
1506
|
+
info.vendorID, std::string(info.vendor).c_str(), std::string(info.architecture).c_str(), info.deviceID,
|
1507
|
+
std::string(info.device).c_str(), std::string(info.description).c_str());
|
1508
|
+
|
1509
|
+
// See GGML Backend Device Interface section
|
1510
|
+
static ggml_backend_device device = {
|
1511
|
+
/* .iface = */ ggml_backend_webgpu_device_i,
|
1512
|
+
/* .reg = */ reg,
|
1513
|
+
/* .context = */ &device_ctx,
|
1514
|
+
};
|
1515
|
+
return &device;
|
1516
|
+
}
|
1517
|
+
|
1518
|
+
static const struct ggml_backend_reg_i ggml_backend_webgpu_reg_i = {
|
1519
|
+
/* .get_name = */ ggml_backend_webgpu_reg_get_name,
|
1520
|
+
/* .get_device_count = */ ggml_backend_webgpu_reg_get_device_count,
|
1521
|
+
/* .get_device = */ ggml_backend_webgpu_reg_get_device,
|
1522
|
+
/* .get_proc_address = */ NULL,
|
1523
|
+
};
|
1524
|
+
|
1525
|
+
/* End GGML Backend Registration Interface */
|
1526
|
+
|
1527
|
+
ggml_backend_reg_t ggml_backend_webgpu_reg() {
|
1528
|
+
WEBGPU_LOG_DEBUG("ggml_backend_webgpu_reg()");
|
1529
|
+
|
1530
|
+
webgpu_context webgpu_ctx = std::make_shared<webgpu_context_struct>();
|
1531
|
+
|
1532
|
+
static ggml_backend_webgpu_reg_context ctx;
|
1533
|
+
ctx.webgpu_ctx = webgpu_ctx;
|
1534
|
+
ctx.name = GGML_WEBGPU_NAME;
|
1535
|
+
ctx.device_count = 1;
|
1536
|
+
|
1537
|
+
wgpu::InstanceDescriptor instance_descriptor{};
|
1538
|
+
std::vector<wgpu::InstanceFeatureName> instance_features = { wgpu::InstanceFeatureName::TimedWaitAny };
|
1539
|
+
instance_descriptor.requiredFeatures = instance_features.data();
|
1540
|
+
instance_descriptor.requiredFeatureCount = instance_features.size();
|
1541
|
+
webgpu_ctx->instance = wgpu::CreateInstance(&instance_descriptor);
|
1542
|
+
GGML_ASSERT(webgpu_ctx->instance != nullptr);
|
1543
|
+
|
1544
|
+
static ggml_backend_reg reg = {
|
1545
|
+
/* .api_version = */ GGML_BACKEND_API_VERSION,
|
1546
|
+
/* .iface = */ ggml_backend_webgpu_reg_i,
|
1547
|
+
/* .context = */ &ctx,
|
1548
|
+
};
|
1549
|
+
return ®
|
1550
|
+
}
|
1551
|
+
|
1552
|
+
ggml_backend_t ggml_backend_webgpu_init(void) {
|
1553
|
+
ggml_backend_dev_t dev = ggml_backend_reg_dev_get(ggml_backend_webgpu_reg(), 0);
|
1554
|
+
|
1555
|
+
return ggml_backend_webgpu_device_init(dev, nullptr);
|
1556
|
+
}
|
1557
|
+
|
1558
|
+
GGML_BACKEND_DL_IMPL(ggml_backend_webgpu_reg)
|