@fugood/llama.node 0.6.3 → 1.0.0-beta.2

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 (377) hide show
  1. package/CMakeLists.txt +40 -30
  2. package/README.md +4 -1
  3. package/lib/binding.js +41 -29
  4. package/lib/binding.ts +26 -25
  5. package/package.json +40 -7
  6. package/scripts/build.js +47 -0
  7. package/scripts/llama.cpp.patch +109 -0
  8. package/src/anyascii.c +22223 -0
  9. package/src/anyascii.h +42 -0
  10. package/src/tts_utils.cpp +20 -7
  11. package/src/tts_utils.h +2 -0
  12. package/bin/darwin/arm64/llama-node.node +0 -0
  13. package/bin/darwin/x64/llama-node.node +0 -0
  14. package/bin/linux/arm64/llama-node.node +0 -0
  15. package/bin/linux/x64/llama-node.node +0 -0
  16. package/bin/linux-cuda/arm64/llama-node.node +0 -0
  17. package/bin/linux-cuda/x64/llama-node.node +0 -0
  18. package/bin/linux-vulkan/arm64/llama-node.node +0 -0
  19. package/bin/linux-vulkan/x64/llama-node.node +0 -0
  20. package/bin/win32/x64/llama-node.node +0 -0
  21. package/bin/win32/x64/node.lib +0 -0
  22. package/bin/win32-vulkan/arm64/llama-node.node +0 -0
  23. package/bin/win32-vulkan/arm64/node.lib +0 -0
  24. package/bin/win32-vulkan/x64/llama-node.node +0 -0
  25. package/bin/win32-vulkan/x64/node.lib +0 -0
  26. package/src/llama.cpp/.github/workflows/build-linux-cross.yml +0 -233
  27. package/src/llama.cpp/.github/workflows/build.yml +0 -1078
  28. package/src/llama.cpp/.github/workflows/close-issue.yml +0 -28
  29. package/src/llama.cpp/.github/workflows/docker.yml +0 -178
  30. package/src/llama.cpp/.github/workflows/editorconfig.yml +0 -29
  31. package/src/llama.cpp/.github/workflows/gguf-publish.yml +0 -44
  32. package/src/llama.cpp/.github/workflows/labeler.yml +0 -17
  33. package/src/llama.cpp/.github/workflows/python-check-requirements.yml +0 -33
  34. package/src/llama.cpp/.github/workflows/python-lint.yml +0 -30
  35. package/src/llama.cpp/.github/workflows/python-type-check.yml +0 -40
  36. package/src/llama.cpp/.github/workflows/release.yml +0 -739
  37. package/src/llama.cpp/.github/workflows/server.yml +0 -237
  38. package/src/llama.cpp/.github/workflows/winget.yml +0 -42
  39. package/src/llama.cpp/cmake/arm64-apple-clang.cmake +0 -16
  40. package/src/llama.cpp/cmake/arm64-windows-llvm.cmake +0 -16
  41. package/src/llama.cpp/cmake/build-info.cmake +0 -64
  42. package/src/llama.cpp/cmake/common.cmake +0 -35
  43. package/src/llama.cpp/cmake/git-vars.cmake +0 -22
  44. package/src/llama.cpp/cmake/x64-windows-llvm.cmake +0 -5
  45. package/src/llama.cpp/common/build-info.cpp.in +0 -4
  46. package/src/llama.cpp/docs/build.md +0 -561
  47. package/src/llama.cpp/examples/CMakeLists.txt +0 -43
  48. package/src/llama.cpp/examples/batched/CMakeLists.txt +0 -5
  49. package/src/llama.cpp/examples/batched/batched.cpp +0 -246
  50. package/src/llama.cpp/examples/chat-13B.bat +0 -57
  51. package/src/llama.cpp/examples/convert-llama2c-to-ggml/CMakeLists.txt +0 -5
  52. package/src/llama.cpp/examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp +0 -941
  53. package/src/llama.cpp/examples/deprecation-warning/deprecation-warning.cpp +0 -35
  54. package/src/llama.cpp/examples/embedding/CMakeLists.txt +0 -5
  55. package/src/llama.cpp/examples/embedding/embedding.cpp +0 -323
  56. package/src/llama.cpp/examples/eval-callback/CMakeLists.txt +0 -10
  57. package/src/llama.cpp/examples/eval-callback/eval-callback.cpp +0 -194
  58. package/src/llama.cpp/examples/gen-docs/CMakeLists.txt +0 -5
  59. package/src/llama.cpp/examples/gen-docs/gen-docs.cpp +0 -83
  60. package/src/llama.cpp/examples/gguf/CMakeLists.txt +0 -5
  61. package/src/llama.cpp/examples/gguf/gguf.cpp +0 -265
  62. package/src/llama.cpp/examples/gguf-hash/CMakeLists.txt +0 -22
  63. package/src/llama.cpp/examples/gguf-hash/deps/rotate-bits/rotate-bits.h +0 -46
  64. package/src/llama.cpp/examples/gguf-hash/deps/sha1/sha1.c +0 -295
  65. package/src/llama.cpp/examples/gguf-hash/deps/sha1/sha1.h +0 -52
  66. package/src/llama.cpp/examples/gguf-hash/deps/sha256/sha256.c +0 -221
  67. package/src/llama.cpp/examples/gguf-hash/deps/sha256/sha256.h +0 -24
  68. package/src/llama.cpp/examples/gguf-hash/deps/xxhash/xxhash.c +0 -42
  69. package/src/llama.cpp/examples/gguf-hash/deps/xxhash/xxhash.h +0 -7093
  70. package/src/llama.cpp/examples/gguf-hash/gguf-hash.cpp +0 -694
  71. package/src/llama.cpp/examples/gritlm/CMakeLists.txt +0 -5
  72. package/src/llama.cpp/examples/gritlm/gritlm.cpp +0 -229
  73. package/src/llama.cpp/examples/jeopardy/questions.txt +0 -100
  74. package/src/llama.cpp/examples/llama.android/app/build.gradle.kts +0 -65
  75. package/src/llama.cpp/examples/llama.android/build.gradle.kts +0 -6
  76. package/src/llama.cpp/examples/llama.android/llama/build.gradle.kts +0 -71
  77. package/src/llama.cpp/examples/llama.android/llama/src/main/cpp/CMakeLists.txt +0 -53
  78. package/src/llama.cpp/examples/llama.android/llama/src/main/cpp/llama-android.cpp +0 -452
  79. package/src/llama.cpp/examples/llama.android/settings.gradle.kts +0 -18
  80. package/src/llama.cpp/examples/lookahead/CMakeLists.txt +0 -5
  81. package/src/llama.cpp/examples/lookahead/lookahead.cpp +0 -472
  82. package/src/llama.cpp/examples/lookup/CMakeLists.txt +0 -23
  83. package/src/llama.cpp/examples/lookup/lookup-create.cpp +0 -40
  84. package/src/llama.cpp/examples/lookup/lookup-merge.cpp +0 -47
  85. package/src/llama.cpp/examples/lookup/lookup-stats.cpp +0 -157
  86. package/src/llama.cpp/examples/lookup/lookup.cpp +0 -242
  87. package/src/llama.cpp/examples/parallel/CMakeLists.txt +0 -5
  88. package/src/llama.cpp/examples/parallel/parallel.cpp +0 -492
  89. package/src/llama.cpp/examples/passkey/CMakeLists.txt +0 -5
  90. package/src/llama.cpp/examples/passkey/passkey.cpp +0 -277
  91. package/src/llama.cpp/examples/retrieval/CMakeLists.txt +0 -5
  92. package/src/llama.cpp/examples/retrieval/retrieval.cpp +0 -304
  93. package/src/llama.cpp/examples/save-load-state/CMakeLists.txt +0 -5
  94. package/src/llama.cpp/examples/save-load-state/save-load-state.cpp +0 -246
  95. package/src/llama.cpp/examples/simple/CMakeLists.txt +0 -5
  96. package/src/llama.cpp/examples/simple/simple.cpp +0 -206
  97. package/src/llama.cpp/examples/simple-chat/CMakeLists.txt +0 -5
  98. package/src/llama.cpp/examples/simple-chat/simple-chat.cpp +0 -206
  99. package/src/llama.cpp/examples/simple-cmake-pkg/CMakeLists.txt +0 -11
  100. package/src/llama.cpp/examples/speculative/CMakeLists.txt +0 -5
  101. package/src/llama.cpp/examples/speculative/speculative.cpp +0 -644
  102. package/src/llama.cpp/examples/speculative-simple/CMakeLists.txt +0 -5
  103. package/src/llama.cpp/examples/speculative-simple/speculative-simple.cpp +0 -261
  104. package/src/llama.cpp/examples/sycl/CMakeLists.txt +0 -9
  105. package/src/llama.cpp/examples/sycl/build.sh +0 -23
  106. package/src/llama.cpp/examples/sycl/ls-sycl-device.cpp +0 -13
  107. package/src/llama.cpp/examples/sycl/run-llama2.sh +0 -27
  108. package/src/llama.cpp/examples/sycl/run-llama3.sh +0 -28
  109. package/src/llama.cpp/examples/sycl/win-build-sycl.bat +0 -33
  110. package/src/llama.cpp/examples/sycl/win-run-llama2.bat +0 -9
  111. package/src/llama.cpp/examples/sycl/win-run-llama3.bat +0 -9
  112. package/src/llama.cpp/examples/training/CMakeLists.txt +0 -5
  113. package/src/llama.cpp/examples/training/finetune.cpp +0 -96
  114. package/src/llama.cpp/ggml/cmake/GitVars.cmake +0 -22
  115. package/src/llama.cpp/ggml/cmake/common.cmake +0 -26
  116. package/src/llama.cpp/ggml/src/ggml-alloc.c +0 -1042
  117. package/src/llama.cpp/ggml/src/ggml-backend-impl.h +0 -255
  118. package/src/llama.cpp/ggml/src/ggml-backend-reg.cpp +0 -586
  119. package/src/llama.cpp/ggml/src/ggml-backend.cpp +0 -2008
  120. package/src/llama.cpp/ggml/src/ggml-blas/CMakeLists.txt +0 -87
  121. package/src/llama.cpp/ggml/src/ggml-blas/ggml-blas.cpp +0 -517
  122. package/src/llama.cpp/ggml/src/ggml-cann/CMakeLists.txt +0 -74
  123. package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.cpp +0 -179
  124. package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.h +0 -258
  125. package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.cpp +0 -2863
  126. package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.h +0 -1110
  127. package/src/llama.cpp/ggml/src/ggml-cann/common.h +0 -420
  128. package/src/llama.cpp/ggml/src/ggml-cann/ggml-cann.cpp +0 -2570
  129. package/src/llama.cpp/ggml/src/ggml-common.h +0 -1857
  130. package/src/llama.cpp/ggml/src/ggml-cpu/cmake/FindSIMD.cmake +0 -100
  131. package/src/llama.cpp/ggml/src/ggml-cuda/CMakeLists.txt +0 -184
  132. package/src/llama.cpp/ggml/src/ggml-cuda/vendors/cuda.h +0 -15
  133. package/src/llama.cpp/ggml/src/ggml-cuda/vendors/hip.h +0 -243
  134. package/src/llama.cpp/ggml/src/ggml-cuda/vendors/musa.h +0 -140
  135. package/src/llama.cpp/ggml/src/ggml-hip/CMakeLists.txt +0 -131
  136. package/src/llama.cpp/ggml/src/ggml-impl.h +0 -601
  137. package/src/llama.cpp/ggml/src/ggml-kompute/CMakeLists.txt +0 -166
  138. package/src/llama.cpp/ggml/src/ggml-kompute/ggml-kompute.cpp +0 -2251
  139. package/src/llama.cpp/ggml/src/ggml-metal/CMakeLists.txt +0 -120
  140. package/src/llama.cpp/ggml/src/ggml-metal/ggml-metal-impl.h +0 -622
  141. package/src/llama.cpp/ggml/src/ggml-musa/CMakeLists.txt +0 -113
  142. package/src/llama.cpp/ggml/src/ggml-opencl/CMakeLists.txt +0 -96
  143. package/src/llama.cpp/ggml/src/ggml-opencl/ggml-opencl.cpp +0 -5124
  144. package/src/llama.cpp/ggml/src/ggml-opt.cpp +0 -1037
  145. package/src/llama.cpp/ggml/src/ggml-quants.c +0 -5232
  146. package/src/llama.cpp/ggml/src/ggml-quants.h +0 -100
  147. package/src/llama.cpp/ggml/src/ggml-rpc/CMakeLists.txt +0 -9
  148. package/src/llama.cpp/ggml/src/ggml-rpc/ggml-rpc.cpp +0 -1813
  149. package/src/llama.cpp/ggml/src/ggml-sycl/CMakeLists.txt +0 -189
  150. package/src/llama.cpp/ggml/src/ggml-sycl/backend.hpp +0 -37
  151. package/src/llama.cpp/ggml/src/ggml-sycl/binbcast.cpp +0 -239
  152. package/src/llama.cpp/ggml/src/ggml-sycl/binbcast.hpp +0 -39
  153. package/src/llama.cpp/ggml/src/ggml-sycl/common.cpp +0 -83
  154. package/src/llama.cpp/ggml/src/ggml-sycl/common.hpp +0 -493
  155. package/src/llama.cpp/ggml/src/ggml-sycl/concat.cpp +0 -197
  156. package/src/llama.cpp/ggml/src/ggml-sycl/concat.hpp +0 -20
  157. package/src/llama.cpp/ggml/src/ggml-sycl/conv.cpp +0 -100
  158. package/src/llama.cpp/ggml/src/ggml-sycl/conv.hpp +0 -20
  159. package/src/llama.cpp/ggml/src/ggml-sycl/convert.cpp +0 -623
  160. package/src/llama.cpp/ggml/src/ggml-sycl/convert.hpp +0 -34
  161. package/src/llama.cpp/ggml/src/ggml-sycl/cpy.cpp +0 -701
  162. package/src/llama.cpp/ggml/src/ggml-sycl/cpy.hpp +0 -11
  163. package/src/llama.cpp/ggml/src/ggml-sycl/dequantize.hpp +0 -791
  164. package/src/llama.cpp/ggml/src/ggml-sycl/dmmv.cpp +0 -1160
  165. package/src/llama.cpp/ggml/src/ggml-sycl/dmmv.hpp +0 -27
  166. package/src/llama.cpp/ggml/src/ggml-sycl/dpct/helper.hpp +0 -2957
  167. package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.cpp +0 -1536
  168. package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.hpp +0 -75
  169. package/src/llama.cpp/ggml/src/ggml-sycl/gemm.hpp +0 -99
  170. package/src/llama.cpp/ggml/src/ggml-sycl/getrows.cpp +0 -311
  171. package/src/llama.cpp/ggml/src/ggml-sycl/getrows.hpp +0 -20
  172. package/src/llama.cpp/ggml/src/ggml-sycl/ggml-sycl.cpp +0 -4443
  173. package/src/llama.cpp/ggml/src/ggml-sycl/gla.cpp +0 -105
  174. package/src/llama.cpp/ggml/src/ggml-sycl/gla.hpp +0 -8
  175. package/src/llama.cpp/ggml/src/ggml-sycl/im2col.cpp +0 -136
  176. package/src/llama.cpp/ggml/src/ggml-sycl/im2col.hpp +0 -21
  177. package/src/llama.cpp/ggml/src/ggml-sycl/mmq.cpp +0 -3030
  178. package/src/llama.cpp/ggml/src/ggml-sycl/mmq.hpp +0 -33
  179. package/src/llama.cpp/ggml/src/ggml-sycl/mmvq.cpp +0 -1108
  180. package/src/llama.cpp/ggml/src/ggml-sycl/mmvq.hpp +0 -27
  181. package/src/llama.cpp/ggml/src/ggml-sycl/norm.cpp +0 -474
  182. package/src/llama.cpp/ggml/src/ggml-sycl/norm.hpp +0 -26
  183. package/src/llama.cpp/ggml/src/ggml-sycl/outprod.cpp +0 -46
  184. package/src/llama.cpp/ggml/src/ggml-sycl/outprod.hpp +0 -10
  185. package/src/llama.cpp/ggml/src/ggml-sycl/presets.hpp +0 -74
  186. package/src/llama.cpp/ggml/src/ggml-sycl/quants.hpp +0 -83
  187. package/src/llama.cpp/ggml/src/ggml-sycl/rope.cpp +0 -362
  188. package/src/llama.cpp/ggml/src/ggml-sycl/rope.hpp +0 -20
  189. package/src/llama.cpp/ggml/src/ggml-sycl/softmax.cpp +0 -264
  190. package/src/llama.cpp/ggml/src/ggml-sycl/softmax.hpp +0 -20
  191. package/src/llama.cpp/ggml/src/ggml-sycl/sycl_hw.cpp +0 -13
  192. package/src/llama.cpp/ggml/src/ggml-sycl/sycl_hw.hpp +0 -23
  193. package/src/llama.cpp/ggml/src/ggml-sycl/tsembd.cpp +0 -73
  194. package/src/llama.cpp/ggml/src/ggml-sycl/tsembd.hpp +0 -20
  195. package/src/llama.cpp/ggml/src/ggml-sycl/vecdotq.hpp +0 -1215
  196. package/src/llama.cpp/ggml/src/ggml-sycl/wkv.cpp +0 -305
  197. package/src/llama.cpp/ggml/src/ggml-sycl/wkv.hpp +0 -10
  198. package/src/llama.cpp/ggml/src/ggml-threading.cpp +0 -12
  199. package/src/llama.cpp/ggml/src/ggml-threading.h +0 -14
  200. package/src/llama.cpp/ggml/src/ggml-vulkan/CMakeLists.txt +0 -196
  201. package/src/llama.cpp/ggml/src/ggml-vulkan/ggml-vulkan.cpp +0 -10699
  202. package/src/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt +0 -39
  203. package/src/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +0 -751
  204. package/src/llama.cpp/ggml/src/ggml.c +0 -6550
  205. package/src/llama.cpp/ggml/src/gguf.cpp +0 -1330
  206. package/src/llama.cpp/models/.editorconfig +0 -1
  207. package/src/llama.cpp/models/ggml-vocab-aquila.gguf +0 -0
  208. package/src/llama.cpp/models/ggml-vocab-baichuan.gguf +0 -0
  209. package/src/llama.cpp/models/ggml-vocab-bert-bge.gguf +0 -0
  210. package/src/llama.cpp/models/ggml-vocab-bert-bge.gguf.inp +0 -112
  211. package/src/llama.cpp/models/ggml-vocab-bert-bge.gguf.out +0 -46
  212. package/src/llama.cpp/models/ggml-vocab-chameleon.gguf.inp +0 -112
  213. package/src/llama.cpp/models/ggml-vocab-chameleon.gguf.out +0 -46
  214. package/src/llama.cpp/models/ggml-vocab-command-r.gguf +0 -0
  215. package/src/llama.cpp/models/ggml-vocab-command-r.gguf.inp +0 -112
  216. package/src/llama.cpp/models/ggml-vocab-command-r.gguf.out +0 -46
  217. package/src/llama.cpp/models/ggml-vocab-deepseek-coder.gguf +0 -0
  218. package/src/llama.cpp/models/ggml-vocab-deepseek-coder.gguf.inp +0 -112
  219. package/src/llama.cpp/models/ggml-vocab-deepseek-coder.gguf.out +0 -46
  220. package/src/llama.cpp/models/ggml-vocab-deepseek-llm.gguf +0 -0
  221. package/src/llama.cpp/models/ggml-vocab-deepseek-llm.gguf.inp +0 -112
  222. package/src/llama.cpp/models/ggml-vocab-deepseek-llm.gguf.out +0 -46
  223. package/src/llama.cpp/models/ggml-vocab-deepseek-r1-qwen.gguf.inp +0 -112
  224. package/src/llama.cpp/models/ggml-vocab-deepseek-r1-qwen.gguf.out +0 -46
  225. package/src/llama.cpp/models/ggml-vocab-falcon.gguf +0 -0
  226. package/src/llama.cpp/models/ggml-vocab-falcon.gguf.inp +0 -112
  227. package/src/llama.cpp/models/ggml-vocab-falcon.gguf.out +0 -46
  228. package/src/llama.cpp/models/ggml-vocab-gpt-2.gguf +0 -0
  229. package/src/llama.cpp/models/ggml-vocab-gpt-2.gguf.inp +0 -112
  230. package/src/llama.cpp/models/ggml-vocab-gpt-2.gguf.out +0 -46
  231. package/src/llama.cpp/models/ggml-vocab-gpt-4o.gguf.inp +0 -112
  232. package/src/llama.cpp/models/ggml-vocab-gpt-4o.gguf.out +0 -46
  233. package/src/llama.cpp/models/ggml-vocab-gpt-neox.gguf +0 -0
  234. package/src/llama.cpp/models/ggml-vocab-llama-bpe.gguf +0 -0
  235. package/src/llama.cpp/models/ggml-vocab-llama-bpe.gguf.inp +0 -112
  236. package/src/llama.cpp/models/ggml-vocab-llama-bpe.gguf.out +0 -46
  237. package/src/llama.cpp/models/ggml-vocab-llama-spm.gguf +0 -0
  238. package/src/llama.cpp/models/ggml-vocab-llama-spm.gguf.inp +0 -112
  239. package/src/llama.cpp/models/ggml-vocab-llama-spm.gguf.out +0 -46
  240. package/src/llama.cpp/models/ggml-vocab-llama4.gguf.inp +0 -112
  241. package/src/llama.cpp/models/ggml-vocab-llama4.gguf.out +0 -46
  242. package/src/llama.cpp/models/ggml-vocab-mpt.gguf +0 -0
  243. package/src/llama.cpp/models/ggml-vocab-mpt.gguf.inp +0 -112
  244. package/src/llama.cpp/models/ggml-vocab-mpt.gguf.out +0 -46
  245. package/src/llama.cpp/models/ggml-vocab-phi-3.gguf +0 -0
  246. package/src/llama.cpp/models/ggml-vocab-phi-3.gguf.inp +0 -112
  247. package/src/llama.cpp/models/ggml-vocab-phi-3.gguf.out +0 -46
  248. package/src/llama.cpp/models/ggml-vocab-pixtral.gguf.inp +0 -112
  249. package/src/llama.cpp/models/ggml-vocab-pixtral.gguf.out +0 -46
  250. package/src/llama.cpp/models/ggml-vocab-qwen2.gguf +0 -0
  251. package/src/llama.cpp/models/ggml-vocab-qwen2.gguf.inp +0 -112
  252. package/src/llama.cpp/models/ggml-vocab-qwen2.gguf.out +0 -46
  253. package/src/llama.cpp/models/ggml-vocab-refact.gguf +0 -0
  254. package/src/llama.cpp/models/ggml-vocab-refact.gguf.inp +0 -112
  255. package/src/llama.cpp/models/ggml-vocab-refact.gguf.out +0 -46
  256. package/src/llama.cpp/models/ggml-vocab-roberta-bpe.gguf.inp +0 -112
  257. package/src/llama.cpp/models/ggml-vocab-roberta-bpe.gguf.out +0 -46
  258. package/src/llama.cpp/models/ggml-vocab-starcoder.gguf +0 -0
  259. package/src/llama.cpp/models/ggml-vocab-starcoder.gguf.inp +0 -112
  260. package/src/llama.cpp/models/ggml-vocab-starcoder.gguf.out +0 -46
  261. package/src/llama.cpp/pocs/CMakeLists.txt +0 -14
  262. package/src/llama.cpp/pocs/vdot/CMakeLists.txt +0 -9
  263. package/src/llama.cpp/pocs/vdot/q8dot.cpp +0 -173
  264. package/src/llama.cpp/pocs/vdot/vdot.cpp +0 -311
  265. package/src/llama.cpp/prompts/LLM-questions.txt +0 -49
  266. package/src/llama.cpp/prompts/alpaca.txt +0 -1
  267. package/src/llama.cpp/prompts/assistant.txt +0 -31
  268. package/src/llama.cpp/prompts/chat-with-baichuan.txt +0 -4
  269. package/src/llama.cpp/prompts/chat-with-bob.txt +0 -7
  270. package/src/llama.cpp/prompts/chat-with-qwen.txt +0 -1
  271. package/src/llama.cpp/prompts/chat-with-vicuna-v0.txt +0 -7
  272. package/src/llama.cpp/prompts/chat-with-vicuna-v1.txt +0 -7
  273. package/src/llama.cpp/prompts/chat.txt +0 -28
  274. package/src/llama.cpp/prompts/dan-modified.txt +0 -1
  275. package/src/llama.cpp/prompts/dan.txt +0 -1
  276. package/src/llama.cpp/prompts/mnemonics.txt +0 -93
  277. package/src/llama.cpp/prompts/parallel-questions.txt +0 -43
  278. package/src/llama.cpp/prompts/reason-act.txt +0 -18
  279. package/src/llama.cpp/requirements/requirements-all.txt +0 -15
  280. package/src/llama.cpp/requirements/requirements-compare-llama-bench.txt +0 -2
  281. package/src/llama.cpp/requirements/requirements-convert_hf_to_gguf.txt +0 -7
  282. package/src/llama.cpp/requirements/requirements-convert_hf_to_gguf_update.txt +0 -7
  283. package/src/llama.cpp/requirements/requirements-convert_legacy_llama.txt +0 -5
  284. package/src/llama.cpp/requirements/requirements-convert_llama_ggml_to_gguf.txt +0 -1
  285. package/src/llama.cpp/requirements/requirements-convert_lora_to_gguf.txt +0 -4
  286. package/src/llama.cpp/requirements/requirements-gguf_editor_gui.txt +0 -3
  287. package/src/llama.cpp/requirements/requirements-pydantic.txt +0 -3
  288. package/src/llama.cpp/requirements/requirements-test-tokenizer-random.txt +0 -1
  289. package/src/llama.cpp/requirements/requirements-tool_bench.txt +0 -12
  290. package/src/llama.cpp/requirements.txt +0 -13
  291. package/src/llama.cpp/scripts/build-info.sh +0 -30
  292. package/src/llama.cpp/scripts/install-oneapi.bat +0 -19
  293. package/src/llama.cpp/scripts/xxd.cmake +0 -16
  294. package/src/llama.cpp/tests/CMakeLists.txt +0 -177
  295. package/src/llama.cpp/tests/get-model.cpp +0 -21
  296. package/src/llama.cpp/tests/get-model.h +0 -2
  297. package/src/llama.cpp/tests/test-arg-parser.cpp +0 -178
  298. package/src/llama.cpp/tests/test-autorelease.cpp +0 -24
  299. package/src/llama.cpp/tests/test-backend-ops.cpp +0 -4793
  300. package/src/llama.cpp/tests/test-barrier.cpp +0 -94
  301. package/src/llama.cpp/tests/test-c.c +0 -7
  302. package/src/llama.cpp/tests/test-chat-template.cpp +0 -417
  303. package/src/llama.cpp/tests/test-chat.cpp +0 -985
  304. package/src/llama.cpp/tests/test-double-float.cpp +0 -57
  305. package/src/llama.cpp/tests/test-gbnf-validator.cpp +0 -109
  306. package/src/llama.cpp/tests/test-gguf.cpp +0 -1338
  307. package/src/llama.cpp/tests/test-grammar-integration.cpp +0 -1308
  308. package/src/llama.cpp/tests/test-grammar-llguidance.cpp +0 -1201
  309. package/src/llama.cpp/tests/test-grammar-parser.cpp +0 -519
  310. package/src/llama.cpp/tests/test-json-schema-to-grammar.cpp +0 -1304
  311. package/src/llama.cpp/tests/test-llama-grammar.cpp +0 -408
  312. package/src/llama.cpp/tests/test-log.cpp +0 -39
  313. package/src/llama.cpp/tests/test-model-load-cancel.cpp +0 -27
  314. package/src/llama.cpp/tests/test-mtmd-c-api.c +0 -63
  315. package/src/llama.cpp/tests/test-opt.cpp +0 -904
  316. package/src/llama.cpp/tests/test-quantize-fns.cpp +0 -186
  317. package/src/llama.cpp/tests/test-quantize-perf.cpp +0 -365
  318. package/src/llama.cpp/tests/test-quantize-stats.cpp +0 -424
  319. package/src/llama.cpp/tests/test-regex-partial.cpp +0 -288
  320. package/src/llama.cpp/tests/test-rope.cpp +0 -262
  321. package/src/llama.cpp/tests/test-sampling.cpp +0 -399
  322. package/src/llama.cpp/tests/test-tokenizer-0.cpp +0 -312
  323. package/src/llama.cpp/tests/test-tokenizer-1-bpe.cpp +0 -155
  324. package/src/llama.cpp/tests/test-tokenizer-1-spm.cpp +0 -125
  325. package/src/llama.cpp/tools/CMakeLists.txt +0 -39
  326. package/src/llama.cpp/tools/batched-bench/CMakeLists.txt +0 -5
  327. package/src/llama.cpp/tools/batched-bench/batched-bench.cpp +0 -204
  328. package/src/llama.cpp/tools/cvector-generator/CMakeLists.txt +0 -5
  329. package/src/llama.cpp/tools/cvector-generator/completions.txt +0 -582
  330. package/src/llama.cpp/tools/cvector-generator/cvector-generator.cpp +0 -508
  331. package/src/llama.cpp/tools/cvector-generator/mean.hpp +0 -48
  332. package/src/llama.cpp/tools/cvector-generator/negative.txt +0 -4
  333. package/src/llama.cpp/tools/cvector-generator/pca.hpp +0 -315
  334. package/src/llama.cpp/tools/cvector-generator/positive.txt +0 -4
  335. package/src/llama.cpp/tools/export-lora/CMakeLists.txt +0 -5
  336. package/src/llama.cpp/tools/export-lora/export-lora.cpp +0 -434
  337. package/src/llama.cpp/tools/gguf-split/CMakeLists.txt +0 -5
  338. package/src/llama.cpp/tools/gguf-split/gguf-split.cpp +0 -583
  339. package/src/llama.cpp/tools/imatrix/CMakeLists.txt +0 -5
  340. package/src/llama.cpp/tools/imatrix/imatrix.cpp +0 -667
  341. package/src/llama.cpp/tools/llama-bench/CMakeLists.txt +0 -5
  342. package/src/llama.cpp/tools/llama-bench/llama-bench.cpp +0 -2024
  343. package/src/llama.cpp/tools/main/CMakeLists.txt +0 -5
  344. package/src/llama.cpp/tools/main/main.cpp +0 -977
  345. package/src/llama.cpp/tools/mtmd/CMakeLists.txt +0 -58
  346. package/src/llama.cpp/tools/mtmd/clip-impl.h +0 -462
  347. package/src/llama.cpp/tools/mtmd/clip.cpp +0 -4024
  348. package/src/llama.cpp/tools/mtmd/clip.h +0 -101
  349. package/src/llama.cpp/tools/mtmd/deprecation-warning.cpp +0 -22
  350. package/src/llama.cpp/tools/mtmd/miniaudio.h +0 -93468
  351. package/src/llama.cpp/tools/mtmd/mtmd-audio.cpp +0 -855
  352. package/src/llama.cpp/tools/mtmd/mtmd-audio.h +0 -62
  353. package/src/llama.cpp/tools/mtmd/mtmd-cli.cpp +0 -377
  354. package/src/llama.cpp/tools/mtmd/mtmd-helper.cpp +0 -297
  355. package/src/llama.cpp/tools/mtmd/mtmd.cpp +0 -942
  356. package/src/llama.cpp/tools/mtmd/mtmd.h +0 -362
  357. package/src/llama.cpp/tools/mtmd/requirements.txt +0 -5
  358. package/src/llama.cpp/tools/perplexity/CMakeLists.txt +0 -5
  359. package/src/llama.cpp/tools/perplexity/perplexity.cpp +0 -2063
  360. package/src/llama.cpp/tools/quantize/CMakeLists.txt +0 -6
  361. package/src/llama.cpp/tools/quantize/quantize.cpp +0 -519
  362. package/src/llama.cpp/tools/rpc/CMakeLists.txt +0 -4
  363. package/src/llama.cpp/tools/rpc/rpc-server.cpp +0 -322
  364. package/src/llama.cpp/tools/run/CMakeLists.txt +0 -16
  365. package/src/llama.cpp/tools/run/linenoise.cpp/linenoise.cpp +0 -1995
  366. package/src/llama.cpp/tools/run/linenoise.cpp/linenoise.h +0 -137
  367. package/src/llama.cpp/tools/run/run.cpp +0 -1261
  368. package/src/llama.cpp/tools/server/CMakeLists.txt +0 -51
  369. package/src/llama.cpp/tools/server/bench/requirements.txt +0 -2
  370. package/src/llama.cpp/tools/server/httplib.h +0 -10506
  371. package/src/llama.cpp/tools/server/server.cpp +0 -4966
  372. package/src/llama.cpp/tools/server/tests/requirements.txt +0 -8
  373. package/src/llama.cpp/tools/server/utils.hpp +0 -1337
  374. package/src/llama.cpp/tools/tokenize/CMakeLists.txt +0 -5
  375. package/src/llama.cpp/tools/tokenize/tokenize.cpp +0 -416
  376. package/src/llama.cpp/tools/tts/CMakeLists.txt +0 -5
  377. package/src/llama.cpp/tools/tts/tts.cpp +0 -1092
@@ -1,4443 +0,0 @@
1
- //
2
- // MIT license
3
- // Copyright (C) 2024 Intel Corporation
4
- // SPDX-License-Identifier: MIT
5
- //
6
-
7
- //
8
- // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
9
- // See https://llvm.org/LICENSE.txt for license information.
10
- // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11
- //
12
-
13
- #include <algorithm>
14
- #include <assert.h>
15
- #include <atomic>
16
- #include <cinttypes>
17
- #include <cstddef>
18
- #include <cstdint>
19
- #include <cstdlib>
20
- #include <float.h>
21
- #include <limits>
22
- #include <stdint.h>
23
- #include <stdio.h>
24
- #include <vector>
25
- #include <cmath>
26
- #include <iostream>
27
- #include <fstream>
28
- #include <stdio.h>
29
- #include <stdlib.h>
30
- #include <regex>
31
-
32
- #include <sycl/sycl.hpp>
33
- #include <sycl/half_type.hpp>
34
-
35
- #include "ggml-sycl.h"
36
- #include "ggml-impl.h"
37
- #include "ggml-backend-impl.h"
38
-
39
- #include "ggml-sycl/backend.hpp"
40
- #include "ggml-sycl/common.hpp"
41
- #include "ggml-sycl/element_wise.hpp"
42
- #include "ggml-sycl/presets.hpp"
43
- #include "ggml-sycl/gemm.hpp"
44
- #include "ggml-sycl/sycl_hw.hpp"
45
- #include "ggml-sycl/getrows.hpp"
46
- #include "ggml.h"
47
-
48
- static bool g_sycl_loaded = false;
49
- int g_ggml_sycl_debug = 0;
50
- int g_ggml_sycl_disable_optimize = 0;
51
- int g_ggml_sycl_disable_graph = 0;
52
- int g_ggml_sycl_disable_dnn = 0;
53
- int g_ggml_sycl_prioritize_dmmv = 0;
54
-
55
- static ggml_sycl_device_info ggml_sycl_init() {
56
- ggml_sycl_device_info info = {};
57
-
58
- info.device_count = dpct::dev_mgr::instance().device_count();
59
- if (info.device_count == 0) {
60
- GGML_LOG_ERROR("%s: failed to initialize: %s\n", GGML_SYCL_NAME, __func__);
61
- return info;
62
- }
63
-
64
- GGML_ASSERT(info.device_count <= GGML_SYCL_MAX_DEVICES);
65
-
66
- int64_t total_vram = 0;
67
- /* This is a bit misleading; reserved for later */
68
- // #if defined(SYCL_USE_XMX)
69
- // GGML_LOG_INFO("%s: SYCL_USE_XMX: yes\n", __func__);
70
- // #else
71
- // GGML_LOG_INFO("%s: SYCL_USE_XMX: no\n", __func__);
72
- // #endif
73
- for (int i = 0; i < info.device_count; ++i) {
74
- info.devices[i].vmm = 0;
75
- dpct::device_info prop;
76
- sycl::device device = dpct::dev_mgr::instance().get_device(i);
77
-
78
- SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
79
- prop, device)));
80
-
81
- info.default_tensor_split[i] = total_vram;
82
- total_vram += prop.get_global_mem_size();
83
-
84
- info.devices[i].cc =
85
- 100 * prop.get_major_version() + 10 * prop.get_minor_version();
86
- info.devices[i].hw_info = get_device_hw_info(&device);
87
- info.devices[i].opt_feature = check_gpu_optimize_feature(info.devices[i].hw_info.arch);
88
-
89
- info.max_work_group_sizes[i] = prop.get_max_work_group_size();
90
- }
91
-
92
- for (int id = 0; id < info.device_count; ++id) {
93
- info.default_tensor_split[id] /= total_vram;
94
- }
95
- return info;
96
- }
97
-
98
- const ggml_sycl_device_info & ggml_sycl_info() {
99
- static ggml_sycl_device_info info = ggml_sycl_init();
100
- return info;
101
- }
102
-
103
- static void print_device_detail(int id, sycl::device &device, std::string device_type) {
104
-
105
- dpct::device_info prop;
106
- SYCL_CHECK(CHECK_TRY_ERROR(
107
- dpct::get_device_info(prop, device)));
108
-
109
- std::string version;
110
- version += std::to_string(prop.get_major_version());
111
- version += ".";
112
- version += std::to_string(prop.get_minor_version());
113
-
114
- device_type = std::regex_replace(device_type, std::regex("ext_oneapi_"), "");
115
- std::string name = std::string(prop.get_name());
116
- name = std::regex_replace(name, std::regex("\\(R\\)"), "");
117
- name = std::regex_replace(name, std::regex("\\(TM\\)"), "");
118
-
119
- auto global_mem_size = prop.get_global_mem_size()/1000000;
120
- GGML_LOG_INFO("|%2d|%19s|%39s|%7s|%7d|%8d|%5d|%6luM|%21s|\n", id, device_type.c_str(),
121
- name.c_str(), version.c_str(), prop.get_max_compute_units(),
122
- prop.get_max_work_group_size(), prop.get_max_sub_group_size(),
123
- global_mem_size, device.get_info<sycl::info::device::driver_version>().c_str());
124
- }
125
-
126
- static void print_device_opt_feature(int device_count) {
127
- GGML_LOG_INFO("SYCL Optimization Feature:\n");
128
- GGML_LOG_INFO(
129
- "|ID| Device Type|Reorder|\n");
130
- GGML_LOG_INFO(
131
- "|--|-------------------|-------|\n");
132
- std::map<std::string, size_t> DeviceNums;
133
- for (int id = 0; id < device_count; ++id) {
134
- sycl::device device = dpct::dev_mgr::instance().get_device(id);
135
- std::string backend_type = get_device_backend_and_type(device);
136
- int type_id = DeviceNums[backend_type]++;
137
- std::stringstream device_type;
138
- device_type << "[" << backend_type << ":" << std::to_string(type_id)
139
- << "]";
140
- std::string device_type_s = device_type.str();
141
- device_type_s = std::regex_replace(device_type_s, std::regex("ext_oneapi_"), "");
142
- GGML_LOG_INFO("|%2d|%19s|%7s|\n", id, device_type_s.c_str(),
143
- ggml_sycl_info().devices[id].opt_feature.reorder ? "Y": "N");
144
- }
145
-
146
- }
147
- void ggml_backend_sycl_print_sycl_devices() {
148
- GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_print_sycl_devices\n");
149
- int device_count = dpct::dev_mgr::instance().device_count();
150
- std::map<std::string, size_t> DeviceNums;
151
- GGML_LOG_INFO("Found %d SYCL devices:\n", device_count);
152
-
153
- GGML_LOG_INFO(
154
- "| | | | "
155
- " |Max | |Max |Global | |\n");
156
- GGML_LOG_INFO(
157
- "| | | | "
158
- " |compute|Max work|sub |mem | |\n");
159
- GGML_LOG_INFO(
160
- "|ID| Device Type| "
161
- "Name|Version|units |group |group|size | Driver version|\n");
162
- GGML_LOG_INFO(
163
- "|--|-------------------|---------------------------------------|------"
164
- "-|-------|--------|-----|-------|---------------------|\n");
165
-
166
- for (int id = 0; id < device_count; ++id) {
167
- sycl::device device = dpct::dev_mgr::instance().get_device(id);
168
- std::string backend_type = get_device_backend_and_type(device);
169
- int type_id = DeviceNums[backend_type]++;
170
- std::stringstream device_type;
171
- device_type << "[" << backend_type << ":" << std::to_string(type_id)
172
- << "]";
173
- print_device_detail(id, device, device_type.str());
174
- }
175
-
176
- print_device_opt_feature(device_count);
177
- }
178
-
179
- static inline int get_sycl_env(const char *env_name, int default_val) {
180
- char *user_device_string = getenv(env_name);
181
- int user_number = default_val;
182
-
183
- unsigned n;
184
- if (user_device_string != NULL &&
185
- sscanf(user_device_string, " %u", &n) == 1) {
186
- user_number = (int)n;
187
- } else {
188
- user_number = default_val;
189
- }
190
- return user_number;
191
- }
192
-
193
- static void ggml_check_sycl() try {
194
- static bool initialized = false;
195
-
196
- if (!initialized) {
197
- g_ggml_sycl_debug = get_sycl_env("GGML_SYCL_DEBUG", 0);
198
- g_ggml_sycl_disable_optimize= get_sycl_env("GGML_SYCL_DISABLE_OPT", 1);
199
- g_ggml_sycl_disable_graph = get_sycl_env("GGML_SYCL_DISABLE_GRAPH", 1);
200
- g_ggml_sycl_disable_dnn = get_sycl_env("GGML_SYCL_DISABLE_DNN", 0);
201
- g_ggml_sycl_prioritize_dmmv = get_sycl_env("GGML_SYCL_PRIORITIZE_DMMV", 0);
202
- GGML_SYCL_DEBUG("[SYCL] call ggml_check_sycl\n");
203
- GGML_LOG_INFO("Running with Environment Variables:\n");
204
- GGML_LOG_INFO(" GGML_SYCL_DEBUG: %d\n", g_ggml_sycl_debug);
205
- GGML_LOG_INFO(" GGML_SYCL_DISABLE_OPT: %d\n", g_ggml_sycl_disable_optimize);
206
- #ifdef GGML_SYCL_GRAPH
207
- GGML_LOG_INFO(" GGML_SYCL_DISABLE_GRAPH: %d\n", g_ggml_sycl_disable_graph);
208
- #else
209
- GGML_LOG_INFO(" GGML_SYCL_DISABLE_GRAPH: graph disabled by compile flag\n");
210
- #endif
211
- #if GGML_SYCL_DNNL
212
- GGML_LOG_INFO(" GGML_SYCL_DISABLE_DNN: %d\n", g_ggml_sycl_disable_dnn);
213
- #else
214
- GGML_LOG_INFO(" GGML_SYCL_DISABLE_DNN: DNN disabled by compile flag\n");
215
- #endif
216
- GGML_LOG_INFO(" GGML_SYCL_PRIORITIZE_DMMV: %d\n", g_ggml_sycl_prioritize_dmmv);
217
- GGML_LOG_INFO("Build with Macros:\n");
218
- #if defined(GGML_SYCL_FORCE_MMQ)
219
- GGML_LOG_INFO(" GGML_SYCL_FORCE_MMQ: yes\n");
220
- #else
221
- GGML_LOG_INFO(" GGML_SYCL_FORCE_MMQ: no\n");
222
- #endif
223
- #if defined(GGML_SYCL_F16)
224
- GGML_LOG_INFO(" GGML_SYCL_F16: yes\n");
225
- #else
226
- GGML_LOG_INFO(" GGML_SYCL_F16: no\n");
227
- #endif
228
-
229
- /* NOT REMOVE, keep it for next optimize for XMX.
230
- #if defined(SYCL_USE_XMX)
231
- fprintf(stderr, "%s: SYCL_USE_XMX: yes\n", __func__);
232
- #else
233
- fprintf(stderr, "%s: SYCL_USE_XMX: no\n", __func__);
234
- #endif
235
- */
236
-
237
- if (CHECK_TRY_ERROR(g_all_sycl_device_count =
238
- dpct::dev_mgr::instance().device_count()) != 0) {
239
- initialized = true;
240
- g_sycl_loaded = false;
241
- return;
242
- }
243
- GGML_ASSERT(g_all_sycl_device_count <= GGML_SYCL_MAX_DEVICES);
244
-
245
- initialized = true;
246
- g_sycl_loaded = true;
247
- ggml_backend_sycl_print_sycl_devices();
248
- }
249
- }
250
- catch (sycl::exception const &exc) {
251
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
252
- << ", line:" << __LINE__ << std::endl;
253
- std::exit(1);
254
- }
255
-
256
- /*
257
- device_index: device index from 0 to n (continue numbers).
258
- It is used for device select/set in SYCL backend internal data structure.
259
- */
260
- inline void check_allow_gpu_index(const int device_index) {
261
- if (device_index >= ggml_sycl_info().device_count) {
262
- char error_buf[256];
263
- snprintf(
264
- error_buf,
265
- sizeof(error_buf),
266
- "%s error: device_index:%d is out of range: [0-%d]",
267
- __func__,
268
- device_index,
269
- ggml_sycl_info().device_count - 1);
270
- GGML_LOG_ERROR("%s\n", error_buf);
271
- assert(false);
272
- }
273
- }
274
-
275
- GGML_API void ggml_backend_sycl_get_gpu_list(int *id_list, int max_len) try {
276
- GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_gpu_list\n");
277
- for(int i=0;i<max_len;i++) id_list[i] = -1;
278
-
279
- for (int i=0;i< ggml_sycl_info().device_count;i++){
280
- if (i>=max_len) break;
281
- id_list[i] = i;
282
- }
283
- return;
284
- }
285
- catch (sycl::exception const &exc) {
286
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
287
- << ", line:" << __LINE__ << std::endl;
288
- std::exit(1);
289
- }
290
-
291
- // sycl buffer
292
-
293
- struct ggml_backend_sycl_buffer_context {
294
- int device;
295
- void * dev_ptr = nullptr;
296
- queue_ptr stream;
297
- std::string name;
298
- optimize_feature opt_feature;
299
- std::vector<ggml_tensor_extra_gpu *> tensor_extras;
300
-
301
- ggml_backend_sycl_buffer_context(int device, void * dev_ptr, queue_ptr stream) :
302
- device(device), dev_ptr(dev_ptr), stream(stream) {
303
- check_allow_gpu_index(device);
304
- name = (GGML_SYCL_NAME + std::to_string(device));
305
- opt_feature = ggml_sycl_info().devices[device].opt_feature;
306
- }
307
-
308
- ~ggml_backend_sycl_buffer_context() {
309
- if (dev_ptr != nullptr) {
310
- ggml_sycl_set_device(device);
311
- SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(dev_ptr, *stream)));
312
- }
313
-
314
- //release extra used by tensors
315
- for (ggml_tensor_extra_gpu * extra : tensor_extras) {
316
- release_extra_gpu(extra);
317
- }
318
-
319
- }
320
- };
321
-
322
- static const char * ggml_backend_sycl_buffer_type_get_name(ggml_backend_buffer_type_t buft);
323
-
324
- static bool ggml_backend_buffer_is_sycl(ggml_backend_buffer_t buffer) {
325
- return buffer->buft->iface.get_name == ggml_backend_sycl_buffer_type_get_name;
326
- }
327
-
328
- static void
329
- ggml_backend_sycl_buffer_free_buffer(ggml_backend_buffer_t buffer) try {
330
- ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
331
- ggml_sycl_set_device(ctx->device);
332
-
333
- delete ctx;
334
- }
335
- catch (sycl::exception const &exc) {
336
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
337
- << ", line:" << __LINE__ << std::endl;
338
- std::exit(1);
339
- }
340
-
341
- static void * ggml_backend_sycl_buffer_get_base(ggml_backend_buffer_t buffer) {
342
- ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
343
- return ctx->dev_ptr;
344
- }
345
-
346
- static enum ggml_status
347
- ggml_backend_sycl_buffer_init_tensor(ggml_backend_buffer_t buffer,
348
- ggml_tensor *tensor) try {
349
- ggml_backend_sycl_buffer_context * ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
350
-
351
- if (tensor->view_src != NULL) {
352
- assert(tensor->view_src->buffer->buft == buffer->buft);
353
- return GGML_STATUS_SUCCESS;
354
- }
355
- if ((tensor->type == GGML_TYPE_Q4_0 || tensor->type == GGML_TYPE_Q4_K) && !g_ggml_sycl_disable_optimize) {
356
- ggml_tensor_extra_gpu * extra = new ggml_tensor_extra_gpu{};
357
- tensor->extra = extra;
358
- ctx->tensor_extras.push_back(extra); //used to release it when destroy ctx.
359
- }
360
-
361
- if (ggml_is_quantized(tensor->type)) {
362
- // initialize padding to 0 to avoid possible NaN values
363
- size_t original_size = ggml_nbytes(tensor);
364
- size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
365
-
366
- if (padded_size > original_size && tensor->view_src == nullptr) {
367
- SYCL_CHECK(CHECK_TRY_ERROR(ctx->stream->memset(
368
- (char *)tensor->data + original_size, 0,
369
- padded_size - original_size).wait()));
370
- }
371
- }
372
- return GGML_STATUS_SUCCESS;
373
- }
374
- catch (sycl::exception const &exc) {
375
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
376
- << ", line:" << __LINE__ << std::endl;
377
- std::exit(1);
378
- }
379
-
380
- static void ggml_backend_sycl_buffer_set_tensor(ggml_backend_buffer_t buffer,
381
- ggml_tensor *tensor,
382
- const void *data, size_t offset,
383
- size_t size) try {
384
-
385
- ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
386
- ggml_sycl_set_device(ctx->device);
387
- auto stream = &(dpct::dev_mgr::instance().get_device(ctx->device).default_queue());
388
- SYCL_CHECK(CHECK_TRY_ERROR(dpct::dev_mgr::instance().get_device(ctx->device).queues_wait_and_throw()));
389
- #ifndef _WIN32
390
- // Note: Use host buffer to save the data from mmap(), then copy to device. It's workaround for mmap() issue on PVC GPU.
391
- // This function will be called during load model from disk. Use memory buffer replace dynamic won't save more time and brings potential memory leak risk here.
392
- char * host_buf = (char *) malloc(size);
393
- memcpy(host_buf, data, size);
394
- SYCL_CHECK(CHECK_TRY_ERROR((*stream).memcpy((char *) tensor->data + offset, host_buf, size).wait()));
395
- free(host_buf);
396
- #else
397
- SYCL_CHECK(CHECK_TRY_ERROR((*stream).memcpy((char *) tensor->data + offset, data, size).wait()));
398
- #endif
399
- }
400
- catch (sycl::exception const &exc) {
401
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
402
- << ", line:" << __LINE__ << std::endl;
403
- std::exit(1);
404
- }
405
-
406
- static void ggml_backend_sycl_buffer_get_tensor(ggml_backend_buffer_t buffer,
407
- const ggml_tensor *tensor,
408
- void *data, size_t offset,
409
- size_t size) try {
410
-
411
- ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
412
-
413
- ggml_sycl_set_device(ctx->device);
414
- auto stream = dpct::dev_mgr::instance().get_device(ctx->device).default_queue();
415
-
416
- SYCL_CHECK(CHECK_TRY_ERROR(
417
- stream.memcpy(data, (const char *)tensor->data + offset, size)
418
- .wait()));
419
- }
420
- catch (sycl::exception const &exc) {
421
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
422
- << ", line:" << __LINE__ << std::endl;
423
- std::exit(1);
424
- }
425
-
426
- static void dev2dev_memcpy(sycl::queue &q_dst, sycl::queue &q_src, void *ptr_dst,
427
- const void *ptr_src, size_t size) {
428
- char *host_buf = (char *)malloc(size);
429
- q_src.memcpy(host_buf, (const char *)ptr_src, size).wait();
430
- q_dst.memcpy((char *)ptr_dst, host_buf, size).wait();
431
- free(host_buf);
432
- }
433
-
434
- static bool
435
- ggml_backend_sycl_buffer_cpy_tensor(ggml_backend_buffer_t buffer,
436
- const ggml_tensor *src,
437
- ggml_tensor *dst) try {
438
- if (ggml_backend_buffer_is_sycl(src->buffer)) {
439
- ggml_backend_sycl_buffer_context * src_ctx = (ggml_backend_sycl_buffer_context *)src->buffer->context;
440
- ggml_backend_sycl_buffer_context * dst_ctx = (ggml_backend_sycl_buffer_context *)dst->buffer->context;
441
-
442
- ggml_sycl_set_device(src_ctx->device);
443
- /*
444
- DPCT1009:198: SYCL uses exceptions to report errors and does not use the
445
- error codes. The original code was commented out and a warning string
446
- was inserted. You need to rewrite this code.
447
- */
448
- SYCL_CHECK(CHECK_TRY_ERROR(
449
- dpct::dev_mgr::instance().get_device(src_ctx->device).queues_wait_and_throw()));
450
- ggml_sycl_set_device(dst_ctx->device);
451
- /*
452
- DPCT1009:199: SYCL uses exceptions to report errors and does not use the
453
- error codes. The original code was commented out and a warning string
454
- was inserted. You need to rewrite this code.
455
- */
456
- SYCL_CHECK(CHECK_TRY_ERROR(
457
- dpct::dev_mgr::instance().get_device(dst_ctx->device).queues_wait_and_throw()));
458
- /*
459
- DPCT1009:200: SYCL uses exceptions to report errors and does not use the
460
- error codes. The original code was commented out and a warning string
461
- was inserted. You need to rewrite this code.
462
- */
463
-
464
- queue_ptr stream_dst = dst_ctx->stream;
465
- queue_ptr stream_src = src_ctx->stream;
466
- size_t size = ggml_nbytes(src);
467
-
468
- //todo. it's dirty solutino to walkaroud known issue:device2device cross GPUs.
469
- dev2dev_memcpy(*stream_dst, *stream_src, dst->data, src->data, size);
470
-
471
- //todo, it's known issue:error in device2device cross GPUs. reused when the issue is fixed. DON"T remove
472
- #if 0
473
- SYCL_CHECK(CHECK_TRY_ERROR((*stream).memcpy(
474
- (char *)dst->data, (const char *)src->data, size).wait()));
475
-
476
- /*
477
- DPCT1009:201: SYCL uses exceptions to report errors and does not use the
478
- error codes. The original code was commented out and a warning string
479
- was inserted. You need to rewrite this code.
480
- */
481
- SYCL_CHECK(CHECK_TRY_ERROR(
482
- dpct::dev_mgr::instance().get_device(dst_ctx->device).queues_wait_and_throw()));
483
- #endif
484
- return true;
485
- }
486
- return false;
487
- GGML_UNUSED(buffer);
488
- } catch (const sycl::exception & exc) {
489
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl;
490
- std::exit(1);
491
- }
492
-
493
- static void ggml_backend_sycl_buffer_clear(ggml_backend_buffer_t buffer,
494
- uint8_t value) try {
495
- ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
496
-
497
- ggml_sycl_set_device(ctx->device);
498
- queue_ptr stream = ctx->stream;
499
- SYCL_CHECK(
500
- CHECK_TRY_ERROR(dpct::get_current_device().queues_wait_and_throw()));
501
-
502
- SYCL_CHECK(CHECK_TRY_ERROR((*stream)
503
- .memset(ctx->dev_ptr, value, buffer->size)
504
- .wait()));
505
- }
506
- catch (sycl::exception const &exc) {
507
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
508
- << ", line:" << __LINE__ << std::endl;
509
- std::exit(1);
510
- }
511
-
512
- static void ggml_backend_sycl_buffer_memset_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, uint8_t value,
513
- size_t offset, size_t size) {
514
- GGML_SYCL_DEBUG(" [SYCL] call %s\n", __func__);
515
- ggml_backend_sycl_buffer_context * ctx = (ggml_backend_sycl_buffer_context *) buffer->context;
516
- SYCL_CHECK(ggml_sycl_set_device(ctx->device));
517
- auto stream = &(dpct::dev_mgr::instance().get_device(ctx->device).default_queue());
518
- if (size == 0) {
519
- return; // Nothing to do
520
- }
521
- if (tensor->data == nullptr) {
522
- GGML_ABORT("Error: Tensor data pointer is null.\n");
523
- }
524
- void * target_ptr = static_cast<char *>(tensor->data) + offset;
525
- SYCL_CHECK(CHECK_TRY_ERROR((*stream).memset(target_ptr, value, size)));
526
- SYCL_CHECK(CHECK_TRY_ERROR((*stream).wait()));
527
- }
528
-
529
- static void ggml_backend_sycl_buffer_reset(ggml_backend_buffer_t buffer) {
530
- GGML_SYCL_DEBUG("[SYCL] call %s\n", __func__);
531
- if (buffer == nullptr) {
532
- return;
533
- }
534
-
535
- ggml_backend_sycl_buffer_context * ctx = (ggml_backend_sycl_buffer_context *) buffer->context;
536
-
537
- if (ctx != nullptr) {
538
- for (ggml_tensor_extra_gpu * extra : ctx->tensor_extras) {
539
- release_extra_gpu(extra);
540
- }
541
- ctx->tensor_extras.clear(); // reset the tensor_extras vector
542
- }
543
- }
544
-
545
- static const ggml_backend_buffer_i ggml_backend_sycl_buffer_interface = {
546
- /* .free_buffer = */ ggml_backend_sycl_buffer_free_buffer,
547
- /* .get_base = */ ggml_backend_sycl_buffer_get_base,
548
- /* .init_tensor = */ ggml_backend_sycl_buffer_init_tensor,
549
- /* .memset_tensor = */ ggml_backend_sycl_buffer_memset_tensor,
550
- /* .set_tensor = */ ggml_backend_sycl_buffer_set_tensor,
551
- /* .get_tensor = */ ggml_backend_sycl_buffer_get_tensor,
552
- /* .cpy_tensor = */ ggml_backend_sycl_buffer_cpy_tensor,
553
- /* .clear = */ ggml_backend_sycl_buffer_clear,
554
- /* .reset = */ ggml_backend_sycl_buffer_reset,
555
- };
556
-
557
- // sycl buffer type
558
- struct ggml_backend_sycl_buffer_type_context {
559
- int device;
560
- std::string name;
561
-
562
- // each buffer type has its own stream
563
- queue_ptr stream = nullptr;
564
- };
565
-
566
- static const char * ggml_backend_sycl_buffer_type_get_name(ggml_backend_buffer_type_t buft) {
567
- ggml_backend_sycl_buffer_type_context * ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
568
-
569
- return ctx->name.c_str();
570
- }
571
-
572
- static ggml_backend_buffer_t
573
- ggml_backend_sycl_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft,
574
- size_t size) try {
575
- ggml_backend_sycl_buffer_type_context * buft_ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
576
- ggml_sycl_set_device(buft_ctx->device);
577
- const queue_ptr stream = buft_ctx->stream;
578
- size = std::max(size, (size_t)1); // syclMalloc returns null for size 0
579
-
580
- void * dev_ptr;
581
- SYCL_CHECK(CHECK_TRY_ERROR(dev_ptr = (void *)sycl::malloc_device(
582
- size, *stream)));
583
- if (!dev_ptr) {
584
- GGML_LOG_ERROR("%s: can't allocate %lu Bytes of memory on device\n", __func__, size);
585
- return nullptr;
586
- }
587
- ggml_backend_sycl_buffer_context * ctx = new ggml_backend_sycl_buffer_context(buft_ctx->device, dev_ptr, buft_ctx->stream);
588
- return ggml_backend_buffer_init(buft, ggml_backend_sycl_buffer_interface, ctx, size);
589
- }
590
- catch (sycl::exception const &exc) {
591
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
592
- << ", line:" << __LINE__ << std::endl;
593
- std::exit(1);
594
- }
595
-
596
- static size_t ggml_backend_sycl_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
597
- return 128;
598
- GGML_UNUSED(buft);
599
- }
600
-
601
- static size_t ggml_backend_sycl_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
602
- return dpct::get_current_device().get_max_mem_alloc_size();
603
-
604
- GGML_UNUSED(buft);
605
- }
606
-
607
- static size_t ggml_backend_sycl_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
608
- size_t size = ggml_nbytes(tensor);
609
- int64_t ne0 = tensor->ne[0];
610
-
611
- if (ggml_is_quantized(tensor->type)) {
612
- if (ne0 % MATRIX_ROW_PADDING != 0) {
613
- size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
614
- }
615
- }
616
-
617
- return size;
618
-
619
- GGML_UNUSED(buft);
620
- }
621
-
622
- static const ggml_backend_buffer_type_i ggml_backend_sycl_buffer_type_interface = {
623
- /* .get_name = */ ggml_backend_sycl_buffer_type_get_name,
624
- /* .alloc_buffer = */ ggml_backend_sycl_buffer_type_alloc_buffer,
625
- /* .get_alignment = */ ggml_backend_sycl_buffer_type_get_alignment,
626
- /* .get_max_size = */ ggml_backend_sycl_buffer_type_get_max_size,
627
- /* .get_alloc_size = */ ggml_backend_sycl_buffer_type_get_alloc_size,
628
- /* .is_host = */ NULL,
629
- };
630
-
631
- ggml_backend_buffer_type_t ggml_backend_sycl_buffer_type(int device) {
632
- static std::mutex mutex;
633
- std::lock_guard<std::mutex> lock(mutex);
634
-
635
-
636
- auto dev_count = ggml_backend_sycl_get_device_count();
637
-
638
- if (device>=dev_count or device<0) {
639
- GGML_LOG_ERROR("ggml_backend_sycl_buffer_type error: device_index:%d is out of range [0, %d], miss to call ggml_backend_sycl_set_single_device()\n",
640
- device, dev_count-1);
641
- GGML_ASSERT(device<dev_count);
642
- }
643
- static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_types[GGML_SYCL_MAX_DEVICES];
644
-
645
- static bool ggml_backend_sycl_buffer_type_initialized = false;
646
-
647
- if (!ggml_backend_sycl_buffer_type_initialized) {
648
- for (int i = 0; i < dev_count; i++) {
649
- auto & device_i = dpct::dev_mgr::instance().get_device(i);
650
- queue_ptr stream = &(device_i.default_queue());
651
- ggml_backend_sycl_buffer_types[i] = {
652
- /* .iface = */ ggml_backend_sycl_buffer_type_interface,
653
- /* .device = */ ggml_backend_reg_dev_get(ggml_backend_sycl_reg(), i),
654
- /* .context = */ new ggml_backend_sycl_buffer_type_context{i, GGML_SYCL_NAME + std::to_string(i), stream},
655
- };
656
- }
657
- ggml_backend_sycl_buffer_type_initialized = true;
658
- }
659
- return &ggml_backend_sycl_buffer_types[device];
660
- }
661
-
662
- static ggml_backend_buffer_type_t ggml_backend_sycl_buffer_type(ggml_backend_sycl_context * ctx) {
663
- GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_buffer_type\n");
664
-
665
- int device = ctx->device;
666
- if (device>=ggml_sycl_info().device_count or device<0) {
667
- GGML_LOG_ERROR("ggml_backend_sycl_buffer_type error: device_index:%d is out of range [0, %d], miss to call ggml_backend_sycl_set_single_device()\n",
668
- device, ggml_sycl_info().device_count-1);
669
- GGML_ASSERT(device<ggml_sycl_info().device_count);
670
- }
671
- static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_types[GGML_SYCL_MAX_DEVICES];
672
-
673
- static bool ggml_backend_sycl_buffer_type_initialized = false;
674
-
675
- if (!ggml_backend_sycl_buffer_type_initialized) {
676
- for (int i = 0; i < ggml_sycl_info().device_count; i++) {
677
- ggml_backend_sycl_buffer_types[i] = {
678
- /* .iface = */ ggml_backend_sycl_buffer_type_interface,
679
- /* .device = */ nullptr,
680
- /* .context = */ new ggml_backend_sycl_buffer_type_context{i, GGML_SYCL_NAME + std::to_string(i), ctx->stream(i, 0)},
681
- };
682
- }
683
- ggml_backend_sycl_buffer_type_initialized = true;
684
- }
685
- return &ggml_backend_sycl_buffer_types[device];
686
- }
687
-
688
- // sycl split buffer
689
-
690
- static int64_t get_row_rounding(ggml_type type, const std::array<float, GGML_SYCL_MAX_DEVICES> & tensor_split) {
691
- int64_t min_compute_capability = INT_MAX;
692
- int64_t max_compute_capability = INT_MIN;
693
- for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
694
- if (tensor_split[i] < (i + 1 < ggml_sycl_info().device_count ? tensor_split[i + 1] : 1.0f)) {
695
- if (min_compute_capability > ggml_sycl_info().devices[i].cc) {
696
- min_compute_capability = ggml_sycl_info().devices[i].cc;
697
- }
698
- if (max_compute_capability < ggml_sycl_info().devices[i].cc) {
699
- max_compute_capability = ggml_sycl_info().devices[i].cc;
700
- }
701
- }
702
- }
703
-
704
- switch(type) {
705
- case GGML_TYPE_Q4_0:
706
- case GGML_TYPE_Q4_1:
707
- return max_compute_capability >= VER_GEN9 ? 128 : 64;
708
- case GGML_TYPE_Q5_0:
709
- case GGML_TYPE_Q5_1:
710
- case GGML_TYPE_Q8_0:
711
- return 64;
712
- case GGML_TYPE_F16:
713
- case GGML_TYPE_F32:
714
- return 1;
715
- case GGML_TYPE_Q2_K:
716
- case GGML_TYPE_Q3_K:
717
- case GGML_TYPE_Q4_K:
718
- case GGML_TYPE_Q5_K:
719
- case GGML_TYPE_IQ2_XXS:
720
- case GGML_TYPE_IQ2_XS:
721
- case GGML_TYPE_IQ2_S:
722
- case GGML_TYPE_IQ1_S:
723
- case GGML_TYPE_IQ1_M:
724
- case GGML_TYPE_IQ3_XXS:
725
- case GGML_TYPE_IQ4_XS:
726
- case GGML_TYPE_IQ4_NL:
727
- return max_compute_capability >= VER_GEN9 ? 128 : 64;
728
- case GGML_TYPE_IQ3_S:
729
- return max_compute_capability >= VER_GEN9 ? 128 : 64;
730
- case GGML_TYPE_Q6_K:
731
- return 64;
732
- default:
733
- GGML_ABORT("fatal error");
734
- }
735
- }
736
-
737
- static void get_row_split(int64_t * row_low, int64_t * row_high, const ggml_tensor * tensor, const std::array<float, GGML_SYCL_MAX_DEVICES> & tensor_split, int id) {
738
- const int64_t nrows = ggml_nrows(tensor);
739
- const int64_t rounding = get_row_rounding(tensor->type, tensor_split);
740
-
741
- *row_low = id == 0 ? 0 : nrows*tensor_split[id];
742
- *row_low -= *row_low % rounding;
743
- if (id == ggml_sycl_info().device_count - 1) {
744
- *row_high = nrows;
745
- } else {
746
- *row_high = nrows*tensor_split[id + 1];
747
- *row_high -= *row_high % rounding;
748
- }
749
- }
750
-
751
- static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
752
- static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
753
-
754
- return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
755
- }
756
-
757
- struct ggml_backend_sycl_split_buffer_type_context {
758
- std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split;
759
- };
760
-
761
- struct ggml_backend_sycl_split_buffer_context {
762
- ~ggml_backend_sycl_split_buffer_context() try {
763
- for (ggml_tensor_extra_gpu * extra : tensor_extras) {
764
- release_extra_gpu(extra, streams);
765
- }
766
- }
767
- catch (sycl::exception const &exc) {
768
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
769
- << ", line:" << __LINE__ << std::endl;
770
- std::exit(1);
771
- }
772
-
773
- std::vector<ggml_tensor_extra_gpu *> tensor_extras;
774
- std::vector<queue_ptr> streams;
775
- };
776
-
777
- static void ggml_backend_sycl_split_buffer_free_buffer(ggml_backend_buffer_t buffer) {
778
- ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
779
- delete ctx;
780
- }
781
-
782
- static void * ggml_backend_sycl_split_buffer_get_base(ggml_backend_buffer_t buffer) {
783
- // the pointers are stored in the tensor extras, this is just a dummy address and never dereferenced
784
- return (void *)0x1000;
785
-
786
- GGML_UNUSED(buffer);
787
- }
788
-
789
- static enum ggml_status
790
- ggml_backend_sycl_split_buffer_init_tensor(ggml_backend_buffer_t buffer,
791
- ggml_tensor *tensor) try {
792
- GGML_ASSERT(tensor->view_src == nullptr); // views of split tensors are not supported
793
-
794
- ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
795
- ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
796
-
797
- const int64_t ne0 = tensor->ne[0];
798
-
799
- ggml_tensor_extra_gpu * extra = new ggml_tensor_extra_gpu{};
800
-
801
- ctx->tensor_extras.push_back(extra);
802
- ctx->streams.push_back(&(dpct::get_current_device().default_queue()));
803
-
804
- for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
805
- int64_t row_low, row_high;
806
- get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
807
-
808
- int64_t nrows_split = row_high - row_low;
809
- if (nrows_split == 0) {
810
- continue;
811
- }
812
-
813
- size_t size = ggml_nbytes_split(tensor, nrows_split);
814
- const size_t original_size = size;
815
-
816
- // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
817
- if (ne0 % MATRIX_ROW_PADDING != 0) {
818
- size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
819
- }
820
-
821
- // FIXME: do not crash if SYCL Buffer alloc fails
822
- // currently, init_tensor cannot fail, it needs to be fixed in ggml-backend first
823
- ggml_sycl_set_device(i);
824
- const queue_ptr stream = ctx->streams[i];
825
- char * buf;
826
- /*
827
- DPCT1009:208: SYCL uses exceptions to report errors and does not use the
828
- error codes. The original code was commented out and a warning string
829
- was inserted. You need to rewrite this code.
830
- */
831
- SYCL_CHECK(CHECK_TRY_ERROR(buf = (char *)sycl::malloc_device(
832
- size, *stream)));
833
- if (!buf) {
834
- char err_buf[1024];
835
- snprintf(err_buf, 1023, "%s: can't allocate %lu Bytes of memory on device\n", __func__, size);
836
- throw std::runtime_error(err_buf);
837
- }
838
- // set padding to 0 to avoid possible NaN values
839
- if (size > original_size) {
840
- /*
841
- DPCT1009:209: SYCL uses exceptions to report errors and does not use
842
- the error codes. The original code was commented out and a warning
843
- string was inserted. You need to rewrite this code.
844
- */
845
- SYCL_CHECK(CHECK_TRY_ERROR(
846
- (*stream)
847
- .memset(buf + original_size, 0, size - original_size)
848
- .wait()));
849
- }
850
-
851
- extra->data_device[i] = buf;
852
-
853
- for (int64_t is = 0; is < GGML_SYCL_MAX_STREAMS; ++is) {
854
- /*
855
- DPCT1009:210: SYCL uses exceptions to report errors and does not use
856
- the error codes. The original code was commented out and a warning
857
- string was inserted. You need to rewrite this code.
858
- */
859
- SYCL_CHECK(
860
- CHECK_TRY_ERROR(extra->events[i][is] = new sycl::event()));
861
- }
862
- }
863
- tensor->extra = extra;
864
- return GGML_STATUS_SUCCESS;
865
- }
866
- catch (sycl::exception const &exc) {
867
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
868
- << ", line:" << __LINE__ << std::endl;
869
- std::exit(1);
870
- }
871
-
872
- static void
873
- ggml_backend_sycl_split_buffer_set_tensor(ggml_backend_buffer_t buffer,
874
- ggml_tensor *tensor, const void *data,
875
- size_t offset, size_t size) try {
876
- // split tensors must always be set in their entirety at once
877
- GGML_ASSERT(offset == 0);
878
- GGML_ASSERT(size == ggml_nbytes(tensor));
879
-
880
- ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
881
- ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
882
-
883
- const int64_t ne0 = tensor->ne[0];
884
- const size_t nb1 = tensor->nb[1];
885
- ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
886
-
887
- for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
888
- int64_t row_low, row_high;
889
- get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
890
-
891
- int64_t nrows_split = row_high - row_low;
892
- if (nrows_split == 0) {
893
- continue;
894
- }
895
-
896
- const size_t offset_split = row_low*nb1;
897
- size_t size = ggml_nbytes_split(tensor, nrows_split);
898
- const size_t original_size = size;
899
-
900
- // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
901
- if (ne0 % MATRIX_ROW_PADDING != 0) {
902
- size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
903
- }
904
-
905
- const char * buf_host = (const char *)data + offset_split;
906
- /*
907
- DPCT1009:211: SYCL uses exceptions to report errors and does not use the
908
- error codes. The original code was commented out and a warning string
909
- was inserted. You need to rewrite this code.
910
- */
911
- ggml_sycl_set_device(i);
912
- const queue_ptr stream = ctx->streams[i];
913
- SYCL_CHECK(CHECK_TRY_ERROR(
914
- (*stream)
915
- .memcpy(extra->data_device[i], buf_host, original_size)
916
- .wait()));
917
- }
918
- }
919
- catch (sycl::exception const &exc) {
920
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
921
- << ", line:" << __LINE__ << std::endl;
922
- std::exit(1);
923
- }
924
-
925
- static void
926
- ggml_backend_sycl_split_buffer_get_tensor(ggml_backend_buffer_t buffer,
927
- const ggml_tensor *tensor, void *data,
928
- size_t offset, size_t size) try {
929
- // split tensors must always be set in their entirety at once
930
- GGML_ASSERT(offset == 0);
931
- GGML_ASSERT(size == ggml_nbytes(tensor));
932
-
933
- ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
934
- ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
935
-
936
- const int64_t ne0 = tensor->ne[0];
937
- const size_t nb1 = tensor->nb[1];
938
- ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
939
-
940
- for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
941
- int64_t row_low, row_high;
942
- get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
943
-
944
- int64_t nrows_split = row_high - row_low;
945
- if (nrows_split == 0) {
946
- continue;
947
- }
948
-
949
- const size_t offset_split = row_low*nb1;
950
- size_t size = ggml_nbytes_split(tensor, nrows_split);
951
- const size_t original_size = size;
952
-
953
- // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
954
- if (ne0 % MATRIX_ROW_PADDING != 0) {
955
- size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
956
- }
957
-
958
- char * buf_host = (char *)data + offset_split;
959
- /*
960
- DPCT1009:212: SYCL uses exceptions to report errors and does not use the
961
- error codes. The original code was commented out and a warning string
962
- was inserted. You need to rewrite this code.
963
- */
964
- ggml_sycl_set_device(i);
965
- const queue_ptr stream = ctx->streams[i];
966
- SYCL_CHECK(CHECK_TRY_ERROR(
967
- (*stream)
968
- .memcpy(buf_host, extra->data_device[i], original_size)
969
- .wait()));
970
- }
971
- }
972
- catch (sycl::exception const &exc) {
973
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
974
- << ", line:" << __LINE__ << std::endl;
975
- std::exit(1);
976
- }
977
-
978
- static void ggml_backend_sycl_split_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
979
- GGML_UNUSED(buffer);
980
- GGML_UNUSED(value);
981
- }
982
-
983
- static struct ggml_backend_buffer_i ggml_backend_sycl_split_buffer_interface = {
984
- /* .free_buffer = */ ggml_backend_sycl_split_buffer_free_buffer,
985
- /* .get_base = */ ggml_backend_sycl_split_buffer_get_base,
986
- /* .init_tensor = */ ggml_backend_sycl_split_buffer_init_tensor,
987
- /* .memset_tensor = */ NULL,
988
- /* .set_tensor = */ ggml_backend_sycl_split_buffer_set_tensor,
989
- /* .get_tensor = */ ggml_backend_sycl_split_buffer_get_tensor,
990
- /* .cpy_tensor = */ NULL,
991
- /* .clear = */ ggml_backend_sycl_split_buffer_clear,
992
- /* .reset = */ NULL,
993
- };
994
-
995
- // sycl split buffer type
996
-
997
- static const char * ggml_backend_sycl_split_buffer_type_get_name(ggml_backend_buffer_type_t buft) {
998
- return GGML_SYCL_NAME "_Split";
999
-
1000
- GGML_UNUSED(buft);
1001
- }
1002
-
1003
- static bool ggml_backend_buffer_is_sycl_split(ggml_backend_buffer_t buffer) {
1004
- return buffer->buft->iface.get_name == ggml_backend_sycl_split_buffer_type_get_name;
1005
- }
1006
-
1007
- static ggml_backend_buffer_t ggml_backend_sycl_split_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
1008
- // since we don't know the exact split after rounding, we cannot allocate the device buffers at this point
1009
- // instead, we allocate them for each tensor separately in init_tensor
1010
- // however, the size still represents the maximum cumulative size of all the device buffers after the tensors are allocated,
1011
- // as returned by get_alloc_size. this limit is enforced during tensor allocation by ggml-alloc, so it must be correct.
1012
- ggml_backend_sycl_split_buffer_context * ctx = new ggml_backend_sycl_split_buffer_context();
1013
-
1014
- return ggml_backend_buffer_init(buft, ggml_backend_sycl_split_buffer_interface, ctx, size);
1015
- }
1016
-
1017
- static size_t ggml_backend_sycl_split_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
1018
- return 128;
1019
- GGML_UNUSED(buft);
1020
- }
1021
-
1022
- static size_t ggml_backend_sycl_split_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
1023
- ggml_backend_sycl_split_buffer_type_context * ctx = (ggml_backend_sycl_split_buffer_type_context *)buft->context;
1024
-
1025
- size_t total_size = 0;
1026
-
1027
- const int64_t ne0 = tensor->ne[0];
1028
-
1029
- for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
1030
- int64_t row_low, row_high;
1031
- get_row_split(&row_low, &row_high, tensor, ctx->tensor_split, i);
1032
-
1033
- int64_t nrows_split = row_high - row_low;
1034
- if (nrows_split == 0) {
1035
- continue;
1036
- }
1037
-
1038
- total_size += ggml_nbytes_split(tensor, nrows_split);
1039
-
1040
- // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
1041
- if (ne0 % MATRIX_ROW_PADDING != 0) {
1042
- total_size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
1043
- }
1044
- }
1045
-
1046
- return total_size;
1047
- }
1048
-
1049
- static bool ggml_backend_sycl_split_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
1050
- return false;
1051
-
1052
- GGML_UNUSED(buft);
1053
- }
1054
-
1055
- static ggml_backend_buffer_type_i ggml_backend_sycl_split_buffer_type_interface = {
1056
- /* .get_name = */ ggml_backend_sycl_split_buffer_type_get_name,
1057
- /* .alloc_buffer = */ ggml_backend_sycl_split_buffer_type_alloc_buffer,
1058
- /* .get_alignment = */ ggml_backend_sycl_split_buffer_type_get_alignment,
1059
- /* .get_max_size = */ NULL, // defaults to SIZE_MAX
1060
- /* .get_alloc_size = */ ggml_backend_sycl_split_buffer_type_get_alloc_size,
1061
- /* .is_host = */ ggml_backend_sycl_split_buffer_type_is_host,
1062
- };
1063
-
1064
- ggml_backend_buffer_type_t ggml_backend_sycl_split_buffer_type(const float * tensor_split) {
1065
- static std::mutex mutex;
1066
- std::lock_guard<std::mutex> lock(mutex);
1067
-
1068
- GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_split_buffer_type\n");
1069
- ggml_check_sycl();
1070
- // FIXME: this is not thread safe
1071
- static std::map<std::array<float, GGML_SYCL_MAX_DEVICES>, struct ggml_backend_buffer_type> buft_map;
1072
-
1073
- std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split_arr = {};
1074
-
1075
- bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + GGML_SYCL_MAX_DEVICES, [](float x) { return x == 0.0f; });
1076
- if (all_zero) {
1077
- tensor_split_arr = ggml_sycl_info().default_tensor_split;
1078
- } else {
1079
- float split_sum = 0.0f;
1080
- for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
1081
- tensor_split_arr[i] = split_sum;
1082
- split_sum += tensor_split[i];
1083
- }
1084
- for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
1085
- tensor_split_arr[i] /= split_sum;
1086
- }
1087
- }
1088
-
1089
- auto it = buft_map.find(tensor_split_arr);
1090
- if (it != buft_map.end()) {
1091
- return &it->second;
1092
- }
1093
-
1094
- struct ggml_backend_buffer_type buft {
1095
- /* .iface = */ ggml_backend_sycl_split_buffer_type_interface,
1096
- /* .device = */ ggml_backend_reg_dev_get(ggml_backend_sycl_reg(), 0),
1097
- /* .context = */ new ggml_backend_sycl_split_buffer_type_context{tensor_split_arr},
1098
- };
1099
-
1100
- auto result = buft_map.emplace(tensor_split_arr, buft);
1101
- return &result.first->second;
1102
- }
1103
-
1104
- // host buffer type
1105
-
1106
- static const char * ggml_backend_sycl_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
1107
- return GGML_SYCL_NAME "_Host";
1108
-
1109
- GGML_UNUSED(buft);
1110
- }
1111
-
1112
- static void ggml_backend_sycl_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
1113
- ggml_sycl_host_free(buffer->context);
1114
- }
1115
-
1116
- static ggml_backend_buffer_t ggml_backend_sycl_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
1117
- void * ptr = ggml_sycl_host_malloc(size);
1118
-
1119
- if (ptr == nullptr) {
1120
- // fallback to cpu buffer
1121
- return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
1122
- }
1123
-
1124
- // FIXME: this is a hack to avoid having to implement a new buffer type
1125
- ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
1126
- buffer->buft = buft;
1127
- buffer->iface.free_buffer = ggml_backend_sycl_host_buffer_free_buffer;
1128
-
1129
- return buffer;
1130
- }
1131
-
1132
- ggml_backend_buffer_type_t ggml_backend_sycl_host_buffer_type() {
1133
- GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_host_buffer_type\n");
1134
- static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_type_host = {
1135
- /* .iface = */ {
1136
- /* .get_name = */ ggml_backend_sycl_host_buffer_type_name,
1137
- /* .alloc_buffer = */ ggml_backend_sycl_host_buffer_type_alloc_buffer,
1138
- /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
1139
- /* .get_max_size = */ NULL, // TODO: return device.maxBufferLength
1140
- /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
1141
- /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
1142
- },
1143
- /* .device = */ ggml_backend_reg_dev_get(ggml_backend_sycl_reg(), 0),
1144
- /* .context = */ nullptr,
1145
- };
1146
-
1147
- return &ggml_backend_sycl_buffer_type_host;
1148
- }
1149
-
1150
- // buffer pool for sycl (legacy)
1151
- struct ggml_sycl_pool_leg : public ggml_sycl_pool {
1152
- static const int MAX_SYCL_BUFFERS = 256;
1153
-
1154
- int device;
1155
- queue_ptr qptr;
1156
- struct ggml_sycl_buffer {
1157
- void * ptr = nullptr;
1158
- size_t size = 0;
1159
- };
1160
-
1161
- ggml_sycl_buffer buffer_pool[MAX_SYCL_BUFFERS] = {};
1162
- size_t pool_size = 0;
1163
-
1164
- explicit ggml_sycl_pool_leg(queue_ptr qptr_, int device_) : device(device_), qptr(qptr_) {}
1165
-
1166
- ~ggml_sycl_pool_leg() {
1167
- for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
1168
- ggml_sycl_buffer & b = buffer_pool[i];
1169
- if (b.ptr != nullptr) {
1170
- SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(b.ptr, *qptr)));
1171
- pool_size -= b.size;
1172
- }
1173
- }
1174
- GGML_ASSERT(pool_size == 0);
1175
- }
1176
-
1177
- void * alloc(size_t size, size_t * actual_size) override {
1178
- #ifdef DEBUG_sycl_MALLOC
1179
- int nnz = 0;
1180
- size_t max_size = 0;
1181
- #endif
1182
- size_t best_diff = 1ull << 36;
1183
- int ibest = -1;
1184
- for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
1185
- ggml_sycl_buffer& b = buffer_pool[i];
1186
- if (b.ptr != nullptr) {
1187
- #ifdef DEBUG_sycl_MALLOC
1188
- ++nnz;
1189
- if (b.size > max_size) max_size = b.size;
1190
- #endif
1191
- if (b.size >= size) {
1192
- size_t diff = b.size - size;
1193
- if (diff < best_diff) {
1194
- best_diff = diff;
1195
- ibest = i;
1196
- if (!best_diff) {
1197
- void * ptr = b.ptr;
1198
- *actual_size = b.size;
1199
- b.ptr = nullptr;
1200
- b.size = 0;
1201
- return ptr;
1202
- }
1203
- }
1204
- }
1205
- }
1206
- }
1207
- if (ibest >= 0) {
1208
- ggml_sycl_buffer& b = buffer_pool[ibest];
1209
- void * ptr = b.ptr;
1210
- *actual_size = b.size;
1211
- b.ptr = nullptr;
1212
- b.size = 0;
1213
- return ptr;
1214
- }
1215
- void * ptr;
1216
- size_t look_ahead_size = (size_t) (1.05 * size);
1217
-
1218
- SYCL_CHECK(
1219
- CHECK_TRY_ERROR(ptr = (void *)sycl::malloc_device(
1220
- look_ahead_size, *qptr)));
1221
- if (!ptr) {
1222
- GGML_LOG_ERROR("%s: can't allocate %lu Bytes of memory on device/GPU\n", __func__, look_ahead_size);
1223
- return nullptr;
1224
- }
1225
-
1226
- *actual_size = look_ahead_size;
1227
- pool_size += look_ahead_size;
1228
-
1229
- #ifdef DEBUG_SYCL_MALLOC
1230
- GGML_LOG_DEBUG("%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, id, nnz,
1231
- (uint32_t)(max_size/1024/1024), (uint32_t)(g_sycl_pool_size[id]/1024/1024), (uint32_t)(size/1024/1024));
1232
- #endif
1233
-
1234
- // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg look_ahead_size=%lu, return %p\n", look_ahead_size, ptr);
1235
- return ptr;
1236
- }
1237
-
1238
- void free(void * ptr, size_t size) override {
1239
- for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
1240
- ggml_sycl_buffer& b = buffer_pool[i];
1241
- if (b.ptr == nullptr) {
1242
- b.ptr = ptr;
1243
- b.size = size;
1244
- return;
1245
- }
1246
- }
1247
- GGML_LOG_WARN("WARNING: sycl buffer pool full, increase MAX_sycl_BUFFERS\n");
1248
- SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, *qptr)));
1249
- pool_size -= size;
1250
- }
1251
- };
1252
-
1253
- struct ggml_sycl_pool_host : public ggml_sycl_pool {
1254
- queue_ptr qptr;
1255
- int device;
1256
-
1257
- inline static int counter{ 0 };
1258
-
1259
- struct ggml_sycl_buffer {
1260
- void * ptr = nullptr;
1261
- size_t size = 0;
1262
- };
1263
-
1264
- // Set arbitrarly to 64
1265
- static constexpr int MAX_POOL_SIZE{ 64 };
1266
- std::vector<ggml_sycl_buffer> buffer_pool = std::vector<ggml_sycl_buffer>(MAX_POOL_SIZE);
1267
- size_t pool_size = 0;
1268
-
1269
- explicit ggml_sycl_pool_host(queue_ptr qptr_, int device_) : qptr(qptr_), device(device_) {}
1270
-
1271
- ~ggml_sycl_pool_host() {
1272
- for (int i = 0; i < MAX_POOL_SIZE; ++i) {
1273
- ggml_sycl_buffer & b = buffer_pool[i];
1274
- if (b.ptr != nullptr) {
1275
- SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(b.ptr, *qptr)));
1276
- b.ptr = nullptr;
1277
- pool_size -= b.size;
1278
- b.size = 0;
1279
- }
1280
- }
1281
- counter = 0;
1282
- }
1283
-
1284
- void * alloc(size_t size, size_t * actual_size) override {
1285
- if (counter == MAX_POOL_SIZE) {
1286
- ggml_sycl_buffer b = buffer_pool[0];
1287
- void * ptr = b.ptr;
1288
- *actual_size = b.size;
1289
- counter = 1;
1290
- return ptr;
1291
- }
1292
- ggml_sycl_buffer & b = buffer_pool[counter];
1293
-
1294
- if (b.ptr == nullptr) {
1295
- void * ptr;
1296
-
1297
- SYCL_CHECK(CHECK_TRY_ERROR(ptr = (void *) sycl::malloc_host(size, *qptr)));
1298
- if (!ptr) {
1299
- GGML_LOG_ERROR("%s: can't allocate %lu Bytes of memory on host\n", __func__, size);
1300
- return nullptr;
1301
- }
1302
- pool_size += size;
1303
- *actual_size = size;
1304
- counter = counter + 1;
1305
- return ptr;
1306
- } else {
1307
- ++counter;
1308
- b.size = size;
1309
- return b.ptr;
1310
- }
1311
- }
1312
-
1313
- void free(void * ptr, size_t size) override {
1314
- // if the pool is not completed add the pointer to it in place of the first nullptr found.
1315
- // Otherwise do nothing, pointers will be freed once the pool is deallocated.
1316
- for (int i = 0; i < MAX_POOL_SIZE; ++i) {
1317
- ggml_sycl_buffer & b = buffer_pool[i];
1318
- if (b.ptr == nullptr) {
1319
- b.ptr = ptr;
1320
- b.size = size;
1321
- return;
1322
- }
1323
- }
1324
- }
1325
- };
1326
-
1327
- std::unique_ptr<ggml_sycl_pool> ggml_backend_sycl_context::new_pool_for_host(queue_ptr qptr, int device) {
1328
- // return pool for the host to speed up memory management
1329
- return std::unique_ptr<ggml_sycl_pool>(new ggml_sycl_pool_host(qptr, device));
1330
- }
1331
-
1332
- std::unique_ptr<ggml_sycl_pool> ggml_backend_sycl_context::new_pool_for_device(queue_ptr qptr, int device) {
1333
- // TBD: NO VMM support
1334
- // if (ggml_sycl_info().devices[device].vmm) {
1335
- // return std::unique_ptr<ggml_sycl_pool>(new ggml_sycl_pool_vmm(device));
1336
- // }
1337
- return std::unique_ptr<ggml_sycl_pool>(new ggml_sycl_pool_leg(qptr, device));
1338
- }
1339
-
1340
- // TBD pool with virtual memory management
1341
- // struct ggml_sycl_pool_vmm : public ggml_sycl_pool
1342
-
1343
- /// kernels
1344
- typedef void (*ggml_sycl_op_mul_mat_t)(
1345
- ggml_backend_sycl_context & ctx,
1346
- const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
1347
- const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
1348
- float *dst_dd_i, const int64_t row_low, const int64_t row_high,
1349
- const int64_t src1_ncols, const int64_t src1_padded_row_size,
1350
- const queue_ptr &stream);
1351
-
1352
-
1353
-
1354
- template<int QUANT_BLOCK_TILE>
1355
- static void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded,
1356
- const sycl::nd_item<3> &item_ct1) {
1357
- const int ix = (item_ct1.get_local_range(2) * item_ct1.get_group(2) +
1358
- item_ct1.get_local_id(2)) * QUANT_BLOCK_TILE;
1359
-
1360
- if (ix >= kx_padded) {
1361
- return;
1362
- }
1363
-
1364
- const int iy = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
1365
- item_ct1.get_local_id(1);
1366
-
1367
- const int i_padded = iy*kx_padded + ix;
1368
-
1369
- block_q8_1 * y = (block_q8_1 *) vy;
1370
-
1371
- const int ib = i_padded / QK8_1; // block index
1372
- const int iqs = i_padded % QK8_1; // quant index
1373
- typedef sycl::vec<float, QUANT_BLOCK_TILE> TC;
1374
- typedef sycl::vec<int8_t, QUANT_BLOCK_TILE> TQ;
1375
- TC zeros;
1376
- TQ qzeros;
1377
- #pragma unroll
1378
- for (int i = 0; i < QUANT_BLOCK_TILE; i++)
1379
- {
1380
- zeros[i] = 0.f;
1381
- qzeros[i] = 0;
1382
- }
1383
- const TC xi = ix < kx ? *(const TC *)&x[iy * kx + ix] : zeros;
1384
- float sum = xi[0];
1385
- float amax = sycl::fabs(xi[0]);
1386
- #pragma unroll
1387
- for (int i = 1; i < QUANT_BLOCK_TILE; i++)
1388
- {
1389
- sum += xi[i];
1390
- amax = sycl::fmax(sycl::fabs(xi[i]), amax);
1391
- }
1392
- sum = warp_reduce_sum(sum, item_ct1);
1393
- amax = warp_reduce_max(amax, item_ct1);
1394
-
1395
- const float d = amax / 127;
1396
- TQ q = qzeros;
1397
- if (amax != 0.0f)
1398
- {
1399
- #pragma unroll
1400
- for (int i = 0; i < QUANT_BLOCK_TILE; i++) {
1401
- q[i] = sycl::round(xi[i] / d);
1402
- }
1403
- }
1404
-
1405
- *(TQ *)&y[ib].qs[iqs] = q;
1406
-
1407
- if (iqs > 0) {
1408
- return;
1409
- }
1410
-
1411
- reinterpret_cast<sycl::half &>(y[ib].ds.x()) = d;
1412
- reinterpret_cast<sycl::half &>(y[ib].ds.y()) = sum;
1413
- }
1414
-
1415
- static void mul_mat_p021_f16_f32(
1416
- const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
1417
- const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y,
1418
- const sycl::nd_item<3> &item_ct1) {
1419
-
1420
- const sycl::half *x = (const sycl::half *)vx;
1421
-
1422
- const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
1423
- item_ct1.get_local_id(1);
1424
- const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
1425
- item_ct1.get_local_id(0);
1426
- const int channel_x = channel / (nchannels_y / nchannels_x);
1427
-
1428
- const int nrows_y = ncols_x;
1429
- const int nrows_dst = nrows_x;
1430
- const int row_dst = row_x;
1431
-
1432
- float tmp = 0.0f;
1433
-
1434
- for (int col_x0 = 0; col_x0 < ncols_x;
1435
- col_x0 += item_ct1.get_local_range(2)) {
1436
- const int col_x = col_x0 + item_ct1.get_local_id(2);
1437
-
1438
- if (col_x >= ncols_x) {
1439
- break;
1440
- }
1441
-
1442
- // x is transposed and permuted
1443
- const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
1444
- const float xi =
1445
- sycl::vec<sycl::half, 1>(x[ix])
1446
- .convert<float, sycl::rounding_mode::automatic>()[0];
1447
-
1448
- const int row_y = col_x;
1449
-
1450
-
1451
- // y is not transposed but permuted
1452
- const int iy = channel*nrows_y + row_y;
1453
-
1454
- tmp += xi * y[iy];
1455
- }
1456
-
1457
- // dst is not transposed and not permuted
1458
- const int idst = channel*nrows_dst + row_dst;
1459
-
1460
- // sum up partial sums and write back result
1461
- #pragma unroll
1462
- for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
1463
- tmp +=
1464
- dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
1465
- }
1466
-
1467
- if (item_ct1.get_local_id(2) == 0) {
1468
- dst[idst] = tmp;
1469
- }
1470
- }
1471
-
1472
- static void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
1473
- const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
1474
- const int row_stride_x, const int channel_stride_x, const int channel_x_divisor,
1475
- const sycl::nd_item<3> &item_ct1) {
1476
-
1477
- const sycl::half *x = (const sycl::half *)vx;
1478
-
1479
- const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
1480
- item_ct1.get_local_id(1);
1481
- const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
1482
- item_ct1.get_local_id(0);
1483
- const int channel_x = channel / channel_x_divisor;
1484
-
1485
- const int nrows_y = ncols_x;
1486
- const int nrows_dst = nrows_x;
1487
- const int row_dst = row_x;
1488
-
1489
- const int idst = channel*nrows_dst + row_dst;
1490
-
1491
- float tmp = 0.0f;
1492
-
1493
- for (int col_x0 = 0; col_x0 < ncols_x;
1494
- col_x0 += item_ct1.get_local_range(2)) {
1495
- const int col_x = col_x0 + item_ct1.get_local_id(2);
1496
-
1497
- if (col_x >= ncols_x) {
1498
- break;
1499
- }
1500
-
1501
- const int row_y = col_x;
1502
-
1503
- const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
1504
- const int iy = channel*nrows_y + row_y;
1505
-
1506
- const float xi =
1507
- sycl::vec<sycl::half, 1>(x[ix])
1508
- .convert<float, sycl::rounding_mode::automatic>()[0];
1509
-
1510
- tmp += xi * y[iy];
1511
- }
1512
-
1513
- // sum up partial sums and write back result
1514
- #pragma unroll
1515
- for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
1516
- tmp +=
1517
- dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
1518
- }
1519
-
1520
- if (item_ct1.get_local_id(2) == 0) {
1521
- dst[idst] = tmp;
1522
- }
1523
- }
1524
-
1525
- static void k_sum_rows_f32(const float * x, float * dst, const int ncols,
1526
- const sycl::nd_item<3> &item_ct1) {
1527
- const int row = item_ct1.get_group(1);
1528
- const int col = item_ct1.get_local_id(2);
1529
-
1530
- float sum = 0.0f;
1531
- for (int i = col; i < ncols; i += item_ct1.get_local_range(2)) {
1532
- sum += x[row * ncols + i];
1533
- }
1534
-
1535
- sum = warp_reduce_sum(sum, item_ct1);
1536
-
1537
- if (col == 0) {
1538
- dst[row] = sum;
1539
- }
1540
- }
1541
-
1542
-
1543
- template<typename T>
1544
- static inline void ggml_sycl_swap(T & a, T & b) {
1545
- T tmp = a;
1546
- a = b;
1547
- b = tmp;
1548
- }
1549
-
1550
- template <ggml_sort_order order>
1551
- __dpct_inline__ static void
1552
- k_argsort_f32_i32(const float *x, int *dst, const int ncols, int ncols_pad,
1553
- const sycl::nd_item<3> &item_ct1, uint8_t *dpct_local) {
1554
- // bitonic sort
1555
- int col = item_ct1.get_local_id(2);
1556
- int row = item_ct1.get_group(1);
1557
-
1558
- if (col >= ncols_pad) {
1559
- return;
1560
- }
1561
-
1562
- const float * x_row = x + row * ncols;
1563
- auto dst_row = (int *)dpct_local;
1564
-
1565
- // initialize indices
1566
- dst_row[col] = col;
1567
-
1568
- item_ct1.barrier(sycl::access::fence_space::local_space);
1569
-
1570
- for (int k = 2; k <= ncols_pad; k *= 2) {
1571
- for (int j = k / 2; j > 0; j /= 2) {
1572
- int ixj = col ^ j;
1573
- if (ixj > col) {
1574
- if ((col & k) == 0) {
1575
- if (dst_row[col] >= ncols ||
1576
- (dst_row[ixj] < ncols && (order == GGML_SORT_ORDER_ASC ?
1577
- x_row[dst_row[col]] > x_row[dst_row[ixj]] :
1578
- x_row[dst_row[col]] < x_row[dst_row[ixj]]))
1579
- ) {
1580
- ggml_sycl_swap(dst_row[col], dst_row[ixj]);
1581
- }
1582
- } else {
1583
- if (dst_row[ixj] >= ncols ||
1584
- (dst_row[col] < ncols && (order == GGML_SORT_ORDER_ASC ?
1585
- x_row[dst_row[col]] < x_row[dst_row[ixj]] :
1586
- x_row[dst_row[col]] > x_row[dst_row[ixj]]))
1587
- ) {
1588
- ggml_sycl_swap(dst_row[col], dst_row[ixj]);
1589
- }
1590
- }
1591
- }
1592
- /*
1593
- DPCT1118:1: SYCL group functions and algorithms must be encountered
1594
- in converged control flow. You may need to adjust the code.
1595
- */
1596
- item_ct1.barrier(sycl::access::fence_space::local_space);
1597
- }
1598
- }
1599
-
1600
- // copy the result to dst without the padding
1601
- if (col < ncols) {
1602
- dst[row * ncols + col] = dst_row[col];
1603
- }
1604
- }
1605
-
1606
-
1607
- static void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past,
1608
- const sycl::nd_item<3> &item_ct1) {
1609
- const int col = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
1610
- item_ct1.get_local_id(1);
1611
- const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
1612
- item_ct1.get_local_id(2);
1613
-
1614
- if (col >= ncols) {
1615
- return;
1616
- }
1617
-
1618
- const int i = row*ncols + col;
1619
- //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
1620
- //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
1621
- dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
1622
- }
1623
-
1624
- static void scale_f32(const float * x, float * dst, const float scale, const int k,
1625
- const sycl::nd_item<3> &item_ct1) {
1626
- const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
1627
- item_ct1.get_local_id(2);
1628
-
1629
- if (i >= k) {
1630
- return;
1631
- }
1632
-
1633
- dst[i] = scale * x[i];
1634
- }
1635
-
1636
-
1637
- template <typename Ti, typename To>
1638
- static void pool2d_nchw_kernel(
1639
- const int ih, const int iw, const int oh, const int ow,
1640
- const int kh, const int kw, const int sh, const int sw,
1641
- const int ph, const int pw, const int parallel_elements,
1642
- const Ti* src, To* dst, const enum ggml_op_pool op,
1643
- const sycl::nd_item<3> &item_ct1) {
1644
- int idx = item_ct1.get_local_id(2) +
1645
- item_ct1.get_group(2) * item_ct1.get_local_range(2);
1646
- if (idx >= parallel_elements) {
1647
- return;
1648
- }
1649
-
1650
- const int I_HW = ih * iw;
1651
- const int O_HW = oh * ow;
1652
- const int nc = idx / O_HW;
1653
- const int cur_oh = idx % O_HW / ow;
1654
- const int cur_ow = idx % O_HW % ow;
1655
- const Ti* i_ptr = src + nc * I_HW;
1656
- To* o_ptr = dst + nc * O_HW;
1657
- const int start_h = cur_oh * sh - ph;
1658
- const int bh = sycl::max(0, start_h);
1659
- const int eh = sycl::min(ih, start_h + kh);
1660
- const int start_w = cur_ow * sw - pw;
1661
- const int bw = sycl::max(0, start_w);
1662
- const int ew = sycl::min(iw, start_w + kw);
1663
-
1664
- To res = 0;
1665
-
1666
- switch (op) {
1667
- case GGML_OP_POOL_AVG: res = 0; break;
1668
- case GGML_OP_POOL_MAX: res = -FLT_MAX; break;
1669
- default:
1670
- res = (To) sycl::nan(uint32_t(0));
1671
- break;
1672
- }
1673
-
1674
- for (int i = bh; i < eh; i += 1) {
1675
- for (int j = bw; j < ew; j += 1) {
1676
- #if DPCT_COMPATIBILITY_TEMP >= 350
1677
- /*
1678
- DPCT1098:106: The '*' expression is used instead of the __ldg
1679
- call. These two expressions do not provide the exact same
1680
- functionality. Check the generated code for potential precision
1681
- and/or performance issues.
1682
- */
1683
- Ti cur = *(i_ptr + i * iw + j);
1684
- #else
1685
- Ti cur = i_ptr[i * iw + j];
1686
- #endif
1687
- switch (op) {
1688
- case GGML_OP_POOL_AVG: res += (cur / (kh * kw)); break;
1689
- case GGML_OP_POOL_MAX: res = sycl::max(res, (To)cur); break;
1690
- default:
1691
- res = (To) sycl::nan(uint32_t(0));
1692
- break;
1693
- }
1694
- }
1695
- }
1696
- o_ptr[cur_oh * ow + cur_ow] = res;
1697
- }
1698
-
1699
- static void quantize_row_q8_1_sycl(const float *x, void *vy, const int kx,
1700
- const int ky, const int kx_padded,
1701
- queue_ptr stream) {
1702
- const int block_num_x = (kx_padded + SYCL_QUANTIZE_BLOCK_SIZE - 1) / SYCL_QUANTIZE_BLOCK_SIZE;
1703
- const sycl::range<3> num_blocks(1, ky, block_num_x);
1704
- int constexpr QUANT_BLOCK_TILE = QK8_1 / WARP_SIZE;
1705
- static_assert(QK8_1 % WARP_SIZE == 0);
1706
- const sycl::range<3> block_size(1, 1, SYCL_QUANTIZE_BLOCK_SIZE / QUANT_BLOCK_TILE);
1707
- {
1708
- dpct::has_capability_or_fail(stream->get_device(),
1709
- {sycl::aspect::fp16});
1710
-
1711
- stream->parallel_for(
1712
- sycl::nd_range<3>(num_blocks * block_size, block_size),
1713
- [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1714
- quantize_q8_1<QUANT_BLOCK_TILE>(x, vy, kx, kx_padded, item_ct1);
1715
- });
1716
- }
1717
- }
1718
-
1719
- static void ggml_mul_mat_p021_f16_f32_sycl(const void *vx, const float *y,
1720
- float *dst, const int ncols_x,
1721
- const int nrows_x,
1722
- const int nchannels_x,
1723
- const int nchannels_y,
1724
- queue_ptr stream) {
1725
-
1726
- const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
1727
- const sycl::range<3> block_dims(1, 1, WARP_SIZE);
1728
- {
1729
- dpct::has_capability_or_fail(stream->get_device(),
1730
- {sycl::aspect::fp16});
1731
-
1732
- stream->parallel_for(
1733
- sycl::nd_range<3>(block_nums * block_dims, block_dims),
1734
- [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1735
- mul_mat_p021_f16_f32(vx, y, dst, ncols_x, nrows_x, nchannels_x,
1736
- nchannels_y, item_ct1);
1737
- });
1738
- }
1739
- }
1740
-
1741
- static void ggml_mul_mat_vec_nc_f16_f32_sycl(
1742
- const void *vx, const float *y, float *dst, const int ncols_x,
1743
- const int nrows_x, const int row_stride_x, const int nchannels_x,
1744
- const int nchannels_y, const int channel_stride_x, queue_ptr stream) {
1745
-
1746
- const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
1747
- const sycl::range<3> block_dims(1, 1, WARP_SIZE);
1748
- {
1749
- dpct::has_capability_or_fail(stream->get_device(),
1750
- {sycl::aspect::fp16});
1751
-
1752
- stream->parallel_for(
1753
- sycl::nd_range<3>(block_nums * block_dims, block_dims),
1754
- [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1755
- mul_mat_vec_nc_f16_f32(vx, y, dst, ncols_x, nrows_x,
1756
- row_stride_x, channel_stride_x,
1757
- nchannels_y / nchannels_x, item_ct1);
1758
- });
1759
- }
1760
- }
1761
-
1762
-
1763
-
1764
- static void scale_f32_sycl(const float *x, float *dst, const float scale,
1765
- const int k, queue_ptr stream) {
1766
- const int num_blocks = (k + SYCL_SCALE_BLOCK_SIZE - 1) / SYCL_SCALE_BLOCK_SIZE;
1767
- stream->parallel_for(
1768
- sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
1769
- sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE),
1770
- sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE)),
1771
- [=](sycl::nd_item<3> item_ct1) {
1772
- scale_f32(x, dst, scale, k, item_ct1);
1773
- });
1774
- }
1775
-
1776
-
1777
- static void sum_rows_f32_sycl(const float *x, float *dst, const int ncols,
1778
- const int nrows, queue_ptr stream) {
1779
- const sycl::range<3> block_dims(1, 1, WARP_SIZE);
1780
- const sycl::range<3> block_nums(1, nrows, 1);
1781
- stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
1782
- [=](sycl::nd_item<3> item_ct1)
1783
- [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1784
- k_sum_rows_f32(x, dst, ncols, item_ct1);
1785
- });
1786
- }
1787
-
1788
- static int next_power_of_2(int x) {
1789
- int n = 1;
1790
- while (n < x) {
1791
- n *= 2;
1792
- }
1793
- return n;
1794
- }
1795
-
1796
- static void argsort_f32_i32_sycl(const float *x, int *dst, const int ncols,
1797
- const int nrows, ggml_sort_order order,
1798
- queue_ptr stream) {
1799
- // bitonic sort requires ncols to be power of 2
1800
- const int ncols_pad = next_power_of_2(ncols);
1801
-
1802
- const sycl::range<3> block_dims(1, 1, ncols_pad);
1803
- const sycl::range<3> block_nums(1, nrows, 1);
1804
- const size_t shared_mem = ncols_pad * sizeof(int);
1805
-
1806
- if (order == GGML_SORT_ORDER_ASC) {
1807
- stream->submit([&](sycl::handler &cgh) {
1808
- sycl::local_accessor<uint8_t, 1> dpct_local_acc_ct1(
1809
- sycl::range<1>(shared_mem), cgh);
1810
-
1811
- cgh.parallel_for(
1812
- sycl::nd_range<3>(block_nums * block_dims, block_dims),
1813
- [=](sycl::nd_item<3> item_ct1) {
1814
- k_argsort_f32_i32<GGML_SORT_ORDER_ASC>(
1815
- x, dst, ncols, ncols_pad, item_ct1,
1816
- dpct_local_acc_ct1.get_multi_ptr<sycl::access::decorated::no>()
1817
- .get());
1818
- });
1819
- });
1820
- } else if (order == GGML_SORT_ORDER_DESC) {
1821
- stream->submit([&](sycl::handler &cgh) {
1822
- sycl::local_accessor<uint8_t, 1> dpct_local_acc_ct1(
1823
- sycl::range<1>(shared_mem), cgh);
1824
-
1825
- cgh.parallel_for(
1826
- sycl::nd_range<3>(block_nums * block_dims, block_dims),
1827
- [=](sycl::nd_item<3> item_ct1) {
1828
- k_argsort_f32_i32<GGML_SORT_ORDER_DESC>(
1829
- x, dst, ncols, ncols_pad, item_ct1,
1830
- dpct_local_acc_ct1.get_multi_ptr<sycl::access::decorated::no>()
1831
- .get());
1832
- });
1833
- });
1834
- } else {
1835
- GGML_ABORT("fatal error");
1836
- }
1837
- }
1838
-
1839
- static void argmax_f32_i32_sycl(const float *x, int *dst, const int ncols,
1840
- const int nrows, queue_ptr stream) {
1841
- const sycl::range<3> block_dims(1, 1, SYCL_ARGMAX_BLOCK_SIZE);
1842
- const sycl::range<3> block_nums(1, nrows, 1);
1843
- const size_t shared_mem = 256 * sizeof(float);
1844
-
1845
- stream->submit([&](sycl::handler &cgh) {
1846
- sycl::local_accessor<float, 1> shared_data(
1847
- sycl::range<1>(shared_mem/sizeof(float)), cgh);
1848
- sycl::local_accessor<int, 1> shared_indices(
1849
- sycl::range<1>(shared_mem/sizeof(float)), cgh);
1850
-
1851
- cgh.parallel_for(
1852
- sycl::nd_range<3>(block_nums * block_dims, block_dims),
1853
- [=](sycl::nd_item<3> item_ct1) {
1854
- const int tid = item_ct1.get_local_id(2);
1855
- const int row = item_ct1.get_global_id(1);
1856
-
1857
- float max_val = -INFINITY;
1858
- int max_idx = -1;
1859
-
1860
- for (int col = tid; col < ncols; col += 256) {
1861
- float val = x[row * ncols + col];
1862
- if (val > max_val) {
1863
- max_val = val;
1864
- max_idx = col;
1865
- }
1866
- }
1867
-
1868
- shared_data[tid] = max_val;
1869
- shared_indices[tid] = max_idx;
1870
- item_ct1.barrier(sycl::access::fence_space::local_space);
1871
-
1872
- for (int stride = 256/2; stride > 0; stride >>= 1) {
1873
- if (tid < stride) {
1874
- float val1 = shared_data[tid];
1875
- float val2 = shared_data[tid + stride];
1876
- if (val2 > val1) {
1877
- shared_data[tid] = val2;
1878
- shared_indices[tid] = shared_indices[tid + stride];
1879
- }
1880
- }
1881
- item_ct1.barrier(sycl::access::fence_space::local_space);
1882
- }
1883
-
1884
-
1885
- if (tid == 0) {
1886
- dst[row] = shared_indices[0];
1887
- }
1888
- });
1889
- });
1890
- }
1891
- static void diag_mask_inf_f32_sycl(const float *x, float *dst,
1892
- const int ncols_x, const int nrows_x,
1893
- const int rows_per_channel, const int n_past,
1894
- queue_ptr stream) {
1895
- const sycl::range<3> block_dims(1, SYCL_DIAG_MASK_INF_BLOCK_SIZE, 1);
1896
- const int block_num_x = (ncols_x + SYCL_DIAG_MASK_INF_BLOCK_SIZE - 1) / SYCL_DIAG_MASK_INF_BLOCK_SIZE;
1897
- const sycl::range<3> block_nums(1, block_num_x, nrows_x);
1898
- stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
1899
- [=](sycl::nd_item<3> item_ct1) {
1900
- diag_mask_inf_f32(x, dst, ncols_x,
1901
- rows_per_channel, n_past,
1902
- item_ct1);
1903
- });
1904
- }
1905
-
1906
- static dpct::err0 ggml_sycl_cpy_tensor_2d(void *dst,
1907
- const struct ggml_tensor *src,
1908
- int64_t i3, int64_t i2,
1909
- int64_t i1_low, int64_t i1_high,
1910
- queue_ptr stream) try {
1911
-
1912
- dpct::memcpy_direction kind;
1913
- char * src_ptr;
1914
- if (ggml_backend_buffer_is_host(src->buffer)) {
1915
- kind = dpct::host_to_device;
1916
- //GGML_SYCL_DEBUG("%s: Host buffer type src tensor\n", __func__);
1917
- src_ptr = (char *) src->data;
1918
- // GGML_SYCL_DEBUG("ggml_sycl_cpy_tensor_2d GGML_BACKEND_TYPE_CPU src_ptr %p\n", src_ptr);
1919
- } else if (ggml_backend_buffer_is_sycl(src->buffer)) {
1920
- // If buffer is a SYCL buffer
1921
- //GGML_SYCL_DEBUG("%s: SYCL buffer type src tensor\n", __func__);
1922
- kind = dpct::device_to_device;
1923
- src_ptr = (char *) src->data;
1924
- } else if (ggml_backend_buffer_is_sycl_split(src->buffer)) {
1925
- /*
1926
- If buffer is a SYCL split buffer
1927
- */
1928
- //GGML_SYCL_DEBUG("%s: Split buffer type src tensor\n", __func__);
1929
- GGML_ASSERT(i1_low == 0 && i1_high == src->ne[1]);
1930
- kind = dpct::device_to_device;
1931
- ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
1932
- int id;
1933
- SYCL_CHECK(CHECK_TRY_ERROR(
1934
- id = get_current_device_id()));
1935
- // GGML_SYCL_DEBUG("current device index %d\n", id);
1936
- src_ptr = (char *) extra->data_device[id];
1937
- } else {
1938
- // GGML_SYCL_DEBUG("GGML_ABORT("fatal error")\n");
1939
- GGML_ABORT("fatal error");
1940
- }
1941
- char * dst_ptr = (char *) dst;
1942
-
1943
- GGML_TENSOR_LOCALS_1(int64_t, ne, src, ne);
1944
- GGML_TENSOR_LOCALS(int64_t, nb, src, nb);
1945
- const enum ggml_type type = src->type;
1946
- const int64_t ts = ggml_type_size(type);
1947
- const int64_t bs = ggml_blck_size(type);
1948
- int64_t i1_diff = i1_high - i1_low;
1949
-
1950
- const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
1951
- if (nb0 == ts && nb1 == ts*ne0/bs) {
1952
- // GGML_SYCL_DEBUG("stream->memcpy: dst_ptr=%p, x=%p, size=%lu\n", dst_ptr, x, i1_diff * nb1);
1953
- // return CHECK_TRY_ERROR(stream->memcpy(dst_ptr, x, i1_diff * nb1));
1954
- return CHECK_TRY_ERROR(dpct::async_dpct_memcpy(dst_ptr, x, i1_diff * nb1,
1955
- kind, *stream));
1956
-
1957
- } else if (nb0 == ts) {
1958
- return CHECK_TRY_ERROR(
1959
- dpct::async_dpct_memcpy(dst_ptr, ts * ne0 / bs, x, nb1,
1960
- ts * ne0 / bs, i1_diff, kind, *stream));
1961
- } else {
1962
- for (int64_t i1 = 0; i1 < i1_diff; i1++) {
1963
- const void * rx = (const void *) ((const char *) x + i1*nb1);
1964
- void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
1965
- // pretend the row is a matrix with cols=1
1966
- dpct::err0 r = CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
1967
- rd, ts / bs, rx, nb0, ts / bs, ne0, kind, *stream));
1968
- /*
1969
- DPCT1001:85: The statement could not be removed.
1970
- */
1971
- /*
1972
- DPCT1000:86: Error handling if-stmt was detected but could not be
1973
- rewritten.
1974
- */
1975
- if (r != 0) return r;
1976
- }
1977
- return 0;
1978
- }
1979
- }
1980
- catch (sycl::exception const &exc) {
1981
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
1982
- << ", line:" << __LINE__ << std::endl;
1983
- std::exit(1);
1984
- }
1985
-
1986
- inline void ggml_sycl_op_mul_mat_sycl(
1987
- ggml_backend_sycl_context & ctx,
1988
- const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
1989
- const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
1990
- float *dst_dd_i, const int64_t row_low, const int64_t row_high,
1991
- const int64_t src1_ncols, const int64_t src1_padded_row_size,
1992
- const queue_ptr &stream) try {
1993
-
1994
- GGML_ASSERT(src0_dd_i != nullptr);
1995
- GGML_ASSERT(src1_ddf_i != nullptr);
1996
- GGML_ASSERT(dst_dd_i != nullptr);
1997
-
1998
- const int64_t ne00 = src0->ne[0];
1999
- const int64_t ne10 = src1->ne[0];
2000
- GGML_ASSERT(ne00 == ne10);
2001
-
2002
- const int64_t row_diff = row_high - row_low;
2003
-
2004
- int id;
2005
- SYCL_CHECK(
2006
- CHECK_TRY_ERROR(id = get_current_device_id()));
2007
-
2008
- const int64_t ne0 = dst->ne[0]; // used by MKL only
2009
- // the main device has a larger memory buffer to hold the results from all GPUs
2010
- // ldc == nrows of the matrix that cuBLAS writes into
2011
- int ldc = id == ctx.device ? ne0 : row_diff; // used by MKL only
2012
-
2013
- #ifdef GGML_SYCL_F16
2014
- bool use_fp16 = true; // TODO(Yu) SYCL capability check
2015
- #else
2016
- bool use_fp16 = false;
2017
- #endif
2018
- if ((src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
2019
- use_fp16 && ggml_is_contiguous(src0) && row_diff == src0->ne[1] &&
2020
- dst->op_params[0] == GGML_PREC_DEFAULT) {
2021
- // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp16 path\n");
2022
- ggml_sycl_pool_alloc<sycl::half> src0_as_f16(ctx.pool());
2023
- if (src0->type != GGML_TYPE_F16) {
2024
- const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src0->type, dst);
2025
- GGML_ASSERT(to_fp16_sycl != nullptr);
2026
- size_t ne = row_diff*ne00;
2027
- src0_as_f16.alloc(ne);
2028
- to_fp16_sycl(src0_dd_i, src0_as_f16.get(), ne, stream);
2029
- }
2030
- const sycl::half *src0_ptr = src0->type == GGML_TYPE_F16
2031
- ? (const sycl::half *)src0_dd_i
2032
- : src0_as_f16.get();
2033
-
2034
- ggml_sycl_pool_alloc<sycl::half> src1_as_f16(ctx.pool());
2035
- if (src1->type != GGML_TYPE_F16) {
2036
- const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type, dst);
2037
- GGML_ASSERT(to_fp16_sycl != nullptr);
2038
- size_t ne = src1_ncols*ne10;
2039
- src1_as_f16.alloc(ne);
2040
- to_fp16_sycl(src1_ddf_i, src1_as_f16.get(), ne, stream);
2041
- }
2042
- const sycl::half *src1_ptr = src1->type == GGML_TYPE_F16
2043
- ? (const sycl::half *)src1->data + src1_padded_row_size
2044
- : src1_as_f16.get();
2045
- ggml_sycl_pool_alloc<sycl::half> dst_f16(ctx.pool(), row_diff * src1_ncols);
2046
-
2047
- #if GGML_SYCL_DNNL
2048
- if (!g_ggml_sycl_disable_dnn) {
2049
- DnnlGemmWrapper::row_gemm(ctx, src1_ncols, row_diff, ne10, src1_ptr,
2050
- DnnlGemmWrapper::to_dt<sycl::half>(), src0_ptr, DnnlGemmWrapper::to_dt<sycl::half>(),
2051
- dst_f16.get(), DnnlGemmWrapper::to_dt<sycl::half>(), stream);
2052
- const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16, dst);
2053
- to_fp32_sycl(dst_f16.get(), dst_dd_i, row_diff* src1_ncols, stream);
2054
- }
2055
- else
2056
- #endif
2057
- {
2058
- const sycl::half alpha_f16 = 1.0f;
2059
- const sycl::half beta_f16 = 0.0f;
2060
- SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm(
2061
- *stream, oneapi::math::transpose::trans,
2062
- oneapi::math::transpose::nontrans, row_diff, src1_ncols, ne10,
2063
- &alpha_f16, src0_ptr, dpct::library_data_t::real_half, ne00,
2064
- src1_ptr, dpct::library_data_t::real_half, ne10, &beta_f16,
2065
- dst_f16.get(), dpct::library_data_t::real_half, ldc,
2066
- dpct::library_data_t::real_half)));
2067
- const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16, dst);
2068
- to_fp32_sycl(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
2069
- }
2070
- }
2071
- else {
2072
- // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp32 path\n");
2073
- ggml_sycl_pool_alloc<float> src0_ddq_as_f32(ctx.pool());
2074
- ggml_sycl_pool_alloc<float> src1_ddq_as_f32(ctx.pool());
2075
- if (src0->type != GGML_TYPE_F32) {
2076
- const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(src0->type, dst);
2077
- GGML_ASSERT(to_fp32_sycl != nullptr);
2078
- src0_ddq_as_f32.alloc(row_diff*ne00);
2079
- to_fp32_sycl(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
2080
- }
2081
- if (src1->type != GGML_TYPE_F32) {
2082
- const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(src1->type, dst);
2083
- GGML_ASSERT(to_fp32_sycl != nullptr);
2084
- src1_ddq_as_f32.alloc(src1_ncols*ne10);
2085
- to_fp32_sycl(src1_ddf_i, src1_ddq_as_f32.get(), src1_ncols*ne10, stream);
2086
- }
2087
- const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
2088
- const float * src1_ddf1_i = src1->type == GGML_TYPE_F32 ? (const float *) src1_ddf_i : src1_ddq_as_f32.get();
2089
-
2090
- #if GGML_SYCL_DNNL
2091
- if (!g_ggml_sycl_disable_dnn) {
2092
- DnnlGemmWrapper::row_gemm(ctx, src1_ncols, row_diff, ne10, src1_ddf1_i,
2093
- DnnlGemmWrapper::to_dt<float>(), src0_ddf_i, DnnlGemmWrapper::to_dt<float>(),
2094
- dst_dd_i, DnnlGemmWrapper::to_dt<float>(), stream);
2095
- }
2096
- else
2097
- #endif
2098
- {
2099
- const float alpha = 1.0f;
2100
- const float beta = 0.0f;
2101
- SYCL_CHECK(CHECK_TRY_ERROR(oneapi::math::blas::column_major::gemm(
2102
- get_onemath_backend(*stream), oneapi::math::transpose::trans, oneapi::math::transpose::nontrans, row_diff,
2103
- src1_ncols, ne10, dpct::get_value(&alpha, *stream), src0_ddf_i, ne00, src1_ddf1_i, ne10,
2104
- dpct::get_value(&beta, *stream), dst_dd_i, ldc)));
2105
- }
2106
- }
2107
- GGML_UNUSED(dst);
2108
- GGML_UNUSED(src1_ddq_i);
2109
- GGML_UNUSED(src1_padded_row_size);
2110
- }
2111
- catch (sycl::exception const &exc) {
2112
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
2113
- << ", line:" << __LINE__ << std::endl;
2114
- std::exit(1);
2115
- }
2116
-
2117
- static void ggml_sycl_op_pool2d(ggml_backend_sycl_context & ctx, ggml_tensor *dst) {
2118
-
2119
- GGML_ASSERT(dst->src[0]->type == GGML_TYPE_F32);
2120
- GGML_ASSERT( dst->type == GGML_TYPE_F32);
2121
- dpct::queue_ptr main_stream = ctx.stream();
2122
- SYCL_CHECK(ggml_sycl_set_device(ctx.device));
2123
- const float * src0_dd = static_cast<const float *>(dst->src[0]->data);
2124
- float * dst_dd = static_cast<float *>(dst->data);
2125
-
2126
- const int32_t * opts = (const int32_t *)dst->op_params;
2127
- enum ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]);
2128
- const int k0 = opts[1];
2129
- const int k1 = opts[2];
2130
- const int s0 = opts[3];
2131
- const int s1 = opts[4];
2132
- const int p0 = opts[5];
2133
- const int p1 = opts[6];
2134
-
2135
- const int64_t IH = dst->src[0]->ne[1];
2136
- const int64_t IW = dst->src[0]->ne[0];
2137
-
2138
- const int64_t N = dst->ne[3];
2139
- const int64_t OC = dst->ne[2];
2140
- const int64_t OH = dst->ne[1];
2141
- const int64_t OW = dst->ne[0];
2142
-
2143
- const int parallel_elements = N * OC * OH * OW;
2144
- const int num_blocks = (parallel_elements + SYCL_POOL2D_BLOCK_SIZE - 1) / SYCL_POOL2D_BLOCK_SIZE;
2145
- sycl::range<3> block_nums(1, 1, num_blocks);
2146
- main_stream->parallel_for(
2147
- sycl::nd_range<3>(block_nums *
2148
- sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE),
2149
- sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE)),
2150
- [=](sycl::nd_item<3> item_ct1) {
2151
- pool2d_nchw_kernel(IH, IW, OH, OW, k1, k0, s1, s0, p1, p0,
2152
- parallel_elements, src0_dd, dst_dd, op,
2153
- item_ct1);
2154
- });
2155
- }
2156
-
2157
- inline void ggml_sycl_op_sum(ggml_backend_sycl_context & ctx, ggml_tensor *dst) {
2158
- GGML_ASSERT(dst->src[0]->type == GGML_TYPE_F32);
2159
- GGML_ASSERT( dst->type == GGML_TYPE_F32);
2160
- dpct::queue_ptr main_stream = ctx.stream();
2161
- SYCL_CHECK(ggml_sycl_set_device(ctx.device));
2162
- const float * src0_dd = static_cast<const float *>(dst->src[0]->data);
2163
- float * dst_dd = static_cast<float *>(dst->data);
2164
-
2165
- const int64_t ne = ggml_nelements(dst->src[0]);
2166
-
2167
- sum_rows_f32_sycl(src0_dd, dst_dd, ne, 1, main_stream);
2168
- }
2169
-
2170
- inline void ggml_sycl_op_sum_rows(ggml_backend_sycl_context & ctx, ggml_tensor *dst) {
2171
-
2172
- GGML_ASSERT(dst->src[0]->type == GGML_TYPE_F32);
2173
- GGML_ASSERT( dst->type == GGML_TYPE_F32);
2174
- dpct::queue_ptr main_stream = ctx.stream();
2175
- SYCL_CHECK(ggml_sycl_set_device(ctx.device));
2176
- const float * src0_dd = static_cast<const float *>(dst->src[0]->data);
2177
- float * dst_dd = static_cast<float *>(dst->data);
2178
-
2179
- const int64_t ncols = dst->src[0]->ne[0];
2180
- const int64_t nrows = ggml_nrows(dst->src[0]);
2181
-
2182
- sum_rows_f32_sycl(src0_dd, dst_dd, ncols, nrows, main_stream);
2183
- }
2184
-
2185
- inline void ggml_sycl_op_argsort(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
2186
- GGML_ASSERT(dst->src[0]->type == GGML_TYPE_F32);
2187
- GGML_ASSERT(dst->type == GGML_TYPE_I32);
2188
- dpct::queue_ptr main_stream = ctx.stream();
2189
- SYCL_CHECK(ggml_sycl_set_device(ctx.device));
2190
- const float * src0_dd = static_cast<const float *>(dst->src[0]->data);
2191
- int32_t * dst_dd = static_cast<int32_t *>(dst->data);
2192
-
2193
-
2194
- const int64_t ncols = dst->src[0]->ne[0];
2195
- const int64_t nrows = ggml_nrows(dst->src[0]);
2196
-
2197
- enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
2198
-
2199
- argsort_f32_i32_sycl(src0_dd, (int *) dst_dd, ncols, nrows, order, main_stream);
2200
- }
2201
-
2202
- inline void ggml_sycl_op_argmax(ggml_backend_sycl_context & ctx, ggml_tensor *dst) {
2203
-
2204
- GGML_ASSERT(dst->src[0]->type == GGML_TYPE_F32);
2205
- GGML_ASSERT( dst->type == GGML_TYPE_I32);
2206
-
2207
- dpct::queue_ptr main_stream = ctx.stream();
2208
- SYCL_CHECK(ggml_sycl_set_device(ctx.device));
2209
- const float * src0_dd = static_cast<const float *>(dst->src[0]->data);
2210
- int32_t * dst_dd = static_cast<int32_t *>(dst->data);
2211
-
2212
- const int64_t ncols = dst->src[0]->ne[0];
2213
- const int64_t nrows = ggml_nrows(dst->src[0]);
2214
-
2215
- argmax_f32_i32_sycl(src0_dd, dst_dd, ncols, nrows, main_stream);
2216
- }
2217
-
2218
- inline void ggml_sycl_op_diag_mask_inf(ggml_backend_sycl_context & ctx,ggml_tensor *dst) {
2219
-
2220
- GGML_ASSERT(dst->src[0]->type == GGML_TYPE_F32);
2221
- GGML_ASSERT( dst->type == GGML_TYPE_F32);
2222
- dpct::queue_ptr main_stream = ctx.stream();
2223
- SYCL_CHECK(ggml_sycl_set_device(ctx.device));
2224
- const float * src0_dd = static_cast<const float *>(dst->src[0]->data);
2225
- float * dst_dd = static_cast<float *>(dst->data);
2226
-
2227
- const int64_t ne00 = dst->src[0]->ne[0];
2228
- const int64_t ne01 = dst->src[0]->ne[1];
2229
- const int nrows0 = ggml_nrows(dst->src[0]);
2230
-
2231
- const int n_past = ((int32_t *) dst->op_params)[0];
2232
-
2233
- diag_mask_inf_f32_sycl(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
2234
- }
2235
-
2236
- inline void ggml_sycl_op_scale(ggml_backend_sycl_context & ctx, ggml_tensor *dst) {
2237
-
2238
- GGML_ASSERT(dst->src[0]->type == GGML_TYPE_F32);
2239
- GGML_ASSERT( dst->type == GGML_TYPE_F32);
2240
- dpct::queue_ptr main_stream = ctx.stream();
2241
- SYCL_CHECK(ggml_sycl_set_device(ctx.device));
2242
- const float * src0_dd = static_cast<const float *>(dst->src[0]->data);
2243
- float * dst_dd = static_cast<float *>(dst->data);
2244
-
2245
- float scale;
2246
- memcpy(&scale, dst->op_params, sizeof(float));
2247
-
2248
- scale_f32_sycl(src0_dd, dst_dd, scale, ggml_nelements(dst->src[0]), main_stream);
2249
- /*
2250
- DPCT1010:87: SYCL uses exceptions to report errors and does not use the
2251
- error codes. The call was replaced with 0. You need to rewrite this code.
2252
- */
2253
- SYCL_CHECK(0);
2254
- }
2255
-
2256
- static void ggml_sycl_set_peer_access(const int n_tokens, int main_device) {
2257
- static bool peer_access_enabled = false;
2258
-
2259
- const bool enable_peer_access = n_tokens <= GGML_SYCL_PEER_MAX_BATCH_SIZE;
2260
-
2261
- if (peer_access_enabled == enable_peer_access) {
2262
- return;
2263
- }
2264
-
2265
- #ifdef NDEBUG
2266
- for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
2267
- SYCL_CHECK(ggml_sycl_set_device(i));
2268
- }
2269
-
2270
- for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
2271
- SYCL_CHECK(ggml_sycl_set_device(i));
2272
-
2273
- for (int id_other = 0; id_other < ggml_sycl_info().device_count; ++id_other) {
2274
- if (i == id_other) {
2275
- continue;
2276
- }
2277
- if (i != main_device && id_other != main_device) {
2278
- continue;
2279
- }
2280
-
2281
- // int can_access_peer;
2282
- // SYCL_CHECK(syclDeviceCanAccessPeer(&can_access_peer, id, id_other));
2283
- // if (can_access_peer) {
2284
- // if (enable_peer_access) {
2285
- // SYCL_CHECK(syclDeviceEnablePeerAccess(id_other, 0));
2286
- // } else {
2287
- // SYCL_CHECK(syclDeviceDisablePeerAccess(id_other));
2288
- // }
2289
- // }
2290
- }
2291
- }
2292
- #endif // NDEBUG
2293
-
2294
- peer_access_enabled = enable_peer_access;
2295
- }
2296
-
2297
- static void ggml_sycl_op_mul_mat(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
2298
- const ggml_tensor *src1, ggml_tensor *dst,
2299
- ggml_sycl_op_mul_mat_t op,
2300
- const bool convert_src1_to_q8_1) try {
2301
-
2302
- GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne);
2303
-
2304
- GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
2305
- const int64_t nrows1 = ggml_nrows(src1);
2306
-
2307
- GGML_ASSERT(ne03 == ne13);
2308
-
2309
- const int64_t ne0 = dst->ne[0];
2310
- const int64_t ne1 = dst->ne[1];
2311
-
2312
- const int nb2 = dst->nb[2];
2313
- const int nb3 = dst->nb[3];
2314
-
2315
- GGML_ASSERT(!ggml_backend_buffer_is_sycl_split(dst->buffer));
2316
- GGML_ASSERT(!ggml_backend_buffer_is_sycl_split(src1->buffer));
2317
- GGML_ASSERT(src1->type == GGML_TYPE_F32 || (src1->ne[2] == 1 && src1->ne[3] == 1));
2318
-
2319
- GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
2320
-
2321
- const int64_t i02_divisor = ne12 / ne02;
2322
-
2323
- const size_t src0_ts = ggml_type_size(src0->type);
2324
- const size_t src0_bs = ggml_blck_size(src0->type);
2325
- const size_t q8_1_ts = sizeof(block_q8_1);
2326
- const size_t q8_1_bs = QK8_1;
2327
-
2328
- ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
2329
- ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
2330
-
2331
- const bool src0_is_contiguous = ggml_is_contiguous(src0);
2332
- const bool src1_is_contiguous = ggml_is_contiguous(src1);
2333
-
2334
- int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
2335
-
2336
- const bool split = ggml_backend_buffer_is_sycl_split(src0->buffer);
2337
- GGML_ASSERT(!(split && ne02 > 1));
2338
- GGML_ASSERT(!(split && ne03 > 1));
2339
- GGML_ASSERT(!(split && ne02 < ne12));
2340
-
2341
- std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split;
2342
- if (split) {
2343
- // TODO: check that src0->buffer->buft is a split buffer type, replace GGML_BACKEND_TYPE_GPU_SPLIT check
2344
- // GGML_ASSERT(src0->buffer != nullptr && src0->buffer->buft == ...);
2345
- ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *) src0->buffer->buft->context;
2346
- tensor_split = buft_ctx->tensor_split;
2347
- }
2348
-
2349
- struct dev_data {
2350
- ggml_sycl_pool_alloc<char> src0_dd_alloc;
2351
- ggml_sycl_pool_alloc<float> src1_ddf_alloc;
2352
- ggml_sycl_pool_alloc<char> src1_ddq_alloc;
2353
- ggml_sycl_pool_alloc<float> dst_dd_alloc;
2354
-
2355
- char *src0_dd = nullptr;
2356
- float *src1_ddf = nullptr; // float
2357
- char *src1_ddq = nullptr; // q8_1
2358
- float *dst_dd = nullptr;
2359
-
2360
- int64_t row_low;
2361
- int64_t row_high;
2362
- };
2363
-
2364
- dev_data dev[GGML_SYCL_MAX_DEVICES];
2365
-
2366
- int used_devices = 0;
2367
- queue_ptr main_stream = ctx.stream();
2368
-
2369
- for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
2370
- // by default, use all rows
2371
- dev[i].row_low = 0;
2372
- dev[i].row_high = ne01;
2373
-
2374
- // for multi GPU, get the row boundaries from tensor split
2375
- // and round to mul_mat_q tile sizes
2376
- if (split) {
2377
- const int64_t rounding = get_row_rounding(src0->type, tensor_split);
2378
-
2379
- if (i != 0) {
2380
- dev[i].row_low = ne01*tensor_split[i];
2381
- if (dev[i].row_low < ne01) {
2382
- dev[i].row_low -= dev[i].row_low % rounding;
2383
- }
2384
- }
2385
-
2386
- if (i != ggml_sycl_info().device_count - 1) {
2387
- dev[i].row_high = ne01*tensor_split[i + 1];
2388
- if (dev[i].row_high < ne01) {
2389
- dev[i].row_high -= dev[i].row_high % rounding;
2390
- }
2391
- }
2392
- }
2393
- }
2394
-
2395
- for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
2396
- if ((!split && i != ctx.device) || dev[i].row_low == dev[i].row_high) {
2397
- continue;
2398
- }
2399
-
2400
- used_devices++;
2401
-
2402
- const bool src1_on_device = i == ctx.device;
2403
- const bool dst_on_device = i == ctx.device;
2404
-
2405
- ggml_sycl_set_device(i);
2406
- queue_ptr stream = ctx.stream(i, 0);
2407
-
2408
- if (src0_is_contiguous) {
2409
- dev[i].src0_dd = (char *) src0->data;
2410
- } else {
2411
- dev[i].src0_dd = dev[i].src0_dd_alloc.alloc(ctx.pool(i), ggml_nbytes(src0));
2412
- }
2413
-
2414
- if (src1_on_device && src1_is_contiguous) {
2415
- dev[i].src1_ddf = (float *) src1->data;
2416
- } else {
2417
- dev[i].src1_ddf = dev[i].src1_ddf_alloc.alloc(ctx.pool(i), ggml_nelements(src1));
2418
- }
2419
-
2420
- if (convert_src1_to_q8_1) {
2421
- dev[i].src1_ddq = dev[i].src1_ddq_alloc.alloc(ctx.pool(i), nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs);
2422
-
2423
- if (src1_on_device && src1_is_contiguous) {
2424
- quantize_row_q8_1_sycl(dev[i].src1_ddf, dev[i].src1_ddq, ne10, nrows1, src1_padded_col_size, stream);
2425
- /*
2426
- DPCT1010:90: SYCL uses exceptions to report errors and does not
2427
- use the error codes. The call was replaced with 0. You need to
2428
- rewrite this code.
2429
- */
2430
- SYCL_CHECK(0);
2431
- }
2432
- }
2433
-
2434
- if (dst_on_device) {
2435
- dev[i].dst_dd = (float *) dst->data;
2436
- } else {
2437
- const size_t size_dst_ddf = split ? (dev[i].row_high - dev[i].row_low)*ne1 : ggml_nelements(dst);
2438
- dev[i].dst_dd = dev[i].dst_dd_alloc.alloc(ctx.pool(i), size_dst_ddf);
2439
- }
2440
- }
2441
-
2442
- // if multiple devices are used they need to wait for the main device
2443
- // here an event is recorded that signals that the main device has finished calculating the input data
2444
- if (split && used_devices > 1) {
2445
- ggml_sycl_set_device(ctx.device);
2446
- /*
2447
- DPCT1024:91: The original code returned the error code that was further
2448
- consumed by the program logic. This original code was replaced with 0.
2449
- You may need to rewrite the program logic consuming the error code.
2450
- */
2451
- SYCL_CHECK(CHECK_TRY_ERROR(
2452
- *src0_extra->events[ctx.device][0] =
2453
- ctx.stream()->ext_oneapi_submit_barrier()));
2454
- }
2455
-
2456
- const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
2457
- for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
2458
- const int64_t is = split ? (src1_col_0/src1_col_stride) % GGML_SYCL_MAX_STREAMS : 0;
2459
- const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
2460
- for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
2461
- if ((!split && i != ctx.device) || dev[i].row_low == dev[i].row_high) {
2462
- continue;
2463
- }
2464
-
2465
- const bool src1_on_device = i == ctx.device;
2466
- const bool dst_on_device = i == ctx.device;
2467
- const int64_t row_diff = dev[i].row_high - dev[i].row_low;
2468
-
2469
- ggml_sycl_set_device(i);
2470
- queue_ptr stream = ctx.stream(i, is);
2471
-
2472
- // wait for main GPU data if necessary
2473
- if (split && (i != ctx.device || is != 0)) {
2474
- /*
2475
- DPCT1009:163: SYCL uses exceptions to report errors and does not
2476
- use the error codes. The original code was commented out and a
2477
- warning string was inserted. You need to rewrite this code.
2478
- */
2479
- SYCL_CHECK(CHECK_TRY_ERROR(stream->ext_oneapi_submit_barrier(
2480
- {*src0_extra->events[ctx.device][0]})));
2481
- }
2482
-
2483
- for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
2484
- const int64_t i03 = i0 / ne12;
2485
- const int64_t i02 = i0 % ne12;
2486
-
2487
- const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
2488
-
2489
- // for split tensors the data begins at i0 == i0_offset_low
2490
- char * src0_dd_i = dev[i].src0_dd + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
2491
- float * src1_ddf_i = dev[i].src1_ddf + (i0*ne11 + src1_col_0) * ne10;
2492
- char * src1_ddq_i = dev[i].src1_ddq + src1_ddq_i_offset;
2493
- float * dst_dd_i = dev[i].dst_dd + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
2494
-
2495
- // the main device memory buffer can be on VRAM scratch, with space for all partial results
2496
- // in that case an offset on dst_ddf_i is needed
2497
- if (i == ctx.device) {
2498
- dst_dd_i += dev[i].row_low; // offset is 0 if no tensor split
2499
- }
2500
-
2501
- // copy src0, src1 to device if necessary
2502
- if (src1_is_contiguous) {
2503
- if (i != ctx.device) {
2504
- if (convert_src1_to_q8_1) {
2505
- char * src1_ddq_i_source = dev[ctx.device].src1_ddq + src1_ddq_i_offset;
2506
- SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(
2507
- src1_ddq_i, src1_ddq_i_source,
2508
- src1_ncols * src1_padded_col_size * q8_1_ts /
2509
- q8_1_bs).wait()));
2510
- } else {
2511
-
2512
- float * src1_ddf_i_source = (float *) src1_extra->data_device[ctx.device];
2513
- src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
2514
-
2515
- SYCL_CHECK(CHECK_TRY_ERROR(dev2dev_memcpy(*stream, *main_stream,
2516
- src1_ddf_i, src1_ddf_i_source,
2517
- src1_ncols * ne10 * sizeof(float))));
2518
- }
2519
- }
2520
- } else if (src1_on_device && !src1_is_contiguous) {
2521
- SYCL_CHECK(ggml_sycl_cpy_tensor_2d(
2522
- src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
2523
- } else {
2524
- GGML_ABORT("fatal error");
2525
- }
2526
-
2527
- if (convert_src1_to_q8_1 && !src1_is_contiguous) {
2528
- quantize_row_q8_1_sycl(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
2529
- /*
2530
- DPCT1010:92: SYCL uses exceptions to report errors and does
2531
- not use the error codes. The call was replaced with 0. You
2532
- need to rewrite this code.
2533
- */
2534
- SYCL_CHECK(0);
2535
- }
2536
-
2537
- if (src1_col_0 == 0 && !src0_is_contiguous && i02 % i02_divisor == 0) {
2538
- SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, dev[i].row_low, dev[i].row_high, stream));
2539
- }
2540
- if (src1->type == GGML_TYPE_F16) {
2541
- src1_padded_col_size = (i0 * ne11 + src1_col_0) * ne10;
2542
- }
2543
- // do the computation
2544
- SYCL_CHECK(CHECK_TRY_ERROR(op(ctx, src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
2545
- dev[i].row_low, dev[i].row_high, src1_ncols, src1_padded_col_size, stream)));
2546
- /*
2547
- DPCT1010:93: SYCL uses exceptions to report errors and does not
2548
- use the error codes. The call was replaced with 0. You need to
2549
- rewrite this code.
2550
- */
2551
- SYCL_CHECK(0);
2552
-
2553
- // copy dst to host or other device if necessary
2554
- if (!dst_on_device) {
2555
- void * dst_off_device = dst->data;
2556
- if (split) {
2557
- // src0 = weight matrix is saved as a transposed matrix for better memory layout.
2558
- // dst is NOT transposed.
2559
- // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
2560
- // Instead they need to be copied to the correct slice in ne0 = dst row index.
2561
- // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
2562
- float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
2563
- GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
2564
- dhf_dst_i += src1_col_0*ne0 + dev[i].row_low;
2565
-
2566
- SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
2567
- dhf_dst_i, ne0 * sizeof(float), dst_dd_i,
2568
- row_diff * sizeof(float), row_diff * sizeof(float),
2569
- src1_ncols, dpct::device_to_device, *stream)));
2570
- } else {
2571
- float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
2572
- GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
2573
- dhf_dst_i += src1_col_0*ne0;
2574
- SYCL_CHECK(CHECK_TRY_ERROR(
2575
- stream->memcpy(dhf_dst_i, dst_dd_i,
2576
- src1_ncols * ne0 * sizeof(float)).wait()));
2577
- }
2578
- }
2579
-
2580
- // add event for the main device to wait on until other device is done
2581
- if (split && (i != ctx.device || is != 0)) {
2582
- /*
2583
- DPCT1024:94: The original code returned the error code that
2584
- was further consumed by the program logic. This original
2585
- code was replaced with 0. You may need to rewrite the
2586
- program logic consuming the error code.
2587
- */
2588
- SYCL_CHECK(CHECK_TRY_ERROR(
2589
- *src0_extra->events[i][is] =
2590
- stream->ext_oneapi_submit_barrier()));
2591
- }
2592
- }
2593
- }
2594
- }
2595
-
2596
- // main device waits for all other devices to be finished
2597
- if (split && ggml_sycl_info().device_count > 1) {
2598
- int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
2599
- is_max = is_max <= GGML_SYCL_MAX_STREAMS ? is_max : GGML_SYCL_MAX_STREAMS;
2600
-
2601
- ggml_sycl_set_device(ctx.device);
2602
- for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
2603
- if (dev[i].row_low == dev[i].row_high) {
2604
- continue;
2605
- }
2606
- for (int64_t is = 0; is < is_max; ++is) {
2607
- SYCL_CHECK(CHECK_TRY_ERROR(
2608
- ctx.stream()->ext_oneapi_submit_barrier(
2609
- {*src0_extra->events[i][is]})));
2610
- }
2611
- }
2612
- }
2613
- }
2614
- catch (sycl::exception const &exc) {
2615
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
2616
- << ", line:" << __LINE__ << std::endl;
2617
- std::exit(1);
2618
- }
2619
-
2620
-
2621
- static void ggml_sycl_get_rows(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
2622
- GGML_SYCL_DEBUG("call %s\n", __func__);
2623
- ggml_sycl_op_get_rows(ctx, dst);
2624
- GGML_SYCL_DEBUG("call %s done\n", __func__);
2625
- }
2626
-
2627
- static void ggml_sycl_norm(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
2628
- GGML_SYCL_DEBUG("call %s\n", __func__);
2629
- ggml_sycl_op_norm(ctx, dst);
2630
- GGML_SYCL_DEBUG("call %s done\n", __func__);
2631
- }
2632
-
2633
- static void ggml_sycl_rms_norm(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
2634
- GGML_SYCL_DEBUG("call %s\n", __func__);
2635
- ggml_sycl_op_rms_norm(ctx, dst);
2636
- GGML_SYCL_DEBUG("call %s done\n", __func__);
2637
- }
2638
-
2639
- static void ggml_sycl_l2_norm(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
2640
- GGML_SYCL_DEBUG("call %s\n", __func__);
2641
- ggml_sycl_op_l2_norm(ctx, dst);
2642
- GGML_SYCL_DEBUG("call %s done\n", __func__);
2643
- }
2644
-
2645
- static void ggml_sycl_group_norm(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
2646
- GGML_SYCL_DEBUG("call %s\n", __func__);
2647
- ggml_sycl_op_group_norm(ctx, dst);
2648
- GGML_SYCL_DEBUG("call %s done\n", __func__);
2649
- }
2650
-
2651
- static void ggml_sycl_mul_mat_vec_p021(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
2652
- const ggml_tensor *src1,
2653
- ggml_tensor *dst) try {
2654
- GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
2655
- GGML_ASSERT(!ggml_backend_buffer_is_sycl_split(src0->buffer));
2656
- GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
2657
- GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
2658
- GGML_ASSERT(src0->type == GGML_TYPE_F16);
2659
- GGML_ASSERT(src1->type == GGML_TYPE_F32);
2660
-
2661
- const int64_t ne00 = src0->ne[0];
2662
- const int64_t ne01 = src0->ne[1];
2663
- const int64_t ne02 = src0->ne[2];
2664
-
2665
- const int64_t ne12 = src1->ne[2];
2666
-
2667
- SYCL_CHECK(ggml_sycl_set_device(ctx.device));
2668
- queue_ptr main_stream = ctx.stream();
2669
-
2670
- void * src0_ddq = src0->data;
2671
- float * src1_ddf = (float *) src1->data;
2672
- float * dst_ddf = (float *) dst->data;
2673
-
2674
- ggml_mul_mat_p021_f16_f32_sycl(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
2675
- }
2676
- catch (sycl::exception const &exc) {
2677
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
2678
- << ", line:" << __LINE__ << std::endl;
2679
- std::exit(1);
2680
- }
2681
-
2682
- static void ggml_sycl_mul_mat_vec_nc(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
2683
- const ggml_tensor *src1,
2684
- ggml_tensor *dst) try {
2685
- GGML_ASSERT(!ggml_is_transposed(src0));
2686
- GGML_ASSERT(!ggml_is_transposed(src1));
2687
- GGML_ASSERT(!ggml_is_permuted(src0));
2688
- GGML_ASSERT(!ggml_backend_buffer_is_sycl_split(src0->buffer));
2689
- GGML_ASSERT(src0->type == GGML_TYPE_F16);
2690
- GGML_ASSERT(src1->type == GGML_TYPE_F32);
2691
-
2692
- const int64_t ne00 = src0->ne[0];
2693
- const int64_t ne01 = src0->ne[1];
2694
- const int64_t ne02 = src0->ne[2];
2695
-
2696
- const int64_t nb01 = src0->nb[1];
2697
- const int64_t nb02 = src0->nb[2];
2698
-
2699
- const int64_t ne12 = src1->ne[2];
2700
-
2701
- SYCL_CHECK(ggml_sycl_set_device(ctx.device));
2702
- queue_ptr main_stream = ctx.stream();
2703
-
2704
- void * src0_ddq = src0->data;
2705
- float * src1_ddf = (float *) src1->data;
2706
- float * dst_ddf = (float *) dst->data;
2707
-
2708
- const int64_t row_stride_x = nb01 / sizeof(sycl::half);
2709
- const int64_t channel_stride_x = nb02 / sizeof(sycl::half);
2710
-
2711
- ggml_mul_mat_vec_nc_f16_f32_sycl(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, row_stride_x, ne02, ne12, channel_stride_x, main_stream);
2712
- }
2713
- catch (sycl::exception const &exc) {
2714
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
2715
- << ", line:" << __LINE__ << std::endl;
2716
- std::exit(1);
2717
- }
2718
-
2719
- static void k_compute_batched_ptrs(const sycl::half * src0_as_f16, const sycl::half * src1_as_f16, void * dst,
2720
- const void ** ptrs_src, void ** ptrs_dst, int64_t ne12, int64_t ne13, int64_t ne23,
2721
- size_t nb02, size_t nb03, size_t nb12, size_t nb13, size_t nbd2, size_t nbd3,
2722
- int64_t r2, int64_t r3, const sycl::nd_item<3> & item_ct1) {
2723
- const int64_t i13 = item_ct1.get_group(2) * item_ct1.get_local_range(2) + item_ct1.get_local_id(2);
2724
- const int64_t i12 = item_ct1.get_group(1) * item_ct1.get_local_range(1) + item_ct1.get_local_id(1);
2725
-
2726
- if (i13 >= ne13 || i12 >= ne12) {
2727
- return;
2728
- }
2729
-
2730
- const int64_t i03 = i13 / r3;
2731
- const int64_t i02 = i12 / r2;
2732
-
2733
- const uint8_t * src0_bytes = reinterpret_cast<const uint8_t *>(src0_as_f16);
2734
- const uint8_t * src1_bytes = reinterpret_cast<const uint8_t *>(src1_as_f16);
2735
- uint8_t * dst_bytes = static_cast<uint8_t *>(dst);
2736
-
2737
- ptrs_src[0 * ne23 + i12 + i13 * ne12] = src0_bytes + i02 * nb02 + i03 * nb03;
2738
- ptrs_src[1 * ne23 + i12 + i13 * ne12] = src1_bytes + i12 * nb12 + i13 * nb13;
2739
- ptrs_dst[0 * ne23 + i12 + i13 * ne12] = dst_bytes + i12 * nbd2 + i13 * nbd3;
2740
- }
2741
-
2742
- static void ggml_sycl_mul_mat_batched_sycl(ggml_backend_sycl_context & ctx, const ggml_tensor * src0,
2743
- const ggml_tensor * src1, ggml_tensor * dst) try {
2744
- GGML_ASSERT(!ggml_is_transposed(src0));
2745
- GGML_ASSERT(!ggml_is_transposed(src1));
2746
- GGML_ASSERT(!ggml_backend_buffer_is_sycl_split(src0->buffer));
2747
- GGML_ASSERT(src0->type == GGML_TYPE_F16);
2748
- GGML_ASSERT(dst->type == GGML_TYPE_F32);
2749
-
2750
- GGML_TENSOR_BINARY_OP_LOCALS
2751
-
2752
- // TODO: see https://github.com/ggml-org/llama.cpp/pull/13155
2753
- // Batched mul_mat requires a rewrite to support both oneDNN and non-contiguous dst
2754
- GGML_ASSERT(ggml_is_contiguous(dst));
2755
-
2756
- SYCL_CHECK(ggml_sycl_set_device(ctx.device));
2757
- queue_ptr queue = ctx.stream();
2758
-
2759
- dpct::has_capability_or_fail(queue->get_device(), { sycl::aspect::fp16 });
2760
-
2761
- const sycl::half * src0_f16 = static_cast<const sycl::half *>(src0->data);
2762
- float * dst_ddf = static_cast<float *>(dst->data);
2763
-
2764
- const sycl::half * src1_f16 = static_cast<const sycl::half *>(src1->data);
2765
- const size_t type_size_src1 = ggml_type_size(src1->type);
2766
- GGML_ASSERT(nb10 == type_size_src1);
2767
-
2768
- // SRC1 strides
2769
- int64_t s11 = nb11 / type_size_src1;
2770
- int64_t s12 = nb12 / type_size_src1;
2771
- int64_t s13 = nb13 / type_size_src1;
2772
- ggml_sycl_pool_alloc<sycl::half> src1_f16_alloc(ctx.pool());
2773
-
2774
- // convert src1 to fp16
2775
- if (src1->type != GGML_TYPE_F16) {
2776
- const to_fp16_nc_sycl_t to_fp16_nc_sycl = get_to_fp16_nc_sycl(src1->type);
2777
- GGML_ASSERT(to_fp16_nc_sycl != nullptr);
2778
- const int64_t ne_src1 = ggml_nelements(src1);
2779
- src1_f16_alloc.alloc(ne_src1);
2780
- to_fp16_nc_sycl(src1_f16, src1_f16_alloc.get(), ne10, ne11, ne12, ne13, s11, s12, s13, queue);
2781
-
2782
- src1_f16 = src1_f16_alloc.get();
2783
- s11 = ne10;
2784
- s12 = ne11 * s11;
2785
- s13 = ne12 * s12;
2786
- }
2787
-
2788
- ggml_sycl_pool_alloc<sycl::half> dst_f16(ctx.pool());
2789
-
2790
- dpct::library_data_t mkl_compute_type = dpct::library_data_t::real_float;
2791
- dpct::library_data_t mkl_data_type = dpct::library_data_t::real_float;
2792
-
2793
- // dst strides
2794
- size_t nbd2 = dst->nb[2];
2795
- size_t nbd3 = dst->nb[3];
2796
-
2797
- const float alpha_f32 = 1.0f;
2798
- const float beta_f32 = 0.0f;
2799
-
2800
- const void * alpha = &alpha_f32;
2801
- const void * beta = &beta_f32;
2802
-
2803
- GGML_ASSERT(ne12 % ne02 == 0);
2804
- GGML_ASSERT(ne13 % ne03 == 0);
2805
- GGML_ASSERT(ne01 == static_cast<int64_t>(nb1/nb0));
2806
- GGML_ASSERT(ne10 == ne00);
2807
-
2808
- // broadcast factors
2809
- const int64_t r2 = ne12 / ne02;
2810
- const int64_t r3 = ne13 / ne03;
2811
-
2812
- #if GGML_SYCL_DNNL
2813
- if (!g_ggml_sycl_disable_dnn) {
2814
- auto dnn_gemm = [&ctx, queue, ne11, ne01, ne10, nb00, nb01, nb02, s11, s12]
2815
- (const sycl::half* src1, const sycl::half* src0, float* dst, const dnnl_dim_t batches_a, const dnnl_dim_t batches_b) {
2816
-
2817
- DnnlGemmWrapper::gemm(ctx, ne11,ne01, ne10,
2818
- src1, DnnlGemmWrapper::to_dt<sycl::half>(), s11, 1, s12,
2819
- src0, DnnlGemmWrapper::to_dt<sycl::half>(), 1, nb01/nb00, nb02/nb00,
2820
- dst, DnnlGemmWrapper::to_dt<float>(), queue, batches_a, batches_b);
2821
- };
2822
-
2823
- if (r2 == 1 && r3 == 1) {
2824
- if (ggml_is_contiguous_2(src0) && ggml_is_contiguous_2(src1)) {
2825
- dnn_gemm(src1_f16, src0_f16, dst_ddf, ne12*ne13, ne02 * ne03);
2826
- }
2827
- else {
2828
- for (int64_t ie03 = 0; ie03 < ne03; ++ie03) {
2829
- const sycl::half* src0_f16_shifted = src0_f16 + ((ie03*nb03)/sizeof(sycl::half)); // nb is in bytes
2830
- const sycl::half* src1_f16_shifted = src1_f16 + ie03*s13;
2831
- float* dst_shifted = dst_ddf + ((ie03*nb3)/sizeof(float));
2832
- dnn_gemm(src1_f16_shifted, src0_f16_shifted, dst_shifted, ne12, ne02);
2833
- }
2834
- }
2835
- } else {
2836
- // iterate over batches from smaller set of matrices (matrix 0)
2837
- for (int64_t ie02 = 0; ie02 < ne02; ++ie02) {
2838
- for (int64_t ie03 = 0; ie03 < ne03; ++ie03) {
2839
- const sycl::half* src0_f16_shifted = src0_f16 + ((ie02*nb02 + ie03*nb03)/sizeof(sycl::half));
2840
- const sycl::half* src1_f16_shifted = src1_f16 + ie02*s12*r2 + ie03*s13*r3;
2841
- float* dst_shifted = dst_ddf + ((ie02*nb2*r2 + ie03*nb3*r3)/sizeof(float));
2842
- dnn_gemm(src1_f16_shifted, src0_f16_shifted, dst_shifted, r2*r3, 1);
2843
- }
2844
- }
2845
- }
2846
- }
2847
- else
2848
- #endif
2849
- {
2850
- if (r2 == 1 && r3 == 1 && ggml_is_contiguous_2(src0) && ggml_is_contiguous_2(src1)) {
2851
- // there is no broadcast and src0, src1 are contiguous across dims 2, 3
2852
- SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(*queue, oneapi::math::transpose::trans,
2853
- oneapi::math::transpose::nontrans, ne01, ne11, ne10, alpha,
2854
- src0_f16, dpct::library_data_t::real_half, nb01 / nb00, nb02 / nb00,
2855
- src1_f16, dpct::library_data_t::real_half, s11, s12, beta, dst_ddf,
2856
- mkl_data_type, ne0, ne1 * ne0, ne12 * ne13, mkl_compute_type)));
2857
- } else {
2858
- const int ne23 = ne12 * ne13;
2859
-
2860
- ggml_sycl_pool_alloc<const void *> ptrs_src(ctx.pool(), 2 * ne23);
2861
- ggml_sycl_pool_alloc<void *> ptrs_dst(ctx.pool(), 1 * ne23);
2862
- ggml_sycl_pool_alloc<matrix_info_t<float>> matrix_info(ctx.host_pool(), 1);
2863
-
2864
- sycl::range<3> block_dims(1, ne12, ne13);
2865
- queue->submit([&](sycl::handler & cgh) {
2866
- const void ** ptrs_src_get = ptrs_src.get();
2867
- void ** ptrs_dst_get = ptrs_dst.get();
2868
- size_t nb12_scaled = src1->type == GGML_TYPE_F16 ? nb12 : s12 * sizeof(sycl::half);
2869
- size_t nb13_scaled = src1->type == GGML_TYPE_F16 ? nb13 : s13 * sizeof(sycl::half);
2870
- cgh.parallel_for(sycl::nd_range<3>(block_dims, block_dims), [=](sycl::nd_item<3> item_ct1) {
2871
- k_compute_batched_ptrs(src0_f16, src1_f16, dst_ddf, ptrs_src_get, ptrs_dst_get, ne12, ne13, ne23, nb02,
2872
- nb03, nb12_scaled, nb13_scaled, nbd2, nbd3, r2, r3, item_ct1);
2873
- });
2874
- });
2875
-
2876
- SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(
2877
- *queue, oneapi::math::transpose::trans, oneapi::math::transpose::nontrans, ne01, ne11, ne10, alpha,
2878
- (const void **) (ptrs_src.get() + 0 * ne23), dpct::library_data_t::real_half, nb01 / nb00,
2879
- (const void **) (ptrs_src.get() + 1 * ne23), dpct::library_data_t::real_half, s11, beta,
2880
- (void **) (ptrs_dst.get() + 0 * ne23), mkl_data_type, ne0, ne23, mkl_compute_type, matrix_info.get())));
2881
- }
2882
- }
2883
- } catch (const sycl::exception & exc) {
2884
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl;
2885
- std::exit(1);
2886
- }
2887
-
2888
- enum class mul_mat_algo {
2889
- DMMV = 0,
2890
- MMVQ = 1,
2891
- MUL_MAT_SYCL = 2,
2892
- };
2893
-
2894
- inline bool ggml_sycl_supports_mmq(enum ggml_type type) {
2895
- // TODO: accuracy issues in MMQ
2896
- GGML_UNUSED(type);
2897
- return false;
2898
- }
2899
-
2900
- inline bool ggml_sycl_supports_reorder_mul_mat_sycl(enum ggml_type type) {
2901
- switch (type) {
2902
- case GGML_TYPE_Q4_0:
2903
- return true;
2904
- case GGML_TYPE_Q4_K:
2905
- return !g_ggml_sycl_prioritize_dmmv;
2906
- default:
2907
- return false;
2908
- }
2909
- }
2910
-
2911
- inline bool ggml_sycl_supports_reorder_dmmv(enum ggml_type type) {
2912
- switch (type) {
2913
- case GGML_TYPE_Q4_0:
2914
- return true;
2915
- default:
2916
- return false;
2917
- }
2918
- }
2919
-
2920
- inline bool ggml_sycl_supports_reorder_mmvq(enum ggml_type type) {
2921
- switch (type) {
2922
- case GGML_TYPE_Q4_0:
2923
- case GGML_TYPE_Q4_K:
2924
- return true;
2925
- default:
2926
- return false;
2927
- }
2928
- }
2929
-
2930
- static bool ggml_sycl_supports_dmmv(enum ggml_type type) {
2931
- switch (type) {
2932
- case GGML_TYPE_Q4_0:
2933
- case GGML_TYPE_Q4_1:
2934
- case GGML_TYPE_Q5_0:
2935
- case GGML_TYPE_Q5_1:
2936
- case GGML_TYPE_Q8_0:
2937
- case GGML_TYPE_Q2_K:
2938
- case GGML_TYPE_Q3_K:
2939
- case GGML_TYPE_Q4_K:
2940
- case GGML_TYPE_Q5_K:
2941
- case GGML_TYPE_Q6_K:
2942
- case GGML_TYPE_F16:
2943
- return true;
2944
- default:
2945
- return false;
2946
- }
2947
- }
2948
-
2949
- static void reorder_qw_q4_0(uint8_t * data_device, const int ncols, const int nrows, size_t size, size_t offset,
2950
- dpct::queue_ptr stream) {
2951
- auto * tmp_buf = sycl::malloc_shared<uint8_t>(size, *stream);
2952
- SYCL_CHECK(
2953
- CHECK_TRY_ERROR((*stream).memcpy(tmp_buf, data_device, size)
2954
- .wait()));
2955
- GGML_ASSERT((size % sizeof(block_q4_0) == 0));
2956
- GGML_ASSERT((offset % sizeof(block_q4_0) == 0));
2957
- int offset_blks = offset / sizeof(block_q4_0);
2958
- auto qs_ptr = data_device + offset_blks * QK4_0 / 2;
2959
- auto d_ptr = (sycl::half*)(qs_ptr + ncols * nrows / 2) + offset_blks;
2960
-
2961
- stream->parallel_for(
2962
- size / sizeof(block_q4_0),
2963
- [=](auto i) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
2964
- const block_q4_0* x = (const block_q4_0*)tmp_buf;
2965
- const int ib = i;
2966
-
2967
- for (int j = 0; j < QK4_0/2; j ++)
2968
- {
2969
- *(qs_ptr + ib * QK4_0 / 2 + j) = x[ib].qs[j];
2970
- }
2971
- *(d_ptr + ib) = x[ib].d;
2972
- }).wait_and_throw();
2973
-
2974
- sycl::free(tmp_buf, *stream);
2975
- }
2976
-
2977
- static void reorder_qw_q4_k(uint8_t * data_device, size_t size, size_t offset, dpct::queue_ptr stream) {
2978
- GGML_ASSERT(size % sizeof(block_q4_K) == 0);
2979
- GGML_ASSERT(offset % sizeof(block_q4_K) == 0);
2980
-
2981
- const int nblocks = size / sizeof(block_q4_K);
2982
-
2983
- auto * tmp_buf = sycl::malloc_shared<uint8_t>(size, *stream);
2984
- SYCL_CHECK(CHECK_TRY_ERROR((*stream).memcpy(tmp_buf, data_device, size).wait()));
2985
-
2986
- auto * qs_ptr = data_device;
2987
- auto * scales_ptr = qs_ptr + QK_K / 2 * nblocks;
2988
- auto * dm_ptr = (sycl::half2 *) (scales_ptr + K_SCALE_SIZE * nblocks);
2989
-
2990
- stream->parallel_for(nblocks, [=](auto i) {
2991
- const block_q4_K * x = (const block_q4_K *) tmp_buf;
2992
- const int ib = i;
2993
-
2994
- for (int j = 0; j < QK_K / 2; ++j) {
2995
- qs_ptr[ib * (QK_K / 2) + j] = x[ib].qs[j];
2996
- }
2997
-
2998
- for (int j = 0; j < K_SCALE_SIZE; ++j) {
2999
- scales_ptr[ib * K_SCALE_SIZE + j] = x[ib].scales[j];
3000
- }
3001
-
3002
- dm_ptr[ib] = x[ib].dm;
3003
- }).wait_and_throw();
3004
-
3005
- sycl::free(tmp_buf, *stream);
3006
- }
3007
-
3008
- static void reorder_qw(const ggml_tensor * src0, dpct::queue_ptr stream) {
3009
- uint8_t * data_device = (uint8_t *) src0->data;
3010
- size_t ncols = src0->ne[0];
3011
- size_t nrows = src0->ne[1];
3012
- size_t size = ggml_nbytes(src0);
3013
-
3014
- switch (src0->type) {
3015
- case GGML_TYPE_Q4_0:
3016
- reorder_qw_q4_0(data_device, ncols, nrows, size, 0, stream);
3017
- break;
3018
- case GGML_TYPE_Q4_K:
3019
- reorder_qw_q4_k(data_device, size, 0, stream);
3020
- break;
3021
- default:
3022
- GGML_ABORT("reorder_qw() called with unsupported type");
3023
- break;
3024
- }
3025
- }
3026
-
3027
- static bool should_reorder_tensor(ggml_backend_sycl_context& ctx, const ggml_tensor * dst) {
3028
- return !g_ggml_sycl_disable_optimize && //allow optimize, controlled by $GGML_SYCL_DISABLE_OPT
3029
- ctx.opt_feature.reorder && //allow this device due to good perf, skip the devices with bad perf.
3030
- dst->op == GGML_OP_MUL_MAT && //limit to some supported cases of Q4_0, to do for more cases.
3031
- dst->src[1]->ne[1]==1 && dst->src[1]->ne[2]==1 && dst->src[1]->ne[3]==1;
3032
- }
3033
-
3034
- static void opt_for_reorder(ggml_backend_sycl_context * ctx, const ggml_tensor * src0, const ggml_tensor * /* src1 */,
3035
- ggml_tensor * dst, mul_mat_algo mm_algorithm) {
3036
- if (!should_reorder_tensor(*ctx, dst)) {
3037
- return;
3038
- }
3039
-
3040
- ggml_tensor_extra_gpu * extra = static_cast<ggml_tensor_extra_gpu *>(src0->extra);
3041
- if (!extra || extra->optimized_feature.reorder) {
3042
- return; // Skip permutations and already reordered tensors
3043
- }
3044
-
3045
- switch (mm_algorithm) {
3046
- case mul_mat_algo::DMMV:
3047
- if (!ggml_sycl_supports_reorder_dmmv(src0->type)) {
3048
- return;
3049
- }
3050
- break;
3051
- case mul_mat_algo::MMVQ:
3052
- if (!ggml_sycl_supports_reorder_mmvq(src0->type)) {
3053
- return;
3054
- }
3055
- break;
3056
- case mul_mat_algo::MUL_MAT_SYCL:
3057
- if (!ggml_sycl_supports_reorder_mul_mat_sycl(src0->type)) {
3058
- return;
3059
- }
3060
- break;
3061
- }
3062
-
3063
- reorder_qw(src0, ctx->stream());
3064
- extra->optimized_feature.reorder = true; // Used to decode/dequan in next steps and avoid re-reordering
3065
- }
3066
-
3067
-
3068
- static bool can_use_dequantize_mul_mat_vec(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
3069
- return ggml_sycl_supports_dmmv(src0->type) && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32 &&
3070
- src0->ne[0] % GGML_SYCL_DMMV_X == 0 && src1->ne[1] == 1;
3071
- }
3072
-
3073
- static bool can_use_mul_mat_vec_q(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
3074
- return ggml_is_quantized(src0->type) && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32 &&
3075
- src1->ne[1] <= MMVQ_MAX_BATCH_SIZE;
3076
- }
3077
-
3078
- static void ggml_sycl_mul_mat(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
3079
- const bool split = ggml_backend_buffer_is_sycl_split(src0->buffer);
3080
- int64_t min_compute_capability = INT_MAX;
3081
-
3082
- if (split) {
3083
- ggml_backend_sycl_split_buffer_type_context * buft_ctx =
3084
- (ggml_backend_sycl_split_buffer_type_context *) src0->buffer->buft->context;
3085
- auto & tensor_split = buft_ctx->tensor_split;
3086
- for (int id = 0; id < ggml_sycl_info().device_count; ++id) {
3087
- // skip devices that are not going to do any work:
3088
- if (tensor_split[id] >= (id + 1 < ggml_sycl_info().device_count ? tensor_split[id + 1] : 1.0f)) {
3089
- continue;
3090
- }
3091
-
3092
- if (min_compute_capability > ggml_sycl_info().devices[id].cc) {
3093
- min_compute_capability = ggml_sycl_info().devices[id].cc;
3094
- }
3095
- }
3096
- } else {
3097
- min_compute_capability = ggml_sycl_info().devices[ctx.device].cc;
3098
- }
3099
-
3100
- // check data types and tensor shapes for custom matrix multiplication kernels:
3101
- bool use_dequantize_mul_mat_vec = can_use_dequantize_mul_mat_vec(src0, src1, dst);
3102
-
3103
- bool use_mul_mat_vec_q = can_use_mul_mat_vec_q(src0, src1, dst);
3104
-
3105
- bool use_mul_mat_q = ggml_sycl_supports_mmq(src0->type)
3106
- && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32;
3107
-
3108
- // mmvq and mmq need the __dp4a instruction which is available for gen12+
3109
- // Workaround in https://github.com/ggerganov/llama.cpp/commit/95f84d5ce8b449a9b16009434aca800df504a02e
3110
- use_mul_mat_q = use_mul_mat_q && (src0->type != GGML_TYPE_IQ2_XXS);
3111
- #ifdef SYCL_USE_XMX
3112
- use_mul_mat_q = use_mul_mat_q && (src1->ne[1] <= MMQ_MAX_BATCH_SIZE);
3113
- #endif // SYCL_USE_XMX
3114
-
3115
-
3116
- // mmvq path is faster in the CUDA backend.
3117
- if (!g_ggml_sycl_prioritize_dmmv && (ctx.stream()->get_backend() == sycl::backend::ext_oneapi_cuda
3118
- // Dispatch becomes obscure with the reorder, MMVQ when the reorder optimization
3119
- // is enabled takes precedence over DMMV, the current if-else implementation
3120
- // requires disabling DMMV if both conditions are met
3121
- || (should_reorder_tensor(ctx, dst) && ggml_sycl_supports_reorder_mmvq(src0->type)))) {
3122
- use_dequantize_mul_mat_vec = use_dequantize_mul_mat_vec && !use_mul_mat_vec_q;
3123
- }
3124
-
3125
- if (!split && src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
3126
- // TODO: Refactor and cleanup of mul mat dispatching.
3127
- if (src0->ne[3] == 1 && src1->ne[3] == 1) {
3128
- // KQ single-batch
3129
- // mmv p021 was specific for these dimensions
3130
- ggml_sycl_mul_mat_vec_p021(ctx, src0, src1, dst);
3131
- } else {
3132
- // The kernel from the if path is faster for that specific case, but does not support all mul mats.
3133
- ggml_sycl_mul_mat_batched_sycl(ctx, src0, src1, dst);
3134
- }
3135
- } else if (!split && src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && ggml_is_contiguous(src1) && !ggml_is_transposed(src1) && src1->ne[1] == 1) {
3136
- // KQV single-batch
3137
- ggml_sycl_mul_mat_vec_nc(ctx, src0, src1, dst);
3138
- } else if (!split && src0->type == GGML_TYPE_F16 && !ggml_is_transposed(src0) && !ggml_is_transposed(src1) && src1->ne[2]*src1->ne[3] > 1) {
3139
- // KQ + KQV multi-batch
3140
- ggml_sycl_mul_mat_batched_sycl(ctx, src0, src1, dst);
3141
- } else if (use_dequantize_mul_mat_vec) {
3142
- constexpr bool convert_src1_to_q8_1 = false;
3143
- opt_for_reorder(&ctx, src0, src1, dst, mul_mat_algo::DMMV);
3144
- ggml_sycl_op_mul_mat(ctx, src0, src1, dst, ggml_sycl_op_dequantize_mul_mat_vec, convert_src1_to_q8_1);
3145
- } else if (use_mul_mat_vec_q) {
3146
- constexpr bool convert_src1_to_q8_1 = true;
3147
- opt_for_reorder(&ctx, src0, src1, dst, mul_mat_algo::MMVQ);
3148
- ggml_sycl_op_mul_mat(ctx, src0, src1, dst, ggml_sycl_op_mul_mat_vec_q, convert_src1_to_q8_1);
3149
- } else if (use_mul_mat_q) {
3150
- constexpr bool convert_src1_to_q8_1 = true;
3151
- ggml_sycl_op_mul_mat(ctx, src0, src1, dst, ggml_sycl_op_mul_mat_q, convert_src1_to_q8_1);
3152
- } else {
3153
- constexpr bool convert_src1_to_q8_1 = false;
3154
- ggml_sycl_op_mul_mat(ctx, src0, src1, dst, ggml_sycl_op_mul_mat_sycl, convert_src1_to_q8_1);
3155
- }
3156
- GGML_SYCL_DEBUG("call %s done\n", __func__);
3157
- }
3158
-
3159
-
3160
- struct mmid_row_mapping {
3161
- int32_t i1;
3162
- int32_t i2;
3163
- };
3164
-
3165
- __dpct_inline__ static void k_copy_src1_to_contiguous(
3166
- const char *__restrict__ src1_original, char *__restrict__ src1_contiguous,
3167
- int *__restrict__ cur_src1_row, mmid_row_mapping *__restrict__ row_mapping,
3168
- const char *__restrict ids, int64_t i02, size_t ids_nb1, size_t ids_nb0,
3169
- int64_t ne11, int64_t ne10, size_t nb11, size_t nb12,
3170
- const sycl::nd_item<3> &item_ct1, int &src1_row) {
3171
- int32_t iid1 = item_ct1.get_group(2);
3172
- int32_t id = item_ct1.get_group(1);
3173
-
3174
- const int32_t row_id_i = *(const int32_t *) (ids + iid1*ids_nb1 + id*ids_nb0);
3175
-
3176
- if (row_id_i != i02) {
3177
- return;
3178
- }
3179
-
3180
- const int64_t i11 = id % ne11;
3181
- const int64_t i12 = iid1;
3182
-
3183
- if (item_ct1.get_local_id(2) == 0) {
3184
- src1_row =
3185
- dpct::atomic_fetch_add<sycl::access::address_space::generic_space>(
3186
- cur_src1_row, 1);
3187
- row_mapping[src1_row] = {id, iid1};
3188
- }
3189
- /*
3190
- DPCT1065:194: Consider replacing sycl::nd_item::barrier() with
3191
- sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better
3192
- performance if there is no access to global memory.
3193
- */
3194
- item_ct1.barrier();
3195
-
3196
- const float * src1_row_original = (const float *)(src1_original + i11*nb11 + i12*nb12);
3197
- float * src1_row_contiguous = (float *)(src1_contiguous + src1_row*nb11);
3198
-
3199
- #pragma unroll
3200
- for (int i = item_ct1.get_local_id(2); i < ne10;
3201
- i += item_ct1.get_local_range(2)) {
3202
- src1_row_contiguous[i] = src1_row_original[i];
3203
- }
3204
- }
3205
-
3206
- __dpct_inline__ static void k_copy_dst_from_contiguous(
3207
- char *__restrict__ dst_original, const char *__restrict__ dst_contiguous,
3208
- const mmid_row_mapping *__restrict__ row_mapping, int64_t ne0, size_t nb1,
3209
- size_t nb2, const sycl::nd_item<3> &item_ct1) {
3210
- int32_t i = item_ct1.get_group(2);
3211
-
3212
- const int32_t i1 = row_mapping[i].i1;
3213
- const int32_t i2 = row_mapping[i].i2;
3214
-
3215
- const float * dst_row_contiguous = (const float *)(dst_contiguous + i*nb1);
3216
- float * dst_row_original = (float *)(dst_original + i1*nb1 + i2*nb2);
3217
-
3218
- #pragma unroll
3219
- for (int j = item_ct1.get_local_id(2); j < ne0;
3220
- j += item_ct1.get_local_range(2)) {
3221
- dst_row_original[j] = dst_row_contiguous[j];
3222
- }
3223
- }
3224
-
3225
- static void ggml_sycl_mul_mat_id(ggml_backend_sycl_context & ctx,
3226
- ggml_tensor *dst) try {
3227
- const ggml_tensor *src0 = dst->src[0];
3228
- const ggml_tensor *src1 = dst->src[1];
3229
- GGML_ASSERT(!ggml_backend_buffer_is_sycl_split(src0->buffer) && "mul_mat_id does not support split buffers");
3230
-
3231
- const ggml_tensor *ids = dst->src[2];
3232
- GGML_TENSOR_BINARY_OP_LOCALS
3233
-
3234
- const queue_ptr stream = ctx.stream();
3235
-
3236
- const int64_t n_as = ne02;
3237
- const int64_t n_ids = ids->ne[0];
3238
-
3239
- std::vector<char> ids_host(ggml_nbytes(ids));
3240
- const char * ids_dev = (const char *) ids->data;
3241
-
3242
- SYCL_CHECK(CHECK_TRY_ERROR(
3243
- stream->memcpy(ids_host.data(), ids_dev, ggml_nbytes(ids))));
3244
- SYCL_CHECK(CHECK_TRY_ERROR(stream->wait()));
3245
-
3246
- ggml_tensor src0_row = *src0;
3247
- ggml_tensor src1_row = *src1;
3248
- ggml_tensor dst_row = *dst;
3249
-
3250
- char *src0_original = (char *)src0->data;
3251
- char *src1_original = (char *)src1->data;
3252
- char *dst_original = (char *)dst->data;
3253
-
3254
- src0_row.ne[2] = 1;
3255
- src0_row.ne[3] = 1;
3256
- src0_row.nb[3] = nb02;
3257
-
3258
- src1_row.ne[1] = 1;
3259
- src1_row.ne[2] = 1;
3260
- src1_row.ne[3] = 1;
3261
- src1_row.nb[2] = nb11;
3262
- src1_row.nb[3] = nb11;
3263
-
3264
- dst_row.ne[1] = 1;
3265
- dst_row.ne[2] = 1;
3266
- dst_row.ne[3] = 1;
3267
- dst_row.nb[2] = nb1;
3268
- dst_row.nb[3] = nb1;
3269
- if (ne12 == 1) {
3270
- for (int64_t iid1 = 0; iid1 < ids->ne[1]; iid1++) {
3271
- for (int64_t id = 0; id < n_ids; id++) {
3272
- const int32_t i02 = *(const int32_t *) (ids_host.data() + iid1*ids->nb[1] + id*ids->nb[0]);
3273
- GGML_ASSERT(i02 >= 0 && i02 < n_as);
3274
-
3275
- const int64_t i11 = id % ne11;
3276
- const int64_t i12 = iid1;
3277
-
3278
- const int64_t i1 = id;
3279
- const int64_t i2 = i12;
3280
-
3281
- src0_row.data = src0_original + i02*nb02;
3282
- src1_row.data = src1_original + i11*nb11 + i12*nb12;
3283
- dst_row.data = dst_original + i1*nb1 + i2*nb2;
3284
-
3285
- ggml_sycl_mul_mat(ctx, &src0_row, &src1_row, &dst_row);
3286
- }
3287
- }
3288
- } else {
3289
- ggml_sycl_pool_alloc<char> src1_contiguous(ctx.pool(), sizeof(float)*ggml_nelements(src1));
3290
- ggml_sycl_pool_alloc<char> dst_contiguous(ctx.pool(), sizeof(float)*ggml_nelements(dst));
3291
-
3292
- src1_row.data = src1_contiguous.get();
3293
- dst_row.data = dst_contiguous.get();
3294
-
3295
- for (int64_t i02 = 0; i02 < n_as; i02++) {
3296
- int64_t num_src1_rows = 0;
3297
- for (int64_t iid1 = 0; iid1 < ids->ne[1]; iid1++) {
3298
- for (int64_t id = 0; id < n_ids; id++) {
3299
- const int32_t row_id_i = *(const int32_t *) (ids_host.data() + iid1*ids->nb[1] + id*ids->nb[0]);
3300
-
3301
- GGML_ASSERT(row_id_i >= 0 && row_id_i < n_as);
3302
-
3303
- if (row_id_i != i02) {
3304
- continue;
3305
- }
3306
-
3307
- num_src1_rows++;
3308
- }
3309
- }
3310
-
3311
- if (num_src1_rows == 0) {
3312
- continue;
3313
- }
3314
-
3315
-
3316
- ggml_sycl_pool_alloc<int> dev_cur_src1_row(ctx.pool(), 1);
3317
- ggml_sycl_pool_alloc<mmid_row_mapping> dev_row_mapping(ctx.pool(), num_src1_rows);
3318
- SYCL_CHECK(CHECK_TRY_ERROR(
3319
- stream->memset(dev_cur_src1_row.get(), 0, sizeof(int))));
3320
-
3321
- {
3322
- sycl::range<3> block_dims(1, 1, std::min((unsigned int)ne10, 768u));
3323
- sycl::range<3> grid_dims(1, n_ids, ids->ne[1]);
3324
- stream->submit([&](sycl::handler &cgh) {
3325
- sycl::local_accessor<int, 0> src1_row_acc(cgh);
3326
-
3327
- char *__restrict src1_contiguous_get =
3328
- src1_contiguous.get();
3329
- int *__restrict dev_cur_src1_row_get =
3330
- dev_cur_src1_row.get();
3331
- mmid_row_mapping *__restrict dev_row_mapping_get =
3332
- dev_row_mapping.get();
3333
- size_t ids_nb_ct6 = ids->nb[1];
3334
- size_t ids_nb_ct7 = ids->nb[0];
3335
-
3336
- cgh.parallel_for(
3337
- sycl::nd_range<3>(grid_dims * block_dims, block_dims),
3338
- [=](sycl::nd_item<3> item_ct1) {
3339
- k_copy_src1_to_contiguous(
3340
- src1_original, src1_contiguous_get,
3341
- dev_cur_src1_row_get,
3342
- dev_row_mapping_get, ids_dev, i02,
3343
- ids_nb_ct6, ids_nb_ct7, ne11, ne10, nb11, nb12,
3344
- item_ct1, src1_row_acc);
3345
- });
3346
- });
3347
- }
3348
-
3349
- src0_row.data = src0_original + i02*nb02;
3350
-
3351
- GGML_ASSERT(nb11 == sizeof(float)*ne10);
3352
- GGML_ASSERT(nb1 == sizeof(float)*ne0);
3353
- src1_row.ne[1] = num_src1_rows;
3354
-
3355
- src1_row.nb[1] = nb11;
3356
- src1_row.nb[2] = num_src1_rows*nb11;
3357
- src1_row.nb[3] = num_src1_rows*nb11;
3358
-
3359
- dst_row.ne[1] = num_src1_rows;
3360
- dst_row.nb[1] = nb1;
3361
- dst_row.nb[2] = num_src1_rows*nb1;
3362
- dst_row.nb[3] = num_src1_rows*nb1;
3363
-
3364
- ggml_sycl_mul_mat(ctx, &src0_row, &src1_row, &dst_row);
3365
-
3366
- {
3367
- sycl::range<3> block_dims(1, 1, std::min((unsigned int)ne0, 768u));
3368
- sycl::range<3> grid_dims(1, 1, num_src1_rows);
3369
- stream->submit([&](sycl::handler &cgh) {
3370
- const char *__restrict dst_contiguous_get =
3371
- dst_contiguous.get();
3372
- const mmid_row_mapping *__restrict dev_row_mapping_get =
3373
- dev_row_mapping.get();
3374
-
3375
- cgh.parallel_for(
3376
- sycl::nd_range<3>(grid_dims * block_dims, block_dims),
3377
- [=](sycl::nd_item<3> item_ct1) {
3378
- k_copy_dst_from_contiguous(dst_original,
3379
- dst_contiguous_get,
3380
- dev_row_mapping_get,
3381
- ne0, nb1, nb2, item_ct1);
3382
- });
3383
- });
3384
- }
3385
- }
3386
- }
3387
- }
3388
- catch (sycl::exception const &exc) {
3389
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
3390
- << ", line:" << __LINE__ << std::endl;
3391
- std::exit(1);
3392
- }
3393
-
3394
- static void ggml_sycl_scale(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
3395
- ggml_sycl_op_scale(ctx, dst);
3396
- }
3397
-
3398
- static void ggml_sycl_diag_mask_inf(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
3399
- ggml_sycl_op_diag_mask_inf(ctx, dst);
3400
- }
3401
-
3402
- static void ggml_sycl_pool2d(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
3403
- ggml_sycl_op_pool2d(ctx, dst);
3404
- }
3405
-
3406
- static void ggml_sycl_im2col(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
3407
- ggml_sycl_op_im2col(ctx, dst);
3408
- }
3409
-
3410
- static void ggml_sycl_sum(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
3411
- GGML_ASSERT(ggml_is_contiguous(dst->src[0]));
3412
- ggml_sycl_op_sum(ctx, dst);
3413
- }
3414
-
3415
- static void ggml_sycl_sum_rows(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
3416
- GGML_ASSERT(ggml_is_contiguous(dst->src[0]));
3417
- ggml_sycl_op_sum_rows(ctx, dst);
3418
- }
3419
-
3420
- static void ggml_sycl_argsort(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
3421
- GGML_ASSERT(ggml_is_contiguous(dst->src[0]));
3422
- ggml_sycl_op_argsort(ctx, dst);
3423
- }
3424
-
3425
- static void ggml_sycl_argmax(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
3426
- GGML_ASSERT(ggml_is_contiguous(dst->src[0]));
3427
- ggml_sycl_op_argmax(ctx, dst);
3428
- }
3429
-
3430
-
3431
- static void ggml_sycl_set_main_device(const int main_device) try {
3432
- if (dpct::get_current_device_id() == static_cast<unsigned int> (main_device)) {
3433
- return;
3434
- }
3435
- check_allow_gpu_index(main_device);
3436
- dpct::select_device(main_device);
3437
-
3438
- if (g_ggml_sycl_debug) {
3439
- dpct::device_info prop;
3440
- SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
3441
- prop, dpct::dev_mgr::instance().get_device(main_device))));
3442
- GGML_LOG_INFO("Using device %d (%s) as main device\n",
3443
- main_device, prop.get_name());
3444
- }
3445
- }
3446
- catch (sycl::exception const &exc) {
3447
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
3448
- << ", line:" << __LINE__ << std::endl;
3449
- std::exit(1);
3450
- }
3451
-
3452
- static bool ggml_sycl_compute_forward(ggml_backend_sycl_context & ctx, struct ggml_tensor * dst) try {
3453
- if (!g_sycl_loaded) return false;
3454
-
3455
- if (dst->src[0] != nullptr && ggml_backend_buffer_is_sycl_split(dst->src[0]->buffer)) {
3456
- ggml_sycl_set_peer_access(dst->src[1]->ne[1], ctx.device);
3457
- }
3458
-
3459
- switch (dst->op) {
3460
- case GGML_OP_ARGMAX:
3461
- ggml_sycl_argmax(ctx, dst);
3462
- break;
3463
- case GGML_OP_CONV_TRANSPOSE_1D:
3464
- ggml_sycl_op_conv_transpose_1d(ctx, dst);
3465
- break;
3466
- case GGML_OP_REPEAT:
3467
- ggml_sycl_repeat(ctx, dst);
3468
- break;
3469
- case GGML_OP_GET_ROWS:
3470
- ggml_sycl_get_rows(ctx, dst);
3471
- break;
3472
- case GGML_OP_DUP:
3473
- ggml_sycl_dup(ctx, dst);
3474
- break;
3475
- case GGML_OP_ADD:
3476
- case GGML_OP_ADD1: // TODO: more efficient implementation
3477
- ggml_sycl_add(ctx, dst);
3478
- break;
3479
- case GGML_OP_SUB:
3480
- ggml_sycl_sub(ctx, dst);
3481
- break;
3482
- case GGML_OP_ACC:
3483
- ggml_sycl_acc(ctx, dst);
3484
- break;
3485
- case GGML_OP_MUL:
3486
- ggml_sycl_mul(ctx, dst);
3487
- break;
3488
- case GGML_OP_LOG:
3489
- ggml_sycl_log(ctx, dst);
3490
- break;
3491
- case GGML_OP_DIV:
3492
- ggml_sycl_div(ctx, dst);
3493
- break;
3494
- case GGML_OP_UNARY:
3495
- switch (ggml_get_unary_op(dst)) {
3496
- case GGML_UNARY_OP_NEG:
3497
- ggml_sycl_neg(ctx, dst);
3498
- break;
3499
- case GGML_UNARY_OP_STEP:
3500
- ggml_sycl_step(ctx, dst);
3501
- break;
3502
- case GGML_UNARY_OP_GELU:
3503
- ggml_sycl_gelu(ctx, dst);
3504
- break;
3505
- case GGML_UNARY_OP_SILU:
3506
- ggml_sycl_silu(ctx, dst);
3507
- break;
3508
- case GGML_UNARY_OP_GELU_QUICK:
3509
- ggml_sycl_gelu_quick(ctx, dst);
3510
- break;
3511
- case GGML_UNARY_OP_TANH:
3512
- ggml_sycl_tanh(ctx, dst);
3513
- break;
3514
- case GGML_UNARY_OP_RELU:
3515
- ggml_sycl_relu(ctx, dst);
3516
- break;
3517
- case GGML_UNARY_OP_SIGMOID:
3518
- ggml_sycl_sigmoid(ctx, dst);
3519
- break;
3520
- case GGML_UNARY_OP_HARDSIGMOID:
3521
- ggml_sycl_hardsigmoid(ctx, dst);
3522
- break;
3523
- case GGML_UNARY_OP_HARDSWISH:
3524
- ggml_sycl_hardswish(ctx, dst);
3525
- break;
3526
- case GGML_UNARY_OP_EXP:
3527
- ggml_sycl_exp(ctx, dst);
3528
- break;
3529
- case GGML_UNARY_OP_SGN:
3530
- ggml_sycl_sgn(ctx, dst);
3531
- break;
3532
- case GGML_UNARY_OP_ABS:
3533
- ggml_sycl_abs(ctx, dst);
3534
- break;
3535
- case GGML_UNARY_OP_ELU:
3536
- ggml_sycl_elu(ctx, dst);
3537
- break;
3538
- default:
3539
- return false;
3540
- }
3541
- break;
3542
- case GGML_OP_NORM:
3543
- ggml_sycl_norm(ctx, dst);
3544
- break;
3545
- case GGML_OP_GROUP_NORM:
3546
- ggml_sycl_group_norm(ctx, dst);
3547
- break;
3548
- case GGML_OP_CONCAT:
3549
- ggml_sycl_op_concat(ctx, dst);
3550
- break;
3551
- case GGML_OP_UPSCALE:
3552
- ggml_sycl_upscale(ctx, dst);
3553
- break;
3554
- case GGML_OP_PAD:
3555
- ggml_sycl_pad(ctx, dst);
3556
- break;
3557
- case GGML_OP_LEAKY_RELU:
3558
- ggml_sycl_leaky_relu(ctx, dst);
3559
- break;
3560
- case GGML_OP_RMS_NORM:
3561
- ggml_sycl_rms_norm(ctx, dst);
3562
- break;
3563
- case GGML_OP_L2_NORM:
3564
- ggml_sycl_l2_norm(ctx, dst);
3565
- break;
3566
- case GGML_OP_MUL_MAT:
3567
- if (dst->src[0]->ne[3] != dst->src[1]->ne[3]) {
3568
- return false;
3569
- }
3570
- /* ggml_sycl_mul_mat_id is dependent on ggml_sycl_mul_mat */
3571
- ggml_sycl_mul_mat(ctx, dst->src[0], dst->src[1], dst);
3572
- break;
3573
- case GGML_OP_MUL_MAT_ID:
3574
- if (dst->src[0]->ne[3] != dst->src[1]->ne[3]) {
3575
- return false;
3576
- }
3577
- ggml_sycl_mul_mat_id(ctx, dst);
3578
- break;
3579
- case GGML_OP_OUT_PROD:
3580
- ggml_sycl_op_out_prod(ctx, dst);
3581
- break;
3582
- case GGML_OP_SCALE:
3583
- ggml_sycl_scale(ctx, dst);
3584
- break;
3585
- case GGML_OP_SQR:
3586
- ggml_sycl_sqr(ctx, dst);
3587
- break;
3588
- case GGML_OP_SQRT:
3589
- ggml_sycl_sqrt(ctx, dst);
3590
- break;
3591
- case GGML_OP_SIN:
3592
- ggml_sycl_sin(ctx, dst);
3593
- break;
3594
- case GGML_OP_COS:
3595
- ggml_sycl_cos(ctx, dst);
3596
- break;
3597
- case GGML_OP_CLAMP:
3598
- ggml_sycl_clamp(ctx, dst);
3599
- break;
3600
- case GGML_OP_CPY:
3601
- ggml_sycl_cpy(ctx, dst->src[0], dst->src[1]);
3602
- break;
3603
- case GGML_OP_CONT:
3604
- ggml_sycl_dup(ctx, dst);
3605
- break;
3606
- case GGML_OP_NONE:
3607
- case GGML_OP_RESHAPE:
3608
- case GGML_OP_VIEW:
3609
- case GGML_OP_PERMUTE:
3610
- case GGML_OP_TRANSPOSE:
3611
- GGML_SYCL_DEBUG("%s: Tensor NO-OP\n", __func__);
3612
- break;
3613
- case GGML_OP_DIAG_MASK_INF:
3614
- ggml_sycl_diag_mask_inf(ctx, dst);
3615
- break;
3616
- case GGML_OP_SOFT_MAX:
3617
- ggml_sycl_op_soft_max(ctx, dst);
3618
- break;
3619
- case GGML_OP_ROPE:
3620
- ggml_sycl_rope(ctx, dst);
3621
- break;
3622
- case GGML_OP_IM2COL:
3623
- ggml_sycl_im2col(ctx, dst);
3624
- break;
3625
- case GGML_OP_POOL_2D:
3626
- ggml_sycl_pool2d(ctx, dst);
3627
- break;
3628
- case GGML_OP_SUM:
3629
- ggml_sycl_sum(ctx, dst);
3630
- break;
3631
- case GGML_OP_SUM_ROWS:
3632
- ggml_sycl_sum_rows(ctx, dst);
3633
- break;
3634
- case GGML_OP_ARGSORT:
3635
- ggml_sycl_argsort(ctx, dst);
3636
- break;
3637
- case GGML_OP_TIMESTEP_EMBEDDING:
3638
- ggml_sycl_op_timestep_embedding(ctx, dst);
3639
- break;
3640
- case GGML_OP_RWKV_WKV6:
3641
- ggml_sycl_op_rwkv_wkv6(ctx, dst);
3642
- break;
3643
- case GGML_OP_RWKV_WKV7:
3644
- ggml_sycl_op_rwkv_wkv7(ctx, dst);
3645
- break;
3646
- case GGML_OP_GATED_LINEAR_ATTN:
3647
- ggml_sycl_op_gated_linear_attn(ctx, dst);
3648
- break;
3649
- default:
3650
- return false;
3651
- }
3652
-
3653
- return true;
3654
- } catch (sycl::exception & e) {
3655
- std::cerr << e.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl;
3656
- std::exit(1);
3657
- }
3658
-
3659
- GGML_API void ggml_backend_sycl_get_device_description(int device, char *description,
3660
- size_t description_size) try {
3661
- GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_description\n");
3662
- dpct::device_info prop;
3663
- SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
3664
- prop, dpct::dev_mgr::instance().get_device(device))));
3665
- snprintf(description, description_size, "%s", prop.get_name());
3666
- }
3667
- catch (sycl::exception const &exc) {
3668
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
3669
- << ", line:" << __LINE__ << std::endl;
3670
- std::exit(1);
3671
- }
3672
-
3673
- void ggml_backend_sycl_get_device_memory(int device, size_t *free,
3674
- size_t *total) try {
3675
- GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_memory\n");
3676
- ggml_sycl_set_device(device);
3677
-
3678
- /*
3679
- DPCT1009:218: SYCL uses exceptions to report errors and does not use the
3680
- error codes. The original code was commented out and a warning string was
3681
- inserted. You need to rewrite this code.
3682
- */
3683
- /*
3684
- DPCT1106:217: 'cudaMemGetInfo' was migrated with the Intel extensions for
3685
- device information which may not be supported by all compilers or runtimes.
3686
- You may need to adjust the code.
3687
- */
3688
- SYCL_CHECK(CHECK_TRY_ERROR(
3689
- dpct::dev_mgr::instance().get_device(device).get_memory_info(*free, *total)));
3690
- }
3691
- catch (sycl::exception const &exc) {
3692
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
3693
- << ", line:" << __LINE__ << std::endl;
3694
- std::exit(1);
3695
- }
3696
-
3697
- ////////////////////////////////////////////////////////////////////////////////
3698
-
3699
- // backend
3700
-
3701
- static const char * ggml_backend_sycl_get_name(ggml_backend_t backend) {
3702
-
3703
- ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
3704
-
3705
- return sycl_ctx->name.c_str();
3706
- }
3707
-
3708
- static void ggml_backend_sycl_free(ggml_backend_t backend) {
3709
- ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
3710
-
3711
- delete sycl_ctx;
3712
- delete backend;
3713
- }
3714
-
3715
- static void ggml_backend_sycl_set_tensor_async(ggml_backend_t backend,
3716
- ggml_tensor *tensor,
3717
- const void *data, size_t offset,
3718
- size_t size) try {
3719
- ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
3720
- ggml_backend_buffer_t buf = tensor->view_src ? tensor->view_src->buffer : tensor->buffer;
3721
-
3722
- GGML_ASSERT(buf->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
3723
- const queue_ptr stream = sycl_ctx->stream(sycl_ctx->device, 0);
3724
- SYCL_CHECK(CHECK_TRY_ERROR(
3725
- (stream)->memcpy((char *)tensor->data + offset, data, size)));
3726
- }
3727
- catch (sycl::exception const &exc) {
3728
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
3729
- << ", line:" << __LINE__ << std::endl;
3730
- std::exit(1);
3731
- }
3732
-
3733
- static void ggml_backend_sycl_get_tensor_async(ggml_backend_t backend,
3734
- const ggml_tensor *tensor,
3735
- void *data, size_t offset,
3736
- size_t size) try {
3737
- ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
3738
- ggml_backend_buffer_t buf = tensor->view_src ? tensor->view_src->buffer : tensor->buffer;
3739
-
3740
- GGML_ASSERT(buf->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
3741
- const queue_ptr stream = sycl_ctx->stream(sycl_ctx->device, 0);
3742
- SYCL_CHECK(CHECK_TRY_ERROR((stream)->memcpy(
3743
- data, (const char *)tensor->data + offset, size)));
3744
- }
3745
- catch (sycl::exception const &exc) {
3746
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
3747
- << ", line:" << __LINE__ << std::endl;
3748
- std::exit(1);
3749
- }
3750
-
3751
- static bool ggml_backend_sycl_cpy_tensor_async(ggml_backend_t backend,
3752
- const ggml_tensor *src,
3753
- ggml_tensor *dst) try {
3754
- ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
3755
- if (dst->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && ggml_backend_buffer_is_sycl(src->buffer)) {
3756
- /*
3757
- DPCT1009:215: SYCL uses exceptions to report errors and does not use the
3758
- error codes. The original code was commented out and a warning string
3759
- was inserted. You need to rewrite this code.
3760
- */
3761
- const queue_ptr stream = sycl_ctx->stream(sycl_ctx->device, 0);
3762
- SYCL_CHECK(CHECK_TRY_ERROR((stream)->memcpy(
3763
- dst->data, src->data, ggml_nbytes(dst))));
3764
- return true;
3765
- }
3766
-
3767
- return false;
3768
- }
3769
- catch (sycl::exception const &exc) {
3770
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
3771
- << ", line:" << __LINE__ << std::endl;
3772
- std::exit(1);
3773
- }
3774
-
3775
- static void ggml_backend_sycl_synchronize(ggml_backend_t backend) try {
3776
- ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
3777
- const queue_ptr stream = sycl_ctx->stream(sycl_ctx->device, 0);
3778
- SYCL_CHECK(CHECK_TRY_ERROR((stream)->wait()));
3779
-
3780
- GGML_UNUSED(backend);
3781
- }
3782
- catch (sycl::exception const &exc) {
3783
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
3784
- << ", line:" << __LINE__ << std::endl;
3785
- std::exit(1);
3786
- }
3787
-
3788
- static void ggml_backend_sycl_graph_compute_impl(ggml_backend_sycl_context * sycl_ctx, ggml_cgraph * cgraph) {
3789
- ggml_sycl_set_main_device(sycl_ctx->device);
3790
-
3791
- for (int i = 0; i < cgraph->n_nodes; i++) {
3792
- ggml_tensor * node = cgraph->nodes[i];
3793
- if (ggml_is_empty(node) || node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE || node->op == GGML_OP_NONE) {
3794
- continue;
3795
- }
3796
- #ifndef NDEBUG
3797
- assert(node->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
3798
- for (int j = 0; j < GGML_MAX_SRC; j++) {
3799
- if (node->src[j] != nullptr) {
3800
- assert(node->src[j]->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
3801
- }
3802
- }
3803
- #endif
3804
- bool ok = ggml_sycl_compute_forward(*sycl_ctx, node);
3805
- if (!ok) {
3806
- GGML_LOG_ERROR("%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
3807
- }
3808
- GGML_ASSERT(ok);
3809
- }
3810
- }
3811
-
3812
- #ifdef GGML_SYCL_GRAPH
3813
- static bool check_graph_compatibility(ggml_cgraph * cgraph) {
3814
- if (ggml_sycl_info().device_count > 1) {
3815
- // A sycl_ex::command_graph object can only be created for a single device
3816
- GGML_LOG_INFO("%s: disabling SYCL graphs due to multiple devices\n", __func__);
3817
- return false;
3818
- }
3819
-
3820
- for (int i = 0; i < cgraph->n_nodes; i++) {
3821
- const ggml_op node_op = cgraph->nodes[i]->op;
3822
- switch (node_op) {
3823
- default:
3824
- break;
3825
- case GGML_OP_CONCAT:
3826
- // ggml_sycl_op_concat() does a blocking host wait after memcpy operations,
3827
- // but wait() can't be called on the events returned by a queue recording
3828
- // to a graph.
3829
- [[fallthrough]];
3830
- case GGML_OP_MUL_MAT_ID:
3831
- // ggml_sycl_mul_mat_id() does a blocking host wait on the sycl queue after
3832
- // submitting a memcpy operation, but wait() can't be called on a queue that
3833
- // is recording to a graph.
3834
- GGML_LOG_INFO("%s: disabling SYCL graphs due to unsupported node type %s\n", __func__,
3835
- ggml_op_name(node_op));
3836
- return false;
3837
- }
3838
- }
3839
- return true;
3840
- }
3841
- #endif
3842
-
3843
- static ggml_status ggml_backend_sycl_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
3844
- auto * sycl_ctx = static_cast<ggml_backend_sycl_context *>(backend->context);
3845
-
3846
- #ifdef GGML_SYCL_GRAPH
3847
- bool use_sycl_graph = !g_ggml_sycl_disable_graph && check_graph_compatibility(cgraph);
3848
- if (use_sycl_graph) {
3849
- const bool graph_support = dpct::get_device(sycl_ctx->device).has(sycl::aspect::ext_oneapi_limited_graph);
3850
- if (!graph_support) {
3851
- GGML_SYCL_DEBUG("[SYCL-GRAPH] can not use graphs on device:%d\n", sycl_ctx->device);
3852
- ggml_backend_sycl_graph_compute_impl(sycl_ctx, cgraph);
3853
- return GGML_STATUS_SUCCESS;
3854
- }
3855
-
3856
- sycl_ex::command_graph model_sycl_graph(*(sycl_ctx->stream()), {sycl_ex::property::graph::assume_buffer_outlives_graph{}});
3857
-
3858
- model_sycl_graph.begin_recording(*(sycl_ctx->stream()));
3859
- ggml_backend_sycl_graph_compute_impl(sycl_ctx, cgraph);
3860
- model_sycl_graph.end_recording();
3861
-
3862
- const bool graph_update_support = dpct::get_device(sycl_ctx->device).has(sycl::aspect::ext_oneapi_graph);
3863
- if (!sycl_ctx->exec_graph || !graph_update_support) {
3864
- auto exec_graph = graph_update_support ? model_sycl_graph.finalize(sycl_ex::property::graph::updatable{}) :
3865
- model_sycl_graph.finalize();
3866
- sycl_ctx->exec_graph = std::make_unique<
3867
- sycl_ex::command_graph<sycl_ex::graph_state::executable>>(exec_graph);
3868
- } else {
3869
- try {
3870
- sycl_ctx->exec_graph->update(model_sycl_graph);
3871
- GGML_SYCL_DEBUG("[SYCL-GRAPH] update success\n");
3872
- } catch (sycl::exception const & e) {
3873
- GGML_SYCL_DEBUG("[SYCL-GRAPH] Exception when updating graph, %s\n", e.what());
3874
- auto exec_graph = model_sycl_graph.finalize({sycl_ex::property::graph::updatable{}});
3875
- sycl_ctx->exec_graph = std::make_unique<
3876
- sycl_ex::command_graph<sycl_ex::graph_state::executable>>(exec_graph);
3877
- }
3878
- }
3879
-
3880
- sycl_ctx->stream()->ext_oneapi_graph(*(sycl_ctx->exec_graph));
3881
- } else
3882
- #endif
3883
- {
3884
- ggml_backend_sycl_graph_compute_impl(sycl_ctx, cgraph);
3885
- }
3886
- return GGML_STATUS_SUCCESS;
3887
- }
3888
-
3889
- static void ggml_backend_sycl_event_record(ggml_backend_t backend, ggml_backend_event_t event)
3890
- try
3891
- {
3892
- ggml_backend_sycl_context *sycl_ctx =
3893
- (ggml_backend_sycl_context *)backend->context;
3894
-
3895
- sycl::event *sycl_event = static_cast<sycl::event *>(event->context);
3896
-
3897
- const queue_ptr &stream = sycl_ctx->stream(sycl_ctx->device, 0);
3898
- // Record the current state of the queue
3899
- SYCL_CHECK(CHECK_TRY_ERROR(*sycl_event = stream->ext_oneapi_submit_barrier()));
3900
- }
3901
- catch (sycl::exception const &exc)
3902
- {
3903
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
3904
- << ", line:" << __LINE__ << std::endl;
3905
- std::exit(1);
3906
- }
3907
-
3908
- static void ggml_backend_sycl_event_wait(ggml_backend_t backend, ggml_backend_event_t event) try {
3909
-
3910
- sycl::event* sycl_event = static_cast<sycl::event*>(event->context);
3911
-
3912
- if (ggml_backend_is_sycl(backend)) {
3913
- SYCL_CHECK(CHECK_TRY_ERROR(sycl_event->wait()));
3914
- } else
3915
- GGML_ABORT("fatal error");
3916
- } catch (sycl::exception const& exc) {
3917
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
3918
- << ", line:" << __LINE__ << std::endl;
3919
- std::exit(1);
3920
- }
3921
-
3922
- static ggml_backend_i ggml_backend_sycl_interface = {
3923
- /* .get_name = */ ggml_backend_sycl_get_name,
3924
- /* .free = */ ggml_backend_sycl_free,
3925
- /* .set_tensor_async = */ ggml_backend_sycl_set_tensor_async,
3926
- /* .get_tensor_async = */ ggml_backend_sycl_get_tensor_async,
3927
- /* .cpy_tensor_async = */ NULL, // ggml_backend_sycl_cpy_tensor_async,
3928
- // // TODO: update for the new
3929
- // interface
3930
- /* .synchronize = */ ggml_backend_sycl_synchronize,
3931
- /* .graph_plan_create = */ NULL,
3932
- /* .graph_plan_free = */ NULL,
3933
- /* .graph_plan_update = */ NULL,
3934
- /* .graph_plan_compute = */ NULL,
3935
- /* .graph_compute = */ ggml_backend_sycl_graph_compute,
3936
- /* .event_record = */ ggml_backend_sycl_event_record,
3937
- /* .event_wait = */ ggml_backend_sycl_event_wait,
3938
- };
3939
-
3940
- static ggml_guid_t ggml_backend_sycl_guid() {
3941
- static ggml_guid guid = { 0x58, 0x05, 0x13, 0x8f, 0xcd, 0x3a, 0x61, 0x9d, 0xe7, 0xcd, 0x98, 0xa9, 0x03, 0xfd, 0x7c, 0x53 };
3942
- return &guid;
3943
- }
3944
-
3945
- bool ggml_backend_is_sycl(ggml_backend_t backend) {
3946
- return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_sycl_guid());
3947
- }
3948
-
3949
- int ggml_backend_sycl_get_device_count() {
3950
- return ggml_sycl_info().device_count;
3951
- }
3952
-
3953
-
3954
- // backend device
3955
-
3956
- struct ggml_backend_sycl_device_context {
3957
- int device;
3958
- std::string name;
3959
- std::string description;
3960
- };
3961
-
3962
- static const char * ggml_backend_sycl_device_get_name(ggml_backend_dev_t dev) {
3963
- ggml_backend_sycl_device_context * ctx = (ggml_backend_sycl_device_context *)dev->context;
3964
- return ctx->name.c_str();
3965
- }
3966
-
3967
- static const char * ggml_backend_sycl_device_get_description(ggml_backend_dev_t dev) {
3968
- ggml_backend_sycl_device_context * ctx = (ggml_backend_sycl_device_context *)dev->context;
3969
- return ctx->description.c_str();
3970
- }
3971
-
3972
- static void ggml_backend_sycl_device_get_memory(ggml_backend_dev_t dev, size_t * free, size_t * total) {
3973
- ggml_backend_sycl_device_context * ctx = (ggml_backend_sycl_device_context *)dev->context;
3974
- ggml_sycl_set_device(ctx->device);
3975
- SYCL_CHECK(CHECK_TRY_ERROR(
3976
- dpct::dev_mgr::instance().get_device(ctx->device).get_memory_info(*free, *total)));
3977
- }
3978
-
3979
- static enum ggml_backend_dev_type ggml_backend_sycl_device_get_type(ggml_backend_dev_t dev) {
3980
- GGML_UNUSED(dev);
3981
- return GGML_BACKEND_DEVICE_TYPE_GPU;
3982
- }
3983
-
3984
- static void ggml_backend_sycl_device_get_props(ggml_backend_dev_t dev, ggml_backend_dev_props * props) {
3985
- props->name = ggml_backend_sycl_device_get_name(dev);
3986
- props->description = ggml_backend_sycl_device_get_description(dev);
3987
- props->type = ggml_backend_sycl_device_get_type(dev);
3988
- ggml_backend_sycl_device_get_memory(dev, &props->memory_free, &props->memory_total);
3989
-
3990
- bool host_buffer = getenv("GGML_SYCL_NO_PINNED") == nullptr;
3991
- #ifdef GGML_SYCL_NO_PEER_COPY
3992
- bool events = false;
3993
- #else
3994
- bool events = true;
3995
- #endif
3996
-
3997
- props->caps = {
3998
- /* .async = */ true,
3999
- /* .host_buffer = */ host_buffer,
4000
- /* .buffer_from_host_ptr = */ false,
4001
- /* .events = */ events,
4002
- };
4003
- }
4004
-
4005
- static ggml_backend_t ggml_backend_sycl_device_init(ggml_backend_dev_t dev, const char * params) {
4006
- GGML_UNUSED(params);
4007
- ggml_backend_sycl_device_context * ctx = (ggml_backend_sycl_device_context *)dev->context;
4008
- return ggml_backend_sycl_init(ctx->device);
4009
- }
4010
-
4011
- static ggml_backend_buffer_type_t ggml_backend_sycl_device_get_buffer_type(ggml_backend_dev_t dev) {
4012
- ggml_backend_sycl_device_context * ctx = (ggml_backend_sycl_device_context *)dev->context;
4013
- return ggml_backend_sycl_buffer_type(ctx->device);
4014
- }
4015
-
4016
- static ggml_backend_buffer_type_t ggml_backend_sycl_device_get_host_buffer_type(ggml_backend_dev_t dev) {
4017
- GGML_UNUSED(dev);
4018
- return ggml_backend_sycl_host_buffer_type();
4019
- }
4020
-
4021
- static ggml_backend_buffer_t ggml_backend_sycl_device_buffer_from_host_ptr(ggml_backend_dev_t dev, void * ptr, size_t size, size_t max_tensor_size) {
4022
- GGML_UNUSED(dev);
4023
- GGML_UNUSED(ptr);
4024
- GGML_UNUSED(size);
4025
- GGML_UNUSED(max_tensor_size);
4026
- return nullptr;
4027
- }
4028
-
4029
- static bool ggml_backend_sycl_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
4030
- switch (op->op) {
4031
- case GGML_OP_CONV_TRANSPOSE_1D:
4032
- {
4033
- ggml_type src0_type = op->src[0]->type;
4034
- ggml_type src1_type = op->src[1]->type;
4035
- if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
4036
- return true;
4037
- }
4038
- return false;
4039
- }
4040
- case GGML_OP_UNARY:
4041
- switch (ggml_get_unary_op(op)) {
4042
- case GGML_UNARY_OP_NEG:
4043
- case GGML_UNARY_OP_STEP:
4044
- case GGML_UNARY_OP_GELU:
4045
- case GGML_UNARY_OP_SILU:
4046
- case GGML_UNARY_OP_RELU:
4047
- case GGML_UNARY_OP_SIGMOID:
4048
- case GGML_UNARY_OP_HARDSIGMOID:
4049
- case GGML_UNARY_OP_HARDSWISH:
4050
- case GGML_UNARY_OP_GELU_QUICK:
4051
- case GGML_UNARY_OP_TANH:
4052
- case GGML_UNARY_OP_EXP:
4053
- case GGML_UNARY_OP_SGN:
4054
- case GGML_UNARY_OP_ABS:
4055
- case GGML_UNARY_OP_ELU:
4056
- #if defined (GGML_SYCL_F16)
4057
- return ggml_is_contiguous(op->src[0]) && (op->type == op->src[0]->type);
4058
- #else
4059
- return ggml_is_contiguous(op->src[0]) && (op->src[0]->type == GGML_TYPE_F32 && op->type == GGML_TYPE_F32) && (op->type == op->src[0]->type);
4060
- #endif
4061
- default:
4062
- return false;
4063
- }
4064
- case GGML_OP_MUL_MAT:
4065
- case GGML_OP_MUL_MAT_ID:
4066
- {
4067
- struct ggml_tensor * a;
4068
- struct ggml_tensor * b;
4069
- if (op->op == GGML_OP_MUL_MAT) {
4070
- a = op->src[0];
4071
- b = op->src[1];
4072
- } else {
4073
- a = op->src[2];
4074
- b = op->src[1];
4075
- }
4076
- if (a->ne[3] != b->ne[3]) {
4077
- return false;
4078
- }
4079
- ggml_type a_type = a->type;
4080
- if (a_type == GGML_TYPE_IQ4_NL || a_type == GGML_TYPE_IQ4_XS ||
4081
- a_type == GGML_TYPE_IQ3_XXS || a_type == GGML_TYPE_IQ3_S ||
4082
- a_type == GGML_TYPE_IQ2_XXS || a_type == GGML_TYPE_IQ2_XS || a_type == GGML_TYPE_IQ2_S ||
4083
- a_type == GGML_TYPE_IQ1_S || a_type == GGML_TYPE_IQ1_M
4084
- ) {
4085
- if (b->ne[1] == 1 && ggml_nrows(b) > 1) {
4086
- return false;
4087
- }
4088
- }
4089
- ggml_type src0_type = op->src[0]->type;
4090
- if (src0_type == GGML_TYPE_BF16) {
4091
- return false;
4092
- }
4093
- return true;
4094
- }
4095
- case GGML_OP_OUT_PROD:
4096
- return op->type == GGML_TYPE_F32 && op->src[0]->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_F32 && op->ne[2] == 1 && op->ne[3] == 1;
4097
- case GGML_OP_GET_ROWS:
4098
- {
4099
- switch (op->src[0]->type) {
4100
- case GGML_TYPE_F16:
4101
- case GGML_TYPE_F32:
4102
- case GGML_TYPE_Q4_0:
4103
- case GGML_TYPE_Q4_1:
4104
- case GGML_TYPE_Q5_0:
4105
- case GGML_TYPE_Q5_1:
4106
- case GGML_TYPE_Q8_0:
4107
- return true;
4108
- default:
4109
- return false;
4110
- }
4111
- }
4112
- case GGML_OP_CPY:
4113
- {
4114
- ggml_type src0_type = op->src[0]->type;
4115
- ggml_type src1_type = op->src[1]->type;
4116
- if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
4117
- return true;
4118
- }
4119
- if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
4120
- return true;
4121
- }
4122
- if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
4123
- return true;
4124
- }
4125
- if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
4126
- return true;
4127
- }
4128
- if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
4129
- return true;
4130
- }
4131
- if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
4132
- return true;
4133
- }
4134
- if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
4135
- return true;
4136
- }
4137
- if (src0_type == GGML_TYPE_Q8_0 && src1_type == GGML_TYPE_F32) {
4138
- return true;
4139
- }
4140
- if (src0_type == GGML_TYPE_Q4_0 && src1_type == GGML_TYPE_F32) {
4141
- return true;
4142
- }
4143
- if (src0_type == GGML_TYPE_Q4_1 && src1_type == GGML_TYPE_F32) {
4144
- return true;
4145
- }
4146
- if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q5_0) {
4147
- return true;
4148
- }
4149
- if (src0_type == GGML_TYPE_Q5_0 && src1_type == GGML_TYPE_F32) {
4150
- return true;
4151
- }
4152
- if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q5_1) {
4153
- return true;
4154
- }
4155
- if (src0_type == GGML_TYPE_Q5_1 && src1_type == GGML_TYPE_F32) {
4156
- return true;
4157
- }
4158
- if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_IQ4_NL) {
4159
- return true;
4160
- }
4161
- return false;
4162
- }
4163
- case GGML_OP_CONCAT:
4164
- {
4165
- ggml_type src0_type = op->src[0]->type;
4166
- return src0_type != GGML_TYPE_I32 && src0_type != GGML_TYPE_I16;
4167
- }
4168
- case GGML_OP_DUP:
4169
- case GGML_OP_ARGMAX:
4170
- case GGML_OP_NONE:
4171
- case GGML_OP_RESHAPE:
4172
- case GGML_OP_VIEW:
4173
- case GGML_OP_PERMUTE:
4174
- case GGML_OP_TRANSPOSE:
4175
- return true;
4176
- case GGML_OP_ADD:
4177
- case GGML_OP_ADD1:
4178
- case GGML_OP_SUB:
4179
- case GGML_OP_MUL:
4180
- case GGML_OP_DIV:
4181
- case GGML_OP_REPEAT:
4182
- return true;
4183
- case GGML_OP_SQR:
4184
- case GGML_OP_SQRT:
4185
- case GGML_OP_SIN:
4186
- case GGML_OP_COS:
4187
- case GGML_OP_CLAMP:
4188
- case GGML_OP_LOG:
4189
- #if defined (GGML_SYCL_F16)
4190
- return ((op->type == GGML_TYPE_F32 || op->type == GGML_SYCL_F16) && (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_SYCL_F16) && (op->type == op->src[0]->type));
4191
- #else
4192
- return (op->type == GGML_TYPE_F32 && op->src[0]->type == GGML_TYPE_F32) && (op->type == op->src[0]->type);
4193
- #endif
4194
- case GGML_OP_NORM:
4195
- case GGML_OP_RMS_NORM:
4196
- case GGML_OP_L2_NORM:
4197
- case GGML_OP_GROUP_NORM:
4198
- return ggml_is_contiguous(op->src[0]);
4199
- case GGML_OP_SCALE:
4200
- return true;
4201
- case GGML_OP_CONT:
4202
- return op->src[0]->type != GGML_TYPE_BF16;
4203
- case GGML_OP_DIAG_MASK_INF:
4204
- case GGML_OP_SOFT_MAX:
4205
- return true;
4206
- case GGML_OP_ROPE:
4207
- {
4208
- const int mode = ((const int32_t *) op->op_params)[2];
4209
- // mode is not used as a bitmask in practice, the various rope type modes are independent implementations
4210
- if (mode == GGML_ROPE_TYPE_MROPE) {
4211
- return false;
4212
- }
4213
- return true;
4214
- }
4215
- case GGML_OP_IM2COL:
4216
- return true;
4217
- case GGML_OP_UPSCALE:
4218
- return op->src[0]->type == GGML_TYPE_F32 && op->op_params[0] == GGML_SCALE_MODE_NEAREST;
4219
- case GGML_OP_POOL_2D:
4220
- case GGML_OP_SUM:
4221
- case GGML_OP_SUM_ROWS:
4222
- case GGML_OP_ARGSORT:
4223
- case GGML_OP_ACC:
4224
- case GGML_OP_PAD:
4225
- case GGML_OP_LEAKY_RELU:
4226
- case GGML_OP_TIMESTEP_EMBEDDING:
4227
- case GGML_OP_RWKV_WKV6:
4228
- case GGML_OP_RWKV_WKV7:
4229
- case GGML_OP_GATED_LINEAR_ATTN:
4230
- return true;
4231
- default:
4232
- return false;
4233
- }
4234
-
4235
- GGML_UNUSED(dev);
4236
- }
4237
-
4238
- static bool ggml_backend_sycl_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
4239
- if (buft->iface.get_name != ggml_backend_sycl_buffer_type_get_name) {
4240
- return false;
4241
- }
4242
- ggml_backend_sycl_buffer_type_context * buft_ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
4243
- ggml_backend_sycl_device_context * sycl_ctx = (ggml_backend_sycl_device_context *)dev->context;
4244
- return buft_ctx->device == sycl_ctx->device;
4245
- }
4246
-
4247
- static int64_t get_op_batch_size(const ggml_tensor * op) {
4248
- switch (op->op) {
4249
- case GGML_OP_GET_ROWS:
4250
- return 0;
4251
- case GGML_OP_MUL_MAT:
4252
- return op->ne[1];
4253
- case GGML_OP_MUL_MAT_ID:
4254
- case GGML_OP_ROPE:
4255
- return op->ne[2];
4256
- default:
4257
- return ggml_nrows(op);
4258
- }
4259
- }
4260
-
4261
- static bool ggml_backend_sycl_device_offload_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
4262
- const int min_batch_size = 32;
4263
- return get_op_batch_size(op) >= min_batch_size;
4264
- GGML_UNUSED(dev);
4265
- }
4266
-
4267
- static ggml_backend_event_t
4268
- ggml_backend_sycl_device_event_new(ggml_backend_dev_t dev) {
4269
-
4270
- #ifdef GGML_SYCL_NO_PEER_COPY
4271
- return nullptr;
4272
- #else
4273
- sycl::event *event_ptr = new sycl::event();
4274
-
4275
- return new ggml_backend_event{
4276
- /* .device = */ dev,
4277
- /* .context = */ event_ptr,
4278
- };
4279
- #endif
4280
- }
4281
-
4282
- static void ggml_backend_sycl_device_event_free(ggml_backend_dev_t dev, ggml_backend_event_t event) try {
4283
- GGML_UNUSED(dev);
4284
- if (event == nullptr) {
4285
- return;
4286
- }
4287
-
4288
- if (event->context != nullptr) {
4289
- sycl::event *sycl_event = static_cast<sycl::event *>(event->context);
4290
- delete sycl_event;
4291
- event->context = nullptr;
4292
- }
4293
-
4294
- delete event;
4295
- } catch (sycl::exception const &exc) {
4296
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
4297
- << ", line:" << __LINE__ << std::endl;
4298
- std::exit(1);
4299
- }
4300
-
4301
-
4302
- static void ggml_backend_sycl_device_event_synchronize(ggml_backend_dev_t dev, ggml_backend_event_t event) try {
4303
- GGML_UNUSED(dev);
4304
-
4305
- sycl::event *sycl_event = static_cast<sycl::event *>(event->context);
4306
- SYCL_CHECK(CHECK_TRY_ERROR(sycl_event->wait()));
4307
- } catch (sycl::exception const &exc) {
4308
- std::cerr << exc.what() << "Exception caught at file:" << __FILE__
4309
- << ", line:" << __LINE__ << std::endl;
4310
- std::exit(1);
4311
- }
4312
-
4313
- static const ggml_backend_device_i ggml_backend_sycl_device_interface = {
4314
- /* .get_name = */ ggml_backend_sycl_device_get_name,
4315
- /* .get_description = */ ggml_backend_sycl_device_get_description,
4316
- /* .get_memory = */ ggml_backend_sycl_device_get_memory,
4317
- /* .get_type = */ ggml_backend_sycl_device_get_type,
4318
- /* .get_props = */ ggml_backend_sycl_device_get_props,
4319
- /* .init_backend = */ ggml_backend_sycl_device_init,
4320
- /* .get_buffer_type = */ ggml_backend_sycl_device_get_buffer_type,
4321
- /* .get_host_buffer_type = */ ggml_backend_sycl_device_get_host_buffer_type,
4322
- /* .buffer_from_host_ptr = */ ggml_backend_sycl_device_buffer_from_host_ptr,
4323
- /* .supports_op = */ ggml_backend_sycl_device_supports_op,
4324
- /* .supports_buft = */ ggml_backend_sycl_device_supports_buft,
4325
- /* .offload_op = */ ggml_backend_sycl_device_offload_op,
4326
- /* .event_new = */ ggml_backend_sycl_device_event_new,
4327
- /* .event_free = */ ggml_backend_sycl_device_event_free,
4328
- /* .event_synchronize = */ ggml_backend_sycl_device_event_synchronize,
4329
- };
4330
-
4331
- // backend reg
4332
-
4333
- struct ggml_backend_sycl_reg_context {
4334
- std::vector<ggml_backend_dev_t> devices;
4335
- };
4336
-
4337
- static const char * ggml_backend_sycl_reg_get_name(ggml_backend_reg_t reg) {
4338
- GGML_UNUSED(reg);
4339
- return GGML_SYCL_NAME;
4340
- }
4341
-
4342
- static size_t ggml_backend_sycl_reg_get_device_count(ggml_backend_reg_t reg) {
4343
- ggml_backend_sycl_reg_context * ctx = (ggml_backend_sycl_reg_context *)reg->context;
4344
- return ctx->devices.size();
4345
- }
4346
-
4347
- static ggml_backend_dev_t ggml_backend_sycl_reg_get_device(ggml_backend_reg_t reg, size_t index) {
4348
- ggml_backend_sycl_reg_context * ctx = (ggml_backend_sycl_reg_context *)reg->context;
4349
- GGML_ASSERT(index < ctx->devices.size());
4350
- return ctx->devices[index];
4351
- }
4352
-
4353
- static void *ggml_backend_sycl_reg_get_proc_address(ggml_backend_reg_t reg, const char *name) {
4354
- GGML_UNUSED(reg);
4355
-
4356
- if (strcmp(name, "ggml_backend_split_buffer_type") == 0) {
4357
- return (void *)ggml_backend_sycl_split_buffer_type;
4358
- }
4359
-
4360
- // SYCL doesn't support registering host memory, left here for reference
4361
- // "ggml_backend_register_host_buffer"
4362
- // "ggml_backend_unregister_host_buffer"
4363
- GGML_UNUSED(name);
4364
- return nullptr;
4365
- }
4366
-
4367
- static const ggml_backend_reg_i ggml_backend_sycl_reg_interface = {
4368
- /* .get_name = */ ggml_backend_sycl_reg_get_name,
4369
- /* .get_device_count = */ ggml_backend_sycl_reg_get_device_count,
4370
- /* .get_device = */ ggml_backend_sycl_reg_get_device,
4371
- /* .get_proc_address = */ ggml_backend_sycl_reg_get_proc_address,
4372
- };
4373
-
4374
-
4375
- // backend registry
4376
-
4377
- ggml_backend_reg_t ggml_backend_sycl_reg() {
4378
- static ggml_backend_reg reg;
4379
- static bool initialized = false;
4380
-
4381
- {
4382
- static std::mutex mutex;
4383
- std::lock_guard<std::mutex> lock(mutex);
4384
- if (!initialized) {
4385
- ggml_backend_sycl_reg_context * ctx = new ggml_backend_sycl_reg_context;
4386
-
4387
- for (int i = 0; i < ggml_sycl_info().device_count; i++) {
4388
- ggml_backend_sycl_device_context * dev_ctx = new ggml_backend_sycl_device_context;
4389
- dev_ctx->device = i;
4390
- dev_ctx->name = GGML_SYCL_NAME + std::to_string(i);
4391
-
4392
- ggml_sycl_set_device(i);
4393
-
4394
- dpct::device_info prop;
4395
- SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
4396
- prop, dpct::dev_mgr::instance().get_device(i))));
4397
-
4398
- dev_ctx->description = prop.get_name();
4399
-
4400
- ggml_backend_dev_t dev = new ggml_backend_device {
4401
- /* .iface = */ ggml_backend_sycl_device_interface,
4402
- /* .reg = */ &reg,
4403
- /* .context = */ dev_ctx
4404
- };
4405
- ctx->devices.push_back(dev);
4406
- }
4407
-
4408
- reg = ggml_backend_reg {
4409
- /* .api_version = */ GGML_BACKEND_API_VERSION,
4410
- /* .iface = */ ggml_backend_sycl_reg_interface,
4411
- /* .context = */ ctx
4412
- };
4413
- }
4414
-
4415
- initialized = true;
4416
- }
4417
-
4418
- return &reg;
4419
- }
4420
-
4421
- ggml_backend_t ggml_backend_sycl_init(int device) {
4422
- GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_init\n");
4423
- ggml_check_sycl();
4424
-
4425
- check_allow_gpu_index(device);
4426
-
4427
- ggml_backend_sycl_context * ctx = new ggml_backend_sycl_context(device);
4428
- if (ctx == nullptr) {
4429
- GGML_LOG_ERROR("%s: error: failed to allocate context\n", __func__);
4430
- return nullptr;
4431
- };
4432
-
4433
- ggml_backend_t sycl_backend = new ggml_backend {
4434
- /* .guid = */ ggml_backend_sycl_guid(),
4435
- /* .interface = */ ggml_backend_sycl_interface,
4436
- /* .device = */ ggml_backend_reg_dev_get(ggml_backend_sycl_reg(), device),
4437
- /* .context = */ ctx
4438
- };
4439
-
4440
- return sycl_backend;
4441
- }
4442
-
4443
- GGML_BACKEND_DL_IMPL(ggml_backend_sycl_reg)