cui-llama.rn 1.4.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/android/src/main/jni.cpp +9 -9
  2. package/cpp/common.cpp +163 -60
  3. package/cpp/common.h +43 -12
  4. package/cpp/ggml-alloc.c +1042 -1037
  5. package/cpp/ggml-backend-impl.h +255 -256
  6. package/cpp/ggml-backend-reg.cpp +582 -582
  7. package/cpp/ggml-backend.cpp +2002 -2002
  8. package/cpp/ggml-backend.h +354 -352
  9. package/cpp/ggml-common.h +1853 -1853
  10. package/cpp/ggml-cpp.h +39 -39
  11. package/cpp/ggml-cpu-aarch64.cpp +4247 -4247
  12. package/cpp/ggml-cpu-aarch64.h +8 -8
  13. package/cpp/ggml-cpu-impl.h +386 -386
  14. package/cpp/ggml-cpu-quants.c +10920 -10839
  15. package/cpp/ggml-cpu-traits.cpp +36 -36
  16. package/cpp/ggml-cpu-traits.h +38 -38
  17. package/cpp/ggml-cpu.c +329 -60
  18. package/cpp/ggml-cpu.cpp +10 -2
  19. package/cpp/ggml-cpu.h +135 -135
  20. package/cpp/ggml-impl.h +567 -567
  21. package/cpp/ggml-metal-impl.h +17 -17
  22. package/cpp/ggml-metal.m +4884 -4884
  23. package/cpp/ggml-quants.c +5238 -5238
  24. package/cpp/ggml-threading.h +14 -14
  25. package/cpp/ggml.c +6514 -6448
  26. package/cpp/ggml.h +2194 -2163
  27. package/cpp/gguf.cpp +1329 -1325
  28. package/cpp/gguf.h +202 -202
  29. package/cpp/json-schema-to-grammar.cpp +1045 -1045
  30. package/cpp/json-schema-to-grammar.h +8 -8
  31. package/cpp/json.hpp +24766 -24766
  32. package/cpp/llama-adapter.cpp +347 -346
  33. package/cpp/llama-adapter.h +74 -73
  34. package/cpp/llama-arch.cpp +1487 -1434
  35. package/cpp/llama-arch.h +400 -395
  36. package/cpp/llama-batch.cpp +368 -368
  37. package/cpp/llama-batch.h +88 -88
  38. package/cpp/llama-chat.cpp +578 -567
  39. package/cpp/llama-chat.h +52 -51
  40. package/cpp/llama-context.cpp +1775 -1771
  41. package/cpp/llama-context.h +128 -128
  42. package/cpp/llama-cparams.cpp +1 -1
  43. package/cpp/llama-cparams.h +37 -37
  44. package/cpp/llama-cpp.h +30 -30
  45. package/cpp/llama-grammar.cpp +1139 -1139
  46. package/cpp/llama-grammar.h +143 -143
  47. package/cpp/llama-hparams.cpp +71 -71
  48. package/cpp/llama-hparams.h +139 -140
  49. package/cpp/llama-impl.cpp +167 -167
  50. package/cpp/llama-impl.h +61 -61
  51. package/cpp/llama-kv-cache.cpp +718 -718
  52. package/cpp/llama-kv-cache.h +218 -218
  53. package/cpp/llama-mmap.cpp +2 -1
  54. package/cpp/llama-mmap.h +67 -67
  55. package/cpp/llama-model-loader.cpp +1124 -1011
  56. package/cpp/llama-model-loader.h +167 -158
  57. package/cpp/llama-model.cpp +3997 -2202
  58. package/cpp/llama-model.h +370 -391
  59. package/cpp/llama-sampling.cpp +2408 -2406
  60. package/cpp/llama-sampling.h +32 -48
  61. package/cpp/llama-vocab.cpp +3247 -1982
  62. package/cpp/llama-vocab.h +125 -182
  63. package/cpp/llama.cpp +416 -2886
  64. package/cpp/llama.h +1323 -1285
  65. package/cpp/log.cpp +401 -401
  66. package/cpp/log.h +121 -121
  67. package/cpp/rn-llama.hpp +18 -12
  68. package/cpp/sampling.cpp +505 -500
  69. package/cpp/sgemm.cpp +2597 -2597
  70. package/cpp/speculative.cpp +277 -274
  71. package/cpp/speculative.h +28 -28
  72. package/cpp/unicode.cpp +2 -3
  73. package/package.json +1 -1
@@ -1,386 +1,386 @@
1
- #pragma once
2
-
3
- // GGML CPU internal header
4
-
5
- #include "ggml.h"
6
- #include "ggml-impl.h"
7
- #include <stdlib.h> // load `stdlib.h` before other headers to work around MinGW bug: https://sourceforge.net/p/mingw-w64/bugs/192/
8
- //#include <stddef.h>
9
- #include <stdbool.h>
10
- #include <string.h> // memcpy
11
- #include <math.h> // fabsf
12
-
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(__ARM_FEATURE_SVE)
63
- #include <arm_sve.h>
64
- #include <sys/prctl.h>
65
- #endif
66
-
67
- // 16-bit float
68
- // on Arm, we use __fp16
69
- // on x86, we use uint16_t
70
- #if defined(__ARM_NEON)
71
-
72
- // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
73
- //
74
- // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
75
- //
76
- #include <arm_neon.h>
77
-
78
- #ifdef _MSC_VER
79
-
80
- typedef uint16_t lm_ggml_fp16_internal_t;
81
-
82
- #define lm_ggml_vld1q_u32(w,x,y,z) { ((w) + ((uint64_t)(x) << 32)), ((y) + ((uint64_t)(z) << 32)) }
83
-
84
- #else
85
-
86
- typedef __fp16 lm_ggml_fp16_internal_t;
87
-
88
- #define lm_ggml_vld1q_u32(w,x,y,z) { (w), (x), (y), (z) }
89
-
90
- #endif // _MSC_VER
91
-
92
- #if !defined(__aarch64__)
93
-
94
- // 32-bit ARM compatibility
95
-
96
- // vaddlvq_s16
97
- // vpaddq_s16
98
- // vpaddq_s32
99
- // vaddvq_s32
100
- // vaddvq_f32
101
- // vmaxvq_f32
102
- // vcvtnq_s32_f32
103
- // vzip1_u8
104
- // vzip2_u8
105
-
106
- inline static int32_t vaddlvq_s16(int16x8_t v) {
107
- int32x4_t v0 = vreinterpretq_s32_s64(vpaddlq_s32(vpaddlq_s16(v)));
108
- return vgetq_lane_s32(v0, 0) + vgetq_lane_s32(v0, 2);
109
- }
110
-
111
- inline static int16x8_t vpaddq_s16(int16x8_t a, int16x8_t b) {
112
- int16x4_t a0 = vpadd_s16(vget_low_s16(a), vget_high_s16(a));
113
- int16x4_t b0 = vpadd_s16(vget_low_s16(b), vget_high_s16(b));
114
- return vcombine_s16(a0, b0);
115
- }
116
-
117
- inline static int32x4_t vpaddq_s32(int32x4_t a, int32x4_t b) {
118
- int32x2_t a0 = vpadd_s32(vget_low_s32(a), vget_high_s32(a));
119
- int32x2_t b0 = vpadd_s32(vget_low_s32(b), vget_high_s32(b));
120
- return vcombine_s32(a0, b0);
121
- }
122
-
123
- inline static int32_t vaddvq_s32(int32x4_t v) {
124
- return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
125
- }
126
-
127
- inline static float vaddvq_f32(float32x4_t v) {
128
- return vgetq_lane_f32(v, 0) + vgetq_lane_f32(v, 1) + vgetq_lane_f32(v, 2) + vgetq_lane_f32(v, 3);
129
- }
130
-
131
- inline static float vmaxvq_f32(float32x4_t v) {
132
- return
133
- MAX(MAX(vgetq_lane_f32(v, 0), vgetq_lane_f32(v, 1)),
134
- MAX(vgetq_lane_f32(v, 2), vgetq_lane_f32(v, 3)));
135
- }
136
-
137
- inline static int32x4_t vcvtnq_s32_f32(float32x4_t v) {
138
- int32x4_t res;
139
-
140
- res[0] = roundf(vgetq_lane_f32(v, 0));
141
- res[1] = roundf(vgetq_lane_f32(v, 1));
142
- res[2] = roundf(vgetq_lane_f32(v, 2));
143
- res[3] = roundf(vgetq_lane_f32(v, 3));
144
-
145
- return res;
146
- }
147
-
148
- inline static uint8x8_t vzip1_u8(uint8x8_t a, uint8x8_t b) {
149
- uint8x8_t res;
150
-
151
- res[0] = a[0]; res[1] = b[0];
152
- res[2] = a[1]; res[3] = b[1];
153
- res[4] = a[2]; res[5] = b[2];
154
- res[6] = a[3]; res[7] = b[3];
155
-
156
- return res;
157
- }
158
-
159
- inline static uint8x8_t vzip2_u8(uint8x8_t a, uint8x8_t b) {
160
- uint8x8_t res;
161
-
162
- res[0] = a[4]; res[1] = b[4];
163
- res[2] = a[5]; res[3] = b[5];
164
- res[4] = a[6]; res[5] = b[6];
165
- res[6] = a[7]; res[7] = b[7];
166
-
167
- return res;
168
- }
169
-
170
- // vld1q_s16_x2
171
- // vld1q_u8_x2
172
- // vld1q_u8_x4
173
- // vld1q_s8_x2
174
- // vld1q_s8_x4
175
- // TODO: double-check these work correctly
176
-
177
- typedef struct lm_ggml_int16x8x2_t {
178
- int16x8_t val[2];
179
- } lm_ggml_int16x8x2_t;
180
-
181
- inline static lm_ggml_int16x8x2_t lm_ggml_vld1q_s16_x2(const int16_t * ptr) {
182
- lm_ggml_int16x8x2_t res;
183
-
184
- res.val[0] = vld1q_s16(ptr + 0);
185
- res.val[1] = vld1q_s16(ptr + 8);
186
-
187
- return res;
188
- }
189
-
190
- typedef struct lm_ggml_uint8x16x2_t {
191
- uint8x16_t val[2];
192
- } lm_ggml_uint8x16x2_t;
193
-
194
- inline static lm_ggml_uint8x16x2_t lm_ggml_vld1q_u8_x2(const uint8_t * ptr) {
195
- lm_ggml_uint8x16x2_t res;
196
-
197
- res.val[0] = vld1q_u8(ptr + 0);
198
- res.val[1] = vld1q_u8(ptr + 16);
199
-
200
- return res;
201
- }
202
-
203
- typedef struct lm_ggml_uint8x16x4_t {
204
- uint8x16_t val[4];
205
- } lm_ggml_uint8x16x4_t;
206
-
207
- inline static lm_ggml_uint8x16x4_t lm_ggml_vld1q_u8_x4(const uint8_t * ptr) {
208
- lm_ggml_uint8x16x4_t res;
209
-
210
- res.val[0] = vld1q_u8(ptr + 0);
211
- res.val[1] = vld1q_u8(ptr + 16);
212
- res.val[2] = vld1q_u8(ptr + 32);
213
- res.val[3] = vld1q_u8(ptr + 48);
214
-
215
- return res;
216
- }
217
-
218
- typedef struct lm_ggml_int8x16x2_t {
219
- int8x16_t val[2];
220
- } lm_ggml_int8x16x2_t;
221
-
222
- inline static lm_ggml_int8x16x2_t lm_ggml_vld1q_s8_x2(const int8_t * ptr) {
223
- lm_ggml_int8x16x2_t res;
224
-
225
- res.val[0] = vld1q_s8(ptr + 0);
226
- res.val[1] = vld1q_s8(ptr + 16);
227
-
228
- return res;
229
- }
230
-
231
- typedef struct lm_ggml_int8x16x4_t {
232
- int8x16_t val[4];
233
- } lm_ggml_int8x16x4_t;
234
-
235
- inline static lm_ggml_int8x16x4_t lm_ggml_vld1q_s8_x4(const int8_t * ptr) {
236
- lm_ggml_int8x16x4_t res;
237
-
238
- res.val[0] = vld1q_s8(ptr + 0);
239
- res.val[1] = vld1q_s8(ptr + 16);
240
- res.val[2] = vld1q_s8(ptr + 32);
241
- res.val[3] = vld1q_s8(ptr + 48);
242
-
243
- return res;
244
- }
245
-
246
- // NOTE: not tested
247
- inline static int8x16_t lm_ggml_vqtbl1q_s8(int8x16_t a, uint8x16_t b) {
248
- int8x16_t res;
249
-
250
- res[ 0] = a[b[ 0]];
251
- res[ 1] = a[b[ 1]];
252
- res[ 2] = a[b[ 2]];
253
- res[ 3] = a[b[ 3]];
254
- res[ 4] = a[b[ 4]];
255
- res[ 5] = a[b[ 5]];
256
- res[ 6] = a[b[ 6]];
257
- res[ 7] = a[b[ 7]];
258
- res[ 8] = a[b[ 8]];
259
- res[ 9] = a[b[ 9]];
260
- res[10] = a[b[10]];
261
- res[11] = a[b[11]];
262
- res[12] = a[b[12]];
263
- res[13] = a[b[13]];
264
- res[14] = a[b[14]];
265
- res[15] = a[b[15]];
266
-
267
- return res;
268
- }
269
-
270
- // NOTE: not tested
271
- inline static uint8x16_t lm_ggml_vqtbl1q_u8(uint8x16_t a, uint8x16_t b) {
272
- uint8x16_t res;
273
-
274
- res[ 0] = a[b[ 0]];
275
- res[ 1] = a[b[ 1]];
276
- res[ 2] = a[b[ 2]];
277
- res[ 3] = a[b[ 3]];
278
- res[ 4] = a[b[ 4]];
279
- res[ 5] = a[b[ 5]];
280
- res[ 6] = a[b[ 6]];
281
- res[ 7] = a[b[ 7]];
282
- res[ 8] = a[b[ 8]];
283
- res[ 9] = a[b[ 9]];
284
- res[10] = a[b[10]];
285
- res[11] = a[b[11]];
286
- res[12] = a[b[12]];
287
- res[13] = a[b[13]];
288
- res[14] = a[b[14]];
289
- res[15] = a[b[15]];
290
-
291
- return res;
292
- }
293
-
294
- #else
295
-
296
- #define lm_ggml_int16x8x2_t int16x8x2_t
297
- #define lm_ggml_uint8x16x2_t uint8x16x2_t
298
- #define lm_ggml_uint8x16x4_t uint8x16x4_t
299
- #define lm_ggml_int8x16x2_t int8x16x2_t
300
- #define lm_ggml_int8x16x4_t int8x16x4_t
301
-
302
- #define lm_ggml_vld1q_s16_x2 vld1q_s16_x2
303
- #define lm_ggml_vld1q_u8_x2 vld1q_u8_x2
304
- #define lm_ggml_vld1q_u8_x4 vld1q_u8_x4
305
- #define lm_ggml_vld1q_s8_x2 vld1q_s8_x2
306
- #define lm_ggml_vld1q_s8_x4 vld1q_s8_x4
307
- #define lm_ggml_vqtbl1q_s8 vqtbl1q_s8
308
- #define lm_ggml_vqtbl1q_u8 vqtbl1q_u8
309
-
310
- #endif // !defined(__aarch64__)
311
-
312
- #if !defined(__ARM_FEATURE_DOTPROD)
313
-
314
- inline static int32x4_t lm_ggml_vdotq_s32(int32x4_t acc, int8x16_t a, int8x16_t b) {
315
- const int16x8_t p0 = vmull_s8(vget_low_s8 (a), vget_low_s8 (b));
316
- const int16x8_t p1 = vmull_s8(vget_high_s8(a), vget_high_s8(b));
317
-
318
- return vaddq_s32(acc, vaddq_s32(vpaddlq_s16(p0), vpaddlq_s16(p1)));
319
- }
320
-
321
- #else
322
-
323
- #define lm_ggml_vdotq_s32(a, b, c) vdotq_s32(a, b, c)
324
-
325
- #endif // !defined(__ARM_FEATURE_DOTPROD)
326
-
327
- #endif // defined(__ARM_NEON)
328
-
329
- #ifdef __wasm_simd128__
330
- #include <wasm_simd128.h>
331
- #else
332
- #ifdef __POWER9_VECTOR__
333
- #include <altivec.h>
334
- #undef bool
335
- #define bool _Bool
336
- #else
337
- #if defined(_MSC_VER) || defined(__MINGW32__)
338
- #include <intrin.h>
339
- #else
340
- #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__) || defined(__SSE__)
341
- #if !defined(__riscv)
342
- #include <immintrin.h>
343
- #endif
344
- #endif
345
- #endif
346
- #endif
347
- #endif
348
-
349
- #ifdef __riscv_v_intrinsic
350
- #include <riscv_vector.h>
351
- #endif
352
-
353
- #if defined(__loongarch64)
354
- #if defined(__loongarch_asx)
355
- #include <lasxintrin.h>
356
- #endif
357
- #if defined(__loongarch_sx)
358
- #include <lsxintrin.h>
359
- #endif
360
- #endif
361
-
362
- #if defined(__loongarch_asx)
363
-
364
- typedef union {
365
- int32_t i;
366
- float f;
367
- } ft_union;
368
-
369
- /* float type data load instructions */
370
- static __m128 __lsx_vreplfr2vr_s(float val) {
371
- ft_union fi_tmpval = {.f = val};
372
- return (__m128)__lsx_vreplgr2vr_w(fi_tmpval.i);
373
- }
374
-
375
- static __m256 __lasx_xvreplfr2vr_s(float val) {
376
- ft_union fi_tmpval = {.f = val};
377
- return (__m256)__lasx_xvreplgr2vr_w(fi_tmpval.i);
378
- }
379
- #endif
380
-
381
- // TODO: move to ggml-threading
382
- void lm_ggml_barrier(struct lm_ggml_threadpool * tp);
383
-
384
- #ifdef __cplusplus
385
- }
386
- #endif
1
+ #pragma once
2
+
3
+ // GGML CPU internal header
4
+
5
+ #include "ggml.h"
6
+ #include "ggml-impl.h"
7
+ #include <stdlib.h> // load `stdlib.h` before other headers to work around MinGW bug: https://sourceforge.net/p/mingw-w64/bugs/192/
8
+ //#include <stddef.h>
9
+ #include <stdbool.h>
10
+ #include <string.h> // memcpy
11
+ #include <math.h> // fabsf
12
+
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(__ARM_FEATURE_SVE)
63
+ #include <arm_sve.h>
64
+ #include <sys/prctl.h>
65
+ #endif
66
+
67
+ // 16-bit float
68
+ // on Arm, we use __fp16
69
+ // on x86, we use uint16_t
70
+ #if defined(__ARM_NEON)
71
+
72
+ // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
73
+ //
74
+ // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
75
+ //
76
+ #include <arm_neon.h>
77
+
78
+ #ifdef _MSC_VER
79
+
80
+ typedef uint16_t lm_ggml_fp16_internal_t;
81
+
82
+ #define lm_ggml_vld1q_u32(w,x,y,z) { ((w) + ((uint64_t)(x) << 32)), ((y) + ((uint64_t)(z) << 32)) }
83
+
84
+ #else
85
+
86
+ typedef __fp16 lm_ggml_fp16_internal_t;
87
+
88
+ #define lm_ggml_vld1q_u32(w,x,y,z) { (w), (x), (y), (z) }
89
+
90
+ #endif // _MSC_VER
91
+
92
+ #if !defined(__aarch64__)
93
+
94
+ // 32-bit ARM compatibility
95
+
96
+ // vaddlvq_s16
97
+ // vpaddq_s16
98
+ // vpaddq_s32
99
+ // vaddvq_s32
100
+ // vaddvq_f32
101
+ // vmaxvq_f32
102
+ // vcvtnq_s32_f32
103
+ // vzip1_u8
104
+ // vzip2_u8
105
+
106
+ inline static int32_t vaddlvq_s16(int16x8_t v) {
107
+ int32x4_t v0 = vreinterpretq_s32_s64(vpaddlq_s32(vpaddlq_s16(v)));
108
+ return vgetq_lane_s32(v0, 0) + vgetq_lane_s32(v0, 2);
109
+ }
110
+
111
+ inline static int16x8_t vpaddq_s16(int16x8_t a, int16x8_t b) {
112
+ int16x4_t a0 = vpadd_s16(vget_low_s16(a), vget_high_s16(a));
113
+ int16x4_t b0 = vpadd_s16(vget_low_s16(b), vget_high_s16(b));
114
+ return vcombine_s16(a0, b0);
115
+ }
116
+
117
+ inline static int32x4_t vpaddq_s32(int32x4_t a, int32x4_t b) {
118
+ int32x2_t a0 = vpadd_s32(vget_low_s32(a), vget_high_s32(a));
119
+ int32x2_t b0 = vpadd_s32(vget_low_s32(b), vget_high_s32(b));
120
+ return vcombine_s32(a0, b0);
121
+ }
122
+
123
+ inline static int32_t vaddvq_s32(int32x4_t v) {
124
+ return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
125
+ }
126
+
127
+ inline static float vaddvq_f32(float32x4_t v) {
128
+ return vgetq_lane_f32(v, 0) + vgetq_lane_f32(v, 1) + vgetq_lane_f32(v, 2) + vgetq_lane_f32(v, 3);
129
+ }
130
+
131
+ inline static float vmaxvq_f32(float32x4_t v) {
132
+ return
133
+ MAX(MAX(vgetq_lane_f32(v, 0), vgetq_lane_f32(v, 1)),
134
+ MAX(vgetq_lane_f32(v, 2), vgetq_lane_f32(v, 3)));
135
+ }
136
+
137
+ inline static int32x4_t vcvtnq_s32_f32(float32x4_t v) {
138
+ int32x4_t res;
139
+
140
+ res[0] = roundf(vgetq_lane_f32(v, 0));
141
+ res[1] = roundf(vgetq_lane_f32(v, 1));
142
+ res[2] = roundf(vgetq_lane_f32(v, 2));
143
+ res[3] = roundf(vgetq_lane_f32(v, 3));
144
+
145
+ return res;
146
+ }
147
+
148
+ inline static uint8x8_t vzip1_u8(uint8x8_t a, uint8x8_t b) {
149
+ uint8x8_t res;
150
+
151
+ res[0] = a[0]; res[1] = b[0];
152
+ res[2] = a[1]; res[3] = b[1];
153
+ res[4] = a[2]; res[5] = b[2];
154
+ res[6] = a[3]; res[7] = b[3];
155
+
156
+ return res;
157
+ }
158
+
159
+ inline static uint8x8_t vzip2_u8(uint8x8_t a, uint8x8_t b) {
160
+ uint8x8_t res;
161
+
162
+ res[0] = a[4]; res[1] = b[4];
163
+ res[2] = a[5]; res[3] = b[5];
164
+ res[4] = a[6]; res[5] = b[6];
165
+ res[6] = a[7]; res[7] = b[7];
166
+
167
+ return res;
168
+ }
169
+
170
+ // vld1q_s16_x2
171
+ // vld1q_u8_x2
172
+ // vld1q_u8_x4
173
+ // vld1q_s8_x2
174
+ // vld1q_s8_x4
175
+ // TODO: double-check these work correctly
176
+
177
+ typedef struct lm_ggml_int16x8x2_t {
178
+ int16x8_t val[2];
179
+ } lm_ggml_int16x8x2_t;
180
+
181
+ inline static lm_ggml_int16x8x2_t lm_ggml_vld1q_s16_x2(const int16_t * ptr) {
182
+ lm_ggml_int16x8x2_t res;
183
+
184
+ res.val[0] = vld1q_s16(ptr + 0);
185
+ res.val[1] = vld1q_s16(ptr + 8);
186
+
187
+ return res;
188
+ }
189
+
190
+ typedef struct lm_ggml_uint8x16x2_t {
191
+ uint8x16_t val[2];
192
+ } lm_ggml_uint8x16x2_t;
193
+
194
+ inline static lm_ggml_uint8x16x2_t lm_ggml_vld1q_u8_x2(const uint8_t * ptr) {
195
+ lm_ggml_uint8x16x2_t res;
196
+
197
+ res.val[0] = vld1q_u8(ptr + 0);
198
+ res.val[1] = vld1q_u8(ptr + 16);
199
+
200
+ return res;
201
+ }
202
+
203
+ typedef struct lm_ggml_uint8x16x4_t {
204
+ uint8x16_t val[4];
205
+ } lm_ggml_uint8x16x4_t;
206
+
207
+ inline static lm_ggml_uint8x16x4_t lm_ggml_vld1q_u8_x4(const uint8_t * ptr) {
208
+ lm_ggml_uint8x16x4_t res;
209
+
210
+ res.val[0] = vld1q_u8(ptr + 0);
211
+ res.val[1] = vld1q_u8(ptr + 16);
212
+ res.val[2] = vld1q_u8(ptr + 32);
213
+ res.val[3] = vld1q_u8(ptr + 48);
214
+
215
+ return res;
216
+ }
217
+
218
+ typedef struct lm_ggml_int8x16x2_t {
219
+ int8x16_t val[2];
220
+ } lm_ggml_int8x16x2_t;
221
+
222
+ inline static lm_ggml_int8x16x2_t lm_ggml_vld1q_s8_x2(const int8_t * ptr) {
223
+ lm_ggml_int8x16x2_t res;
224
+
225
+ res.val[0] = vld1q_s8(ptr + 0);
226
+ res.val[1] = vld1q_s8(ptr + 16);
227
+
228
+ return res;
229
+ }
230
+
231
+ typedef struct lm_ggml_int8x16x4_t {
232
+ int8x16_t val[4];
233
+ } lm_ggml_int8x16x4_t;
234
+
235
+ inline static lm_ggml_int8x16x4_t lm_ggml_vld1q_s8_x4(const int8_t * ptr) {
236
+ lm_ggml_int8x16x4_t res;
237
+
238
+ res.val[0] = vld1q_s8(ptr + 0);
239
+ res.val[1] = vld1q_s8(ptr + 16);
240
+ res.val[2] = vld1q_s8(ptr + 32);
241
+ res.val[3] = vld1q_s8(ptr + 48);
242
+
243
+ return res;
244
+ }
245
+
246
+ // NOTE: not tested
247
+ inline static int8x16_t lm_ggml_vqtbl1q_s8(int8x16_t a, uint8x16_t b) {
248
+ int8x16_t res;
249
+
250
+ res[ 0] = a[b[ 0]];
251
+ res[ 1] = a[b[ 1]];
252
+ res[ 2] = a[b[ 2]];
253
+ res[ 3] = a[b[ 3]];
254
+ res[ 4] = a[b[ 4]];
255
+ res[ 5] = a[b[ 5]];
256
+ res[ 6] = a[b[ 6]];
257
+ res[ 7] = a[b[ 7]];
258
+ res[ 8] = a[b[ 8]];
259
+ res[ 9] = a[b[ 9]];
260
+ res[10] = a[b[10]];
261
+ res[11] = a[b[11]];
262
+ res[12] = a[b[12]];
263
+ res[13] = a[b[13]];
264
+ res[14] = a[b[14]];
265
+ res[15] = a[b[15]];
266
+
267
+ return res;
268
+ }
269
+
270
+ // NOTE: not tested
271
+ inline static uint8x16_t lm_ggml_vqtbl1q_u8(uint8x16_t a, uint8x16_t b) {
272
+ uint8x16_t res;
273
+
274
+ res[ 0] = a[b[ 0]];
275
+ res[ 1] = a[b[ 1]];
276
+ res[ 2] = a[b[ 2]];
277
+ res[ 3] = a[b[ 3]];
278
+ res[ 4] = a[b[ 4]];
279
+ res[ 5] = a[b[ 5]];
280
+ res[ 6] = a[b[ 6]];
281
+ res[ 7] = a[b[ 7]];
282
+ res[ 8] = a[b[ 8]];
283
+ res[ 9] = a[b[ 9]];
284
+ res[10] = a[b[10]];
285
+ res[11] = a[b[11]];
286
+ res[12] = a[b[12]];
287
+ res[13] = a[b[13]];
288
+ res[14] = a[b[14]];
289
+ res[15] = a[b[15]];
290
+
291
+ return res;
292
+ }
293
+
294
+ #else
295
+
296
+ #define lm_ggml_int16x8x2_t int16x8x2_t
297
+ #define lm_ggml_uint8x16x2_t uint8x16x2_t
298
+ #define lm_ggml_uint8x16x4_t uint8x16x4_t
299
+ #define lm_ggml_int8x16x2_t int8x16x2_t
300
+ #define lm_ggml_int8x16x4_t int8x16x4_t
301
+
302
+ #define lm_ggml_vld1q_s16_x2 vld1q_s16_x2
303
+ #define lm_ggml_vld1q_u8_x2 vld1q_u8_x2
304
+ #define lm_ggml_vld1q_u8_x4 vld1q_u8_x4
305
+ #define lm_ggml_vld1q_s8_x2 vld1q_s8_x2
306
+ #define lm_ggml_vld1q_s8_x4 vld1q_s8_x4
307
+ #define lm_ggml_vqtbl1q_s8 vqtbl1q_s8
308
+ #define lm_ggml_vqtbl1q_u8 vqtbl1q_u8
309
+
310
+ #endif // !defined(__aarch64__)
311
+
312
+ #if !defined(__ARM_FEATURE_DOTPROD)
313
+
314
+ inline static int32x4_t lm_ggml_vdotq_s32(int32x4_t acc, int8x16_t a, int8x16_t b) {
315
+ const int16x8_t p0 = vmull_s8(vget_low_s8 (a), vget_low_s8 (b));
316
+ const int16x8_t p1 = vmull_s8(vget_high_s8(a), vget_high_s8(b));
317
+
318
+ return vaddq_s32(acc, vaddq_s32(vpaddlq_s16(p0), vpaddlq_s16(p1)));
319
+ }
320
+
321
+ #else
322
+
323
+ #define lm_ggml_vdotq_s32(a, b, c) vdotq_s32(a, b, c)
324
+
325
+ #endif // !defined(__ARM_FEATURE_DOTPROD)
326
+
327
+ #endif // defined(__ARM_NEON)
328
+
329
+ #ifdef __wasm_simd128__
330
+ #include <wasm_simd128.h>
331
+ #else
332
+ #ifdef __POWER9_VECTOR__
333
+ #include <altivec.h>
334
+ #undef bool
335
+ #define bool _Bool
336
+ #else
337
+ #if defined(_MSC_VER) || defined(__MINGW32__)
338
+ #include <intrin.h>
339
+ #else
340
+ #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__) || defined(__SSE__)
341
+ #if !defined(__riscv)
342
+ #include <immintrin.h>
343
+ #endif
344
+ #endif
345
+ #endif
346
+ #endif
347
+ #endif
348
+
349
+ #ifdef __riscv_v_intrinsic
350
+ #include <riscv_vector.h>
351
+ #endif
352
+
353
+ #if defined(__loongarch64)
354
+ #if defined(__loongarch_asx)
355
+ #include <lasxintrin.h>
356
+ #endif
357
+ #if defined(__loongarch_sx)
358
+ #include <lsxintrin.h>
359
+ #endif
360
+ #endif
361
+
362
+ #if defined(__loongarch_asx)
363
+
364
+ typedef union {
365
+ int32_t i;
366
+ float f;
367
+ } ft_union;
368
+
369
+ /* float type data load instructions */
370
+ static __m128 __lsx_vreplfr2vr_s(float val) {
371
+ ft_union fi_tmpval = {.f = val};
372
+ return (__m128)__lsx_vreplgr2vr_w(fi_tmpval.i);
373
+ }
374
+
375
+ static __m256 __lasx_xvreplfr2vr_s(float val) {
376
+ ft_union fi_tmpval = {.f = val};
377
+ return (__m256)__lasx_xvreplgr2vr_w(fi_tmpval.i);
378
+ }
379
+ #endif
380
+
381
+ // TODO: move to ggml-threading
382
+ void lm_ggml_barrier(struct lm_ggml_threadpool * tp);
383
+
384
+ #ifdef __cplusplus
385
+ }
386
+ #endif