llama-cpp-capacitor 0.0.6 → 0.0.8

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 (150) hide show
  1. package/android/src/main/CMakeLists.txt +9 -9
  2. package/cpp/LICENSE +21 -0
  3. package/cpp/README.md +4 -0
  4. package/cpp/anyascii.c +22223 -0
  5. package/cpp/anyascii.h +42 -0
  6. package/cpp/chat-parser.cpp +393 -0
  7. package/cpp/chat-parser.h +120 -0
  8. package/cpp/chat.cpp +2315 -0
  9. package/cpp/chat.h +221 -0
  10. package/cpp/common.cpp +1619 -0
  11. package/cpp/common.h +744 -0
  12. package/cpp/ggml-alloc.c +1028 -0
  13. package/cpp/ggml-alloc.h +76 -0
  14. package/cpp/ggml-backend-impl.h +255 -0
  15. package/cpp/ggml-backend-reg.cpp +600 -0
  16. package/cpp/ggml-backend.cpp +2118 -0
  17. package/cpp/ggml-backend.h +354 -0
  18. package/cpp/ggml-common.h +1878 -0
  19. package/cpp/ggml-cpp.h +39 -0
  20. package/cpp/ggml-cpu/amx/amx.cpp +221 -0
  21. package/cpp/ggml-cpu/amx/amx.h +8 -0
  22. package/cpp/ggml-cpu/amx/common.h +91 -0
  23. package/cpp/ggml-cpu/amx/mmq.cpp +2512 -0
  24. package/cpp/ggml-cpu/amx/mmq.h +10 -0
  25. package/cpp/ggml-cpu/arch/arm/cpu-feats.cpp +94 -0
  26. package/cpp/ggml-cpu/arch/arm/quants.c +3650 -0
  27. package/cpp/ggml-cpu/arch/arm/repack.cpp +1891 -0
  28. package/cpp/ggml-cpu/arch/x86/cpu-feats.cpp +327 -0
  29. package/cpp/ggml-cpu/arch/x86/quants.c +3820 -0
  30. package/cpp/ggml-cpu/arch/x86/repack.cpp +6307 -0
  31. package/cpp/ggml-cpu/arch-fallback.h +215 -0
  32. package/cpp/ggml-cpu/binary-ops.cpp +158 -0
  33. package/cpp/ggml-cpu/binary-ops.h +16 -0
  34. package/cpp/ggml-cpu/common.h +73 -0
  35. package/cpp/ggml-cpu/ggml-cpu-impl.h +525 -0
  36. package/cpp/ggml-cpu/ggml-cpu.c +3578 -0
  37. package/cpp/ggml-cpu/ggml-cpu.cpp +672 -0
  38. package/cpp/ggml-cpu/ops.cpp +10587 -0
  39. package/cpp/ggml-cpu/ops.h +114 -0
  40. package/cpp/ggml-cpu/quants.c +1193 -0
  41. package/cpp/ggml-cpu/quants.h +97 -0
  42. package/cpp/ggml-cpu/repack.cpp +1982 -0
  43. package/cpp/ggml-cpu/repack.h +120 -0
  44. package/cpp/ggml-cpu/simd-mappings.h +1184 -0
  45. package/cpp/ggml-cpu/traits.cpp +36 -0
  46. package/cpp/ggml-cpu/traits.h +38 -0
  47. package/cpp/ggml-cpu/unary-ops.cpp +186 -0
  48. package/cpp/ggml-cpu/unary-ops.h +28 -0
  49. package/cpp/ggml-cpu/vec.cpp +348 -0
  50. package/cpp/ggml-cpu/vec.h +1121 -0
  51. package/cpp/ggml-cpu.h +145 -0
  52. package/cpp/ggml-impl.h +622 -0
  53. package/cpp/ggml-metal-impl.h +688 -0
  54. package/cpp/ggml-metal.h +66 -0
  55. package/cpp/ggml-metal.m +6833 -0
  56. package/cpp/ggml-opt.cpp +1093 -0
  57. package/cpp/ggml-opt.h +256 -0
  58. package/cpp/ggml-quants.c +5324 -0
  59. package/cpp/ggml-quants.h +106 -0
  60. package/cpp/ggml-threading.cpp +12 -0
  61. package/cpp/ggml-threading.h +14 -0
  62. package/cpp/ggml.c +7108 -0
  63. package/cpp/ggml.h +2492 -0
  64. package/cpp/gguf.cpp +1358 -0
  65. package/cpp/gguf.h +202 -0
  66. package/cpp/json-partial.cpp +256 -0
  67. package/cpp/json-partial.h +38 -0
  68. package/cpp/json-schema-to-grammar.cpp +985 -0
  69. package/cpp/json-schema-to-grammar.h +21 -0
  70. package/cpp/llama-adapter.cpp +388 -0
  71. package/cpp/llama-adapter.h +76 -0
  72. package/cpp/llama-arch.cpp +2355 -0
  73. package/cpp/llama-arch.h +499 -0
  74. package/cpp/llama-batch.cpp +875 -0
  75. package/cpp/llama-batch.h +160 -0
  76. package/cpp/llama-chat.cpp +783 -0
  77. package/cpp/llama-chat.h +65 -0
  78. package/cpp/llama-context.cpp +2748 -0
  79. package/cpp/llama-context.h +306 -0
  80. package/cpp/llama-cparams.cpp +5 -0
  81. package/cpp/llama-cparams.h +41 -0
  82. package/cpp/llama-cpp.h +30 -0
  83. package/cpp/llama-grammar.cpp +1229 -0
  84. package/cpp/llama-grammar.h +173 -0
  85. package/cpp/llama-graph.cpp +1891 -0
  86. package/cpp/llama-graph.h +810 -0
  87. package/cpp/llama-hparams.cpp +180 -0
  88. package/cpp/llama-hparams.h +233 -0
  89. package/cpp/llama-impl.cpp +167 -0
  90. package/cpp/llama-impl.h +61 -0
  91. package/cpp/llama-io.cpp +15 -0
  92. package/cpp/llama-io.h +35 -0
  93. package/cpp/llama-kv-cache-iswa.cpp +318 -0
  94. package/cpp/llama-kv-cache-iswa.h +135 -0
  95. package/cpp/llama-kv-cache.cpp +2059 -0
  96. package/cpp/llama-kv-cache.h +374 -0
  97. package/cpp/llama-kv-cells.h +491 -0
  98. package/cpp/llama-memory-hybrid.cpp +258 -0
  99. package/cpp/llama-memory-hybrid.h +137 -0
  100. package/cpp/llama-memory-recurrent.cpp +1146 -0
  101. package/cpp/llama-memory-recurrent.h +179 -0
  102. package/cpp/llama-memory.cpp +59 -0
  103. package/cpp/llama-memory.h +119 -0
  104. package/cpp/llama-mmap.cpp +600 -0
  105. package/cpp/llama-mmap.h +68 -0
  106. package/cpp/llama-model-loader.cpp +1164 -0
  107. package/cpp/llama-model-loader.h +170 -0
  108. package/cpp/llama-model-saver.cpp +282 -0
  109. package/cpp/llama-model-saver.h +37 -0
  110. package/cpp/llama-model.cpp +19042 -0
  111. package/cpp/llama-model.h +491 -0
  112. package/cpp/llama-sampling.cpp +2575 -0
  113. package/cpp/llama-sampling.h +32 -0
  114. package/cpp/llama-vocab.cpp +3792 -0
  115. package/cpp/llama-vocab.h +176 -0
  116. package/cpp/llama.cpp +358 -0
  117. package/cpp/llama.h +1373 -0
  118. package/cpp/log.cpp +427 -0
  119. package/cpp/log.h +103 -0
  120. package/cpp/minja/chat-template.hpp +550 -0
  121. package/cpp/minja/minja.hpp +3009 -0
  122. package/cpp/nlohmann/json.hpp +25526 -0
  123. package/cpp/nlohmann/json_fwd.hpp +187 -0
  124. package/cpp/regex-partial.cpp +204 -0
  125. package/cpp/regex-partial.h +56 -0
  126. package/cpp/rn-completion.cpp +681 -0
  127. package/cpp/rn-completion.h +116 -0
  128. package/cpp/rn-llama.cpp +345 -0
  129. package/cpp/rn-llama.h +149 -0
  130. package/cpp/rn-mtmd.hpp +602 -0
  131. package/cpp/rn-tts.cpp +591 -0
  132. package/cpp/rn-tts.h +59 -0
  133. package/cpp/sampling.cpp +579 -0
  134. package/cpp/sampling.h +107 -0
  135. package/cpp/tools/mtmd/clip-impl.h +473 -0
  136. package/cpp/tools/mtmd/clip.cpp +4322 -0
  137. package/cpp/tools/mtmd/clip.h +106 -0
  138. package/cpp/tools/mtmd/miniaudio/miniaudio.h +93468 -0
  139. package/cpp/tools/mtmd/mtmd-audio.cpp +769 -0
  140. package/cpp/tools/mtmd/mtmd-audio.h +47 -0
  141. package/cpp/tools/mtmd/mtmd-helper.cpp +460 -0
  142. package/cpp/tools/mtmd/mtmd-helper.h +91 -0
  143. package/cpp/tools/mtmd/mtmd.cpp +1066 -0
  144. package/cpp/tools/mtmd/mtmd.h +298 -0
  145. package/cpp/tools/mtmd/stb/stb_image.h +7988 -0
  146. package/cpp/unicode-data.cpp +7034 -0
  147. package/cpp/unicode-data.h +20 -0
  148. package/cpp/unicode.cpp +1061 -0
  149. package/cpp/unicode.h +68 -0
  150. package/package.json +2 -1
@@ -0,0 +1,525 @@
1
+ #pragma once
2
+
3
+ // GGML CPU internal header
4
+
5
+ #include "ggml.h"
6
+ #include "ggml-impl.h"
7
+
8
+ #include <stdlib.h> // load `stdlib.h` before other headers to work around MinGW bug: https://sourceforge.net/p/mingw-w64/bugs/192/
9
+ //#include <stddef.h>
10
+ #include <stdbool.h>
11
+ #include <string.h> // memcpy
12
+ #include <math.h> // fabsf
13
+
14
+ #ifdef __cplusplus
15
+ extern "C" {
16
+ #endif
17
+
18
+ struct lm_ggml_compute_params {
19
+ // ith = thread index, nth = number of threads
20
+ int ith, nth;
21
+
22
+ // work buffer for all threads
23
+ size_t wsize;
24
+ void * wdata;
25
+
26
+ struct lm_ggml_threadpool * threadpool;
27
+ };
28
+
29
+
30
+ #if defined(_MSC_VER)
31
+
32
+ #define m512bh(p) p
33
+ #define m512i(p) p
34
+
35
+ #else
36
+
37
+ #define m512bh(p) (__m512bh)(p)
38
+ #define m512i(p) (__m512i)(p)
39
+
40
+ #endif
41
+
42
+ // __FMA__ and __F16C__ are not defined in MSVC, however they are implied with AVX2/AVX512
43
+ #if defined(_MSC_VER) && (defined(__AVX2__) || defined(__AVX512F__))
44
+ #ifndef __FMA__
45
+ #define __FMA__
46
+ #endif
47
+ #ifndef __F16C__
48
+ #define __F16C__
49
+ #endif
50
+ #endif
51
+
52
+ // __SSE3__ and __SSSE3__ are not defined in MSVC, but SSE3/SSSE3 are present when AVX/AVX2/AVX512 are available
53
+ #if defined(_MSC_VER) && (defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__))
54
+ #ifndef __SSE3__
55
+ #define __SSE3__
56
+ #endif
57
+ #ifndef __SSSE3__
58
+ #define __SSSE3__
59
+ #endif
60
+ #endif
61
+
62
+ #if defined(__s390x__) && defined(__VEC__)
63
+ #ifndef __VXE__
64
+ #define __VXE__
65
+ #endif // __VXE__
66
+ #ifndef __VXE2__
67
+ #define __VXE2__
68
+ #endif // __VXE2__
69
+ #endif // __s390x__ && __VEC__
70
+
71
+ #if defined(__s390x__) && defined(LM_GGML_NNPA)
72
+ #ifndef __NNPA__
73
+ #define __NNPA__
74
+ #endif // __NNPA__
75
+ #endif // __s390x__ && LM_GGML_NNPA
76
+
77
+ #if defined(__ARM_FEATURE_SVE)
78
+ #include <sys/prctl.h>
79
+ #endif
80
+
81
+ #if defined(__ARM_NEON)
82
+
83
+ // ref: https://github.com/ggml-org/llama.cpp/pull/5404
84
+ #ifdef _MSC_VER
85
+ #define lm_ggml_vld1q_u32(w,x,y,z) { ((w) + ((uint64_t)(x) << 32)), ((y) + ((uint64_t)(z) << 32)) }
86
+ #else
87
+ #define lm_ggml_vld1q_u32(w,x,y,z) { (w), (x), (y), (z) }
88
+ #endif // _MSC_VER
89
+
90
+ #if !defined(__aarch64__)
91
+
92
+ // 32-bit ARM compatibility
93
+
94
+ // vaddlvq_s16
95
+ // vpaddq_s16
96
+ // vpaddq_s32
97
+ // vaddvq_s32
98
+ // vaddvq_f32
99
+ // vmaxvq_f32
100
+ // vcvtnq_s32_f32
101
+ // vzip1_u8
102
+ // vzip2_u8
103
+
104
+ inline static int32_t vaddlvq_s16(int16x8_t v) {
105
+ int32x4_t v0 = vreinterpretq_s32_s64(vpaddlq_s32(vpaddlq_s16(v)));
106
+ return vgetq_lane_s32(v0, 0) + vgetq_lane_s32(v0, 2);
107
+ }
108
+
109
+ inline static int16x8_t vpaddq_s16(int16x8_t a, int16x8_t b) {
110
+ int16x4_t a0 = vpadd_s16(vget_low_s16(a), vget_high_s16(a));
111
+ int16x4_t b0 = vpadd_s16(vget_low_s16(b), vget_high_s16(b));
112
+ return vcombine_s16(a0, b0);
113
+ }
114
+
115
+ inline static int32x4_t vpaddq_s32(int32x4_t a, int32x4_t b) {
116
+ int32x2_t a0 = vpadd_s32(vget_low_s32(a), vget_high_s32(a));
117
+ int32x2_t b0 = vpadd_s32(vget_low_s32(b), vget_high_s32(b));
118
+ return vcombine_s32(a0, b0);
119
+ }
120
+
121
+ inline static int32_t vaddvq_s32(int32x4_t v) {
122
+ return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
123
+ }
124
+
125
+ inline static float vaddvq_f32(float32x4_t v) {
126
+ return vgetq_lane_f32(v, 0) + vgetq_lane_f32(v, 1) + vgetq_lane_f32(v, 2) + vgetq_lane_f32(v, 3);
127
+ }
128
+
129
+ inline static float vmaxvq_f32(float32x4_t v) {
130
+ return
131
+ MAX(MAX(vgetq_lane_f32(v, 0), vgetq_lane_f32(v, 1)),
132
+ MAX(vgetq_lane_f32(v, 2), vgetq_lane_f32(v, 3)));
133
+ }
134
+
135
+ inline static int32x4_t vcvtnq_s32_f32(float32x4_t v) {
136
+ int32x4_t res;
137
+
138
+ res[0] = roundf(vgetq_lane_f32(v, 0));
139
+ res[1] = roundf(vgetq_lane_f32(v, 1));
140
+ res[2] = roundf(vgetq_lane_f32(v, 2));
141
+ res[3] = roundf(vgetq_lane_f32(v, 3));
142
+
143
+ return res;
144
+ }
145
+
146
+ inline static uint8x8_t vzip1_u8(uint8x8_t a, uint8x8_t b) {
147
+ uint8x8_t res;
148
+
149
+ res[0] = a[0]; res[1] = b[0];
150
+ res[2] = a[1]; res[3] = b[1];
151
+ res[4] = a[2]; res[5] = b[2];
152
+ res[6] = a[3]; res[7] = b[3];
153
+
154
+ return res;
155
+ }
156
+
157
+ inline static uint8x8_t vzip2_u8(uint8x8_t a, uint8x8_t b) {
158
+ uint8x8_t res;
159
+
160
+ res[0] = a[4]; res[1] = b[4];
161
+ res[2] = a[5]; res[3] = b[5];
162
+ res[4] = a[6]; res[5] = b[6];
163
+ res[6] = a[7]; res[7] = b[7];
164
+
165
+ return res;
166
+ }
167
+
168
+ // vld1q_s16_x2
169
+ // vld1q_u8_x2
170
+ // vld1q_u8_x4
171
+ // vld1q_s8_x2
172
+ // vld1q_s8_x4
173
+ // TODO: double-check these work correctly
174
+
175
+ typedef struct lm_ggml_int16x8x2_t {
176
+ int16x8_t val[2];
177
+ } lm_ggml_int16x8x2_t;
178
+
179
+ inline static lm_ggml_int16x8x2_t lm_ggml_vld1q_s16_x2(const int16_t * ptr) {
180
+ lm_ggml_int16x8x2_t res;
181
+
182
+ res.val[0] = vld1q_s16(ptr + 0);
183
+ res.val[1] = vld1q_s16(ptr + 8);
184
+
185
+ return res;
186
+ }
187
+
188
+ typedef struct lm_ggml_uint8x16x2_t {
189
+ uint8x16_t val[2];
190
+ } lm_ggml_uint8x16x2_t;
191
+
192
+ inline static lm_ggml_uint8x16x2_t lm_ggml_vld1q_u8_x2(const uint8_t * ptr) {
193
+ lm_ggml_uint8x16x2_t res;
194
+
195
+ res.val[0] = vld1q_u8(ptr + 0);
196
+ res.val[1] = vld1q_u8(ptr + 16);
197
+
198
+ return res;
199
+ }
200
+
201
+ typedef struct lm_ggml_uint8x16x4_t {
202
+ uint8x16_t val[4];
203
+ } lm_ggml_uint8x16x4_t;
204
+
205
+ inline static lm_ggml_uint8x16x4_t lm_ggml_vld1q_u8_x4(const uint8_t * ptr) {
206
+ lm_ggml_uint8x16x4_t res;
207
+
208
+ res.val[0] = vld1q_u8(ptr + 0);
209
+ res.val[1] = vld1q_u8(ptr + 16);
210
+ res.val[2] = vld1q_u8(ptr + 32);
211
+ res.val[3] = vld1q_u8(ptr + 48);
212
+
213
+ return res;
214
+ }
215
+
216
+ typedef struct lm_ggml_int8x16x2_t {
217
+ int8x16_t val[2];
218
+ } lm_ggml_int8x16x2_t;
219
+
220
+ inline static lm_ggml_int8x16x2_t lm_ggml_vld1q_s8_x2(const int8_t * ptr) {
221
+ lm_ggml_int8x16x2_t res;
222
+
223
+ res.val[0] = vld1q_s8(ptr + 0);
224
+ res.val[1] = vld1q_s8(ptr + 16);
225
+
226
+ return res;
227
+ }
228
+
229
+ typedef struct lm_ggml_int8x16x4_t {
230
+ int8x16_t val[4];
231
+ } lm_ggml_int8x16x4_t;
232
+
233
+ inline static lm_ggml_int8x16x4_t lm_ggml_vld1q_s8_x4(const int8_t * ptr) {
234
+ lm_ggml_int8x16x4_t res;
235
+
236
+ res.val[0] = vld1q_s8(ptr + 0);
237
+ res.val[1] = vld1q_s8(ptr + 16);
238
+ res.val[2] = vld1q_s8(ptr + 32);
239
+ res.val[3] = vld1q_s8(ptr + 48);
240
+
241
+ return res;
242
+ }
243
+
244
+ // NOTE: not tested
245
+ inline static int8x16_t lm_ggml_vqtbl1q_s8(int8x16_t a, uint8x16_t b) {
246
+ int8x16_t res;
247
+
248
+ res[ 0] = a[b[ 0]];
249
+ res[ 1] = a[b[ 1]];
250
+ res[ 2] = a[b[ 2]];
251
+ res[ 3] = a[b[ 3]];
252
+ res[ 4] = a[b[ 4]];
253
+ res[ 5] = a[b[ 5]];
254
+ res[ 6] = a[b[ 6]];
255
+ res[ 7] = a[b[ 7]];
256
+ res[ 8] = a[b[ 8]];
257
+ res[ 9] = a[b[ 9]];
258
+ res[10] = a[b[10]];
259
+ res[11] = a[b[11]];
260
+ res[12] = a[b[12]];
261
+ res[13] = a[b[13]];
262
+ res[14] = a[b[14]];
263
+ res[15] = a[b[15]];
264
+
265
+ return res;
266
+ }
267
+
268
+ // NOTE: not tested
269
+ inline static uint8x16_t lm_ggml_vqtbl1q_u8(uint8x16_t a, uint8x16_t b) {
270
+ uint8x16_t res;
271
+
272
+ res[ 0] = a[b[ 0]];
273
+ res[ 1] = a[b[ 1]];
274
+ res[ 2] = a[b[ 2]];
275
+ res[ 3] = a[b[ 3]];
276
+ res[ 4] = a[b[ 4]];
277
+ res[ 5] = a[b[ 5]];
278
+ res[ 6] = a[b[ 6]];
279
+ res[ 7] = a[b[ 7]];
280
+ res[ 8] = a[b[ 8]];
281
+ res[ 9] = a[b[ 9]];
282
+ res[10] = a[b[10]];
283
+ res[11] = a[b[11]];
284
+ res[12] = a[b[12]];
285
+ res[13] = a[b[13]];
286
+ res[14] = a[b[14]];
287
+ res[15] = a[b[15]];
288
+
289
+ return res;
290
+ }
291
+
292
+ #else
293
+
294
+ #define lm_ggml_int16x8x2_t int16x8x2_t
295
+ #define lm_ggml_uint8x16x2_t uint8x16x2_t
296
+ #define lm_ggml_uint8x16x4_t uint8x16x4_t
297
+ #define lm_ggml_int8x16x2_t int8x16x2_t
298
+ #define lm_ggml_int8x16x4_t int8x16x4_t
299
+
300
+ #define lm_ggml_vld1q_s16_x2 vld1q_s16_x2
301
+ #define lm_ggml_vld1q_u8_x2 vld1q_u8_x2
302
+ #define lm_ggml_vld1q_u8_x4 vld1q_u8_x4
303
+ #define lm_ggml_vld1q_s8_x2 vld1q_s8_x2
304
+ #define lm_ggml_vld1q_s8_x4 vld1q_s8_x4
305
+ #define lm_ggml_vqtbl1q_s8 vqtbl1q_s8
306
+ #define lm_ggml_vqtbl1q_u8 vqtbl1q_u8
307
+
308
+ #endif // !defined(__aarch64__)
309
+
310
+ #if !defined(__ARM_FEATURE_DOTPROD)
311
+
312
+ inline static int32x4_t lm_ggml_vdotq_s32(int32x4_t acc, int8x16_t a, int8x16_t b) {
313
+ const int16x8_t p0 = vmull_s8(vget_low_s8 (a), vget_low_s8 (b));
314
+ const int16x8_t p1 = vmull_s8(vget_high_s8(a), vget_high_s8(b));
315
+
316
+ return vaddq_s32(acc, vaddq_s32(vpaddlq_s16(p0), vpaddlq_s16(p1)));
317
+ }
318
+
319
+ #else
320
+
321
+ #define lm_ggml_vdotq_s32(a, b, c) vdotq_s32(a, b, c)
322
+
323
+ #endif // !defined(__ARM_FEATURE_DOTPROD)
324
+
325
+ #endif // defined(__ARM_NEON)
326
+
327
+ #ifdef __wasm_simd128__
328
+ #include <wasm_simd128.h>
329
+ #endif
330
+
331
+ #ifdef __POWER9_VECTOR__
332
+ #include <altivec.h>
333
+ #endif
334
+
335
+ #if defined(_MSC_VER) || defined(__MINGW32__)
336
+ #include <intrin.h>
337
+ #elif defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__) || defined(__SSE__)
338
+ #include <immintrin.h>
339
+ #endif
340
+
341
+ #ifdef __riscv_v_intrinsic
342
+ #include <riscv_vector.h>
343
+ #endif
344
+
345
+ #if defined(__loongarch64)
346
+ #if defined(__loongarch_asx)
347
+ #include <lasxintrin.h>
348
+ #endif
349
+ #if defined(__loongarch_sx)
350
+ #include <lsxintrin.h>
351
+ #endif
352
+ #endif
353
+
354
+ #if defined(__VXE__) || defined(__VXE2__)
355
+ #include <vecintrin.h>
356
+
357
+ #define vec_neg(a) (-(a)) // Vector Negate
358
+ #define vec_add(a, b) ((a) + (b)) // Vector Add
359
+ #define vec_sub(a, b) ((a) - (b)) // Vector Subtract
360
+ #define vec_mul(a, b) ((a) * (b)) // Vector Multiply
361
+ #define vec_div(a, b) ((a) / (b)) // Vector Divide
362
+ #define vec_sl(a, b) ((a) << (b)) // Vector Shift Left
363
+ #define vec_sra(a, b) ((a) >> (b)) // Vector Shift Right
364
+ #define vec_sr(a, b) ((a) >> (b)) // Vector Shift Right Algebraic
365
+ #define vec_slo(a, b) vec_slb(a, (b) << 64) // Vector Shift Left by Octet
366
+ #define vec_sro(a, b) vec_srb(a, (b) << 64) // Vector Shift Right by Octet
367
+
368
+ #ifndef vec_and
369
+ #define vec_and(a, b) ((a) & (b)) // Vector AND
370
+ #endif
371
+
372
+ #ifndef vec_or
373
+ #define vec_or(a, b) ((a) | (b)) // Vector OR
374
+ #endif
375
+
376
+ #ifndef vec_xor
377
+ #define vec_xor(a, b) ((a) ^ (b)) // Vector XOR
378
+ #endif
379
+
380
+ typedef signed char char8x16_t __attribute__((vector_size(16)));
381
+ typedef unsigned char uchar8x16_t __attribute__((vector_size(16)));
382
+
383
+ typedef int8_t int8x16_t __attribute__((vector_size(16)));
384
+ typedef int16_t int16x8_t __attribute__((vector_size(16)));
385
+ typedef int32_t int32x4_t __attribute__((vector_size(16)));
386
+
387
+ typedef uint8_t uint8x16_t __attribute__((vector_size(16)));
388
+ typedef uint16_t uint16x8_t __attribute__((vector_size(16)));
389
+ typedef uint32_t uint32x4_t __attribute__((vector_size(16)));
390
+
391
+ typedef float float32x4_t __attribute__((vector_size(16)));
392
+ typedef double double64x2_t __attribute__((vector_size(16)));
393
+
394
+ typedef signed long long long64x2_t __attribute__((vector_size(16)));
395
+ typedef unsigned long long ulong64x2_t __attribute__((vector_size(16)));
396
+
397
+ typedef struct lm_ggml_uint8x16x2_t {
398
+ uint8x16_t val[2];
399
+ } lm_ggml_uint8x16x2_t;
400
+
401
+ inline static lm_ggml_uint8x16x2_t lm_ggml_vec_xl_u8x2(const uint8_t * ptr) {
402
+ lm_ggml_uint8x16x2_t res;
403
+
404
+ res.val[0] = vec_xl( 0, ptr);
405
+ res.val[1] = vec_xl(16, ptr);
406
+
407
+ return res;
408
+ }
409
+
410
+ typedef struct lm_ggml_uint8x16x4_t {
411
+ uint8x16_t val[4];
412
+ } lm_ggml_uint8x16x4_t;
413
+
414
+ inline static lm_ggml_uint8x16x4_t lm_ggml_vec_xl_u8x4(const uint8_t * ptr) {
415
+ lm_ggml_uint8x16x4_t res;
416
+
417
+ res.val[0] = vec_xl( 0, ptr);
418
+ res.val[1] = vec_xl(16, ptr);
419
+ res.val[2] = vec_xl(32, ptr);
420
+ res.val[3] = vec_xl(48, ptr);
421
+
422
+ return res;
423
+ }
424
+
425
+ typedef struct lm_ggml_int8x16x4_t {
426
+ int8x16_t val[4];
427
+ } lm_ggml_int8x16x4_t;
428
+
429
+ inline static lm_ggml_int8x16x4_t lm_ggml_vec_xl_s8x4(const int8_t * ptr) {
430
+ lm_ggml_int8x16x4_t res;
431
+
432
+ res.val[0] = vec_xl( 0, ptr);
433
+ res.val[1] = vec_xl(16, ptr);
434
+ res.val[2] = vec_xl(32, ptr);
435
+ res.val[3] = vec_xl(48, ptr);
436
+
437
+ return res;
438
+ }
439
+
440
+ typedef struct lm_ggml_int16x8x2_t {
441
+ int16x8_t val[2];
442
+ } lm_ggml_int16x8x2_t;
443
+
444
+ inline static lm_ggml_int16x8x2_t lm_ggml_vec_xl_s16x2(const int16_t * ptr) {
445
+ lm_ggml_int16x8x2_t res;
446
+
447
+ res.val[0] = vec_xl( 0, ptr);
448
+ res.val[1] = vec_xl(16, ptr);
449
+
450
+ return res;
451
+ }
452
+
453
+ /*
454
+ ! WARNING: Very slow. Use vec_perm if possible. Refer to iq4_xs
455
+ ! or iq4_nl for example implementation.
456
+ */
457
+ inline static int8x16_t lm_ggml_vec_tbl(int8x16_t a, uint8x16_t b) {
458
+ int8x16_t res;
459
+
460
+ res[ 0] = a[b[ 0]];
461
+ res[ 1] = a[b[ 1]];
462
+ res[ 2] = a[b[ 2]];
463
+ res[ 3] = a[b[ 3]];
464
+ res[ 4] = a[b[ 4]];
465
+ res[ 5] = a[b[ 5]];
466
+ res[ 6] = a[b[ 6]];
467
+ res[ 7] = a[b[ 7]];
468
+ res[ 8] = a[b[ 8]];
469
+ res[ 9] = a[b[ 9]];
470
+ res[10] = a[b[10]];
471
+ res[11] = a[b[11]];
472
+ res[12] = a[b[12]];
473
+ res[13] = a[b[13]];
474
+ res[14] = a[b[14]];
475
+ res[15] = a[b[15]];
476
+
477
+ return res;
478
+ }
479
+
480
+ inline static int16x8_t vec_padd_s16(int16x8_t a, int16x8_t b) {
481
+ const uchar8x16_t v_maske = { 0, 1, 4, 5, 8, 9, 12, 13,
482
+ 16, 17, 20, 21, 24, 25, 28, 29 };
483
+
484
+ const int16x8_t v_abo = vec_pack((int32x4_t)a, (int32x4_t)b);
485
+ const int16x8_t v_abe = vec_perm(a, b, v_maske);
486
+ return v_abo + v_abe;
487
+ }
488
+
489
+ /**
490
+ * @see https://github.com/ggml-org/llama.cpp/pull/14037
491
+ */
492
+ inline float vec_hsum(float32x4_t v) {
493
+ float32x4_t v_temp = v + vec_reve(v);
494
+ return v_temp[0] + v_temp[1];
495
+ }
496
+
497
+ inline static int32x4_t lm_ggml_vec_dot(int32x4_t acc, int8x16_t a, int8x16_t b) {
498
+ const int16x8_t p = vec_mule(a, b) + vec_mulo(a, b);
499
+ return acc + (vec_unpackh(p) + vec_unpackl(p));
500
+ }
501
+
502
+ #endif
503
+
504
+ #if defined(__loongarch_asx)
505
+ /* float type data load instructions */
506
+ static __m128 __lsx_vreplfr2vr_s(const float val) {
507
+ v4f32 res = {val, val, val, val};
508
+ return (__m128)res;
509
+ }
510
+
511
+ static __m256 __lasx_xvreplfr2vr_s(const float val) {
512
+ v8f32 res = {val, val, val, val, val, val, val, val};
513
+ return (__m256)res;
514
+ }
515
+ #endif
516
+
517
+ // TODO: move to ggml-threading
518
+ void lm_ggml_barrier(struct lm_ggml_threadpool * tp);
519
+
520
+ void lm_ggml_threadpool_chunk_set(struct lm_ggml_threadpool * tp, int value);
521
+ int lm_ggml_threadpool_chunk_add(struct lm_ggml_threadpool * tp, int value);
522
+
523
+ #ifdef __cplusplus
524
+ }
525
+ #endif