@fugood/llama.node 0.3.16 → 0.3.17

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 (202) hide show
  1. package/CMakeLists.txt +3 -0
  2. package/bin/darwin/arm64/llama-node.node +0 -0
  3. package/bin/darwin/x64/llama-node.node +0 -0
  4. package/bin/linux/arm64/llama-node.node +0 -0
  5. package/bin/linux/x64/llama-node.node +0 -0
  6. package/bin/linux-cuda/arm64/llama-node.node +0 -0
  7. package/bin/linux-cuda/x64/llama-node.node +0 -0
  8. package/bin/linux-vulkan/arm64/llama-node.node +0 -0
  9. package/bin/linux-vulkan/x64/llama-node.node +0 -0
  10. package/bin/win32/arm64/llama-node.node +0 -0
  11. package/bin/win32/arm64/node.lib +0 -0
  12. package/bin/win32/x64/llama-node.node +0 -0
  13. package/bin/win32/x64/node.lib +0 -0
  14. package/bin/win32-vulkan/arm64/llama-node.node +0 -0
  15. package/bin/win32-vulkan/arm64/node.lib +0 -0
  16. package/bin/win32-vulkan/x64/llama-node.node +0 -0
  17. package/bin/win32-vulkan/x64/node.lib +0 -0
  18. package/lib/binding.ts +5 -0
  19. package/package.json +1 -1
  20. package/src/LlamaCompletionWorker.cpp +8 -0
  21. package/src/LlamaCompletionWorker.h +1 -0
  22. package/src/LlamaContext.cpp +3 -2
  23. package/src/llama.cpp/.github/workflows/build-linux-cross.yml +124 -0
  24. package/src/llama.cpp/.github/workflows/build.yml +70 -27
  25. package/src/llama.cpp/.github/workflows/docker.yml +6 -6
  26. package/src/llama.cpp/.github/workflows/server.yml +7 -11
  27. package/src/llama.cpp/CMakeLists.txt +23 -1
  28. package/src/llama.cpp/common/CMakeLists.txt +6 -3
  29. package/src/llama.cpp/common/arg.cpp +809 -105
  30. package/src/llama.cpp/common/arg.h +9 -0
  31. package/src/llama.cpp/common/chat.cpp +1 -1
  32. package/src/llama.cpp/common/common.cpp +31 -521
  33. package/src/llama.cpp/common/common.h +17 -36
  34. package/src/llama.cpp/common/json-schema-to-grammar.cpp +3 -0
  35. package/src/llama.cpp/common/llguidance.cpp +30 -47
  36. package/src/llama.cpp/common/minja/chat-template.hpp +15 -7
  37. package/src/llama.cpp/common/minja/minja.hpp +119 -93
  38. package/src/llama.cpp/common/sampling.cpp +3 -0
  39. package/src/llama.cpp/docs/build.md +122 -7
  40. package/src/llama.cpp/examples/CMakeLists.txt +0 -9
  41. package/src/llama.cpp/examples/batched/batched.cpp +1 -1
  42. package/src/llama.cpp/examples/batched-bench/batched-bench.cpp +1 -1
  43. package/src/llama.cpp/examples/embedding/embedding.cpp +7 -1
  44. package/src/llama.cpp/examples/export-lora/export-lora.cpp +1 -1
  45. package/src/llama.cpp/examples/gguf-split/gguf-split.cpp +15 -16
  46. package/src/llama.cpp/examples/gritlm/gritlm.cpp +1 -1
  47. package/src/llama.cpp/examples/llama-bench/llama-bench.cpp +210 -8
  48. package/src/llama.cpp/examples/llama.android/llama/build.gradle.kts +1 -0
  49. package/src/llama.cpp/examples/llava/CMakeLists.txt +39 -24
  50. package/src/llama.cpp/examples/llava/clip-impl.h +345 -0
  51. package/src/llama.cpp/examples/llava/clip.cpp +2152 -1803
  52. package/src/llama.cpp/examples/llava/clip.h +39 -22
  53. package/src/llama.cpp/examples/llava/deprecation-warning.cpp +22 -0
  54. package/src/llama.cpp/examples/llava/llava.cpp +64 -52
  55. package/src/llama.cpp/examples/llava/mtmd-cli.cpp +344 -0
  56. package/src/llama.cpp/examples/llava/mtmd.cpp +708 -0
  57. package/src/llama.cpp/examples/llava/mtmd.h +168 -0
  58. package/src/llama.cpp/examples/llava/{qwen2vl-cli.cpp → qwen2vl-test.cpp} +83 -31
  59. package/src/llama.cpp/examples/main/main.cpp +16 -5
  60. package/src/llama.cpp/examples/parallel/parallel.cpp +3 -1
  61. package/src/llama.cpp/examples/passkey/passkey.cpp +1 -1
  62. package/src/llama.cpp/examples/perplexity/perplexity.cpp +17 -3
  63. package/src/llama.cpp/examples/quantize/quantize.cpp +115 -2
  64. package/src/llama.cpp/examples/rpc/CMakeLists.txt +4 -2
  65. package/src/llama.cpp/examples/rpc/rpc-server.cpp +163 -8
  66. package/src/llama.cpp/examples/run/CMakeLists.txt +12 -1
  67. package/src/llama.cpp/examples/run/run.cpp +14 -28
  68. package/src/llama.cpp/examples/server/httplib.h +313 -247
  69. package/src/llama.cpp/examples/server/server.cpp +238 -139
  70. package/src/llama.cpp/examples/server/utils.hpp +51 -2
  71. package/src/llama.cpp/examples/speculative/speculative.cpp +1 -1
  72. package/src/llama.cpp/examples/speculative-simple/speculative-simple.cpp +1 -1
  73. package/src/llama.cpp/examples/sycl/build.sh +2 -2
  74. package/src/llama.cpp/examples/sycl/win-build-sycl.bat +2 -2
  75. package/src/llama.cpp/examples/tts/tts.cpp +6 -9
  76. package/src/llama.cpp/ggml/CMakeLists.txt +8 -2
  77. package/src/llama.cpp/ggml/cmake/GitVars.cmake +22 -0
  78. package/src/llama.cpp/ggml/include/ggml-cpu.h +5 -0
  79. package/src/llama.cpp/ggml/include/ggml-rpc.h +6 -1
  80. package/src/llama.cpp/ggml/include/ggml.h +66 -99
  81. package/src/llama.cpp/ggml/src/CMakeLists.txt +10 -7
  82. package/src/llama.cpp/ggml/src/ggml-cann/CMakeLists.txt +0 -2
  83. package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.cpp +8 -4
  84. package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.h +5 -5
  85. package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.cpp +692 -1534
  86. package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.h +613 -122
  87. package/src/llama.cpp/ggml/src/ggml-cann/common.h +135 -1
  88. package/src/llama.cpp/ggml/src/ggml-cann/ggml-cann.cpp +507 -137
  89. package/src/llama.cpp/ggml/src/ggml-common.h +12 -6
  90. package/src/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +48 -22
  91. package/src/llama.cpp/ggml/src/ggml-cpu/binary-ops.cpp +158 -0
  92. package/src/llama.cpp/ggml/src/ggml-cpu/binary-ops.h +16 -0
  93. package/src/llama.cpp/ggml/src/ggml-cpu/common.h +72 -0
  94. package/src/llama.cpp/ggml/src/ggml-cpu/cpu-feats-x86.cpp +1 -1
  95. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.cpp +896 -192
  96. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h +2 -21
  97. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.c +754 -404
  98. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +1003 -13519
  99. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.cpp +2 -0
  100. package/src/llama.cpp/ggml/src/ggml-cpu/kleidiai/kernels.cpp +2 -7
  101. package/src/llama.cpp/ggml/src/ggml-cpu/kleidiai/kernels.h +0 -1
  102. package/src/llama.cpp/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +3 -4
  103. package/src/llama.cpp/ggml/src/ggml-cpu/llamafile/sgemm.cpp +533 -88
  104. package/src/llama.cpp/ggml/src/ggml-cpu/ops.cpp +8809 -0
  105. package/src/llama.cpp/ggml/src/ggml-cpu/ops.h +110 -0
  106. package/src/llama.cpp/ggml/src/ggml-cpu/simd-mappings.h +892 -0
  107. package/src/llama.cpp/ggml/src/ggml-cpu/unary-ops.cpp +186 -0
  108. package/src/llama.cpp/ggml/src/ggml-cpu/unary-ops.h +28 -0
  109. package/src/llama.cpp/ggml/src/ggml-cpu/vec.cpp +258 -0
  110. package/src/llama.cpp/ggml/src/ggml-cpu/vec.h +802 -0
  111. package/src/llama.cpp/ggml/src/ggml-cuda/vendors/hip.h +7 -0
  112. package/src/llama.cpp/ggml/src/ggml-cuda/vendors/musa.h +1 -0
  113. package/src/llama.cpp/ggml/src/ggml-hip/CMakeLists.txt +0 -4
  114. package/src/llama.cpp/ggml/src/ggml-impl.h +52 -18
  115. package/src/llama.cpp/ggml/src/ggml-metal/ggml-metal-impl.h +70 -3
  116. package/src/llama.cpp/ggml/src/ggml-opencl/CMakeLists.txt +67 -119
  117. package/src/llama.cpp/ggml/src/ggml-opencl/ggml-opencl.cpp +1023 -260
  118. package/src/llama.cpp/ggml/src/ggml-rpc/ggml-rpc.cpp +293 -40
  119. package/src/llama.cpp/ggml/src/ggml-sycl/CMakeLists.txt +96 -22
  120. package/src/llama.cpp/ggml/src/ggml-sycl/backend.hpp +1 -0
  121. package/src/llama.cpp/ggml/src/ggml-sycl/binbcast.cpp +350 -0
  122. package/src/llama.cpp/ggml/src/ggml-sycl/binbcast.hpp +39 -0
  123. package/src/llama.cpp/ggml/src/ggml-sycl/common.cpp +0 -35
  124. package/src/llama.cpp/ggml/src/ggml-sycl/common.hpp +2 -292
  125. package/src/llama.cpp/ggml/src/ggml-sycl/dpct/helper.hpp +79 -90
  126. package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.cpp +967 -438
  127. package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.hpp +22 -23
  128. package/src/llama.cpp/ggml/src/ggml-sycl/getrows.cpp +24 -20
  129. package/src/llama.cpp/ggml/src/ggml-sycl/getrows.hpp +1 -4
  130. package/src/llama.cpp/ggml/src/ggml-sycl/ggml-sycl.cpp +204 -280
  131. package/src/llama.cpp/ggml/src/ggml-sycl/im2col.cpp +84 -74
  132. package/src/llama.cpp/ggml/src/ggml-sycl/im2col.hpp +1 -3
  133. package/src/llama.cpp/ggml/src/ggml-sycl/norm.cpp +37 -49
  134. package/src/llama.cpp/ggml/src/ggml-sycl/norm.hpp +7 -22
  135. package/src/llama.cpp/ggml/src/ggml-sycl/outprod.cpp +4 -14
  136. package/src/llama.cpp/ggml/src/ggml-sycl/rope.cpp +204 -118
  137. package/src/llama.cpp/ggml/src/ggml-sycl/rope.hpp +1 -3
  138. package/src/llama.cpp/ggml/src/ggml-vulkan/CMakeLists.txt +23 -0
  139. package/src/llama.cpp/ggml/src/ggml-vulkan/ggml-vulkan.cpp +646 -114
  140. package/src/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt +12 -0
  141. package/src/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +17 -8
  142. package/src/llama.cpp/ggml/src/ggml.c +141 -245
  143. package/src/llama.cpp/ggml/src/gguf.cpp +1 -0
  144. package/src/llama.cpp/include/llama.h +30 -11
  145. package/src/llama.cpp/models/ggml-vocab-llama4.gguf.inp +112 -0
  146. package/src/llama.cpp/models/ggml-vocab-llama4.gguf.out +46 -0
  147. package/src/llama.cpp/models/ggml-vocab-pixtral.gguf.inp +112 -0
  148. package/src/llama.cpp/models/ggml-vocab-pixtral.gguf.out +46 -0
  149. package/src/llama.cpp/requirements/requirements-all.txt +2 -0
  150. package/src/llama.cpp/requirements/requirements-gguf_editor_gui.txt +3 -0
  151. package/src/llama.cpp/src/CMakeLists.txt +3 -2
  152. package/src/llama.cpp/src/llama-adapter.cpp +37 -1
  153. package/src/llama.cpp/src/llama-arch.cpp +160 -17
  154. package/src/llama.cpp/src/llama-arch.h +16 -0
  155. package/src/llama.cpp/src/llama-chat.cpp +82 -17
  156. package/src/llama.cpp/src/llama-chat.h +6 -2
  157. package/src/llama.cpp/src/llama-context.cpp +108 -92
  158. package/src/llama.cpp/src/llama-context.h +1 -2
  159. package/src/llama.cpp/src/llama-graph.cpp +189 -119
  160. package/src/llama.cpp/src/llama-graph.h +26 -6
  161. package/src/llama.cpp/src/llama-hparams.h +13 -0
  162. package/src/llama.cpp/src/llama-kv-cache.cpp +70 -123
  163. package/src/llama.cpp/src/llama-kv-cache.h +41 -115
  164. package/src/llama.cpp/src/llama-memory.h +1 -1
  165. package/src/llama.cpp/src/llama-mmap.cpp +1 -1
  166. package/src/llama.cpp/src/llama-model-loader.cpp +10 -5
  167. package/src/llama.cpp/src/llama-model-loader.h +5 -3
  168. package/src/llama.cpp/src/llama-model.cpp +1760 -534
  169. package/src/llama.cpp/src/llama-model.h +13 -1
  170. package/src/llama.cpp/src/llama-quant.cpp +29 -8
  171. package/src/llama.cpp/src/llama-sampling.cpp +7 -1
  172. package/src/llama.cpp/src/llama-vocab.cpp +44 -6
  173. package/src/llama.cpp/src/llama.cpp +1 -1
  174. package/src/llama.cpp/tests/CMakeLists.txt +43 -30
  175. package/src/llama.cpp/tests/test-arg-parser.cpp +51 -4
  176. package/src/llama.cpp/tests/test-backend-ops.cpp +82 -43
  177. package/src/llama.cpp/tests/test-chat-template.cpp +34 -13
  178. package/src/llama.cpp/tests/test-chat.cpp +12 -2
  179. package/src/llama.cpp/{examples/gbnf-validator/gbnf-validator.cpp → tests/test-gbnf-validator.cpp} +2 -2
  180. package/src/llama.cpp/tests/test-grammar-integration.cpp +3 -2
  181. package/src/llama.cpp/tests/test-grammar-llguidance.cpp +63 -2
  182. package/src/llama.cpp/tests/test-grammar-parser.cpp +3 -1
  183. package/src/llama.cpp/tests/test-json-schema-to-grammar.cpp +17 -1
  184. package/src/llama.cpp/tests/test-llama-grammar.cpp +2 -1
  185. package/src/llama.cpp/{examples/quantize-stats/quantize-stats.cpp → tests/test-quantize-stats.cpp} +3 -1
  186. package/src/llama.cpp/tests/test-tokenizer-1-bpe.cpp +2 -1
  187. package/src/llama.cpp/tests/test-tokenizer-1-spm.cpp +2 -1
  188. package/src/llama.cpp/examples/gbnf-validator/CMakeLists.txt +0 -5
  189. package/src/llama.cpp/examples/llava/gemma3-cli.cpp +0 -341
  190. package/src/llama.cpp/examples/llava/llava-cli.cpp +0 -332
  191. package/src/llama.cpp/examples/llava/minicpmv-cli.cpp +0 -354
  192. package/src/llama.cpp/examples/quantize-stats/CMakeLists.txt +0 -6
  193. package/src/llama.cpp/ggml/src/ggml-cann/kernels/CMakeLists.txt +0 -30
  194. package/src/llama.cpp/ggml/src/ggml-cann/kernels/ascendc_kernels.h +0 -19
  195. package/src/llama.cpp/ggml/src/ggml-cann/kernels/dup.cpp +0 -234
  196. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_f16.cpp +0 -197
  197. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_f32.cpp +0 -190
  198. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_q4_0.cpp +0 -204
  199. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_q8_0.cpp +0 -191
  200. package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_f16_q8_0.cpp +0 -218
  201. package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_f32_q8_0.cpp +0 -216
  202. package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_float_to_q4_0.cpp +0 -295
@@ -1,15 +1,4 @@
1
- #ifndef CANN_ACLNN_OPS
2
- #define CANN_ACLNN_OPS
3
-
4
1
  /**
5
- * @file acl_tensor
6
- * @brief This file contains related functions of ggml_tensor and acl_tensor.
7
- * Contains conversion from ggml_tensor to acl_tensor, broadcast and other
8
- * functions.
9
- * @author hipudding <huafengchun@gmail.com>
10
- * @author wangshuai09 <391746016@qq.com>
11
- * @date July 15, 2024
12
- *
13
2
  * Copyright (c) 2023-2024 The ggml authors
14
3
  *
15
4
  * Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -31,20 +20,31 @@
31
20
  * IN THE SOFTWARE.
32
21
  */
33
22
 
34
- #include <aclnnop/aclnn_add.h>
23
+ #ifndef CANN_ACLNN_OPS
24
+ #define CANN_ACLNN_OPS
25
+
26
+ #include <functional>
27
+ #include <aclnnop/aclnn_abs.h>
28
+ #include <aclnnop/aclnn_neg.h>
29
+ #include <aclnnop/aclnn_exp.h>
35
30
  #include <aclnnop/aclnn_arange.h>
36
31
  #include <aclnnop/aclnn_argsort.h>
37
32
  #include <aclnnop/aclnn_cat.h>
38
33
  #include <aclnnop/aclnn_clamp.h>
39
- #include <aclnnop/aclnn_div.h>
40
34
  #include <aclnnop/aclnn_gelu.h>
35
+ #include <aclnnop/aclnn_gelu_v2.h>
36
+ #include <aclnnop/aclnn_sigmoid.h>
41
37
  #include <aclnnop/aclnn_hardsigmoid.h>
42
38
  #include <aclnnop/aclnn_hardswish.h>
43
39
  #include <aclnnop/aclnn_leaky_relu.h>
44
- #include <aclnnop/aclnn_mul.h>
45
40
  #include <aclnnop/aclnn_relu.h>
46
41
  #include <aclnnop/aclnn_silu.h>
47
42
  #include <aclnnop/aclnn_tanh.h>
43
+ #include <aclnnop/aclnn_sqrt.h>
44
+ #include <aclnnop/aclnn_sin.h>
45
+ #include <aclnnop/aclnn_cos.h>
46
+ #include <aclnnop/aclnn_log.h>
47
+ #include <aclnnop/aclnn_sign.h>
48
48
  #include "acl_tensor.h"
49
49
  #include "common.h"
50
50
 
@@ -63,23 +63,6 @@
63
63
  */
64
64
  void ggml_cann_repeat(ggml_backend_cann_context& ctx, ggml_tensor* dst);
65
65
 
66
- /**
67
- * @brief Adds two ggml tensors using the CANN backend.
68
- *
69
- * @details This function performs an element-wise addition of two tensors. In
70
- * case the tensors do not have the same shape, one or both tensors
71
- * will be broadcasted to match the shape of the other before the
72
- * addition is performed.The formula for the operation is given by:
73
- * \f[
74
- * \text{dst} = \text{acl_src0} + \alpha \cdot \text{acl_src1}
75
- * \f]
76
- *
77
- * @param ctx The CANN context used for operations.
78
- * @param dst The ggml tensor representing the destination, result of the
79
- * addition is stored at dst->data, and dst->op is `GGML_OP_ADD`
80
- */
81
- void ggml_cann_add(ggml_backend_cann_context& ctx, ggml_tensor* dst);
82
-
83
66
  /**
84
67
  * @brief Applies the Leaky ReLU activation function to a tensor using the CANN
85
68
  * backend.
@@ -131,19 +114,6 @@ void ggml_cann_concat(ggml_backend_cann_context& ctx, ggml_tensor* dst);
131
114
  */
132
115
  void ggml_cann_arange(ggml_backend_cann_context& ctx, ggml_tensor* dst);
133
116
 
134
- /**
135
- * @brief Computes the square of the elements of a ggml tensor using the CANN
136
- * backend.
137
- * @details The function sets the second source tensor of the destination
138
- * tensor `dst` to be equal to the first source tensor. This is
139
- * effectively squaring the elements since the multiplication becomes
140
- * `element * element`.
141
- * @param ctx The CANN context used for operations.
142
- * @param dst The destination tensor where the squared values will be stored,
143
- * which dst->op is `GGML_OP_SQR`.
144
- */
145
- void ggml_cann_sqr(ggml_backend_cann_context& ctx, ggml_tensor* dst);
146
-
147
117
  /**
148
118
  * @brief Applies a clamp operation to the elements of a ggml tensor using the
149
119
  * CANN backend.
@@ -275,6 +245,20 @@ void ggml_cann_acc(ggml_backend_cann_context& ctx, ggml_tensor* dst);
275
245
  */
276
246
  void ggml_cann_sum_rows(ggml_backend_cann_context& ctx, ggml_tensor* dst);
277
247
 
248
+ /**
249
+ * @brief Computes the sum of elements in a ggml tensor.
250
+ *
251
+ * @details This function performs a reduction sum operation along the last
252
+ * dimension of the input tensor `src`. The result of the sum is stored
253
+ * in the destination tensor `dst`.
254
+ *
255
+ * @param ctx The CANN context used for operations.
256
+ * @param dst The destination tensor where the reduced values will be stored。
257
+ *
258
+ */
259
+
260
+ void ggml_cann_sum(ggml_backend_cann_context& ctx, ggml_tensor* dst);
261
+
278
262
  /**
279
263
  * @brief Upsamples a ggml tensor using nearest neighbor interpolation using
280
264
  * the CANN backend.
@@ -484,109 +468,616 @@ void ggml_cann_mul_mat(ggml_backend_cann_context& ctx, ggml_tensor* dst);
484
468
  */
485
469
  void ggml_cann_rope(ggml_backend_cann_context& ctx, ggml_tensor* dst);
486
470
 
487
- template <aclnnStatus getWorkspaceSize(const aclTensor*, const aclTensor*,
488
- aclTensor*, uint64_t*, aclOpExecutor**),
489
- aclnnStatus execute(void*, uint64_t, aclOpExecutor*, aclrtStream)>
490
- void ggml_cann_mul_div(ggml_backend_cann_context& ctx, ggml_tensor* dst) {
491
- ggml_tensor* src0 = dst->src[0];
492
- ggml_tensor* src1 = dst->src[1];
493
- GGML_ASSERT(ggml_can_repeat(src1, src0) && ggml_are_same_shape(src0, dst));
471
+ /**
472
+ * @brief Computes the index of the maximum value along the specified dimension
473
+ * of a ggml tensor using the CANN backend.
474
+ *
475
+ * @details This function performs an argmax operation on the input tensor.
476
+ * It finds the index of the maximum value along the specified axis
477
+ * and stores these indices in the destination tensor `dst`. The
478
+ * operation is executed using the CANN backend for optimized performance.
479
+ *
480
+ * @param ctx The CANN context used for operations.
481
+ * @param dst The destination tensor where the indices of the maximum values will
482
+ * be stored. dst->op is `GGML_OP_ARGMAX`.
483
+ */
484
+ void ggml_cann_argmax(ggml_backend_cann_context& ctx, ggml_tensor* dst);
494
485
 
495
- aclTensor* acl_src0;
496
- aclTensor* acl_src1;
497
- aclTensor* acl_dst;
486
+ /**
487
+ * @brief Adds two tensors element-wise and stores the result in a destination
488
+ * tensor.
489
+ *
490
+ * This function performs the operation:
491
+ * \f[
492
+ * dst = acl\_src0 + alpha \times acl\_src1
493
+ * \f]
494
+ * where alpha is a scalar value and defaults to 1.0f.
495
+ *
496
+ * @param ctx The context for the CANN backend operations.
497
+ * @param acl_src0 The first source tensor.
498
+ * @param acl_src1 The second source tensor.
499
+ * @param acl_dst The destination tensor where the result will be stored.
500
+ */
501
+ void aclnn_add(ggml_backend_cann_context& ctx, aclTensor* acl_src0,
502
+ aclTensor* acl_src1, aclTensor* acl_dst = nullptr);
498
503
 
499
- // Need bcast
500
- if (!ggml_are_same_shape(src0, src1) && ggml_cann_need_bcast(src0, src1)) {
501
- BCAST_SHAPE(src0, src1)
502
- acl_src0 = ggml_cann_create_tensor(src0, BCAST_PARAM(src0));
503
- acl_src1 = ggml_cann_create_tensor(src1, BCAST_PARAM(src1));
504
- acl_dst = ggml_cann_create_tensor(dst, BCAST_PARAM(src0));
505
- } else {
506
- acl_src0 = ggml_cann_create_tensor(src0);
507
- acl_src1 = ggml_cann_create_tensor(src1);
508
- acl_dst = ggml_cann_create_tensor(dst);
504
+ /**
505
+ * @brief Sub two tensors element-wise and stores the result in a destination
506
+ * tensor.
507
+ *
508
+ * This function performs the operation:
509
+ * \f[
510
+ * dst = acl\_src0 - alpha \times acl\_src1
511
+ * \f]
512
+ * where alpha is a scalar value and defaults to 1.0f.
513
+ *
514
+ * @param ctx The context for the CANN backend operations.
515
+ * @param acl_src0 The first source tensor.
516
+ * @param acl_src1 The second source tensor.
517
+ * @param acl_dst The destination tensor where the result will be stored.
518
+ */
519
+ void aclnn_sub(ggml_backend_cann_context& ctx, aclTensor* acl_src0,
520
+ aclTensor* acl_src1, aclTensor* acl_dst = nullptr);
521
+
522
+ /**
523
+ * @brief Performs element-wise multiplication of two tensors and stores the
524
+ * result in a destination tensor.
525
+ *
526
+ * This function performs element-wise multiplication of the tensors `acl_src`
527
+ * and `acl_other` and stores the result in the destination tensor `acl_dst`.
528
+ * The operation is defined as:
529
+ * \f[
530
+ * \text {acl_dst }_i=\text {acl_src }_i \times \text {acl_other }_i
531
+ * \f]
532
+ *
533
+ * @param ctx The context for the CANN backend operations.
534
+ * @param acl_src The first tensor for element-wise multiplication.
535
+ * @param acl_other The second tensor for element-wise multiplication.
536
+ * @param acl_dst The destination tensor where the result will be stored.
537
+ */
538
+ void aclnn_mul(ggml_backend_cann_context& ctx, aclTensor* acl_src,
539
+ aclTensor* acl_other, aclTensor* acl_dst = nullptr);
540
+
541
+ /**
542
+ * @brief Matrix division, optionally in-place.
543
+ *
544
+ * This function division each element of the source tensor `acl_src` by the
545
+ * tensor `acl_other` and stores the result in the destination tensor `acl_dst`.
546
+ * If `inplace` is true, `acl_dst` will not be used and the operation is
547
+ * performed in-place on `acl_src`. The operation is defined as: \f[
548
+ * \text{dst}_i = \frac{\text{acl_src}_i}{\text{acl_other}_i}
549
+ * \f]
550
+ *
551
+ * @param ctx The context for the CANN backend operations.
552
+ * @param acl_src Numerator tensor..
553
+ * @param acl_other Denominator tensor.
554
+ * @param acl_dst The destination tensor where the result will be stored if
555
+ * `inplace` is false.
556
+ * @param inplace Flag indicating whether to perform the operation in-place on
557
+ * `acl_src`.
558
+ */
559
+ void aclnn_div(ggml_backend_cann_context& ctx, aclTensor* acl_src,
560
+ aclTensor* acl_other, aclTensor* acl_dst = nullptr);
561
+
562
+ /**
563
+ * @brief Applies element-wise cosine function to the elements of a tensor.
564
+ *
565
+ * This function computes the cosine of each element in the source tensor
566
+ * `acl_src` and stores the result in the destination tensor `acl_dst`. The
567
+ * operation is defined as: \f[ \text {acl_dst }_i=\cos \left(\text {acl_src
568
+ * }_i\right) \f]
569
+ *
570
+ * @param ctx The context for the CANN backend operations.
571
+ * @param acl_src The source tensor on which the cosine function will be
572
+ * applied.
573
+ * @param acl_dst The destination tensor where the cosine results will be
574
+ * stored.
575
+ */
576
+ void aclnn_cos(ggml_backend_cann_context& ctx, aclTensor* acl_src,
577
+ aclTensor* acl_dst);
578
+
579
+ /**
580
+ * @brief Applies element-wise sine function to the elements of a tensor.
581
+ *
582
+ * This function computes the sine of each element in the source tensor
583
+ `acl_src`
584
+ * and stores the result in the destination tensor `acl_dst`.
585
+ * The operation is defined as:
586
+ * \f[
587
+ * \text {acl_dst }_i=\sin \left(\text {acl_src }_i\right)
588
+ * \f]
589
+
590
+ * @param ctx The context for the CANN backend operations.
591
+ * @param acl_src The source tensor on which the sine function will be applied.
592
+ * @param acl_dst The destination tensor where the sine results will be stored.
593
+ */
594
+ void aclnn_sin(ggml_backend_cann_context& ctx, aclTensor* acl_src,
595
+ aclTensor* acl_dst);
596
+
597
+ /**
598
+ * @brief Prepares broadcast-compatible ACL tensors for two input tensors and one
599
+ * output tensor.
600
+ *
601
+ * This function checks whether broadcasting is needed between `src0` and `src1`.
602
+ * If broadcasting is required, it calculates the proper shapes and creates
603
+ * ACL tensors with broadcast parameters. Otherwise, it directly creates ACL tensors
604
+ * based on the original tensor shapes.
605
+ *
606
+ * @param src0 The first input tensor (reference shape).
607
+ * @param src1 The second input tensor (possibly broadcasted).
608
+ * @param dst The destination/output tensor.
609
+ * @param acl_src0 Output pointer to the created ACL tensor corresponding to src0.
610
+ * @param acl_src1 Output pointer to the created ACL tensor corresponding to src1.
611
+ * @param acl_dst Output pointer to the created ACL tensor corresponding to dst.
612
+ */
613
+ void bcast_shape(ggml_tensor * src0, ggml_tensor * src1, ggml_tensor * dst,
614
+ aclTensor ** acl_src0, aclTensor ** acl_src1, aclTensor ** acl_dst);
615
+
616
+ /**
617
+ * @brief Computes the 1D transposed convolution (deconvolution) of a ggml
618
+ * tensor using the CANN backend.
619
+ *
620
+ * @details This function performs a 1D transposed convolution (also known as
621
+ * deconvolution) operation on the input tensor. The computed result is stored
622
+ * in the destination tensor `dst`. The operation is optimized using the CANN
623
+ * backend for improved performance.
624
+ *
625
+ * @param ctx The CANN context used for operations.
626
+ * @param dst The destination tensor where the transposed convolution result
627
+ * will be stored. dst->op is `GGML_OP_CONV_TRANSPOSE_1D`.
628
+ */
629
+ void ggml_cann_conv_transpose_1d(ggml_backend_cann_context& ctx, ggml_tensor* dst);
630
+
631
+ /**
632
+ * @brief Applies the ELU (Exponential Linear Unit) activation to a ggml tensor
633
+ * using the CANN backend.
634
+ *
635
+ * @details This function performs an element-wise ELU activation on the input
636
+ * tensor.
637
+ * The result is written to the destination tensor `dst` in-place.
638
+ * The ELU function is defined as:
639
+ *
640
+ * \text{ELU}(x) =
641
+ * \begin{cases}
642
+ * x, & \text{if } x > 0 \\
643
+ * \alpha \left( \exp(x) - 1 \right), & \text{if } x \leq 0
644
+ * \end{cases}
645
+ *
646
+ * where α (alpha) is a hyperparameter, typically set to 1.0.
647
+ * This operation is optimized using the CANN backend for high-performance
648
+ * inference or training.
649
+ *
650
+ * @param ctx The CANN context used for operations.
651
+ * @param dst The destination tensor where the ELU-activated result will be stored.
652
+ * dst->op is expected to be `GGML_OP_ELU`.
653
+ */
654
+ void ggml_cann_elu(ggml_backend_cann_context& ctx, ggml_tensor* dst);
655
+
656
+ /**
657
+ * @brief Computes the mean of a ggml tensor element-wise using the CANN backend.
658
+ *
659
+ * @details This function calculates the element-wise mean of the input tensor.
660
+ * The result is written to the destination tensor `dst`.
661
+ * The mean is computed by averaging the values across the entire tensor.
662
+ *
663
+ * This operation is optimized using the CANN backend for high-performance inference or training.
664
+ *
665
+ * @param ctx The CANN context used for operations.
666
+ * @param dst The destination tensor where the mean result will be stored.
667
+ * dst->op is expected to be `GGML_OP_MEAN`.
668
+ */
669
+ void ggml_cann_mean(ggml_backend_cann_context& ctx, ggml_tensor* dst);
670
+
671
+ /**
672
+ * @brief Applies 1D reflect padding to a ggml tensor using the CANN backend.
673
+ *
674
+ * @details This function performs 1D reflect padding on the input tensor.
675
+ * The amount of padding on each side is specified by parameters stored in `dst->op_params`.
676
+ * The operation reflects the values at the borders of the tensor to generate the padded output.
677
+ *
678
+ * This operation is optimized using the CANN backend for high-performance inference or training.
679
+ *
680
+ * @param ctx The CANN context used for operations.
681
+ * @param dst The destination tensor where the padded result will be stored.
682
+ * dst->op is expected to be `GGML_OP_PAD_REFLECT_1D`.
683
+ */
684
+ void ggml_cann_pad_reflect_1d(ggml_backend_cann_context& ctx, ggml_tensor* dst);
685
+
686
+ /**
687
+ * @brief Counts the number of equal elements in two ggml tensors using the CANN backend.
688
+ *
689
+ * @details This function performs an element-wise comparison between two input tensors,
690
+ * and counts the number of positions where the elements are equal. The result is
691
+ * stored in the destination tensor `dst` as a scalar.
692
+ *
693
+ * The operation is optimized using the CANN backend, making it suitable for
694
+ * high-performance inference or training scenarios.
695
+ *
696
+ * @param ctx The CANN context used for operations.
697
+ * @param dst The destination tensor where the result will be stored.
698
+ * dst->op is expected to be `GGML_OP_COUNT_EQUAL`.
699
+ */
700
+ void ggml_cann_count_equal(ggml_backend_cann_context& ctx, ggml_tensor* dst);
701
+
702
+ /**
703
+ * @brief Applies the Step activation function to a ggml tensor using the CANN backend.
704
+ *
705
+ * @details This function applies a step function element-wise to the input tensor, where
706
+ * each element is transformed to 1.0 if it is greater than 0, and 0.0 otherwise.
707
+ * The result is stored in the destination tensor `dst`.
708
+ *
709
+ * This operation is accelerated using the CANN backend to improve runtime performance.
710
+ *
711
+ * @param ctx The CANN context used for operations.
712
+ * @param dst The destination tensor where the result will be stored.
713
+ * dst->op is expected to be `GGML_OP_STEP`.
714
+ */
715
+ void ggml_cann_step(ggml_backend_cann_context& ctx, ggml_tensor* dst);
716
+
717
+ /*
718
+ * @brief A generic wrapper for ACL resources with custom deleter support.
719
+ */
720
+ using any_acl_resource = std::unique_ptr<void, std::function<void(void*)>>;
721
+
722
+ /**
723
+ * @brief Trait structure used to define how to destroy a given ACL resource type.
724
+ *
725
+ * @tparam T ACL resource type.
726
+ */
727
+ template<typename T>
728
+ struct acl_resource_traits;
729
+
730
+ /**
731
+ * @brief Specialization for aclTensor, defines how to destroy an aclTensor resource.
732
+ */
733
+ template<>
734
+ struct acl_resource_traits<aclTensor> {
735
+ static void destroy(void* p) {
736
+ ACL_CHECK(aclDestroyTensor(static_cast<aclTensor*>(p)));
509
737
  }
738
+ };
510
739
 
511
- uint64_t workspaceSize = 0;
512
- aclOpExecutor* executor;
513
- void* workspaceAddr = nullptr;
740
+ /**
741
+ * @brief Specialization for aclIntArray, defines how to destroy an aclIntArray resource.
742
+ */
743
+ template<>
744
+ struct acl_resource_traits<aclIntArray> {
745
+ static void destroy(void* p) {
746
+ ACL_CHECK(aclDestroyIntArray(static_cast<aclIntArray*>(p)));
747
+ }
748
+ };
514
749
 
515
- ACL_CHECK(getWorkspaceSize(acl_src0, acl_src1, acl_dst, &workspaceSize,
516
- &executor));
517
- if (workspaceSize > 0) {
518
- ggml_cann_pool_alloc workspace_allocator(ctx.pool(), workspaceSize);
519
- workspaceAddr = workspace_allocator.get();
750
+ /**
751
+ * @brief Specialization for aclScalar, defines how to destroy an aclScalar resource.
752
+ */
753
+ template<>
754
+ struct acl_resource_traits<aclScalar> {
755
+ static void destroy(void* p) {
756
+ ACL_CHECK(aclDestroyScalar(static_cast<aclScalar*>(p)));
520
757
  }
758
+ };
521
759
 
522
- aclrtStream main_stream = ctx.stream();
523
- ACL_CHECK(execute(workspaceAddr, workspaceSize, executor, main_stream));
760
+ /**
761
+ * @brief Specialization for aclTensorList, defines how to destroy an aclTensorList resource.
762
+ */
763
+ template<>
764
+ struct acl_resource_traits<aclTensorList> {
765
+ static void destroy(void* p) {
766
+ ACL_CHECK(aclDestroyTensorList(static_cast<aclTensorList*>(p)));
767
+ }
768
+ };
524
769
 
525
- ACL_CHECK(aclDestroyTensor(acl_src0));
526
- ACL_CHECK(aclDestroyTensor(acl_src1));
527
- ACL_CHECK(aclDestroyTensor(acl_dst));
770
+ /**
771
+ * @brief Creates a generic ACL resource wrapper with proper destruction logic.
772
+ *
773
+ * @tparam T ACL resource type.
774
+ * @param ptr Raw pointer to ACL resource.
775
+ * @return any_acl_resource Smart pointer that handles destruction.
776
+ */
777
+ template<typename T>
778
+ any_acl_resource make_acl_resource(T* ptr) {
779
+ return any_acl_resource(
780
+ static_cast<void*>(ptr),
781
+ [](void* p) {
782
+ acl_resource_traits<T>::destroy(p);
783
+ }
784
+ );
528
785
  }
529
786
 
530
- // Activation functions template.
531
- template <aclnnStatus getWorkspaceSize(const aclTensor*, aclTensor*, uint64_t*,
532
- aclOpExecutor**),
533
- aclnnStatus execute(void*, uint64_t, aclOpExecutor*,
534
- const aclrtStream)>
535
- void ggml_cann_activation(ggml_backend_cann_context& ctx, ggml_tensor* dst) {
536
- ggml_tensor* src = dst->src[0];
787
+ /**
788
+ * @brief Registers multiple ACL resources into a vector for lifetime management.
789
+ *
790
+ * @tparam Args Variadic list of ACL resource types.
791
+ * @param vec Target vector to hold ACL resources.
792
+ * @param args Raw pointers to ACL resources.
793
+ */
794
+ template<typename... Args>
795
+ void register_acl_resources(std::vector<any_acl_resource>& vec, Args*... args) {
796
+ (vec.emplace_back(make_acl_resource(args)), ...);
797
+ }
537
798
 
538
- GGML_ASSERT(src->type == GGML_TYPE_F32);
539
- GGML_ASSERT(dst->type == GGML_TYPE_F32);
799
+ /**
800
+ * @brief Task class that wraps the execution of an aclnn function call.
801
+ */
802
+ class aclnn_task : public cann_task {
803
+ public:
804
+ aclnn_task(aclnn_func_t aclnn_func, void * workspace_addr,
805
+ uint64_t workspace_size, aclOpExecutor * executor,
806
+ aclrtStream stream) :
807
+ aclnn_func_(aclnn_func),
808
+ workspace_addr_(workspace_addr),
809
+ workspace_size_(workspace_size),
810
+ executor_(executor),
811
+ stream_(stream) {}
812
+ virtual void run_task() override {
813
+ ACL_CHECK(aclnn_func_(workspace_addr_, workspace_size_, executor_, stream_));
814
+ }
815
+ private:
816
+ aclnn_func_t aclnn_func_;
817
+ void * workspace_addr_;
818
+ uint64_t workspace_size_;
819
+ aclOpExecutor * executor_;
820
+ aclrtStream stream_;
821
+ };
540
822
 
541
- aclTensor* acl_src = ggml_cann_create_tensor(src);
542
- aclTensor* acl_dst = ggml_cann_create_tensor(dst);
823
+ /**
824
+ * @brief Task class that releases ACL resources after usage.
825
+ */
826
+ class release_resource_task : public cann_task {
827
+ public:
828
+ release_resource_task(std::vector<any_acl_resource>&& resources){
829
+ resource_ = std::move(resources);
830
+ }
543
831
 
544
- uint64_t workspaceSize = 0;
545
- aclOpExecutor* executor;
546
- void* workspaceAddr = nullptr;
832
+ virtual void run_task() override {
833
+ resource_.clear();
834
+ }
835
+ private:
836
+ std::vector<any_acl_resource> resource_;
837
+ };
838
+
839
+ /**
840
+ * @brief Task class for performing asynchronous memory copy operations.
841
+ */
842
+ class async_memcpy_task : public cann_task {
843
+ public:
844
+ async_memcpy_task(void* dst, const void* src, size_t size,
845
+ aclrtMemcpyKind kind, aclrtStream stream)
846
+ : dst_(dst), src_(src), size_(size), kind_(kind), stream_(stream) {}
547
847
 
548
- ACL_CHECK(getWorkspaceSize(acl_src, acl_dst, &workspaceSize, &executor));
549
- if (workspaceSize > 0) {
550
- ggml_cann_pool_alloc workspace_allocator(ctx.pool(), workspaceSize);
551
- workspaceAddr = workspace_allocator.get();
848
+ virtual void run_task() override {
849
+ ACL_CHECK(aclrtMemcpyAsync(dst_, size_, src_, size_, kind_, stream_));
552
850
  }
851
+ private:
852
+ void* dst_;
853
+ const void* src_;
854
+ size_t size_;
855
+ aclrtMemcpyKind kind_;
856
+ aclrtStream stream_;
857
+ };
553
858
 
554
- aclrtStream main_stream = ctx.stream();
555
- ACL_CHECK(execute(workspaceAddr, workspaceSize, executor, main_stream));
859
+ /**
860
+ * @brief Task class for performing asynchronous memory set operations.
861
+ */
862
+ class async_memset_task : public cann_task {
863
+ public:
864
+ async_memset_task(void* buffer, size_t size, int32_t value, aclrtStream stream)
865
+ : buffer_(buffer), size_(size), value_(value), stream_(stream) {}
556
866
 
557
- ACL_CHECK(aclDestroyTensor(acl_src));
558
- ACL_CHECK(aclDestroyTensor(acl_dst));
559
- }
867
+ virtual void run_task() override {
868
+ ACL_CHECK(aclrtMemsetAsync(buffer_, size_, value_, size_, stream_));
869
+ }
870
+ private:
871
+ void* buffer_;
872
+ size_t size_;
873
+ int32_t value_;
874
+ aclrtStream stream_;
875
+ };
560
876
 
561
- // Activation functions template for const aclTensors.
562
- template <aclnnStatus getWorkspaceSize(const aclTensor*, const aclTensor*,
563
- uint64_t*, aclOpExecutor**),
564
- aclnnStatus execute(void*, uint64_t, aclOpExecutor*,
565
- const aclrtStream)>
566
- void ggml_cann_activation(ggml_backend_cann_context& ctx, ggml_tensor* dst) {
567
- ggml_tensor* src = dst->src[0];
877
+ /**
878
+ * @brief Launches an asynchronous task using the memory allocator.
879
+ *
880
+ * This macro submit an asynchronous task on the specified stream.
881
+ * The task uses memory allocated by the allocator. It is guaranteed
882
+ * that the memory will not be accessed by other tasks until this task
883
+ * completes, due to the sequential execution order within the same stream.
884
+ *
885
+ * @param OP_NAME aclnn operator name.
886
+ * @param args Additional arguments required by the task.
887
+ *
888
+ * @note
889
+ * Memory from the allocator will be "freed" immediately and can be
890
+ * reallocated to other pointers. However, it won't be accessed by any
891
+ * other task before this asynchronous task ends, because all tasks in the
892
+ * same stream are executed in queue order.
893
+ */
568
894
 
569
- GGML_ASSERT(src->type == GGML_TYPE_F32);
570
- GGML_ASSERT(dst->type == GGML_TYPE_F32);
895
+ #define GGML_CANN_CALL_ACLNN_OP(CTX, OP_NAME, ...) \
896
+ do { \
897
+ uint64_t workspaceSize = 0; \
898
+ aclOpExecutor * executor; \
899
+ void * workspaceAddr = nullptr; \
900
+ ACL_CHECK(aclnn##OP_NAME##GetWorkspaceSize(__VA_ARGS__, &workspaceSize, &executor));\
901
+ /* workspace should alloced in main thread to keep malloc order when using vmm. */ \
902
+ if (workspaceSize > 0) { \
903
+ ggml_cann_pool_alloc workspace_allocator(CTX.pool(), workspaceSize); \
904
+ workspaceAddr = workspace_allocator.get(); \
905
+ } \
906
+ if (CTX.async_mode) { \
907
+ auto task = \
908
+ std::make_unique<aclnn_task>(aclnn##OP_NAME, workspaceAddr, workspaceSize, \
909
+ executor, CTX.stream()); \
910
+ CTX.task_queue.submit_task(std::move(task)); \
911
+ } else { \
912
+ ACL_CHECK(aclnn##OP_NAME(workspaceAddr, workspaceSize, executor, CTX.stream()));\
913
+ } \
914
+ } while (0)
571
915
 
572
- aclTensor* acl_src = ggml_cann_create_tensor(src);
573
- aclTensor* acl_dst = ggml_cann_create_tensor(dst);
916
+ /**
917
+ * @brief Registers and releases multiple ACL resources, optionally deferring the release
918
+ * using a task.
919
+ *
920
+ * @tparam Args Types of the ACL resources.
921
+ * @param ctx Backend context which manages task submission and async mode.
922
+ * @param args Pointers to ACL resources to be released.
923
+ */
924
+ template <typename... Args>
925
+ void ggml_cann_release_resources(ggml_backend_cann_context & ctx, Args &&... args) {
926
+ std::vector<any_acl_resource> resources;
927
+ register_acl_resources(resources, std::forward<Args>(args)...);
928
+ if(ctx.async_mode) {
929
+ auto task = std::make_unique<release_resource_task>(std::move(resources));
930
+ ctx.task_queue.submit_task(std::move(task));
931
+ }
932
+ }
933
+
934
+ /**
935
+ * @brief Performs an asynchronous memory copy operation, optionally deferred via task submission.
936
+ *
937
+ * @param ctx Backend context containing stream and async configuration.
938
+ * @param dst Destination memory address.
939
+ * @param src Source memory address.
940
+ * @param len Size of memory to copy (in bytes).
941
+ * @param kind Type of memory copy (host-to-device, device-to-host, etc).
942
+ */
943
+ inline void ggml_cann_async_memcpy(ggml_backend_cann_context & ctx, void * dst,
944
+ const void * src, size_t len, aclrtMemcpyKind kind) {
945
+ if (ctx.async_mode) {
946
+ auto task = std::make_unique<async_memcpy_task>(dst, const_cast<void *>(src), len, kind, ctx.stream());
947
+ ctx.task_queue.submit_task(std::move(task));
948
+ } else {
949
+ ACL_CHECK(aclrtMemcpyAsync(dst, len, src, len, kind, ctx.stream()));
950
+ }
951
+ }
574
952
 
575
- uint64_t workspaceSize = 0;
576
- aclOpExecutor* executor;
577
- void* workspaceAddr = nullptr;
953
+ inline void ggml_cann_async_memcpy(ggml_backend_cann_context * ctx, void * dst,
954
+ const void * src, size_t len, aclrtMemcpyKind kind) {
955
+ if (ctx->async_mode) {
956
+ auto task = std::make_unique<async_memcpy_task>(dst, const_cast<void *>(src), len, kind, ctx->stream());
957
+ ctx->task_queue.submit_task(std::move(task));
958
+ } else {
959
+ ACL_CHECK(aclrtMemcpyAsync(dst, len, src, len, kind, ctx->stream()));
960
+ }
961
+ }
578
962
 
579
- ACL_CHECK(getWorkspaceSize(acl_src, acl_dst, &workspaceSize, &executor));
580
- if (workspaceSize > 0) {
581
- ggml_cann_pool_alloc workspace_allocator(ctx.pool(), workspaceSize);
582
- workspaceAddr = workspace_allocator.get();
963
+ /**
964
+ * @brief Performs an asynchronous memory set operation, optionally deferred via task submission.
965
+ *
966
+ * @param ctx Backend context containing stream and async configuration.
967
+ * @param buffer Memory buffer to be set.
968
+ * @param size Size of the memory buffer (in bytes).
969
+ * @param value Value to set in the buffer.
970
+ */
971
+ inline void ggml_cann_async_memset(ggml_backend_cann_context & ctx, void * buffer,
972
+ size_t size, int value) {
973
+ if (ctx.async_mode) {
974
+ auto task = std::make_unique<async_memset_task>(buffer, size, value, ctx.stream());
975
+ ctx.task_queue.submit_task(std::move(task));
976
+ } else {
977
+ ACL_CHECK(aclrtMemsetAsync(buffer, size, value, size, ctx.stream()));
583
978
  }
979
+ }
584
980
 
585
- aclrtStream main_stream = ctx.stream();
586
- ACL_CHECK(execute(workspaceAddr, workspaceSize, executor, main_stream));
981
+ /**
982
+ * @brief Applies a element-wise operation to two input tensors using the CANN
983
+ * backend.
984
+ *
985
+ * This templated function takes a binary operator and applies it to two source
986
+ * tensors
987
+ * associated with the destination tensor. The function handles broadcasting as
988
+ * needed.
989
+ *
990
+ * @tparam binary_op A callable object (e.g., lambda or function pointer) representing
991
+ * the binary operation to be performed. It must take three arguments:
992
+ * (ggml_backend_cann_context&, aclTensor*, aclTensor*, aclTensor*).
993
+ *
994
+ * @param ctx The CANN backend context used to manage execution and resources.
995
+ * @param dst The destination tensor.
996
+ */
997
+ template <auto binary_op>
998
+ void ggml_cann_binary_op(ggml_backend_cann_context& ctx, ggml_tensor* dst) {
999
+ ggml_tensor* src0 = dst->src[0];
1000
+ ggml_tensor* src1 = dst->src[1];
587
1001
 
588
- ACL_CHECK(aclDestroyTensor(acl_src));
589
- ACL_CHECK(aclDestroyTensor(acl_dst));
1002
+ aclTensor* acl_src0;
1003
+ aclTensor* acl_src1;
1004
+ aclTensor* acl_dst;
1005
+
1006
+ // Need bcast
1007
+ bcast_shape(src0, src1, dst, &acl_src0, &acl_src1, &acl_dst);
1008
+ binary_op(ctx, acl_src0, acl_src1, acl_dst);
1009
+
1010
+ ggml_cann_release_resources(ctx, acl_src0, acl_src1, acl_dst);
590
1011
  }
591
1012
 
1013
+
1014
+ /**
1015
+ * @brief Applies a unary operation to an input tensor using the CANN backend.
1016
+ *
1017
+ * This templated function applies a unary operator to the source tensor of `dst`
1018
+ * and stores the result in the destination tensor.
1019
+ *
1020
+ * @tparam unary_op A callable with the signature:
1021
+ * void(ggml_backend_cann_context&, aclTensor*, aclTensor*)
1022
+ * where the first aclTensor is the source and the second is the destination.
1023
+ * @param ctx The CANN backend context for managing resources and execution.
1024
+ * @param dst The destination tensor. Its src[0] is treated as the input tensor.
1025
+ */
1026
+ template <void unary_op(ggml_backend_cann_context&, aclTensor*, aclTensor*)>
1027
+ void ggml_cann_unary_op(ggml_backend_cann_context& ctx, ggml_tensor* dst) {
1028
+ ggml_tensor* src = dst->src[0];
1029
+
1030
+ aclTensor* acl_src = ggml_cann_create_tensor(src);
1031
+ aclTensor* acl_dst = ggml_cann_create_tensor(dst);
1032
+
1033
+ unary_op(ctx, acl_src, acl_dst);
1034
+ ggml_cann_release_resources(ctx, acl_src, acl_dst);
1035
+ }
1036
+
1037
+ /**
1038
+ * @brief Applies a unary operation to a ggml tensor using the CANN backend.
1039
+ *
1040
+ * @details This function performs a unary operation on the input tensor using
1041
+ * a user-provided lambda or callable object `unary_op`, which accepts the CANN
1042
+ * context and two ACL tensors (source and destination). Internally, this function
1043
+ * creates ACL representations of the ggml tensors and invokes the unary operation.
1044
+ * The result is stored in the destination tensor `dst`. This utility abstracts the
1045
+ * common boilerplate of tensor conversion and cleanup when implementing unary ops.
1046
+ *
1047
+ * @param unary_op A callable that performs the unary operation using CANN APIs.
1048
+ * @param ctx The CANN context used for operations.
1049
+ * @param dst The destination tensor where the result will be stored.
1050
+ * The source tensor is retrieved from `dst->src[0]`.
1051
+ */
1052
+ void ggml_cann_unary_op(
1053
+ std::function<void(ggml_backend_cann_context&, aclTensor*, aclTensor*)> unary_op,
1054
+ ggml_backend_cann_context& ctx, ggml_tensor* dst);
1055
+
1056
+ /**
1057
+ * @brief Helper macro to invoke a unary ACL operation using ggml_cann_unary_op.
1058
+ *
1059
+ * This macro defines an inline lambda wrapping a specific ACL operation name,
1060
+ * and passes it to the templated ggml_cann_unary_op function. It simplifies
1061
+ * calling unary ops by hiding the lambda boilerplate.
1062
+ *
1063
+ * Internally, the lambda will call:
1064
+ * @code
1065
+ * GGML_CANN_CALL_ACLNN_OP(ctx, OP_NAME, acl_src, acl_dst);
1066
+ * @endcode
1067
+ *
1068
+ * @param OP_NAME The name of the ACL unary operator to invoke via GGML_CANN_CALL_ACLNN_OP.
1069
+ *
1070
+ * @see ggml_cann_unary_op
1071
+ * @see GGML_CANN_CALL_ACLNN_OP
1072
+ */
1073
+ #define GGML_CANN_CALL_UNARY_OP(OP_NAME) \
1074
+ do { \
1075
+ auto lambda = [](ggml_backend_cann_context& ctx, \
1076
+ aclTensor* acl_src, \
1077
+ aclTensor* acl_dst) { \
1078
+ GGML_CANN_CALL_ACLNN_OP(ctx, OP_NAME, acl_src, acl_dst); \
1079
+ }; \
1080
+ ggml_cann_unary_op(lambda, ctx, dst); \
1081
+ } \
1082
+ while (0)
592
1083
  #endif // CANN_ACLNN_OPS