local-llm-rn 1.0.0
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.
- package/cpp/CMakeLists.txt +285 -0
- package/cpp/common/CMakeLists.txt +149 -0
- package/cpp/common/arg.cpp +3799 -0
- package/cpp/common/arg.h +131 -0
- package/cpp/common/base64.hpp +392 -0
- package/cpp/common/build-info.cpp.in +4 -0
- package/cpp/common/chat-parser-xml-toolcall.cpp +879 -0
- package/cpp/common/chat-parser-xml-toolcall.h +45 -0
- package/cpp/common/chat-parser.cpp +1649 -0
- package/cpp/common/chat-parser.h +133 -0
- package/cpp/common/chat-peg-parser.cpp +124 -0
- package/cpp/common/chat-peg-parser.h +105 -0
- package/cpp/common/chat.cpp +3355 -0
- package/cpp/common/chat.h +252 -0
- package/cpp/common/common.cpp +1824 -0
- package/cpp/common/common.h +930 -0
- package/cpp/common/console.cpp +1137 -0
- package/cpp/common/console.h +41 -0
- package/cpp/common/debug.cpp +167 -0
- package/cpp/common/debug.h +43 -0
- package/cpp/common/download.cpp +792 -0
- package/cpp/common/download.h +84 -0
- package/cpp/common/http.h +84 -0
- package/cpp/common/jinja/README.md +88 -0
- package/cpp/common/jinja/caps.cpp +285 -0
- package/cpp/common/jinja/caps.h +30 -0
- package/cpp/common/jinja/lexer.cpp +341 -0
- package/cpp/common/jinja/lexer.h +157 -0
- package/cpp/common/jinja/parser.cpp +591 -0
- package/cpp/common/jinja/parser.h +21 -0
- package/cpp/common/jinja/runtime.cpp +867 -0
- package/cpp/common/jinja/runtime.h +638 -0
- package/cpp/common/jinja/string.cpp +213 -0
- package/cpp/common/jinja/string.h +61 -0
- package/cpp/common/jinja/utils.h +149 -0
- package/cpp/common/jinja/value.cpp +1393 -0
- package/cpp/common/jinja/value.h +756 -0
- package/cpp/common/json-partial.cpp +324 -0
- package/cpp/common/json-partial.h +39 -0
- package/cpp/common/json-schema-to-grammar.cpp +1153 -0
- package/cpp/common/json-schema-to-grammar.h +43 -0
- package/cpp/common/llguidance.cpp +258 -0
- package/cpp/common/log.cpp +446 -0
- package/cpp/common/log.h +119 -0
- package/cpp/common/ngram-cache.cpp +285 -0
- package/cpp/common/ngram-cache.h +101 -0
- package/cpp/common/ngram-map.cpp +530 -0
- package/cpp/common/ngram-map.h +115 -0
- package/cpp/common/ngram-mod.cpp +60 -0
- package/cpp/common/ngram-mod.h +38 -0
- package/cpp/common/peg-parser.cpp +1712 -0
- package/cpp/common/peg-parser.h +459 -0
- package/cpp/common/preset.cpp +483 -0
- package/cpp/common/preset.h +83 -0
- package/cpp/common/regex-partial.cpp +204 -0
- package/cpp/common/regex-partial.h +56 -0
- package/cpp/common/sampling.cpp +745 -0
- package/cpp/common/sampling.h +119 -0
- package/cpp/common/speculative.cpp +1074 -0
- package/cpp/common/speculative.h +41 -0
- package/cpp/common/unicode.cpp +64 -0
- package/cpp/common/unicode.h +22 -0
- package/cpp/ggml/CMakeLists.txt +494 -0
- package/cpp/ggml/cmake/GitVars.cmake +22 -0
- package/cpp/ggml/cmake/common.cmake +50 -0
- package/cpp/ggml/cmake/ggml-config.cmake.in +191 -0
- package/cpp/ggml/include/ggml-alloc.h +85 -0
- package/cpp/ggml/include/ggml-backend.h +373 -0
- package/cpp/ggml/include/ggml-blas.h +25 -0
- package/cpp/ggml/include/ggml-cann.h +123 -0
- package/cpp/ggml/include/ggml-cpp.h +39 -0
- package/cpp/ggml/include/ggml-cpu.h +151 -0
- package/cpp/ggml/include/ggml-cuda.h +47 -0
- package/cpp/ggml/include/ggml-hexagon.h +19 -0
- package/cpp/ggml/include/ggml-metal.h +61 -0
- package/cpp/ggml/include/ggml-opencl.h +26 -0
- package/cpp/ggml/include/ggml-opt.h +256 -0
- package/cpp/ggml/include/ggml-rpc.h +30 -0
- package/cpp/ggml/include/ggml-sycl.h +49 -0
- package/cpp/ggml/include/ggml-virtgpu.h +14 -0
- package/cpp/ggml/include/ggml-vulkan.h +29 -0
- package/cpp/ggml/include/ggml-webgpu.h +19 -0
- package/cpp/ggml/include/ggml-zdnn.h +17 -0
- package/cpp/ggml/include/ggml-zendnn.h +22 -0
- package/cpp/ggml/include/ggml.h +2753 -0
- package/cpp/ggml/include/gguf.h +204 -0
- package/cpp/ggml/src/CMakeLists.txt +492 -0
- package/cpp/ggml/src/ggml-alloc.c +1244 -0
- package/cpp/ggml/src/ggml-backend-dl.cpp +48 -0
- package/cpp/ggml/src/ggml-backend-dl.h +45 -0
- package/cpp/ggml/src/ggml-backend-impl.h +255 -0
- package/cpp/ggml/src/ggml-backend-reg.cpp +566 -0
- package/cpp/ggml/src/ggml-backend.cpp +2270 -0
- package/cpp/ggml/src/ggml-blas/CMakeLists.txt +101 -0
- package/cpp/ggml/src/ggml-blas/ggml-blas.cpp +518 -0
- package/cpp/ggml/src/ggml-common.h +1878 -0
- package/cpp/ggml/src/ggml-cpu/CMakeLists.txt +691 -0
- package/cpp/ggml/src/ggml-cpu/amx/amx.cpp +247 -0
- package/cpp/ggml/src/ggml-cpu/amx/amx.h +8 -0
- package/cpp/ggml/src/ggml-cpu/amx/common.h +91 -0
- package/cpp/ggml/src/ggml-cpu/amx/mmq.cpp +2512 -0
- package/cpp/ggml/src/ggml-cpu/amx/mmq.h +10 -0
- package/cpp/ggml/src/ggml-cpu/arch/arm/cpu-feats.cpp +98 -0
- package/cpp/ggml/src/ggml-cpu/arch/arm/quants.c +4052 -0
- package/cpp/ggml/src/ggml-cpu/arch/arm/repack.cpp +4935 -0
- package/cpp/ggml/src/ggml-cpu/arch/loongarch/quants.c +2159 -0
- package/cpp/ggml/src/ggml-cpu/arch/powerpc/cpu-feats.cpp +82 -0
- package/cpp/ggml/src/ggml-cpu/arch/powerpc/quants.c +2305 -0
- package/cpp/ggml/src/ggml-cpu/arch/riscv/cpu-feats.cpp +38 -0
- package/cpp/ggml/src/ggml-cpu/arch/riscv/quants.c +2726 -0
- package/cpp/ggml/src/ggml-cpu/arch/riscv/repack.cpp +342 -0
- package/cpp/ggml/src/ggml-cpu/arch/s390/cpu-feats.cpp +50 -0
- package/cpp/ggml/src/ggml-cpu/arch/s390/quants.c +1468 -0
- package/cpp/ggml/src/ggml-cpu/arch/wasm/quants.c +1221 -0
- package/cpp/ggml/src/ggml-cpu/arch/x86/cpu-feats.cpp +327 -0
- package/cpp/ggml/src/ggml-cpu/arch/x86/quants.c +3820 -0
- package/cpp/ggml/src/ggml-cpu/arch/x86/repack.cpp +6307 -0
- package/cpp/ggml/src/ggml-cpu/arch-fallback.h +313 -0
- package/cpp/ggml/src/ggml-cpu/binary-ops.cpp +154 -0
- package/cpp/ggml/src/ggml-cpu/binary-ops.h +16 -0
- package/cpp/ggml/src/ggml-cpu/cmake/FindSIMD.cmake +100 -0
- package/cpp/ggml/src/ggml-cpu/common.h +95 -0
- package/cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h +529 -0
- package/cpp/ggml/src/ggml-cpu/ggml-cpu.c +3734 -0
- package/cpp/ggml/src/ggml-cpu/ggml-cpu.cpp +701 -0
- package/cpp/ggml/src/ggml-cpu/hbm.cpp +55 -0
- package/cpp/ggml/src/ggml-cpu/hbm.h +8 -0
- package/cpp/ggml/src/ggml-cpu/kleidiai/kernels.cpp +938 -0
- package/cpp/ggml/src/ggml-cpu/kleidiai/kernels.h +90 -0
- package/cpp/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +798 -0
- package/cpp/ggml/src/ggml-cpu/kleidiai/kleidiai.h +17 -0
- package/cpp/ggml/src/ggml-cpu/llamafile/sgemm.cpp +4033 -0
- package/cpp/ggml/src/ggml-cpu/llamafile/sgemm.h +25 -0
- package/cpp/ggml/src/ggml-cpu/ops.cpp +10978 -0
- package/cpp/ggml/src/ggml-cpu/ops.h +116 -0
- package/cpp/ggml/src/ggml-cpu/quants.c +1193 -0
- package/cpp/ggml/src/ggml-cpu/quants.h +97 -0
- package/cpp/ggml/src/ggml-cpu/repack.cpp +3316 -0
- package/cpp/ggml/src/ggml-cpu/repack.h +173 -0
- package/cpp/ggml/src/ggml-cpu/simd-gemm.h +136 -0
- package/cpp/ggml/src/ggml-cpu/simd-mappings.h +1279 -0
- package/cpp/ggml/src/ggml-cpu/spacemit/ime.cpp +1025 -0
- package/cpp/ggml/src/ggml-cpu/spacemit/ime.h +13 -0
- package/cpp/ggml/src/ggml-cpu/spacemit/ime1_kernels.cpp +3196 -0
- package/cpp/ggml/src/ggml-cpu/spacemit/ime_kernels.h +26 -0
- package/cpp/ggml/src/ggml-cpu/traits.cpp +36 -0
- package/cpp/ggml/src/ggml-cpu/traits.h +38 -0
- package/cpp/ggml/src/ggml-cpu/unary-ops.cpp +337 -0
- package/cpp/ggml/src/ggml-cpu/unary-ops.h +35 -0
- package/cpp/ggml/src/ggml-cpu/vec.cpp +629 -0
- package/cpp/ggml/src/ggml-cpu/vec.h +1585 -0
- package/cpp/ggml/src/ggml-hexagon/CMakeLists.txt +117 -0
- package/cpp/ggml/src/ggml-hexagon/ggml-hexagon.cpp +3232 -0
- package/cpp/ggml/src/ggml-hexagon/htp/CMakeLists.txt +45 -0
- package/cpp/ggml/src/ggml-hexagon/htp/act-ops.c +815 -0
- package/cpp/ggml/src/ggml-hexagon/htp/argsort-ops.c +281 -0
- package/cpp/ggml/src/ggml-hexagon/htp/binary-ops.c +827 -0
- package/cpp/ggml/src/ggml-hexagon/htp/cmake-toolchain.cmake +157 -0
- package/cpp/ggml/src/ggml-hexagon/htp/cpy-ops.c +251 -0
- package/cpp/ggml/src/ggml-hexagon/htp/flash-attn-ops.c +666 -0
- package/cpp/ggml/src/ggml-hexagon/htp/get-rows-ops.c +111 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hex-dma.c +63 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hex-dma.h +182 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hex-dump.h +77 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hex-fastdiv.h +37 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hex-utils.h +51 -0
- package/cpp/ggml/src/ggml-hexagon/htp/htp-ctx.h +35 -0
- package/cpp/ggml/src/ggml-hexagon/htp/htp-msg.h +154 -0
- package/cpp/ggml/src/ggml-hexagon/htp/htp-ops.h +65 -0
- package/cpp/ggml/src/ggml-hexagon/htp/htp_iface.idl +16 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-arith.h +470 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-base.h +173 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-copy.h +245 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-div.h +116 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-dump.h +129 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-exp.h +215 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-floor.h +100 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-inverse.h +176 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-reduce.h +266 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-scale.h +133 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-sigmoid.h +141 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-sqrt.h +126 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-types.h +36 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-utils.h +18 -0
- package/cpp/ggml/src/ggml-hexagon/htp/main.c +1150 -0
- package/cpp/ggml/src/ggml-hexagon/htp/matmul-ops.c +2595 -0
- package/cpp/ggml/src/ggml-hexagon/htp/rope-ops.c +498 -0
- package/cpp/ggml/src/ggml-hexagon/htp/set-rows-ops.c +167 -0
- package/cpp/ggml/src/ggml-hexagon/htp/softmax-ops.c +421 -0
- package/cpp/ggml/src/ggml-hexagon/htp/sum-rows-ops.c +130 -0
- package/cpp/ggml/src/ggml-hexagon/htp/unary-ops.c +384 -0
- package/cpp/ggml/src/ggml-hexagon/htp/worker-pool.c +293 -0
- package/cpp/ggml/src/ggml-hexagon/htp/worker-pool.h +57 -0
- package/cpp/ggml/src/ggml-hexagon/htp-drv.cpp +418 -0
- package/cpp/ggml/src/ggml-hexagon/htp-drv.h +121 -0
- package/cpp/ggml/src/ggml-hexagon/libdl.h +79 -0
- package/cpp/ggml/src/ggml-hexagon/libggml-htp.inf +38 -0
- package/cpp/ggml/src/ggml-hexagon/op-desc.h +153 -0
- package/cpp/ggml/src/ggml-impl.h +724 -0
- package/cpp/ggml/src/ggml-metal/CMakeLists.txt +124 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal-common.cpp +457 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal-common.h +52 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal-context.h +41 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal-context.m +702 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal-device.cpp +1890 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal-device.h +290 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal-device.m +1749 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal-impl.h +1054 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal-ops.cpp +4370 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal-ops.h +94 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal.cpp +937 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal.metal +9819 -0
- package/cpp/ggml/src/ggml-musa/CMakeLists.txt +125 -0
- package/cpp/ggml/src/ggml-musa/mudnn.cu +112 -0
- package/cpp/ggml/src/ggml-musa/mudnn.cuh +12 -0
- package/cpp/ggml/src/ggml-opencl/CMakeLists.txt +150 -0
- package/cpp/ggml/src/ggml-opencl/ggml-opencl.cpp +11553 -0
- package/cpp/ggml/src/ggml-opencl/kernels/add.cl +190 -0
- package/cpp/ggml/src/ggml-opencl/kernels/add_id.cl +42 -0
- package/cpp/ggml/src/ggml-opencl/kernels/argsort.cl +86 -0
- package/cpp/ggml/src/ggml-opencl/kernels/clamp.cl +20 -0
- package/cpp/ggml/src/ggml-opencl/kernels/concat.cl +51 -0
- package/cpp/ggml/src/ggml-opencl/kernels/conv2d.cl +185 -0
- package/cpp/ggml/src/ggml-opencl/kernels/conv2d_f16_f32.cl +176 -0
- package/cpp/ggml/src/ggml-opencl/kernels/cpy.cl +184 -0
- package/cpp/ggml/src/ggml-opencl/kernels/cvt.cl +417 -0
- package/cpp/ggml/src/ggml-opencl/kernels/diag_mask_inf.cl +58 -0
- package/cpp/ggml/src/ggml-opencl/kernels/div.cl +138 -0
- package/cpp/ggml/src/ggml-opencl/kernels/embed_kernel.py +26 -0
- package/cpp/ggml/src/ggml-opencl/kernels/expm1.cl +113 -0
- package/cpp/ggml/src/ggml-opencl/kernels/fill.cl +17 -0
- package/cpp/ggml/src/ggml-opencl/kernels/flash_attn_f16.cl +370 -0
- package/cpp/ggml/src/ggml-opencl/kernels/flash_attn_f32.cl +371 -0
- package/cpp/ggml/src/ggml-opencl/kernels/flash_attn_f32_f16.cl +373 -0
- package/cpp/ggml/src/ggml-opencl/kernels/gelu.cl +89 -0
- package/cpp/ggml/src/ggml-opencl/kernels/gemm_moe_mxfp4_f32.cl +162 -0
- package/cpp/ggml/src/ggml-opencl/kernels/gemv_moe_mxfp4_f32.cl +156 -0
- package/cpp/ggml/src/ggml-opencl/kernels/gemv_noshuffle.cl +268 -0
- package/cpp/ggml/src/ggml-opencl/kernels/gemv_noshuffle_general.cl +274 -0
- package/cpp/ggml/src/ggml-opencl/kernels/gemv_noshuffle_general_q8_0_f32.cl +195 -0
- package/cpp/ggml/src/ggml-opencl/kernels/get_rows.cl +187 -0
- package/cpp/ggml/src/ggml-opencl/kernels/glu.cl +378 -0
- package/cpp/ggml/src/ggml-opencl/kernels/group_norm.cl +121 -0
- package/cpp/ggml/src/ggml-opencl/kernels/im2col_f16.cl +57 -0
- package/cpp/ggml/src/ggml-opencl/kernels/im2col_f32.cl +57 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mean.cl +140 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul.cl +152 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mat_Ab_Bi_8x4.cl +139 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mat_f16_f32.cl +130 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mm_f16_f32_kq_kqv.cl +273 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mm_f16_f32_l4_lm.cl +146 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mm_f32_f32_l4_lm.cl +147 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mm_q4_0_f32_l4_lm.cl +163 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mm_q4_1_f32_l4_lm.cl +165 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mm_q6_k_f32_l4_lm.cl +158 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mm_q8_0_f32_8x4.cl +129 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mm_q8_0_f32_l4_lm.cl +154 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_f16_f16.cl +118 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_f16_f32.cl +118 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_f16_f32_1row.cl +94 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_f16_f32_l4.cl +84 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_f32_f32.cl +118 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_id_mxfp4_f32.cl +189 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_id_mxfp4_f32_flat.cl +176 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_id_q4_0_f32_8x_flat.cl +283 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_id_q8_0_f32.cl +140 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_id_q8_0_f32_flat.cl +222 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_mxfp4_f32.cl +144 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_mxfp4_f32_flat.cl +167 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32.cl +192 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32_1d_16x_flat.cl +307 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32_1d_8x_flat.cl +265 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32_8x_flat.cl +272 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32_v.cl +254 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q4_1_f32.cl +219 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q4_1_f32_flat.cl +229 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q4_k_f32.cl +180 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q6_k_f32.cl +194 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q6_k_f32_flat.cl +194 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q8_0_f32.cl +125 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q8_0_f32_flat.cl +202 -0
- package/cpp/ggml/src/ggml-opencl/kernels/norm.cl +161 -0
- package/cpp/ggml/src/ggml-opencl/kernels/pad.cl +39 -0
- package/cpp/ggml/src/ggml-opencl/kernels/relu.cl +16 -0
- package/cpp/ggml/src/ggml-opencl/kernels/repeat.cl +38 -0
- package/cpp/ggml/src/ggml-opencl/kernels/rms_norm.cl +190 -0
- package/cpp/ggml/src/ggml-opencl/kernels/rope.cl +747 -0
- package/cpp/ggml/src/ggml-opencl/kernels/scale.cl +27 -0
- package/cpp/ggml/src/ggml-opencl/kernels/set_rows.cl +208 -0
- package/cpp/ggml/src/ggml-opencl/kernels/sigmoid.cl +29 -0
- package/cpp/ggml/src/ggml-opencl/kernels/silu.cl +30 -0
- package/cpp/ggml/src/ggml-opencl/kernels/softmax_4_f16.cl +108 -0
- package/cpp/ggml/src/ggml-opencl/kernels/softmax_4_f32.cl +108 -0
- package/cpp/ggml/src/ggml-opencl/kernels/softmax_f16.cl +107 -0
- package/cpp/ggml/src/ggml-opencl/kernels/softmax_f32.cl +107 -0
- package/cpp/ggml/src/ggml-opencl/kernels/softplus.cl +116 -0
- package/cpp/ggml/src/ggml-opencl/kernels/solve_tri.cl +51 -0
- package/cpp/ggml/src/ggml-opencl/kernels/sqr.cl +53 -0
- package/cpp/ggml/src/ggml-opencl/kernels/sqrt.cl +53 -0
- package/cpp/ggml/src/ggml-opencl/kernels/ssm_conv.cl +77 -0
- package/cpp/ggml/src/ggml-opencl/kernels/sub.cl +138 -0
- package/cpp/ggml/src/ggml-opencl/kernels/sum_rows.cl +140 -0
- package/cpp/ggml/src/ggml-opencl/kernels/tanh.cl +109 -0
- package/cpp/ggml/src/ggml-opencl/kernels/transpose.cl +117 -0
- package/cpp/ggml/src/ggml-opencl/kernels/tri.cl +32 -0
- package/cpp/ggml/src/ggml-opencl/kernels/tsembd.cl +48 -0
- package/cpp/ggml/src/ggml-opencl/kernels/upscale.cl +120 -0
- package/cpp/ggml/src/ggml-opt.cpp +1093 -0
- package/cpp/ggml/src/ggml-quants.c +5325 -0
- package/cpp/ggml/src/ggml-quants.h +106 -0
- package/cpp/ggml/src/ggml-rpc/CMakeLists.txt +9 -0
- package/cpp/ggml/src/ggml-rpc/ggml-rpc.cpp +2118 -0
- package/cpp/ggml/src/ggml-threading.cpp +12 -0
- package/cpp/ggml/src/ggml-threading.h +14 -0
- package/cpp/ggml/src/ggml-virtgpu/CMakeLists.txt +70 -0
- package/cpp/ggml/src/ggml-virtgpu/apir_cs_ggml-rpc-front.cpp +87 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/CMakeLists.txt +21 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/apir_cs_ggml-rpc-back.cpp +115 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/backend-convert.h +13 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/backend-dispatched-backend.cpp +102 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/backend-dispatched-buffer-type.cpp +105 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/backend-dispatched-buffer.cpp +179 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/backend-dispatched-device.cpp +148 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/backend-dispatched.cpp +51 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/backend-dispatched.gen.h +73 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/backend-dispatched.h +27 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/backend-virgl-apir.h +32 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/backend.cpp +144 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/shared/api_remoting.h +95 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/shared/apir_backend.gen.h +94 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/shared/apir_backend.h +50 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/shared/apir_cs.h +378 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/shared/apir_cs_ggml.h +232 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/shared/apir_cs_rpc.h +58 -0
- package/cpp/ggml/src/ggml-virtgpu/ggml-backend-buffer-type.cpp +81 -0
- package/cpp/ggml/src/ggml-virtgpu/ggml-backend-buffer.cpp +119 -0
- package/cpp/ggml/src/ggml-virtgpu/ggml-backend-device.cpp +158 -0
- package/cpp/ggml/src/ggml-virtgpu/ggml-backend-reg.cpp +213 -0
- package/cpp/ggml/src/ggml-virtgpu/ggml-backend.cpp +69 -0
- package/cpp/ggml/src/ggml-virtgpu/ggml-remoting.h +71 -0
- package/cpp/ggml/src/ggml-virtgpu/ggmlremoting_functions.yaml +166 -0
- package/cpp/ggml/src/ggml-virtgpu/include/apir_hw.h +9 -0
- package/cpp/ggml/src/ggml-virtgpu/regenerate_remoting.py +333 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu-apir.h +15 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu-forward-backend.cpp +58 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu-forward-buffer-type.cpp +110 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu-forward-buffer.cpp +173 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu-forward-device.cpp +192 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu-forward-impl.h +36 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu-forward.gen.h +53 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu-shm.cpp +98 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu-shm.h +23 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu-utils.cpp +179 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu-utils.h +86 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu.cpp +544 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu.h +117 -0
- package/cpp/ggml/src/ggml-webgpu/CMakeLists.txt +80 -0
- package/cpp/ggml/src/ggml-webgpu/ggml-webgpu-shader-lib.hpp +1231 -0
- package/cpp/ggml/src/ggml-webgpu/ggml-webgpu.cpp +3150 -0
- package/cpp/ggml/src/ggml-webgpu/pre_wgsl.hpp +778 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/argmax.wgsl +72 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/argsort.wgsl +106 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/argsort_merge.wgsl +134 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/binary.wgsl +107 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/common_decls.tmpl +923 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/cpy.tmpl.wgsl +107 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/cumsum.wgsl +66 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/embed_wgsl.py +182 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/flash_attn.wgsl +636 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/get_rows.wgsl +668 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/glu.tmpl.wgsl +323 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/memset.wgsl +40 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat.wgsl +713 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_decls.tmpl +103 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_reg_tile.wgsl +138 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_subgroup_matrix.wgsl +188 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_vec.wgsl +194 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/pad.wgsl +86 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/rms_norm.wgsl +123 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/rope.tmpl.wgsl +295 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/scale.wgsl +63 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/set_rows.wgsl +109 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/soft_max.tmpl.wgsl +345 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/sum_rows.wgsl +55 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/unary.wgsl +193 -0
- package/cpp/ggml/src/ggml-zdnn/CMakeLists.txt +36 -0
- package/cpp/ggml/src/ggml-zdnn/common.hpp +59 -0
- package/cpp/ggml/src/ggml-zdnn/ggml-zdnn.cpp +633 -0
- package/cpp/ggml/src/ggml-zdnn/mmf.cpp +80 -0
- package/cpp/ggml/src/ggml-zdnn/mmf.hpp +12 -0
- package/cpp/ggml/src/ggml-zdnn/utils.cpp +79 -0
- package/cpp/ggml/src/ggml-zdnn/utils.hpp +19 -0
- package/cpp/ggml/src/ggml-zendnn/CMakeLists.txt +92 -0
- package/cpp/ggml/src/ggml-zendnn/ggml-zendnn.cpp +469 -0
- package/cpp/ggml/src/ggml.c +7669 -0
- package/cpp/ggml/src/ggml.cpp +26 -0
- package/cpp/ggml/src/gguf.cpp +1699 -0
- package/cpp/include/llama-cpp.h +32 -0
- package/cpp/include/llama.h +1568 -0
- package/cpp/mtmd/CMakeLists.txt +98 -0
- package/cpp/mtmd/README.md +63 -0
- package/cpp/mtmd/clip-graph.h +117 -0
- package/cpp/mtmd/clip-impl.h +586 -0
- package/cpp/mtmd/clip-model.h +390 -0
- package/cpp/mtmd/clip.cpp +4154 -0
- package/cpp/mtmd/clip.h +121 -0
- package/cpp/mtmd/deprecation-warning.cpp +22 -0
- package/cpp/mtmd/legacy-models/convert_image_encoder_to_gguf.py +412 -0
- package/cpp/mtmd/legacy-models/glmedge-convert-image-encoder-to-gguf.py +280 -0
- package/cpp/mtmd/legacy-models/glmedge-surgery.py +33 -0
- package/cpp/mtmd/legacy-models/llava_surgery.py +38 -0
- package/cpp/mtmd/legacy-models/llava_surgery_v2.py +180 -0
- package/cpp/mtmd/legacy-models/minicpmv-convert-image-encoder-to-gguf.py +892 -0
- package/cpp/mtmd/legacy-models/minicpmv-surgery.py +47 -0
- package/cpp/mtmd/models/cogvlm.cpp +98 -0
- package/cpp/mtmd/models/conformer.cpp +216 -0
- package/cpp/mtmd/models/glm4v.cpp +122 -0
- package/cpp/mtmd/models/internvl.cpp +69 -0
- package/cpp/mtmd/models/kimik25.cpp +101 -0
- package/cpp/mtmd/models/kimivl.cpp +63 -0
- package/cpp/mtmd/models/llama4.cpp +96 -0
- package/cpp/mtmd/models/llava.cpp +374 -0
- package/cpp/mtmd/models/minicpmv.cpp +114 -0
- package/cpp/mtmd/models/mobilenetv5.cpp +451 -0
- package/cpp/mtmd/models/models.h +128 -0
- package/cpp/mtmd/models/nemotron-v2-vl.cpp +35 -0
- package/cpp/mtmd/models/paddleocr.cpp +52 -0
- package/cpp/mtmd/models/pixtral.cpp +86 -0
- package/cpp/mtmd/models/qwen2vl.cpp +183 -0
- package/cpp/mtmd/models/qwen3vl.cpp +193 -0
- package/cpp/mtmd/models/siglip.cpp +86 -0
- package/cpp/mtmd/models/whisper-enc.cpp +115 -0
- package/cpp/mtmd/models/youtuvl.cpp +179 -0
- package/cpp/mtmd/mtmd-audio.cpp +730 -0
- package/cpp/mtmd/mtmd-audio.h +113 -0
- package/cpp/mtmd/mtmd-cli.cpp +437 -0
- package/cpp/mtmd/mtmd-helper.cpp +521 -0
- package/cpp/mtmd/mtmd-helper.h +96 -0
- package/cpp/mtmd/mtmd.cpp +1156 -0
- package/cpp/mtmd/mtmd.h +319 -0
- package/cpp/mtmd/requirements.txt +5 -0
- package/cpp/mtmd/test-1.jpeg +0 -0
- package/cpp/mtmd/test-2.mp3 +0 -0
- package/cpp/mtmd/tests.sh +192 -0
- package/cpp/src/CMakeLists.txt +169 -0
- package/cpp/src/llama-adapter.cpp +488 -0
- package/cpp/src/llama-adapter.h +89 -0
- package/cpp/src/llama-arch.cpp +2855 -0
- package/cpp/src/llama-arch.h +619 -0
- package/cpp/src/llama-batch.cpp +917 -0
- package/cpp/src/llama-batch.h +173 -0
- package/cpp/src/llama-chat.cpp +896 -0
- package/cpp/src/llama-chat.h +71 -0
- package/cpp/src/llama-context.cpp +3512 -0
- package/cpp/src/llama-context.h +359 -0
- package/cpp/src/llama-cparams.cpp +5 -0
- package/cpp/src/llama-cparams.h +44 -0
- package/cpp/src/llama-grammar.cpp +1464 -0
- package/cpp/src/llama-grammar.h +194 -0
- package/cpp/src/llama-graph.cpp +2685 -0
- package/cpp/src/llama-graph.h +1026 -0
- package/cpp/src/llama-hparams.cpp +234 -0
- package/cpp/src/llama-hparams.h +339 -0
- package/cpp/src/llama-impl.cpp +171 -0
- package/cpp/src/llama-impl.h +73 -0
- package/cpp/src/llama-io.cpp +15 -0
- package/cpp/src/llama-io.h +35 -0
- package/cpp/src/llama-kv-cache-iswa.cpp +330 -0
- package/cpp/src/llama-kv-cache-iswa.h +137 -0
- package/cpp/src/llama-kv-cache.cpp +2271 -0
- package/cpp/src/llama-kv-cache.h +388 -0
- package/cpp/src/llama-kv-cells.h +533 -0
- package/cpp/src/llama-memory-hybrid-iswa.cpp +275 -0
- package/cpp/src/llama-memory-hybrid-iswa.h +140 -0
- package/cpp/src/llama-memory-hybrid.cpp +268 -0
- package/cpp/src/llama-memory-hybrid.h +139 -0
- package/cpp/src/llama-memory-recurrent.cpp +1165 -0
- package/cpp/src/llama-memory-recurrent.h +182 -0
- package/cpp/src/llama-memory.cpp +59 -0
- package/cpp/src/llama-memory.h +122 -0
- package/cpp/src/llama-mmap.cpp +785 -0
- package/cpp/src/llama-mmap.h +92 -0
- package/cpp/src/llama-model-loader.cpp +1414 -0
- package/cpp/src/llama-model-loader.h +203 -0
- package/cpp/src/llama-model-saver.cpp +286 -0
- package/cpp/src/llama-model-saver.h +37 -0
- package/cpp/src/llama-model.cpp +9253 -0
- package/cpp/src/llama-model.h +576 -0
- package/cpp/src/llama-quant.cpp +1119 -0
- package/cpp/src/llama-quant.h +1 -0
- package/cpp/src/llama-sampler.cpp +3885 -0
- package/cpp/src/llama-sampler.h +42 -0
- package/cpp/src/llama-vocab.cpp +3970 -0
- package/cpp/src/llama-vocab.h +187 -0
- package/cpp/src/llama.cpp +1313 -0
- package/cpp/src/models/afmoe.cpp +191 -0
- package/cpp/src/models/apertus.cpp +125 -0
- package/cpp/src/models/arcee.cpp +135 -0
- package/cpp/src/models/arctic.cpp +138 -0
- package/cpp/src/models/arwkv7.cpp +86 -0
- package/cpp/src/models/baichuan.cpp +122 -0
- package/cpp/src/models/bailingmoe.cpp +144 -0
- package/cpp/src/models/bailingmoe2.cpp +135 -0
- package/cpp/src/models/bert.cpp +178 -0
- package/cpp/src/models/bitnet.cpp +160 -0
- package/cpp/src/models/bloom.cpp +101 -0
- package/cpp/src/models/chameleon.cpp +178 -0
- package/cpp/src/models/chatglm.cpp +132 -0
- package/cpp/src/models/codeshell.cpp +111 -0
- package/cpp/src/models/cogvlm.cpp +102 -0
- package/cpp/src/models/cohere2-iswa.cpp +134 -0
- package/cpp/src/models/command-r.cpp +122 -0
- package/cpp/src/models/dbrx.cpp +123 -0
- package/cpp/src/models/deci.cpp +135 -0
- package/cpp/src/models/deepseek.cpp +144 -0
- package/cpp/src/models/deepseek2.cpp +262 -0
- package/cpp/src/models/delta-net-base.cpp +376 -0
- package/cpp/src/models/dots1.cpp +134 -0
- package/cpp/src/models/dream.cpp +105 -0
- package/cpp/src/models/ernie4-5-moe.cpp +150 -0
- package/cpp/src/models/ernie4-5.cpp +110 -0
- package/cpp/src/models/eurobert.cpp +97 -0
- package/cpp/src/models/exaone-moe.cpp +146 -0
- package/cpp/src/models/exaone.cpp +114 -0
- package/cpp/src/models/exaone4.cpp +123 -0
- package/cpp/src/models/falcon-h1.cpp +111 -0
- package/cpp/src/models/falcon.cpp +120 -0
- package/cpp/src/models/gemma-embedding.cpp +116 -0
- package/cpp/src/models/gemma.cpp +112 -0
- package/cpp/src/models/gemma2-iswa.cpp +128 -0
- package/cpp/src/models/gemma3.cpp +155 -0
- package/cpp/src/models/gemma3n-iswa.cpp +384 -0
- package/cpp/src/models/glm4-moe.cpp +170 -0
- package/cpp/src/models/glm4.cpp +157 -0
- package/cpp/src/models/gpt2.cpp +105 -0
- package/cpp/src/models/gptneox.cpp +144 -0
- package/cpp/src/models/granite-hybrid.cpp +196 -0
- package/cpp/src/models/granite.cpp +211 -0
- package/cpp/src/models/grok.cpp +159 -0
- package/cpp/src/models/grovemoe.cpp +141 -0
- package/cpp/src/models/hunyuan-dense.cpp +132 -0
- package/cpp/src/models/hunyuan-moe.cpp +154 -0
- package/cpp/src/models/internlm2.cpp +120 -0
- package/cpp/src/models/jais.cpp +86 -0
- package/cpp/src/models/jais2.cpp +123 -0
- package/cpp/src/models/jamba.cpp +106 -0
- package/cpp/src/models/kimi-linear.cpp +392 -0
- package/cpp/src/models/lfm2.cpp +190 -0
- package/cpp/src/models/llada-moe.cpp +122 -0
- package/cpp/src/models/llada.cpp +99 -0
- package/cpp/src/models/llama-iswa.cpp +178 -0
- package/cpp/src/models/llama.cpp +168 -0
- package/cpp/src/models/maincoder.cpp +117 -0
- package/cpp/src/models/mamba-base.cpp +285 -0
- package/cpp/src/models/mamba.cpp +54 -0
- package/cpp/src/models/mimo2-iswa.cpp +123 -0
- package/cpp/src/models/minicpm3.cpp +200 -0
- package/cpp/src/models/minimax-m2.cpp +124 -0
- package/cpp/src/models/mistral3.cpp +160 -0
- package/cpp/src/models/models.h +684 -0
- package/cpp/src/models/modern-bert.cpp +109 -0
- package/cpp/src/models/mpt.cpp +126 -0
- package/cpp/src/models/nemotron-h.cpp +148 -0
- package/cpp/src/models/nemotron.cpp +122 -0
- package/cpp/src/models/neo-bert.cpp +104 -0
- package/cpp/src/models/olmo.cpp +121 -0
- package/cpp/src/models/olmo2.cpp +150 -0
- package/cpp/src/models/olmoe.cpp +124 -0
- package/cpp/src/models/openai-moe-iswa.cpp +127 -0
- package/cpp/src/models/openelm.cpp +124 -0
- package/cpp/src/models/orion.cpp +123 -0
- package/cpp/src/models/paddleocr.cpp +122 -0
- package/cpp/src/models/pangu-embedded.cpp +121 -0
- package/cpp/src/models/phi2.cpp +121 -0
- package/cpp/src/models/phi3.cpp +152 -0
- package/cpp/src/models/plamo.cpp +110 -0
- package/cpp/src/models/plamo2.cpp +318 -0
- package/cpp/src/models/plamo3.cpp +128 -0
- package/cpp/src/models/plm.cpp +169 -0
- package/cpp/src/models/qwen.cpp +108 -0
- package/cpp/src/models/qwen2.cpp +126 -0
- package/cpp/src/models/qwen2moe.cpp +151 -0
- package/cpp/src/models/qwen2vl.cpp +117 -0
- package/cpp/src/models/qwen3.cpp +117 -0
- package/cpp/src/models/qwen35.cpp +386 -0
- package/cpp/src/models/qwen35moe.cpp +420 -0
- package/cpp/src/models/qwen3moe.cpp +124 -0
- package/cpp/src/models/qwen3next.cpp +525 -0
- package/cpp/src/models/qwen3vl-moe.cpp +140 -0
- package/cpp/src/models/qwen3vl.cpp +132 -0
- package/cpp/src/models/refact.cpp +94 -0
- package/cpp/src/models/rnd1.cpp +126 -0
- package/cpp/src/models/rwkv6-base.cpp +164 -0
- package/cpp/src/models/rwkv6.cpp +94 -0
- package/cpp/src/models/rwkv6qwen2.cpp +86 -0
- package/cpp/src/models/rwkv7-base.cpp +137 -0
- package/cpp/src/models/rwkv7.cpp +90 -0
- package/cpp/src/models/seed-oss.cpp +124 -0
- package/cpp/src/models/smallthinker.cpp +126 -0
- package/cpp/src/models/smollm3.cpp +128 -0
- package/cpp/src/models/stablelm.cpp +146 -0
- package/cpp/src/models/starcoder.cpp +100 -0
- package/cpp/src/models/starcoder2.cpp +121 -0
- package/cpp/src/models/step35-iswa.cpp +168 -0
- package/cpp/src/models/t5-dec.cpp +166 -0
- package/cpp/src/models/t5-enc.cpp +96 -0
- package/cpp/src/models/wavtokenizer-dec.cpp +149 -0
- package/cpp/src/models/xverse.cpp +108 -0
- package/cpp/src/unicode-data.cpp +7034 -0
- package/cpp/src/unicode-data.h +20 -0
- package/cpp/src/unicode.cpp +1103 -0
- package/cpp/src/unicode.h +111 -0
- package/cpp/vendor/nlohmann/json.hpp +25526 -0
- package/cpp/vendor/nlohmann/json_fwd.hpp +187 -0
- package/cpp/vendor/stb/stb_image.h +7988 -0
- package/ios/LocalLLM-Bridging-Header.h +2 -0
- package/ios/LocalLLM.h +5 -0
- package/ios/LocalLLM.mm +1267 -0
- package/local-llm-rn.podspec +60 -0
- package/package.json +35 -0
- package/src/NativeLocalLLM.ts +73 -0
- package/src/device.ts +50 -0
- package/src/download-adapter.ts +17 -0
- package/src/index.ts +21 -0
- package/src/native-bridge.ts +142 -0
- package/src/rn-downloader.ts +37 -0
|
@@ -0,0 +1,4370 @@
|
|
|
1
|
+
#include "ggml-metal-ops.h"
|
|
2
|
+
|
|
3
|
+
#include "ggml.h"
|
|
4
|
+
#include "ggml-impl.h"
|
|
5
|
+
#include "ggml-backend-impl.h"
|
|
6
|
+
|
|
7
|
+
#include "ggml-metal-impl.h"
|
|
8
|
+
#include "ggml-metal-common.h"
|
|
9
|
+
#include "ggml-metal-device.h"
|
|
10
|
+
|
|
11
|
+
#include <cassert>
|
|
12
|
+
#include <algorithm>
|
|
13
|
+
#include <limits>
|
|
14
|
+
#include <cmath>
|
|
15
|
+
|
|
16
|
+
static ggml_metal_buffer_id ggml_metal_get_buffer_id(const ggml_tensor * t) {
|
|
17
|
+
if (!t) {
|
|
18
|
+
return { nullptr, 0 };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
ggml_backend_buffer_t buffer = t->view_src ? t->view_src->buffer : t->buffer;
|
|
22
|
+
|
|
23
|
+
ggml_metal_buffer_t ctx = (ggml_metal_buffer_t) buffer->context;
|
|
24
|
+
|
|
25
|
+
return ggml_metal_buffer_get_id(ctx, t);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
struct ggml_metal_op {
|
|
29
|
+
ggml_metal_op(
|
|
30
|
+
ggml_metal_device_t dev,
|
|
31
|
+
ggml_metal_cmd_buf_t cmd_buf,
|
|
32
|
+
ggml_cgraph * gf,
|
|
33
|
+
int idx_start,
|
|
34
|
+
int idx_end,
|
|
35
|
+
bool use_fusion,
|
|
36
|
+
bool use_concurrency,
|
|
37
|
+
bool use_capture,
|
|
38
|
+
int debug_graph,
|
|
39
|
+
int debug_fusion) {
|
|
40
|
+
this->dev = dev;
|
|
41
|
+
this->lib = ggml_metal_device_get_library(dev);
|
|
42
|
+
this->enc = ggml_metal_encoder_init(cmd_buf, use_concurrency);
|
|
43
|
+
this->mem_ranges = ggml_mem_ranges_init(debug_graph);
|
|
44
|
+
this->idx_start = idx_start;
|
|
45
|
+
this->idx_end = idx_end;
|
|
46
|
+
this->use_fusion = use_fusion;
|
|
47
|
+
this->use_concurrency = use_concurrency;
|
|
48
|
+
this->use_capture = use_capture;
|
|
49
|
+
this->debug_graph = debug_graph;
|
|
50
|
+
this->debug_fusion = debug_fusion;
|
|
51
|
+
this->gf = gf;
|
|
52
|
+
|
|
53
|
+
idxs.reserve(gf->n_nodes);
|
|
54
|
+
|
|
55
|
+
// filter empty nodes
|
|
56
|
+
// TODO: this can be removed when the allocator starts filtering them earlier
|
|
57
|
+
// https://github.com/ggml-org/llama.cpp/pull/16130#issuecomment-3327905830
|
|
58
|
+
for (int i = idx_start; i < idx_end; i++) {
|
|
59
|
+
if (!ggml_op_is_empty(gf->nodes[i]->op) && !ggml_is_empty(gf->nodes[i])) {
|
|
60
|
+
idxs.push_back(i);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
~ggml_metal_op() {
|
|
66
|
+
ggml_metal_encoder_end_encoding(this->enc);
|
|
67
|
+
ggml_metal_encoder_free(this->enc);
|
|
68
|
+
ggml_mem_ranges_free(this->mem_ranges);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
int n_nodes() const {
|
|
72
|
+
return idxs.size();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
ggml_tensor * node(int i) const {
|
|
76
|
+
assert(i >= 0 && i < (int) idxs.size());
|
|
77
|
+
return ggml_graph_node(gf, idxs[i]);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
bool can_fuse(int i0, const ggml_op * ops, int n_ops) const {
|
|
81
|
+
assert(use_fusion);
|
|
82
|
+
assert(i0 >= 0 && i0 < n_nodes());
|
|
83
|
+
|
|
84
|
+
if (i0 + n_ops > n_nodes()) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return ggml_can_fuse_ext(gf, idxs.data() + i0, ops, n_ops);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
ggml_metal_device_t dev;
|
|
92
|
+
ggml_metal_library_t lib;
|
|
93
|
+
ggml_metal_encoder_t enc;
|
|
94
|
+
ggml_mem_ranges_t mem_ranges;
|
|
95
|
+
|
|
96
|
+
bool use_fusion;
|
|
97
|
+
bool use_concurrency;
|
|
98
|
+
bool use_capture;
|
|
99
|
+
|
|
100
|
+
int debug_graph;
|
|
101
|
+
int debug_fusion;
|
|
102
|
+
|
|
103
|
+
private:
|
|
104
|
+
ggml_cgraph * gf;
|
|
105
|
+
|
|
106
|
+
int idx_start;
|
|
107
|
+
int idx_end;
|
|
108
|
+
|
|
109
|
+
// non-empty node indices
|
|
110
|
+
std::vector<int> idxs;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
ggml_metal_op_t ggml_metal_op_init(
|
|
114
|
+
ggml_metal_device_t dev,
|
|
115
|
+
ggml_metal_cmd_buf_t cmd_buf,
|
|
116
|
+
ggml_cgraph * gf,
|
|
117
|
+
int idx_start,
|
|
118
|
+
int idx_end,
|
|
119
|
+
bool use_fusion,
|
|
120
|
+
bool use_concurrency,
|
|
121
|
+
bool use_capture,
|
|
122
|
+
int debug_graph,
|
|
123
|
+
int debug_fusion) {
|
|
124
|
+
ggml_metal_op_t res = new ggml_metal_op(
|
|
125
|
+
dev,
|
|
126
|
+
cmd_buf,
|
|
127
|
+
gf,
|
|
128
|
+
idx_start,
|
|
129
|
+
idx_end,
|
|
130
|
+
use_fusion,
|
|
131
|
+
use_concurrency,
|
|
132
|
+
use_capture,
|
|
133
|
+
debug_graph,
|
|
134
|
+
debug_fusion);
|
|
135
|
+
|
|
136
|
+
return res;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
void ggml_metal_op_free(ggml_metal_op_t ctx) {
|
|
140
|
+
delete ctx;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
int ggml_metal_op_n_nodes(ggml_metal_op_t ctx) {
|
|
144
|
+
return ctx->n_nodes();
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
static bool ggml_metal_op_concurrency_reset(ggml_metal_op_t ctx) {
|
|
148
|
+
if (!ctx->mem_ranges) {
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
ggml_metal_encoder_memory_barrier(ctx->enc);
|
|
153
|
+
|
|
154
|
+
ggml_mem_ranges_reset(ctx->mem_ranges);
|
|
155
|
+
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
static bool ggml_metal_op_concurrency_check(ggml_metal_op_t ctx, const ggml_tensor * node) {
|
|
160
|
+
if (!ctx->mem_ranges) {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return ggml_mem_ranges_check(ctx->mem_ranges, node);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
static bool ggml_metal_op_concurrency_add(ggml_metal_op_t ctx, const ggml_tensor * node) {
|
|
168
|
+
if (!ctx->mem_ranges) {
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return ggml_mem_ranges_add(ctx->mem_ranges, node);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
static int ggml_metal_op_encode_impl(ggml_metal_op_t ctx, int idx) {
|
|
176
|
+
struct ggml_tensor * node = ctx->node(idx);
|
|
177
|
+
|
|
178
|
+
//GGML_LOG_INFO("%s: encoding node %3d, op = %8s\n", __func__, idx, ggml_op_name(node->op));
|
|
179
|
+
|
|
180
|
+
if (ggml_is_empty(node)) {
|
|
181
|
+
return 1;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
switch (node->op) {
|
|
185
|
+
case GGML_OP_NONE:
|
|
186
|
+
case GGML_OP_RESHAPE:
|
|
187
|
+
case GGML_OP_VIEW:
|
|
188
|
+
case GGML_OP_TRANSPOSE:
|
|
189
|
+
case GGML_OP_PERMUTE:
|
|
190
|
+
{
|
|
191
|
+
// noop -> next node
|
|
192
|
+
if (ctx->debug_graph > 0) {
|
|
193
|
+
GGML_LOG_DEBUG("%s: node[%5d] - %-12s %s\n", __func__, idx, ggml_op_name(node->op), "(noop)");
|
|
194
|
+
}
|
|
195
|
+
} return 1;
|
|
196
|
+
default:
|
|
197
|
+
{
|
|
198
|
+
} break;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (!ggml_metal_device_supports_op(ctx->dev, node)) {
|
|
202
|
+
GGML_LOG_ERROR("%s: error: unsupported op '%s'\n", __func__, ggml_op_desc(node));
|
|
203
|
+
GGML_ABORT("unsupported op");
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if ((node->flags & GGML_TENSOR_FLAG_COMPUTE) == 0) {
|
|
207
|
+
return 1;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
int n_fuse = 1;
|
|
211
|
+
|
|
212
|
+
// check if the current node can run concurrently with other nodes before it
|
|
213
|
+
// the condition is that:
|
|
214
|
+
// - the current node cannot write to any previous src or dst ranges
|
|
215
|
+
// - the current node cannot read from any previous dst ranges
|
|
216
|
+
//
|
|
217
|
+
// if the condition is not satisfied, we put a memory barrier and clear all ranges
|
|
218
|
+
// otherwise, we add the new ranges to the encoding context and process the node concurrently
|
|
219
|
+
//
|
|
220
|
+
{
|
|
221
|
+
const bool is_concurrent = ggml_metal_op_concurrency_check(ctx, node);
|
|
222
|
+
|
|
223
|
+
if (!is_concurrent) {
|
|
224
|
+
ggml_metal_op_concurrency_reset(ctx);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (ctx->debug_graph > 0) {
|
|
228
|
+
GGML_LOG_DEBUG("%s: node[%5d] - %-12s %-12s %s\n", __func__, idx, ggml_op_name(node->op), ggml_get_name(node), is_concurrent ? "(concurrent)" : "");
|
|
229
|
+
}
|
|
230
|
+
if (ctx->debug_graph > 1) {
|
|
231
|
+
GGML_TENSOR_LOCALS( int64_t, ne0, node->src[0], ne);
|
|
232
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, node->src[0], nb);
|
|
233
|
+
GGML_TENSOR_LOCALS( int64_t, ne1, node->src[1], ne);
|
|
234
|
+
GGML_TENSOR_LOCALS(uint64_t, nb1, node->src[1], nb);
|
|
235
|
+
GGML_TENSOR_LOCALS( int64_t, ne2, node->src[2], ne);
|
|
236
|
+
GGML_TENSOR_LOCALS(uint64_t, nb2, node->src[2], nb);
|
|
237
|
+
GGML_TENSOR_LOCALS( int64_t, ne3, node->src[3], ne);
|
|
238
|
+
GGML_TENSOR_LOCALS(uint64_t, nb3, node->src[3], nb);
|
|
239
|
+
GGML_TENSOR_LOCALS( int64_t, ne, node, ne);
|
|
240
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, node, nb);
|
|
241
|
+
|
|
242
|
+
if (node->src[0]) {
|
|
243
|
+
GGML_LOG_DEBUG("%s: src0 - %4s [%5lld, %5lld, %5lld, %5lld] [%5lld, %5lld, %5lld, %5lld], %d, %s\n", __func__, ggml_type_name(node->src[0]->type), ne00, ne01, ne02, ne03, nb00, nb01, nb02, nb03,
|
|
244
|
+
ggml_is_contiguous(node->src[0]), node->src[0]->name);
|
|
245
|
+
}
|
|
246
|
+
if (node->src[1]) {
|
|
247
|
+
GGML_LOG_DEBUG("%s: src1 - %4s [%5lld, %5lld, %5lld, %5lld] [%5lld, %5lld, %5lld, %5lld], %d, %s\n", __func__, ggml_type_name(node->src[1]->type), ne10, ne11, ne12, ne13, nb10, nb11, nb12, nb13,
|
|
248
|
+
ggml_is_contiguous(node->src[1]), node->src[1]->name);
|
|
249
|
+
}
|
|
250
|
+
if (node->src[2]) {
|
|
251
|
+
GGML_LOG_DEBUG("%s: src2 - %4s [%5lld, %5lld, %5lld, %5lld] [%5lld, %5lld, %5lld, %5lld], %d, %s\n", __func__, ggml_type_name(node->src[2]->type), ne20, ne21, ne22, ne23, nb20, nb21, nb22, nb23,
|
|
252
|
+
ggml_is_contiguous(node->src[2]), node->src[2]->name);
|
|
253
|
+
}
|
|
254
|
+
if (node->src[3]) {
|
|
255
|
+
GGML_LOG_DEBUG("%s: src3 - %4s [%5lld, %5lld, %5lld, %5lld] [%5lld, %5lld, %5lld, %5lld], %d, %s\n", __func__, ggml_type_name(node->src[3]->type), ne30, ne31, ne32, ne33, nb30, nb31, nb32, nb33,
|
|
256
|
+
ggml_is_contiguous(node->src[3]), node->src[3]->name);
|
|
257
|
+
}
|
|
258
|
+
if (node) {
|
|
259
|
+
GGML_LOG_DEBUG("%s: node - %4s [%5lld, %5lld, %5lld, %5lld] [%5lld, %5lld, %5lld, %5lld], 1, %s\n", __func__, ggml_type_name(node->type), ne0, ne1, ne2, ne3, nb0, nb1, nb2, nb3,
|
|
260
|
+
node->name);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
switch (node->op) {
|
|
266
|
+
case GGML_OP_CONCAT:
|
|
267
|
+
{
|
|
268
|
+
n_fuse = ggml_metal_op_concat(ctx, idx);
|
|
269
|
+
} break;
|
|
270
|
+
case GGML_OP_ADD:
|
|
271
|
+
case GGML_OP_SUB:
|
|
272
|
+
case GGML_OP_MUL:
|
|
273
|
+
case GGML_OP_DIV:
|
|
274
|
+
{
|
|
275
|
+
n_fuse = ggml_metal_op_bin(ctx, idx);
|
|
276
|
+
} break;
|
|
277
|
+
case GGML_OP_ADD_ID:
|
|
278
|
+
{
|
|
279
|
+
n_fuse = ggml_metal_op_add_id(ctx, idx);
|
|
280
|
+
} break;
|
|
281
|
+
case GGML_OP_REPEAT:
|
|
282
|
+
{
|
|
283
|
+
n_fuse = ggml_metal_op_repeat(ctx, idx);
|
|
284
|
+
} break;
|
|
285
|
+
case GGML_OP_ACC:
|
|
286
|
+
{
|
|
287
|
+
n_fuse = ggml_metal_op_acc(ctx, idx);
|
|
288
|
+
} break;
|
|
289
|
+
case GGML_OP_SCALE:
|
|
290
|
+
case GGML_OP_FILL:
|
|
291
|
+
case GGML_OP_CLAMP:
|
|
292
|
+
case GGML_OP_LEAKY_RELU:
|
|
293
|
+
case GGML_OP_SQR:
|
|
294
|
+
case GGML_OP_SQRT:
|
|
295
|
+
case GGML_OP_SIN:
|
|
296
|
+
case GGML_OP_COS:
|
|
297
|
+
case GGML_OP_LOG:
|
|
298
|
+
case GGML_OP_UNARY:
|
|
299
|
+
{
|
|
300
|
+
n_fuse = ggml_metal_op_unary(ctx, idx);
|
|
301
|
+
} break;
|
|
302
|
+
case GGML_OP_GLU:
|
|
303
|
+
{
|
|
304
|
+
n_fuse = ggml_metal_op_glu(ctx, idx);
|
|
305
|
+
} break;
|
|
306
|
+
case GGML_OP_SUM:
|
|
307
|
+
{
|
|
308
|
+
n_fuse = ggml_metal_op_sum(ctx, idx);
|
|
309
|
+
} break;
|
|
310
|
+
case GGML_OP_SUM_ROWS:
|
|
311
|
+
case GGML_OP_MEAN:
|
|
312
|
+
{
|
|
313
|
+
n_fuse = ggml_metal_op_sum_rows(ctx, idx);
|
|
314
|
+
} break;
|
|
315
|
+
case GGML_OP_CUMSUM:
|
|
316
|
+
{
|
|
317
|
+
n_fuse = ggml_metal_op_cumsum(ctx, idx);
|
|
318
|
+
} break;
|
|
319
|
+
case GGML_OP_SOFT_MAX:
|
|
320
|
+
{
|
|
321
|
+
n_fuse = ggml_metal_op_soft_max(ctx, idx);
|
|
322
|
+
} break;
|
|
323
|
+
case GGML_OP_SSM_CONV:
|
|
324
|
+
{
|
|
325
|
+
n_fuse = ggml_metal_op_ssm_conv(ctx, idx);
|
|
326
|
+
} break;
|
|
327
|
+
case GGML_OP_SSM_SCAN:
|
|
328
|
+
{
|
|
329
|
+
n_fuse = ggml_metal_op_ssm_scan(ctx, idx);
|
|
330
|
+
} break;
|
|
331
|
+
case GGML_OP_RWKV_WKV6:
|
|
332
|
+
case GGML_OP_RWKV_WKV7:
|
|
333
|
+
{
|
|
334
|
+
n_fuse = ggml_metal_op_rwkv(ctx, idx);
|
|
335
|
+
} break;
|
|
336
|
+
case GGML_OP_SOLVE_TRI:
|
|
337
|
+
{
|
|
338
|
+
n_fuse = ggml_metal_op_solve_tri(ctx, idx);
|
|
339
|
+
} break;
|
|
340
|
+
case GGML_OP_MUL_MAT:
|
|
341
|
+
{
|
|
342
|
+
n_fuse = ggml_metal_op_mul_mat(ctx, idx);
|
|
343
|
+
} break;
|
|
344
|
+
case GGML_OP_MUL_MAT_ID:
|
|
345
|
+
{
|
|
346
|
+
n_fuse = ggml_metal_op_mul_mat_id(ctx, idx);
|
|
347
|
+
} break;
|
|
348
|
+
case GGML_OP_GET_ROWS:
|
|
349
|
+
{
|
|
350
|
+
n_fuse = ggml_metal_op_get_rows(ctx, idx);
|
|
351
|
+
} break;
|
|
352
|
+
case GGML_OP_SET_ROWS:
|
|
353
|
+
{
|
|
354
|
+
n_fuse = ggml_metal_op_set_rows(ctx, idx);
|
|
355
|
+
} break;
|
|
356
|
+
case GGML_OP_DIAG:
|
|
357
|
+
{
|
|
358
|
+
n_fuse = ggml_metal_op_diag(ctx, idx);
|
|
359
|
+
} break;
|
|
360
|
+
case GGML_OP_L2_NORM:
|
|
361
|
+
{
|
|
362
|
+
n_fuse = ggml_metal_op_l2_norm(ctx, idx);
|
|
363
|
+
} break;
|
|
364
|
+
case GGML_OP_GROUP_NORM:
|
|
365
|
+
{
|
|
366
|
+
n_fuse = ggml_metal_op_group_norm(ctx, idx);
|
|
367
|
+
} break;
|
|
368
|
+
case GGML_OP_NORM:
|
|
369
|
+
case GGML_OP_RMS_NORM:
|
|
370
|
+
{
|
|
371
|
+
n_fuse = ggml_metal_op_norm(ctx, idx);
|
|
372
|
+
} break;
|
|
373
|
+
case GGML_OP_ROPE:
|
|
374
|
+
{
|
|
375
|
+
n_fuse = ggml_metal_op_rope(ctx, idx);
|
|
376
|
+
} break;
|
|
377
|
+
case GGML_OP_IM2COL:
|
|
378
|
+
{
|
|
379
|
+
n_fuse = ggml_metal_op_im2col(ctx, idx);
|
|
380
|
+
} break;
|
|
381
|
+
case GGML_OP_CONV_2D:
|
|
382
|
+
{
|
|
383
|
+
n_fuse = ggml_metal_op_conv_2d(ctx, idx);
|
|
384
|
+
} break;
|
|
385
|
+
case GGML_OP_CONV_TRANSPOSE_1D:
|
|
386
|
+
{
|
|
387
|
+
n_fuse = ggml_metal_op_conv_transpose_1d(ctx, idx);
|
|
388
|
+
} break;
|
|
389
|
+
case GGML_OP_CONV_TRANSPOSE_2D:
|
|
390
|
+
{
|
|
391
|
+
n_fuse = ggml_metal_op_conv_transpose_2d(ctx, idx);
|
|
392
|
+
} break;
|
|
393
|
+
case GGML_OP_UPSCALE:
|
|
394
|
+
{
|
|
395
|
+
n_fuse = ggml_metal_op_upscale(ctx, idx);
|
|
396
|
+
} break;
|
|
397
|
+
case GGML_OP_PAD:
|
|
398
|
+
{
|
|
399
|
+
n_fuse = ggml_metal_op_pad(ctx, idx);
|
|
400
|
+
} break;
|
|
401
|
+
case GGML_OP_PAD_REFLECT_1D:
|
|
402
|
+
{
|
|
403
|
+
n_fuse = ggml_metal_op_pad_reflect_1d(ctx, idx);
|
|
404
|
+
} break;
|
|
405
|
+
case GGML_OP_ARANGE:
|
|
406
|
+
{
|
|
407
|
+
n_fuse = ggml_metal_op_arange(ctx, idx);
|
|
408
|
+
} break;
|
|
409
|
+
case GGML_OP_TIMESTEP_EMBEDDING:
|
|
410
|
+
{
|
|
411
|
+
n_fuse = ggml_metal_op_timestep_embedding(ctx, idx);
|
|
412
|
+
} break;
|
|
413
|
+
case GGML_OP_ARGSORT:
|
|
414
|
+
{
|
|
415
|
+
n_fuse = ggml_metal_op_argsort(ctx, idx);
|
|
416
|
+
} break;
|
|
417
|
+
case GGML_OP_TOP_K:
|
|
418
|
+
{
|
|
419
|
+
n_fuse = ggml_metal_op_top_k(ctx, idx);
|
|
420
|
+
} break;
|
|
421
|
+
case GGML_OP_TRI:
|
|
422
|
+
{
|
|
423
|
+
n_fuse = ggml_metal_op_tri(ctx, idx);
|
|
424
|
+
} break;
|
|
425
|
+
case GGML_OP_FLASH_ATTN_EXT:
|
|
426
|
+
{
|
|
427
|
+
n_fuse = ggml_metal_op_flash_attn_ext(ctx, idx);
|
|
428
|
+
} break;
|
|
429
|
+
case GGML_OP_SET:
|
|
430
|
+
{
|
|
431
|
+
n_fuse = ggml_metal_op_set(ctx, idx);
|
|
432
|
+
} break;
|
|
433
|
+
case GGML_OP_DUP:
|
|
434
|
+
case GGML_OP_CPY:
|
|
435
|
+
case GGML_OP_CONT:
|
|
436
|
+
{
|
|
437
|
+
n_fuse = ggml_metal_op_cpy(ctx, idx);
|
|
438
|
+
} break;
|
|
439
|
+
case GGML_OP_POOL_1D:
|
|
440
|
+
{
|
|
441
|
+
n_fuse = ggml_metal_op_pool_1d(ctx, idx);
|
|
442
|
+
} break;
|
|
443
|
+
case GGML_OP_POOL_2D:
|
|
444
|
+
{
|
|
445
|
+
n_fuse = ggml_metal_op_pool_2d(ctx, idx);
|
|
446
|
+
} break;
|
|
447
|
+
case GGML_OP_ARGMAX:
|
|
448
|
+
{
|
|
449
|
+
n_fuse = ggml_metal_op_argmax(ctx, idx);
|
|
450
|
+
} break;
|
|
451
|
+
case GGML_OP_OPT_STEP_ADAMW:
|
|
452
|
+
{
|
|
453
|
+
n_fuse = ggml_metal_op_opt_step_adamw(ctx, idx);
|
|
454
|
+
} break;
|
|
455
|
+
case GGML_OP_OPT_STEP_SGD:
|
|
456
|
+
{
|
|
457
|
+
n_fuse = ggml_metal_op_opt_step_sgd(ctx, idx);
|
|
458
|
+
} break;
|
|
459
|
+
case GGML_OP_COUNT_EQUAL:
|
|
460
|
+
{
|
|
461
|
+
n_fuse = ggml_metal_op_count_equal(ctx, idx);
|
|
462
|
+
} break;
|
|
463
|
+
default:
|
|
464
|
+
{
|
|
465
|
+
GGML_LOG_ERROR("%s: error: node %3d, op = %8s not implemented\n", __func__, idx, ggml_op_name(node->op));
|
|
466
|
+
GGML_ABORT("fatal error");
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
if (ctx->debug_graph > 0) {
|
|
471
|
+
if (n_fuse > 1) {
|
|
472
|
+
GGML_LOG_DEBUG("%s: fuse %d ops\n", __func__, n_fuse);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
// update the mem ranges in the encoding context
|
|
477
|
+
for (int i = 0; i < n_fuse; ++i) {
|
|
478
|
+
if (!ggml_metal_op_concurrency_add(ctx, ctx->node(idx + i))) {
|
|
479
|
+
ggml_metal_op_concurrency_reset(ctx);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
return n_fuse;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
int ggml_metal_op_encode(ggml_metal_op_t ctx, int idx) {
|
|
487
|
+
if (ctx->use_capture) {
|
|
488
|
+
ggml_metal_encoder_debug_group_push(ctx->enc, ggml_op_desc(ctx->node(idx)));
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
int res = ggml_metal_op_encode_impl(ctx, idx);
|
|
492
|
+
if (idx + res > ctx->n_nodes()) {
|
|
493
|
+
GGML_ABORT("fusion error: nodes spanning multiple encoders have been fused. this indicates a bug in the fusion logic %s",
|
|
494
|
+
"https://github.com/ggml-org/llama.cpp/pull/14849");
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
if (ctx->use_capture) {
|
|
498
|
+
ggml_metal_encoder_debug_group_pop(ctx->enc);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
return res;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
int ggml_metal_op_concat(ggml_metal_op_t ctx, int idx) {
|
|
505
|
+
ggml_tensor * op = ctx->node(idx);
|
|
506
|
+
|
|
507
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
508
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
509
|
+
|
|
510
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
511
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
512
|
+
GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
|
|
513
|
+
GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb);
|
|
514
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
515
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
516
|
+
|
|
517
|
+
const int32_t dim = ((const int32_t *) op->op_params)[0];
|
|
518
|
+
|
|
519
|
+
ggml_metal_kargs_concat args = {
|
|
520
|
+
/*.ne00 =*/ ne00,
|
|
521
|
+
/*.ne01 =*/ ne01,
|
|
522
|
+
/*.ne02 =*/ ne02,
|
|
523
|
+
/*.ne03 =*/ ne03,
|
|
524
|
+
/*.nb00 =*/ nb00,
|
|
525
|
+
/*.nb01 =*/ nb01,
|
|
526
|
+
/*.nb02 =*/ nb02,
|
|
527
|
+
/*.nb03 =*/ nb03,
|
|
528
|
+
/*.ne10 =*/ ne10,
|
|
529
|
+
/*.ne11 =*/ ne11,
|
|
530
|
+
/*.ne12 =*/ ne12,
|
|
531
|
+
/*.ne13 =*/ ne13,
|
|
532
|
+
/*.nb10 =*/ nb10,
|
|
533
|
+
/*.nb11 =*/ nb11,
|
|
534
|
+
/*.nb12 =*/ nb12,
|
|
535
|
+
/*.nb13 =*/ nb13,
|
|
536
|
+
/*.ne0 =*/ ne0,
|
|
537
|
+
/*.ne1 =*/ ne1,
|
|
538
|
+
/*.ne2 =*/ ne2,
|
|
539
|
+
/*.ne3 =*/ ne3,
|
|
540
|
+
/*.nb0 =*/ nb0,
|
|
541
|
+
/*.nb1 =*/ nb1,
|
|
542
|
+
/*.nb2 =*/ nb2,
|
|
543
|
+
/*.nb3 =*/ nb3,
|
|
544
|
+
/*.dim =*/ dim,
|
|
545
|
+
};
|
|
546
|
+
|
|
547
|
+
auto pipeline = ggml_metal_library_get_pipeline_base(lib, GGML_OP_CONCAT);
|
|
548
|
+
|
|
549
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
550
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
551
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
552
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2);
|
|
553
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3);
|
|
554
|
+
|
|
555
|
+
const int nth = std::min(1024, ne0);
|
|
556
|
+
|
|
557
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ne1, ne2, ne3, nth, 1, 1);
|
|
558
|
+
|
|
559
|
+
return 1;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
int ggml_metal_op_repeat(ggml_metal_op_t ctx, int idx) {
|
|
563
|
+
ggml_tensor * op = ctx->node(idx);
|
|
564
|
+
|
|
565
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
566
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
567
|
+
|
|
568
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
569
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
570
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
571
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
572
|
+
|
|
573
|
+
auto pipeline = ggml_metal_library_get_pipeline_repeat(lib, op->type);
|
|
574
|
+
|
|
575
|
+
ggml_metal_kargs_repeat args = {
|
|
576
|
+
/*.ne00 =*/ ne00,
|
|
577
|
+
/*.ne01 =*/ ne01,
|
|
578
|
+
/*.ne02 =*/ ne02,
|
|
579
|
+
/*.ne03 =*/ ne03,
|
|
580
|
+
/*.nb00 =*/ nb00,
|
|
581
|
+
/*.nb01 =*/ nb01,
|
|
582
|
+
/*.nb02 =*/ nb02,
|
|
583
|
+
/*.nb03 =*/ nb03,
|
|
584
|
+
/*.ne0 =*/ ne0,
|
|
585
|
+
/*.ne1 =*/ ne1,
|
|
586
|
+
/*.ne2 =*/ ne2,
|
|
587
|
+
/*.ne3 =*/ ne3,
|
|
588
|
+
/*.nb0 =*/ nb0,
|
|
589
|
+
/*.nb1 =*/ nb1,
|
|
590
|
+
/*.nb2 =*/ nb2,
|
|
591
|
+
/*.nb3 =*/ nb3,
|
|
592
|
+
};
|
|
593
|
+
|
|
594
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
595
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
596
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
597
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2);
|
|
598
|
+
|
|
599
|
+
const int nth = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), ne0);
|
|
600
|
+
|
|
601
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ne1, ne2, ne3, nth, 1, 1);
|
|
602
|
+
|
|
603
|
+
return 1;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
int ggml_metal_op_acc(ggml_metal_op_t ctx, int idx) {
|
|
607
|
+
ggml_tensor * op = ctx->node(idx);
|
|
608
|
+
|
|
609
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
610
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
611
|
+
|
|
612
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
613
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
614
|
+
GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
|
|
615
|
+
GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb);
|
|
616
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
617
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
618
|
+
|
|
619
|
+
GGML_ASSERT(op->src[0]->type == GGML_TYPE_F32);
|
|
620
|
+
GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32);
|
|
621
|
+
GGML_ASSERT(op->type == GGML_TYPE_F32);
|
|
622
|
+
|
|
623
|
+
GGML_ASSERT(ggml_is_contiguous_rows(op->src[0]));
|
|
624
|
+
GGML_ASSERT(ggml_is_contiguous_rows(op->src[1]));
|
|
625
|
+
|
|
626
|
+
const size_t pnb1 = ((const int32_t *) op->op_params)[0];
|
|
627
|
+
const size_t pnb2 = ((const int32_t *) op->op_params)[1];
|
|
628
|
+
const size_t pnb3 = ((const int32_t *) op->op_params)[2];
|
|
629
|
+
const size_t offs = ((const int32_t *) op->op_params)[3];
|
|
630
|
+
|
|
631
|
+
const bool inplace = (bool) ((const int32_t *) op->op_params)[4];
|
|
632
|
+
|
|
633
|
+
if (!inplace) {
|
|
634
|
+
// run a separete kernel to cpy src->dst
|
|
635
|
+
// not sure how to avoid this
|
|
636
|
+
// TODO: make a simpler cpy_bytes kernel
|
|
637
|
+
|
|
638
|
+
//const id<MTLComputePipelineState> pipeline = ctx->pipelines[GGML_METAL_PIPELINE_TYPE_CPY_F32_F32].obj;
|
|
639
|
+
auto pipeline = ggml_metal_library_get_pipeline_cpy(lib, op->src[0]->type, op->type);
|
|
640
|
+
|
|
641
|
+
ggml_metal_kargs_cpy args = {
|
|
642
|
+
/*.nk0 =*/ ne00,
|
|
643
|
+
/*.ne00 =*/ ne00,
|
|
644
|
+
/*.ne01 =*/ ne01,
|
|
645
|
+
/*.ne02 =*/ ne02,
|
|
646
|
+
/*.ne03 =*/ ne03,
|
|
647
|
+
/*.nb00 =*/ nb00,
|
|
648
|
+
/*.nb01 =*/ nb01,
|
|
649
|
+
/*.nb02 =*/ nb02,
|
|
650
|
+
/*.nb03 =*/ nb03,
|
|
651
|
+
/*.ne0 =*/ ne0,
|
|
652
|
+
/*.ne1 =*/ ne1,
|
|
653
|
+
/*.ne2 =*/ ne2,
|
|
654
|
+
/*.ne3 =*/ ne3,
|
|
655
|
+
/*.nb0 =*/ nb0,
|
|
656
|
+
/*.nb1 =*/ nb1,
|
|
657
|
+
/*.nb2 =*/ nb2,
|
|
658
|
+
/*.nb3 =*/ nb3,
|
|
659
|
+
};
|
|
660
|
+
|
|
661
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
662
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
663
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
664
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2);
|
|
665
|
+
|
|
666
|
+
const int nth = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), ne00);
|
|
667
|
+
|
|
668
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, ne03, nth, 1, 1);
|
|
669
|
+
|
|
670
|
+
ggml_metal_op_concurrency_reset(ctx);
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
ggml_metal_kargs_bin args = {
|
|
674
|
+
/*.ne00 =*/ ne10,
|
|
675
|
+
/*.ne01 =*/ ne11,
|
|
676
|
+
/*.ne02 =*/ ne12,
|
|
677
|
+
/*.ne03 =*/ ne13,
|
|
678
|
+
/*.nb00 =*/ nb00,
|
|
679
|
+
/*.nb01 =*/ pnb1,
|
|
680
|
+
/*.nb02 =*/ pnb2,
|
|
681
|
+
/*.nb03 =*/ pnb3,
|
|
682
|
+
/*.ne10 =*/ ne10,
|
|
683
|
+
/*.ne11 =*/ ne11,
|
|
684
|
+
/*.ne12 =*/ ne12,
|
|
685
|
+
/*.ne13 =*/ ne13,
|
|
686
|
+
/*.nb10 =*/ nb10,
|
|
687
|
+
/*.nb11 =*/ nb11,
|
|
688
|
+
/*.nb12 =*/ nb12,
|
|
689
|
+
/*.nb13 =*/ nb13,
|
|
690
|
+
/*.ne0 =*/ ne10,
|
|
691
|
+
/*.ne1 =*/ ne11,
|
|
692
|
+
/*.ne2 =*/ ne12,
|
|
693
|
+
/*.ne3 =*/ ne13,
|
|
694
|
+
/*.nb0 =*/ nb0,
|
|
695
|
+
/*.nb1 =*/ pnb1,
|
|
696
|
+
/*.nb2 =*/ pnb2,
|
|
697
|
+
/*.nb3 =*/ pnb3,
|
|
698
|
+
/*.offs =*/ offs,
|
|
699
|
+
/*.o1 =*/ { 0 },
|
|
700
|
+
};
|
|
701
|
+
|
|
702
|
+
auto pipeline = ggml_metal_library_get_pipeline_bin_one(lib, GGML_OP_ADD);
|
|
703
|
+
|
|
704
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
705
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
706
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
707
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2);
|
|
708
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3);
|
|
709
|
+
|
|
710
|
+
const int nth_max = MIN(256, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline));
|
|
711
|
+
|
|
712
|
+
int nth = 1;
|
|
713
|
+
|
|
714
|
+
while (2*nth < args.ne0 && nth < nth_max) {
|
|
715
|
+
nth *= 2;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ne11, ne12, ne13, nth, 1, 1);
|
|
719
|
+
|
|
720
|
+
return 1;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
int ggml_metal_op_unary(ggml_metal_op_t ctx, int idx) {
|
|
724
|
+
ggml_tensor * op = ctx->node(idx);
|
|
725
|
+
|
|
726
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
727
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
728
|
+
|
|
729
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
730
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
731
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
732
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
733
|
+
|
|
734
|
+
GGML_ASSERT(ggml_is_contiguous_rows(op->src[0]));
|
|
735
|
+
|
|
736
|
+
ggml_metal_buffer_id bid_src0 = ggml_metal_get_buffer_id(op->src[0]);
|
|
737
|
+
ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op);
|
|
738
|
+
|
|
739
|
+
ggml_metal_kargs_unary args = {
|
|
740
|
+
/*.ne00 =*/ ne00,
|
|
741
|
+
/*.ne01 =*/ ne01,
|
|
742
|
+
/*.ne02 =*/ ne02,
|
|
743
|
+
/*.ne03 =*/ ne03,
|
|
744
|
+
/*.nb00 =*/ nb00,
|
|
745
|
+
/*.nb01 =*/ nb01,
|
|
746
|
+
/*.nb02 =*/ nb02,
|
|
747
|
+
/*.nb03 =*/ nb03,
|
|
748
|
+
/*.ne0 =*/ ne0,
|
|
749
|
+
/*.ne1 =*/ ne1,
|
|
750
|
+
/*.ne2 =*/ ne2,
|
|
751
|
+
/*.ne3 =*/ ne3,
|
|
752
|
+
/*.nb0 =*/ nb0,
|
|
753
|
+
/*.nb1 =*/ nb1,
|
|
754
|
+
/*.nb2 =*/ nb2,
|
|
755
|
+
/*.nb3 =*/ nb3,
|
|
756
|
+
/*.slope =*/ 0.0,
|
|
757
|
+
/*.scale =*/ 0.0,
|
|
758
|
+
/*.bias =*/ 0.0,
|
|
759
|
+
/*.val =*/ 0.0,
|
|
760
|
+
/*.min =*/ 0.0,
|
|
761
|
+
/*.max =*/ 0.0,
|
|
762
|
+
};
|
|
763
|
+
|
|
764
|
+
if (op->op == GGML_OP_LEAKY_RELU) {
|
|
765
|
+
args.slope = ggml_get_op_params_f32(op, 0);
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
if (op->op == GGML_OP_SCALE) {
|
|
769
|
+
args.scale = ggml_get_op_params_f32(op, 0);
|
|
770
|
+
args.bias = ggml_get_op_params_f32(op, 1);
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
if (op->op == GGML_OP_FILL) {
|
|
774
|
+
args.val = ggml_get_op_params_f32(op, 0);
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
if (op->op == GGML_OP_CLAMP) {
|
|
778
|
+
args.min = ggml_get_op_params_f32(op, 0);
|
|
779
|
+
args.max = ggml_get_op_params_f32(op, 1);
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
auto pipeline = ggml_metal_library_get_pipeline_unary(lib, op);
|
|
783
|
+
|
|
784
|
+
if (pipeline.c4) {
|
|
785
|
+
args.ne00 = ne00/4;
|
|
786
|
+
args.ne0 = ne0/4;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
790
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
791
|
+
ggml_metal_encoder_set_buffer (enc, bid_src0, 1);
|
|
792
|
+
ggml_metal_encoder_set_buffer (enc, bid_dst, 2);
|
|
793
|
+
|
|
794
|
+
if (pipeline.cnt) {
|
|
795
|
+
const int n = pipeline.c4 ? ggml_nelements(op)/4 : ggml_nelements(op);
|
|
796
|
+
|
|
797
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, n, 1, 1, 1, 1, 1);
|
|
798
|
+
} else {
|
|
799
|
+
const int nth_max = MIN(256, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline));
|
|
800
|
+
|
|
801
|
+
const int nth = MIN(args.ne00, nth_max);
|
|
802
|
+
|
|
803
|
+
const int nk0 = (args.ne00 + nth - 1)/nth;
|
|
804
|
+
|
|
805
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, nk0*ne01, ne02, ne03, nth, 1, 1);
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
return 1;
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
int ggml_metal_op_glu(ggml_metal_op_t ctx, int idx) {
|
|
812
|
+
ggml_tensor * op = ctx->node(idx);
|
|
813
|
+
|
|
814
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
815
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
816
|
+
|
|
817
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
818
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
819
|
+
GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
|
|
820
|
+
GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb);
|
|
821
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
822
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
823
|
+
|
|
824
|
+
if (op->src[1]) {
|
|
825
|
+
GGML_ASSERT(ggml_are_same_shape(op->src[0], op->src[1]));
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
auto pipeline = ggml_metal_library_get_pipeline_glu(lib, op);
|
|
829
|
+
|
|
830
|
+
const int32_t swp = ggml_get_op_params_i32(op, 1);
|
|
831
|
+
const float alpha = ggml_get_op_params_f32(op, 2);
|
|
832
|
+
const float limit = ggml_get_op_params_f32(op, 3);
|
|
833
|
+
|
|
834
|
+
const int32_t i00 = swp ? ne0 : 0;
|
|
835
|
+
const int32_t i10 = swp ? 0 : ne0;
|
|
836
|
+
|
|
837
|
+
ggml_metal_kargs_glu args = {
|
|
838
|
+
/*.ne00 =*/ ne00,
|
|
839
|
+
/*.nb01 =*/ nb01,
|
|
840
|
+
/*.ne10 =*/ op->src[1] ? ne10 : ne00,
|
|
841
|
+
/*.nb11 =*/ op->src[1] ? nb11 : nb01,
|
|
842
|
+
/*.ne0 =*/ ne0,
|
|
843
|
+
/*.nb1 =*/ nb1,
|
|
844
|
+
/*.i00 =*/ op->src[1] ? 0 : i00,
|
|
845
|
+
/*.i10 =*/ op->src[1] ? 0 : i10,
|
|
846
|
+
/*.alpha=*/ alpha,
|
|
847
|
+
/*.limit=*/ limit
|
|
848
|
+
};
|
|
849
|
+
|
|
850
|
+
const int64_t nrows = ggml_nrows(op->src[0]);
|
|
851
|
+
|
|
852
|
+
const int32_t nth = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), ne00/2);
|
|
853
|
+
|
|
854
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
855
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
856
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
857
|
+
if (op->src[1]) {
|
|
858
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2);
|
|
859
|
+
} else {
|
|
860
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 2);
|
|
861
|
+
}
|
|
862
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3);
|
|
863
|
+
|
|
864
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, nrows, 1, 1, nth, 1, 1);
|
|
865
|
+
|
|
866
|
+
return 1;
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
int ggml_metal_op_sum(ggml_metal_op_t ctx, int idx) {
|
|
870
|
+
ggml_tensor * op = ctx->node(idx);
|
|
871
|
+
|
|
872
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
873
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
874
|
+
|
|
875
|
+
const uint64_t n = (uint64_t) ggml_nelements(op->src[0]);
|
|
876
|
+
|
|
877
|
+
ggml_metal_kargs_sum args = {
|
|
878
|
+
/*.np =*/ n,
|
|
879
|
+
};
|
|
880
|
+
|
|
881
|
+
auto pipeline = ggml_metal_library_get_pipeline_sum(lib, op);
|
|
882
|
+
|
|
883
|
+
int nth = 32; // SIMD width
|
|
884
|
+
|
|
885
|
+
while (nth < (int) n && nth < ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)) {
|
|
886
|
+
nth *= 2;
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
nth = std::min(nth, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline));
|
|
890
|
+
nth = std::min(nth, (int) n);
|
|
891
|
+
|
|
892
|
+
const int nsg = (nth + 31) / 32;
|
|
893
|
+
|
|
894
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
895
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
896
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
897
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2);
|
|
898
|
+
|
|
899
|
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, nsg * sizeof(float), 0);
|
|
900
|
+
|
|
901
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, 1, 1, 1, nth, 1, 1);
|
|
902
|
+
|
|
903
|
+
return 1;
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
int ggml_metal_op_sum_rows(ggml_metal_op_t ctx, int idx) {
|
|
907
|
+
ggml_tensor * op = ctx->node(idx);
|
|
908
|
+
|
|
909
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
910
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
911
|
+
|
|
912
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
913
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
914
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
915
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
916
|
+
|
|
917
|
+
GGML_ASSERT(ggml_is_contiguous_rows(op->src[0]));
|
|
918
|
+
|
|
919
|
+
ggml_metal_buffer_id bid_src0 = ggml_metal_get_buffer_id(op->src[0]);
|
|
920
|
+
ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op);
|
|
921
|
+
|
|
922
|
+
ggml_metal_kargs_sum_rows args = {
|
|
923
|
+
/*.ne00 =*/ ne00,
|
|
924
|
+
/*.ne01 =*/ ne01,
|
|
925
|
+
/*.ne02 =*/ ne02,
|
|
926
|
+
/*.ne03 =*/ ne03,
|
|
927
|
+
/*.nb00 =*/ nb00,
|
|
928
|
+
/*.nb01 =*/ nb01,
|
|
929
|
+
/*.nb02 =*/ nb02,
|
|
930
|
+
/*.nb03 =*/ nb03,
|
|
931
|
+
/*.ne0 =*/ ne0,
|
|
932
|
+
/*.ne1 =*/ ne1,
|
|
933
|
+
/*.ne2 =*/ ne2,
|
|
934
|
+
/*.ne3 =*/ ne3,
|
|
935
|
+
/*.nb0 =*/ nb0,
|
|
936
|
+
/*.nb1 =*/ nb1,
|
|
937
|
+
/*.nb2 =*/ nb2,
|
|
938
|
+
/*.nb3 =*/ nb3,
|
|
939
|
+
};
|
|
940
|
+
|
|
941
|
+
auto pipeline = ggml_metal_library_get_pipeline_sum_rows(lib, op);
|
|
942
|
+
|
|
943
|
+
if (pipeline.c4) {
|
|
944
|
+
args.ne00 = ne00/4;
|
|
945
|
+
args.ne0 = ne0/4;
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
int nth = 32; // SIMD width
|
|
949
|
+
|
|
950
|
+
while (nth < args.ne00 && nth < ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)) {
|
|
951
|
+
nth *= 2;
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
nth = std::min(nth, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline));
|
|
955
|
+
nth = std::min(nth, (int) args.ne00);
|
|
956
|
+
|
|
957
|
+
const size_t smem = pipeline.smem;
|
|
958
|
+
|
|
959
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
960
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
961
|
+
ggml_metal_encoder_set_buffer (enc, bid_src0, 1);
|
|
962
|
+
ggml_metal_encoder_set_buffer (enc, bid_dst, 2);
|
|
963
|
+
|
|
964
|
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0);
|
|
965
|
+
|
|
966
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, ne03, nth, 1, 1);
|
|
967
|
+
|
|
968
|
+
return 1;
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
int ggml_metal_op_cumsum(ggml_metal_op_t ctx, int idx) {
|
|
972
|
+
ggml_tensor * op = ctx->node(idx);
|
|
973
|
+
|
|
974
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
975
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
976
|
+
|
|
977
|
+
GGML_ASSERT(ggml_is_contiguous_rows(op->src[0]));
|
|
978
|
+
|
|
979
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
980
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
981
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
982
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
983
|
+
|
|
984
|
+
auto pipeline_blk = ggml_metal_library_get_pipeline_cumsum_blk(lib, op);
|
|
985
|
+
|
|
986
|
+
int nth = 1;
|
|
987
|
+
while (nth < ne00 && 2*nth <= ggml_metal_pipeline_max_theads_per_threadgroup(pipeline_blk)) {
|
|
988
|
+
nth *= 2;
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
GGML_ASSERT(ne00 <= nth*nth);
|
|
992
|
+
|
|
993
|
+
const int64_t net0 = (ne00 + nth - 1) / nth;
|
|
994
|
+
const int64_t net1 = ne01;
|
|
995
|
+
const int64_t net2 = ne02;
|
|
996
|
+
const int64_t net3 = ne03;
|
|
997
|
+
|
|
998
|
+
const uint64_t nbt0 = sizeof(float);
|
|
999
|
+
const uint64_t nbt1 = net0*nbt0;
|
|
1000
|
+
const uint64_t nbt2 = net1*nbt1;
|
|
1001
|
+
const uint64_t nbt3 = net2*nbt2;
|
|
1002
|
+
|
|
1003
|
+
const size_t smem = GGML_PAD(32*sizeof(float), 16);
|
|
1004
|
+
|
|
1005
|
+
ggml_metal_buffer_id bid_src0 = ggml_metal_get_buffer_id(op->src[0]);
|
|
1006
|
+
ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op);
|
|
1007
|
+
|
|
1008
|
+
ggml_metal_buffer_id bid_tmp = bid_dst;
|
|
1009
|
+
bid_tmp.offs += ggml_nbytes(op);
|
|
1010
|
+
|
|
1011
|
+
{
|
|
1012
|
+
ggml_metal_kargs_cumsum_blk args = {
|
|
1013
|
+
/*.ne00 =*/ ne00,
|
|
1014
|
+
/*.ne01 =*/ ne01,
|
|
1015
|
+
/*.ne02 =*/ ne02,
|
|
1016
|
+
/*.ne03 =*/ ne03,
|
|
1017
|
+
/*.nb00 =*/ nb00,
|
|
1018
|
+
/*.nb01 =*/ nb01,
|
|
1019
|
+
/*.nb02 =*/ nb02,
|
|
1020
|
+
/*.nb03 =*/ nb03,
|
|
1021
|
+
/*.net0 =*/ net0,
|
|
1022
|
+
/*.net1 =*/ net1,
|
|
1023
|
+
/*.net2 =*/ net2,
|
|
1024
|
+
/*.net3 =*/ net3,
|
|
1025
|
+
/*.nbt0 =*/ nbt0,
|
|
1026
|
+
/*.nbt1 =*/ nbt1,
|
|
1027
|
+
/*.nbt2 =*/ nbt2,
|
|
1028
|
+
/*.nbt3 =*/ nbt3,
|
|
1029
|
+
/*.outb =*/ ne00 > nth,
|
|
1030
|
+
};
|
|
1031
|
+
|
|
1032
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline_blk);
|
|
1033
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
1034
|
+
ggml_metal_encoder_set_buffer (enc, bid_src0, 1);
|
|
1035
|
+
ggml_metal_encoder_set_buffer (enc, bid_tmp, 2);
|
|
1036
|
+
ggml_metal_encoder_set_buffer (enc, bid_dst, 3);
|
|
1037
|
+
|
|
1038
|
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0);
|
|
1039
|
+
|
|
1040
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, net0*ne01, ne02, ne03, nth, 1, 1);
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
if (ne00 > nth) {
|
|
1044
|
+
ggml_metal_op_concurrency_reset(ctx);
|
|
1045
|
+
|
|
1046
|
+
{
|
|
1047
|
+
ggml_metal_kargs_cumsum_blk args = {
|
|
1048
|
+
/*.ne00 =*/ net0,
|
|
1049
|
+
/*.ne01 =*/ net1,
|
|
1050
|
+
/*.ne02 =*/ net2,
|
|
1051
|
+
/*.ne03 =*/ net3,
|
|
1052
|
+
/*.nb00 =*/ nbt0,
|
|
1053
|
+
/*.nb01 =*/ nbt1,
|
|
1054
|
+
/*.nb02 =*/ nbt2,
|
|
1055
|
+
/*.nb03 =*/ nbt3,
|
|
1056
|
+
/*.net0 =*/ net0,
|
|
1057
|
+
/*.net1 =*/ net1,
|
|
1058
|
+
/*.net2 =*/ net2,
|
|
1059
|
+
/*.net3 =*/ net3,
|
|
1060
|
+
/*.nbt0 =*/ nbt0,
|
|
1061
|
+
/*.nbt1 =*/ nbt1,
|
|
1062
|
+
/*.nbt2 =*/ nbt2,
|
|
1063
|
+
/*.nbt3 =*/ nbt3,
|
|
1064
|
+
/*.outb =*/ false,
|
|
1065
|
+
};
|
|
1066
|
+
|
|
1067
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline_blk);
|
|
1068
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
1069
|
+
ggml_metal_encoder_set_buffer (enc, bid_tmp, 1);
|
|
1070
|
+
ggml_metal_encoder_set_buffer (enc, bid_tmp, 2);
|
|
1071
|
+
ggml_metal_encoder_set_buffer (enc, bid_tmp, 3);
|
|
1072
|
+
|
|
1073
|
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0);
|
|
1074
|
+
|
|
1075
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, net1, net2, net3, nth, 1, 1);
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
ggml_metal_op_concurrency_reset(ctx);
|
|
1079
|
+
|
|
1080
|
+
{
|
|
1081
|
+
auto pipeline_add = ggml_metal_library_get_pipeline_cumsum_add(lib, op);
|
|
1082
|
+
|
|
1083
|
+
ggml_metal_kargs_cumsum_add args = {
|
|
1084
|
+
/*.ne00 =*/ ne00,
|
|
1085
|
+
/*.ne01 =*/ ne01,
|
|
1086
|
+
/*.ne02 =*/ ne02,
|
|
1087
|
+
/*.ne03 =*/ ne03,
|
|
1088
|
+
/*.nb00 =*/ nb00,
|
|
1089
|
+
/*.nb01 =*/ nb01,
|
|
1090
|
+
/*.nb02 =*/ nb02,
|
|
1091
|
+
/*.nb03 =*/ nb03,
|
|
1092
|
+
/*.net0 =*/ net0,
|
|
1093
|
+
/*.net1 =*/ net1,
|
|
1094
|
+
/*.net2 =*/ net2,
|
|
1095
|
+
/*.net3 =*/ net3,
|
|
1096
|
+
/*.nbt0 =*/ nbt0,
|
|
1097
|
+
/*.nbt1 =*/ nbt1,
|
|
1098
|
+
/*.nbt2 =*/ nbt2,
|
|
1099
|
+
/*.nbt3 =*/ nbt3,
|
|
1100
|
+
};
|
|
1101
|
+
|
|
1102
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline_add);
|
|
1103
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
1104
|
+
ggml_metal_encoder_set_buffer (enc, bid_tmp, 1);
|
|
1105
|
+
ggml_metal_encoder_set_buffer (enc, bid_dst, 2);
|
|
1106
|
+
|
|
1107
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, net0*ne01, ne02, ne03, nth, 1, 1);
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
return 1;
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
int ggml_metal_op_get_rows(ggml_metal_op_t ctx, int idx) {
|
|
1115
|
+
ggml_tensor * op = ctx->node(idx);
|
|
1116
|
+
|
|
1117
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
1118
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
1119
|
+
|
|
1120
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
1121
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
1122
|
+
GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
|
|
1123
|
+
GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb);
|
|
1124
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
1125
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
1126
|
+
|
|
1127
|
+
auto pipeline = ggml_metal_library_get_pipeline_get_rows(lib, op->src[0]->type);
|
|
1128
|
+
|
|
1129
|
+
ggml_metal_kargs_get_rows args = {
|
|
1130
|
+
/*.ne00t =*/ ggml_is_quantized(op->src[0]->type) ? ne00/16 : ne00,
|
|
1131
|
+
/*.ne00 =*/ ne00,
|
|
1132
|
+
/*.nb01 =*/ nb01,
|
|
1133
|
+
/*.nb02 =*/ nb02,
|
|
1134
|
+
/*.nb03 =*/ nb03,
|
|
1135
|
+
/*.ne10 =*/ ne10,
|
|
1136
|
+
/*.nb10 =*/ nb10,
|
|
1137
|
+
/*.nb11 =*/ nb11,
|
|
1138
|
+
/*.nb12 =*/ nb12,
|
|
1139
|
+
/*.nb1 =*/ nb1,
|
|
1140
|
+
/*.nb2 =*/ nb2,
|
|
1141
|
+
/*.nb3 =*/ nb3,
|
|
1142
|
+
};
|
|
1143
|
+
|
|
1144
|
+
const int nth = std::min(args.ne00t, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline));
|
|
1145
|
+
|
|
1146
|
+
const int nw0 = (args.ne00t + nth - 1)/nth;
|
|
1147
|
+
|
|
1148
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
1149
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
1150
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
1151
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2);
|
|
1152
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3);
|
|
1153
|
+
|
|
1154
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, nw0*ne10, ne11, ne12, nth, 1, 1);
|
|
1155
|
+
|
|
1156
|
+
return 1;
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
int ggml_metal_op_set_rows(ggml_metal_op_t ctx, int idx) {
|
|
1160
|
+
ggml_tensor * op = ctx->node(idx);
|
|
1161
|
+
|
|
1162
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
1163
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
1164
|
+
|
|
1165
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
1166
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
1167
|
+
GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
|
|
1168
|
+
GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb);
|
|
1169
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
1170
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
1171
|
+
|
|
1172
|
+
auto pipeline = ggml_metal_library_get_pipeline_set_rows(lib, op->src[1]->type, op->type);
|
|
1173
|
+
|
|
1174
|
+
const int32_t nk0 = ne0/ggml_blck_size(op->type);
|
|
1175
|
+
|
|
1176
|
+
int nth = 32; // SIMD width
|
|
1177
|
+
|
|
1178
|
+
while (nth < nk0 && nth < ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)) {
|
|
1179
|
+
nth *= 2;
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
int nrptg = 1;
|
|
1183
|
+
if (nth > nk0) {
|
|
1184
|
+
nrptg = (nth + nk0 - 1)/nk0;
|
|
1185
|
+
nth = nk0;
|
|
1186
|
+
|
|
1187
|
+
if (nrptg*nth > ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)) {
|
|
1188
|
+
nrptg--;
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
nth = std::min(nth, nk0);
|
|
1193
|
+
|
|
1194
|
+
ggml_metal_kargs_set_rows args = {
|
|
1195
|
+
/*.nk0 =*/ nk0,
|
|
1196
|
+
/*.ne01 =*/ ne01,
|
|
1197
|
+
/*.nb01 =*/ nb01,
|
|
1198
|
+
/*.nb02 =*/ nb02,
|
|
1199
|
+
/*.nb03 =*/ nb03,
|
|
1200
|
+
/*.ne11 =*/ ne11,
|
|
1201
|
+
/*.ne12 =*/ ne12,
|
|
1202
|
+
/*.nb10 =*/ nb10,
|
|
1203
|
+
/*.nb11 =*/ nb11,
|
|
1204
|
+
/*.nb12 =*/ nb12,
|
|
1205
|
+
/*.nb1 =*/ nb1,
|
|
1206
|
+
/*.nb2 =*/ nb2,
|
|
1207
|
+
/*.nb3 =*/ nb3,
|
|
1208
|
+
};
|
|
1209
|
+
|
|
1210
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
1211
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
1212
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
1213
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2);
|
|
1214
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3);
|
|
1215
|
+
|
|
1216
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, (ne01 + nrptg - 1)/nrptg, ne02, ne03, nth, nrptg, 1);
|
|
1217
|
+
|
|
1218
|
+
return 1;
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
int ggml_metal_op_diag(ggml_metal_op_t ctx, int idx) {
|
|
1222
|
+
ggml_tensor * op = ctx->node(idx);
|
|
1223
|
+
|
|
1224
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
1225
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
1226
|
+
|
|
1227
|
+
GGML_TENSOR_LOCALS(int32_t, ne0, op->src[0], ne);
|
|
1228
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
1229
|
+
GGML_TENSOR_LOCALS(int32_t, ne, op, ne);
|
|
1230
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
1231
|
+
|
|
1232
|
+
ggml_metal_kargs_diag args = {
|
|
1233
|
+
/*.ne00 =*/ne00,
|
|
1234
|
+
/*.ne01 =*/ne01,
|
|
1235
|
+
/*.ne02 =*/ne02,
|
|
1236
|
+
/*.ne03 =*/ne03,
|
|
1237
|
+
/*.nb00 =*/nb00,
|
|
1238
|
+
/*.nb01 =*/nb01,
|
|
1239
|
+
/*.nb02 =*/nb02,
|
|
1240
|
+
/*.nb03 =*/nb03,
|
|
1241
|
+
/*.ne0 =*/ne0,
|
|
1242
|
+
/*.ne1 =*/ne1,
|
|
1243
|
+
/*.ne2 =*/ne2,
|
|
1244
|
+
/*.ne3 =*/ne3,
|
|
1245
|
+
/*.nb0 =*/nb0,
|
|
1246
|
+
/*.nb1 =*/nb1,
|
|
1247
|
+
/*.nb2 =*/nb2,
|
|
1248
|
+
/*.nb3 =*/nb3,
|
|
1249
|
+
};
|
|
1250
|
+
|
|
1251
|
+
auto pipeline = ggml_metal_library_get_pipeline_diag(lib, op);
|
|
1252
|
+
|
|
1253
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
1254
|
+
ggml_metal_encoder_set_bytes(enc, &args, sizeof(args), 0);
|
|
1255
|
+
ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
1256
|
+
ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op), 2);
|
|
1257
|
+
|
|
1258
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ne1, ne2, ne3, 32, 1, 1);
|
|
1259
|
+
|
|
1260
|
+
return 1;
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
int ggml_metal_op_soft_max(ggml_metal_op_t ctx, int idx) {
|
|
1264
|
+
ggml_tensor * op = ctx->node(idx);
|
|
1265
|
+
|
|
1266
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
1267
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
1268
|
+
|
|
1269
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
1270
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
1271
|
+
GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
|
|
1272
|
+
GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb);
|
|
1273
|
+
GGML_TENSOR_LOCALS( int32_t, ne2, op->src[2], ne);
|
|
1274
|
+
GGML_TENSOR_LOCALS(uint64_t, nb2, op->src[2], nb);
|
|
1275
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
1276
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
1277
|
+
|
|
1278
|
+
float scale;
|
|
1279
|
+
float max_bias;
|
|
1280
|
+
|
|
1281
|
+
memcpy(&scale, ((const int32_t *) op->op_params) + 0, sizeof(scale));
|
|
1282
|
+
memcpy(&max_bias, ((const int32_t *) op->op_params) + 1, sizeof(max_bias));
|
|
1283
|
+
|
|
1284
|
+
const uint32_t n_head = op->src[0]->ne[2];
|
|
1285
|
+
const int32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head));
|
|
1286
|
+
|
|
1287
|
+
const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
|
|
1288
|
+
const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
|
|
1289
|
+
|
|
1290
|
+
// softmax
|
|
1291
|
+
|
|
1292
|
+
ggml_metal_kargs_soft_max args = {
|
|
1293
|
+
/*.ne00 =*/ ne00,
|
|
1294
|
+
/*.ne01 =*/ ne01,
|
|
1295
|
+
/*.ne02 =*/ ne02,
|
|
1296
|
+
/*.nb01 =*/ nb01,
|
|
1297
|
+
/*.nb02 =*/ nb02,
|
|
1298
|
+
/*.nb03 =*/ nb03,
|
|
1299
|
+
/*.ne11 =*/ ne11,
|
|
1300
|
+
/*.ne12 =*/ ne12,
|
|
1301
|
+
/*.ne13 =*/ ne13,
|
|
1302
|
+
/*.nb11 =*/ nb11,
|
|
1303
|
+
/*.nb12 =*/ nb12,
|
|
1304
|
+
/*.nb13 =*/ nb13,
|
|
1305
|
+
/*.nb1 =*/ nb1,
|
|
1306
|
+
/*.nb2 =*/ nb2,
|
|
1307
|
+
/*.nb3 =*/ nb3,
|
|
1308
|
+
/*.scale =*/ scale,
|
|
1309
|
+
/*.max_bias =*/ max_bias,
|
|
1310
|
+
/*.m0 =*/ m0,
|
|
1311
|
+
/*.m1 =*/ m1,
|
|
1312
|
+
/*.n_head_log2 =*/ n_head_log2,
|
|
1313
|
+
};
|
|
1314
|
+
|
|
1315
|
+
auto pipeline = ggml_metal_library_get_pipeline_soft_max(lib, op);
|
|
1316
|
+
|
|
1317
|
+
int nth = 32; // SIMD width
|
|
1318
|
+
|
|
1319
|
+
if (ne00%4 == 0) {
|
|
1320
|
+
while (nth < ne00/4 && nth*ne01*ne02*ne03 < 256) {
|
|
1321
|
+
nth *= 2;
|
|
1322
|
+
}
|
|
1323
|
+
} else {
|
|
1324
|
+
while (nth < ne00 && nth*ne01*ne02*ne03 < 256) {
|
|
1325
|
+
nth *= 2;
|
|
1326
|
+
}
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1329
|
+
const size_t smem = pipeline.smem;
|
|
1330
|
+
|
|
1331
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
1332
|
+
ggml_metal_encoder_set_bytes(enc, &args, sizeof(args), 0);
|
|
1333
|
+
ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
1334
|
+
if (op->src[1]) {
|
|
1335
|
+
ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[1]), 2);
|
|
1336
|
+
} else {
|
|
1337
|
+
ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[0]), 2);
|
|
1338
|
+
}
|
|
1339
|
+
if (op->src[2]) {
|
|
1340
|
+
ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[2]), 3);
|
|
1341
|
+
} else {
|
|
1342
|
+
ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[0]), 3);
|
|
1343
|
+
}
|
|
1344
|
+
ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op), 4);
|
|
1345
|
+
|
|
1346
|
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0);
|
|
1347
|
+
|
|
1348
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, ne03, nth, 1, 1);
|
|
1349
|
+
|
|
1350
|
+
return 1;
|
|
1351
|
+
}
|
|
1352
|
+
|
|
1353
|
+
int ggml_metal_op_ssm_conv(ggml_metal_op_t ctx, int idx) {
|
|
1354
|
+
ggml_tensor * op = ctx->node(idx);
|
|
1355
|
+
|
|
1356
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
1357
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
1358
|
+
|
|
1359
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
1360
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
1361
|
+
GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
|
|
1362
|
+
GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb);
|
|
1363
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
1364
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
1365
|
+
|
|
1366
|
+
ggml_metal_kargs_ssm_conv args = {
|
|
1367
|
+
/*.ne00 =*/ ne00,
|
|
1368
|
+
/*.ne01 =*/ ne01,
|
|
1369
|
+
/*.ne02 =*/ ne02,
|
|
1370
|
+
/*.nb00 =*/ nb00,
|
|
1371
|
+
/*.nb01 =*/ nb01,
|
|
1372
|
+
/*.nb02 =*/ nb02,
|
|
1373
|
+
/*.ne10 =*/ ne10,
|
|
1374
|
+
/*.ne11 =*/ ne11,
|
|
1375
|
+
/*.nb10 =*/ nb10,
|
|
1376
|
+
/*.nb11 =*/ nb11,
|
|
1377
|
+
/*.ne0 =*/ ne0,
|
|
1378
|
+
/*.ne1 =*/ ne1,
|
|
1379
|
+
/*.ne2 =*/ ne2,
|
|
1380
|
+
/*.nb0 =*/ nb0,
|
|
1381
|
+
/*.nb1 =*/ nb1,
|
|
1382
|
+
/*.nb2 =*/ nb2,
|
|
1383
|
+
};
|
|
1384
|
+
|
|
1385
|
+
// Use batched kernel for prefill (ne1 > 1) to reduce threadgroup dispatch overhead
|
|
1386
|
+
const bool use_batched = (ne1 > 1);
|
|
1387
|
+
|
|
1388
|
+
if (use_batched) {
|
|
1389
|
+
// Determine the smallest power of 2 that's >= ne1, but <= 256
|
|
1390
|
+
int BATCH_SIZE;
|
|
1391
|
+
if (ne1 > 128) BATCH_SIZE = 256;
|
|
1392
|
+
else if (ne1 > 64 ) BATCH_SIZE = 128;
|
|
1393
|
+
else if (ne1 > 32 ) BATCH_SIZE = 64;
|
|
1394
|
+
else if (ne1 > 16 ) BATCH_SIZE = 32;
|
|
1395
|
+
else if (ne1 > 8 ) BATCH_SIZE = 16;
|
|
1396
|
+
else if (ne1 > 4 ) BATCH_SIZE = 8;
|
|
1397
|
+
else BATCH_SIZE = 2;
|
|
1398
|
+
|
|
1399
|
+
auto pipeline = ggml_metal_library_get_pipeline_ssm_conv_batched(lib, op, BATCH_SIZE);
|
|
1400
|
+
|
|
1401
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
1402
|
+
ggml_metal_encoder_set_bytes(enc, &args, sizeof(args), 0);
|
|
1403
|
+
ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
1404
|
+
ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[1]), 2);
|
|
1405
|
+
ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op), 3);
|
|
1406
|
+
|
|
1407
|
+
// Dispatch: ne01 rows, ceil(ne1/BATCH_SIZE) token batches, ne02 sequences
|
|
1408
|
+
// Each threadgroup has BATCH_SIZE threads, each handling one token
|
|
1409
|
+
const int n_token_batches = (ne1 + BATCH_SIZE - 1) / BATCH_SIZE;
|
|
1410
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ne01, n_token_batches, ne02, BATCH_SIZE, 1, 1);
|
|
1411
|
+
} else {
|
|
1412
|
+
auto pipeline = ggml_metal_library_get_pipeline_ssm_conv(lib, op);
|
|
1413
|
+
|
|
1414
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
1415
|
+
ggml_metal_encoder_set_bytes(enc, &args, sizeof(args), 0);
|
|
1416
|
+
ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
1417
|
+
ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[1]), 2);
|
|
1418
|
+
ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op), 3);
|
|
1419
|
+
|
|
1420
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne1, ne02, 1, 1, 1);
|
|
1421
|
+
}
|
|
1422
|
+
|
|
1423
|
+
return 1;
|
|
1424
|
+
}
|
|
1425
|
+
|
|
1426
|
+
int ggml_metal_op_ssm_scan(ggml_metal_op_t ctx, int idx) {
|
|
1427
|
+
ggml_tensor * op = ctx->node(idx);
|
|
1428
|
+
|
|
1429
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
1430
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
1431
|
+
|
|
1432
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
1433
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
1434
|
+
GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
|
|
1435
|
+
GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb);
|
|
1436
|
+
GGML_TENSOR_LOCALS( int32_t, ne2, op->src[2], ne);
|
|
1437
|
+
GGML_TENSOR_LOCALS(uint64_t, nb2, op->src[2], nb);
|
|
1438
|
+
GGML_TENSOR_LOCALS( int32_t, ne3, op->src[3], ne);
|
|
1439
|
+
GGML_TENSOR_LOCALS(uint64_t, nb3, op->src[3], nb);
|
|
1440
|
+
GGML_TENSOR_LOCALS( int32_t, ne4, op->src[4], ne);
|
|
1441
|
+
GGML_TENSOR_LOCALS(uint64_t, nb4, op->src[4], nb);
|
|
1442
|
+
GGML_TENSOR_LOCALS( int32_t, ne5, op->src[5], ne);
|
|
1443
|
+
GGML_TENSOR_LOCALS(uint64_t, nb5, op->src[5], nb);
|
|
1444
|
+
GGML_TENSOR_LOCALS( int32_t, ne6, op->src[6], ne);
|
|
1445
|
+
GGML_TENSOR_LOCALS(uint64_t, nb6, op->src[6], nb);
|
|
1446
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
1447
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
1448
|
+
|
|
1449
|
+
const ggml_tensor * src3 = op->src[3];
|
|
1450
|
+
const ggml_tensor * src4 = op->src[4];
|
|
1451
|
+
const ggml_tensor * src5 = op->src[5];
|
|
1452
|
+
const ggml_tensor * src6 = op->src[6];
|
|
1453
|
+
|
|
1454
|
+
GGML_ASSERT(src3);
|
|
1455
|
+
GGML_ASSERT(src4);
|
|
1456
|
+
GGML_ASSERT(src5);
|
|
1457
|
+
GGML_ASSERT(src6);
|
|
1458
|
+
|
|
1459
|
+
const int64_t d_state = ne00;
|
|
1460
|
+
const int64_t d_inner = ne01;
|
|
1461
|
+
const int64_t n_head = ne02;
|
|
1462
|
+
const int64_t n_group = ne41;
|
|
1463
|
+
const int64_t n_seq_tokens = ne12;
|
|
1464
|
+
const int64_t n_seqs = ne13;
|
|
1465
|
+
|
|
1466
|
+
ggml_metal_kargs_ssm_scan args = {
|
|
1467
|
+
/*.d_state =*/ d_state,
|
|
1468
|
+
/*.d_inner =*/ d_inner,
|
|
1469
|
+
/*.n_head =*/ n_head,
|
|
1470
|
+
/*.n_group =*/ n_group,
|
|
1471
|
+
/*.n_seq_tokens =*/ n_seq_tokens,
|
|
1472
|
+
/*.n_seqs =*/ n_seqs,
|
|
1473
|
+
/*.s_off =*/ ggml_nelements(op->src[1]) * sizeof(float),
|
|
1474
|
+
/*.nb00 =*/ nb00,
|
|
1475
|
+
/*.nb01 =*/ nb01,
|
|
1476
|
+
/*.nb02 =*/ nb02,
|
|
1477
|
+
/*.nb03 =*/ nb03,
|
|
1478
|
+
/*.nb10 =*/ nb10,
|
|
1479
|
+
/*.nb11 =*/ nb11,
|
|
1480
|
+
/*.nb12 =*/ nb12,
|
|
1481
|
+
/*.ns12 =*/ nb12/nb10,
|
|
1482
|
+
/*.nb13 =*/ nb13,
|
|
1483
|
+
/*.nb20 =*/ nb20,
|
|
1484
|
+
/*.nb21 =*/ nb21,
|
|
1485
|
+
/*.ns21 =*/ nb21/nb20,
|
|
1486
|
+
/*.nb22 =*/ nb22,
|
|
1487
|
+
/*.ne30 =*/ ne30,
|
|
1488
|
+
/*.nb31 =*/ nb31,
|
|
1489
|
+
/*.nb41 =*/ nb41,
|
|
1490
|
+
/*.nb42 =*/ nb42,
|
|
1491
|
+
/*.ns42 =*/ nb42/nb40,
|
|
1492
|
+
/*.nb43 =*/ nb43,
|
|
1493
|
+
/*.nb51 =*/ nb51,
|
|
1494
|
+
/*.nb52 =*/ nb52,
|
|
1495
|
+
/*.ns52 =*/ nb52/nb50,
|
|
1496
|
+
/*.nb53 =*/ nb53,
|
|
1497
|
+
/*.nb0 =*/ nb0,
|
|
1498
|
+
};
|
|
1499
|
+
|
|
1500
|
+
auto pipeline = ggml_metal_library_get_pipeline_ssm_scan(lib, op);
|
|
1501
|
+
|
|
1502
|
+
GGML_ASSERT(d_state <= ggml_metal_pipeline_max_theads_per_threadgroup(pipeline));
|
|
1503
|
+
|
|
1504
|
+
const size_t smem = pipeline.smem;
|
|
1505
|
+
|
|
1506
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
1507
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
1508
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
1509
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2);
|
|
1510
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[2]), 3);
|
|
1511
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[3]), 4);
|
|
1512
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[4]), 5);
|
|
1513
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[5]), 6);
|
|
1514
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[6]), 7);
|
|
1515
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 8);
|
|
1516
|
+
|
|
1517
|
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0);
|
|
1518
|
+
|
|
1519
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, d_inner, n_head, n_seqs, d_state, 1, 1);
|
|
1520
|
+
|
|
1521
|
+
return 1;
|
|
1522
|
+
}
|
|
1523
|
+
|
|
1524
|
+
int ggml_metal_op_rwkv(ggml_metal_op_t ctx, int idx) {
|
|
1525
|
+
ggml_tensor * op = ctx->node(idx);
|
|
1526
|
+
|
|
1527
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
1528
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
1529
|
+
|
|
1530
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
1531
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
1532
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
1533
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
1534
|
+
|
|
1535
|
+
const int64_t B = op->op == GGML_OP_RWKV_WKV6 ? op->src[5]->ne[1] : op->src[6]->ne[1];
|
|
1536
|
+
const int64_t T = op->src[0]->ne[2];
|
|
1537
|
+
const int64_t C = op->ne[0];
|
|
1538
|
+
const int64_t H = op->src[0]->ne[1];
|
|
1539
|
+
|
|
1540
|
+
auto pipeline = ggml_metal_library_get_pipeline_rwkv(lib, op);
|
|
1541
|
+
|
|
1542
|
+
int ida = 0;
|
|
1543
|
+
|
|
1544
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
1545
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), ida++);
|
|
1546
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), ida++);
|
|
1547
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[2]), ida++);
|
|
1548
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[3]), ida++);
|
|
1549
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[4]), ida++);
|
|
1550
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[5]), ida++);
|
|
1551
|
+
if (op->op == GGML_OP_RWKV_WKV7) {
|
|
1552
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[6]), ida++);
|
|
1553
|
+
}
|
|
1554
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), ida++);
|
|
1555
|
+
ggml_metal_encoder_set_bytes (enc, (void *) &B, sizeof(B), ida++);
|
|
1556
|
+
ggml_metal_encoder_set_bytes (enc, (void *) &T, sizeof(T), ida++);
|
|
1557
|
+
ggml_metal_encoder_set_bytes (enc, (void *) &C, sizeof(C), ida++);
|
|
1558
|
+
ggml_metal_encoder_set_bytes (enc, (void *) &H, sizeof(H), ida++);
|
|
1559
|
+
|
|
1560
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, B * H, 1, 1, C/H, 1, 1);
|
|
1561
|
+
|
|
1562
|
+
return 1;
|
|
1563
|
+
}
|
|
1564
|
+
|
|
1565
|
+
int ggml_metal_op_solve_tri(ggml_metal_op_t ctx, int idx) {
|
|
1566
|
+
ggml_tensor * op = ctx->node(idx);
|
|
1567
|
+
|
|
1568
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
1569
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
1570
|
+
|
|
1571
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
1572
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
1573
|
+
GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
|
|
1574
|
+
GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb);
|
|
1575
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
1576
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
1577
|
+
|
|
1578
|
+
ggml_metal_kargs_solve_tri args = {
|
|
1579
|
+
/*.ne00 =*/ ne00,
|
|
1580
|
+
/*.ne01 =*/ ne01,
|
|
1581
|
+
/*.ne02 =*/ ne02,
|
|
1582
|
+
/*.ne03 =*/ ne03,
|
|
1583
|
+
/*.nb00 =*/ nb00,
|
|
1584
|
+
/*.nb01 =*/ nb01,
|
|
1585
|
+
/*.nb02 =*/ nb02,
|
|
1586
|
+
/*.nb03 =*/ nb03,
|
|
1587
|
+
/*.ne10 =*/ ne10,
|
|
1588
|
+
/*.ne11 =*/ ne11,
|
|
1589
|
+
/*.ne12 =*/ ne12,
|
|
1590
|
+
/*.ne13 =*/ ne13,
|
|
1591
|
+
/*.nb10 =*/ nb10,
|
|
1592
|
+
/*.nb11 =*/ nb11,
|
|
1593
|
+
/*.nb12 =*/ nb12,
|
|
1594
|
+
/*.nb13 =*/ nb13,
|
|
1595
|
+
/*.ne0 =*/ ne0,
|
|
1596
|
+
/*.ne1 =*/ ne1,
|
|
1597
|
+
/*.ne2 =*/ ne2,
|
|
1598
|
+
/*.ne3 =*/ ne3,
|
|
1599
|
+
/*.nb0 =*/ nb0,
|
|
1600
|
+
/*.nb1 =*/ nb1,
|
|
1601
|
+
/*.nb2 =*/ nb2,
|
|
1602
|
+
/*.nb3 =*/ nb3,
|
|
1603
|
+
};
|
|
1604
|
+
|
|
1605
|
+
auto pipeline = ggml_metal_library_get_pipeline_solve_tri(lib, op);
|
|
1606
|
+
|
|
1607
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
1608
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
1609
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
1610
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2);
|
|
1611
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3);
|
|
1612
|
+
|
|
1613
|
+
const int nsg = pipeline.nsg;
|
|
1614
|
+
|
|
1615
|
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, pipeline.smem, 0);
|
|
1616
|
+
|
|
1617
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, (ne10 + nsg - 1)/nsg, ne02, ne03, 32, nsg, 1);
|
|
1618
|
+
|
|
1619
|
+
return 1;
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
int ggml_metal_op_set(ggml_metal_op_t ctx, int idx) {
|
|
1623
|
+
ggml_tensor * op = ctx->node(idx);
|
|
1624
|
+
|
|
1625
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
1626
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
1627
|
+
|
|
1628
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
1629
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
1630
|
+
GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
|
|
1631
|
+
GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb);
|
|
1632
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
1633
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
1634
|
+
|
|
1635
|
+
ggml_metal_buffer_id bid_src0 = ggml_metal_get_buffer_id(op->src[0]);
|
|
1636
|
+
ggml_metal_buffer_id bid_src1 = ggml_metal_get_buffer_id(op->src[1]);
|
|
1637
|
+
ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op);
|
|
1638
|
+
|
|
1639
|
+
const size_t pnb1 = ((const int32_t *) op->op_params)[0];
|
|
1640
|
+
const size_t pnb2 = ((const int32_t *) op->op_params)[1];
|
|
1641
|
+
const size_t pnb3 = ((const int32_t *) op->op_params)[2];
|
|
1642
|
+
const size_t offs = ((const int32_t *) op->op_params)[3];
|
|
1643
|
+
|
|
1644
|
+
const bool inplace = (bool) ((const int32_t *) op->op_params)[4];
|
|
1645
|
+
|
|
1646
|
+
if (!inplace) {
|
|
1647
|
+
// run a separete kernel to cpy src->dst
|
|
1648
|
+
// not sure how to avoid this
|
|
1649
|
+
// TODO: make a simpler cpy_bytes kernel
|
|
1650
|
+
|
|
1651
|
+
//const id<MTLComputePipelineState> pipeline = ctx->pipelines[GGML_METAL_PIPELINE_TYPE_CPY_F32_F32].obj;
|
|
1652
|
+
auto pipeline = ggml_metal_library_get_pipeline_cpy(lib, op->src[0]->type, op->type);
|
|
1653
|
+
|
|
1654
|
+
ggml_metal_kargs_cpy args = {
|
|
1655
|
+
/*.nk0 =*/ ne00,
|
|
1656
|
+
/*.ne00 =*/ ne00,
|
|
1657
|
+
/*.ne01 =*/ ne01,
|
|
1658
|
+
/*.ne02 =*/ ne02,
|
|
1659
|
+
/*.ne03 =*/ ne03,
|
|
1660
|
+
/*.nb00 =*/ nb00,
|
|
1661
|
+
/*.nb01 =*/ nb01,
|
|
1662
|
+
/*.nb02 =*/ nb02,
|
|
1663
|
+
/*.nb03 =*/ nb03,
|
|
1664
|
+
/*.ne0 =*/ ne0,
|
|
1665
|
+
/*.ne1 =*/ ne1,
|
|
1666
|
+
/*.ne2 =*/ ne2,
|
|
1667
|
+
/*.ne3 =*/ ne3,
|
|
1668
|
+
/*.nb0 =*/ nb0,
|
|
1669
|
+
/*.nb1 =*/ nb1,
|
|
1670
|
+
/*.nb2 =*/ nb2,
|
|
1671
|
+
/*.nb3 =*/ nb3,
|
|
1672
|
+
};
|
|
1673
|
+
|
|
1674
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
1675
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
1676
|
+
ggml_metal_encoder_set_buffer (enc, bid_src0, 1);
|
|
1677
|
+
ggml_metal_encoder_set_buffer (enc, bid_dst, 2);
|
|
1678
|
+
|
|
1679
|
+
const int nth = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), ne00);
|
|
1680
|
+
|
|
1681
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, ne03, nth, 1, 1);
|
|
1682
|
+
|
|
1683
|
+
ggml_metal_op_concurrency_reset(ctx);
|
|
1684
|
+
}
|
|
1685
|
+
|
|
1686
|
+
auto pipeline = ggml_metal_library_get_pipeline_cpy(lib, op->src[1]->type, op->type);
|
|
1687
|
+
|
|
1688
|
+
GGML_ASSERT(ne10 % ggml_blck_size(op->src[1]->type) == 0);
|
|
1689
|
+
|
|
1690
|
+
int64_t nk0 = ne10;
|
|
1691
|
+
if (ggml_is_quantized(op->src[1]->type)) {
|
|
1692
|
+
nk0 = ne10/16;
|
|
1693
|
+
} else if (ggml_is_quantized(op->type)) {
|
|
1694
|
+
nk0 = ne10/ggml_blck_size(op->type);
|
|
1695
|
+
}
|
|
1696
|
+
|
|
1697
|
+
int nth = std::min<int>(nk0, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline));
|
|
1698
|
+
|
|
1699
|
+
// when rows are small, we can batch them together in a single threadgroup
|
|
1700
|
+
int nrptg = 1;
|
|
1701
|
+
|
|
1702
|
+
// TODO: relax this constraint in the future
|
|
1703
|
+
if (ggml_blck_size(op->src[1]->type) == 1 && ggml_blck_size(op->type) == 1) {
|
|
1704
|
+
if (nth > nk0) {
|
|
1705
|
+
nrptg = (nth + nk0 - 1)/nk0;
|
|
1706
|
+
nth = nk0;
|
|
1707
|
+
|
|
1708
|
+
if (nrptg*nth > ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)) {
|
|
1709
|
+
nrptg--;
|
|
1710
|
+
}
|
|
1711
|
+
}
|
|
1712
|
+
}
|
|
1713
|
+
|
|
1714
|
+
nth = std::min<int>(nth, nk0);
|
|
1715
|
+
|
|
1716
|
+
ggml_metal_kargs_cpy args = {
|
|
1717
|
+
/*.nk0 =*/ nk0,
|
|
1718
|
+
/*.ne00 =*/ ne10,
|
|
1719
|
+
/*.ne01 =*/ ne11,
|
|
1720
|
+
/*.ne02 =*/ ne12,
|
|
1721
|
+
/*.ne03 =*/ ne13,
|
|
1722
|
+
/*.nb00 =*/ nb10,
|
|
1723
|
+
/*.nb01 =*/ nb11,
|
|
1724
|
+
/*.nb02 =*/ nb12,
|
|
1725
|
+
/*.nb03 =*/ nb13,
|
|
1726
|
+
/*.ne0 =*/ ne10,
|
|
1727
|
+
/*.ne1 =*/ ne11,
|
|
1728
|
+
/*.ne2 =*/ ne12,
|
|
1729
|
+
/*.ne3 =*/ ne13,
|
|
1730
|
+
/*.nb0 =*/ ggml_element_size(op),
|
|
1731
|
+
/*.nb1 =*/ pnb1,
|
|
1732
|
+
/*.nb2 =*/ pnb2,
|
|
1733
|
+
/*.nb3 =*/ pnb3,
|
|
1734
|
+
};
|
|
1735
|
+
|
|
1736
|
+
const int nw0 = nrptg == 1 ? (nk0 + nth - 1)/nth : 1;
|
|
1737
|
+
|
|
1738
|
+
bid_dst.offs += offs;
|
|
1739
|
+
|
|
1740
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
1741
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
1742
|
+
ggml_metal_encoder_set_buffer (enc, bid_src1, 1);
|
|
1743
|
+
ggml_metal_encoder_set_buffer (enc, bid_dst, 2);
|
|
1744
|
+
|
|
1745
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, nw0*(ne11 + nrptg - 1)/nrptg, ne12, ne13, nth, nrptg, 1);
|
|
1746
|
+
|
|
1747
|
+
return 1;
|
|
1748
|
+
}
|
|
1749
|
+
|
|
1750
|
+
int ggml_metal_op_cpy(ggml_metal_op_t ctx, int idx) {
|
|
1751
|
+
ggml_tensor * op = ctx->node(idx);
|
|
1752
|
+
|
|
1753
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
1754
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
1755
|
+
|
|
1756
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
1757
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
1758
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
1759
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
1760
|
+
|
|
1761
|
+
auto pipeline = ggml_metal_library_get_pipeline_cpy(lib, op->src[0]->type, op->type);
|
|
1762
|
+
|
|
1763
|
+
GGML_ASSERT(ne00 % ggml_blck_size(op->src[0]->type) == 0);
|
|
1764
|
+
|
|
1765
|
+
int64_t nk0 = ne00;
|
|
1766
|
+
if (ggml_is_quantized(op->src[0]->type)) {
|
|
1767
|
+
nk0 = ne00/16;
|
|
1768
|
+
} else if (ggml_is_quantized(op->type)) {
|
|
1769
|
+
nk0 = ne00/ggml_blck_size(op->type);
|
|
1770
|
+
}
|
|
1771
|
+
|
|
1772
|
+
int nth = std::min<int>(nk0, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline));
|
|
1773
|
+
|
|
1774
|
+
// when rows are small, we can batch them together in a single threadgroup
|
|
1775
|
+
int nrptg = 1;
|
|
1776
|
+
|
|
1777
|
+
// TODO: relax this constraint in the future
|
|
1778
|
+
if (ggml_blck_size(op->src[0]->type) == 1 && ggml_blck_size(op->type) == 1) {
|
|
1779
|
+
if (nth > nk0) {
|
|
1780
|
+
nrptg = (nth + nk0 - 1)/nk0;
|
|
1781
|
+
nth = nk0;
|
|
1782
|
+
|
|
1783
|
+
if (nrptg*nth > ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)) {
|
|
1784
|
+
nrptg--;
|
|
1785
|
+
}
|
|
1786
|
+
}
|
|
1787
|
+
}
|
|
1788
|
+
|
|
1789
|
+
nth = std::min<int>(nth, nk0);
|
|
1790
|
+
|
|
1791
|
+
ggml_metal_kargs_cpy args = {
|
|
1792
|
+
/*.nk0 =*/ nk0,
|
|
1793
|
+
/*.ne00 =*/ ne00,
|
|
1794
|
+
/*.ne01 =*/ ne01,
|
|
1795
|
+
/*.ne02 =*/ ne02,
|
|
1796
|
+
/*.ne03 =*/ ne03,
|
|
1797
|
+
/*.nb00 =*/ nb00,
|
|
1798
|
+
/*.nb01 =*/ nb01,
|
|
1799
|
+
/*.nb02 =*/ nb02,
|
|
1800
|
+
/*.nb03 =*/ nb03,
|
|
1801
|
+
/*.ne0 =*/ ne0,
|
|
1802
|
+
/*.ne1 =*/ ne1,
|
|
1803
|
+
/*.ne2 =*/ ne2,
|
|
1804
|
+
/*.ne3 =*/ ne3,
|
|
1805
|
+
/*.nb0 =*/ nb0,
|
|
1806
|
+
/*.nb1 =*/ nb1,
|
|
1807
|
+
/*.nb2 =*/ nb2,
|
|
1808
|
+
/*.nb3 =*/ nb3,
|
|
1809
|
+
};
|
|
1810
|
+
|
|
1811
|
+
const int nw0 = nrptg == 1 ? (nk0 + nth - 1)/nth : 1;
|
|
1812
|
+
|
|
1813
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
1814
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
1815
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
1816
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2);
|
|
1817
|
+
|
|
1818
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, nw0*(ne01 + nrptg - 1)/nrptg, ne02, ne03, nth, nrptg, 1);
|
|
1819
|
+
|
|
1820
|
+
return 1;
|
|
1821
|
+
}
|
|
1822
|
+
|
|
1823
|
+
int ggml_metal_op_pool_1d(ggml_metal_op_t ctx, int idx) {
|
|
1824
|
+
ggml_tensor * op = ctx->node(idx);
|
|
1825
|
+
|
|
1826
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
1827
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
1828
|
+
|
|
1829
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
1830
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
1831
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
1832
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
1833
|
+
|
|
1834
|
+
const int32_t * opts = op->op_params;
|
|
1835
|
+
ggml_op_pool op_pool = (ggml_op_pool) opts[0];
|
|
1836
|
+
|
|
1837
|
+
const int32_t k0 = opts[1];
|
|
1838
|
+
const int32_t s0 = opts[2];
|
|
1839
|
+
const int32_t p0 = opts[3];
|
|
1840
|
+
|
|
1841
|
+
const int64_t IW = op->src[0]->ne[0];
|
|
1842
|
+
const int64_t OW = op->ne[0];
|
|
1843
|
+
|
|
1844
|
+
const int64_t np = ggml_nelements(op);
|
|
1845
|
+
|
|
1846
|
+
ggml_metal_kargs_pool_1d args_pool_1d = {
|
|
1847
|
+
/* .k0 = */ k0,
|
|
1848
|
+
/* .s0 = */ s0,
|
|
1849
|
+
/* .p0 = */ p0,
|
|
1850
|
+
/* .IW = */ IW,
|
|
1851
|
+
/* .OW = */ OW,
|
|
1852
|
+
/* .np = */ np
|
|
1853
|
+
};
|
|
1854
|
+
|
|
1855
|
+
auto pipeline = ggml_metal_library_get_pipeline_pool_1d(lib, op, op_pool);
|
|
1856
|
+
|
|
1857
|
+
const int nth = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), (int) np);
|
|
1858
|
+
const int ntg = (np + nth - 1) / nth;
|
|
1859
|
+
|
|
1860
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
1861
|
+
ggml_metal_encoder_set_bytes (enc, &args_pool_1d, sizeof(args_pool_1d), 0);
|
|
1862
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
1863
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2);
|
|
1864
|
+
|
|
1865
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ntg, 1, 1, nth, 1, 1);
|
|
1866
|
+
|
|
1867
|
+
return 1;
|
|
1868
|
+
}
|
|
1869
|
+
|
|
1870
|
+
|
|
1871
|
+
int ggml_metal_op_pool_2d(ggml_metal_op_t ctx, int idx) {
|
|
1872
|
+
ggml_tensor * op = ctx->node(idx);
|
|
1873
|
+
|
|
1874
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
1875
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
1876
|
+
|
|
1877
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
1878
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
1879
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
1880
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
1881
|
+
|
|
1882
|
+
const int32_t * opts = op->op_params;
|
|
1883
|
+
ggml_op_pool op_pool = (ggml_op_pool) opts[0];
|
|
1884
|
+
|
|
1885
|
+
const int32_t k0 = opts[1];
|
|
1886
|
+
const int32_t k1 = opts[2];
|
|
1887
|
+
const int32_t s0 = opts[3];
|
|
1888
|
+
const int32_t s1 = opts[4];
|
|
1889
|
+
const int32_t p0 = opts[5];
|
|
1890
|
+
const int32_t p1 = opts[6];
|
|
1891
|
+
|
|
1892
|
+
const int64_t IH = op->src[0]->ne[1];
|
|
1893
|
+
const int64_t IW = op->src[0]->ne[0];
|
|
1894
|
+
|
|
1895
|
+
const int64_t N = op->ne[3];
|
|
1896
|
+
const int64_t OC = op->ne[2];
|
|
1897
|
+
const int64_t OH = op->ne[1];
|
|
1898
|
+
const int64_t OW = op->ne[0];
|
|
1899
|
+
|
|
1900
|
+
const int64_t np = N * OC * OH * OW;
|
|
1901
|
+
|
|
1902
|
+
ggml_metal_kargs_pool_2d args_pool_2d = {
|
|
1903
|
+
/* .k0 = */ k0,
|
|
1904
|
+
/* .k1 = */ k1,
|
|
1905
|
+
/* .s0 = */ s0,
|
|
1906
|
+
/* .s1 = */ s1,
|
|
1907
|
+
/* .p0 = */ p0,
|
|
1908
|
+
/* .p1 = */ p1,
|
|
1909
|
+
/* .IH = */ IH,
|
|
1910
|
+
/* .IW = */ IW,
|
|
1911
|
+
/* .OH = */ OH,
|
|
1912
|
+
/* .OW = */ OW,
|
|
1913
|
+
/* .np = */ np
|
|
1914
|
+
};
|
|
1915
|
+
|
|
1916
|
+
auto pipeline = ggml_metal_library_get_pipeline_pool_2d(lib, op, op_pool);
|
|
1917
|
+
|
|
1918
|
+
const int nth = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), (int) np);
|
|
1919
|
+
const int ntg = (np + nth - 1) / nth;
|
|
1920
|
+
|
|
1921
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
1922
|
+
ggml_metal_encoder_set_bytes (enc, &args_pool_2d, sizeof(args_pool_2d), 0);
|
|
1923
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
1924
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2);
|
|
1925
|
+
|
|
1926
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ntg, 1, 1, nth, 1, 1);
|
|
1927
|
+
|
|
1928
|
+
return 1;
|
|
1929
|
+
}
|
|
1930
|
+
|
|
1931
|
+
int ggml_metal_op_mul_mat(ggml_metal_op_t ctx, int idx) {
|
|
1932
|
+
ggml_tensor * op = ctx->node(idx);
|
|
1933
|
+
|
|
1934
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
1935
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
1936
|
+
|
|
1937
|
+
const ggml_metal_device_props * props_dev = ggml_metal_device_get_props(ctx->dev);
|
|
1938
|
+
|
|
1939
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
1940
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
1941
|
+
GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
|
|
1942
|
+
GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb);
|
|
1943
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
1944
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
1945
|
+
|
|
1946
|
+
GGML_ASSERT(ne00 == ne10);
|
|
1947
|
+
|
|
1948
|
+
GGML_ASSERT(ne12 % ne02 == 0);
|
|
1949
|
+
GGML_ASSERT(ne13 % ne03 == 0);
|
|
1950
|
+
|
|
1951
|
+
const int16_t r2 = ne12/ne02;
|
|
1952
|
+
const int16_t r3 = ne13/ne03;
|
|
1953
|
+
|
|
1954
|
+
// find the break-even point where the matrix-matrix kernel becomes more efficient compared
|
|
1955
|
+
// to the matrix-vector kernel
|
|
1956
|
+
const int ne11_mm_min = 8;
|
|
1957
|
+
|
|
1958
|
+
// first try to use small-batch mat-mv kernels
|
|
1959
|
+
// these should be efficient for BS [2, ~8]
|
|
1960
|
+
if (op->src[1]->type == GGML_TYPE_F32 && (ne00%128 == 0) &&
|
|
1961
|
+
(
|
|
1962
|
+
(
|
|
1963
|
+
(
|
|
1964
|
+
op->src[0]->type == GGML_TYPE_F32 || // TODO: helper function
|
|
1965
|
+
op->src[0]->type == GGML_TYPE_F16 ||
|
|
1966
|
+
op->src[0]->type == GGML_TYPE_Q4_0 ||
|
|
1967
|
+
op->src[0]->type == GGML_TYPE_Q4_1 ||
|
|
1968
|
+
op->src[0]->type == GGML_TYPE_Q5_0 ||
|
|
1969
|
+
op->src[0]->type == GGML_TYPE_Q5_1 ||
|
|
1970
|
+
op->src[0]->type == GGML_TYPE_Q8_0 ||
|
|
1971
|
+
op->src[0]->type == GGML_TYPE_MXFP4 ||
|
|
1972
|
+
op->src[0]->type == GGML_TYPE_IQ4_NL ||
|
|
1973
|
+
false) && (ne11 >= 2 && ne11 <= 8)
|
|
1974
|
+
) ||
|
|
1975
|
+
(
|
|
1976
|
+
(
|
|
1977
|
+
op->src[0]->type == GGML_TYPE_Q4_K ||
|
|
1978
|
+
op->src[0]->type == GGML_TYPE_Q5_K ||
|
|
1979
|
+
op->src[0]->type == GGML_TYPE_Q6_K ||
|
|
1980
|
+
false) && (ne11 >= 4 && ne11 <= 8)
|
|
1981
|
+
)
|
|
1982
|
+
)
|
|
1983
|
+
) {
|
|
1984
|
+
// TODO: determine the optimal parameters based on grid utilization
|
|
1985
|
+
// I still don't know why we should not always use the maximum available threads:
|
|
1986
|
+
//
|
|
1987
|
+
// nsg = pipeline.maxTotalThreadsPerThreadgroup / 32
|
|
1988
|
+
//
|
|
1989
|
+
// my current hypothesis is that the work grid is not evenly divisible for different nsg
|
|
1990
|
+
// values and there can be some tail effects when nsg is high. need to confirm this
|
|
1991
|
+
//
|
|
1992
|
+
const int nsg = 2; // num simdgroups per threadgroup
|
|
1993
|
+
|
|
1994
|
+
// num threads along row per simdgroup
|
|
1995
|
+
int16_t nxpsg = 0;
|
|
1996
|
+
if (ne00 % 256 == 0 && ne11 < 3) {
|
|
1997
|
+
nxpsg = 16;
|
|
1998
|
+
} else if (ne00 % 128 == 0) {
|
|
1999
|
+
nxpsg = 8;
|
|
2000
|
+
} else {
|
|
2001
|
+
nxpsg = 4;
|
|
2002
|
+
}
|
|
2003
|
+
|
|
2004
|
+
const int16_t nypsg = 32/nxpsg; // num threads along col per simdgroup (i.e. a simdgroup processes that many src0 rows at a time)
|
|
2005
|
+
const int16_t r0ptg = nypsg*nsg; // num src0 rows per threadgroup
|
|
2006
|
+
int16_t r1ptg = 4; // num src1 rows per threadgroup
|
|
2007
|
+
|
|
2008
|
+
// note: not sure how optimal are those across all different hardware. there might be someting cleverer
|
|
2009
|
+
switch (ne11) {
|
|
2010
|
+
case 2:
|
|
2011
|
+
r1ptg = 2; break;
|
|
2012
|
+
case 3:
|
|
2013
|
+
case 6:
|
|
2014
|
+
r1ptg = 3; break;
|
|
2015
|
+
case 4:
|
|
2016
|
+
case 7:
|
|
2017
|
+
case 8:
|
|
2018
|
+
r1ptg = 4; break;
|
|
2019
|
+
case 5:
|
|
2020
|
+
r1ptg = 5; break;
|
|
2021
|
+
default:
|
|
2022
|
+
GGML_ABORT("unsupported ne11");
|
|
2023
|
+
};
|
|
2024
|
+
|
|
2025
|
+
auto pipeline = ggml_metal_library_get_pipeline_mul_mv_ext(lib, op->src[0]->type, op->src[1]->type, nsg, nxpsg, r1ptg);
|
|
2026
|
+
|
|
2027
|
+
ggml_metal_kargs_mul_mv_ext args = {
|
|
2028
|
+
/*.ne00 =*/ ne00,
|
|
2029
|
+
/*.ne01 =*/ ne01,
|
|
2030
|
+
/*.ne02 =*/ ne02,
|
|
2031
|
+
/*.nb00 =*/ nb00,
|
|
2032
|
+
/*.nb01 =*/ nb01,
|
|
2033
|
+
/*.nb02 =*/ nb02,
|
|
2034
|
+
/*.nb03 =*/ nb03,
|
|
2035
|
+
/*.ne10 =*/ ne10,
|
|
2036
|
+
/*.ne11 =*/ ne11,
|
|
2037
|
+
/*.ne12 =*/ ne12,
|
|
2038
|
+
/*.nb10 =*/ nb10,
|
|
2039
|
+
/*.nb11 =*/ nb11,
|
|
2040
|
+
/*.nb12 =*/ nb12,
|
|
2041
|
+
/*.nb13 =*/ nb13,
|
|
2042
|
+
/*.ne0 =*/ ne0,
|
|
2043
|
+
/*.ne1 =*/ ne1,
|
|
2044
|
+
/*.r2 =*/ r2,
|
|
2045
|
+
/*.r3 =*/ r3,
|
|
2046
|
+
};
|
|
2047
|
+
|
|
2048
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
2049
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
2050
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
2051
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2);
|
|
2052
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3);
|
|
2053
|
+
|
|
2054
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ((ne01 + r0ptg - 1)/r0ptg), ((ne11 + r1ptg - 1)/r1ptg), ne12*ne13, 32, nsg, 1);
|
|
2055
|
+
} else if (
|
|
2056
|
+
!ggml_is_transposed(op->src[0]) &&
|
|
2057
|
+
!ggml_is_transposed(op->src[1]) &&
|
|
2058
|
+
// for now the matrix-matrix multiplication kernel only works on A14+/M1+ SoCs
|
|
2059
|
+
// AMD GPU and older A-chips will reuse matrix-vector multiplication kernel
|
|
2060
|
+
props_dev->has_simdgroup_mm && ne00 >= 64 && ne11 > ne11_mm_min) {
|
|
2061
|
+
//GGML_LOG_INFO("matrix: ne00 = %6d, ne01 = %6d, ne02 = %6d, ne11 = %6d, ne12 = %6d\n", ne00, ne01, ne02, ne11, ne12);
|
|
2062
|
+
|
|
2063
|
+
// some Metal matrix data types require aligned pointers
|
|
2064
|
+
// ref: https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf (Table 2.5)
|
|
2065
|
+
//switch (op->src[0]->type) {
|
|
2066
|
+
// case GGML_TYPE_F32: GGML_ASSERT(nb01 % 16 == 0); break;
|
|
2067
|
+
// case GGML_TYPE_F16: GGML_ASSERT(nb01 % 8 == 0); break;
|
|
2068
|
+
// case GGML_TYPE_BF16: GGML_ASSERT(nb01 % 8 == 0); break;
|
|
2069
|
+
// default: break;
|
|
2070
|
+
//}
|
|
2071
|
+
|
|
2072
|
+
auto pipeline = ggml_metal_library_get_pipeline_mul_mm(lib, op);
|
|
2073
|
+
|
|
2074
|
+
ggml_metal_kargs_mul_mm args = {
|
|
2075
|
+
/*.ne00 =*/ ne00,
|
|
2076
|
+
/*.ne02 =*/ ne02,
|
|
2077
|
+
/*.nb01 =*/ nb01,
|
|
2078
|
+
/*.nb02 =*/ nb02,
|
|
2079
|
+
/*.nb03 =*/ nb03,
|
|
2080
|
+
/*.ne12 =*/ ne12,
|
|
2081
|
+
/*.nb10 =*/ nb10,
|
|
2082
|
+
/*.nb11 =*/ nb11,
|
|
2083
|
+
/*.nb12 =*/ nb12,
|
|
2084
|
+
/*.nb13 =*/ nb13,
|
|
2085
|
+
/*.ne0 =*/ ne0,
|
|
2086
|
+
/*.ne1 =*/ ne1,
|
|
2087
|
+
/*.r2 =*/ r2,
|
|
2088
|
+
/*.r3 =*/ r3,
|
|
2089
|
+
};
|
|
2090
|
+
|
|
2091
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
2092
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
2093
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
2094
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2);
|
|
2095
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3);
|
|
2096
|
+
|
|
2097
|
+
const size_t smem = pipeline.smem;
|
|
2098
|
+
|
|
2099
|
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0);
|
|
2100
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ((ne11 + 31)/32), ((ne01 + 63)/64), ne12*ne13, 128, 1, 1);
|
|
2101
|
+
} else {
|
|
2102
|
+
auto pipeline = ggml_metal_library_get_pipeline_mul_mv(lib, op);
|
|
2103
|
+
|
|
2104
|
+
const int nr0 = pipeline.nr0;
|
|
2105
|
+
const int nr1 = pipeline.nr1;
|
|
2106
|
+
const int nsg = pipeline.nsg;
|
|
2107
|
+
|
|
2108
|
+
const size_t smem = pipeline.smem;
|
|
2109
|
+
|
|
2110
|
+
ggml_metal_kargs_mul_mv args = {
|
|
2111
|
+
/*.ne00 =*/ ne00,
|
|
2112
|
+
/*.ne01 =*/ ne01,
|
|
2113
|
+
/*.ne02 =*/ ne02,
|
|
2114
|
+
/*.nb00 =*/ nb00,
|
|
2115
|
+
/*.nb01 =*/ nb01,
|
|
2116
|
+
/*.nb02 =*/ nb02,
|
|
2117
|
+
/*.nb03 =*/ nb03,
|
|
2118
|
+
/*.ne10 =*/ ne10,
|
|
2119
|
+
/*.ne11 =*/ ne11,
|
|
2120
|
+
/*.ne12 =*/ ne12,
|
|
2121
|
+
/*.nb10 =*/ nb10,
|
|
2122
|
+
/*.nb11 =*/ nb11,
|
|
2123
|
+
/*.nb12 =*/ nb12,
|
|
2124
|
+
/*.nb13 =*/ nb13,
|
|
2125
|
+
/*.ne0 =*/ ne0,
|
|
2126
|
+
/*.ne1 =*/ ne1,
|
|
2127
|
+
/*.nr0 =*/ nr0,
|
|
2128
|
+
/*.r2 =*/ r2,
|
|
2129
|
+
/*.r3 =*/ r3,
|
|
2130
|
+
};
|
|
2131
|
+
|
|
2132
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
2133
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
2134
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
2135
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2);
|
|
2136
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3);
|
|
2137
|
+
|
|
2138
|
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0);
|
|
2139
|
+
|
|
2140
|
+
if (op->src[0]->type == GGML_TYPE_F32 ||
|
|
2141
|
+
op->src[0]->type == GGML_TYPE_F16 ||
|
|
2142
|
+
op->src[0]->type == GGML_TYPE_BF16 ||
|
|
2143
|
+
op->src[0]->type == GGML_TYPE_Q8_0) {
|
|
2144
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ((ne01 + nr0 - 1)/(nr0)), ((ne11 + nr1 - 1)/nr1), ne12*ne13, 32, nsg, 1);
|
|
2145
|
+
} else {
|
|
2146
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ((ne01 + nr0*nsg - 1)/(nr0*nsg)), ((ne11 + nr1 - 1)/nr1), ne12*ne13, 32, nsg, 1);
|
|
2147
|
+
}
|
|
2148
|
+
}
|
|
2149
|
+
|
|
2150
|
+
return 1;
|
|
2151
|
+
}
|
|
2152
|
+
|
|
2153
|
+
size_t ggml_metal_op_mul_mat_id_extra_tpe(const ggml_tensor * op) {
|
|
2154
|
+
assert(op->op == GGML_OP_MUL_MAT_ID);
|
|
2155
|
+
|
|
2156
|
+
const int64_t ne02 = op->src[0]->ne[2]; // n_expert
|
|
2157
|
+
|
|
2158
|
+
return ggml_type_size(GGML_TYPE_I32)*ne02;
|
|
2159
|
+
}
|
|
2160
|
+
|
|
2161
|
+
size_t ggml_metal_op_mul_mat_id_extra_ids(const ggml_tensor * op) {
|
|
2162
|
+
assert(op->op == GGML_OP_MUL_MAT_ID);
|
|
2163
|
+
|
|
2164
|
+
const int64_t ne02 = op->src[0]->ne[2]; // n_expert
|
|
2165
|
+
const int64_t ne21 = op->src[2]->ne[1]; // n_token
|
|
2166
|
+
|
|
2167
|
+
return ggml_type_size(GGML_TYPE_I32)*ne02*ne21;
|
|
2168
|
+
}
|
|
2169
|
+
|
|
2170
|
+
int ggml_metal_op_mul_mat_id(ggml_metal_op_t ctx, int idx) {
|
|
2171
|
+
ggml_tensor * op = ctx->node(idx);
|
|
2172
|
+
|
|
2173
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
2174
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
2175
|
+
|
|
2176
|
+
const ggml_metal_device_props * props_dev = ggml_metal_device_get_props(ctx->dev);
|
|
2177
|
+
|
|
2178
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
2179
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
2180
|
+
GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
|
|
2181
|
+
GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb);
|
|
2182
|
+
GGML_TENSOR_LOCALS( int32_t, ne2, op->src[2], ne);
|
|
2183
|
+
GGML_TENSOR_LOCALS(uint64_t, nb2, op->src[2], nb);
|
|
2184
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
2185
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
2186
|
+
|
|
2187
|
+
// src2 = ids
|
|
2188
|
+
GGML_ASSERT(op->src[2]->type == GGML_TYPE_I32);
|
|
2189
|
+
|
|
2190
|
+
GGML_ASSERT(!ggml_is_transposed(op->src[0]));
|
|
2191
|
+
GGML_ASSERT(!ggml_is_transposed(op->src[1]));
|
|
2192
|
+
|
|
2193
|
+
GGML_ASSERT(ne03 == 1);
|
|
2194
|
+
GGML_ASSERT(ne13 == 1);
|
|
2195
|
+
|
|
2196
|
+
ggml_metal_buffer_id bid_src0 = ggml_metal_get_buffer_id(op->src[0]);
|
|
2197
|
+
ggml_metal_buffer_id bid_src1 = ggml_metal_get_buffer_id(op->src[1]);
|
|
2198
|
+
ggml_metal_buffer_id bid_src2 = ggml_metal_get_buffer_id(op->src[2]);
|
|
2199
|
+
ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op);
|
|
2200
|
+
|
|
2201
|
+
const uint32_t r2 = 1;
|
|
2202
|
+
const uint32_t r3 = 1;
|
|
2203
|
+
|
|
2204
|
+
// find the break-even point where the matrix-matrix kernel becomes more efficient compared
|
|
2205
|
+
// to the matrix-vector kernel
|
|
2206
|
+
// ne20 = n_used_experts
|
|
2207
|
+
// ne21 = n_rows (batch size)
|
|
2208
|
+
const int ne21_mm_id_min = 32;
|
|
2209
|
+
|
|
2210
|
+
if (props_dev->has_simdgroup_mm && ne00 >= 64 && (ne21 >= ne21_mm_id_min)) {
|
|
2211
|
+
// some Metal matrix data types require aligned pointers
|
|
2212
|
+
// ref: https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf (Table 2.5)
|
|
2213
|
+
//switch (op->src[0]->type) {
|
|
2214
|
+
// case GGML_TYPE_F32: GGML_ASSERT(nb01 % 16 == 0); break;
|
|
2215
|
+
// case GGML_TYPE_F16: GGML_ASSERT(nb01 % 8 == 0); break;
|
|
2216
|
+
// case GGML_TYPE_BF16: GGML_ASSERT(nb01 % 8 == 0); break;
|
|
2217
|
+
// default: break;
|
|
2218
|
+
//}
|
|
2219
|
+
|
|
2220
|
+
// extra buffers for intermediate id mapping
|
|
2221
|
+
ggml_metal_buffer_id bid_tpe = bid_dst;
|
|
2222
|
+
bid_tpe.offs += ggml_nbytes(op);
|
|
2223
|
+
|
|
2224
|
+
ggml_metal_buffer_id bid_ids = bid_tpe;
|
|
2225
|
+
bid_ids.offs += ggml_metal_op_mul_mat_id_extra_tpe(op);
|
|
2226
|
+
|
|
2227
|
+
{
|
|
2228
|
+
ggml_metal_kargs_mul_mm_id_map0 args = {
|
|
2229
|
+
ne02,
|
|
2230
|
+
ne10,
|
|
2231
|
+
ne11, // n_expert_used (bcast)
|
|
2232
|
+
nb11,
|
|
2233
|
+
nb12,
|
|
2234
|
+
ne21, // n_tokens
|
|
2235
|
+
ne20, // n_expert_used
|
|
2236
|
+
nb21,
|
|
2237
|
+
};
|
|
2238
|
+
|
|
2239
|
+
auto pipeline = ggml_metal_library_get_pipeline_mul_mm_id_map0(lib, ne02, ne20);
|
|
2240
|
+
|
|
2241
|
+
const size_t smem = pipeline.smem;
|
|
2242
|
+
|
|
2243
|
+
GGML_ASSERT(ne02 <= ggml_metal_pipeline_max_theads_per_threadgroup(pipeline));
|
|
2244
|
+
|
|
2245
|
+
GGML_ASSERT(smem <= props_dev->max_theadgroup_memory_size);
|
|
2246
|
+
|
|
2247
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
2248
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
2249
|
+
ggml_metal_encoder_set_buffer (enc, bid_src2, 1);
|
|
2250
|
+
ggml_metal_encoder_set_buffer (enc, bid_tpe, 2);
|
|
2251
|
+
ggml_metal_encoder_set_buffer (enc, bid_ids, 3);
|
|
2252
|
+
|
|
2253
|
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0);
|
|
2254
|
+
|
|
2255
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, 1, 1, 1, ne02, 1, 1);
|
|
2256
|
+
}
|
|
2257
|
+
|
|
2258
|
+
// this barrier is always needed because the next kernel has to wait for the id maps to be computed
|
|
2259
|
+
ggml_metal_op_concurrency_reset(ctx);
|
|
2260
|
+
|
|
2261
|
+
{
|
|
2262
|
+
auto pipeline = ggml_metal_library_get_pipeline_mul_mm_id(lib, op);
|
|
2263
|
+
|
|
2264
|
+
ggml_metal_kargs_mul_mm_id args = {
|
|
2265
|
+
/*.ne00 =*/ ne00,
|
|
2266
|
+
/*.ne02 =*/ ne02,
|
|
2267
|
+
/*.nb01 =*/ nb01,
|
|
2268
|
+
/*.nb02 =*/ nb02,
|
|
2269
|
+
/*.nb03 =*/ nb03,
|
|
2270
|
+
/*.ne11 =*/ ne11, // n_expert_used (bcast)
|
|
2271
|
+
/*.nb10 =*/ nb10,
|
|
2272
|
+
/*.nb11 =*/ nb11,
|
|
2273
|
+
/*.nb12 =*/ nb12,
|
|
2274
|
+
/*.nb13 =*/ nb13,
|
|
2275
|
+
/*.ne20 =*/ ne20, // n_expert_used
|
|
2276
|
+
/*.ne21 =*/ ne21, // n_tokens
|
|
2277
|
+
/*.ne0 =*/ ne0,
|
|
2278
|
+
/*.ne1 =*/ ne1,
|
|
2279
|
+
/*.r2 =*/ r2,
|
|
2280
|
+
/*.r3 =*/ r3,
|
|
2281
|
+
};
|
|
2282
|
+
|
|
2283
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
2284
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
2285
|
+
ggml_metal_encoder_set_buffer (enc, bid_src0, 1);
|
|
2286
|
+
ggml_metal_encoder_set_buffer (enc, bid_src1, 2);
|
|
2287
|
+
ggml_metal_encoder_set_buffer (enc, bid_tpe, 3);
|
|
2288
|
+
ggml_metal_encoder_set_buffer (enc, bid_ids, 4);
|
|
2289
|
+
ggml_metal_encoder_set_buffer (enc, bid_dst, 5);
|
|
2290
|
+
|
|
2291
|
+
const size_t smem = pipeline.smem;
|
|
2292
|
+
|
|
2293
|
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0);
|
|
2294
|
+
|
|
2295
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, (ne21 + 31)/32, (ne01 + 63)/64, ne02, 128, 1, 1);
|
|
2296
|
+
}
|
|
2297
|
+
} else {
|
|
2298
|
+
auto pipeline = ggml_metal_library_get_pipeline_mul_mv_id(lib, op);
|
|
2299
|
+
|
|
2300
|
+
const int nr0 = pipeline.nr0;
|
|
2301
|
+
const int nr1 = pipeline.nr1;
|
|
2302
|
+
const int nsg = pipeline.nsg;
|
|
2303
|
+
|
|
2304
|
+
const size_t smem = pipeline.smem;
|
|
2305
|
+
|
|
2306
|
+
ggml_metal_kargs_mul_mv_id args = {
|
|
2307
|
+
/*.nei0 =*/ ne20,
|
|
2308
|
+
/*.nei1 =*/ ne21,
|
|
2309
|
+
/*.nbi1 =*/ nb21,
|
|
2310
|
+
/*.ne00 =*/ ne00,
|
|
2311
|
+
/*.ne01 =*/ ne01,
|
|
2312
|
+
/*.ne02 =*/ ne02,
|
|
2313
|
+
/*.nb00 =*/ nb00,
|
|
2314
|
+
/*.nb01 =*/ nb01,
|
|
2315
|
+
/*.nb02 =*/ nb02,
|
|
2316
|
+
/*.ne10 =*/ ne10,
|
|
2317
|
+
/*.ne11 =*/ ne11,
|
|
2318
|
+
/*.ne12 =*/ ne12,
|
|
2319
|
+
/*.ne13 =*/ ne13,
|
|
2320
|
+
/*.nb10 =*/ nb10,
|
|
2321
|
+
/*.nb11 =*/ nb11,
|
|
2322
|
+
/*.nb12 =*/ nb12,
|
|
2323
|
+
/*.ne0 =*/ ne0,
|
|
2324
|
+
/*.ne1 =*/ ne1,
|
|
2325
|
+
/*.nb1 =*/ nb1,
|
|
2326
|
+
/*.nr0 =*/ nr0,
|
|
2327
|
+
};
|
|
2328
|
+
|
|
2329
|
+
if (ggml_is_quantized(op->src[0]->type)) {
|
|
2330
|
+
GGML_ASSERT(ne00 >= nsg*nr0);
|
|
2331
|
+
}
|
|
2332
|
+
|
|
2333
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
2334
|
+
ggml_metal_encoder_set_bytes(enc, &args, sizeof(args), 0);
|
|
2335
|
+
ggml_metal_encoder_set_buffer(enc, bid_src0, 1);
|
|
2336
|
+
ggml_metal_encoder_set_buffer(enc, bid_src1, 2);
|
|
2337
|
+
ggml_metal_encoder_set_buffer(enc, bid_dst, 3);
|
|
2338
|
+
ggml_metal_encoder_set_buffer(enc, bid_src2, 4);
|
|
2339
|
+
|
|
2340
|
+
const int64_t _ne1 = 1;
|
|
2341
|
+
const int64_t ne123 = ne20*ne21;
|
|
2342
|
+
|
|
2343
|
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0);
|
|
2344
|
+
|
|
2345
|
+
if (op->src[0]->type == GGML_TYPE_F32 ||
|
|
2346
|
+
op->src[0]->type == GGML_TYPE_F16 ||
|
|
2347
|
+
op->src[0]->type == GGML_TYPE_BF16 ||
|
|
2348
|
+
op->src[0]->type == GGML_TYPE_Q8_0) {
|
|
2349
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, (ne01 + nr0 - 1)/(nr0), (_ne1 + nr1 - 1)/nr1, ne123, 32, nsg, 1);
|
|
2350
|
+
} else {
|
|
2351
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, (ne01 + nr0*nsg - 1)/(nr0*nsg), (_ne1 + nr1 - 1)/nr1, ne123, 32, nsg, 1);
|
|
2352
|
+
}
|
|
2353
|
+
}
|
|
2354
|
+
|
|
2355
|
+
return 1;
|
|
2356
|
+
}
|
|
2357
|
+
|
|
2358
|
+
int ggml_metal_op_add_id(ggml_metal_op_t ctx, int idx) {
|
|
2359
|
+
ggml_tensor * op = ctx->node(idx);
|
|
2360
|
+
|
|
2361
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
2362
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
2363
|
+
|
|
2364
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
2365
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
2366
|
+
GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
|
|
2367
|
+
GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb);
|
|
2368
|
+
GGML_TENSOR_LOCALS( int32_t, ne2, op->src[2], ne);
|
|
2369
|
+
GGML_TENSOR_LOCALS(uint64_t, nb2, op->src[2], nb);
|
|
2370
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
2371
|
+
|
|
2372
|
+
GGML_ASSERT(op->src[0]->type == GGML_TYPE_F32);
|
|
2373
|
+
GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32);
|
|
2374
|
+
GGML_ASSERT(op->src[2]->type == GGML_TYPE_I32);
|
|
2375
|
+
GGML_ASSERT(op->type == GGML_TYPE_F32);
|
|
2376
|
+
|
|
2377
|
+
GGML_ASSERT(ggml_is_contiguous_rows(op->src[0]));
|
|
2378
|
+
|
|
2379
|
+
ggml_metal_kargs_add_id args = {
|
|
2380
|
+
/*.ne0 =*/ ne0,
|
|
2381
|
+
/*.ne1 =*/ ne1,
|
|
2382
|
+
/*.nb01 =*/ nb01,
|
|
2383
|
+
/*.nb02 =*/ nb02,
|
|
2384
|
+
/*.nb11 =*/ nb11,
|
|
2385
|
+
/*.nb21 =*/ nb21,
|
|
2386
|
+
};
|
|
2387
|
+
|
|
2388
|
+
auto pipeline = ggml_metal_library_get_pipeline_base(lib, GGML_OP_ADD_ID);
|
|
2389
|
+
|
|
2390
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
2391
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
2392
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
2393
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2);
|
|
2394
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[2]), 3);
|
|
2395
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 4);
|
|
2396
|
+
|
|
2397
|
+
const int nth = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), ne00);
|
|
2398
|
+
|
|
2399
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, 1, nth, 1, 1);
|
|
2400
|
+
|
|
2401
|
+
return 1;
|
|
2402
|
+
}
|
|
2403
|
+
|
|
2404
|
+
bool ggml_metal_op_flash_attn_ext_use_vec(const ggml_tensor * op) {
|
|
2405
|
+
assert(op->op == GGML_OP_FLASH_ATTN_EXT);
|
|
2406
|
+
|
|
2407
|
+
const int64_t ne00 = op->src[0]->ne[0]; // head size
|
|
2408
|
+
const int64_t ne01 = op->src[0]->ne[1]; // batch size
|
|
2409
|
+
|
|
2410
|
+
// use vec kernel if the batch size is small and if the head size is supported
|
|
2411
|
+
return (ne01 < 20) && (ne00 % 32 == 0);
|
|
2412
|
+
}
|
|
2413
|
+
|
|
2414
|
+
size_t ggml_metal_op_flash_attn_ext_extra_pad(const ggml_tensor * op) {
|
|
2415
|
+
assert(op->op == GGML_OP_FLASH_ATTN_EXT);
|
|
2416
|
+
|
|
2417
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
2418
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
2419
|
+
GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
|
|
2420
|
+
GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb);
|
|
2421
|
+
GGML_TENSOR_LOCALS( int32_t, ne2, op->src[2], ne);
|
|
2422
|
+
GGML_TENSOR_LOCALS(uint64_t, nb2, op->src[2], nb);
|
|
2423
|
+
GGML_TENSOR_LOCALS( int32_t, ne3, op->src[3], ne);
|
|
2424
|
+
GGML_TENSOR_LOCALS(uint64_t, nb3, op->src[3], nb);
|
|
2425
|
+
|
|
2426
|
+
size_t res = 0;
|
|
2427
|
+
|
|
2428
|
+
const bool has_mask = op->src[3] != nullptr;
|
|
2429
|
+
|
|
2430
|
+
// note: the non-vec kernel requires more extra memory, so always reserve for it
|
|
2431
|
+
GGML_ASSERT(OP_FLASH_ATTN_EXT_NCPSG >= OP_FLASH_ATTN_EXT_VEC_NCPSG);
|
|
2432
|
+
|
|
2433
|
+
//if (ggml_metal_op_flash_attn_ext_use_vec(op)) {
|
|
2434
|
+
if (false) {
|
|
2435
|
+
// note: always reserve the padding space to avoid graph reallocations
|
|
2436
|
+
//const bool has_kvpad = ne11 % OP_FLASH_ATTN_EXT_VEC_NCPSG != 0;
|
|
2437
|
+
const bool has_kvpad = true;
|
|
2438
|
+
|
|
2439
|
+
if (has_kvpad) {
|
|
2440
|
+
res += OP_FLASH_ATTN_EXT_VEC_NCPSG*(
|
|
2441
|
+
nb11*ne12*ne13 +
|
|
2442
|
+
nb21*ne22*ne23 +
|
|
2443
|
+
(has_mask ? ggml_type_size(GGML_TYPE_F16)*ne31*ne32*ne33 : 0));
|
|
2444
|
+
}
|
|
2445
|
+
} else {
|
|
2446
|
+
//const bool has_kvpad = ne11 % OP_FLASH_ATTN_EXT_NCPSG != 0;
|
|
2447
|
+
const bool has_kvpad = true;
|
|
2448
|
+
|
|
2449
|
+
if (has_kvpad) {
|
|
2450
|
+
res += OP_FLASH_ATTN_EXT_NCPSG*(
|
|
2451
|
+
nb11*ne12*ne13 +
|
|
2452
|
+
nb21*ne22*ne23 +
|
|
2453
|
+
(has_mask ? ggml_type_size(GGML_TYPE_F16)*ne31*ne32*ne33 : 0));
|
|
2454
|
+
}
|
|
2455
|
+
}
|
|
2456
|
+
|
|
2457
|
+
return res;
|
|
2458
|
+
}
|
|
2459
|
+
|
|
2460
|
+
size_t ggml_metal_op_flash_attn_ext_extra_blk(const ggml_tensor * op) {
|
|
2461
|
+
assert(op->op == GGML_OP_FLASH_ATTN_EXT);
|
|
2462
|
+
|
|
2463
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
2464
|
+
//GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
2465
|
+
//GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
|
|
2466
|
+
//GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb);
|
|
2467
|
+
//GGML_TENSOR_LOCALS( int32_t, ne2, op->src[2], ne);
|
|
2468
|
+
//GGML_TENSOR_LOCALS(uint64_t, nb2, op->src[2], nb);
|
|
2469
|
+
GGML_TENSOR_LOCALS( int32_t, ne3, op->src[3], ne);
|
|
2470
|
+
GGML_TENSOR_LOCALS(uint64_t, nb3, op->src[3], nb);
|
|
2471
|
+
|
|
2472
|
+
size_t res = 0;
|
|
2473
|
+
|
|
2474
|
+
const bool has_mask = op->src[3] != nullptr;
|
|
2475
|
+
|
|
2476
|
+
if (!has_mask) {
|
|
2477
|
+
return res;
|
|
2478
|
+
}
|
|
2479
|
+
|
|
2480
|
+
const bool is_vec = ggml_metal_op_flash_attn_ext_use_vec(op);
|
|
2481
|
+
|
|
2482
|
+
// this optimization is not useful for the vector kernels
|
|
2483
|
+
// note: always reserve the blk buffer to avoid graph reallocations
|
|
2484
|
+
//if (is_vec) {
|
|
2485
|
+
// return res;
|
|
2486
|
+
//}
|
|
2487
|
+
|
|
2488
|
+
const int nqptg = is_vec ? OP_FLASH_ATTN_EXT_VEC_NQPSG : OP_FLASH_ATTN_EXT_NQPSG;
|
|
2489
|
+
const int ncpsg = is_vec ? OP_FLASH_ATTN_EXT_VEC_NCPSG : OP_FLASH_ATTN_EXT_NCPSG;
|
|
2490
|
+
|
|
2491
|
+
const int64_t ne1 = (ne01 + nqptg - 1)/nqptg;
|
|
2492
|
+
const int64_t ne0 = (ne30 + ncpsg - 1)/ncpsg;
|
|
2493
|
+
|
|
2494
|
+
res += GGML_PAD(ggml_type_size(GGML_TYPE_I8)*ne0*ne1*ne32*ne33, 32);
|
|
2495
|
+
|
|
2496
|
+
return res;
|
|
2497
|
+
}
|
|
2498
|
+
|
|
2499
|
+
size_t ggml_metal_op_flash_attn_ext_extra_tmp(const ggml_tensor * op) {
|
|
2500
|
+
assert(op->op == GGML_OP_FLASH_ATTN_EXT);
|
|
2501
|
+
|
|
2502
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
2503
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
2504
|
+
//GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
|
|
2505
|
+
//GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb);
|
|
2506
|
+
GGML_TENSOR_LOCALS( int32_t, ne2, op->src[2], ne);
|
|
2507
|
+
GGML_TENSOR_LOCALS(uint64_t, nb2, op->src[2], nb);
|
|
2508
|
+
//GGML_TENSOR_LOCALS( int32_t, ne3, op->src[3], ne);
|
|
2509
|
+
//GGML_TENSOR_LOCALS(uint64_t, nb3, op->src[3], nb);
|
|
2510
|
+
|
|
2511
|
+
size_t res = 0;
|
|
2512
|
+
|
|
2513
|
+
// note: always reserve the temp buffer to avoid graph reallocations
|
|
2514
|
+
//if (ggml_metal_op_flash_attn_ext_use_vec(op)) {
|
|
2515
|
+
if (true) {
|
|
2516
|
+
const int64_t nwg = 32;
|
|
2517
|
+
const int64_t ne01_max = std::min(ne01, 32);
|
|
2518
|
+
|
|
2519
|
+
// temp buffer for writing the results from each workgroup
|
|
2520
|
+
// - ne20: the size of the Value head
|
|
2521
|
+
// - + 2: the S and M values for each intermediate result
|
|
2522
|
+
res += ggml_type_size(GGML_TYPE_F32)*(ne01_max*ne02*ne03*nwg*(ne20 + 2));
|
|
2523
|
+
}
|
|
2524
|
+
|
|
2525
|
+
return res;
|
|
2526
|
+
}
|
|
2527
|
+
|
|
2528
|
+
int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) {
|
|
2529
|
+
ggml_tensor * op = ctx->node(idx);
|
|
2530
|
+
|
|
2531
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
2532
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
2533
|
+
|
|
2534
|
+
const ggml_metal_device_props * props_dev = ggml_metal_device_get_props(ctx->dev);
|
|
2535
|
+
|
|
2536
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
2537
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
2538
|
+
GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
|
|
2539
|
+
GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb);
|
|
2540
|
+
GGML_TENSOR_LOCALS( int32_t, ne2, op->src[2], ne);
|
|
2541
|
+
GGML_TENSOR_LOCALS(uint64_t, nb2, op->src[2], nb);
|
|
2542
|
+
GGML_TENSOR_LOCALS( int32_t, ne3, op->src[3], ne);
|
|
2543
|
+
GGML_TENSOR_LOCALS(uint64_t, nb3, op->src[3], nb);
|
|
2544
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
2545
|
+
GGML_TENSOR_LOCALS( int32_t, nb, op, nb);
|
|
2546
|
+
|
|
2547
|
+
GGML_ASSERT(ne00 % 4 == 0);
|
|
2548
|
+
|
|
2549
|
+
GGML_ASSERT(op->src[0]->type == GGML_TYPE_F32);
|
|
2550
|
+
GGML_ASSERT(op->src[1]->type == op->src[2]->type);
|
|
2551
|
+
|
|
2552
|
+
//GGML_ASSERT(ggml_are_same_shape (src1, src2));
|
|
2553
|
+
GGML_ASSERT(ne11 == ne21);
|
|
2554
|
+
GGML_ASSERT(ne12 == ne22);
|
|
2555
|
+
|
|
2556
|
+
GGML_ASSERT(!op->src[3] || op->src[3]->type == GGML_TYPE_F16);
|
|
2557
|
+
GGML_ASSERT(!op->src[3] || op->src[3]->ne[1] >= op->src[0]->ne[1] &&
|
|
2558
|
+
"the Flash-Attention Metal kernel requires the mask to be at least n_queries big");
|
|
2559
|
+
|
|
2560
|
+
float scale;
|
|
2561
|
+
float max_bias;
|
|
2562
|
+
float logit_softcap;
|
|
2563
|
+
|
|
2564
|
+
memcpy(&scale, ((const int32_t *) op->op_params) + 0, sizeof(scale));
|
|
2565
|
+
memcpy(&max_bias, ((const int32_t *) op->op_params) + 1, sizeof(max_bias));
|
|
2566
|
+
memcpy(&logit_softcap, ((const int32_t *) op->op_params) + 2, sizeof(logit_softcap));
|
|
2567
|
+
|
|
2568
|
+
if (logit_softcap != 0.0f) {
|
|
2569
|
+
scale /= logit_softcap;
|
|
2570
|
+
}
|
|
2571
|
+
|
|
2572
|
+
const bool has_mask = op->src[3] != NULL;
|
|
2573
|
+
const bool has_sinks = op->src[4] != NULL;
|
|
2574
|
+
const bool has_bias = max_bias != 0.0f;
|
|
2575
|
+
const bool has_scap = logit_softcap != 0.0f;
|
|
2576
|
+
|
|
2577
|
+
const uint32_t n_head = op->src[0]->ne[2];
|
|
2578
|
+
const int32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head));
|
|
2579
|
+
|
|
2580
|
+
const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
|
|
2581
|
+
const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
|
|
2582
|
+
|
|
2583
|
+
GGML_ASSERT(ne01 < 65536);
|
|
2584
|
+
|
|
2585
|
+
ggml_metal_buffer_id bid_src0 = ggml_metal_get_buffer_id(op->src[0]);
|
|
2586
|
+
ggml_metal_buffer_id bid_src1 = ggml_metal_get_buffer_id(op->src[1]);
|
|
2587
|
+
ggml_metal_buffer_id bid_src2 = ggml_metal_get_buffer_id(op->src[2]);
|
|
2588
|
+
ggml_metal_buffer_id bid_src3 = has_mask ? ggml_metal_get_buffer_id(op->src[3]) : bid_src0;
|
|
2589
|
+
ggml_metal_buffer_id bid_src4 = has_sinks ? ggml_metal_get_buffer_id(op->src[4]) : bid_src0;
|
|
2590
|
+
|
|
2591
|
+
ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op);
|
|
2592
|
+
|
|
2593
|
+
ggml_metal_buffer_id bid_pad = bid_dst;
|
|
2594
|
+
bid_pad.offs += ggml_nbytes(op);
|
|
2595
|
+
|
|
2596
|
+
ggml_metal_buffer_id bid_blk = bid_pad;
|
|
2597
|
+
bid_blk.offs += ggml_metal_op_flash_attn_ext_extra_pad(op);
|
|
2598
|
+
|
|
2599
|
+
ggml_metal_buffer_id bid_tmp = bid_blk;
|
|
2600
|
+
bid_tmp.offs += ggml_metal_op_flash_attn_ext_extra_blk(op);
|
|
2601
|
+
|
|
2602
|
+
if (!ggml_metal_op_flash_attn_ext_use_vec(op)) {
|
|
2603
|
+
// half8x8 kernel
|
|
2604
|
+
const int nqptg = OP_FLASH_ATTN_EXT_NQPSG; // queries per threadgroup
|
|
2605
|
+
const int ncpsg = OP_FLASH_ATTN_EXT_NCPSG; // cache values per simdgroup
|
|
2606
|
+
|
|
2607
|
+
GGML_ASSERT(nqptg <= 32);
|
|
2608
|
+
GGML_ASSERT(nqptg % 8 == 0);
|
|
2609
|
+
GGML_ASSERT(ncpsg % 32 == 0);
|
|
2610
|
+
|
|
2611
|
+
bool need_sync = false;
|
|
2612
|
+
|
|
2613
|
+
const bool has_kvpad = ne11 % ncpsg != 0;
|
|
2614
|
+
|
|
2615
|
+
if (has_kvpad) {
|
|
2616
|
+
assert(ggml_metal_op_flash_attn_ext_extra_pad(op) != 0);
|
|
2617
|
+
|
|
2618
|
+
ggml_metal_kargs_flash_attn_ext_pad args0 = {
|
|
2619
|
+
/*.ne11 =*/ne11,
|
|
2620
|
+
/*.ne_12_2 =*/ne12,
|
|
2621
|
+
/*.ne_12_3 =*/ne13,
|
|
2622
|
+
/*.nb11 =*/nb11,
|
|
2623
|
+
/*.nb12 =*/nb12,
|
|
2624
|
+
/*.nb13 =*/nb13,
|
|
2625
|
+
/*.nb21 =*/nb21,
|
|
2626
|
+
/*.nb22 =*/nb22,
|
|
2627
|
+
/*.nb23 =*/nb23,
|
|
2628
|
+
/*.ne31 =*/ne31,
|
|
2629
|
+
/*.ne32 =*/ne32,
|
|
2630
|
+
/*.ne33 =*/ne33,
|
|
2631
|
+
/*.nb31 =*/nb31,
|
|
2632
|
+
/*.nb32 =*/nb32,
|
|
2633
|
+
/*.nb33 =*/nb33,
|
|
2634
|
+
};
|
|
2635
|
+
|
|
2636
|
+
auto pipeline0 = ggml_metal_library_get_pipeline_flash_attn_ext_pad(lib, op, has_mask, ncpsg);
|
|
2637
|
+
|
|
2638
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline0);
|
|
2639
|
+
ggml_metal_encoder_set_bytes (enc, &args0, sizeof(args0), 0);
|
|
2640
|
+
ggml_metal_encoder_set_buffer (enc, bid_src1, 1);
|
|
2641
|
+
ggml_metal_encoder_set_buffer (enc, bid_src2, 2);
|
|
2642
|
+
ggml_metal_encoder_set_buffer (enc, bid_src3, 3);
|
|
2643
|
+
ggml_metal_encoder_set_buffer (enc, bid_pad, 4);
|
|
2644
|
+
|
|
2645
|
+
assert(ne12 == ne22);
|
|
2646
|
+
assert(ne13 == ne23);
|
|
2647
|
+
|
|
2648
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ncpsg, std::max(ne12, ne32), std::max(ne13, ne33), 32, 1, 1);
|
|
2649
|
+
|
|
2650
|
+
need_sync = true;
|
|
2651
|
+
}
|
|
2652
|
+
|
|
2653
|
+
if (has_mask) {
|
|
2654
|
+
assert(ggml_metal_op_flash_attn_ext_extra_blk(op) != 0);
|
|
2655
|
+
|
|
2656
|
+
ggml_metal_kargs_flash_attn_ext_blk args0 = {
|
|
2657
|
+
/*.ne01 =*/ ne01,
|
|
2658
|
+
/*.ne30 =*/ ne30,
|
|
2659
|
+
/*.ne31 =*/ ne31,
|
|
2660
|
+
/*.ne32 =*/ ne32,
|
|
2661
|
+
/*.ne33 =*/ ne33,
|
|
2662
|
+
/*.nb31 =*/ nb31,
|
|
2663
|
+
/*.nb32 =*/ nb32,
|
|
2664
|
+
/*.nb33 =*/ nb33,
|
|
2665
|
+
};
|
|
2666
|
+
|
|
2667
|
+
auto pipeline0 = ggml_metal_library_get_pipeline_flash_attn_ext_blk(lib, op, nqptg, ncpsg);
|
|
2668
|
+
|
|
2669
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline0);
|
|
2670
|
+
ggml_metal_encoder_set_bytes (enc, &args0, sizeof(args0), 0);
|
|
2671
|
+
ggml_metal_encoder_set_buffer (enc, bid_src3, 1);
|
|
2672
|
+
ggml_metal_encoder_set_buffer (enc, bid_blk, 2);
|
|
2673
|
+
|
|
2674
|
+
const int32_t nblk1 = ((ne01 + nqptg - 1)/nqptg);
|
|
2675
|
+
const int32_t nblk0 = ((ne30 + ncpsg - 1)/ncpsg);
|
|
2676
|
+
|
|
2677
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, nblk0, nblk1, ne32*ne33, 32, 1, 1);
|
|
2678
|
+
|
|
2679
|
+
need_sync = true;
|
|
2680
|
+
}
|
|
2681
|
+
|
|
2682
|
+
if (need_sync) {
|
|
2683
|
+
ggml_metal_op_concurrency_reset(ctx);
|
|
2684
|
+
}
|
|
2685
|
+
|
|
2686
|
+
const int is_q = ggml_is_quantized(op->src[1]->type) ? 1 : 0;
|
|
2687
|
+
|
|
2688
|
+
// 2*(2*ncpsg)
|
|
2689
|
+
// ncpsg soft_max values + ncpsg mask values
|
|
2690
|
+
//
|
|
2691
|
+
// 16*32*(nsg)
|
|
2692
|
+
// the shared memory needed for the simdgroups to load the KV cache
|
|
2693
|
+
// each thread loads (dequantizes) 16 head elements, there are 32 threads in th SG
|
|
2694
|
+
//
|
|
2695
|
+
#define FATTN_SMEM(nsg) (GGML_PAD((nqptg*(ne00 + 2*GGML_PAD(ne20, 64) + 2*(2*ncpsg)) + is_q*(16*32*(nsg)))*(sizeof(float)/2), 16))
|
|
2696
|
+
|
|
2697
|
+
//int64_t nsgmax = 4;
|
|
2698
|
+
//
|
|
2699
|
+
//if (is_q) {
|
|
2700
|
+
// nsgmax = 2;
|
|
2701
|
+
// while (true) {
|
|
2702
|
+
// const size_t smem = FATTN_SMEM(nsgmax);
|
|
2703
|
+
// if (smem > props_dev->max_theadgroup_memory_size) {
|
|
2704
|
+
// break;
|
|
2705
|
+
// }
|
|
2706
|
+
// nsgmax *= 2;
|
|
2707
|
+
// }
|
|
2708
|
+
// nsgmax /= 2;
|
|
2709
|
+
//}
|
|
2710
|
+
|
|
2711
|
+
// simdgroups per threadgroup (a.k.a. warps)
|
|
2712
|
+
//nsg = ne01 <= nqptg ? MAX(4, MIN(nsgmax, MIN(ne11/ncpsg, (int64_t) pipeline.maxTotalThreadsPerThreadgroup/32))) : 4;
|
|
2713
|
+
int32_t nsg = ne00 >= 512 ? 8 : 4;
|
|
2714
|
+
|
|
2715
|
+
const size_t smem = FATTN_SMEM(nsg);
|
|
2716
|
+
|
|
2717
|
+
ggml_metal_kargs_flash_attn_ext args = {
|
|
2718
|
+
/*.ne01 =*/ ne01,
|
|
2719
|
+
/*.ne02 =*/ ne02,
|
|
2720
|
+
/*.ne03 =*/ ne03,
|
|
2721
|
+
/*.nb01 =*/ nb01,
|
|
2722
|
+
/*.nb02 =*/ nb02,
|
|
2723
|
+
/*.nb03 =*/ nb03,
|
|
2724
|
+
/*.ne11 =*/ ne11,
|
|
2725
|
+
/*.ne_12_2 =*/ ne12,
|
|
2726
|
+
/*.ne_12_3 =*/ ne13,
|
|
2727
|
+
/*.ns10 =*/ int32_t(nb11/nb10),
|
|
2728
|
+
/*.nb11 =*/ nb11,
|
|
2729
|
+
/*.nb12 =*/ nb12,
|
|
2730
|
+
/*.nb13 =*/ nb13,
|
|
2731
|
+
/*.ns20 =*/ int32_t(nb21/nb20),
|
|
2732
|
+
/*.nb21 =*/ nb21,
|
|
2733
|
+
/*.nb22 =*/ nb22,
|
|
2734
|
+
/*.nb23 =*/ nb23,
|
|
2735
|
+
/*.ne31 =*/ ne31,
|
|
2736
|
+
/*.ne32 =*/ ne32,
|
|
2737
|
+
/*.ne33 =*/ ne33,
|
|
2738
|
+
/*.nb31 =*/ nb31,
|
|
2739
|
+
/*.nb32 =*/ nb32,
|
|
2740
|
+
/*.nb33 =*/ nb33,
|
|
2741
|
+
/*.ne1 =*/ ne1,
|
|
2742
|
+
/*.ne2 =*/ ne2,
|
|
2743
|
+
/*.ne3 =*/ ne3,
|
|
2744
|
+
/*.scale =*/ scale,
|
|
2745
|
+
/*.max_bias =*/ max_bias,
|
|
2746
|
+
/*.m0 =*/ m0,
|
|
2747
|
+
/*.m1 =*/ m1,
|
|
2748
|
+
/*.n_head_log2 =*/ n_head_log2,
|
|
2749
|
+
/*.logit_softcap =*/ logit_softcap,
|
|
2750
|
+
};
|
|
2751
|
+
|
|
2752
|
+
auto pipeline = ggml_metal_library_get_pipeline_flash_attn_ext(lib, op, has_mask, has_sinks, has_bias, has_scap, has_kvpad, nsg);
|
|
2753
|
+
|
|
2754
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
2755
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
2756
|
+
ggml_metal_encoder_set_buffer (enc, bid_src0, 1);
|
|
2757
|
+
ggml_metal_encoder_set_buffer (enc, bid_src1, 2);
|
|
2758
|
+
ggml_metal_encoder_set_buffer (enc, bid_src2, 3);
|
|
2759
|
+
ggml_metal_encoder_set_buffer (enc, bid_src3, 4);
|
|
2760
|
+
ggml_metal_encoder_set_buffer (enc, bid_src4, 5);
|
|
2761
|
+
ggml_metal_encoder_set_buffer (enc, bid_pad, 6);
|
|
2762
|
+
ggml_metal_encoder_set_buffer (enc, bid_blk, 7);
|
|
2763
|
+
ggml_metal_encoder_set_buffer (enc, bid_dst, 8);
|
|
2764
|
+
|
|
2765
|
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0);
|
|
2766
|
+
|
|
2767
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, (ne01 + nqptg - 1)/nqptg, ne02, ne03, 32, nsg, 1);
|
|
2768
|
+
#undef FATTN_SMEM
|
|
2769
|
+
} else {
|
|
2770
|
+
// half4x4 kernel
|
|
2771
|
+
const int nqptg = OP_FLASH_ATTN_EXT_VEC_NQPSG; // queries per threadgroup
|
|
2772
|
+
const int ncpsg = OP_FLASH_ATTN_EXT_VEC_NCPSG; // cache values per simdgroup !! sync with kernel template arguments !!
|
|
2773
|
+
const int nhptg = 1; // heads per threadgroup
|
|
2774
|
+
|
|
2775
|
+
GGML_ASSERT(nqptg <= 32);
|
|
2776
|
+
GGML_ASSERT(nqptg % 1 == 0);
|
|
2777
|
+
GGML_ASSERT(ncpsg % 32 == 0);
|
|
2778
|
+
|
|
2779
|
+
bool need_sync = false;
|
|
2780
|
+
|
|
2781
|
+
const bool has_kvpad = ne11 % ncpsg != 0;
|
|
2782
|
+
|
|
2783
|
+
if (has_kvpad) {
|
|
2784
|
+
assert(ggml_metal_op_flash_attn_ext_extra_pad(op) != 0);
|
|
2785
|
+
|
|
2786
|
+
ggml_metal_kargs_flash_attn_ext_pad args0 = {
|
|
2787
|
+
/*.ne11 =*/ne11,
|
|
2788
|
+
/*.ne_12_2 =*/ne12,
|
|
2789
|
+
/*.ne_12_3 =*/ne13,
|
|
2790
|
+
/*.nb11 =*/nb11,
|
|
2791
|
+
/*.nb12 =*/nb12,
|
|
2792
|
+
/*.nb13 =*/nb13,
|
|
2793
|
+
/*.nb21 =*/nb21,
|
|
2794
|
+
/*.nb22 =*/nb22,
|
|
2795
|
+
/*.nb23 =*/nb23,
|
|
2796
|
+
/*.ne31 =*/ne31,
|
|
2797
|
+
/*.ne32 =*/ne32,
|
|
2798
|
+
/*.ne33 =*/ne33,
|
|
2799
|
+
/*.nb31 =*/nb31,
|
|
2800
|
+
/*.nb32 =*/nb32,
|
|
2801
|
+
/*.nb33 =*/nb33,
|
|
2802
|
+
};
|
|
2803
|
+
|
|
2804
|
+
auto pipeline0 = ggml_metal_library_get_pipeline_flash_attn_ext_pad(lib, op, has_mask, ncpsg);
|
|
2805
|
+
|
|
2806
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline0);
|
|
2807
|
+
ggml_metal_encoder_set_bytes (enc, &args0, sizeof(args0), 0);
|
|
2808
|
+
ggml_metal_encoder_set_buffer (enc, bid_src1, 1);
|
|
2809
|
+
ggml_metal_encoder_set_buffer (enc, bid_src2, 2);
|
|
2810
|
+
ggml_metal_encoder_set_buffer (enc, bid_src3, 3);
|
|
2811
|
+
ggml_metal_encoder_set_buffer (enc, bid_pad, 4);
|
|
2812
|
+
|
|
2813
|
+
assert(ne12 == ne22);
|
|
2814
|
+
assert(ne13 == ne23);
|
|
2815
|
+
|
|
2816
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ncpsg, std::max(ne12, ne32), std::max(ne13, ne33), 32, 1, 1);
|
|
2817
|
+
|
|
2818
|
+
need_sync = true;
|
|
2819
|
+
}
|
|
2820
|
+
|
|
2821
|
+
if (need_sync) {
|
|
2822
|
+
ggml_metal_op_concurrency_reset(ctx);
|
|
2823
|
+
}
|
|
2824
|
+
|
|
2825
|
+
// note: for simplicity assume the K is larger or equal than V
|
|
2826
|
+
GGML_ASSERT(ne10 >= ne20);
|
|
2827
|
+
|
|
2828
|
+
// ne00 + 2*ncpsg*(nsg)
|
|
2829
|
+
// for each query, we load it as f16 in shared memory (ne00)
|
|
2830
|
+
// and store the soft_max values and the mask
|
|
2831
|
+
//
|
|
2832
|
+
// ne20*(nsg)
|
|
2833
|
+
// each simdgroup has a full f32 head vector in shared mem to accumulate results
|
|
2834
|
+
//
|
|
2835
|
+
#define FATTN_SMEM(nsg) (GGML_PAD(((GGML_PAD(ne00, 128) + 4*ncpsg + 2*GGML_PAD(ne20, 128))*(nsg))*(sizeof(float)/2), 16))
|
|
2836
|
+
|
|
2837
|
+
int64_t nsg = 1;
|
|
2838
|
+
|
|
2839
|
+
// workgroups
|
|
2840
|
+
// each workgroup handles nsg*nkpsg cache values
|
|
2841
|
+
int32_t nwg = 1;
|
|
2842
|
+
if (false) {
|
|
2843
|
+
// for small KV caches, we could launch a single workgroup and write the results directly to dst/
|
|
2844
|
+
// however, this does not lead to significant improvement, so disabled
|
|
2845
|
+
nwg = 1;
|
|
2846
|
+
nsg = 4;
|
|
2847
|
+
} else {
|
|
2848
|
+
nwg = 32;
|
|
2849
|
+
nsg = 1;
|
|
2850
|
+
while (2*nwg*nsg*ncpsg < ne11 && nsg < 4) {
|
|
2851
|
+
nsg *= 2;
|
|
2852
|
+
}
|
|
2853
|
+
}
|
|
2854
|
+
|
|
2855
|
+
ggml_metal_kargs_flash_attn_ext_vec args = {
|
|
2856
|
+
/*.ne01 =*/ ne01,
|
|
2857
|
+
/*.ne02 =*/ ne02,
|
|
2858
|
+
/*.ne03 =*/ ne03,
|
|
2859
|
+
/*.nb01 =*/ nb01,
|
|
2860
|
+
/*.nb02 =*/ nb02,
|
|
2861
|
+
/*.nb03 =*/ nb03,
|
|
2862
|
+
/*.ne11 =*/ ne11,
|
|
2863
|
+
/*.ne_12_2 =*/ ne12,
|
|
2864
|
+
/*.ne_12_3 =*/ ne13,
|
|
2865
|
+
/*.ns10 =*/ int32_t(nb11/nb10),
|
|
2866
|
+
/*.nb11 =*/ nb11,
|
|
2867
|
+
/*.nb12 =*/ nb12,
|
|
2868
|
+
/*.nb13 =*/ nb13,
|
|
2869
|
+
/*.ns20 =*/ int32_t(nb21/nb20),
|
|
2870
|
+
/*.nb21 =*/ nb21,
|
|
2871
|
+
/*.nb22 =*/ nb22,
|
|
2872
|
+
/*.nb23 =*/ nb23,
|
|
2873
|
+
/*.ne31 =*/ ne31,
|
|
2874
|
+
/*.ne32 =*/ ne32,
|
|
2875
|
+
/*.ne33 =*/ ne33,
|
|
2876
|
+
/*.nb31 =*/ nb31,
|
|
2877
|
+
/*.nb32 =*/ nb32,
|
|
2878
|
+
/*.nb33 =*/ nb33,
|
|
2879
|
+
/*.ne1 =*/ ne1,
|
|
2880
|
+
/*.ne2 =*/ ne2,
|
|
2881
|
+
/*.ne3 =*/ ne3,
|
|
2882
|
+
/*.scale =*/ scale,
|
|
2883
|
+
/*.max_bias =*/ max_bias,
|
|
2884
|
+
/*.m0 =*/ m0,
|
|
2885
|
+
/*.m1 =*/ m1,
|
|
2886
|
+
/*.n_head_log2 =*/ n_head_log2,
|
|
2887
|
+
/*.logit_softcap =*/ logit_softcap,
|
|
2888
|
+
};
|
|
2889
|
+
|
|
2890
|
+
auto pipeline = ggml_metal_library_get_pipeline_flash_attn_ext_vec(lib, op, has_mask, has_sinks, has_bias, has_scap, has_kvpad, nsg, nwg);
|
|
2891
|
+
|
|
2892
|
+
GGML_ASSERT(nsg*32 <= ggml_metal_pipeline_max_theads_per_threadgroup(pipeline));
|
|
2893
|
+
|
|
2894
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
2895
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
2896
|
+
ggml_metal_encoder_set_buffer (enc, bid_src0, 1);
|
|
2897
|
+
ggml_metal_encoder_set_buffer (enc, bid_src1, 2);
|
|
2898
|
+
ggml_metal_encoder_set_buffer (enc, bid_src2, 3);
|
|
2899
|
+
ggml_metal_encoder_set_buffer (enc, bid_src3, 4);
|
|
2900
|
+
ggml_metal_encoder_set_buffer (enc, bid_src4, 5);
|
|
2901
|
+
|
|
2902
|
+
const size_t smem = FATTN_SMEM(nsg);
|
|
2903
|
+
|
|
2904
|
+
//printf("smem: %zu, max: %zu, nsg = %d, nsgmax = %d\n", smem, props_dev->max_theadgroup_memory_size, (int) nsg, (int) nsgmax);
|
|
2905
|
+
GGML_ASSERT(smem <= props_dev->max_theadgroup_memory_size);
|
|
2906
|
+
|
|
2907
|
+
if (nwg == 1) {
|
|
2908
|
+
assert(ggml_metal_op_flash_attn_ext_extra_tmp(op) == 0);
|
|
2909
|
+
|
|
2910
|
+
// using 1 workgroup -> write the result directly into dst
|
|
2911
|
+
ggml_metal_encoder_set_buffer(enc, bid_pad, 6);
|
|
2912
|
+
ggml_metal_encoder_set_buffer(enc, bid_dst, 7);
|
|
2913
|
+
|
|
2914
|
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0);
|
|
2915
|
+
|
|
2916
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, (ne01 + nqptg - 1)/nqptg, (ne02 + nhptg - 1)/nhptg, ne03*nwg, 32, nsg, 1);
|
|
2917
|
+
} else {
|
|
2918
|
+
// sanity checks
|
|
2919
|
+
assert(ggml_metal_op_flash_attn_ext_extra_tmp(op) != 0);
|
|
2920
|
+
|
|
2921
|
+
GGML_ASSERT(ne01*ne02*ne03 == ne1*ne2*ne3);
|
|
2922
|
+
GGML_ASSERT((uint64_t)ne1*ne2*ne3 <= (1u << 31));
|
|
2923
|
+
|
|
2924
|
+
// write the results from each workgroup into a temp buffer
|
|
2925
|
+
ggml_metal_encoder_set_buffer(enc, bid_pad, 6);
|
|
2926
|
+
ggml_metal_encoder_set_buffer(enc, bid_tmp, 7);
|
|
2927
|
+
|
|
2928
|
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0);
|
|
2929
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, (ne01 + nqptg - 1)/nqptg, (ne02 + nhptg - 1)/nhptg, ne03*nwg, 32, nsg, 1);
|
|
2930
|
+
|
|
2931
|
+
// sync the 2 kernels
|
|
2932
|
+
ggml_metal_op_concurrency_reset(ctx);
|
|
2933
|
+
|
|
2934
|
+
// reduce the results from the workgroups
|
|
2935
|
+
{
|
|
2936
|
+
const int32_t nrows = ne1*ne2*ne3;
|
|
2937
|
+
|
|
2938
|
+
ggml_metal_kargs_flash_attn_ext_vec_reduce args0 = {
|
|
2939
|
+
nrows,
|
|
2940
|
+
};
|
|
2941
|
+
|
|
2942
|
+
auto pipeline0 = ggml_metal_library_get_pipeline_flash_attn_ext_vec_reduce(lib, op, ne20, nwg);
|
|
2943
|
+
|
|
2944
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline0);
|
|
2945
|
+
ggml_metal_encoder_set_bytes (enc, &args0, sizeof(args0), 0);
|
|
2946
|
+
ggml_metal_encoder_set_buffer (enc, bid_tmp, 1);
|
|
2947
|
+
ggml_metal_encoder_set_buffer (enc, bid_dst, 2);
|
|
2948
|
+
|
|
2949
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, nrows, 1, 1, 32*nwg, 1, 1);
|
|
2950
|
+
}
|
|
2951
|
+
}
|
|
2952
|
+
#undef FATTN_SMEM
|
|
2953
|
+
}
|
|
2954
|
+
|
|
2955
|
+
return 1;
|
|
2956
|
+
}
|
|
2957
|
+
|
|
2958
|
+
int ggml_metal_op_bin(ggml_metal_op_t ctx, int idx) {
|
|
2959
|
+
ggml_tensor * op = ctx->node(idx);
|
|
2960
|
+
|
|
2961
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
2962
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
2963
|
+
|
|
2964
|
+
const bool use_fusion = ctx->use_fusion;
|
|
2965
|
+
|
|
2966
|
+
const int debug_fusion = ctx->debug_fusion;
|
|
2967
|
+
|
|
2968
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
2969
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
2970
|
+
GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
|
|
2971
|
+
GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb);
|
|
2972
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
2973
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
2974
|
+
|
|
2975
|
+
GGML_ASSERT(op->src[0]->type == GGML_TYPE_F32);
|
|
2976
|
+
GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32);
|
|
2977
|
+
|
|
2978
|
+
GGML_ASSERT(ggml_is_contiguous_rows(op->src[0]));
|
|
2979
|
+
GGML_ASSERT(ggml_is_contiguous_rows(op->src[1]));
|
|
2980
|
+
|
|
2981
|
+
ggml_metal_buffer_id bid_src0 = ggml_metal_get_buffer_id(op->src[0]);
|
|
2982
|
+
ggml_metal_buffer_id bid_src1 = ggml_metal_get_buffer_id(op->src[1]);
|
|
2983
|
+
ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op);
|
|
2984
|
+
|
|
2985
|
+
ggml_metal_kargs_bin args = {
|
|
2986
|
+
/*.ne00 =*/ ne00,
|
|
2987
|
+
/*.ne01 =*/ ne01,
|
|
2988
|
+
/*.ne02 =*/ ne02,
|
|
2989
|
+
/*.ne03 =*/ ne03,
|
|
2990
|
+
/*.nb00 =*/ nb00,
|
|
2991
|
+
/*.nb01 =*/ nb01,
|
|
2992
|
+
/*.nb02 =*/ nb02,
|
|
2993
|
+
/*.nb03 =*/ nb03,
|
|
2994
|
+
/*.ne10 =*/ ne10,
|
|
2995
|
+
/*.ne11 =*/ ne11,
|
|
2996
|
+
/*.ne12 =*/ ne12,
|
|
2997
|
+
/*.ne13 =*/ ne13,
|
|
2998
|
+
/*.nb10 =*/ nb10,
|
|
2999
|
+
/*.nb11 =*/ nb11,
|
|
3000
|
+
/*.nb12 =*/ nb12,
|
|
3001
|
+
/*.nb13 =*/ nb13,
|
|
3002
|
+
/*.ne0 =*/ ne0,
|
|
3003
|
+
/*.ne1 =*/ ne1,
|
|
3004
|
+
/*.ne2 =*/ ne2,
|
|
3005
|
+
/*.ne3 =*/ ne3,
|
|
3006
|
+
/*.nb0 =*/ nb0,
|
|
3007
|
+
/*.nb1 =*/ nb1,
|
|
3008
|
+
/*.nb2 =*/ nb2,
|
|
3009
|
+
/*.nb3 =*/ nb3,
|
|
3010
|
+
/*.offs =*/ 0,
|
|
3011
|
+
/*.o1 =*/ { bid_src1.offs },
|
|
3012
|
+
};
|
|
3013
|
+
|
|
3014
|
+
ggml_op fops[8];
|
|
3015
|
+
|
|
3016
|
+
int n_fuse = 1;
|
|
3017
|
+
|
|
3018
|
+
// c[0] = add(a, b[0])
|
|
3019
|
+
// c[1] = add(c[0], b[1])
|
|
3020
|
+
// c[2] = add(c[1], b[2])
|
|
3021
|
+
// ...
|
|
3022
|
+
if (use_fusion) {
|
|
3023
|
+
fops[0] = GGML_OP_ADD;
|
|
3024
|
+
fops[1] = GGML_OP_ADD;
|
|
3025
|
+
fops[2] = GGML_OP_ADD;
|
|
3026
|
+
fops[3] = GGML_OP_ADD;
|
|
3027
|
+
fops[4] = GGML_OP_ADD;
|
|
3028
|
+
fops[5] = GGML_OP_ADD;
|
|
3029
|
+
fops[6] = GGML_OP_ADD;
|
|
3030
|
+
fops[7] = GGML_OP_ADD;
|
|
3031
|
+
|
|
3032
|
+
// note: in metal, we sometimes encode the graph in parallel so we have to avoid fusing ops
|
|
3033
|
+
// across splits. idx_end indicates the last node in the current split
|
|
3034
|
+
for (n_fuse = 0; n_fuse <= 6; ++n_fuse) {
|
|
3035
|
+
if (!ctx->can_fuse(idx + n_fuse, fops + n_fuse, 2)) {
|
|
3036
|
+
break;
|
|
3037
|
+
}
|
|
3038
|
+
|
|
3039
|
+
ggml_tensor * f0 = ctx->node(idx + n_fuse);
|
|
3040
|
+
ggml_tensor * f1 = ctx->node(idx + n_fuse + 1);
|
|
3041
|
+
|
|
3042
|
+
if (f0 != f1->src[0]) {
|
|
3043
|
+
break;
|
|
3044
|
+
}
|
|
3045
|
+
|
|
3046
|
+
// b[0] === b[1] === ...
|
|
3047
|
+
if (!ggml_are_same_layout(f0->src[1], f1->src[1])) {
|
|
3048
|
+
break;
|
|
3049
|
+
}
|
|
3050
|
+
|
|
3051
|
+
// only fuse ops if src1 is in the same Metal buffer
|
|
3052
|
+
ggml_metal_buffer_id bid_fuse = ggml_metal_get_buffer_id(f1->src[1]);
|
|
3053
|
+
if (bid_fuse.metal != bid_src1.metal) {
|
|
3054
|
+
break;
|
|
3055
|
+
}
|
|
3056
|
+
|
|
3057
|
+
//ctx->fuse_cnt[ops[n_fuse + 1]->op]++;
|
|
3058
|
+
|
|
3059
|
+
args.o1[n_fuse + 1] = bid_fuse.offs;
|
|
3060
|
+
}
|
|
3061
|
+
|
|
3062
|
+
++n_fuse;
|
|
3063
|
+
|
|
3064
|
+
if (debug_fusion > 1 && n_fuse > 1) {
|
|
3065
|
+
GGML_LOG_DEBUG("%s: fuse: ADD x %d\n", __func__, n_fuse);
|
|
3066
|
+
}
|
|
3067
|
+
}
|
|
3068
|
+
|
|
3069
|
+
// the offsets of src1 and all fused buffers are relative to the start of the src1 buffer
|
|
3070
|
+
bid_src1.offs = 0;
|
|
3071
|
+
|
|
3072
|
+
struct ggml_metal_pipeline_with_params pipeline;
|
|
3073
|
+
|
|
3074
|
+
pipeline = ggml_metal_library_get_pipeline_bin(lib, op, n_fuse);
|
|
3075
|
+
|
|
3076
|
+
if (n_fuse > 1) {
|
|
3077
|
+
bid_dst = ggml_metal_get_buffer_id(ctx->node(idx + n_fuse - 1));
|
|
3078
|
+
|
|
3079
|
+
for (int i = 1; i < n_fuse; ++i) {
|
|
3080
|
+
if (!ggml_metal_op_concurrency_check(ctx, ctx->node(idx + i))) {
|
|
3081
|
+
ggml_metal_op_concurrency_reset(ctx);
|
|
3082
|
+
|
|
3083
|
+
break;
|
|
3084
|
+
}
|
|
3085
|
+
}
|
|
3086
|
+
}
|
|
3087
|
+
|
|
3088
|
+
if (pipeline.c4) {
|
|
3089
|
+
args.ne00 = ne00/4;
|
|
3090
|
+
args.ne10 = ne10/4;
|
|
3091
|
+
args.ne0 = ne0/4;
|
|
3092
|
+
}
|
|
3093
|
+
|
|
3094
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
3095
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
3096
|
+
ggml_metal_encoder_set_buffer (enc, bid_src0, 1);
|
|
3097
|
+
ggml_metal_encoder_set_buffer (enc, bid_src1, 2);
|
|
3098
|
+
ggml_metal_encoder_set_buffer (enc, bid_dst, 3);
|
|
3099
|
+
|
|
3100
|
+
if (pipeline.cnt) {
|
|
3101
|
+
const int n = pipeline.c4 ? ggml_nelements(op)/4 : ggml_nelements(op);
|
|
3102
|
+
|
|
3103
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, n, 1, 1, 1, 1, 1);
|
|
3104
|
+
} else {
|
|
3105
|
+
const int nth_max = MIN(256, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline));
|
|
3106
|
+
|
|
3107
|
+
int nth = 1;
|
|
3108
|
+
|
|
3109
|
+
while (2*nth < args.ne0 && nth < nth_max) {
|
|
3110
|
+
nth *= 2;
|
|
3111
|
+
}
|
|
3112
|
+
|
|
3113
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, ne03, nth, 1, 1);
|
|
3114
|
+
}
|
|
3115
|
+
|
|
3116
|
+
return n_fuse;
|
|
3117
|
+
}
|
|
3118
|
+
|
|
3119
|
+
int ggml_metal_op_l2_norm(ggml_metal_op_t ctx, int idx) {
|
|
3120
|
+
ggml_tensor * op = ctx->node(idx);
|
|
3121
|
+
|
|
3122
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
3123
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
3124
|
+
|
|
3125
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
3126
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
3127
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
3128
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
3129
|
+
|
|
3130
|
+
GGML_ASSERT(ggml_is_contiguous_rows(op->src[0]));
|
|
3131
|
+
|
|
3132
|
+
ggml_metal_buffer_id bid_src0 = ggml_metal_get_buffer_id(op->src[0]);
|
|
3133
|
+
ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op);
|
|
3134
|
+
|
|
3135
|
+
float eps;
|
|
3136
|
+
memcpy(&eps, op->op_params, sizeof(float));
|
|
3137
|
+
|
|
3138
|
+
ggml_metal_kargs_l2_norm args = {
|
|
3139
|
+
/*.ne00 =*/ ne00,
|
|
3140
|
+
/*.ne01 =*/ ne01,
|
|
3141
|
+
/*.ne02 =*/ ne02,
|
|
3142
|
+
/*.ne03 =*/ ne03,
|
|
3143
|
+
/*.nb00 =*/ nb00,
|
|
3144
|
+
/*.nb01 =*/ nb01,
|
|
3145
|
+
/*.nb02 =*/ nb02,
|
|
3146
|
+
/*.nb03 =*/ nb03,
|
|
3147
|
+
/*.ne0 =*/ ne0,
|
|
3148
|
+
/*.ne1 =*/ ne1,
|
|
3149
|
+
/*.ne2 =*/ ne2,
|
|
3150
|
+
/*.ne3 =*/ ne3,
|
|
3151
|
+
/*.nb0 =*/ nb0,
|
|
3152
|
+
/*.nb1 =*/ nb1,
|
|
3153
|
+
/*.nb2 =*/ nb2,
|
|
3154
|
+
/*.nb3 =*/ nb3,
|
|
3155
|
+
/*.eps =*/ eps,
|
|
3156
|
+
};
|
|
3157
|
+
|
|
3158
|
+
auto pipeline = ggml_metal_library_get_pipeline_l2_norm(lib, op);
|
|
3159
|
+
|
|
3160
|
+
if (pipeline.c4) {
|
|
3161
|
+
args.ne00 = ne00/4;
|
|
3162
|
+
args.ne0 = ne0/4;
|
|
3163
|
+
}
|
|
3164
|
+
|
|
3165
|
+
int nth = 32; // SIMD width
|
|
3166
|
+
|
|
3167
|
+
while (nth < ne00 && nth < ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)) {
|
|
3168
|
+
nth *= 2;
|
|
3169
|
+
}
|
|
3170
|
+
|
|
3171
|
+
nth = std::min(nth, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline));
|
|
3172
|
+
|
|
3173
|
+
const size_t smem = pipeline.smem;
|
|
3174
|
+
|
|
3175
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
3176
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
3177
|
+
ggml_metal_encoder_set_buffer (enc, bid_src0, 1);
|
|
3178
|
+
ggml_metal_encoder_set_buffer (enc, bid_dst, 2);
|
|
3179
|
+
|
|
3180
|
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0);
|
|
3181
|
+
|
|
3182
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, ne03, nth, 1, 1);
|
|
3183
|
+
|
|
3184
|
+
return 1;
|
|
3185
|
+
}
|
|
3186
|
+
|
|
3187
|
+
int ggml_metal_op_group_norm(ggml_metal_op_t ctx, int idx) {
|
|
3188
|
+
ggml_tensor * op = ctx->node(idx);
|
|
3189
|
+
|
|
3190
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
3191
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
3192
|
+
|
|
3193
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
3194
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
3195
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
3196
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
3197
|
+
|
|
3198
|
+
const int32_t ngrp = ((const int32_t *) op->op_params)[0];
|
|
3199
|
+
|
|
3200
|
+
float eps;
|
|
3201
|
+
memcpy(&eps, op->op_params + 1, sizeof(float));
|
|
3202
|
+
|
|
3203
|
+
ggml_metal_kargs_group_norm args = {
|
|
3204
|
+
/*.ne00 =*/ ne00,
|
|
3205
|
+
/*.ne01 =*/ ne01,
|
|
3206
|
+
/*.ne02 =*/ ne02,
|
|
3207
|
+
/*.nb00 =*/ nb00,
|
|
3208
|
+
/*.nb01 =*/ nb01,
|
|
3209
|
+
/*.nb02 =*/ nb02,
|
|
3210
|
+
/*.ngrp =*/ ngrp,
|
|
3211
|
+
/*.eps =*/ eps,
|
|
3212
|
+
};
|
|
3213
|
+
|
|
3214
|
+
auto pipeline = ggml_metal_library_get_pipeline_group_norm(lib, op);
|
|
3215
|
+
|
|
3216
|
+
int nth = 32; // SIMD width
|
|
3217
|
+
//while (nth < ne00/4 && nth < ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)) {
|
|
3218
|
+
// nth *= 2;
|
|
3219
|
+
//}
|
|
3220
|
+
|
|
3221
|
+
//nth = std::min(nth, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline));
|
|
3222
|
+
//nth = std::min(nth, ne00/4);
|
|
3223
|
+
|
|
3224
|
+
const size_t smem = pipeline.smem;
|
|
3225
|
+
|
|
3226
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
3227
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
3228
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
3229
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2);
|
|
3230
|
+
|
|
3231
|
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0);
|
|
3232
|
+
|
|
3233
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ngrp, 1, 1, nth, 1, 1);
|
|
3234
|
+
|
|
3235
|
+
return 1;
|
|
3236
|
+
}
|
|
3237
|
+
|
|
3238
|
+
int ggml_metal_op_norm(ggml_metal_op_t ctx, int idx) {
|
|
3239
|
+
ggml_tensor * op = ctx->node(idx);
|
|
3240
|
+
|
|
3241
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
3242
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
3243
|
+
|
|
3244
|
+
const bool use_fusion = ctx->use_fusion;
|
|
3245
|
+
|
|
3246
|
+
const int debug_fusion = ctx->debug_fusion;
|
|
3247
|
+
|
|
3248
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
3249
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
3250
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
3251
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
3252
|
+
|
|
3253
|
+
float eps;
|
|
3254
|
+
memcpy(&eps, op->op_params, sizeof(float));
|
|
3255
|
+
|
|
3256
|
+
ggml_metal_buffer_id bid_src0 = ggml_metal_get_buffer_id(op->src[0]);
|
|
3257
|
+
ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op);
|
|
3258
|
+
|
|
3259
|
+
ggml_metal_kargs_norm args = {
|
|
3260
|
+
/*.ne00 =*/ ne00,
|
|
3261
|
+
/*.ne00_t =*/ ne00 % 4 == 0 ? ne00/4 : ne00,
|
|
3262
|
+
/*.nb1 =*/ nb1,
|
|
3263
|
+
/*.nb2 =*/ nb2,
|
|
3264
|
+
/*.nb3 =*/ nb3,
|
|
3265
|
+
/*.eps =*/ eps,
|
|
3266
|
+
/*.nef1 =*/ { ne01 },
|
|
3267
|
+
/*.nef2 =*/ { ne02 },
|
|
3268
|
+
/*.nef3 =*/ { ne03 },
|
|
3269
|
+
/*.nbf1 =*/ { nb01 },
|
|
3270
|
+
/*.nbf2 =*/ { nb02 },
|
|
3271
|
+
/*.nbf3 =*/ { nb03 },
|
|
3272
|
+
};
|
|
3273
|
+
|
|
3274
|
+
ggml_op fops[8];
|
|
3275
|
+
|
|
3276
|
+
int n_fuse = 1;
|
|
3277
|
+
|
|
3278
|
+
ggml_metal_buffer_id bid_fuse[2] = { bid_src0, bid_src0 };
|
|
3279
|
+
|
|
3280
|
+
// d[0] = norm(a)
|
|
3281
|
+
// d[1] = mul(d[0], b)
|
|
3282
|
+
// d[2] = add(d[1], c)
|
|
3283
|
+
if (use_fusion) {
|
|
3284
|
+
fops[0] = op->op;
|
|
3285
|
+
fops[1] = GGML_OP_MUL;
|
|
3286
|
+
fops[2] = GGML_OP_ADD;
|
|
3287
|
+
|
|
3288
|
+
for (n_fuse = 0; n_fuse <= 1; ++n_fuse) {
|
|
3289
|
+
if (!ctx->can_fuse(idx + n_fuse, fops + n_fuse, 2)) {
|
|
3290
|
+
break;
|
|
3291
|
+
}
|
|
3292
|
+
|
|
3293
|
+
ggml_tensor * f0 = ctx->node(idx + n_fuse);
|
|
3294
|
+
ggml_tensor * f1 = ctx->node(idx + n_fuse + 1);
|
|
3295
|
+
|
|
3296
|
+
if (f0 != f1->src[0]) {
|
|
3297
|
+
break;
|
|
3298
|
+
}
|
|
3299
|
+
|
|
3300
|
+
if (f1->src[1]->ne[0] != op->ne[0]) {
|
|
3301
|
+
break;
|
|
3302
|
+
}
|
|
3303
|
+
|
|
3304
|
+
if (!ggml_is_contiguous_rows(f1->src[1])) {
|
|
3305
|
+
break;
|
|
3306
|
+
}
|
|
3307
|
+
|
|
3308
|
+
if (f1->type != GGML_TYPE_F32) {
|
|
3309
|
+
break;
|
|
3310
|
+
}
|
|
3311
|
+
|
|
3312
|
+
//ctx->fuse_cnt[f1->op]++;
|
|
3313
|
+
|
|
3314
|
+
bid_fuse[n_fuse] = ggml_metal_get_buffer_id(f1->src[1]);
|
|
3315
|
+
|
|
3316
|
+
args.nef1[n_fuse + 1] = f1->src[1]->ne[1];
|
|
3317
|
+
args.nef2[n_fuse + 1] = f1->src[1]->ne[2];
|
|
3318
|
+
args.nef3[n_fuse + 1] = f1->src[1]->ne[3];
|
|
3319
|
+
|
|
3320
|
+
args.nbf1[n_fuse + 1] = f1->src[1]->nb[1];
|
|
3321
|
+
args.nbf2[n_fuse + 1] = f1->src[1]->nb[2];
|
|
3322
|
+
args.nbf3[n_fuse + 1] = f1->src[1]->nb[3];
|
|
3323
|
+
}
|
|
3324
|
+
|
|
3325
|
+
++n_fuse;
|
|
3326
|
+
|
|
3327
|
+
if (debug_fusion > 1 && n_fuse > 1) {
|
|
3328
|
+
if (n_fuse == 2) {
|
|
3329
|
+
GGML_LOG_DEBUG("%s: fuse: %s + MUL\n", __func__, ggml_op_name(op->op));
|
|
3330
|
+
}
|
|
3331
|
+
if (n_fuse == 3) {
|
|
3332
|
+
GGML_LOG_DEBUG("%s: fuse: %s + MUL + ADD\n", __func__, ggml_op_name(op->op));
|
|
3333
|
+
}
|
|
3334
|
+
}
|
|
3335
|
+
}
|
|
3336
|
+
|
|
3337
|
+
if (n_fuse > 1) {
|
|
3338
|
+
bid_dst = ggml_metal_get_buffer_id(ctx->node(idx + n_fuse - 1));
|
|
3339
|
+
|
|
3340
|
+
for (int i = 1; i < n_fuse; ++i) {
|
|
3341
|
+
if (!ggml_metal_op_concurrency_check(ctx, ctx->node(idx + i))) {
|
|
3342
|
+
ggml_metal_op_concurrency_reset(ctx);
|
|
3343
|
+
|
|
3344
|
+
break;
|
|
3345
|
+
}
|
|
3346
|
+
}
|
|
3347
|
+
}
|
|
3348
|
+
|
|
3349
|
+
auto pipeline = ggml_metal_library_get_pipeline_norm(lib, op, n_fuse);
|
|
3350
|
+
|
|
3351
|
+
int nth = 32; // SIMD width
|
|
3352
|
+
|
|
3353
|
+
while (nth < args.ne00_t && nth < ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)) {
|
|
3354
|
+
nth *= 2;
|
|
3355
|
+
}
|
|
3356
|
+
|
|
3357
|
+
nth = std::min(nth, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline));
|
|
3358
|
+
nth = std::min(nth, args.ne00_t);
|
|
3359
|
+
|
|
3360
|
+
const size_t smem = pipeline.smem;
|
|
3361
|
+
|
|
3362
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
3363
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
3364
|
+
ggml_metal_encoder_set_buffer (enc, bid_src0, 1);
|
|
3365
|
+
ggml_metal_encoder_set_buffer (enc, bid_fuse[0], 2);
|
|
3366
|
+
ggml_metal_encoder_set_buffer (enc, bid_fuse[1], 3);
|
|
3367
|
+
ggml_metal_encoder_set_buffer (enc, bid_dst, 4);
|
|
3368
|
+
|
|
3369
|
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0);
|
|
3370
|
+
|
|
3371
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, ne03, nth, 1, 1);
|
|
3372
|
+
|
|
3373
|
+
return n_fuse;
|
|
3374
|
+
}
|
|
3375
|
+
|
|
3376
|
+
int ggml_metal_op_rope(ggml_metal_op_t ctx, int idx) {
|
|
3377
|
+
ggml_tensor * op = ctx->node(idx);
|
|
3378
|
+
|
|
3379
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
3380
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
3381
|
+
|
|
3382
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
3383
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
3384
|
+
GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
|
|
3385
|
+
GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb);
|
|
3386
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
3387
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
3388
|
+
|
|
3389
|
+
// make sure we have one or more position id(ne10) per token(ne02)
|
|
3390
|
+
GGML_ASSERT(ne10 % ne02 == 0);
|
|
3391
|
+
GGML_ASSERT(ne10 >= ne02);
|
|
3392
|
+
|
|
3393
|
+
const int nth = std::min(1024, ne00);
|
|
3394
|
+
|
|
3395
|
+
const int n_past = ((const int32_t *) op->op_params)[0];
|
|
3396
|
+
const int n_dims = ((const int32_t *) op->op_params)[1];
|
|
3397
|
+
//const int mode = ((const int32_t *) op->op_params)[2];
|
|
3398
|
+
// skip 3, n_ctx, used in GLM RoPE, unimplemented in metal
|
|
3399
|
+
const int n_ctx_orig = ((const int32_t *) op->op_params)[4];
|
|
3400
|
+
|
|
3401
|
+
float freq_base;
|
|
3402
|
+
float freq_scale;
|
|
3403
|
+
float ext_factor;
|
|
3404
|
+
float attn_factor;
|
|
3405
|
+
float beta_fast;
|
|
3406
|
+
float beta_slow;
|
|
3407
|
+
|
|
3408
|
+
memcpy(&freq_base, (const int32_t *) op->op_params + 5, sizeof(float));
|
|
3409
|
+
memcpy(&freq_scale, (const int32_t *) op->op_params + 6, sizeof(float));
|
|
3410
|
+
memcpy(&ext_factor, (const int32_t *) op->op_params + 7, sizeof(float));
|
|
3411
|
+
memcpy(&attn_factor, (const int32_t *) op->op_params + 8, sizeof(float));
|
|
3412
|
+
memcpy(&beta_fast, (const int32_t *) op->op_params + 9, sizeof(float));
|
|
3413
|
+
memcpy(&beta_slow, (const int32_t *) op->op_params + 10, sizeof(float));
|
|
3414
|
+
|
|
3415
|
+
// mrope
|
|
3416
|
+
const int sect_0 = ((const int32_t *) op->op_params)[11];
|
|
3417
|
+
const int sect_1 = ((const int32_t *) op->op_params)[12];
|
|
3418
|
+
const int sect_2 = ((const int32_t *) op->op_params)[13];
|
|
3419
|
+
const int sect_3 = ((const int32_t *) op->op_params)[14];
|
|
3420
|
+
|
|
3421
|
+
ggml_metal_kargs_rope args = {
|
|
3422
|
+
/*.ne00 =*/ ne00,
|
|
3423
|
+
/*.ne01 =*/ ne01,
|
|
3424
|
+
/*.ne02 =*/ ne02,
|
|
3425
|
+
/*.ne03 =*/ ne03,
|
|
3426
|
+
/*.nb00 =*/ nb00,
|
|
3427
|
+
/*.nb01 =*/ nb01,
|
|
3428
|
+
/*.nb02 =*/ nb02,
|
|
3429
|
+
/*.nb03 =*/ nb03,
|
|
3430
|
+
/*.ne0 =*/ ne0,
|
|
3431
|
+
/*.ne1 =*/ ne1,
|
|
3432
|
+
/*.ne2 =*/ ne2,
|
|
3433
|
+
/*.ne3 =*/ ne3,
|
|
3434
|
+
/*.nb0 =*/ nb0,
|
|
3435
|
+
/*.nb1 =*/ nb1,
|
|
3436
|
+
/*.nb2 =*/ nb2,
|
|
3437
|
+
/*.nb3 =*/ nb3,
|
|
3438
|
+
/*.n_past =*/ n_past,
|
|
3439
|
+
/*.n_dims =*/ n_dims,
|
|
3440
|
+
/*.n_ctx_orig =*/ n_ctx_orig,
|
|
3441
|
+
/*.freq_base =*/ freq_base,
|
|
3442
|
+
/*.freq_scale =*/ freq_scale,
|
|
3443
|
+
/*.ext_factor =*/ ext_factor,
|
|
3444
|
+
/*.attn_factor =*/ attn_factor,
|
|
3445
|
+
/*.beta_fast =*/ beta_fast,
|
|
3446
|
+
/*.beta_slow =*/ beta_slow,
|
|
3447
|
+
/* sect_0 =*/ sect_0,
|
|
3448
|
+
/* sect_1 =*/ sect_1,
|
|
3449
|
+
/* sect_2 =*/ sect_2,
|
|
3450
|
+
/* sect_3 =*/ sect_3,
|
|
3451
|
+
/* src2 =*/ op->src[2] != nullptr,
|
|
3452
|
+
};
|
|
3453
|
+
|
|
3454
|
+
auto pipeline = ggml_metal_library_get_pipeline_rope(lib, op);
|
|
3455
|
+
|
|
3456
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
3457
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
3458
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
3459
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2);
|
|
3460
|
+
if (op->src[2]) {
|
|
3461
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[2]), 3);
|
|
3462
|
+
} else {
|
|
3463
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 3);
|
|
3464
|
+
}
|
|
3465
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 4);
|
|
3466
|
+
|
|
3467
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, ne03, nth, 1, 1);
|
|
3468
|
+
|
|
3469
|
+
return 1;
|
|
3470
|
+
}
|
|
3471
|
+
|
|
3472
|
+
int ggml_metal_op_im2col(ggml_metal_op_t ctx, int idx) {
|
|
3473
|
+
ggml_tensor * op = ctx->node(idx);
|
|
3474
|
+
|
|
3475
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
3476
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
3477
|
+
|
|
3478
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
3479
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
3480
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
3481
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
3482
|
+
|
|
3483
|
+
const int32_t s0 = ((const int32_t *)(op->op_params))[0];
|
|
3484
|
+
const int32_t s1 = ((const int32_t *)(op->op_params))[1];
|
|
3485
|
+
const int32_t p0 = ((const int32_t *)(op->op_params))[2];
|
|
3486
|
+
const int32_t p1 = ((const int32_t *)(op->op_params))[3];
|
|
3487
|
+
const int32_t d0 = ((const int32_t *)(op->op_params))[4];
|
|
3488
|
+
const int32_t d1 = ((const int32_t *)(op->op_params))[5];
|
|
3489
|
+
|
|
3490
|
+
const bool is_2D = ((const int32_t *)(op->op_params))[6] == 1;
|
|
3491
|
+
|
|
3492
|
+
const int32_t N = op->src[1]->ne[is_2D ? 3 : 2];
|
|
3493
|
+
const int32_t IC = op->src[1]->ne[is_2D ? 2 : 1];
|
|
3494
|
+
const int32_t IH = is_2D ? op->src[1]->ne[1] : 1;
|
|
3495
|
+
const int32_t IW = op->src[1]->ne[0];
|
|
3496
|
+
|
|
3497
|
+
const int32_t KH = is_2D ? op->src[0]->ne[1] : 1;
|
|
3498
|
+
const int32_t KW = op->src[0]->ne[0];
|
|
3499
|
+
|
|
3500
|
+
const int32_t OH = is_2D ? op->ne[2] : 1;
|
|
3501
|
+
const int32_t OW = op->ne[1];
|
|
3502
|
+
|
|
3503
|
+
const int32_t CHW = IC * KH * KW;
|
|
3504
|
+
|
|
3505
|
+
const uint64_t ofs0 = op->src[1]->nb[is_2D ? 3 : 2] / 4;
|
|
3506
|
+
const uint64_t ofs1 = op->src[1]->nb[is_2D ? 2 : 1] / 4;
|
|
3507
|
+
|
|
3508
|
+
ggml_metal_kargs_im2col args = {
|
|
3509
|
+
/*.ofs0 =*/ ofs0,
|
|
3510
|
+
/*.ofs1 =*/ ofs1,
|
|
3511
|
+
/*.IW =*/ IW,
|
|
3512
|
+
/*.IH =*/ IH,
|
|
3513
|
+
/*.CHW =*/ CHW,
|
|
3514
|
+
/*.s0 =*/ s0,
|
|
3515
|
+
/*.s1 =*/ s1,
|
|
3516
|
+
/*.p0 =*/ p0,
|
|
3517
|
+
/*.p1 =*/ p1,
|
|
3518
|
+
/*.d0 =*/ d0,
|
|
3519
|
+
/*.d1 =*/ d1,
|
|
3520
|
+
/*.N =*/ N,
|
|
3521
|
+
/*.KH =*/ KH,
|
|
3522
|
+
/*.KW =*/ KW,
|
|
3523
|
+
/*.KHW =*/ KH * KW,
|
|
3524
|
+
};
|
|
3525
|
+
|
|
3526
|
+
auto pipeline = ggml_metal_library_get_pipeline_im2col(lib, op);
|
|
3527
|
+
|
|
3528
|
+
GGML_ASSERT(KH*KW <= ggml_metal_pipeline_max_theads_per_threadgroup(pipeline));
|
|
3529
|
+
|
|
3530
|
+
const uint64_t ntptg0 = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)/(KH*KW), N);
|
|
3531
|
+
|
|
3532
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
3533
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
3534
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 1);
|
|
3535
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2);
|
|
3536
|
+
|
|
3537
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, IC, OH, OW, ntptg0, KH, KW);
|
|
3538
|
+
|
|
3539
|
+
return 1;
|
|
3540
|
+
}
|
|
3541
|
+
|
|
3542
|
+
int ggml_metal_op_conv_2d(ggml_metal_op_t ctx, int idx) {
|
|
3543
|
+
ggml_tensor * op = ctx->node(idx);
|
|
3544
|
+
|
|
3545
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
3546
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
3547
|
+
|
|
3548
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
3549
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
3550
|
+
GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
|
|
3551
|
+
GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb);
|
|
3552
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
3553
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
3554
|
+
|
|
3555
|
+
GGML_ASSERT(ggml_is_contiguous(op->src[0]));
|
|
3556
|
+
GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32);
|
|
3557
|
+
GGML_ASSERT(op->type == GGML_TYPE_F32);
|
|
3558
|
+
GGML_ASSERT(op->src[0]->type == GGML_TYPE_F16 || op->src[0]->type == GGML_TYPE_F32);
|
|
3559
|
+
|
|
3560
|
+
const int32_t s0 = ((const int32_t *) op->op_params)[0];
|
|
3561
|
+
const int32_t s1 = ((const int32_t *) op->op_params)[1];
|
|
3562
|
+
const int32_t p0 = ((const int32_t *) op->op_params)[2];
|
|
3563
|
+
const int32_t p1 = ((const int32_t *) op->op_params)[3];
|
|
3564
|
+
const int32_t d0 = ((const int32_t *) op->op_params)[4];
|
|
3565
|
+
const int32_t d1 = ((const int32_t *) op->op_params)[5];
|
|
3566
|
+
|
|
3567
|
+
ggml_metal_kargs_conv_2d args = {
|
|
3568
|
+
/*.nb00 =*/ nb00,
|
|
3569
|
+
/*.nb01 =*/ nb01,
|
|
3570
|
+
/*.nb02 =*/ nb02,
|
|
3571
|
+
/*.nb03 =*/ nb03,
|
|
3572
|
+
/*.nb10 =*/ nb10,
|
|
3573
|
+
/*.nb11 =*/ nb11,
|
|
3574
|
+
/*.nb12 =*/ nb12,
|
|
3575
|
+
/*.nb13 =*/ nb13,
|
|
3576
|
+
/*.nb0 =*/ nb0,
|
|
3577
|
+
/*.nb1 =*/ nb1,
|
|
3578
|
+
/*.nb2 =*/ nb2,
|
|
3579
|
+
/*.nb3 =*/ nb3,
|
|
3580
|
+
/*.IW =*/ ne10,
|
|
3581
|
+
/*.IH =*/ ne11,
|
|
3582
|
+
/*.KW =*/ ne00,
|
|
3583
|
+
/*.KH =*/ ne01,
|
|
3584
|
+
/*.IC =*/ ne02,
|
|
3585
|
+
/*.OC =*/ ne03,
|
|
3586
|
+
/*.OW =*/ ne0,
|
|
3587
|
+
/*.OH =*/ ne1,
|
|
3588
|
+
/*.N =*/ ne3,
|
|
3589
|
+
/*.s0 =*/ s0,
|
|
3590
|
+
/*.s1 =*/ s1,
|
|
3591
|
+
/*.p0 =*/ p0,
|
|
3592
|
+
/*.p1 =*/ p1,
|
|
3593
|
+
/*.d0 =*/ d0,
|
|
3594
|
+
/*.d1 =*/ d1,
|
|
3595
|
+
};
|
|
3596
|
+
|
|
3597
|
+
auto pipeline = ggml_metal_library_get_pipeline_conv_2d(lib, op);
|
|
3598
|
+
|
|
3599
|
+
int nth = ggml_metal_pipeline_max_theads_per_threadgroup(pipeline);
|
|
3600
|
+
nth = std::min(nth, 256);
|
|
3601
|
+
nth = std::max(nth, 1);
|
|
3602
|
+
|
|
3603
|
+
const uint64_t n_out = ggml_nelements(op);
|
|
3604
|
+
|
|
3605
|
+
uint64_t tg = (n_out + nth - 1)/nth;
|
|
3606
|
+
tg = std::max<uint64_t>(tg, 1);
|
|
3607
|
+
tg = std::min<uint64_t>(tg, (uint64_t) std::numeric_limits<int>::max());
|
|
3608
|
+
|
|
3609
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
3610
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
3611
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
3612
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2);
|
|
3613
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3);
|
|
3614
|
+
|
|
3615
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, tg, 1, 1, nth, 1, 1);
|
|
3616
|
+
|
|
3617
|
+
return 1;
|
|
3618
|
+
}
|
|
3619
|
+
|
|
3620
|
+
int ggml_metal_op_conv_transpose_1d(ggml_metal_op_t ctx, int idx) {
|
|
3621
|
+
ggml_tensor * op = ctx->node(idx);
|
|
3622
|
+
|
|
3623
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
3624
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
3625
|
+
|
|
3626
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
3627
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
3628
|
+
GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
|
|
3629
|
+
GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb);
|
|
3630
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
3631
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
3632
|
+
|
|
3633
|
+
const int32_t s0 = ((const int32_t *)(op->op_params))[0];
|
|
3634
|
+
|
|
3635
|
+
const int32_t IC = op->src[1]->ne[1];
|
|
3636
|
+
const int32_t IL = op->src[1]->ne[0];
|
|
3637
|
+
|
|
3638
|
+
const int32_t K = op->src[0]->ne[0];
|
|
3639
|
+
|
|
3640
|
+
const int32_t OL = op->ne[0];
|
|
3641
|
+
const int32_t OC = op->ne[1];
|
|
3642
|
+
|
|
3643
|
+
ggml_metal_kargs_conv_transpose_1d args = {
|
|
3644
|
+
/*.IC =*/ IC,
|
|
3645
|
+
/*.IL =*/ IL,
|
|
3646
|
+
/*.K =*/ K,
|
|
3647
|
+
/*.s0 =*/ s0,
|
|
3648
|
+
/*.nb0 =*/ nb0,
|
|
3649
|
+
/*.nb1 =*/ nb1,
|
|
3650
|
+
};
|
|
3651
|
+
|
|
3652
|
+
auto pipeline = ggml_metal_library_get_pipeline_conv_transpose_1d(lib, op);
|
|
3653
|
+
|
|
3654
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
3655
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
3656
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
3657
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2);
|
|
3658
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3);
|
|
3659
|
+
|
|
3660
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, OL, OC, 1, 1, 1, 1);
|
|
3661
|
+
|
|
3662
|
+
return 1;
|
|
3663
|
+
}
|
|
3664
|
+
|
|
3665
|
+
int ggml_metal_op_conv_transpose_2d(ggml_metal_op_t ctx, int idx) {
|
|
3666
|
+
ggml_tensor * op = ctx->node(idx);
|
|
3667
|
+
|
|
3668
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
3669
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
3670
|
+
|
|
3671
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
3672
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
3673
|
+
GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
|
|
3674
|
+
GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb);
|
|
3675
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
3676
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
3677
|
+
|
|
3678
|
+
const int32_t s0 = ((const int32_t *)(op->op_params))[0];
|
|
3679
|
+
|
|
3680
|
+
const int32_t IC = op->src[1]->ne[2];
|
|
3681
|
+
const int32_t IH = op->src[1]->ne[1];
|
|
3682
|
+
const int32_t IW = op->src[1]->ne[0];
|
|
3683
|
+
|
|
3684
|
+
const int32_t KH = op->src[0]->ne[1];
|
|
3685
|
+
const int32_t KW = op->src[0]->ne[0];
|
|
3686
|
+
|
|
3687
|
+
const int32_t OW = op->ne[0];
|
|
3688
|
+
const int32_t OH = op->ne[1];
|
|
3689
|
+
const int32_t OC = op->ne[2];
|
|
3690
|
+
|
|
3691
|
+
ggml_metal_kargs_conv_transpose_2d args = {
|
|
3692
|
+
/*.IC =*/ IC,
|
|
3693
|
+
/*.IH =*/ IH,
|
|
3694
|
+
/*.IW =*/ IW,
|
|
3695
|
+
/*.KH =*/ KH,
|
|
3696
|
+
/*.KW =*/ KW,
|
|
3697
|
+
/*.OC =*/ OC,
|
|
3698
|
+
/*.s0 =*/ s0,
|
|
3699
|
+
/*.nb0 =*/ nb0,
|
|
3700
|
+
/*.nb1 =*/ nb1,
|
|
3701
|
+
/*.nb2 =*/ nb2,
|
|
3702
|
+
};
|
|
3703
|
+
|
|
3704
|
+
auto pipeline = ggml_metal_library_get_pipeline_conv_transpose_2d(lib, op);
|
|
3705
|
+
|
|
3706
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
3707
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
3708
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
3709
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2);
|
|
3710
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3);
|
|
3711
|
+
|
|
3712
|
+
// Metal requires buffer size to be multiple of 16 bytes
|
|
3713
|
+
const size_t smem = GGML_PAD(KW * KH * sizeof(float), 16);
|
|
3714
|
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0);
|
|
3715
|
+
|
|
3716
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, OW, OH, OC, KW, KH, 1);
|
|
3717
|
+
|
|
3718
|
+
return 1;
|
|
3719
|
+
}
|
|
3720
|
+
|
|
3721
|
+
int ggml_metal_op_upscale(ggml_metal_op_t ctx, int idx) {
|
|
3722
|
+
ggml_tensor * op = ctx->node(idx);
|
|
3723
|
+
|
|
3724
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
3725
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
3726
|
+
|
|
3727
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
3728
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
3729
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
3730
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
3731
|
+
|
|
3732
|
+
const float sf0 = (float)ne0/op->src[0]->ne[0];
|
|
3733
|
+
const float sf1 = (float)ne1/op->src[0]->ne[1];
|
|
3734
|
+
const float sf2 = (float)ne2/op->src[0]->ne[2];
|
|
3735
|
+
const float sf3 = (float)ne3/op->src[0]->ne[3];
|
|
3736
|
+
|
|
3737
|
+
ggml_metal_kargs_upscale args = {
|
|
3738
|
+
/*.ne00 =*/ ne00,
|
|
3739
|
+
/*.ne01 =*/ ne01,
|
|
3740
|
+
/*.ne02 =*/ ne02,
|
|
3741
|
+
/*.ne03 =*/ ne03,
|
|
3742
|
+
/*.nb00 =*/ nb00,
|
|
3743
|
+
/*.nb01 =*/ nb01,
|
|
3744
|
+
/*.nb02 =*/ nb02,
|
|
3745
|
+
/*.nb03 =*/ nb03,
|
|
3746
|
+
/*.ne0 =*/ ne0,
|
|
3747
|
+
/*.ne1 =*/ ne1,
|
|
3748
|
+
/*.ne2 =*/ ne2,
|
|
3749
|
+
/*.ne3 =*/ ne3,
|
|
3750
|
+
/*.nb0 =*/ nb0,
|
|
3751
|
+
/*.nb1 =*/ nb1,
|
|
3752
|
+
/*.nb2 =*/ nb2,
|
|
3753
|
+
/*.nb3 =*/ nb3,
|
|
3754
|
+
/*.sf0 =*/ sf0,
|
|
3755
|
+
/*.sf1 =*/ sf1,
|
|
3756
|
+
/*.sf2 =*/ sf2,
|
|
3757
|
+
/*.sf3 =*/ sf3
|
|
3758
|
+
};
|
|
3759
|
+
|
|
3760
|
+
auto pipeline = ggml_metal_library_get_pipeline_upscale(lib, op);
|
|
3761
|
+
|
|
3762
|
+
const int nth = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), ne0);
|
|
3763
|
+
|
|
3764
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
3765
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
3766
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
3767
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2);
|
|
3768
|
+
|
|
3769
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ne1, ne2, ne3, nth, 1, 1);
|
|
3770
|
+
|
|
3771
|
+
return 1;
|
|
3772
|
+
}
|
|
3773
|
+
|
|
3774
|
+
int ggml_metal_op_pad(ggml_metal_op_t ctx, int idx) {
|
|
3775
|
+
ggml_tensor * op = ctx->node(idx);
|
|
3776
|
+
|
|
3777
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
3778
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
3779
|
+
|
|
3780
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
3781
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
3782
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
3783
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
3784
|
+
|
|
3785
|
+
ggml_metal_kargs_pad args = {
|
|
3786
|
+
/*.ne00 =*/ ne00,
|
|
3787
|
+
/*.ne01 =*/ ne01,
|
|
3788
|
+
/*.ne02 =*/ ne02,
|
|
3789
|
+
/*.ne03 =*/ ne03,
|
|
3790
|
+
/*.nb00 =*/ nb00,
|
|
3791
|
+
/*.nb01 =*/ nb01,
|
|
3792
|
+
/*.nb02 =*/ nb02,
|
|
3793
|
+
/*.nb03 =*/ nb03,
|
|
3794
|
+
/*.ne0 =*/ ne0,
|
|
3795
|
+
/*.ne1 =*/ ne1,
|
|
3796
|
+
/*.ne2 =*/ ne2,
|
|
3797
|
+
/*.ne3 =*/ ne3,
|
|
3798
|
+
/*.nb0 =*/ nb0,
|
|
3799
|
+
/*.nb1 =*/ nb1,
|
|
3800
|
+
/*.nb2 =*/ nb2,
|
|
3801
|
+
/*.nb3 =*/ nb3
|
|
3802
|
+
};
|
|
3803
|
+
|
|
3804
|
+
auto pipeline = ggml_metal_library_get_pipeline_pad(lib, op);
|
|
3805
|
+
|
|
3806
|
+
const int nth = std::min(1024, ne0);
|
|
3807
|
+
|
|
3808
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
3809
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
3810
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
3811
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2);
|
|
3812
|
+
|
|
3813
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ne1, ne2, ne3, nth, 1, 1);
|
|
3814
|
+
|
|
3815
|
+
return 1;
|
|
3816
|
+
}
|
|
3817
|
+
|
|
3818
|
+
int ggml_metal_op_pad_reflect_1d(ggml_metal_op_t ctx, int idx) {
|
|
3819
|
+
ggml_tensor * op = ctx->node(idx);
|
|
3820
|
+
|
|
3821
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
3822
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
3823
|
+
|
|
3824
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
3825
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
3826
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
3827
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
3828
|
+
|
|
3829
|
+
ggml_metal_kargs_pad_reflect_1d args = {
|
|
3830
|
+
/*.ne00 =*/ ne00,
|
|
3831
|
+
/*.ne01 =*/ ne01,
|
|
3832
|
+
/*.ne02 =*/ ne02,
|
|
3833
|
+
/*.ne03 =*/ ne03,
|
|
3834
|
+
/*.nb00 =*/ nb00,
|
|
3835
|
+
/*.nb01 =*/ nb01,
|
|
3836
|
+
/*.nb02 =*/ nb02,
|
|
3837
|
+
/*.nb03 =*/ nb03,
|
|
3838
|
+
/*.ne0 =*/ ne0,
|
|
3839
|
+
/*.ne1 =*/ ne1,
|
|
3840
|
+
/*.ne2 =*/ ne2,
|
|
3841
|
+
/*.ne3 =*/ ne3,
|
|
3842
|
+
/*.nb0 =*/ nb0,
|
|
3843
|
+
/*.nb1 =*/ nb1,
|
|
3844
|
+
/*.nb2 =*/ nb2,
|
|
3845
|
+
/*.nb3 =*/ nb3,
|
|
3846
|
+
/*.p0 =*/ ((const int32_t *)(op->op_params))[0],
|
|
3847
|
+
/*.p1 =*/ ((const int32_t *)(op->op_params))[1]
|
|
3848
|
+
};
|
|
3849
|
+
|
|
3850
|
+
auto pipeline = ggml_metal_library_get_pipeline_pad_reflect_1d(lib, op);
|
|
3851
|
+
|
|
3852
|
+
const int nth = std::min(1024, ne0);
|
|
3853
|
+
|
|
3854
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
3855
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
3856
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
3857
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2);
|
|
3858
|
+
|
|
3859
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ne1, ne2, ne3, nth, 1, 1);
|
|
3860
|
+
|
|
3861
|
+
return 1;
|
|
3862
|
+
}
|
|
3863
|
+
|
|
3864
|
+
int ggml_metal_op_arange(ggml_metal_op_t ctx, int idx) {
|
|
3865
|
+
ggml_tensor * op = ctx->node(idx);
|
|
3866
|
+
|
|
3867
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
3868
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
3869
|
+
|
|
3870
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
3871
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
3872
|
+
|
|
3873
|
+
float start;
|
|
3874
|
+
float step;
|
|
3875
|
+
|
|
3876
|
+
memcpy(&start, ((const int32_t *) op->op_params) + 0, sizeof(float));
|
|
3877
|
+
memcpy(&step, ((const int32_t *) op->op_params) + 2, sizeof(float));
|
|
3878
|
+
|
|
3879
|
+
ggml_metal_kargs_arange args = {
|
|
3880
|
+
/*.ne0 =*/ ne0,
|
|
3881
|
+
/*.start =*/ start,
|
|
3882
|
+
/*.step =*/ step
|
|
3883
|
+
};
|
|
3884
|
+
|
|
3885
|
+
const int nth = std::min(1024, ne0);
|
|
3886
|
+
|
|
3887
|
+
auto pipeline = ggml_metal_library_get_pipeline_arange(lib, op);
|
|
3888
|
+
|
|
3889
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
3890
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
3891
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 1);
|
|
3892
|
+
|
|
3893
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, 1, 1, 1, nth, 1, 1);
|
|
3894
|
+
|
|
3895
|
+
return 1;
|
|
3896
|
+
}
|
|
3897
|
+
|
|
3898
|
+
int ggml_metal_op_timestep_embedding(ggml_metal_op_t ctx, int idx) {
|
|
3899
|
+
ggml_tensor * op = ctx->node(idx);
|
|
3900
|
+
|
|
3901
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
3902
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
3903
|
+
|
|
3904
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
3905
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
3906
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
3907
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
3908
|
+
|
|
3909
|
+
const int dim = op->op_params[0];
|
|
3910
|
+
const int max_period = op->op_params[1];
|
|
3911
|
+
|
|
3912
|
+
ggml_metal_kargs_timestep_embedding args = {
|
|
3913
|
+
/*.nb1 =*/ nb1,
|
|
3914
|
+
/*.dim =*/ dim,
|
|
3915
|
+
/*.max_period =*/ max_period,
|
|
3916
|
+
};
|
|
3917
|
+
|
|
3918
|
+
auto pipeline = ggml_metal_library_get_pipeline_timestep_embedding(lib, op);
|
|
3919
|
+
|
|
3920
|
+
const int nth = std::max(1, std::min(1024, dim/2));
|
|
3921
|
+
|
|
3922
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
3923
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
3924
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
3925
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2);
|
|
3926
|
+
|
|
3927
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ne00, 1, 1, nth, 1, 1);
|
|
3928
|
+
|
|
3929
|
+
return 1;
|
|
3930
|
+
}
|
|
3931
|
+
|
|
3932
|
+
int ggml_metal_op_argmax(ggml_metal_op_t ctx, int idx) {
|
|
3933
|
+
ggml_tensor * op = ctx->node(idx);
|
|
3934
|
+
|
|
3935
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
3936
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
3937
|
+
|
|
3938
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
3939
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
3940
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
3941
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
3942
|
+
|
|
3943
|
+
ggml_metal_kargs_argmax args = {
|
|
3944
|
+
/*.ne00 = */ ne00,
|
|
3945
|
+
/*.nb01 = */ nb01,
|
|
3946
|
+
};
|
|
3947
|
+
|
|
3948
|
+
auto pipeline = ggml_metal_library_get_pipeline_argmax(lib, op);
|
|
3949
|
+
|
|
3950
|
+
const int64_t nrows = ggml_nrows(op->src[0]);
|
|
3951
|
+
|
|
3952
|
+
int nth = 32; // SIMD width
|
|
3953
|
+
while (nth < ne00 && nth*ne01*ne02*ne03 < 256) {
|
|
3954
|
+
nth *= 2;
|
|
3955
|
+
}
|
|
3956
|
+
|
|
3957
|
+
const size_t smem = pipeline.smem;
|
|
3958
|
+
|
|
3959
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
3960
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
3961
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
3962
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2);
|
|
3963
|
+
|
|
3964
|
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0);
|
|
3965
|
+
|
|
3966
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, nrows, 1, 1, nth, 1, 1);
|
|
3967
|
+
|
|
3968
|
+
return 1;
|
|
3969
|
+
}
|
|
3970
|
+
|
|
3971
|
+
int ggml_metal_op_argsort(ggml_metal_op_t ctx, int idx) {
|
|
3972
|
+
ggml_tensor * op = ctx->node(idx);
|
|
3973
|
+
|
|
3974
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
3975
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
3976
|
+
|
|
3977
|
+
GGML_ASSERT(ggml_is_contiguous_rows(op->src[0]));
|
|
3978
|
+
|
|
3979
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
3980
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
3981
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
3982
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
3983
|
+
|
|
3984
|
+
auto pipeline = ggml_metal_library_get_pipeline_argsort(lib, op);
|
|
3985
|
+
|
|
3986
|
+
// bitonic sort requires the number of elements to be power of 2
|
|
3987
|
+
int nth = 1;
|
|
3988
|
+
while (nth < ne00 && 2*nth <= ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)) {
|
|
3989
|
+
nth *= 2;
|
|
3990
|
+
}
|
|
3991
|
+
|
|
3992
|
+
const int npr = (ne00 + nth - 1)/nth;
|
|
3993
|
+
|
|
3994
|
+
// Metal kernels require the buffer size to be multiple of 16 bytes
|
|
3995
|
+
// https://developer.apple.com/documentation/metal/mtlcomputecommandencoder/1443142-setthreadgroupmemorylength
|
|
3996
|
+
const size_t smem = GGML_PAD(nth*sizeof(int32_t), 16);
|
|
3997
|
+
|
|
3998
|
+
ggml_metal_buffer_id bid_src0 = ggml_metal_get_buffer_id(op->src[0]);
|
|
3999
|
+
ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op);
|
|
4000
|
+
|
|
4001
|
+
ggml_metal_buffer_id bid_tmp = bid_dst;
|
|
4002
|
+
bid_tmp.offs += ggml_nbytes(op);
|
|
4003
|
+
|
|
4004
|
+
if ((int) ceil(std::log(npr) / std::log(2)) % 2 == 1) {
|
|
4005
|
+
std::swap(bid_dst, bid_tmp);
|
|
4006
|
+
}
|
|
4007
|
+
|
|
4008
|
+
ggml_metal_kargs_argsort args = {
|
|
4009
|
+
/*.ne00 =*/ ne00,
|
|
4010
|
+
/*.ne01 =*/ ne01,
|
|
4011
|
+
/*.ne02 =*/ ne02,
|
|
4012
|
+
/*.ne03 =*/ ne03,
|
|
4013
|
+
/*.nb00 =*/ nb00,
|
|
4014
|
+
/*.nb01 =*/ nb01,
|
|
4015
|
+
/*.nb02 =*/ nb02,
|
|
4016
|
+
/*.nb03 =*/ nb03,
|
|
4017
|
+
/*.ne0 =*/ ne0,
|
|
4018
|
+
/*.ne1 =*/ ne1,
|
|
4019
|
+
/*.ne2 =*/ ne2,
|
|
4020
|
+
/*.ne3 =*/ ne3,
|
|
4021
|
+
/*.top_k =*/ nth,
|
|
4022
|
+
};
|
|
4023
|
+
|
|
4024
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
4025
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
4026
|
+
ggml_metal_encoder_set_buffer (enc, bid_src0, 1);
|
|
4027
|
+
ggml_metal_encoder_set_buffer (enc, bid_dst, 2);
|
|
4028
|
+
|
|
4029
|
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0);
|
|
4030
|
+
|
|
4031
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, npr*ne01, ne02, ne03, nth, 1, 1);
|
|
4032
|
+
|
|
4033
|
+
auto pipeline_merge = ggml_metal_library_get_pipeline_argsort_merge(lib, op);
|
|
4034
|
+
|
|
4035
|
+
int len = nth;
|
|
4036
|
+
|
|
4037
|
+
while (len < ne00) {
|
|
4038
|
+
ggml_metal_op_concurrency_reset(ctx);
|
|
4039
|
+
|
|
4040
|
+
ggml_metal_kargs_argsort_merge args_merge = {
|
|
4041
|
+
/*.ne00 =*/ ne00,
|
|
4042
|
+
/*.ne01 =*/ ne01,
|
|
4043
|
+
/*.ne02 =*/ ne02,
|
|
4044
|
+
/*.ne03 =*/ ne03,
|
|
4045
|
+
/*.nb00 =*/ nb00,
|
|
4046
|
+
/*.nb01 =*/ nb01,
|
|
4047
|
+
/*.nb02 =*/ nb02,
|
|
4048
|
+
/*.nb03 =*/ nb03,
|
|
4049
|
+
/*.ne0 =*/ ne0,
|
|
4050
|
+
/*.ne1 =*/ ne1,
|
|
4051
|
+
/*.ne2 =*/ ne2,
|
|
4052
|
+
/*.ne3 =*/ ne3,
|
|
4053
|
+
/*.top_k =*/ ne00,
|
|
4054
|
+
/*.len =*/ len,
|
|
4055
|
+
};
|
|
4056
|
+
|
|
4057
|
+
// merges per row
|
|
4058
|
+
const int nm = (ne00 + 2*len - 1) / (2*len);
|
|
4059
|
+
|
|
4060
|
+
const int nth = std::min(512, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline_merge));
|
|
4061
|
+
|
|
4062
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline_merge);
|
|
4063
|
+
ggml_metal_encoder_set_bytes (enc, &args_merge, sizeof(args_merge), 0);
|
|
4064
|
+
ggml_metal_encoder_set_buffer (enc, bid_src0, 1);
|
|
4065
|
+
ggml_metal_encoder_set_buffer (enc, bid_dst, 2);
|
|
4066
|
+
ggml_metal_encoder_set_buffer (enc, bid_tmp, 3);
|
|
4067
|
+
|
|
4068
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, nm*ne01, ne02, ne03, nth, 1, 1);
|
|
4069
|
+
|
|
4070
|
+
std::swap(bid_dst, bid_tmp);
|
|
4071
|
+
|
|
4072
|
+
len <<= 1;
|
|
4073
|
+
}
|
|
4074
|
+
|
|
4075
|
+
return 1;
|
|
4076
|
+
}
|
|
4077
|
+
|
|
4078
|
+
int ggml_metal_op_top_k(ggml_metal_op_t ctx, int idx) {
|
|
4079
|
+
ggml_tensor * op = ctx->node(idx);
|
|
4080
|
+
|
|
4081
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
4082
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
4083
|
+
|
|
4084
|
+
GGML_ASSERT(ggml_is_contiguous_rows(op->src[0]));
|
|
4085
|
+
|
|
4086
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
4087
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
4088
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
4089
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
4090
|
+
|
|
4091
|
+
auto pipeline = ggml_metal_library_get_pipeline_top_k(lib, op);
|
|
4092
|
+
|
|
4093
|
+
// bitonic sort requires the number of elements to be power of 2
|
|
4094
|
+
int nth = 1;
|
|
4095
|
+
while (nth < ne00 && 2*nth <= ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)) {
|
|
4096
|
+
nth *= 2;
|
|
4097
|
+
}
|
|
4098
|
+
|
|
4099
|
+
// blocks per row
|
|
4100
|
+
const int npr = (ne00 + nth - 1)/nth;
|
|
4101
|
+
|
|
4102
|
+
const size_t smem = GGML_PAD(nth*sizeof(int32_t), 16);
|
|
4103
|
+
|
|
4104
|
+
ggml_metal_buffer_id bid_src0 = ggml_metal_get_buffer_id(op->src[0]);
|
|
4105
|
+
ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op);
|
|
4106
|
+
|
|
4107
|
+
ggml_metal_buffer_id bid_tmp = bid_dst;
|
|
4108
|
+
bid_tmp.offs += sizeof(int32_t)*ggml_nelements(op->src[0]);
|
|
4109
|
+
|
|
4110
|
+
if ((int) ceil(std::log(npr) / std::log(2)) % 2 == 1) {
|
|
4111
|
+
std::swap(bid_dst, bid_tmp);
|
|
4112
|
+
}
|
|
4113
|
+
|
|
4114
|
+
const int top_k = ne0;
|
|
4115
|
+
|
|
4116
|
+
ggml_metal_kargs_argsort args = {
|
|
4117
|
+
/*.ne00 =*/ ne00,
|
|
4118
|
+
/*.ne01 =*/ ne01,
|
|
4119
|
+
/*.ne02 =*/ ne02,
|
|
4120
|
+
/*.ne03 =*/ ne03,
|
|
4121
|
+
/*.nb00 =*/ nb00,
|
|
4122
|
+
/*.nb01 =*/ nb01,
|
|
4123
|
+
/*.nb02 =*/ nb02,
|
|
4124
|
+
/*.nb03 =*/ nb03,
|
|
4125
|
+
/*.ne0 =*/ ne0,
|
|
4126
|
+
/*.ne1 =*/ ne1,
|
|
4127
|
+
/*.ne2 =*/ ne2,
|
|
4128
|
+
/*.ne3 =*/ ne3,
|
|
4129
|
+
/*.top_k =*/ std::min(nth, top_k), // for each block, keep just the top_k indices
|
|
4130
|
+
};
|
|
4131
|
+
|
|
4132
|
+
if (npr > 1) {
|
|
4133
|
+
args.ne0 = (npr - 1)*args.top_k + std::min(ne00 - (npr - 1)*nth, args.top_k);
|
|
4134
|
+
}
|
|
4135
|
+
|
|
4136
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
4137
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
4138
|
+
ggml_metal_encoder_set_buffer (enc, bid_src0, 1);
|
|
4139
|
+
ggml_metal_encoder_set_buffer (enc, bid_dst, 2);
|
|
4140
|
+
|
|
4141
|
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0);
|
|
4142
|
+
|
|
4143
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, npr*ne01, ne02, ne03, nth, 1, 1);
|
|
4144
|
+
|
|
4145
|
+
auto pipeline_merge = ggml_metal_library_get_pipeline_top_k_merge(lib, op);
|
|
4146
|
+
|
|
4147
|
+
int len = args.top_k;
|
|
4148
|
+
|
|
4149
|
+
while (len < args.ne0) {
|
|
4150
|
+
ggml_metal_op_concurrency_reset(ctx);
|
|
4151
|
+
|
|
4152
|
+
// merges per row
|
|
4153
|
+
const int nm = (args.ne0 + 2*len - 1) / (2*len);
|
|
4154
|
+
|
|
4155
|
+
const int nth = std::min(512, std::min(len, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline_merge)));
|
|
4156
|
+
|
|
4157
|
+
ggml_metal_kargs_argsort_merge args_merge = {
|
|
4158
|
+
/*.ne00 =*/ ne00,
|
|
4159
|
+
/*.ne01 =*/ ne01,
|
|
4160
|
+
/*.ne02 =*/ ne02,
|
|
4161
|
+
/*.ne03 =*/ ne03,
|
|
4162
|
+
/*.nb00 =*/ nb00,
|
|
4163
|
+
/*.nb01 =*/ nb01,
|
|
4164
|
+
/*.nb02 =*/ nb02,
|
|
4165
|
+
/*.nb03 =*/ nb03,
|
|
4166
|
+
/*.ne0 =*/ args.ne0,
|
|
4167
|
+
/*.ne1 =*/ ne1,
|
|
4168
|
+
/*.ne2 =*/ ne2,
|
|
4169
|
+
/*.ne3 =*/ ne3,
|
|
4170
|
+
/*.top_k =*/ nm == 1 ? top_k : args.ne0, // the final merge outputs top_k elements
|
|
4171
|
+
/*.len =*/ len,
|
|
4172
|
+
};
|
|
4173
|
+
|
|
4174
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline_merge);
|
|
4175
|
+
ggml_metal_encoder_set_bytes (enc, &args_merge, sizeof(args_merge), 0);
|
|
4176
|
+
ggml_metal_encoder_set_buffer (enc, bid_src0, 1);
|
|
4177
|
+
ggml_metal_encoder_set_buffer (enc, bid_dst, 2);
|
|
4178
|
+
ggml_metal_encoder_set_buffer (enc, bid_tmp, 3);
|
|
4179
|
+
|
|
4180
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, nm*ne01, ne02, ne03, nth, 1, 1);
|
|
4181
|
+
|
|
4182
|
+
std::swap(bid_dst, bid_tmp);
|
|
4183
|
+
|
|
4184
|
+
len <<= 1;
|
|
4185
|
+
}
|
|
4186
|
+
|
|
4187
|
+
return 1;
|
|
4188
|
+
}
|
|
4189
|
+
|
|
4190
|
+
int ggml_metal_op_tri(ggml_metal_op_t ctx, int idx) {
|
|
4191
|
+
ggml_tensor * op = ctx->node(idx);
|
|
4192
|
+
|
|
4193
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
4194
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
4195
|
+
|
|
4196
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
4197
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
4198
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
4199
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
4200
|
+
|
|
4201
|
+
ggml_metal_kargs_tri args = {
|
|
4202
|
+
/*.ne00 =*/ ne00,
|
|
4203
|
+
/*.ne01 =*/ ne01,
|
|
4204
|
+
/*.ne02 =*/ ne02,
|
|
4205
|
+
/*.ne03 =*/ ne03,
|
|
4206
|
+
/*.nb00 =*/ nb00,
|
|
4207
|
+
/*.nb01 =*/ nb01,
|
|
4208
|
+
/*.nb02 =*/ nb02,
|
|
4209
|
+
/*.nb03 =*/ nb03,
|
|
4210
|
+
/*.ne0 =*/ ne0,
|
|
4211
|
+
/*.ne1 =*/ ne1,
|
|
4212
|
+
/*.ne2 =*/ ne2,
|
|
4213
|
+
/*.ne3 =*/ ne3,
|
|
4214
|
+
/*.nb0 =*/ nb0,
|
|
4215
|
+
/*.nb1 =*/ nb1,
|
|
4216
|
+
/*.nb2 =*/ nb2,
|
|
4217
|
+
/*.nb3 =*/ nb3,
|
|
4218
|
+
};
|
|
4219
|
+
|
|
4220
|
+
auto pipeline = ggml_metal_library_get_pipeline_tri(lib, op);
|
|
4221
|
+
|
|
4222
|
+
int nth = 32; // SIMD width
|
|
4223
|
+
|
|
4224
|
+
while (nth < ne00 && nth < ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)) {
|
|
4225
|
+
nth *= 2;
|
|
4226
|
+
}
|
|
4227
|
+
|
|
4228
|
+
nth = std::min(nth, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline));
|
|
4229
|
+
nth = std::min(nth, ne00);
|
|
4230
|
+
|
|
4231
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
4232
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
|
|
4233
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
4234
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2);
|
|
4235
|
+
|
|
4236
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, ne03, nth, 1, 1);
|
|
4237
|
+
|
|
4238
|
+
return 1;
|
|
4239
|
+
}
|
|
4240
|
+
|
|
4241
|
+
int ggml_metal_op_opt_step_adamw(ggml_metal_op_t ctx, int idx) {
|
|
4242
|
+
ggml_tensor * op = ctx->node(idx);
|
|
4243
|
+
|
|
4244
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
4245
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
4246
|
+
|
|
4247
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
4248
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
4249
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
4250
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
4251
|
+
|
|
4252
|
+
auto pipeline = ggml_metal_library_get_pipeline_opt_step_adamw(lib, op);
|
|
4253
|
+
|
|
4254
|
+
const int64_t np = ggml_nelements(op->src[0]);
|
|
4255
|
+
ggml_metal_kargs_opt_step_adamw args = {
|
|
4256
|
+
/*.np =*/ np,
|
|
4257
|
+
};
|
|
4258
|
+
|
|
4259
|
+
int ida = 0;
|
|
4260
|
+
|
|
4261
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
4262
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), ida++);
|
|
4263
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), ida++);
|
|
4264
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), ida++);
|
|
4265
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[2]), ida++);
|
|
4266
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[3]), ida++);
|
|
4267
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[4]), ida++);
|
|
4268
|
+
|
|
4269
|
+
const int nth = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), ne0);
|
|
4270
|
+
const int64_t n = (np + nth - 1) / nth;
|
|
4271
|
+
|
|
4272
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, n, 1, 1, nth, 1, 1);
|
|
4273
|
+
|
|
4274
|
+
return 1;
|
|
4275
|
+
}
|
|
4276
|
+
|
|
4277
|
+
int ggml_metal_op_opt_step_sgd(ggml_metal_op_t ctx, int idx) {
|
|
4278
|
+
ggml_tensor * op = ctx->node(idx);
|
|
4279
|
+
|
|
4280
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
4281
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
4282
|
+
|
|
4283
|
+
GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
|
|
4284
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
4285
|
+
GGML_TENSOR_LOCALS( int32_t, ne, op, ne);
|
|
4286
|
+
GGML_TENSOR_LOCALS(uint64_t, nb, op, nb);
|
|
4287
|
+
|
|
4288
|
+
auto pipeline = ggml_metal_library_get_pipeline_opt_step_sgd(lib, op);
|
|
4289
|
+
|
|
4290
|
+
const int64_t np = ggml_nelements(op->src[0]);
|
|
4291
|
+
ggml_metal_kargs_opt_step_sgd args = {
|
|
4292
|
+
/*.np =*/ np,
|
|
4293
|
+
};
|
|
4294
|
+
|
|
4295
|
+
int ida = 0;
|
|
4296
|
+
|
|
4297
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
4298
|
+
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), ida++);
|
|
4299
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), ida++);
|
|
4300
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), ida++);
|
|
4301
|
+
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[2]), ida++);
|
|
4302
|
+
|
|
4303
|
+
const int nth = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), ne0);
|
|
4304
|
+
const int64_t n = (np + nth - 1) / nth;
|
|
4305
|
+
|
|
4306
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, n, 1, 1, nth, 1, 1);
|
|
4307
|
+
|
|
4308
|
+
return 1;
|
|
4309
|
+
}
|
|
4310
|
+
|
|
4311
|
+
int ggml_metal_op_count_equal(ggml_metal_op_t ctx, int idx) {
|
|
4312
|
+
ggml_tensor * op = ctx->node(idx);
|
|
4313
|
+
|
|
4314
|
+
ggml_metal_library_t lib = ctx->lib;
|
|
4315
|
+
ggml_metal_encoder_t enc = ctx->enc;
|
|
4316
|
+
|
|
4317
|
+
GGML_TENSOR_LOCALS(int32_t, ne0, op->src[0], ne);
|
|
4318
|
+
GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb);
|
|
4319
|
+
GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb);
|
|
4320
|
+
|
|
4321
|
+
{
|
|
4322
|
+
ggml_metal_kargs_memset args = { /*.val =*/ 0 };
|
|
4323
|
+
|
|
4324
|
+
auto pipeline = ggml_metal_library_get_pipeline_memset(lib, op);
|
|
4325
|
+
|
|
4326
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
4327
|
+
ggml_metal_encoder_set_bytes(enc, &args, sizeof(args), 0);
|
|
4328
|
+
ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op), 1);
|
|
4329
|
+
|
|
4330
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, 1, 1, 1, 1, 1, 1);
|
|
4331
|
+
}
|
|
4332
|
+
|
|
4333
|
+
ggml_metal_op_concurrency_reset(ctx);
|
|
4334
|
+
|
|
4335
|
+
{
|
|
4336
|
+
ggml_metal_kargs_count_equal args = {
|
|
4337
|
+
/*.ne00 =*/ ne00,
|
|
4338
|
+
/*.ne01 =*/ ne01,
|
|
4339
|
+
/*.ne02 =*/ ne02,
|
|
4340
|
+
/*.ne03 =*/ ne03,
|
|
4341
|
+
/*.nb00 =*/ nb00,
|
|
4342
|
+
/*.nb01 =*/ nb01,
|
|
4343
|
+
/*.nb02 =*/ nb02,
|
|
4344
|
+
/*.nb03 =*/ nb03,
|
|
4345
|
+
/*.nb10 =*/ nb10,
|
|
4346
|
+
/*.nb11 =*/ nb11,
|
|
4347
|
+
/*.nb12 =*/ nb12,
|
|
4348
|
+
/*.nb13 =*/ nb13,
|
|
4349
|
+
};
|
|
4350
|
+
|
|
4351
|
+
auto pipeline = ggml_metal_library_get_pipeline_count_equal(lib, op);
|
|
4352
|
+
|
|
4353
|
+
const size_t smem = pipeline.smem;
|
|
4354
|
+
|
|
4355
|
+
const int nth = 32*pipeline.nsg;
|
|
4356
|
+
|
|
4357
|
+
GGML_ASSERT(nth <= ggml_metal_pipeline_max_theads_per_threadgroup(pipeline));
|
|
4358
|
+
|
|
4359
|
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
|
|
4360
|
+
ggml_metal_encoder_set_bytes(enc, &args, sizeof(args), 0);
|
|
4361
|
+
ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[0]), 1);
|
|
4362
|
+
ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[1]), 2);
|
|
4363
|
+
ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op), 3);
|
|
4364
|
+
|
|
4365
|
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0);
|
|
4366
|
+
ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, ne03, nth, 1, 1);
|
|
4367
|
+
}
|
|
4368
|
+
|
|
4369
|
+
return 1;
|
|
4370
|
+
}
|