@novastera-oss/llamarn 0.2.9 → 0.3.1

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.
Files changed (314) hide show
  1. package/android/build.gradle +2 -1
  2. package/android/proguard-rules.pro +12 -0
  3. package/android/src/main/cpp/include/llama.h +15 -47
  4. package/android/src/main/jniLibs/arm64-v8a/libggml-base.so +0 -0
  5. package/android/src/main/jniLibs/arm64-v8a/libggml-cpu.so +0 -0
  6. package/android/src/main/jniLibs/arm64-v8a/libggml.so +0 -0
  7. package/android/src/main/jniLibs/arm64-v8a/libllama.so +0 -0
  8. package/android/src/main/jniLibs/armeabi-v7a/libggml-base.so +0 -0
  9. package/android/src/main/jniLibs/armeabi-v7a/libggml-cpu.so +0 -0
  10. package/android/src/main/jniLibs/armeabi-v7a/libggml.so +0 -0
  11. package/android/src/main/jniLibs/armeabi-v7a/libllama.so +0 -0
  12. package/android/src/main/jniLibs/x86/libggml-base.so +0 -0
  13. package/android/src/main/jniLibs/x86/libggml-cpu.so +0 -0
  14. package/android/src/main/jniLibs/x86/libggml.so +0 -0
  15. package/android/src/main/jniLibs/x86/libllama.so +0 -0
  16. package/android/src/main/jniLibs/x86_64/libggml-base.so +0 -0
  17. package/android/src/main/jniLibs/x86_64/libggml-cpu.so +0 -0
  18. package/android/src/main/jniLibs/x86_64/libggml.so +0 -0
  19. package/android/src/main/jniLibs/x86_64/libllama.so +0 -0
  20. package/cpp/build-info.cpp +2 -2
  21. package/cpp/llama.cpp/CMakeLists.txt +0 -1
  22. package/cpp/llama.cpp/CMakePresets.json +11 -0
  23. package/cpp/llama.cpp/CODEOWNERS +1 -0
  24. package/cpp/llama.cpp/README.md +8 -8
  25. package/cpp/llama.cpp/build-xcframework.sh +1 -1
  26. package/cpp/llama.cpp/common/CMakeLists.txt +4 -5
  27. package/cpp/llama.cpp/common/arg.cpp +62 -1
  28. package/cpp/llama.cpp/common/chat.cpp +37 -20
  29. package/cpp/llama.cpp/common/chat.h +2 -0
  30. package/cpp/llama.cpp/common/common.cpp +22 -6
  31. package/cpp/llama.cpp/common/common.h +22 -4
  32. package/cpp/llama.cpp/convert_hf_to_gguf.py +1250 -43
  33. package/cpp/llama.cpp/convert_hf_to_gguf_update.py +21 -13
  34. package/cpp/llama.cpp/ggml/CMakeLists.txt +13 -3
  35. package/cpp/llama.cpp/ggml/cmake/ggml-config.cmake.in +85 -47
  36. package/cpp/llama.cpp/ggml/include/ggml-backend.h +1 -1
  37. package/cpp/llama.cpp/ggml/include/ggml-webgpu.h +19 -0
  38. package/cpp/llama.cpp/ggml/include/ggml.h +173 -10
  39. package/cpp/llama.cpp/ggml/src/CMakeLists.txt +1 -1
  40. package/cpp/llama.cpp/ggml/src/ggml-alloc.c +0 -15
  41. package/cpp/llama.cpp/ggml/src/ggml-backend-reg.cpp +7 -8
  42. package/cpp/llama.cpp/ggml/src/ggml-backend.cpp +44 -38
  43. package/cpp/llama.cpp/ggml/src/ggml-cann/acl_tensor.cpp +3 -1
  44. package/cpp/llama.cpp/ggml/src/ggml-cann/aclnn_ops.cpp +126 -8
  45. package/cpp/llama.cpp/ggml/src/ggml-cann/aclnn_ops.h +130 -22
  46. package/cpp/llama.cpp/ggml/src/ggml-cann/ggml-cann.cpp +138 -18
  47. package/cpp/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +11 -3
  48. package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/loongarch/quants.c +1 -1
  49. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +28 -1
  50. package/cpp/llama.cpp/ggml/src/ggml-cpu/kleidiai/kernels.cpp +109 -12
  51. package/cpp/llama.cpp/ggml/src/ggml-cpu/kleidiai/kernels.h +3 -0
  52. package/cpp/llama.cpp/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +88 -10
  53. package/cpp/llama.cpp/ggml/src/ggml-cpu/llamafile/sgemm.cpp +343 -1094
  54. package/cpp/llama.cpp/ggml/src/ggml-cpu/ops.cpp +1206 -163
  55. package/cpp/llama.cpp/ggml/src/ggml-cpu/ops.h +6 -0
  56. package/cpp/llama.cpp/ggml/src/ggml-cpu/repack.cpp +0 -1
  57. package/cpp/llama.cpp/ggml/src/ggml-cpu/simd-mappings.h +1 -1
  58. package/cpp/llama.cpp/ggml/src/ggml-cpu/vec.cpp +36 -9
  59. package/cpp/llama.cpp/ggml/src/ggml-cpu/vec.h +142 -9
  60. package/cpp/llama.cpp/ggml/src/ggml-cuda/CMakeLists.txt +3 -3
  61. package/cpp/llama.cpp/ggml/src/ggml-cuda/common.cuh +31 -4
  62. package/cpp/llama.cpp/ggml/src/ggml-cuda/convert.cu +86 -17
  63. package/cpp/llama.cpp/ggml/src/ggml-cuda/convert.cuh +5 -0
  64. package/cpp/llama.cpp/ggml/src/ggml-cuda/cpy-utils.cuh +225 -0
  65. package/cpp/llama.cpp/ggml/src/ggml-cuda/cpy.cu +41 -301
  66. package/cpp/llama.cpp/ggml/src/ggml-cuda/cross-entropy-loss.cu +2 -14
  67. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-common.cuh +85 -64
  68. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-mma-f16.cuh +47 -60
  69. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-tile-f16.cu +29 -42
  70. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-tile-f32.cu +46 -59
  71. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-vec-f16.cuh +36 -45
  72. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-vec-f32.cuh +38 -45
  73. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-wmma-f16.cu +23 -36
  74. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn.cu +3 -13
  75. package/cpp/llama.cpp/ggml/src/ggml-cuda/getrows.cu +8 -0
  76. package/cpp/llama.cpp/ggml/src/ggml-cuda/ggml-cuda.cu +255 -99
  77. package/cpp/llama.cpp/ggml/src/ggml-cuda/im2col.cu +1 -1
  78. package/cpp/llama.cpp/ggml/src/ggml-cuda/mma.cuh +111 -3
  79. package/cpp/llama.cpp/ggml/src/ggml-cuda/mmq.cu +6 -4
  80. package/cpp/llama.cpp/ggml/src/ggml-cuda/mmq.cuh +1152 -695
  81. package/cpp/llama.cpp/ggml/src/ggml-cuda/norm.cu +92 -5
  82. package/cpp/llama.cpp/ggml/src/ggml-cuda/norm.cuh +2 -0
  83. package/cpp/llama.cpp/ggml/src/ggml-cuda/rope.cu +21 -27
  84. package/cpp/llama.cpp/ggml/src/ggml-cuda/scale.cu +8 -6
  85. package/cpp/llama.cpp/ggml/src/ggml-cuda/set-rows.cu +275 -0
  86. package/cpp/llama.cpp/ggml/src/ggml-cuda/set-rows.cuh +7 -0
  87. package/cpp/llama.cpp/ggml/src/ggml-cuda/softmax.cu +119 -58
  88. package/cpp/llama.cpp/ggml/src/ggml-cuda/ssm-conv.cu +10 -2
  89. package/cpp/llama.cpp/ggml/src/ggml-cuda/ssm-scan.cu +192 -52
  90. package/cpp/llama.cpp/ggml/src/ggml-cuda/unary.cu +104 -0
  91. package/cpp/llama.cpp/ggml/src/ggml-cuda/unary.cuh +13 -0
  92. package/cpp/llama.cpp/ggml/src/ggml-cuda/upscale.cu +92 -6
  93. package/cpp/llama.cpp/ggml/src/ggml-cuda/vendors/hip.h +27 -6
  94. package/cpp/llama.cpp/ggml/src/ggml-cuda/vendors/musa.h +2 -2
  95. package/cpp/llama.cpp/ggml/src/ggml-impl.h +80 -0
  96. package/cpp/llama.cpp/ggml/src/ggml-metal/CMakeLists.txt +4 -2
  97. package/cpp/llama.cpp/ggml/src/ggml-metal/ggml-metal-impl.h +48 -12
  98. package/cpp/llama.cpp/ggml/src/ggml-metal/ggml-metal.m +572 -106
  99. package/cpp/llama.cpp/ggml/src/ggml-metal/ggml-metal.metal +599 -105
  100. package/cpp/llama.cpp/ggml/src/ggml-musa/CMakeLists.txt +18 -4
  101. package/cpp/llama.cpp/ggml/src/ggml-opencl/CMakeLists.txt +5 -0
  102. package/cpp/llama.cpp/ggml/src/ggml-opencl/ggml-opencl.cpp +800 -42
  103. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/conv2d.cl +185 -0
  104. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/conv2d_f16_f32.cl +176 -0
  105. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/gelu.cl +27 -0
  106. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/glu.cl +337 -0
  107. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/im2col_f16.cl +1 -1
  108. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/im2col_f32.cl +1 -1
  109. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/mul_mat_f16_f32.cl +130 -0
  110. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/rms_norm.cl +79 -0
  111. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/scale.cl +3 -2
  112. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/set_rows.cl +95 -0
  113. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/softmax_4_f16.cl +24 -11
  114. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/softmax_4_f32.cl +24 -11
  115. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/softmax_f16.cl +24 -11
  116. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/softmax_f32.cl +24 -11
  117. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/upscale.cl +2 -3
  118. package/cpp/llama.cpp/ggml/src/ggml-quants.c +6 -6
  119. package/cpp/llama.cpp/ggml/src/ggml-rpc/ggml-rpc.cpp +4 -4
  120. package/cpp/llama.cpp/ggml/src/ggml-sycl/backend.hpp +1 -0
  121. package/cpp/llama.cpp/ggml/src/ggml-sycl/element_wise.cpp +693 -1034
  122. package/cpp/llama.cpp/ggml/src/ggml-sycl/element_wise.hpp +18 -9
  123. package/cpp/llama.cpp/ggml/src/ggml-sycl/gemm.hpp +14 -26
  124. package/cpp/llama.cpp/ggml/src/ggml-sycl/ggml-sycl.cpp +191 -55
  125. package/cpp/llama.cpp/ggml/src/ggml-sycl/im2col.cpp +1 -1
  126. package/cpp/llama.cpp/ggml/src/ggml-sycl/quants.hpp +8 -9
  127. package/cpp/llama.cpp/ggml/src/ggml-sycl/rope.cpp +15 -18
  128. package/cpp/llama.cpp/ggml/src/ggml-sycl/set_rows.cpp +131 -0
  129. package/cpp/llama.cpp/ggml/src/ggml-sycl/set_rows.hpp +8 -0
  130. package/cpp/llama.cpp/ggml/src/ggml-sycl/vecdotq.hpp +2 -6
  131. package/cpp/llama.cpp/ggml/src/ggml-vulkan/ggml-vulkan.cpp +991 -307
  132. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/conv2d_mm.comp +265 -0
  133. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/copy_to_quant.comp +59 -12
  134. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q2_k.comp +1 -1
  135. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q3_k.comp +1 -1
  136. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q4_k.comp +1 -1
  137. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q5_k.comp +1 -1
  138. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q6_k.comp +1 -1
  139. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn.comp +28 -23
  140. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_base.comp +14 -9
  141. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm1.comp +38 -32
  142. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm2.comp +32 -27
  143. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_split_k_reduce.comp +44 -12
  144. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/geglu.comp +13 -0
  145. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/geglu_erf.comp +27 -0
  146. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/geglu_quick.comp +11 -0
  147. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/gelu_erf.comp +39 -0
  148. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/generic_binary_head.comp +2 -0
  149. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/glu_head.comp +17 -0
  150. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/glu_main.comp +29 -0
  151. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/im2col.comp +3 -8
  152. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm.comp +128 -72
  153. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm_cm2.comp +38 -9
  154. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/reglu.comp +9 -0
  155. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/rms_norm.comp +18 -3
  156. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/roll.comp +46 -0
  157. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/rope_head.comp +1 -4
  158. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/rope_multi.comp +7 -9
  159. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/rope_neox.comp +7 -9
  160. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/rope_norm.comp +7 -9
  161. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/rte.comp +5 -0
  162. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/scale.comp +1 -1
  163. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/soft_max.comp +20 -4
  164. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/swiglu.comp +9 -0
  165. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/upscale.comp +69 -5
  166. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +84 -9
  167. package/cpp/llama.cpp/ggml/src/ggml-webgpu/CMakeLists.txt +54 -0
  168. package/cpp/llama.cpp/ggml/src/ggml-webgpu/ggml-webgpu.cpp +907 -0
  169. package/cpp/llama.cpp/ggml/src/ggml-webgpu/wgsl-shaders/cpy.wgsl +60 -0
  170. package/cpp/llama.cpp/ggml/src/ggml-webgpu/wgsl-shaders/embed_wgsl.py +35 -0
  171. package/cpp/llama.cpp/ggml/src/ggml-webgpu/wgsl-shaders/memset.wgsl +40 -0
  172. package/cpp/llama.cpp/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat.wgsl +56 -0
  173. package/cpp/llama.cpp/ggml/src/ggml.c +386 -67
  174. package/cpp/llama.cpp/ggml/src/gguf.cpp +8 -1
  175. package/cpp/llama.cpp/gguf-py/gguf/constants.py +307 -0
  176. package/cpp/llama.cpp/gguf-py/gguf/gguf_writer.py +8 -2
  177. package/cpp/llama.cpp/gguf-py/gguf/metadata.py +4 -0
  178. package/cpp/llama.cpp/gguf-py/gguf/scripts/gguf_dump.py +24 -1
  179. package/cpp/llama.cpp/gguf-py/gguf/tensor_mapping.py +122 -47
  180. package/cpp/llama.cpp/gguf-py/gguf/vocab.py +12 -3
  181. package/cpp/llama.cpp/include/llama.h +15 -47
  182. package/cpp/llama.cpp/models/templates/llama-cpp-rwkv-world.jinja +34 -0
  183. package/cpp/llama.cpp/models/templates/moonshotai-Kimi-K2.jinja +43 -0
  184. package/cpp/llama.cpp/requirements/requirements-all.txt +1 -0
  185. package/cpp/llama.cpp/requirements/requirements-server-bench.txt +5 -0
  186. package/cpp/llama.cpp/src/llama-arch.cpp +316 -3
  187. package/cpp/llama.cpp/src/llama-arch.h +23 -1
  188. package/cpp/llama.cpp/src/llama-batch.cpp +103 -71
  189. package/cpp/llama.cpp/src/llama-batch.h +31 -18
  190. package/cpp/llama.cpp/src/llama-chat.cpp +58 -1
  191. package/cpp/llama.cpp/src/llama-chat.h +3 -0
  192. package/cpp/llama.cpp/src/llama-context.cpp +180 -106
  193. package/cpp/llama.cpp/src/llama-context.h +26 -16
  194. package/cpp/llama.cpp/src/llama-cparams.h +3 -2
  195. package/cpp/llama.cpp/src/llama-graph.cpp +310 -211
  196. package/cpp/llama.cpp/src/llama-graph.h +184 -122
  197. package/cpp/llama.cpp/src/llama-hparams.cpp +47 -1
  198. package/cpp/llama.cpp/src/llama-hparams.h +13 -2
  199. package/cpp/llama.cpp/src/llama-kv-cache-unified-iswa.cpp +38 -22
  200. package/cpp/llama.cpp/src/llama-kv-cache-unified-iswa.h +7 -2
  201. package/cpp/llama.cpp/src/llama-kv-cache-unified.cpp +849 -304
  202. package/cpp/llama.cpp/src/llama-kv-cache-unified.h +143 -47
  203. package/cpp/llama.cpp/src/llama-kv-cells.h +62 -10
  204. package/cpp/llama.cpp/src/llama-memory-hybrid.cpp +10 -4
  205. package/cpp/llama.cpp/src/llama-memory-hybrid.h +3 -1
  206. package/cpp/llama.cpp/src/llama-memory-recurrent.cpp +36 -11
  207. package/cpp/llama.cpp/src/llama-memory.cpp +17 -0
  208. package/cpp/llama.cpp/src/llama-memory.h +3 -0
  209. package/cpp/llama.cpp/src/llama-model.cpp +3545 -719
  210. package/cpp/llama.cpp/src/llama-model.h +21 -4
  211. package/cpp/llama.cpp/src/llama-quant.cpp +2 -2
  212. package/cpp/llama.cpp/src/llama-vocab.cpp +376 -10
  213. package/cpp/llama.cpp/src/llama-vocab.h +43 -0
  214. package/cpp/llama.cpp/src/unicode.cpp +207 -0
  215. package/cpp/llama.cpp/src/unicode.h +2 -0
  216. package/ios/include/chat.h +2 -0
  217. package/ios/include/common.h +22 -4
  218. package/ios/include/llama.h +15 -47
  219. package/ios/libs/llama.xcframework/Info.plist +13 -13
  220. package/ios/libs/llama.xcframework/ios-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  221. package/ios/libs/llama.xcframework/ios-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +5267 -4890
  222. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/ggml-backend.h +1 -1
  223. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/ggml.h +173 -10
  224. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/llama.h +15 -47
  225. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/llama +0 -0
  226. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  227. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +5238 -4861
  228. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +4014 -3764
  229. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/ggml-backend.h +1 -1
  230. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +173 -10
  231. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/llama.h +15 -47
  232. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/llama +0 -0
  233. package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  234. package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +5238 -4861
  235. package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +4016 -3766
  236. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/ggml-backend.h +1 -1
  237. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/ggml.h +173 -10
  238. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/llama.h +15 -47
  239. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/ggml-backend.h +1 -1
  240. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/ggml.h +173 -10
  241. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/llama.h +15 -47
  242. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/llama +0 -0
  243. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/ggml-backend.h +1 -1
  244. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/ggml.h +173 -10
  245. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/llama.h +15 -47
  246. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/llama +0 -0
  247. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/llama +0 -0
  248. package/ios/libs/llama.xcframework/tvos-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  249. package/ios/libs/llama.xcframework/tvos-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +5267 -4890
  250. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/ggml-backend.h +1 -1
  251. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/ggml.h +173 -10
  252. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/llama.h +15 -47
  253. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/llama +0 -0
  254. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  255. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +5238 -4861
  256. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +4014 -3764
  257. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/ggml-backend.h +1 -1
  258. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +173 -10
  259. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/llama.h +15 -47
  260. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/llama +0 -0
  261. package/ios/libs/llama.xcframework/xros-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  262. package/ios/libs/llama.xcframework/xros-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +5303 -4926
  263. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/ggml-backend.h +1 -1
  264. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/ggml.h +173 -10
  265. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/llama.h +15 -47
  266. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/llama +0 -0
  267. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  268. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +5274 -4897
  269. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +4044 -3794
  270. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/ggml-backend.h +1 -1
  271. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +173 -10
  272. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/llama.h +15 -47
  273. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/llama +0 -0
  274. package/package.json +4 -4
  275. package/cpp/llama.cpp/ggml/include/ggml-kompute.h +0 -50
  276. package/cpp/llama.cpp/ggml/src/ggml-kompute/CMakeLists.txt +0 -166
  277. package/cpp/llama.cpp/ggml/src/ggml-kompute/ggml-kompute.cpp +0 -2251
  278. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/common.comp +0 -112
  279. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_add.comp +0 -58
  280. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_addrow.comp +0 -25
  281. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f16_f16.comp +0 -52
  282. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f16_f32.comp +0 -52
  283. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f32_f16.comp +0 -52
  284. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f32_f32.comp +0 -52
  285. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_diagmask.comp +0 -30
  286. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_gelu.comp +0 -22
  287. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_getrows.comp +0 -17
  288. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_getrows_f16.comp +0 -31
  289. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_getrows_f32.comp +0 -31
  290. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_getrows_q4_0.comp +0 -38
  291. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_getrows_q4_1.comp +0 -39
  292. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_getrows_q6_k.comp +0 -44
  293. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_mul.comp +0 -52
  294. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_f16.comp +0 -69
  295. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_mat_f32.comp +0 -51
  296. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q4_0.comp +0 -33
  297. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q4_1.comp +0 -35
  298. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q4_k.comp +0 -140
  299. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q6_k.comp +0 -106
  300. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q8_0.comp +0 -73
  301. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_mul_mv_q_n.comp +0 -52
  302. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_mul_mv_q_n_pre.comp +0 -28
  303. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_norm.comp +0 -84
  304. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_relu.comp +0 -21
  305. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_rmsnorm.comp +0 -53
  306. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_rope_neox_f16.comp +0 -52
  307. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_rope_neox_f32.comp +0 -52
  308. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_rope_norm_f16.comp +0 -52
  309. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_rope_norm_f32.comp +0 -52
  310. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_scale.comp +0 -19
  311. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_scale_8.comp +0 -23
  312. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_silu.comp +0 -22
  313. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_softmax.comp +0 -72
  314. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/rope_common.comp +0 -71
@@ -0,0 +1,907 @@
1
+ #include "ggml-webgpu.h"
2
+
3
+ #include <webgpu/webgpu_cpp.h>
4
+
5
+ #include "ggml-impl.h"
6
+ #include "ggml-backend-impl.h"
7
+
8
+ #include "ggml-wgsl-shaders.hpp"
9
+
10
+ #include <cstring>
11
+ #include <iostream>
12
+ #include <mutex>
13
+ #include <vector>
14
+
15
+ #ifdef GGML_WEBGPU_DEBUG
16
+ #define WEBGPU_LOG_DEBUG(msg) std::cout << msg << std::endl
17
+ #else
18
+ #define WEBGPU_LOG_DEBUG(msg) ((void) 0)
19
+ #endif // GGML_WEBGPU_DEBUG
20
+
21
+ /* Constants */
22
+
23
+ #define WEBGPU_MUL_MAT_WG_SIZE 64
24
+ #define WEBGPU_MUL_MAT_PARAMS_SIZE (13 * sizeof(uint32_t)) // M, N, K, batch sizes, broadcasts
25
+ #define WEBGPU_CPY_PARAMS_SIZE (15 * sizeof(uint32_t)) // strides and offsets
26
+ #define WEBGPU_STORAGE_BUF_BINDING_MULT 4 // a storage buffer binding size must be a multiple of 4
27
+
28
+ /* End Constants */
29
+
30
+ // This is a "fake" base pointer, since WebGPU buffers do not have pointers to their locations.
31
+ static void * const webgpu_ptr_base = (void *)(uintptr_t) 0x1000; // NOLINT
32
+
33
+ // Always returns the base offset of a tensor, regardless of views.
34
+ static uint64_t webgpu_tensor_offset(const ggml_tensor * tensor) {
35
+ if (tensor->view_src) {
36
+ return (uint8_t *) tensor->view_src->data - (uint8_t *) webgpu_ptr_base;
37
+ }
38
+ return (uint8_t *) tensor->data - (uint8_t *) webgpu_ptr_base;
39
+ }
40
+
41
+ /* Struct definitions */
42
+
43
+ // All the base objects needed to run operations on a WebGPU device
44
+ struct webgpu_context_struct {
45
+ wgpu::Instance instance;
46
+ wgpu::Adapter adapter;
47
+ wgpu::Device device;
48
+ wgpu::Queue queue;
49
+ wgpu::Limits limits;
50
+ wgpu::SupportedFeatures features;
51
+
52
+ std::mutex mutex;
53
+ bool device_initialized = false;
54
+
55
+ // pipelines and parameter buffers
56
+ // TODO: reuse params buffers for different pipelines when possible
57
+ wgpu::ComputePipeline memset_pipeline;
58
+ wgpu::Buffer memset_params_dev_buf;
59
+ wgpu::Buffer memset_params_host_buf;
60
+ wgpu::ComputePipeline mul_mat_pipeline;
61
+ wgpu::Buffer mul_mat_params_dev_buf;
62
+ wgpu::Buffer mul_mat_params_host_buf;
63
+ wgpu::ComputePipeline cpy_pipeline;
64
+ wgpu::Buffer cpy_params_dev_buf;
65
+ wgpu::Buffer cpy_params_host_buf;
66
+
67
+ size_t memset_bytes_per_thread;
68
+
69
+ // Staging buffer for reading data from the GPU
70
+ wgpu::Buffer get_tensor_staging_buf;
71
+ };
72
+
73
+ typedef std::shared_ptr<webgpu_context_struct> webgpu_context;
74
+
75
+ struct ggml_backend_webgpu_reg_context {
76
+ webgpu_context webgpu_ctx;
77
+
78
+ size_t device_count;
79
+ const char * name;
80
+ };
81
+
82
+ struct ggml_backend_webgpu_device_context {
83
+ webgpu_context webgpu_ctx;
84
+
85
+ std::string device_name;
86
+ std::string device_desc;
87
+ };
88
+
89
+ struct ggml_backend_webgpu_context {
90
+ webgpu_context webgpu_ctx;
91
+
92
+ std::string name;
93
+ };
94
+
95
+ struct ggml_backend_webgpu_buffer_context {
96
+ webgpu_context webgpu_ctx;
97
+
98
+ wgpu::Buffer buffer;
99
+
100
+ ggml_backend_webgpu_buffer_context(webgpu_context ctx, wgpu::Buffer buf) :
101
+ webgpu_ctx(ctx), buffer(buf) {
102
+ }
103
+ };
104
+
105
+ /* End struct definitions */
106
+
107
+ /* WebGPU object initializations */
108
+
109
+ static void ggml_webgpu_create_pipeline(wgpu::Device &device, wgpu::ComputePipeline &pipeline, const char * shader_code, const char * label, const std::vector<wgpu::ConstantEntry> &constants = {}) {
110
+ WEBGPU_LOG_DEBUG("ggml_webgpu_create_pipeline()");
111
+ wgpu::ShaderSourceWGSL shader_source;
112
+ shader_source.code = shader_code;
113
+ wgpu::ShaderModuleDescriptor shader_desc;
114
+ shader_desc.nextInChain = &shader_source;
115
+ wgpu::ShaderModule shader_module = device.CreateShaderModule(&shader_desc);
116
+
117
+ wgpu::ComputePipelineDescriptor pipeline_desc;
118
+ pipeline_desc.label = label;
119
+ pipeline_desc.compute.module = shader_module;
120
+ pipeline_desc.compute.entryPoint = "main"; // Entry point in the WGSL code
121
+ pipeline_desc.layout = nullptr; // nullptr means auto layout
122
+ if (constants.size() > 0) {
123
+ pipeline_desc.compute.constants = constants.data();
124
+ pipeline_desc.compute.constantCount = constants.size();
125
+ }
126
+ pipeline = device.CreateComputePipeline(&pipeline_desc);
127
+ }
128
+
129
+ static void ggml_webgpu_create_buffer(wgpu::Device &device, wgpu::Buffer &buffer, size_t size, wgpu::BufferUsage usage, const char* label) {
130
+ WEBGPU_LOG_DEBUG("ggml_webgpu_create_buffer()");
131
+
132
+ wgpu::BufferDescriptor buffer_desc;
133
+ buffer_desc.size = size;
134
+ buffer_desc.usage = usage;
135
+ buffer_desc.label = label;
136
+ buffer_desc.mappedAtCreation = false;
137
+ // TODO: error handling
138
+ buffer = device.CreateBuffer(&buffer_desc);
139
+ }
140
+
141
+ /** End WebGPU object initializations */
142
+
143
+ /** WebGPU Actions */
144
+
145
+ static void ggml_backend_webgpu_map_buffer(webgpu_context ctx, wgpu::Buffer buffer, wgpu::MapMode mode, size_t offset, size_t size) {
146
+ ctx->instance.WaitAny(buffer.MapAsync(
147
+ mode, offset, size, wgpu::CallbackMode::WaitAnyOnly,
148
+ [](wgpu::MapAsyncStatus status, wgpu::StringView message) {
149
+ if (status != wgpu::MapAsyncStatus::Success) {
150
+ GGML_LOG_ERROR("ggml_webgpu: Failed to map buffer: %s\n", message.data);
151
+ }
152
+ }),
153
+ UINT64_MAX
154
+ );
155
+ }
156
+
157
+ static void ggml_backend_webgpu_buffer_memset(webgpu_context ctx, wgpu::Buffer buf, uint32_t value, size_t offset, size_t size) {
158
+ std::lock_guard<std::mutex> lock(ctx->mutex);
159
+ wgpu::Device device = ctx->device;
160
+
161
+ // map the host parameters buffer
162
+ ggml_backend_webgpu_map_buffer(ctx, ctx->memset_params_host_buf, wgpu::MapMode::Write, 0, ctx->memset_params_host_buf.GetSize());
163
+ uint32_t * params = (uint32_t *) ctx->memset_params_host_buf.GetMappedRange();
164
+
165
+ params[0] = (uint32_t)offset;
166
+ params[1] = (uint32_t)size;
167
+ params[2] = value;
168
+ ctx->memset_params_host_buf.Unmap();
169
+
170
+ wgpu::BindGroupEntry entries[2];
171
+ entries[0].binding = 0; // binding for the buffer to memset
172
+ entries[0].buffer = buf;
173
+ entries[0].offset = 0;
174
+ entries[0].size = buf.GetSize();
175
+ entries[1].binding = 1; // binding for the parameters
176
+ entries[1].buffer = ctx->memset_params_dev_buf;
177
+ entries[1].offset = 0;
178
+ entries[1].size = ctx->memset_params_dev_buf.GetSize();
179
+
180
+ wgpu::BindGroupDescriptor bind_group_desc;
181
+ bind_group_desc.layout = ctx->memset_pipeline.GetBindGroupLayout(0);
182
+ bind_group_desc.entryCount = 2;
183
+ bind_group_desc.label = "ggml_memset";
184
+ bind_group_desc.entries = entries;
185
+ wgpu::BindGroup bind_group = device.CreateBindGroup(&bind_group_desc);
186
+
187
+ wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
188
+ encoder.CopyBufferToBuffer(
189
+ ctx->memset_params_host_buf, 0,
190
+ ctx->memset_params_dev_buf, 0,
191
+ ctx->memset_params_dev_buf.GetSize()
192
+ );
193
+ wgpu::ComputePassEncoder pass = encoder.BeginComputePass();
194
+ pass.SetPipeline(ctx->memset_pipeline);
195
+ pass.SetBindGroup(0, bind_group);
196
+ size_t bytes_per_wg = ctx->limits.maxComputeWorkgroupSizeX * ctx->memset_bytes_per_thread;
197
+ pass.DispatchWorkgroups(((size + 3) + bytes_per_wg - 1) / bytes_per_wg, 1, 1);
198
+ pass.End();
199
+ wgpu::CommandBuffer commands = encoder.Finish();
200
+
201
+ ctx->queue.Submit(1, &commands);
202
+ }
203
+
204
+ static void ggml_backend_webgpu_wait_on_submission(webgpu_context ctx) {
205
+ // Wait for the queue to finish processing all commands
206
+ ctx->instance.WaitAny(ctx->queue.OnSubmittedWorkDone(wgpu::CallbackMode::WaitAnyOnly,
207
+ [](wgpu::QueueWorkDoneStatus status, wgpu::StringView message) {
208
+ if (status != wgpu::QueueWorkDoneStatus::Success) {
209
+ GGML_LOG_ERROR("ggml_webgpu: Failed to wait on queue: %s\n", message.data);
210
+ }
211
+ }),
212
+ UINT64_MAX
213
+ );
214
+ }
215
+
216
+ /** End WebGPU Actions */
217
+
218
+ /** GGML Backend Interface */
219
+
220
+ static const char * ggml_backend_webgpu_name(ggml_backend_t backend) {
221
+ ggml_backend_webgpu_context * ctx = (ggml_backend_webgpu_context *)backend->context;
222
+ return ctx->name.c_str();
223
+ }
224
+
225
+ static void ggml_backend_webgpu_free(ggml_backend_t backend) {
226
+ ggml_backend_webgpu_context * ctx = (ggml_backend_webgpu_context *)backend->context;
227
+ WEBGPU_LOG_DEBUG("ggml_backend_webgpu_free(" << ctx->name << ")");
228
+
229
+ // TODO: cleanup
230
+ GGML_UNUSED(ctx);
231
+ }
232
+
233
+ // Returns true if node has enqueued work into the queue, false otherwise
234
+ static bool ggml_webgpu_encode_node(webgpu_context ctx, ggml_tensor * node){
235
+ if (ggml_is_empty(node)) {
236
+ return false;
237
+ }
238
+
239
+ WEBGPU_LOG_DEBUG("ggml_webgpu_encode_node(" << node << ", " << ggml_op_name(node->op) << ")");
240
+
241
+
242
+ switch (node->op) {
243
+ // no-ops
244
+ case GGML_OP_NONE:
245
+ case GGML_OP_VIEW:
246
+ case GGML_OP_PERMUTE:
247
+ return false;
248
+
249
+ case GGML_OP_CPY: {
250
+ std::lock_guard<std::mutex> lock(ctx->mutex);
251
+ const ggml_tensor * src = node->src[0];
252
+ ggml_backend_webgpu_buffer_context * src_ctx = (ggml_backend_webgpu_buffer_context *) src->buffer->context;
253
+ size_t src_offset = webgpu_tensor_offset(src) + src->view_offs;
254
+ // assumes power of 2 offset alignment
255
+ size_t src_misalignment = src_offset & (ctx->limits.minStorageBufferOffsetAlignment - 1);
256
+ // align to minimum offset alignment
257
+ src_offset &= ~(ctx->limits.minStorageBufferOffsetAlignment - 1);
258
+ ggml_backend_webgpu_buffer_context * dst_ctx = (ggml_backend_webgpu_buffer_context *) node->buffer->context;
259
+ size_t dst_offset = webgpu_tensor_offset(node) + node->view_offs;
260
+ size_t dst_misalignment = dst_offset & (ctx->limits.minStorageBufferOffsetAlignment - 1);
261
+ dst_offset &= ~(ctx->limits.minStorageBufferOffsetAlignment - 1);
262
+
263
+ wgpu::Device device = ctx->device;
264
+ ggml_backend_webgpu_map_buffer(ctx, ctx->cpy_params_host_buf,
265
+ wgpu::MapMode::Write, 0, ctx->cpy_params_host_buf.GetSize());
266
+ uint32_t * params = (uint32_t *) ctx->cpy_params_host_buf.GetMappedRange();
267
+ uint32_t ne = (uint32_t)ggml_nelements(node);
268
+ params[0] = ne;
269
+ params[1] = src_misalignment/ggml_type_size(src->type);
270
+ params[2] = dst_misalignment/ggml_type_size(node->type);
271
+
272
+ // Convert byte-strides to element-strides
273
+ params[3] = (uint32_t)src->nb[0]/ggml_type_size(src->type);
274
+ params[4] = (uint32_t)src->nb[1]/ggml_type_size(src->type);
275
+ params[5] = (uint32_t)src->nb[2]/ggml_type_size(src->type);
276
+ params[6] = (uint32_t)src->nb[3]/ggml_type_size(src->type);
277
+ params[7] = (uint32_t)node->nb[0]/ggml_type_size(node->type);
278
+ params[8] = (uint32_t)node->nb[1]/ggml_type_size(node->type);
279
+ params[9] = (uint32_t)node->nb[2]/ggml_type_size(node->type);
280
+ params[10] = (uint32_t)node->nb[3]/ggml_type_size(node->type);
281
+ // Logical shape — same for both tensors even if permuted
282
+ params[11] = (uint32_t)(src->ne[0]);
283
+ params[12] = (uint32_t)(src->ne[1]);
284
+ params[13] = (uint32_t)(src->ne[2]);
285
+ params[14] = (uint32_t)(src->ne[3]);
286
+
287
+ ctx->cpy_params_host_buf.Unmap();
288
+
289
+ wgpu::BindGroupEntry entries[3];
290
+ entries[0].binding = 0;
291
+ entries[0].buffer = src_ctx->buffer;
292
+ entries[0].offset = src_offset;
293
+ entries[0].size = (ggml_nbytes(src) + src_misalignment + WEBGPU_STORAGE_BUF_BINDING_MULT - 1) & ~(WEBGPU_STORAGE_BUF_BINDING_MULT - 1);
294
+
295
+ entries[1].binding = 1;
296
+ entries[1].buffer = dst_ctx->buffer;
297
+ entries[1].offset = dst_offset;
298
+ entries[1].size = (ggml_nbytes(node) + dst_misalignment + WEBGPU_STORAGE_BUF_BINDING_MULT - 1) & ~(WEBGPU_STORAGE_BUF_BINDING_MULT - 1);
299
+
300
+ entries[2].binding = 2;
301
+ entries[2].buffer = ctx->cpy_params_dev_buf;
302
+ entries[2].offset = 0;
303
+ entries[2].size = ctx->cpy_params_dev_buf.GetSize();
304
+
305
+ wgpu::BindGroupDescriptor bind_group_desc;
306
+ bind_group_desc.layout = ctx->cpy_pipeline.GetBindGroupLayout(0);
307
+ bind_group_desc.label = "ggml_op_cpy";
308
+ bind_group_desc.entryCount = 3;
309
+ bind_group_desc.entries = entries;
310
+ wgpu::BindGroup bind_group = device.CreateBindGroup(&bind_group_desc);
311
+
312
+ wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
313
+ encoder.CopyBufferToBuffer(
314
+ ctx->cpy_params_host_buf, 0,
315
+ ctx->cpy_params_dev_buf, 0,
316
+ ctx->cpy_params_dev_buf.GetSize()
317
+ );
318
+ wgpu::ComputePassEncoder pass = encoder.BeginComputePass();
319
+ pass.SetPipeline(ctx->cpy_pipeline);
320
+ pass.SetBindGroup(0, bind_group);
321
+ size_t max_wg_size = ctx->limits.maxComputeWorkgroupSizeX;
322
+ pass.DispatchWorkgroups((ne + max_wg_size - 1) / max_wg_size);
323
+ pass.End();
324
+ wgpu::CommandBuffer commands = encoder.Finish();
325
+
326
+ // TODO, don't submit here, batch submissions
327
+ ctx->queue.Submit(1, &commands);
328
+ // TODO, don't wait on submission here
329
+ ggml_backend_webgpu_wait_on_submission(ctx);
330
+ return true;
331
+ }
332
+
333
+ case GGML_OP_MUL_MAT:
334
+ {
335
+ const ggml_tensor * src0 = node->src[0];
336
+ ggml_backend_webgpu_buffer_context * src0_ctx = (ggml_backend_webgpu_buffer_context *) src0->buffer->context;
337
+ size_t src0_offset = webgpu_tensor_offset(src0) + src0->view_offs;
338
+ const ggml_tensor * src1 = node->src[1];
339
+ ggml_backend_webgpu_buffer_context * src1_ctx = (ggml_backend_webgpu_buffer_context *) src1->buffer->context;
340
+ size_t src1_offset = webgpu_tensor_offset(src1) + src1->view_offs;
341
+ ggml_backend_webgpu_buffer_context * dst_ctx = (ggml_backend_webgpu_buffer_context *) node->buffer->context;
342
+
343
+ size_t dst_offset = webgpu_tensor_offset(node) + node->view_offs;
344
+
345
+ wgpu::Device device = ctx->device;
346
+
347
+ // map the host parameters buffer
348
+ ggml_backend_webgpu_map_buffer(ctx, ctx->mul_mat_params_host_buf,
349
+ wgpu::MapMode::Write, 0, ctx->mul_mat_params_host_buf.GetSize());
350
+ uint32_t * params = (uint32_t *) ctx->mul_mat_params_host_buf.GetMappedRange();
351
+
352
+ params[0] = (uint32_t)node->ne[1]; // number of rows in result (M)
353
+ params[1] = (uint32_t)node->ne[0]; // number of columns in result (N)
354
+ params[2] = (uint32_t)src0->ne[0]; // number of columns in src0/src1 (K)
355
+
356
+ params[3] = (uint32_t)src0->nb[1]/ggml_type_size(src0->type); // stride (elements) of src0 in dimension 1
357
+ params[4] = (uint32_t)src1->nb[1]/ggml_type_size(src1->type); // stride (elements) of src1 in dimension 1
358
+ params[5] = (uint32_t)src0->nb[2]/ggml_type_size(src0->type); // stride (elements) of src0 in dimension 2
359
+ params[6] = (uint32_t)src1->nb[2]/ggml_type_size(src1->type); // stride (elements) of src1 in dimension 2
360
+ params[7] = (uint32_t)src0->nb[3]/ggml_type_size(src0->type); // stride (elements) of src0 in dimension 3
361
+ params[8] = (uint32_t)src1->nb[3]/ggml_type_size(src1->type); // stride (elements) of src1 in dimension 3
362
+
363
+ params[9] = (uint32_t)src0->ne[2]; // batch size in dimension 2
364
+ params[10] = (uint32_t)src0->ne[3]; // batch size in dimension 3
365
+ params[11] = (uint32_t)(src1->ne[2]/src0->ne[2]); // broadcast in dimension 2
366
+ params[12] = (uint32_t)(src1->ne[3]/src0->ne[3]); // broadcast in dimension 3
367
+
368
+ ctx->mul_mat_params_host_buf.Unmap();
369
+
370
+ wgpu::BindGroupEntry entries[4];
371
+ entries[0].binding = 0;
372
+ entries[0].buffer = src0_ctx->buffer;
373
+ entries[0].offset = src0_offset;
374
+ entries[0].size = ggml_nbytes(src0);
375
+
376
+ entries[1].binding = 1;
377
+ entries[1].buffer = src1_ctx->buffer;
378
+ entries[1].offset = src1_offset;
379
+ entries[1].size = ggml_nbytes(src1);
380
+
381
+ entries[2].binding = 2;
382
+ entries[2].buffer = dst_ctx->buffer;
383
+ entries[2].offset = dst_offset;
384
+ entries[2].size = ggml_nbytes(node);
385
+
386
+ entries[3].binding = 3;
387
+ entries[3].buffer = ctx->mul_mat_params_dev_buf;
388
+ entries[3].offset = 0;
389
+ entries[3].size = ctx->mul_mat_params_dev_buf.GetSize();
390
+
391
+ wgpu::BindGroupDescriptor bind_group_desc;
392
+ bind_group_desc.layout = ctx->mul_mat_pipeline.GetBindGroupLayout(0);
393
+ bind_group_desc.entryCount = 4;
394
+ bind_group_desc.label = "ggml_op_mul_mat";
395
+ bind_group_desc.entries = entries;
396
+ wgpu::BindGroup bind_group = device.CreateBindGroup(&bind_group_desc);
397
+
398
+ wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
399
+ encoder.CopyBufferToBuffer(
400
+ ctx->mul_mat_params_host_buf, 0,
401
+ ctx->mul_mat_params_dev_buf, 0,
402
+ ctx->mul_mat_params_dev_buf.GetSize()
403
+ );
404
+ wgpu::ComputePassEncoder pass = encoder.BeginComputePass();
405
+ pass.SetPipeline(ctx->mul_mat_pipeline);
406
+ pass.SetBindGroup(0, bind_group);
407
+ pass.DispatchWorkgroups((node->ne[0] * node->ne[1] * node->ne[2] * node->ne[3] + WEBGPU_MUL_MAT_WG_SIZE - 1) / WEBGPU_MUL_MAT_WG_SIZE);
408
+ pass.End();
409
+ wgpu::CommandBuffer commands = encoder.Finish();
410
+
411
+ // TODO, don't submit here, batch submissions
412
+ ctx->queue.Submit(1, &commands);
413
+ // TODO, don't wait on submission here
414
+ ggml_backend_webgpu_wait_on_submission(ctx);
415
+ return true;
416
+ }
417
+
418
+ default:
419
+ return false;
420
+ }
421
+ }
422
+
423
+ static ggml_status ggml_backend_webgpu_graph_compute(ggml_backend_t backend, struct ggml_cgraph * cgraph) {
424
+ WEBGPU_LOG_DEBUG("ggml_backend_webgpu_graph_compute(" << cgraph->n_nodes << " nodes)");
425
+
426
+ ggml_backend_webgpu_context * backend_ctx = static_cast<ggml_backend_webgpu_context *>(backend->context);
427
+ webgpu_context ctx = backend_ctx->webgpu_ctx;
428
+
429
+ for (int i = 0; i < cgraph->n_nodes; i++) {
430
+ ggml_webgpu_encode_node(ctx, cgraph->nodes[i]);
431
+ }
432
+
433
+ return GGML_STATUS_SUCCESS;
434
+ }
435
+
436
+ static ggml_backend_i ggml_backend_webgpu_i = {
437
+ /* .get_name = */ ggml_backend_webgpu_name,
438
+ /* .free = */ ggml_backend_webgpu_free,
439
+ /* .set_tensor_async = */ NULL,
440
+ /* .get_tensor_async = */ NULL,
441
+ /* .cpy_tensor_async = */ NULL,
442
+ /* .synchronize = */ NULL,
443
+ /* .graph_plan_create = */ NULL,
444
+ /* .graph_plan_free = */ NULL,
445
+ /* .graph_plan_update = */ NULL,
446
+ /* .graph_plan_compute = */ NULL,
447
+ /* .graph_compute = */ ggml_backend_webgpu_graph_compute,
448
+ /* .event_record = */ NULL,
449
+ /* .event_wait = */ NULL,
450
+ };
451
+
452
+ /* End GGML Backend Interface */
453
+
454
+ /* GGML Backend Buffer Interface */
455
+
456
+ static void ggml_backend_webgpu_buffer_free_buffer(ggml_backend_buffer_t buffer) {
457
+ WEBGPU_LOG_DEBUG("ggml_backend_webgpu_buffer_free_buffer()");
458
+ ggml_backend_webgpu_buffer_context * ctx = static_cast<ggml_backend_webgpu_buffer_context *>(buffer->context);
459
+ ctx->buffer.Destroy();
460
+ }
461
+
462
+ // Returns the "fake" base pointer.
463
+ static void * ggml_backend_webgpu_buffer_get_base(ggml_backend_buffer_t buffer) {
464
+ GGML_UNUSED(buffer);
465
+ return webgpu_ptr_base;
466
+ }
467
+
468
+ static void ggml_backend_webgpu_buffer_memset_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, uint8_t value, size_t offset, size_t size) {
469
+ if (size == 0) {
470
+ WEBGPU_LOG_DEBUG("ggml_backend_webgpu_buffer_memset_tensor: size is zero, nothing to do.");
471
+ return;
472
+ }
473
+
474
+ WEBGPU_LOG_DEBUG("ggml_backend_webgpu_buffer_memset_tensor(" << buffer << ", " << tensor << ", " << value << ", " << offset << ", " << size << ")");
475
+
476
+ ggml_backend_webgpu_buffer_context * buf_ctx = (ggml_backend_webgpu_buffer_context *) buffer->context;
477
+ size_t total_offset = webgpu_tensor_offset(tensor) + tensor->view_offs + offset;
478
+ // This is a trick to set all bytes of a u32 to the same 1 byte value.
479
+ uint32_t val32 = (uint32_t)value * 0x01010101;
480
+ ggml_backend_webgpu_buffer_memset(buf_ctx->webgpu_ctx, buf_ctx->buffer, val32, total_offset, size);
481
+ }
482
+
483
+ static void ggml_backend_webgpu_buffer_set_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
484
+ WEBGPU_LOG_DEBUG("ggml_backend_webgpu_buffer_set_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
485
+ ggml_backend_webgpu_buffer_context * buf_ctx = (ggml_backend_webgpu_buffer_context *) buffer->context;
486
+ webgpu_context webgpu_ctx = buf_ctx->webgpu_ctx;
487
+
488
+ size_t total_offset = webgpu_tensor_offset(tensor) + tensor->view_offs + offset;
489
+
490
+ webgpu_ctx->queue.WriteBuffer(buf_ctx->buffer, total_offset, data, (size/4)*4);
491
+
492
+ if (size % 4 != 0) {
493
+ // If size is not a multiple of 4, we need to memset the remaining bytes
494
+ size_t remaining_size = size % 4;
495
+ // pack the remaining bytes into a uint32_t
496
+ uint32_t val32 = 0;
497
+ for (size_t i = 0; i < remaining_size; i++) {
498
+ ((uint8_t *)&val32)[i] = ((const uint8_t *)data)[size - remaining_size + i];
499
+ }
500
+ // memset the remaining bytes
501
+ ggml_backend_webgpu_buffer_memset(webgpu_ctx, buf_ctx->buffer, val32, total_offset + (size - remaining_size), remaining_size);
502
+ }
503
+ }
504
+
505
+ static void ggml_backend_webgpu_buffer_get_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
506
+ WEBGPU_LOG_DEBUG("ggml_backend_webgpu_buffer_get_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
507
+
508
+ ggml_backend_webgpu_buffer_context * buf_ctx = (ggml_backend_webgpu_buffer_context *) buffer->context;
509
+ webgpu_context webgpu_ctx = buf_ctx->webgpu_ctx;
510
+ wgpu::Device device = webgpu_ctx->device;
511
+
512
+ size_t total_offset = webgpu_tensor_offset(tensor) + tensor->view_offs + offset;
513
+
514
+ size_t final_size = size;
515
+ if (size % 4 != 0) {
516
+ // If size is not a multiple of 4, we need to round it up to the next multiple of 4
517
+ final_size = size + (4 - (size % 4));
518
+ }
519
+
520
+ std::lock_guard<std::mutex> lock(webgpu_ctx->mutex);
521
+
522
+ if (webgpu_ctx->get_tensor_staging_buf == nullptr ||
523
+ webgpu_ctx->get_tensor_staging_buf.GetSize() < final_size) {
524
+ // Create a new staging buffer if it doesn't exist or is too small
525
+ if (webgpu_ctx->get_tensor_staging_buf) {
526
+ webgpu_ctx->get_tensor_staging_buf.Destroy();
527
+ }
528
+ ggml_webgpu_create_buffer(device, webgpu_ctx->get_tensor_staging_buf, final_size,
529
+ wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::MapRead, "get_tensor_staging_buf");
530
+ }
531
+
532
+ // Copy the data from the buffer to the staging buffer
533
+ wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
534
+ encoder.CopyBufferToBuffer(buf_ctx->buffer, total_offset, webgpu_ctx->get_tensor_staging_buf, 0, final_size);
535
+ wgpu::CommandBuffer commands = encoder.Finish();
536
+ // Submit the command buffer to the queue
537
+ webgpu_ctx->queue.Submit(1, &commands);
538
+
539
+ // Map the staging buffer to read the data
540
+ ggml_backend_webgpu_map_buffer(webgpu_ctx, webgpu_ctx->get_tensor_staging_buf, wgpu::MapMode::Read, 0, final_size);
541
+ // Must specify size here since the staging buffer might be larger than the tensor size
542
+ const void * mapped_range = webgpu_ctx->get_tensor_staging_buf.GetConstMappedRange(0, final_size);
543
+
544
+ // Copy the data from the mapped range to the output buffer
545
+ std::memcpy(data, mapped_range, size);
546
+ webgpu_ctx->get_tensor_staging_buf.Unmap();
547
+ }
548
+
549
+ static void ggml_backend_webgpu_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
550
+ WEBGPU_LOG_DEBUG("ggml_backend_webgpu_buffer_clear(" << buffer << ", " << (uint32_t) value << ")");
551
+
552
+ ggml_backend_webgpu_buffer_context * buf_ctx = (ggml_backend_webgpu_buffer_context *) buffer->context;
553
+ ggml_backend_webgpu_buffer_memset(buf_ctx->webgpu_ctx, buf_ctx->buffer, value, 0, buffer->size);
554
+ }
555
+
556
+ static ggml_backend_buffer_i ggml_backend_webgpu_buffer_interface = {
557
+ /* .free_buffer = */ ggml_backend_webgpu_buffer_free_buffer,
558
+ /* .get_base = */ ggml_backend_webgpu_buffer_get_base,
559
+ /* .init_tensor = */ NULL, // TODO: optional, needed?
560
+ /* .memset_tensor = */ ggml_backend_webgpu_buffer_memset_tensor,
561
+ /* .set_tensor = */ ggml_backend_webgpu_buffer_set_tensor,
562
+ /* .get_tensor = */ ggml_backend_webgpu_buffer_get_tensor,
563
+ /* .cpy_tensor = */ NULL, // TODO: optional, implement this
564
+ /* .clear = */ ggml_backend_webgpu_buffer_clear,
565
+ /* .reset = */ NULL, // TODO: optional, think it coordinates with .init_tensor
566
+ };
567
+
568
+ /* End GGML Backend Buffer Interface */
569
+
570
+ /* GGML Backend Buffer Type Interface */
571
+
572
+ static const char * ggml_backend_webgpu_buffer_type_get_name(ggml_backend_buffer_type_t buft) {
573
+ ggml_backend_webgpu_device_context * ctx = static_cast<ggml_backend_webgpu_device_context *>(buft->device->context);
574
+ return ctx->device_name.c_str();
575
+ }
576
+
577
+ static ggml_backend_buffer_t ggml_backend_webgpu_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
578
+ WEBGPU_LOG_DEBUG("ggml_backend_webgpu_buffer_type_alloc_buffer(" << size << ")");
579
+ ggml_backend_webgpu_device_context * ctx = static_cast<ggml_backend_webgpu_device_context *>(buft->device->context);
580
+
581
+ wgpu::Buffer buf;
582
+ ggml_webgpu_create_buffer(ctx->webgpu_ctx->device, buf, size,
583
+ wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::CopyDst, "allocated_buffer");
584
+
585
+ ggml_backend_webgpu_buffer_context * buf_ctx = new ggml_backend_webgpu_buffer_context(ctx->webgpu_ctx, buf);
586
+
587
+ return ggml_backend_buffer_init(buft, ggml_backend_webgpu_buffer_interface, buf_ctx, size);
588
+ }
589
+
590
+ static size_t ggml_backend_webgpu_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
591
+ ggml_backend_webgpu_device_context * ctx = static_cast<ggml_backend_webgpu_device_context *>(buft->device->context);
592
+ return ctx->webgpu_ctx->limits.minStorageBufferOffsetAlignment;
593
+ }
594
+
595
+ // maxBufferSize might be larger, but you can't bind more than maxStorageBufferBindingSize to a single binding.
596
+ static size_t ggml_backend_webgpu_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
597
+ ggml_backend_webgpu_device_context * ctx = static_cast<ggml_backend_webgpu_device_context *>(buft->device->context);
598
+ return ctx->webgpu_ctx->limits.maxStorageBufferBindingSize;
599
+ }
600
+
601
+ /* End GGML Backend Buffer Type Interface */
602
+
603
+ /* GGML Backend Device Interface */
604
+
605
+ static const char * ggml_backend_webgpu_device_get_name(ggml_backend_dev_t dev) {
606
+ ggml_backend_webgpu_device_context * ctx = static_cast<ggml_backend_webgpu_device_context *>(dev->context);
607
+ return ctx->device_name.c_str();
608
+ }
609
+
610
+ static const char * ggml_backend_webgpu_device_get_description(ggml_backend_dev_t dev) {
611
+ ggml_backend_webgpu_device_context * ctx = static_cast<ggml_backend_webgpu_device_context *>(dev->context);
612
+ return ctx->device_desc.c_str();
613
+ }
614
+
615
+ static void ggml_backend_webgpu_device_get_memory(ggml_backend_dev_t dev, size_t * free, size_t * total) {
616
+ ggml_backend_webgpu_device_context * ctx = static_cast<ggml_backend_webgpu_device_context *>(dev->context);
617
+ // TODO: what do we actually want to return here? maxBufferSize might not be the full available memory.
618
+ *free = ctx->webgpu_ctx->limits.maxBufferSize;
619
+ *total = ctx->webgpu_ctx->limits.maxBufferSize;
620
+ }
621
+
622
+ static enum ggml_backend_dev_type ggml_backend_webgpu_device_get_type(ggml_backend_dev_t dev) {
623
+ GGML_UNUSED(dev);
624
+ return GGML_BACKEND_DEVICE_TYPE_GPU;
625
+ }
626
+
627
+ static void ggml_backend_webgpu_device_get_props(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props) {
628
+ props->name = ggml_backend_webgpu_device_get_name(dev);
629
+ props->description = ggml_backend_webgpu_device_get_description(dev);
630
+ props->type = ggml_backend_webgpu_device_get_type(dev);
631
+ ggml_backend_webgpu_device_get_memory(dev, &props->memory_free, &props->memory_total);
632
+ props->caps = {
633
+ /* .async = */ false,
634
+ /* .host_buffer = */ false,
635
+ /* .buffer_from_host_ptr = */ false,
636
+ /* .events = */ false,
637
+ };
638
+ }
639
+
640
+ static ggml_guid_t ggml_backend_webgpu_guid(void) {
641
+ static const char * guid_str = "__ggml_webgpu :)";
642
+ return reinterpret_cast<ggml_guid_t>((void *)guid_str);
643
+ }
644
+
645
+ static void ggml_webgpu_init_memset_pipeline(webgpu_context webgpu_ctx) {
646
+ // we use the maximum workgroup size for the memset pipeline
647
+ size_t max_wg_size = webgpu_ctx->limits.maxComputeWorkgroupSizeX;
648
+ size_t max_threads = max_wg_size * webgpu_ctx->limits.maxComputeWorkgroupsPerDimension;
649
+ // Size the bytes_per_thread so that the largest buffer size can be handled
650
+ webgpu_ctx->memset_bytes_per_thread = (webgpu_ctx->limits.maxStorageBufferBindingSize + max_threads - 1) / max_threads;
651
+ std::vector<wgpu::ConstantEntry> constants(2);
652
+ constants[0].key = "wg_size";
653
+ constants[0].value = max_wg_size;
654
+ constants[1].key = "bytes_per_thread";
655
+ constants[1].value = webgpu_ctx->memset_bytes_per_thread;
656
+ ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->memset_pipeline, wgsl_memset, "memset", constants);
657
+ ggml_webgpu_create_buffer(webgpu_ctx->device, webgpu_ctx->memset_params_dev_buf,
658
+ 3 * sizeof(uint32_t), // 3 parameters: buffer size, offset, value
659
+ wgpu::BufferUsage::Uniform | wgpu::BufferUsage::CopyDst, "memset_params_dev_buf");
660
+ ggml_webgpu_create_buffer(webgpu_ctx->device, webgpu_ctx->memset_params_host_buf,
661
+ 3 * sizeof(uint32_t), wgpu::BufferUsage::MapWrite | wgpu::BufferUsage::CopySrc, "memset_params_host_buf");
662
+ }
663
+
664
+ static void ggml_webgpu_init_mul_mat_pipeline(webgpu_context webgpu_ctx) {
665
+ ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->mul_mat_pipeline, wgsl_mul_mat, "mul_mat");
666
+ ggml_webgpu_create_buffer(webgpu_ctx->device, webgpu_ctx->mul_mat_params_dev_buf, WEBGPU_MUL_MAT_PARAMS_SIZE,
667
+ wgpu::BufferUsage::Uniform | wgpu::BufferUsage::CopyDst, "mul_mat_params_dev_buf");
668
+ ggml_webgpu_create_buffer(webgpu_ctx->device, webgpu_ctx->mul_mat_params_host_buf, WEBGPU_MUL_MAT_PARAMS_SIZE,
669
+ wgpu::BufferUsage::MapWrite | wgpu::BufferUsage::CopySrc, "mul_mat_params_host_buf");
670
+ }
671
+
672
+ static void ggml_webgpu_init_cpy_pipeline(webgpu_context webgpu_ctx) {
673
+ std::vector<wgpu::ConstantEntry> constants(1);
674
+ constants[0].key = "wg_size";
675
+ constants[0].value = webgpu_ctx->limits.maxComputeWorkgroupSizeX;
676
+
677
+ ggml_webgpu_create_pipeline(webgpu_ctx->device, webgpu_ctx->cpy_pipeline, wgsl_cpy, "cpy", constants);
678
+ ggml_webgpu_create_buffer(webgpu_ctx->device, webgpu_ctx->cpy_params_dev_buf, WEBGPU_CPY_PARAMS_SIZE,
679
+ wgpu::BufferUsage::Uniform | wgpu::BufferUsage::CopyDst, "cpy_params_dev_buf");
680
+ ggml_webgpu_create_buffer(webgpu_ctx->device, webgpu_ctx->cpy_params_host_buf, WEBGPU_CPY_PARAMS_SIZE,
681
+ wgpu::BufferUsage::MapWrite | wgpu::BufferUsage::CopySrc, "cpy_params_host_buf");
682
+ }
683
+
684
+ // TODO: Make thread safe if multiple devices are used
685
+ static ggml_backend_t ggml_backend_webgpu_device_init(ggml_backend_dev_t dev, const char * params) {
686
+ GGML_UNUSED(params);
687
+
688
+ WEBGPU_LOG_DEBUG("ggml_backend_webgpu_device_init()");
689
+
690
+ ggml_backend_webgpu_device_context * dev_ctx = static_cast<ggml_backend_webgpu_device_context *>(dev->context);
691
+ webgpu_context webgpu_ctx = dev_ctx->webgpu_ctx;
692
+
693
+ std::lock_guard<std::mutex> lock(webgpu_ctx->mutex);
694
+
695
+ if (!webgpu_ctx->device_initialized) {
696
+ // Initialize device
697
+ wgpu::DeviceDescriptor dev_desc;
698
+ dev_desc.requiredLimits = &webgpu_ctx->limits;
699
+ dev_desc.requiredFeatures = webgpu_ctx->features.features;
700
+ dev_desc.requiredFeatureCount = webgpu_ctx->features.featureCount;
701
+ dev_desc.SetDeviceLostCallback(wgpu::CallbackMode::AllowSpontaneous,
702
+ [](const wgpu::Device& device, wgpu::DeviceLostReason reason, wgpu::StringView message) {
703
+ GGML_UNUSED(device);
704
+ GGML_LOG_ERROR("ggml_webgpu: Device lost! Reason: %d, Message: %s\n", static_cast<int>(reason), message.data);
705
+ });
706
+ dev_desc.SetUncapturedErrorCallback(
707
+ [](const wgpu::Device& device, wgpu::ErrorType reason, wgpu::StringView message) {
708
+ GGML_UNUSED(device);
709
+ GGML_LOG_ERROR("ggml_webgpu: Device error! Reason: %d, Message: %s\n", static_cast<int>(reason), message.data);
710
+ });
711
+ webgpu_ctx->instance.WaitAny(webgpu_ctx->adapter.RequestDevice(&dev_desc, wgpu::CallbackMode::WaitAnyOnly,
712
+ [webgpu_ctx](wgpu::RequestDeviceStatus status, wgpu::Device device, wgpu::StringView message) {
713
+ if (status != wgpu::RequestDeviceStatus::Success) {
714
+ GGML_LOG_ERROR("ggml_webgpu: Failed to get a device: %s\n", message.data);
715
+ return;
716
+ }
717
+ webgpu_ctx->device = device;
718
+ }),
719
+ UINT64_MAX
720
+ );
721
+ GGML_ASSERT(webgpu_ctx->device != nullptr);
722
+
723
+ // Initialize (compute) queue
724
+ webgpu_ctx->queue = webgpu_ctx->device.GetQueue();
725
+
726
+ ggml_webgpu_init_memset_pipeline(webgpu_ctx);
727
+ ggml_webgpu_init_mul_mat_pipeline(webgpu_ctx);
728
+ ggml_webgpu_init_cpy_pipeline(webgpu_ctx);
729
+ webgpu_ctx->device_initialized = true;
730
+ }
731
+
732
+ static ggml_backend_webgpu_context backend_ctx;
733
+ backend_ctx.name = GGML_WEBGPU_NAME + std::string(": ") + dev_ctx->device_name;
734
+ backend_ctx.webgpu_ctx = webgpu_ctx;
735
+
736
+ // See GGML Backend Interface section
737
+ static ggml_backend backend = {
738
+ /* .guid = */ ggml_backend_webgpu_guid(),
739
+ /* .interface = */ ggml_backend_webgpu_i,
740
+ /* .device = */ dev,
741
+ /* .context = */ &backend_ctx,
742
+ };
743
+
744
+ return &backend;
745
+ }
746
+
747
+ static ggml_backend_buffer_type_t ggml_backend_webgpu_device_get_buffer_type(ggml_backend_dev_t dev) {
748
+ // See GGML Backend Buffer Type Interface section
749
+ static struct ggml_backend_buffer_type ggml_backend_webgpu_buffer_type = {
750
+ /* .iface = */ {
751
+ /* .get_name = */ ggml_backend_webgpu_buffer_type_get_name,
752
+ /* .alloc_buffer = */ ggml_backend_webgpu_buffer_type_alloc_buffer,
753
+ /* .get_alignment = */ ggml_backend_webgpu_buffer_type_get_alignment,
754
+ /* .get_max_size = */ ggml_backend_webgpu_buffer_type_get_max_size,
755
+ /* .get_alloc_size = */ NULL, // defaults to ggml_nbytes
756
+ /* .is_host = */ NULL, // defaults to false
757
+ },
758
+ /* .device = */ dev,
759
+ /* .context = */ NULL,
760
+ };
761
+
762
+ return &ggml_backend_webgpu_buffer_type;
763
+ }
764
+
765
+ static bool ggml_backend_webgpu_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
766
+ GGML_UNUSED(dev);
767
+ return buft->iface.get_name == ggml_backend_webgpu_buffer_type_get_name;
768
+ }
769
+
770
+ static bool ggml_backend_webgpu_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
771
+ GGML_UNUSED(dev);
772
+
773
+ switch (op->op) {
774
+ case GGML_OP_NONE:
775
+ case GGML_OP_VIEW:
776
+ case GGML_OP_PERMUTE:
777
+ return true;
778
+ case GGML_OP_CPY:
779
+ return op->type == GGML_TYPE_F16 && op->src[0]->type == GGML_TYPE_F32;
780
+ case GGML_OP_MUL_MAT:
781
+ return op->src[0]->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_F32;
782
+ default:
783
+ return false;
784
+ }
785
+ }
786
+
787
+ static struct ggml_backend_device_i ggml_backend_webgpu_device_i = {
788
+ /* .get_name = */ ggml_backend_webgpu_device_get_name,
789
+ /* .get_description = */ ggml_backend_webgpu_device_get_description,
790
+ /* .get_memory = */ ggml_backend_webgpu_device_get_memory,
791
+ /* .get_type = */ ggml_backend_webgpu_device_get_type,
792
+ /* .get_props = */ ggml_backend_webgpu_device_get_props,
793
+ /* .init_backend = */ ggml_backend_webgpu_device_init,
794
+ /* .get_buffer_type = */ ggml_backend_webgpu_device_get_buffer_type,
795
+ /* .get_host_buffer_type = */ NULL,
796
+ /* .buffer_from_host_ptr = */ NULL,
797
+ /* .supports_op = */ ggml_backend_webgpu_device_supports_op,
798
+ /* .supports_buft = */ ggml_backend_webgpu_device_supports_buft,
799
+ /* .offload_op = */ NULL,
800
+ /* .event_new = */ NULL,
801
+ /* .event_free = */ NULL,
802
+ /* .event_synchronize = */ NULL,
803
+ };
804
+
805
+ /* End GGML Backend Device Interface */
806
+
807
+ /* GGML Backend Registration Interface */
808
+
809
+ static const char * ggml_backend_webgpu_reg_get_name(ggml_backend_reg_t reg) {
810
+ ggml_backend_webgpu_reg_context * ctx = static_cast<ggml_backend_webgpu_reg_context *>(reg->context);
811
+ return ctx->name;
812
+ }
813
+
814
+ static size_t ggml_backend_webgpu_reg_get_device_count(ggml_backend_reg_t reg) {
815
+ ggml_backend_webgpu_reg_context * ctx = static_cast<ggml_backend_webgpu_reg_context *>(reg->context);
816
+ return ctx->device_count;
817
+ }
818
+
819
+ // TODO: Does this need to be thread safe? Is it only called once?
820
+ // Only one device is supported for now
821
+ static ggml_backend_dev_t ggml_backend_webgpu_reg_get_device(ggml_backend_reg_t reg, size_t index) {
822
+ GGML_ASSERT(index == 0);
823
+ WEBGPU_LOG_DEBUG("ggml_backend_reg_get_device()");
824
+
825
+ ggml_backend_webgpu_reg_context * reg_ctx = static_cast<ggml_backend_webgpu_reg_context *>(reg->context);
826
+
827
+ webgpu_context ctx = reg_ctx->webgpu_ctx;
828
+
829
+ wgpu::RequestAdapterOptions options = {};
830
+ auto callback = [](wgpu::RequestAdapterStatus status, wgpu::Adapter adapter, const char *message, void *userdata) {
831
+ if (status != wgpu::RequestAdapterStatus::Success) {
832
+ GGML_LOG_ERROR("ggml_webgpu: Failed to get an adapter: %s\n", message);
833
+ return;
834
+ }
835
+ *static_cast<wgpu::Adapter *>(userdata) = adapter;
836
+ };
837
+ void *userdata = &ctx->adapter;
838
+ ctx->instance.WaitAny(ctx->instance.RequestAdapter(&options, wgpu::CallbackMode::WaitAnyOnly, callback, userdata), UINT64_MAX);
839
+ GGML_ASSERT(ctx->adapter != nullptr);
840
+
841
+ ctx->adapter.GetLimits(&ctx->limits);
842
+ ctx->adapter.GetFeatures(&ctx->features);
843
+
844
+ wgpu::AdapterInfo info{};
845
+ ctx->adapter.GetInfo(&info);
846
+
847
+ static ggml_backend_webgpu_device_context device_ctx;
848
+ device_ctx.webgpu_ctx = ctx;
849
+ device_ctx.device_name = GGML_WEBGPU_NAME;
850
+ device_ctx.device_desc = std::string(info.description.data);
851
+
852
+ GGML_LOG_INFO("ggml_webgpu: adapter_info: vendor_id: %u | vendor: %s | architecture: %s | device_id: %u | name: %s | device_desc: %s\n",
853
+ info.vendorID, info.vendor.data, info.architecture.data, info.deviceID, info.device.data, info.description.data);
854
+
855
+ // See GGML Backend Device Interface section
856
+ static ggml_backend_device device = {
857
+ /* .iface = */ ggml_backend_webgpu_device_i,
858
+ /* .reg = */ reg,
859
+ /* .context = */ &device_ctx,
860
+ };
861
+ return &device;
862
+ }
863
+
864
+
865
+ static const struct ggml_backend_reg_i ggml_backend_webgpu_reg_i = {
866
+ /* .get_name = */ ggml_backend_webgpu_reg_get_name,
867
+ /* .get_device_count = */ ggml_backend_webgpu_reg_get_device_count,
868
+ /* .get_device = */ ggml_backend_webgpu_reg_get_device,
869
+ /* .get_proc_address = */ NULL,
870
+ };
871
+
872
+ /* End GGML Backend Registration Interface */
873
+
874
+ // TODO: Does this need to be thread safe? Is it only called once?
875
+ ggml_backend_reg_t ggml_backend_webgpu_reg() {
876
+ WEBGPU_LOG_DEBUG("ggml_backend_webgpu_reg()");
877
+
878
+ webgpu_context webgpu_ctx = std::make_shared<webgpu_context_struct>();
879
+ webgpu_ctx->device_initialized = false;
880
+
881
+ static ggml_backend_webgpu_reg_context ctx;
882
+ ctx.webgpu_ctx = webgpu_ctx;
883
+ ctx.name = GGML_WEBGPU_NAME;
884
+ ctx.device_count = 1;
885
+
886
+ wgpu::InstanceDescriptor instance_descriptor{};
887
+ std::vector<wgpu::InstanceFeatureName> instance_features = {wgpu::InstanceFeatureName::TimedWaitAny};
888
+ instance_descriptor.requiredFeatures = instance_features.data();
889
+ instance_descriptor.requiredFeatureCount = instance_features.size();
890
+ webgpu_ctx->instance = wgpu::CreateInstance(&instance_descriptor);
891
+ GGML_ASSERT(webgpu_ctx->instance != nullptr);
892
+
893
+ static ggml_backend_reg reg = {
894
+ /* .api_version = */ GGML_BACKEND_API_VERSION,
895
+ /* .iface = */ ggml_backend_webgpu_reg_i,
896
+ /* .context = */ &ctx,
897
+ };
898
+ return &reg;
899
+ }
900
+
901
+ ggml_backend_t ggml_backend_webgpu_init(void) {
902
+ ggml_backend_dev_t dev = ggml_backend_reg_dev_get(ggml_backend_webgpu_reg(), 0);
903
+
904
+ return ggml_backend_webgpu_device_init(dev, nullptr);
905
+ }
906
+
907
+ GGML_BACKEND_DL_IMPL(ggml_backend_webgpu_reg)