whispercpp 1.3.3 → 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/ext/ruby_whisper_params.c +55 -25
- data/ext/sources/CMakeLists.txt +1 -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/addon.cpp +19 -19
- data/ext/sources/examples/addon.node/index.js +7 -5
- data/ext/sources/examples/bench/bench.cpp +26 -16
- data/ext/sources/examples/bench.wasm/index-tmpl.html +10 -9
- data/ext/sources/examples/cli/cli.cpp +4 -2
- data/ext/sources/examples/command/command.cpp +26 -24
- data/ext/sources/examples/command.wasm/index-tmpl.html +5 -4
- data/ext/sources/examples/common-ggml.cpp +2 -0
- data/ext/sources/examples/lsp/lsp.cpp +19 -17
- data/ext/sources/examples/server/server.cpp +24 -13
- data/ext/sources/examples/server.py +6 -1
- data/ext/sources/examples/stream/stream.cpp +4 -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 +2 -2
- 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 +588 -15
- data/ext/sources/examples/talk-llama/llama-arch.h +58 -1
- data/ext/sources/examples/talk-llama/llama-batch.cpp +103 -71
- data/ext/sources/examples/talk-llama/llama-batch.h +31 -18
- data/ext/sources/examples/talk-llama/llama-chat.cpp +120 -5
- data/ext/sources/examples/talk-llama/llama-chat.h +7 -0
- data/ext/sources/examples/talk-llama/llama-context.cpp +460 -357
- data/ext/sources/examples/talk-llama/llama-context.h +44 -29
- data/ext/sources/examples/talk-llama/llama-cparams.h +4 -4
- data/ext/sources/examples/talk-llama/llama-graph.cpp +543 -271
- data/ext/sources/examples/talk-llama/llama-graph.h +278 -168
- data/ext/sources/examples/talk-llama/llama-hparams.cpp +118 -4
- data/ext/sources/examples/talk-llama/llama-hparams.h +61 -15
- 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-unified-iswa.h → llama-kv-cache-iswa.h} +38 -29
- data/ext/sources/examples/talk-llama/llama-kv-cache.cpp +2020 -0
- data/ext/sources/examples/talk-llama/llama-kv-cache.h +358 -27
- data/ext/sources/examples/talk-llama/llama-kv-cells.h +80 -28
- data/ext/sources/examples/talk-llama/llama-memory-hybrid.cpp +56 -36
- data/ext/sources/examples/talk-llama/llama-memory-hybrid.h +30 -29
- data/ext/sources/examples/talk-llama/llama-memory-recurrent.cpp +48 -19
- data/ext/sources/examples/talk-llama/llama-memory-recurrent.h +13 -14
- data/ext/sources/examples/talk-llama/llama-memory.h +16 -10
- data/ext/sources/examples/talk-llama/llama-model-loader.cpp +2 -0
- data/ext/sources/examples/talk-llama/llama-model-loader.h +3 -2
- data/ext/sources/examples/talk-llama/llama-model.cpp +7165 -2336
- data/ext/sources/examples/talk-llama/llama-model.h +60 -9
- data/ext/sources/examples/talk-llama/llama-quant.cpp +48 -10
- data/ext/sources/examples/talk-llama/llama-sampling.cpp +226 -126
- data/ext/sources/examples/talk-llama/llama-vocab.cpp +440 -13
- data/ext/sources/examples/talk-llama/llama-vocab.h +45 -0
- data/ext/sources/examples/talk-llama/llama.cpp +65 -10
- data/ext/sources/examples/talk-llama/llama.h +95 -177
- data/ext/sources/examples/talk-llama/talk-llama.cpp +9 -6
- data/ext/sources/examples/talk-llama/unicode.cpp +207 -0
- data/ext/sources/examples/talk-llama/unicode.h +45 -0
- data/ext/sources/examples/wchess/wchess.cmd/wchess.cmd.cpp +4 -2
- data/ext/sources/examples/whisper.wasm/index-tmpl.html +17 -16
- data/ext/sources/ggml/CMakeLists.txt +59 -31
- data/ext/sources/ggml/cmake/ggml-config.cmake.in +132 -93
- data/ext/sources/ggml/include/ggml-backend.h +17 -1
- data/ext/sources/ggml/include/ggml-cpu.h +1 -1
- 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 +221 -16
- data/ext/sources/ggml/src/CMakeLists.txt +17 -2
- 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 +30 -13
- data/ext/sources/ggml/src/ggml-backend.cpp +221 -38
- data/ext/sources/ggml/src/ggml-blas/CMakeLists.txt +1 -1
- data/ext/sources/ggml/src/ggml-blas/ggml-blas.cpp +5 -4
- data/ext/sources/ggml/src/ggml-cann/CMakeLists.txt +14 -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 +143 -1
- data/ext/sources/ggml/src/ggml-cann/ggml-cann.cpp +488 -69
- data/ext/sources/ggml/src/ggml-common.h +17 -0
- data/ext/sources/ggml/src/ggml-cpu/CMakeLists.txt +40 -18
- data/ext/sources/ggml/src/ggml-cpu/amx/amx.cpp +4 -2
- data/ext/sources/ggml/src/ggml-cpu/arch/arm/quants.c +132 -596
- data/ext/sources/ggml/src/ggml-cpu/arch/arm/repack.cpp +14 -286
- data/ext/sources/ggml/src/ggml-cpu/arch/loongarch/quants.c +103 -582
- data/ext/sources/ggml/src/ggml-cpu/arch/powerpc/quants.c +162 -589
- data/ext/sources/ggml/src/ggml-cpu/arch/riscv/quants.c +265 -437
- data/ext/sources/ggml/src/ggml-cpu/arch/riscv/repack.cpp +3 -58
- data/ext/sources/ggml/src/ggml-cpu/arch/s390/quants.c +521 -353
- data/ext/sources/ggml/src/ggml-cpu/arch/wasm/quants.c +54 -314
- data/ext/sources/ggml/src/ggml-cpu/arch/x86/quants.c +184 -675
- data/ext/sources/ggml/src/ggml-cpu/arch/x86/repack.cpp +4679 -1657
- data/ext/sources/ggml/src/ggml-cpu/arch-fallback.h +32 -2
- data/ext/sources/ggml/src/ggml-cpu/common.h +14 -0
- data/ext/sources/ggml/src/ggml-cpu/ggml-cpu-impl.h +13 -6
- data/ext/sources/ggml/src/ggml-cpu/ggml-cpu.c +70 -42
- data/ext/sources/ggml/src/ggml-cpu/ggml-cpu.cpp +35 -28
- 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 +227 -97
- data/ext/sources/ggml/src/ggml-cpu/llamafile/sgemm.cpp +474 -1116
- data/ext/sources/ggml/src/ggml-cpu/ops.cpp +1587 -1177
- data/ext/sources/ggml/src/ggml-cpu/ops.h +5 -8
- data/ext/sources/ggml/src/ggml-cpu/quants.c +35 -0
- data/ext/sources/ggml/src/ggml-cpu/quants.h +8 -0
- data/ext/sources/ggml/src/ggml-cpu/repack.cpp +458 -47
- data/ext/sources/ggml/src/ggml-cpu/repack.h +22 -0
- data/ext/sources/ggml/src/ggml-cpu/simd-mappings.h +89 -60
- 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/traits.cpp +2 -2
- data/ext/sources/ggml/src/ggml-cpu/traits.h +1 -1
- data/ext/sources/ggml/src/ggml-cpu/vec.cpp +170 -26
- data/ext/sources/ggml/src/ggml-cpu/vec.h +506 -63
- 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 +250 -63
- data/ext/sources/ggml/src/ggml-cuda/conv-transpose-1d.cu +1 -4
- 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 +95 -22
- data/ext/sources/ggml/src/ggml-cuda/convert.cuh +15 -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 +498 -367
- data/ext/sources/ggml/src/ggml-cuda/fattn-mma-f16.cuh +137 -91
- 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 +86 -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 +379 -107
- 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 +56 -2
- 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/{mmv.cu → mmvf.cu} +53 -53
- data/ext/sources/ggml/src/ggml-cuda/{mmv.cuh → mmvf.cuh} +3 -3
- 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 -100
- data/ext/sources/ggml/src/ggml-cuda/sum.cu +6 -10
- data/ext/sources/ggml/src/ggml-cuda/sumrows.cu +21 -4
- 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 +90 -0
- data/ext/sources/ggml/src/ggml-cuda/unary.cuh +8 -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 +10 -2
- data/ext/sources/ggml/src/ggml-impl.h +119 -9
- data/ext/sources/ggml/src/ggml-metal/CMakeLists.txt +10 -7
- 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 +136 -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 +2854 -1503
- data/ext/sources/ggml/src/ggml-musa/CMakeLists.txt +18 -8
- data/ext/sources/ggml/src/ggml-opencl/CMakeLists.txt +18 -0
- data/ext/sources/ggml/src/ggml-opencl/ggml-opencl.cpp +2510 -242
- 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/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 +66 -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 +177 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/group_norm.cl +49 -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_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/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/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 +66 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/transpose.cl +20 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/tsembd.cl +2 -2
- data/ext/sources/ggml/src/ggml-opencl/kernels/upscale.cl +2 -3
- data/ext/sources/ggml/src/ggml-opt.cpp +97 -41
- data/ext/sources/ggml/src/ggml-quants.c +111 -16
- data/ext/sources/ggml/src/ggml-quants.h +6 -0
- data/ext/sources/ggml/src/ggml-rpc/ggml-rpc.cpp +67 -47
- data/ext/sources/ggml/src/ggml-sycl/backend.hpp +2 -0
- data/ext/sources/ggml/src/ggml-sycl/binbcast.cpp +15 -5
- data/ext/sources/ggml/src/ggml-sycl/binbcast.hpp +6 -0
- data/ext/sources/ggml/src/ggml-sycl/concat.cpp +25 -16
- data/ext/sources/ggml/src/ggml-sycl/conv.cpp +10 -4
- data/ext/sources/ggml/src/ggml-sycl/convert.cpp +166 -99
- data/ext/sources/ggml/src/ggml-sycl/cpy.cpp +72 -306
- data/ext/sources/ggml/src/ggml-sycl/cpy.hpp +213 -1
- data/ext/sources/ggml/src/ggml-sycl/dmmv.cpp +67 -49
- data/ext/sources/ggml/src/ggml-sycl/dpct/helper.hpp +1 -31
- data/ext/sources/ggml/src/ggml-sycl/element_wise.cpp +79 -29
- data/ext/sources/ggml/src/ggml-sycl/element_wise.hpp +2 -0
- data/ext/sources/ggml/src/ggml-sycl/gemm.hpp +14 -26
- data/ext/sources/ggml/src/ggml-sycl/getrows.cpp +9 -6
- data/ext/sources/ggml/src/ggml-sycl/ggml-sycl.cpp +328 -323
- data/ext/sources/ggml/src/ggml-sycl/gla.cpp +2 -2
- data/ext/sources/ggml/src/ggml-sycl/im2col.cpp +2 -2
- data/ext/sources/ggml/src/ggml-sycl/mmq.cpp +80 -60
- data/ext/sources/ggml/src/ggml-sycl/mmvq.cpp +201 -132
- data/ext/sources/ggml/src/ggml-sycl/norm.cpp +74 -55
- data/ext/sources/ggml/src/ggml-sycl/quantize.hpp +133 -0
- data/ext/sources/ggml/src/ggml-sycl/quants.hpp +8 -9
- data/ext/sources/ggml/src/ggml-sycl/rope.cpp +35 -42
- 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/softmax.cpp +3 -3
- data/ext/sources/ggml/src/ggml-sycl/tsembd.cpp +12 -6
- data/ext/sources/ggml/src/ggml-sycl/vecdotq.hpp +2 -6
- data/ext/sources/ggml/src/ggml-sycl/wkv.cpp +16 -12
- data/ext/sources/ggml/src/ggml-vulkan/ggml-vulkan.cpp +3492 -883
- 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/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_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 +4 -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/rms_norm.comp +55 -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_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 +335 -77
- 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 +478 -98
- data/ext/sources/ggml/src/gguf.cpp +8 -1
- data/ext/sources/src/whisper.cpp +23 -46
- 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/lib/whisper/model/uri.rb +1 -1
- data/sig/whisper.rbs +7 -0
- data/test/test_params.rb +8 -0
- data/test/test_whisper.rb +1 -1
- data/whispercpp.gemspec +1 -1
- metadata +164 -157
- data/ext/sources/examples/talk-llama/llama-kv-cache-unified-iswa.cpp +0 -279
- data/ext/sources/examples/talk-llama/llama-kv-cache-unified.cpp +0 -1841
- data/ext/sources/examples/talk-llama/llama-kv-cache-unified.h +0 -303
- 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-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/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 -6280
@@ -0,0 +1,1308 @@
|
|
1
|
+
#import "ggml-metal-device.h"
|
2
|
+
|
3
|
+
#import "ggml-impl.h"
|
4
|
+
#import "ggml-threading.h"
|
5
|
+
|
6
|
+
#include <Foundation/Foundation.h>
|
7
|
+
|
8
|
+
#include <Metal/Metal.h>
|
9
|
+
|
10
|
+
#ifndef TARGET_OS_VISION
|
11
|
+
#define TARGET_OS_VISION 0
|
12
|
+
#endif
|
13
|
+
|
14
|
+
// create residency sets only on macOS >= 15.0
|
15
|
+
#if !TARGET_CPU_X86_64 && TARGET_OS_OSX && __MAC_OS_X_VERSION_MAX_ALLOWED >= 150000 || \
|
16
|
+
TARGET_OS_IOS && __IPHONE_OS_VERSION_MAX_ALLOWED >= 180000 || \
|
17
|
+
TARGET_OS_TV && __TV_OS_VERSION_MAX_ALLOWED >= 180000 || \
|
18
|
+
TARGET_OS_VISION && __VISION_OS_VERSION_MAX_ALLOWED >= 200000
|
19
|
+
#define GGML_METAL_HAS_RESIDENCY_SETS 1
|
20
|
+
#endif
|
21
|
+
|
22
|
+
// overload of MTLGPUFamilyMetal3 (not available in some environments)
|
23
|
+
static const NSInteger MTLGPUFamilyMetal3_GGML = 5001;
|
24
|
+
|
25
|
+
#if !GGML_METAL_EMBED_LIBRARY
|
26
|
+
// Here to assist with NSBundle Path Hack
|
27
|
+
@interface GGMLMetalClass : NSObject
|
28
|
+
@end
|
29
|
+
@implementation GGMLMetalClass
|
30
|
+
@end
|
31
|
+
#endif
|
32
|
+
|
33
|
+
//
|
34
|
+
// MTLFunctionConstantValues wrapper
|
35
|
+
//
|
36
|
+
|
37
|
+
struct ggml_metal_cv {
|
38
|
+
MTLFunctionConstantValues * obj;
|
39
|
+
};
|
40
|
+
|
41
|
+
ggml_metal_cv_t ggml_metal_cv_init(void) {
|
42
|
+
ggml_metal_cv_t res = calloc(1, sizeof(struct ggml_metal_cv));
|
43
|
+
|
44
|
+
res->obj = [[MTLFunctionConstantValues alloc] init];
|
45
|
+
|
46
|
+
return res;
|
47
|
+
}
|
48
|
+
|
49
|
+
void ggml_metal_cv_free(ggml_metal_cv_t cv) {
|
50
|
+
[cv->obj release];
|
51
|
+
free(cv);
|
52
|
+
}
|
53
|
+
|
54
|
+
void ggml_metal_cv_set_int16(ggml_metal_cv_t cv, int16_t value, int32_t idx) {
|
55
|
+
[cv->obj setConstantValue:&value type:MTLDataTypeShort atIndex:idx];
|
56
|
+
}
|
57
|
+
|
58
|
+
void ggml_metal_cv_set_int32(ggml_metal_cv_t cv, int32_t value, int32_t idx) {
|
59
|
+
[cv->obj setConstantValue:&value type:MTLDataTypeInt atIndex:idx];
|
60
|
+
}
|
61
|
+
|
62
|
+
void ggml_metal_cv_set_bool(ggml_metal_cv_t cv, bool value, int32_t idx) {
|
63
|
+
[cv->obj setConstantValue:&value type:MTLDataTypeBool atIndex:idx];
|
64
|
+
}
|
65
|
+
|
66
|
+
//
|
67
|
+
// MTLComputePipelineState wrapper
|
68
|
+
//
|
69
|
+
|
70
|
+
struct ggml_metal_pipeline {
|
71
|
+
id<MTLComputePipelineState> obj;
|
72
|
+
|
73
|
+
// suggested dispatch sizes
|
74
|
+
int nsg;
|
75
|
+
|
76
|
+
int nr0;
|
77
|
+
int nr1;
|
78
|
+
|
79
|
+
size_t smem;
|
80
|
+
};
|
81
|
+
|
82
|
+
ggml_metal_pipeline_t ggml_metal_pipeline_init(void) {
|
83
|
+
ggml_metal_pipeline_t res = calloc(1, sizeof(struct ggml_metal_pipeline));
|
84
|
+
|
85
|
+
*res = (struct ggml_metal_pipeline) {
|
86
|
+
/*.obj =*/ nil,
|
87
|
+
/*.nsg =*/ 0,
|
88
|
+
/*.nr0 =*/ 0,
|
89
|
+
/*.nr1 =*/ 0,
|
90
|
+
/*.smem =*/ 0,
|
91
|
+
};
|
92
|
+
|
93
|
+
return res;
|
94
|
+
}
|
95
|
+
|
96
|
+
void ggml_metal_pipeline_free(ggml_metal_pipeline_t pipeline) {
|
97
|
+
[pipeline->obj release];
|
98
|
+
|
99
|
+
free(pipeline);
|
100
|
+
}
|
101
|
+
|
102
|
+
void ggml_metal_pipeline_set_nsg(ggml_metal_pipeline_t pipeline, int nsg) {
|
103
|
+
pipeline->nsg = nsg;
|
104
|
+
}
|
105
|
+
|
106
|
+
int ggml_metal_pipeline_get_nsg(ggml_metal_pipeline_t pipeline) {
|
107
|
+
return pipeline->nsg;
|
108
|
+
}
|
109
|
+
|
110
|
+
void ggml_metal_pipeline_set_nr0(ggml_metal_pipeline_t pipeline, int nr0) {
|
111
|
+
pipeline->nr0 = nr0;
|
112
|
+
}
|
113
|
+
|
114
|
+
int ggml_metal_pipeline_get_nr0(ggml_metal_pipeline_t pipeline) {
|
115
|
+
return pipeline->nr0;
|
116
|
+
}
|
117
|
+
|
118
|
+
void ggml_metal_pipeline_set_nr1(ggml_metal_pipeline_t pipeline, int nr1) {
|
119
|
+
pipeline->nr1 = nr1;
|
120
|
+
}
|
121
|
+
|
122
|
+
int ggml_metal_pipeline_get_nr1(ggml_metal_pipeline_t pipeline) {
|
123
|
+
return pipeline->nr1;
|
124
|
+
}
|
125
|
+
|
126
|
+
void ggml_metal_pipeline_set_smem(ggml_metal_pipeline_t pipeline, size_t smem) {
|
127
|
+
pipeline->smem = smem;
|
128
|
+
}
|
129
|
+
|
130
|
+
size_t ggml_metal_pipeline_get_smem(ggml_metal_pipeline_t pipeline) {
|
131
|
+
return pipeline->smem;
|
132
|
+
}
|
133
|
+
|
134
|
+
int ggml_metal_pipeline_max_theads_per_threadgroup(ggml_metal_pipeline_t pipeline) {
|
135
|
+
return pipeline->obj.maxTotalThreadsPerThreadgroup;
|
136
|
+
}
|
137
|
+
|
138
|
+
struct ggml_metal_library {
|
139
|
+
id<MTLLibrary> obj;
|
140
|
+
id<MTLDevice> device;
|
141
|
+
|
142
|
+
ggml_metal_pipelines_t pipelines; // cache of compiled pipelines
|
143
|
+
};
|
144
|
+
|
145
|
+
ggml_metal_library_t ggml_metal_library_init(ggml_metal_device_t dev) {
|
146
|
+
id<MTLLibrary> library = nil;
|
147
|
+
id<MTLDevice> device = ggml_metal_device_get_obj(dev);
|
148
|
+
|
149
|
+
// load library
|
150
|
+
//
|
151
|
+
// - first check if the library is embedded
|
152
|
+
// - then check if the library is in the bundle
|
153
|
+
// - if not found, load the source and compile it
|
154
|
+
// - if that fails, return NULL
|
155
|
+
//
|
156
|
+
// TODO: move to a function
|
157
|
+
{
|
158
|
+
const int64_t t_start = ggml_time_us();
|
159
|
+
|
160
|
+
NSError * error = nil;
|
161
|
+
NSString * src = nil;
|
162
|
+
|
163
|
+
#if GGML_METAL_EMBED_LIBRARY
|
164
|
+
GGML_LOG_INFO("%s: using embedded metal library\n", __func__);
|
165
|
+
|
166
|
+
extern const char ggml_metallib_start[];
|
167
|
+
extern const char ggml_metallib_end[];
|
168
|
+
|
169
|
+
src = [[NSString alloc] initWithBytes:ggml_metallib_start length:(ggml_metallib_end-ggml_metallib_start) encoding:NSUTF8StringEncoding];
|
170
|
+
#else
|
171
|
+
|
172
|
+
#ifdef SWIFT_PACKAGE
|
173
|
+
NSBundle * bundle = SWIFTPM_MODULE_BUNDLE;
|
174
|
+
#else
|
175
|
+
NSBundle * bundle = [NSBundle bundleForClass:[GGMLMetalClass class]];
|
176
|
+
#endif
|
177
|
+
|
178
|
+
NSString * path_lib = [bundle pathForResource:@"default" ofType:@"metallib"];
|
179
|
+
if (path_lib == nil) {
|
180
|
+
// Try to find the resource in the directory where the current binary located.
|
181
|
+
NSString * bin_cur = [[NSProcessInfo processInfo] arguments][0];
|
182
|
+
NSString * bin_dir = [bin_cur stringByDeletingLastPathComponent];
|
183
|
+
|
184
|
+
NSString * path_lib_default = [NSString pathWithComponents:@[bin_dir, @"default.metallib"]];
|
185
|
+
if ([[NSFileManager defaultManager] isReadableFileAtPath:path_lib_default]) {
|
186
|
+
GGML_LOG_INFO("%s: found '%s'\n", __func__, [path_lib_default UTF8String]);
|
187
|
+
|
188
|
+
NSDictionary * atts = [[NSFileManager defaultManager] attributesOfItemAtPath:path_lib_default error:&error];
|
189
|
+
if (atts && atts[NSFileType] == NSFileTypeSymbolicLink) {
|
190
|
+
// Optionally, if this is a symlink, try to resolve it.
|
191
|
+
path_lib_default = [[NSFileManager defaultManager] destinationOfSymbolicLinkAtPath:path_lib_default error:&error];
|
192
|
+
if (path_lib_default && [path_lib_default length] > 0 && ![[path_lib_default substringToIndex:1] isEqualToString:@"/"]) {
|
193
|
+
// It is a relative path, adding the binary directory as directory prefix.
|
194
|
+
path_lib_default = [NSString pathWithComponents:@[bin_dir, path_lib_default]];
|
195
|
+
}
|
196
|
+
if (!path_lib_default || ![[NSFileManager defaultManager] isReadableFileAtPath:path_lib_default]) {
|
197
|
+
// Link to the resource could not be resolved.
|
198
|
+
path_lib_default = nil;
|
199
|
+
} else {
|
200
|
+
GGML_LOG_INFO("%s: symlink resolved '%s'\n", __func__, [path_lib_default UTF8String]);
|
201
|
+
}
|
202
|
+
}
|
203
|
+
} else {
|
204
|
+
// The resource couldn't be found in the binary's directory.
|
205
|
+
path_lib_default = nil;
|
206
|
+
}
|
207
|
+
|
208
|
+
path_lib = path_lib_default;
|
209
|
+
}
|
210
|
+
|
211
|
+
if (path_lib != nil) {
|
212
|
+
// pre-compiled library found
|
213
|
+
NSURL * libURL = [NSURL fileURLWithPath:path_lib];
|
214
|
+
GGML_LOG_INFO("%s: loading '%s'\n", __func__, [path_lib UTF8String]);
|
215
|
+
|
216
|
+
library = [device newLibraryWithURL:libURL error:&error];
|
217
|
+
if (error) {
|
218
|
+
GGML_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
|
219
|
+
return nil;
|
220
|
+
}
|
221
|
+
} else {
|
222
|
+
GGML_LOG_INFO("%s: default.metallib not found, loading from source\n", __func__);
|
223
|
+
|
224
|
+
NSString * path_source;
|
225
|
+
NSString * path_resource = [[NSProcessInfo processInfo].environment objectForKey:@"GGML_METAL_PATH_RESOURCES"];
|
226
|
+
|
227
|
+
GGML_LOG_INFO("%s: GGML_METAL_PATH_RESOURCES = %s\n", __func__, path_resource ? [path_resource UTF8String] : "nil");
|
228
|
+
|
229
|
+
if (path_resource) {
|
230
|
+
path_source = [path_resource stringByAppendingPathComponent:@"ggml-metal.metal"];
|
231
|
+
} else {
|
232
|
+
path_source = [bundle pathForResource:@"ggml-metal" ofType:@"metal"];
|
233
|
+
}
|
234
|
+
|
235
|
+
if (path_source == nil) {
|
236
|
+
GGML_LOG_WARN("%s: error: could not use bundle path to find ggml-metal.metal, falling back to trying cwd\n", __func__);
|
237
|
+
path_source = @"ggml-metal.metal";
|
238
|
+
}
|
239
|
+
|
240
|
+
GGML_LOG_INFO("%s: loading '%s'\n", __func__, [path_source UTF8String]);
|
241
|
+
|
242
|
+
src = [NSString stringWithContentsOfFile:path_source encoding:NSUTF8StringEncoding error:&error];
|
243
|
+
if (error) {
|
244
|
+
GGML_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
|
245
|
+
return nil;
|
246
|
+
}
|
247
|
+
}
|
248
|
+
#endif
|
249
|
+
|
250
|
+
if (!library) {
|
251
|
+
@autoreleasepool {
|
252
|
+
// dictionary of preprocessor macros
|
253
|
+
NSMutableDictionary * prep = [NSMutableDictionary dictionary];
|
254
|
+
|
255
|
+
if (ggml_metal_device_get_props(dev)->has_bfloat) {
|
256
|
+
[prep setObject:@"1" forKey:@"GGML_METAL_HAS_BF16"];
|
257
|
+
}
|
258
|
+
|
259
|
+
#if GGML_METAL_EMBED_LIBRARY
|
260
|
+
[prep setObject:@"1" forKey:@"GGML_METAL_EMBED_LIBRARY"];
|
261
|
+
#endif
|
262
|
+
|
263
|
+
MTLCompileOptions * options = [MTLCompileOptions new];
|
264
|
+
options.preprocessorMacros = prep;
|
265
|
+
|
266
|
+
//[options setFastMathEnabled:false];
|
267
|
+
|
268
|
+
library = [device newLibraryWithSource:src options:options error:&error];
|
269
|
+
if (error) {
|
270
|
+
GGML_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
|
271
|
+
return nil;
|
272
|
+
}
|
273
|
+
|
274
|
+
#if !__has_feature(objc_arc)
|
275
|
+
[options release];
|
276
|
+
#endif
|
277
|
+
}
|
278
|
+
}
|
279
|
+
|
280
|
+
#if GGML_METAL_EMBED_LIBRARY
|
281
|
+
[src release];
|
282
|
+
#endif // GGML_METAL_EMBED_LIBRARY
|
283
|
+
|
284
|
+
GGML_LOG_INFO("%s: loaded in %.3f sec\n", __func__, (ggml_time_us() - t_start) / 1e6);
|
285
|
+
}
|
286
|
+
|
287
|
+
ggml_metal_library_t res = calloc(1, sizeof(struct ggml_metal_library));
|
288
|
+
|
289
|
+
res->obj = library;
|
290
|
+
res->device = device;
|
291
|
+
res->pipelines = ggml_metal_pipelines_init();
|
292
|
+
|
293
|
+
return res;
|
294
|
+
}
|
295
|
+
|
296
|
+
void ggml_metal_library_free(ggml_metal_library_t lib) {
|
297
|
+
if (!lib) {
|
298
|
+
return;
|
299
|
+
}
|
300
|
+
|
301
|
+
if (lib->obj) {
|
302
|
+
[lib->obj release];
|
303
|
+
}
|
304
|
+
|
305
|
+
ggml_metal_pipelines_free(lib->pipelines);
|
306
|
+
|
307
|
+
free(lib);
|
308
|
+
}
|
309
|
+
|
310
|
+
ggml_metal_pipeline_t ggml_metal_library_get_pipeline(ggml_metal_library_t lib, const char * name) {
|
311
|
+
return ggml_metal_pipelines_get(lib->pipelines, name);
|
312
|
+
}
|
313
|
+
|
314
|
+
ggml_metal_pipeline_t ggml_metal_library_compile_pipeline(ggml_metal_library_t lib, const char * base, const char * name, ggml_metal_cv_t cv) {
|
315
|
+
// note: the pipelines are cached in the library per device, so they are shared across all metal contexts
|
316
|
+
ggml_critical_section_start();
|
317
|
+
|
318
|
+
ggml_metal_pipeline_t res = ggml_metal_library_get_pipeline(lib, name);
|
319
|
+
if (res) {
|
320
|
+
ggml_critical_section_end();
|
321
|
+
|
322
|
+
return res;
|
323
|
+
}
|
324
|
+
|
325
|
+
res = ggml_metal_pipeline_init();
|
326
|
+
|
327
|
+
@autoreleasepool {
|
328
|
+
NSError * error = nil;
|
329
|
+
|
330
|
+
NSString * base_func = [NSString stringWithUTF8String:base];
|
331
|
+
|
332
|
+
GGML_LOG_DEBUG("%s: compiling pipeline: base = '%s', name = '%s'\n", __func__, base, name);
|
333
|
+
|
334
|
+
id<MTLFunction> mtl_function;
|
335
|
+
if (!cv) {
|
336
|
+
mtl_function = [lib->obj newFunctionWithName:base_func];
|
337
|
+
} else {
|
338
|
+
mtl_function = [lib->obj newFunctionWithName:base_func constantValues:cv->obj error:&error];
|
339
|
+
}
|
340
|
+
if (!mtl_function) {
|
341
|
+
ggml_critical_section_end();
|
342
|
+
|
343
|
+
GGML_LOG_ERROR("%s: error: failed to compile pipeline: base = '%s', name = '%s'\n", __func__, base, name);
|
344
|
+
if (error) {
|
345
|
+
GGML_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
|
346
|
+
}
|
347
|
+
|
348
|
+
return nil;
|
349
|
+
}
|
350
|
+
|
351
|
+
res->obj = [lib->device newComputePipelineStateWithFunction:mtl_function error:&error];
|
352
|
+
|
353
|
+
ggml_metal_pipelines_add(lib->pipelines, name, res);
|
354
|
+
|
355
|
+
[mtl_function release];
|
356
|
+
|
357
|
+
GGML_LOG_DEBUG("%s: loaded %-40s %16p | th_max = %4d | th_width = %4d\n", __func__, name, (void *) res->obj,
|
358
|
+
(int) res->obj.maxTotalThreadsPerThreadgroup,
|
359
|
+
(int) res->obj.threadExecutionWidth);
|
360
|
+
}
|
361
|
+
|
362
|
+
ggml_critical_section_end();
|
363
|
+
|
364
|
+
return res;
|
365
|
+
}
|
366
|
+
|
367
|
+
//
|
368
|
+
// MTLComputeCommandEncoder wrapper
|
369
|
+
//
|
370
|
+
|
371
|
+
struct ggml_metal_encoder {
|
372
|
+
id<MTLComputeCommandEncoder> obj;
|
373
|
+
};
|
374
|
+
|
375
|
+
ggml_metal_encoder_t ggml_metal_encoder_init(ggml_metal_cmd_buf_t cmd_buf_raw, bool concurrent) {
|
376
|
+
ggml_metal_encoder_t res = calloc(1, sizeof(struct ggml_metal_encoder));
|
377
|
+
|
378
|
+
id<MTLCommandBuffer> cmd_buf = (id<MTLCommandBuffer>) cmd_buf_raw;
|
379
|
+
|
380
|
+
if (concurrent) {
|
381
|
+
res->obj = [cmd_buf computeCommandEncoderWithDispatchType: MTLDispatchTypeConcurrent];
|
382
|
+
} else {
|
383
|
+
res->obj = [cmd_buf computeCommandEncoder];
|
384
|
+
}
|
385
|
+
|
386
|
+
[res->obj retain];
|
387
|
+
|
388
|
+
return res;
|
389
|
+
}
|
390
|
+
|
391
|
+
void ggml_metal_encoder_free(ggml_metal_encoder_t encoder) {
|
392
|
+
[encoder->obj release];
|
393
|
+
free(encoder);
|
394
|
+
}
|
395
|
+
|
396
|
+
void ggml_metal_encoder_debug_group_push(ggml_metal_encoder_t encoder, const char * name) {
|
397
|
+
[encoder->obj pushDebugGroup:[NSString stringWithCString:name encoding:NSUTF8StringEncoding]];
|
398
|
+
}
|
399
|
+
|
400
|
+
void ggml_metal_encoder_debug_group_pop (ggml_metal_encoder_t encoder) {
|
401
|
+
[encoder->obj popDebugGroup];
|
402
|
+
}
|
403
|
+
|
404
|
+
void ggml_metal_encoder_set_pipeline(ggml_metal_encoder_t encoder, ggml_metal_pipeline_t pipeline) {
|
405
|
+
[encoder->obj setComputePipelineState:pipeline->obj];
|
406
|
+
}
|
407
|
+
|
408
|
+
void ggml_metal_encoder_set_bytes(ggml_metal_encoder_t encoder, void * data, size_t size, int idx) {
|
409
|
+
[encoder->obj setBytes:data length:size atIndex:idx];
|
410
|
+
}
|
411
|
+
|
412
|
+
void ggml_metal_encoder_set_buffer(ggml_metal_encoder_t encoder, struct ggml_metal_buffer_id buffer, int idx) {
|
413
|
+
[encoder->obj setBuffer:buffer.metal offset:buffer.offs atIndex:idx];
|
414
|
+
}
|
415
|
+
|
416
|
+
void ggml_metal_encoder_set_threadgroup_memory_size(ggml_metal_encoder_t encoder, size_t size, int idx) {
|
417
|
+
[encoder->obj setThreadgroupMemoryLength:size atIndex:idx];
|
418
|
+
}
|
419
|
+
|
420
|
+
void ggml_metal_encoder_dispatch_threadgroups(ggml_metal_encoder_t encoder, int tg0, int tg1, int tg2, int tptg0, int tptg1, int tptg2) {
|
421
|
+
[encoder->obj dispatchThreadgroups:MTLSizeMake(tg0, tg1, tg2) threadsPerThreadgroup:MTLSizeMake(tptg0, tptg1, tptg2)];
|
422
|
+
}
|
423
|
+
|
424
|
+
void ggml_metal_encoder_memory_barrier(ggml_metal_encoder_t encoder) {
|
425
|
+
[encoder->obj memoryBarrierWithScope:MTLBarrierScopeBuffers];
|
426
|
+
}
|
427
|
+
|
428
|
+
void ggml_metal_encoder_end_encoding(ggml_metal_encoder_t encoder) {
|
429
|
+
[encoder->obj endEncoding];
|
430
|
+
}
|
431
|
+
|
432
|
+
struct ggml_metal_device {
|
433
|
+
id<MTLDevice> mtl_device;
|
434
|
+
|
435
|
+
// a single global queue shared by all Metal backends
|
436
|
+
// technically not needed for devices with unified memory, but enables discrete GPUs support
|
437
|
+
// ref: https://github.com/ggml-org/llama.cpp/pull/15906
|
438
|
+
id<MTLCommandQueue> mtl_queue;
|
439
|
+
|
440
|
+
ggml_metal_library_t library;
|
441
|
+
|
442
|
+
struct ggml_metal_device_props props;
|
443
|
+
};
|
444
|
+
|
445
|
+
ggml_metal_device_t ggml_metal_device_init(void) {
|
446
|
+
ggml_metal_device_t dev = calloc(1, sizeof(struct ggml_metal_device));
|
447
|
+
|
448
|
+
assert(dev != NULL);
|
449
|
+
|
450
|
+
if (dev->mtl_device == nil) {
|
451
|
+
dev->mtl_device = MTLCreateSystemDefaultDevice();
|
452
|
+
|
453
|
+
if (dev->mtl_device) {
|
454
|
+
dev->mtl_queue = [dev->mtl_device newCommandQueue];
|
455
|
+
if (dev->mtl_queue == nil) {
|
456
|
+
GGML_LOG_ERROR("%s: error: failed to create command queue\n", __func__);
|
457
|
+
}
|
458
|
+
|
459
|
+
dev->props.has_simdgroup_reduction = [dev->mtl_device supportsFamily:MTLGPUFamilyApple7];
|
460
|
+
dev->props.has_simdgroup_reduction |= [dev->mtl_device supportsFamily:MTLGPUFamilyMetal3_GGML];
|
461
|
+
|
462
|
+
dev->props.has_simdgroup_mm = [dev->mtl_device supportsFamily:MTLGPUFamilyApple7];
|
463
|
+
dev->props.has_unified_memory = dev->mtl_device.hasUnifiedMemory;
|
464
|
+
|
465
|
+
dev->props.has_bfloat = [dev->mtl_device supportsFamily:MTLGPUFamilyMetal3_GGML];
|
466
|
+
dev->props.has_bfloat |= [dev->mtl_device supportsFamily:MTLGPUFamilyApple6];
|
467
|
+
|
468
|
+
dev->props.use_residency_sets = true;
|
469
|
+
#if defined(GGML_METAL_HAS_RESIDENCY_SETS)
|
470
|
+
dev->props.use_residency_sets = getenv("GGML_METAL_NO_RESIDENCY") == nil;
|
471
|
+
#endif
|
472
|
+
|
473
|
+
dev->props.use_shared_buffers = dev->props.has_unified_memory;
|
474
|
+
|
475
|
+
if (getenv("GGML_METAL_SHARED_BUFFERS_DISABLE") != NULL) {
|
476
|
+
dev->props.use_shared_buffers = false;
|
477
|
+
}
|
478
|
+
|
479
|
+
dev->props.supports_gpu_family_apple7 = [dev->mtl_device supportsFamily:MTLGPUFamilyApple7];
|
480
|
+
|
481
|
+
dev->props.max_buffer_size = dev->mtl_device.maxBufferLength;
|
482
|
+
dev->props.max_working_set_size = dev->mtl_device.recommendedMaxWorkingSetSize;
|
483
|
+
dev->props.max_theadgroup_memory_size = dev->mtl_device.maxThreadgroupMemoryLength;
|
484
|
+
|
485
|
+
strncpy(dev->props.name, [[dev->mtl_device name] UTF8String], sizeof(dev->props.name) - 1);
|
486
|
+
|
487
|
+
dev->library = ggml_metal_library_init(dev);
|
488
|
+
if (!dev->library) {
|
489
|
+
GGML_LOG_ERROR("%s: error: failed to create library\n", __func__);
|
490
|
+
}
|
491
|
+
|
492
|
+
// --------------------------------------------------
|
493
|
+
|
494
|
+
// print MTL GPU family:
|
495
|
+
GGML_LOG_INFO("%s: GPU name: %s\n", __func__, dev->props.name);
|
496
|
+
|
497
|
+
// determine max supported GPU family
|
498
|
+
// https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf
|
499
|
+
// https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
|
500
|
+
{
|
501
|
+
for (int i = MTLGPUFamilyApple1 + 20; i >= MTLGPUFamilyApple1; --i) {
|
502
|
+
if ([dev->mtl_device supportsFamily:i]) {
|
503
|
+
GGML_LOG_INFO("%s: GPU family: MTLGPUFamilyApple%d (%d)\n", __func__, i - (int) MTLGPUFamilyApple1 + 1, i);
|
504
|
+
break;
|
505
|
+
}
|
506
|
+
}
|
507
|
+
|
508
|
+
for (int i = MTLGPUFamilyCommon1 + 5; i >= MTLGPUFamilyCommon1; --i) {
|
509
|
+
if ([dev->mtl_device supportsFamily:i]) {
|
510
|
+
GGML_LOG_INFO("%s: GPU family: MTLGPUFamilyCommon%d (%d)\n", __func__, i - (int) MTLGPUFamilyCommon1 + 1, i);
|
511
|
+
break;
|
512
|
+
}
|
513
|
+
}
|
514
|
+
|
515
|
+
for (int i = MTLGPUFamilyMetal3_GGML + 5; i >= MTLGPUFamilyMetal3_GGML; --i) {
|
516
|
+
if ([dev->mtl_device supportsFamily:i]) {
|
517
|
+
GGML_LOG_INFO("%s: GPU family: MTLGPUFamilyMetal%d (%d)\n", __func__, i - (int) MTLGPUFamilyMetal3_GGML + 3, i);
|
518
|
+
break;
|
519
|
+
}
|
520
|
+
}
|
521
|
+
}
|
522
|
+
|
523
|
+
GGML_LOG_INFO("%s: simdgroup reduction = %s\n", __func__, dev->props.has_simdgroup_reduction ? "true" : "false");
|
524
|
+
GGML_LOG_INFO("%s: simdgroup matrix mul. = %s\n", __func__, dev->props.has_simdgroup_mm ? "true" : "false");
|
525
|
+
GGML_LOG_INFO("%s: has unified memory = %s\n", __func__, dev->props.has_unified_memory ? "true" : "false");
|
526
|
+
GGML_LOG_INFO("%s: has bfloat = %s\n", __func__, dev->props.has_bfloat ? "true" : "false");
|
527
|
+
GGML_LOG_INFO("%s: use residency sets = %s\n", __func__, dev->props.use_residency_sets ? "true" : "false");
|
528
|
+
GGML_LOG_INFO("%s: use shared buffers = %s\n", __func__, dev->props.use_shared_buffers ? "true" : "false");
|
529
|
+
|
530
|
+
#if TARGET_OS_OSX || (TARGET_OS_IOS && __clang_major__ >= 15)
|
531
|
+
if (@available(macOS 10.12, iOS 16.0, *)) {
|
532
|
+
GGML_LOG_INFO("%s: recommendedMaxWorkingSetSize = %8.2f MB\n", __func__, dev->props.max_working_set_size / 1e6);
|
533
|
+
}
|
534
|
+
#endif
|
535
|
+
}
|
536
|
+
}
|
537
|
+
|
538
|
+
return dev;
|
539
|
+
}
|
540
|
+
|
541
|
+
void ggml_metal_device_free(ggml_metal_device_t dev) {
|
542
|
+
assert(dev != NULL);
|
543
|
+
|
544
|
+
ggml_metal_library_free(dev->library);
|
545
|
+
dev->library = NULL;
|
546
|
+
|
547
|
+
if (dev->mtl_queue) {
|
548
|
+
[dev->mtl_queue release];
|
549
|
+
dev->mtl_queue = nil;
|
550
|
+
}
|
551
|
+
|
552
|
+
if (dev->mtl_device) {
|
553
|
+
[dev->mtl_device release];
|
554
|
+
dev->mtl_device = nil;
|
555
|
+
}
|
556
|
+
|
557
|
+
free(dev);
|
558
|
+
}
|
559
|
+
|
560
|
+
void * ggml_metal_device_get_obj(ggml_metal_device_t dev) {
|
561
|
+
return dev->mtl_device;
|
562
|
+
}
|
563
|
+
|
564
|
+
void * ggml_metal_device_get_queue(ggml_metal_device_t dev) {
|
565
|
+
return dev->mtl_queue;
|
566
|
+
}
|
567
|
+
|
568
|
+
ggml_metal_library_t ggml_metal_device_get_library(ggml_metal_device_t dev) {
|
569
|
+
return dev->library;
|
570
|
+
}
|
571
|
+
|
572
|
+
void ggml_metal_device_get_memory(ggml_metal_device_t dev, size_t * free, size_t * total) {
|
573
|
+
if (@available(macOS 10.12, iOS 16.0, *)) {
|
574
|
+
*total = dev->mtl_device.recommendedMaxWorkingSetSize;
|
575
|
+
*free = *total - dev->mtl_device.currentAllocatedSize;
|
576
|
+
} else {
|
577
|
+
*free = 0;
|
578
|
+
*total = 0;
|
579
|
+
}
|
580
|
+
}
|
581
|
+
|
582
|
+
bool ggml_metal_device_supports_op(ggml_metal_device_t dev, const struct ggml_tensor * op) {
|
583
|
+
const bool has_simdgroup_mm = dev->props.has_simdgroup_mm;
|
584
|
+
const bool has_simdgroup_reduction = dev->props.has_simdgroup_reduction;
|
585
|
+
const bool has_bfloat = dev->props.has_bfloat;
|
586
|
+
|
587
|
+
if (!has_bfloat) {
|
588
|
+
if (op->type == GGML_TYPE_BF16) {
|
589
|
+
return false;
|
590
|
+
}
|
591
|
+
|
592
|
+
for (size_t i = 0, n = 3; i < n; ++i) {
|
593
|
+
if (op->src[i] != NULL && op->src[i]->type == GGML_TYPE_BF16) {
|
594
|
+
return false;
|
595
|
+
}
|
596
|
+
}
|
597
|
+
}
|
598
|
+
|
599
|
+
switch (op->op) {
|
600
|
+
case GGML_OP_UNARY:
|
601
|
+
switch (ggml_get_unary_op(op)) {
|
602
|
+
case GGML_UNARY_OP_TANH:
|
603
|
+
case GGML_UNARY_OP_RELU:
|
604
|
+
case GGML_UNARY_OP_SIGMOID:
|
605
|
+
case GGML_UNARY_OP_GELU:
|
606
|
+
case GGML_UNARY_OP_GELU_ERF:
|
607
|
+
case GGML_UNARY_OP_GELU_QUICK:
|
608
|
+
case GGML_UNARY_OP_SILU:
|
609
|
+
case GGML_UNARY_OP_ELU:
|
610
|
+
case GGML_UNARY_OP_NEG:
|
611
|
+
case GGML_UNARY_OP_ABS:
|
612
|
+
case GGML_UNARY_OP_SGN:
|
613
|
+
case GGML_UNARY_OP_STEP:
|
614
|
+
case GGML_UNARY_OP_HARDSWISH:
|
615
|
+
case GGML_UNARY_OP_HARDSIGMOID:
|
616
|
+
case GGML_UNARY_OP_EXP:
|
617
|
+
return ggml_is_contiguous(op->src[0]) && op->src[0]->type == GGML_TYPE_F32;
|
618
|
+
default:
|
619
|
+
return false;
|
620
|
+
}
|
621
|
+
case GGML_OP_GLU:
|
622
|
+
switch (ggml_get_glu_op(op)) {
|
623
|
+
case GGML_GLU_OP_REGLU:
|
624
|
+
case GGML_GLU_OP_GEGLU:
|
625
|
+
case GGML_GLU_OP_SWIGLU:
|
626
|
+
case GGML_GLU_OP_SWIGLU_OAI:
|
627
|
+
case GGML_GLU_OP_GEGLU_ERF:
|
628
|
+
case GGML_GLU_OP_GEGLU_QUICK:
|
629
|
+
return ggml_is_contiguous_1(op->src[0]) && op->src[0]->type == GGML_TYPE_F32;
|
630
|
+
default:
|
631
|
+
return false;
|
632
|
+
}
|
633
|
+
case GGML_OP_NONE:
|
634
|
+
case GGML_OP_RESHAPE:
|
635
|
+
case GGML_OP_VIEW:
|
636
|
+
case GGML_OP_TRANSPOSE:
|
637
|
+
case GGML_OP_PERMUTE:
|
638
|
+
case GGML_OP_CONCAT:
|
639
|
+
return true;
|
640
|
+
case GGML_OP_ADD:
|
641
|
+
case GGML_OP_SUB:
|
642
|
+
case GGML_OP_MUL:
|
643
|
+
case GGML_OP_DIV:
|
644
|
+
case GGML_OP_ADD_ID:
|
645
|
+
return op->src[0]->type == GGML_TYPE_F32;
|
646
|
+
case GGML_OP_ACC:
|
647
|
+
case GGML_OP_REPEAT:
|
648
|
+
case GGML_OP_SCALE:
|
649
|
+
case GGML_OP_CONV_TRANSPOSE_1D:
|
650
|
+
return true;
|
651
|
+
case GGML_OP_CLAMP:
|
652
|
+
return op->src[0]->type == GGML_TYPE_F32;
|
653
|
+
case GGML_OP_SQR:
|
654
|
+
case GGML_OP_SQRT:
|
655
|
+
case GGML_OP_SIN:
|
656
|
+
case GGML_OP_COS:
|
657
|
+
case GGML_OP_LOG:
|
658
|
+
return ggml_is_contiguous(op->src[0]) && op->src[0]->type == GGML_TYPE_F32;
|
659
|
+
case GGML_OP_SUM_ROWS:
|
660
|
+
case GGML_OP_MEAN:
|
661
|
+
case GGML_OP_SOFT_MAX:
|
662
|
+
case GGML_OP_GROUP_NORM:
|
663
|
+
return has_simdgroup_reduction && ggml_is_contiguous_rows(op->src[0]);
|
664
|
+
case GGML_OP_L2_NORM:
|
665
|
+
return has_simdgroup_reduction && (op->ne[0] % 4 == 0 && ggml_is_contiguous_1(op->src[0]));
|
666
|
+
case GGML_OP_ARGMAX:
|
667
|
+
return has_simdgroup_reduction;
|
668
|
+
case GGML_OP_NORM:
|
669
|
+
case GGML_OP_RMS_NORM:
|
670
|
+
return has_simdgroup_reduction && (ggml_is_contiguous_rows(op->src[0]));
|
671
|
+
case GGML_OP_ROPE:
|
672
|
+
return true;
|
673
|
+
case GGML_OP_IM2COL:
|
674
|
+
return ggml_is_contiguous(op->src[1]) && op->src[1]->type == GGML_TYPE_F32 && (op->type == GGML_TYPE_F16 || op->type == GGML_TYPE_F32);
|
675
|
+
case GGML_OP_POOL_1D:
|
676
|
+
return false;
|
677
|
+
case GGML_OP_UPSCALE:
|
678
|
+
return op->src[0]->type == GGML_TYPE_F32 && op->op_params[0] == GGML_SCALE_MODE_NEAREST;
|
679
|
+
case GGML_OP_POOL_2D:
|
680
|
+
return op->src[0]->type == GGML_TYPE_F32;
|
681
|
+
case GGML_OP_PAD:
|
682
|
+
return (ggml_get_op_params_i32(op, 0) == 0) && (ggml_get_op_params_i32(op, 2) == 0) &&
|
683
|
+
(ggml_get_op_params_i32(op, 4) == 0) && (ggml_get_op_params_i32(op, 6) == 0);
|
684
|
+
case GGML_OP_PAD_REFLECT_1D:
|
685
|
+
case GGML_OP_TIMESTEP_EMBEDDING:
|
686
|
+
case GGML_OP_LEAKY_RELU:
|
687
|
+
return op->src[0]->type == GGML_TYPE_F32;
|
688
|
+
case GGML_OP_ARGSORT:
|
689
|
+
// TODO: Support arbitrary column width
|
690
|
+
return op->src[0]->ne[0] <= 1024;
|
691
|
+
case GGML_OP_ARANGE:
|
692
|
+
return true;
|
693
|
+
case GGML_OP_FLASH_ATTN_EXT:
|
694
|
+
// for new head sizes, add checks here
|
695
|
+
if (op->src[0]->ne[0] != 40 &&
|
696
|
+
op->src[0]->ne[0] != 64 &&
|
697
|
+
op->src[0]->ne[0] != 80 &&
|
698
|
+
op->src[0]->ne[0] != 96 &&
|
699
|
+
op->src[0]->ne[0] != 112 &&
|
700
|
+
op->src[0]->ne[0] != 128 &&
|
701
|
+
op->src[0]->ne[0] != 192 &&
|
702
|
+
op->src[0]->ne[0] != 256) {
|
703
|
+
return false;
|
704
|
+
}
|
705
|
+
if (op->src[0]->ne[0] == 576) {
|
706
|
+
// DeepSeek sizes
|
707
|
+
// TODO: disabled for now, until optmized
|
708
|
+
return false;
|
709
|
+
}
|
710
|
+
if (op->src[1]->type != op->src[2]->type) {
|
711
|
+
return false;
|
712
|
+
}
|
713
|
+
return has_simdgroup_mm; // TODO: over-restricted for vec-kernels
|
714
|
+
case GGML_OP_SSM_CONV:
|
715
|
+
case GGML_OP_SSM_SCAN:
|
716
|
+
return has_simdgroup_reduction;
|
717
|
+
case GGML_OP_RWKV_WKV6:
|
718
|
+
case GGML_OP_RWKV_WKV7:
|
719
|
+
return true;
|
720
|
+
case GGML_OP_MUL_MAT:
|
721
|
+
case GGML_OP_MUL_MAT_ID:
|
722
|
+
return has_simdgroup_reduction;
|
723
|
+
case GGML_OP_CPY:
|
724
|
+
case GGML_OP_DUP:
|
725
|
+
case GGML_OP_CONT:
|
726
|
+
{
|
727
|
+
switch (op->src[0]->type) {
|
728
|
+
case GGML_TYPE_F32:
|
729
|
+
switch (op->type) {
|
730
|
+
case GGML_TYPE_F32:
|
731
|
+
case GGML_TYPE_F16:
|
732
|
+
case GGML_TYPE_BF16:
|
733
|
+
case GGML_TYPE_Q8_0:
|
734
|
+
case GGML_TYPE_Q4_0:
|
735
|
+
case GGML_TYPE_Q4_1:
|
736
|
+
case GGML_TYPE_Q5_0:
|
737
|
+
case GGML_TYPE_Q5_1:
|
738
|
+
case GGML_TYPE_IQ4_NL:
|
739
|
+
case GGML_TYPE_I32:
|
740
|
+
return true;
|
741
|
+
default:
|
742
|
+
return false;
|
743
|
+
}
|
744
|
+
case GGML_TYPE_F16:
|
745
|
+
switch (op->type) {
|
746
|
+
case GGML_TYPE_F32:
|
747
|
+
case GGML_TYPE_F16:
|
748
|
+
return true;
|
749
|
+
default:
|
750
|
+
return false;
|
751
|
+
}
|
752
|
+
case GGML_TYPE_BF16:
|
753
|
+
switch (op->type) {
|
754
|
+
case GGML_TYPE_F32:
|
755
|
+
case GGML_TYPE_BF16:
|
756
|
+
return true;
|
757
|
+
default:
|
758
|
+
return false;
|
759
|
+
}
|
760
|
+
case GGML_TYPE_Q4_0:
|
761
|
+
case GGML_TYPE_Q4_1:
|
762
|
+
case GGML_TYPE_Q5_0:
|
763
|
+
case GGML_TYPE_Q5_1:
|
764
|
+
case GGML_TYPE_Q8_0:
|
765
|
+
switch (op->type) {
|
766
|
+
case GGML_TYPE_F32:
|
767
|
+
case GGML_TYPE_F16:
|
768
|
+
return true;
|
769
|
+
default:
|
770
|
+
return false;
|
771
|
+
}
|
772
|
+
case GGML_TYPE_I32:
|
773
|
+
return op->type == GGML_TYPE_F32;
|
774
|
+
default:
|
775
|
+
return false;
|
776
|
+
};
|
777
|
+
}
|
778
|
+
case GGML_OP_GET_ROWS:
|
779
|
+
{
|
780
|
+
return op->ne[3] == 1;
|
781
|
+
}
|
782
|
+
case GGML_OP_SET_ROWS:
|
783
|
+
{
|
784
|
+
if (op->src[0]->type != GGML_TYPE_F32) {
|
785
|
+
return false;
|
786
|
+
}
|
787
|
+
|
788
|
+
switch (op->type) {
|
789
|
+
case GGML_TYPE_F32:
|
790
|
+
case GGML_TYPE_F16:
|
791
|
+
case GGML_TYPE_BF16:
|
792
|
+
case GGML_TYPE_Q8_0:
|
793
|
+
case GGML_TYPE_Q4_0:
|
794
|
+
case GGML_TYPE_Q4_1:
|
795
|
+
case GGML_TYPE_Q5_0:
|
796
|
+
case GGML_TYPE_Q5_1:
|
797
|
+
case GGML_TYPE_IQ4_NL:
|
798
|
+
return true;
|
799
|
+
default:
|
800
|
+
return false;
|
801
|
+
};
|
802
|
+
}
|
803
|
+
default:
|
804
|
+
return false;
|
805
|
+
}
|
806
|
+
}
|
807
|
+
|
808
|
+
const struct ggml_metal_device_props * ggml_metal_device_get_props(ggml_metal_device_t dev) {
|
809
|
+
return &dev->props;
|
810
|
+
}
|
811
|
+
|
812
|
+
//
|
813
|
+
// device buffers
|
814
|
+
//
|
815
|
+
|
816
|
+
// max memory buffers that can be mapped to the device
|
817
|
+
#define GGML_METAL_MAX_BUFFERS 64
|
818
|
+
|
819
|
+
struct ggml_metal_buffer_wrapper {
|
820
|
+
void * data;
|
821
|
+
size_t size;
|
822
|
+
|
823
|
+
id<MTLBuffer> metal;
|
824
|
+
};
|
825
|
+
|
826
|
+
struct ggml_metal_buffer {
|
827
|
+
void * all_data; // TODO: https://github.com/ggml-org/llama.cpp/pull/15985
|
828
|
+
size_t all_size;
|
829
|
+
|
830
|
+
// if false, the Metal buffer data is allocated in private GPU memory and is not shared with the host
|
831
|
+
bool is_shared;
|
832
|
+
bool owned;
|
833
|
+
|
834
|
+
// multiple buffers are used only to avoid the maximum buffer size limitation when using mmap
|
835
|
+
int n_buffers;
|
836
|
+
struct ggml_metal_buffer_wrapper buffers[GGML_METAL_MAX_BUFFERS];
|
837
|
+
|
838
|
+
bool use_residency_sets;
|
839
|
+
|
840
|
+
// optional MTLResidencySet
|
841
|
+
// note: cannot use explicity "id<MTLResidencySet>" here because it is not available on certain OSes
|
842
|
+
id rset;
|
843
|
+
|
844
|
+
// pointers to global device objects
|
845
|
+
id<MTLDevice> device;
|
846
|
+
id<MTLCommandQueue> queue;
|
847
|
+
};
|
848
|
+
|
849
|
+
static void ggml_metal_log_allocated_size(id<MTLDevice> device, size_t size_aligned) {
|
850
|
+
#ifndef GGML_METAL_NDEBUG
|
851
|
+
#if TARGET_OS_OSX || (TARGET_OS_IOS && __clang_major__ >= 15)
|
852
|
+
if (@available(macOS 10.12, iOS 16.0, *)) {
|
853
|
+
GGML_LOG_DEBUG("%s: allocated buffer, size = %8.2f MiB, (%8.2f / %8.2f)\n",
|
854
|
+
__func__,
|
855
|
+
size_aligned / 1024.0 / 1024.0,
|
856
|
+
device.currentAllocatedSize / 1024.0 / 1024.0,
|
857
|
+
device.recommendedMaxWorkingSetSize / 1024.0 / 1024.0);
|
858
|
+
|
859
|
+
if (device.currentAllocatedSize > device.recommendedMaxWorkingSetSize) {
|
860
|
+
GGML_LOG_WARN("%s: warning: current allocated size is greater than the recommended max working set size\n", __func__);
|
861
|
+
}
|
862
|
+
} else {
|
863
|
+
GGML_LOG_INFO("%s: allocated buffer, size = %8.2f MiB, (%8.2f)\n",
|
864
|
+
__func__,
|
865
|
+
size_aligned / 1024.0 / 1024.0,
|
866
|
+
device.currentAllocatedSize / 1024.0 / 1024.0);
|
867
|
+
}
|
868
|
+
#endif
|
869
|
+
#endif
|
870
|
+
GGML_UNUSED(device);
|
871
|
+
GGML_UNUSED(size_aligned);
|
872
|
+
}
|
873
|
+
|
874
|
+
// rset init
|
875
|
+
static bool ggml_metal_buffer_rset_init(ggml_metal_buffer_t buf) {
|
876
|
+
buf->rset = nil;
|
877
|
+
|
878
|
+
if (!buf->use_residency_sets) {
|
879
|
+
return true;
|
880
|
+
}
|
881
|
+
|
882
|
+
#if defined(GGML_METAL_HAS_RESIDENCY_SETS)
|
883
|
+
if (@available(macOS 15.0, iOS 18.0, tvOS 18.0, visionOS 2.0, *)) {
|
884
|
+
MTLResidencySetDescriptor * desc = [[MTLResidencySetDescriptor alloc] init];
|
885
|
+
desc.label = @"ggml_metal";
|
886
|
+
desc.initialCapacity = buf->n_buffers;
|
887
|
+
|
888
|
+
NSError * error;
|
889
|
+
buf->rset = [buf->device newResidencySetWithDescriptor:desc error:&error];
|
890
|
+
if (error) {
|
891
|
+
GGML_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
|
892
|
+
[desc release];
|
893
|
+
return false;
|
894
|
+
}
|
895
|
+
|
896
|
+
[desc release];
|
897
|
+
|
898
|
+
for (int i = 0; i < buf->n_buffers; i++) {
|
899
|
+
[buf->rset addAllocation:buf->buffers[i].metal];
|
900
|
+
}
|
901
|
+
|
902
|
+
[buf->rset commit];
|
903
|
+
[buf->rset requestResidency];
|
904
|
+
|
905
|
+
return true;
|
906
|
+
}
|
907
|
+
#endif
|
908
|
+
|
909
|
+
return true;
|
910
|
+
}
|
911
|
+
|
912
|
+
// rset free
|
913
|
+
static void ggml_metal_buffer_rset_free(ggml_metal_buffer_t buf) {
|
914
|
+
#if defined(GGML_METAL_HAS_RESIDENCY_SETS)
|
915
|
+
if (@available(macOS 15.0, iOS 18.0, tvOS 18.0, visionOS 2.0, *)) {
|
916
|
+
if (buf->rset) {
|
917
|
+
[buf->rset endResidency];
|
918
|
+
[buf->rset removeAllAllocations];
|
919
|
+
[buf->rset release];
|
920
|
+
}
|
921
|
+
}
|
922
|
+
#else
|
923
|
+
GGML_UNUSED(buf);
|
924
|
+
#endif
|
925
|
+
}
|
926
|
+
|
927
|
+
static void * ggml_metal_host_malloc(size_t n) {
|
928
|
+
void * data = NULL;
|
929
|
+
|
930
|
+
#if TARGET_OS_OSX
|
931
|
+
kern_return_t err = vm_allocate((vm_map_t) mach_task_self(), (void *) &data, n, VM_FLAGS_ANYWHERE);
|
932
|
+
if (err != KERN_SUCCESS) {
|
933
|
+
GGML_LOG_ERROR("%s: error: vm_allocate failed\n", __func__);
|
934
|
+
return NULL;
|
935
|
+
}
|
936
|
+
#else
|
937
|
+
const int result = posix_memalign((void **) &data, sysconf(_SC_PAGESIZE), n);
|
938
|
+
if (result != 0) {
|
939
|
+
GGML_LOG_ERROR("%s: error: posix_memalign failed\n", __func__);
|
940
|
+
return NULL;
|
941
|
+
}
|
942
|
+
#endif
|
943
|
+
|
944
|
+
return data;
|
945
|
+
}
|
946
|
+
|
947
|
+
ggml_metal_buffer_t ggml_metal_buffer_init(ggml_metal_device_t dev, size_t size, bool shared) {
|
948
|
+
ggml_metal_buffer_t res = calloc(1, sizeof(struct ggml_metal_buffer));
|
949
|
+
|
950
|
+
const size_t size_page = sysconf(_SC_PAGESIZE);
|
951
|
+
|
952
|
+
size_t size_aligned = size;
|
953
|
+
if ((size_aligned % size_page) != 0) {
|
954
|
+
size_aligned += (size_page - (size_aligned % size_page));
|
955
|
+
}
|
956
|
+
|
957
|
+
const struct ggml_metal_device_props * props_dev = ggml_metal_device_get_props(dev);
|
958
|
+
|
959
|
+
shared = shared && props_dev->use_shared_buffers;
|
960
|
+
|
961
|
+
// allocate shared buffer if the device supports it and it is required by the buffer type
|
962
|
+
if (shared) {
|
963
|
+
res->all_data = ggml_metal_host_malloc(size_aligned);
|
964
|
+
res->is_shared = true;
|
965
|
+
res->owned = true;
|
966
|
+
} else {
|
967
|
+
// dummy, non-NULL value - we'll populate this after creating the Metal buffer below
|
968
|
+
res->all_data = (void *) 0x000000400ULL;
|
969
|
+
res->is_shared = false;
|
970
|
+
}
|
971
|
+
res->all_size = size_aligned;
|
972
|
+
|
973
|
+
res->device = ggml_metal_device_get_obj(dev);
|
974
|
+
res->queue = ggml_metal_device_get_queue(dev);
|
975
|
+
|
976
|
+
res->n_buffers = 1;
|
977
|
+
|
978
|
+
if (res->all_data != NULL) {
|
979
|
+
res->buffers[0].size = size;
|
980
|
+
res->buffers[0].metal = nil;
|
981
|
+
|
982
|
+
if (size_aligned > 0) {
|
983
|
+
if (props_dev->use_shared_buffers &&shared) {
|
984
|
+
res->buffers[0].metal = [res->device newBufferWithBytesNoCopy:res->all_data
|
985
|
+
length:size_aligned
|
986
|
+
options:MTLResourceStorageModeShared
|
987
|
+
deallocator:nil];
|
988
|
+
} else {
|
989
|
+
res->buffers[0].metal = [res->device newBufferWithLength:size_aligned options:MTLResourceStorageModePrivate];
|
990
|
+
|
991
|
+
res->all_data = (void *) (res->buffers[0].metal.gpuAddress);
|
992
|
+
}
|
993
|
+
}
|
994
|
+
|
995
|
+
res->buffers[0].data = res->all_data;
|
996
|
+
}
|
997
|
+
|
998
|
+
if (size_aligned > 0 && (res->all_data == NULL || res->buffers[0].metal == nil)) {
|
999
|
+
GGML_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_aligned / 1024.0 / 1024.0);
|
1000
|
+
free(res);
|
1001
|
+
return NULL;
|
1002
|
+
}
|
1003
|
+
|
1004
|
+
res->use_residency_sets = props_dev->use_residency_sets;
|
1005
|
+
|
1006
|
+
if (!ggml_metal_buffer_rset_init(res)) {
|
1007
|
+
GGML_LOG_ERROR("%s: error: failed to initialize residency set\n", __func__);
|
1008
|
+
free(res);
|
1009
|
+
return NULL;
|
1010
|
+
}
|
1011
|
+
|
1012
|
+
//ggml_metal_log_allocated_size(device, size_aligned);
|
1013
|
+
|
1014
|
+
return res;
|
1015
|
+
}
|
1016
|
+
|
1017
|
+
ggml_metal_buffer_t ggml_metal_buffer_map(ggml_metal_device_t dev, void * ptr, size_t size, size_t max_tensor_size) {
|
1018
|
+
ggml_metal_buffer_t res = calloc(1, sizeof(struct ggml_metal_buffer));
|
1019
|
+
|
1020
|
+
res->all_data = ptr;
|
1021
|
+
res->all_size = size;
|
1022
|
+
|
1023
|
+
res->is_shared = true;
|
1024
|
+
res->owned = false;
|
1025
|
+
|
1026
|
+
res->n_buffers = 0;
|
1027
|
+
|
1028
|
+
const size_t size_page = sysconf(_SC_PAGESIZE);
|
1029
|
+
|
1030
|
+
// page-align the data ptr
|
1031
|
+
{
|
1032
|
+
const uintptr_t offs = (uintptr_t) ptr % size_page;
|
1033
|
+
ptr = (void *) ((char *) ptr - offs);
|
1034
|
+
size += offs;
|
1035
|
+
}
|
1036
|
+
|
1037
|
+
size_t size_aligned = size;
|
1038
|
+
if ((size_aligned % size_page) != 0) {
|
1039
|
+
size_aligned += (size_page - (size_aligned % size_page));
|
1040
|
+
}
|
1041
|
+
|
1042
|
+
res->device = ggml_metal_device_get_obj(dev);
|
1043
|
+
res->queue = ggml_metal_device_get_queue(dev);
|
1044
|
+
|
1045
|
+
const struct ggml_metal_device_props * props_dev = ggml_metal_device_get_props(dev);
|
1046
|
+
|
1047
|
+
// the buffer fits into the max buffer size allowed by the device
|
1048
|
+
if (size_aligned <= props_dev->max_buffer_size) {
|
1049
|
+
res->buffers[res->n_buffers].data = ptr;
|
1050
|
+
res->buffers[res->n_buffers].size = size;
|
1051
|
+
res->buffers[res->n_buffers].metal = nil;
|
1052
|
+
|
1053
|
+
if (size_aligned > 0) {
|
1054
|
+
res->buffers[res->n_buffers].metal = [res->device newBufferWithBytesNoCopy:ptr length:size_aligned options:MTLResourceStorageModeShared deallocator:nil];
|
1055
|
+
|
1056
|
+
if (res->buffers[res->n_buffers].metal == nil) {
|
1057
|
+
GGML_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_aligned / 1024.0 / 1024.0);
|
1058
|
+
free(res);
|
1059
|
+
return NULL;
|
1060
|
+
}
|
1061
|
+
}
|
1062
|
+
|
1063
|
+
ggml_metal_log_allocated_size(res->device, size_aligned);
|
1064
|
+
|
1065
|
+
++res->n_buffers;
|
1066
|
+
} else {
|
1067
|
+
// this overlap between the views will guarantee that the tensor with the maximum size will fully fit into
|
1068
|
+
// one of the views
|
1069
|
+
const size_t size_ovlp = ((max_tensor_size + size_page - 1) / size_page + 1) * size_page; // round-up 2 pages just in case
|
1070
|
+
const size_t size_step = props_dev->max_buffer_size - size_ovlp;
|
1071
|
+
const size_t size_view = props_dev->max_buffer_size;
|
1072
|
+
|
1073
|
+
for (size_t i = 0; i < size; i += size_step) {
|
1074
|
+
const size_t size_step_aligned = (i + size_view <= size) ? size_view : (size_aligned - i);
|
1075
|
+
|
1076
|
+
res->buffers[res->n_buffers].data = (void *) ((uint8_t *) ptr + i);
|
1077
|
+
res->buffers[res->n_buffers].size = size_step_aligned;
|
1078
|
+
res->buffers[res->n_buffers].metal = nil;
|
1079
|
+
|
1080
|
+
if (size_step_aligned > 0) {
|
1081
|
+
res->buffers[res->n_buffers].metal = [res->device newBufferWithBytesNoCopy:(void *) ((uint8_t *) ptr + i) length:size_step_aligned options:MTLResourceStorageModeShared deallocator:nil];
|
1082
|
+
|
1083
|
+
if (res->buffers[res->n_buffers].metal == nil) {
|
1084
|
+
GGML_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_step_aligned / 1024.0 / 1024.0);
|
1085
|
+
free(res);
|
1086
|
+
return NULL;
|
1087
|
+
}
|
1088
|
+
}
|
1089
|
+
|
1090
|
+
ggml_metal_log_allocated_size(res->device, size_step_aligned);
|
1091
|
+
|
1092
|
+
if (i + size_step < size) {
|
1093
|
+
GGML_LOG_INFO("\n");
|
1094
|
+
}
|
1095
|
+
|
1096
|
+
++res->n_buffers;
|
1097
|
+
}
|
1098
|
+
}
|
1099
|
+
|
1100
|
+
res->use_residency_sets = props_dev->use_residency_sets;
|
1101
|
+
|
1102
|
+
if (!ggml_metal_buffer_rset_init(res)) {
|
1103
|
+
GGML_LOG_ERROR("%s: error: failed to initialize residency set\n", __func__);
|
1104
|
+
free(res);
|
1105
|
+
return NULL;
|
1106
|
+
}
|
1107
|
+
|
1108
|
+
return res;
|
1109
|
+
}
|
1110
|
+
|
1111
|
+
void ggml_metal_buffer_free(ggml_metal_buffer_t buf) {
|
1112
|
+
for (int i = 0; i < buf->n_buffers; i++) {
|
1113
|
+
[buf->buffers[i].metal release];
|
1114
|
+
}
|
1115
|
+
|
1116
|
+
ggml_metal_buffer_rset_free(buf);
|
1117
|
+
|
1118
|
+
if (buf->is_shared && buf->owned) {
|
1119
|
+
#if TARGET_OS_OSX
|
1120
|
+
vm_deallocate((vm_map_t)mach_task_self(), (vm_address_t)buf->all_data, buf->all_size);
|
1121
|
+
#else
|
1122
|
+
free(buf->all_data);
|
1123
|
+
#endif
|
1124
|
+
}
|
1125
|
+
|
1126
|
+
free(buf);
|
1127
|
+
}
|
1128
|
+
|
1129
|
+
void * ggml_metal_buffer_get_base(ggml_metal_buffer_t buf) {
|
1130
|
+
return buf->all_data;
|
1131
|
+
}
|
1132
|
+
|
1133
|
+
bool ggml_metal_buffer_is_shared(ggml_metal_buffer_t buf) {
|
1134
|
+
return buf->is_shared;
|
1135
|
+
}
|
1136
|
+
|
1137
|
+
void ggml_metal_buffer_memset_tensor(ggml_metal_buffer_t buf, struct ggml_tensor * tensor, uint8_t value, size_t offset, size_t size) {
|
1138
|
+
if (buf->is_shared) {
|
1139
|
+
memset((char *)tensor->data + offset, value, size);
|
1140
|
+
return;
|
1141
|
+
}
|
1142
|
+
|
1143
|
+
@autoreleasepool {
|
1144
|
+
// dst
|
1145
|
+
struct ggml_metal_buffer_id bid_dst = ggml_metal_buffer_get_id(buf, tensor);
|
1146
|
+
bid_dst.offs += offset;
|
1147
|
+
|
1148
|
+
id<MTLCommandQueue> queue = buf->queue;
|
1149
|
+
id<MTLCommandBuffer> cmd_buf = [queue commandBufferWithUnretainedReferences];
|
1150
|
+
|
1151
|
+
{
|
1152
|
+
id<MTLBlitCommandEncoder> encoder = [cmd_buf blitCommandEncoder];
|
1153
|
+
|
1154
|
+
[encoder fillBuffer:bid_dst.metal
|
1155
|
+
range:NSMakeRange(bid_dst.offs, bid_dst.offs + size)
|
1156
|
+
value:value];
|
1157
|
+
|
1158
|
+
[encoder endEncoding];
|
1159
|
+
}
|
1160
|
+
|
1161
|
+
[cmd_buf commit];
|
1162
|
+
[cmd_buf waitUntilCompleted];
|
1163
|
+
}
|
1164
|
+
}
|
1165
|
+
|
1166
|
+
void ggml_metal_buffer_set_tensor(ggml_metal_buffer_t buf, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
|
1167
|
+
if (buf->is_shared) {
|
1168
|
+
memcpy((char *)tensor->data + offset, data, size);
|
1169
|
+
return;
|
1170
|
+
}
|
1171
|
+
|
1172
|
+
@autoreleasepool {
|
1173
|
+
// src
|
1174
|
+
void * data_ptr = (void *)(uintptr_t) data; // "const cast" the src data
|
1175
|
+
id<MTLBuffer> buf_src = [buf->device newBufferWithBytesNoCopy:data_ptr
|
1176
|
+
length:size
|
1177
|
+
options:MTLResourceStorageModeShared
|
1178
|
+
deallocator:nil];
|
1179
|
+
|
1180
|
+
GGML_ASSERT(buf_src);
|
1181
|
+
|
1182
|
+
// dst
|
1183
|
+
struct ggml_metal_buffer_id bid_dst = ggml_metal_buffer_get_id(buf, tensor);
|
1184
|
+
bid_dst.offs += offset;
|
1185
|
+
|
1186
|
+
// note: for experimentation purposes, here we use a semaphore to wait for the copy to complete
|
1187
|
+
// this is alternative to waitUntilCompleted, which should be faster, but don't seem to make much difference
|
1188
|
+
dispatch_semaphore_t completion_semaphore = dispatch_semaphore_create(0);
|
1189
|
+
|
1190
|
+
id<MTLCommandQueue> queue = buf->queue;
|
1191
|
+
id<MTLCommandBuffer> cmd_buf = [queue commandBufferWithUnretainedReferences];
|
1192
|
+
|
1193
|
+
{
|
1194
|
+
id<MTLBlitCommandEncoder> encoder = [cmd_buf blitCommandEncoder];
|
1195
|
+
|
1196
|
+
[encoder copyFromBuffer:buf_src
|
1197
|
+
sourceOffset:0
|
1198
|
+
toBuffer:bid_dst.metal
|
1199
|
+
destinationOffset:bid_dst.offs
|
1200
|
+
size:size];
|
1201
|
+
|
1202
|
+
[encoder endEncoding];
|
1203
|
+
}
|
1204
|
+
|
1205
|
+
[cmd_buf addCompletedHandler:^(id<MTLCommandBuffer> cb) {
|
1206
|
+
// TODO: can check for errors here
|
1207
|
+
GGML_UNUSED(cb);
|
1208
|
+
|
1209
|
+
dispatch_semaphore_signal(completion_semaphore);
|
1210
|
+
}];
|
1211
|
+
|
1212
|
+
[cmd_buf commit];
|
1213
|
+
|
1214
|
+
dispatch_semaphore_wait(completion_semaphore, DISPATCH_TIME_FOREVER);
|
1215
|
+
dispatch_release(completion_semaphore);
|
1216
|
+
|
1217
|
+
//[cmd_buf waitUntilCompleted];
|
1218
|
+
}
|
1219
|
+
}
|
1220
|
+
|
1221
|
+
void ggml_metal_buffer_get_tensor(ggml_metal_buffer_t buf, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size) {
|
1222
|
+
if (buf->is_shared) {
|
1223
|
+
memcpy(data, (const char *)tensor->data + offset, size);
|
1224
|
+
return;
|
1225
|
+
}
|
1226
|
+
|
1227
|
+
@autoreleasepool {
|
1228
|
+
// src
|
1229
|
+
struct ggml_metal_buffer_id bid_src = ggml_metal_buffer_get_id(buf, tensor);
|
1230
|
+
bid_src.offs += offset;
|
1231
|
+
|
1232
|
+
// dst
|
1233
|
+
id<MTLBuffer> buf_dst = [buf->device newBufferWithBytesNoCopy:data
|
1234
|
+
length:size
|
1235
|
+
options:MTLResourceStorageModeShared
|
1236
|
+
deallocator:nil];
|
1237
|
+
|
1238
|
+
GGML_ASSERT(buf_dst);
|
1239
|
+
|
1240
|
+
id<MTLCommandQueue> queue = buf->queue;
|
1241
|
+
id<MTLCommandBuffer> cmd_buf = [queue commandBufferWithUnretainedReferences];
|
1242
|
+
|
1243
|
+
{
|
1244
|
+
id<MTLBlitCommandEncoder> encoder = [cmd_buf blitCommandEncoder];
|
1245
|
+
|
1246
|
+
[encoder copyFromBuffer:bid_src.metal
|
1247
|
+
sourceOffset:bid_src.offs
|
1248
|
+
toBuffer:buf_dst
|
1249
|
+
destinationOffset:0
|
1250
|
+
size:size];
|
1251
|
+
|
1252
|
+
[encoder endEncoding];
|
1253
|
+
}
|
1254
|
+
|
1255
|
+
[cmd_buf commit];
|
1256
|
+
[cmd_buf waitUntilCompleted];
|
1257
|
+
}
|
1258
|
+
}
|
1259
|
+
|
1260
|
+
void ggml_metal_buffer_clear(ggml_metal_buffer_t buf, uint8_t value) {
|
1261
|
+
if (buf->is_shared) {
|
1262
|
+
memset(buf->all_data, value, buf->all_size);
|
1263
|
+
return;
|
1264
|
+
}
|
1265
|
+
|
1266
|
+
@autoreleasepool {
|
1267
|
+
id<MTLCommandQueue> queue = buf->queue;
|
1268
|
+
id<MTLCommandBuffer> cmd_buf = [queue commandBufferWithUnretainedReferences];
|
1269
|
+
|
1270
|
+
{
|
1271
|
+
id<MTLBlitCommandEncoder> encoder = [cmd_buf blitCommandEncoder];
|
1272
|
+
|
1273
|
+
[encoder fillBuffer:buf->buffers[0].metal
|
1274
|
+
range:NSMakeRange(0, buf->buffers[0].size)
|
1275
|
+
value:value];
|
1276
|
+
|
1277
|
+
[encoder endEncoding];
|
1278
|
+
}
|
1279
|
+
|
1280
|
+
[cmd_buf commit];
|
1281
|
+
[cmd_buf waitUntilCompleted];
|
1282
|
+
}
|
1283
|
+
}
|
1284
|
+
|
1285
|
+
struct ggml_metal_buffer_id ggml_metal_buffer_get_id(ggml_metal_buffer_t buf, const struct ggml_tensor * t) {
|
1286
|
+
struct ggml_metal_buffer_id res = { nil, 0 };
|
1287
|
+
|
1288
|
+
const int64_t tsize = ggml_nbytes(t);
|
1289
|
+
|
1290
|
+
// find the view that contains the tensor fully
|
1291
|
+
for (int i = 0; i < buf->n_buffers; ++i) {
|
1292
|
+
const int64_t ioffs = (int64_t) t->data - (int64_t) buf->buffers[i].data;
|
1293
|
+
|
1294
|
+
//GGML_LOG_INFO("ioffs = %10ld, tsize = %10ld, sum = %10ld, buf->buffers[%d].size = %10ld\n", ioffs, tsize, ioffs + tsize, i, buf->buffers[i].size);
|
1295
|
+
if (ioffs >= 0 && ioffs + tsize <= (int64_t) buf->buffers[i].size) {
|
1296
|
+
res.metal = buf->buffers[i].metal;
|
1297
|
+
res.offs = (size_t) ioffs;
|
1298
|
+
|
1299
|
+
//GGML_LOG_INFO("%s: tensor '%16s', offs = %8ld\n", __func__, t->name, *offs);
|
1300
|
+
|
1301
|
+
return res;
|
1302
|
+
}
|
1303
|
+
}
|
1304
|
+
|
1305
|
+
GGML_LOG_ERROR("%s: error: tensor '%s' buffer is nil\n", __func__, t->name);
|
1306
|
+
|
1307
|
+
return res;
|
1308
|
+
}
|