cui-llama.rn 1.3.0 → 1.3.3

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 (76) hide show
  1. package/android/src/main/CMakeLists.txt +6 -1
  2. package/android/src/main/jni.cpp +6 -6
  3. package/cpp/amx/amx.cpp +196 -0
  4. package/cpp/amx/amx.h +20 -0
  5. package/cpp/amx/common.h +101 -0
  6. package/cpp/amx/mmq.cpp +2524 -0
  7. package/cpp/amx/mmq.h +16 -0
  8. package/cpp/common.cpp +1981 -1682
  9. package/cpp/common.h +636 -600
  10. package/cpp/ggml-aarch64.c +129 -129
  11. package/cpp/ggml-aarch64.h +19 -19
  12. package/cpp/ggml-alloc.c +1038 -1040
  13. package/cpp/ggml-alloc.h +76 -76
  14. package/cpp/ggml-backend-impl.h +238 -216
  15. package/cpp/ggml-backend-reg.cpp +423 -195
  16. package/cpp/ggml-backend.cpp +1999 -1997
  17. package/cpp/ggml-backend.h +351 -328
  18. package/cpp/ggml-common.h +1859 -1853
  19. package/cpp/ggml-cpp.h +38 -38
  20. package/cpp/ggml-cpu-aarch64.c +3823 -3560
  21. package/cpp/ggml-cpu-aarch64.h +32 -30
  22. package/cpp/ggml-cpu-impl.h +386 -371
  23. package/cpp/ggml-cpu-quants.c +10835 -10822
  24. package/cpp/ggml-cpu-quants.h +63 -63
  25. package/cpp/ggml-cpu.c +99 -103
  26. package/cpp/ggml-cpu.cpp +69 -17
  27. package/cpp/ggml-cpu.h +152 -177
  28. package/cpp/ggml-impl.h +556 -550
  29. package/cpp/ggml-metal.h +66 -66
  30. package/cpp/ggml-metal.m +4426 -4294
  31. package/cpp/ggml-quants.c +5247 -5247
  32. package/cpp/ggml-quants.h +100 -100
  33. package/cpp/ggml-threading.cpp +12 -12
  34. package/cpp/ggml-threading.h +12 -12
  35. package/cpp/ggml.c +7618 -8180
  36. package/cpp/ggml.h +2255 -2411
  37. package/cpp/json-schema-to-grammar.cpp +1045 -0
  38. package/cpp/json-schema-to-grammar.h +8 -0
  39. package/cpp/json.hpp +24766 -0
  40. package/cpp/llama-grammar.cpp +1138 -1138
  41. package/cpp/llama-grammar.h +144 -144
  42. package/cpp/llama-impl.h +181 -181
  43. package/cpp/llama-sampling.cpp +2348 -2348
  44. package/cpp/llama-sampling.h +48 -48
  45. package/cpp/llama-vocab.cpp +1984 -1984
  46. package/cpp/llama-vocab.h +170 -170
  47. package/cpp/llama.cpp +22332 -22132
  48. package/cpp/llama.h +1259 -1253
  49. package/cpp/log.cpp +401 -401
  50. package/cpp/log.h +121 -121
  51. package/cpp/rn-llama.hpp +6 -6
  52. package/cpp/sampling.cpp +505 -466
  53. package/cpp/sampling.h +22 -1
  54. package/cpp/sgemm.cpp +1884 -1884
  55. package/cpp/speculative.cpp +270 -0
  56. package/cpp/speculative.h +28 -0
  57. package/cpp/unicode.cpp +11 -0
  58. package/ios/RNLlamaContext.mm +13 -0
  59. package/lib/commonjs/NativeRNLlama.js.map +1 -1
  60. package/lib/commonjs/grammar.js +4 -2
  61. package/lib/commonjs/grammar.js.map +1 -1
  62. package/lib/commonjs/index.js.map +1 -1
  63. package/lib/module/NativeRNLlama.js.map +1 -1
  64. package/lib/module/grammar.js +2 -1
  65. package/lib/module/grammar.js.map +1 -1
  66. package/lib/module/index.js.map +1 -1
  67. package/lib/typescript/NativeRNLlama.d.ts +94 -4
  68. package/lib/typescript/NativeRNLlama.d.ts.map +1 -1
  69. package/lib/typescript/grammar.d.ts +5 -6
  70. package/lib/typescript/grammar.d.ts.map +1 -1
  71. package/lib/typescript/index.d.ts +4 -2
  72. package/lib/typescript/index.d.ts.map +1 -1
  73. package/package.json +2 -1
  74. package/src/NativeRNLlama.ts +97 -10
  75. package/src/grammar.ts +10 -8
  76. package/src/index.ts +22 -1
@@ -1,371 +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
- #if defined(_MSC_VER)
19
-
20
- #define m512bh(p) p
21
- #define m512i(p) p
22
-
23
- #else
24
-
25
- #define m512bh(p) (__m512bh)(p)
26
- #define m512i(p) (__m512i)(p)
27
-
28
- #endif
29
-
30
- // __FMA__ and __F16C__ are not defined in MSVC, however they are implied with AVX2/AVX512
31
- #if defined(_MSC_VER) && (defined(__AVX2__) || defined(__AVX512F__))
32
- #ifndef __FMA__
33
- #define __FMA__
34
- #endif
35
- #ifndef __F16C__
36
- #define __F16C__
37
- #endif
38
- #endif
39
-
40
- // __SSE3__ and __SSSE3__ are not defined in MSVC, but SSE3/SSSE3 are present when AVX/AVX2/AVX512 are available
41
- #if defined(_MSC_VER) && (defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__))
42
- #ifndef __SSE3__
43
- #define __SSE3__
44
- #endif
45
- #ifndef __SSSE3__
46
- #define __SSSE3__
47
- #endif
48
- #endif
49
-
50
- #if defined(__ARM_FEATURE_SVE)
51
- #include <arm_sve.h>
52
- #include <sys/prctl.h>
53
- #endif
54
-
55
- // 16-bit float
56
- // on Arm, we use __fp16
57
- // on x86, we use uint16_t
58
- #if defined(__ARM_NEON)
59
-
60
- // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
61
- //
62
- // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
63
- //
64
- #include <arm_neon.h>
65
-
66
- #ifdef _MSC_VER
67
-
68
- typedef uint16_t lm_ggml_fp16_internal_t;
69
-
70
- #define lm_ggml_vld1q_u32(w,x,y,z) { ((w) + ((uint64_t)(x) << 32)), ((y) + ((uint64_t)(z) << 32)) }
71
-
72
- #else
73
-
74
- typedef __fp16 lm_ggml_fp16_internal_t;
75
-
76
- #define lm_ggml_vld1q_u32(w,x,y,z) { (w), (x), (y), (z) }
77
-
78
- #endif // _MSC_VER
79
-
80
- #if !defined(__aarch64__)
81
-
82
- // 32-bit ARM compatibility
83
-
84
- // vaddlvq_s16
85
- // vpaddq_s16
86
- // vpaddq_s32
87
- // vaddvq_s32
88
- // vaddvq_f32
89
- // vmaxvq_f32
90
- // vcvtnq_s32_f32
91
- // vzip1_u8
92
- // vzip2_u8
93
-
94
- inline static int32_t vaddlvq_s16(int16x8_t v) {
95
- int32x4_t v0 = vreinterpretq_s32_s64(vpaddlq_s32(vpaddlq_s16(v)));
96
- return vgetq_lane_s32(v0, 0) + vgetq_lane_s32(v0, 2);
97
- }
98
-
99
- inline static int16x8_t vpaddq_s16(int16x8_t a, int16x8_t b) {
100
- int16x4_t a0 = vpadd_s16(vget_low_s16(a), vget_high_s16(a));
101
- int16x4_t b0 = vpadd_s16(vget_low_s16(b), vget_high_s16(b));
102
- return vcombine_s16(a0, b0);
103
- }
104
-
105
- inline static int32x4_t vpaddq_s32(int32x4_t a, int32x4_t b) {
106
- int32x2_t a0 = vpadd_s32(vget_low_s32(a), vget_high_s32(a));
107
- int32x2_t b0 = vpadd_s32(vget_low_s32(b), vget_high_s32(b));
108
- return vcombine_s32(a0, b0);
109
- }
110
-
111
- inline static int32_t vaddvq_s32(int32x4_t v) {
112
- return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
113
- }
114
-
115
- inline static float vaddvq_f32(float32x4_t v) {
116
- return vgetq_lane_f32(v, 0) + vgetq_lane_f32(v, 1) + vgetq_lane_f32(v, 2) + vgetq_lane_f32(v, 3);
117
- }
118
-
119
- inline static float vmaxvq_f32(float32x4_t v) {
120
- return
121
- MAX(MAX(vgetq_lane_f32(v, 0), vgetq_lane_f32(v, 1)),
122
- MAX(vgetq_lane_f32(v, 2), vgetq_lane_f32(v, 3)));
123
- }
124
-
125
- inline static int32x4_t vcvtnq_s32_f32(float32x4_t v) {
126
- int32x4_t res;
127
-
128
- res[0] = roundf(vgetq_lane_f32(v, 0));
129
- res[1] = roundf(vgetq_lane_f32(v, 1));
130
- res[2] = roundf(vgetq_lane_f32(v, 2));
131
- res[3] = roundf(vgetq_lane_f32(v, 3));
132
-
133
- return res;
134
- }
135
-
136
- inline static uint8x8_t vzip1_u8(uint8x8_t a, uint8x8_t b) {
137
- uint8x8_t res;
138
-
139
- res[0] = a[0]; res[1] = b[0];
140
- res[2] = a[1]; res[3] = b[1];
141
- res[4] = a[2]; res[5] = b[2];
142
- res[6] = a[3]; res[7] = b[3];
143
-
144
- return res;
145
- }
146
-
147
- inline static uint8x8_t vzip2_u8(uint8x8_t a, uint8x8_t b) {
148
- uint8x8_t res;
149
-
150
- res[0] = a[4]; res[1] = b[4];
151
- res[2] = a[5]; res[3] = b[5];
152
- res[4] = a[6]; res[5] = b[6];
153
- res[6] = a[7]; res[7] = b[7];
154
-
155
- return res;
156
- }
157
-
158
- // vld1q_s16_x2
159
- // vld1q_u8_x2
160
- // vld1q_u8_x4
161
- // vld1q_s8_x2
162
- // vld1q_s8_x4
163
- // TODO: double-check these work correctly
164
-
165
- typedef struct lm_ggml_int16x8x2_t {
166
- int16x8_t val[2];
167
- } lm_ggml_int16x8x2_t;
168
-
169
- inline static lm_ggml_int16x8x2_t lm_ggml_vld1q_s16_x2(const int16_t * ptr) {
170
- lm_ggml_int16x8x2_t res;
171
-
172
- res.val[0] = vld1q_s16(ptr + 0);
173
- res.val[1] = vld1q_s16(ptr + 8);
174
-
175
- return res;
176
- }
177
-
178
- typedef struct lm_ggml_uint8x16x2_t {
179
- uint8x16_t val[2];
180
- } lm_ggml_uint8x16x2_t;
181
-
182
- inline static lm_ggml_uint8x16x2_t lm_ggml_vld1q_u8_x2(const uint8_t * ptr) {
183
- lm_ggml_uint8x16x2_t res;
184
-
185
- res.val[0] = vld1q_u8(ptr + 0);
186
- res.val[1] = vld1q_u8(ptr + 16);
187
-
188
- return res;
189
- }
190
-
191
- typedef struct lm_ggml_uint8x16x4_t {
192
- uint8x16_t val[4];
193
- } lm_ggml_uint8x16x4_t;
194
-
195
- inline static lm_ggml_uint8x16x4_t lm_ggml_vld1q_u8_x4(const uint8_t * ptr) {
196
- lm_ggml_uint8x16x4_t res;
197
-
198
- res.val[0] = vld1q_u8(ptr + 0);
199
- res.val[1] = vld1q_u8(ptr + 16);
200
- res.val[2] = vld1q_u8(ptr + 32);
201
- res.val[3] = vld1q_u8(ptr + 48);
202
-
203
- return res;
204
- }
205
-
206
- typedef struct lm_ggml_int8x16x2_t {
207
- int8x16_t val[2];
208
- } lm_ggml_int8x16x2_t;
209
-
210
- inline static lm_ggml_int8x16x2_t lm_ggml_vld1q_s8_x2(const int8_t * ptr) {
211
- lm_ggml_int8x16x2_t res;
212
-
213
- res.val[0] = vld1q_s8(ptr + 0);
214
- res.val[1] = vld1q_s8(ptr + 16);
215
-
216
- return res;
217
- }
218
-
219
- typedef struct lm_ggml_int8x16x4_t {
220
- int8x16_t val[4];
221
- } lm_ggml_int8x16x4_t;
222
-
223
- inline static lm_ggml_int8x16x4_t lm_ggml_vld1q_s8_x4(const int8_t * ptr) {
224
- lm_ggml_int8x16x4_t res;
225
-
226
- res.val[0] = vld1q_s8(ptr + 0);
227
- res.val[1] = vld1q_s8(ptr + 16);
228
- res.val[2] = vld1q_s8(ptr + 32);
229
- res.val[3] = vld1q_s8(ptr + 48);
230
-
231
- return res;
232
- }
233
-
234
- // NOTE: not tested
235
- inline static int8x16_t lm_ggml_vqtbl1q_s8(int8x16_t a, uint8x16_t b) {
236
- int8x16_t res;
237
-
238
- res[ 0] = a[b[ 0]];
239
- res[ 1] = a[b[ 1]];
240
- res[ 2] = a[b[ 2]];
241
- res[ 3] = a[b[ 3]];
242
- res[ 4] = a[b[ 4]];
243
- res[ 5] = a[b[ 5]];
244
- res[ 6] = a[b[ 6]];
245
- res[ 7] = a[b[ 7]];
246
- res[ 8] = a[b[ 8]];
247
- res[ 9] = a[b[ 9]];
248
- res[10] = a[b[10]];
249
- res[11] = a[b[11]];
250
- res[12] = a[b[12]];
251
- res[13] = a[b[13]];
252
- res[14] = a[b[14]];
253
- res[15] = a[b[15]];
254
-
255
- return res;
256
- }
257
-
258
- // NOTE: not tested
259
- inline static uint8x16_t lm_ggml_vqtbl1q_u8(uint8x16_t a, uint8x16_t b) {
260
- uint8x16_t res;
261
-
262
- res[ 0] = a[b[ 0]];
263
- res[ 1] = a[b[ 1]];
264
- res[ 2] = a[b[ 2]];
265
- res[ 3] = a[b[ 3]];
266
- res[ 4] = a[b[ 4]];
267
- res[ 5] = a[b[ 5]];
268
- res[ 6] = a[b[ 6]];
269
- res[ 7] = a[b[ 7]];
270
- res[ 8] = a[b[ 8]];
271
- res[ 9] = a[b[ 9]];
272
- res[10] = a[b[10]];
273
- res[11] = a[b[11]];
274
- res[12] = a[b[12]];
275
- res[13] = a[b[13]];
276
- res[14] = a[b[14]];
277
- res[15] = a[b[15]];
278
-
279
- return res;
280
- }
281
-
282
- #else
283
-
284
- #define lm_ggml_int16x8x2_t int16x8x2_t
285
- #define lm_ggml_uint8x16x2_t uint8x16x2_t
286
- #define lm_ggml_uint8x16x4_t uint8x16x4_t
287
- #define lm_ggml_int8x16x2_t int8x16x2_t
288
- #define lm_ggml_int8x16x4_t int8x16x4_t
289
-
290
- #define lm_ggml_vld1q_s16_x2 vld1q_s16_x2
291
- #define lm_ggml_vld1q_u8_x2 vld1q_u8_x2
292
- #define lm_ggml_vld1q_u8_x4 vld1q_u8_x4
293
- #define lm_ggml_vld1q_s8_x2 vld1q_s8_x2
294
- #define lm_ggml_vld1q_s8_x4 vld1q_s8_x4
295
- #define lm_ggml_vqtbl1q_s8 vqtbl1q_s8
296
- #define lm_ggml_vqtbl1q_u8 vqtbl1q_u8
297
-
298
- #endif // !defined(__aarch64__)
299
-
300
- #if !defined(__ARM_FEATURE_DOTPROD)
301
-
302
- inline static int32x4_t lm_ggml_vdotq_s32(int32x4_t acc, int8x16_t a, int8x16_t b) {
303
- const int16x8_t p0 = vmull_s8(vget_low_s8 (a), vget_low_s8 (b));
304
- const int16x8_t p1 = vmull_s8(vget_high_s8(a), vget_high_s8(b));
305
-
306
- return vaddq_s32(acc, vaddq_s32(vpaddlq_s16(p0), vpaddlq_s16(p1)));
307
- }
308
-
309
- #else
310
-
311
- #define lm_ggml_vdotq_s32(a, b, c) vdotq_s32(a, b, c)
312
-
313
- #endif // !defined(__ARM_FEATURE_DOTPROD)
314
-
315
- #endif // defined(__ARM_NEON)
316
-
317
- #ifdef __wasm_simd128__
318
- #include <wasm_simd128.h>
319
- #else
320
- #ifdef __POWER9_VECTOR__
321
- #include <altivec.h>
322
- #undef bool
323
- #define bool _Bool
324
- #else
325
- #if defined(_MSC_VER) || defined(__MINGW32__)
326
- #include <intrin.h>
327
- #else
328
- #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__) || defined(__SSE__)
329
- #if !defined(__riscv)
330
- #include <immintrin.h>
331
- #endif
332
- #endif
333
- #endif
334
- #endif
335
- #endif
336
-
337
- #ifdef __riscv_v_intrinsic
338
- #include <riscv_vector.h>
339
- #endif
340
-
341
- #if defined(__loongarch64)
342
- #if defined(__loongarch_asx)
343
- #include <lasxintrin.h>
344
- #endif
345
- #if defined(__loongarch_sx)
346
- #include <lsxintrin.h>
347
- #endif
348
- #endif
349
-
350
- #if defined(__loongarch_asx)
351
-
352
- typedef union {
353
- int32_t i;
354
- float f;
355
- } ft_union;
356
-
357
- /* float type data load instructions */
358
- static __m128 __lsx_vreplfr2vr_s(float val) {
359
- ft_union fi_tmpval = {.f = val};
360
- return (__m128)__lsx_vreplgr2vr_w(fi_tmpval.i);
361
- }
362
-
363
- static __m256 __lasx_xvreplfr2vr_s(float val) {
364
- ft_union fi_tmpval = {.f = val};
365
- return (__m256)__lasx_xvreplgr2vr_w(fi_tmpval.i);
366
- }
367
- #endif
368
-
369
- #ifdef __cplusplus
370
- }
371
- #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