cui-llama.rn 1.4.3 → 1.4.6

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 (134) hide show
  1. package/README.md +93 -114
  2. package/android/src/main/CMakeLists.txt +5 -0
  3. package/android/src/main/java/com/rnllama/LlamaContext.java +91 -17
  4. package/android/src/main/java/com/rnllama/RNLlama.java +37 -4
  5. package/android/src/main/jni-utils.h +6 -0
  6. package/android/src/main/jni.cpp +289 -31
  7. package/android/src/main/jniLibs/arm64-v8a/librnllama.so +0 -0
  8. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8.so +0 -0
  9. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2.so +0 -0
  10. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod.so +0 -0
  11. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod_i8mm.so +0 -0
  12. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_i8mm.so +0 -0
  13. package/android/src/main/jniLibs/x86_64/librnllama.so +0 -0
  14. package/android/src/main/jniLibs/x86_64/librnllama_x86_64.so +0 -0
  15. package/android/src/newarch/java/com/rnllama/RNLlamaModule.java +7 -2
  16. package/android/src/oldarch/java/com/rnllama/RNLlamaModule.java +7 -2
  17. package/cpp/chat-template.hpp +529 -0
  18. package/cpp/chat.cpp +1779 -0
  19. package/cpp/chat.h +135 -0
  20. package/cpp/common.cpp +2064 -1873
  21. package/cpp/common.h +700 -699
  22. package/cpp/ggml-alloc.c +1039 -1042
  23. package/cpp/ggml-alloc.h +1 -1
  24. package/cpp/ggml-backend-impl.h +255 -255
  25. package/cpp/ggml-backend-reg.cpp +586 -582
  26. package/cpp/ggml-backend.cpp +2004 -2002
  27. package/cpp/ggml-backend.h +354 -354
  28. package/cpp/ggml-common.h +1851 -1853
  29. package/cpp/ggml-cpp.h +39 -39
  30. package/cpp/ggml-cpu-aarch64.cpp +4248 -4247
  31. package/cpp/ggml-cpu-aarch64.h +8 -8
  32. package/cpp/ggml-cpu-impl.h +531 -386
  33. package/cpp/ggml-cpu-quants.c +12527 -10920
  34. package/cpp/ggml-cpu-traits.cpp +36 -36
  35. package/cpp/ggml-cpu-traits.h +38 -38
  36. package/cpp/ggml-cpu.c +15766 -14391
  37. package/cpp/ggml-cpu.cpp +655 -635
  38. package/cpp/ggml-cpu.h +138 -135
  39. package/cpp/ggml-impl.h +567 -567
  40. package/cpp/ggml-metal-impl.h +235 -0
  41. package/cpp/ggml-metal.h +1 -1
  42. package/cpp/ggml-metal.m +5146 -4884
  43. package/cpp/ggml-opt.cpp +854 -854
  44. package/cpp/ggml-opt.h +216 -216
  45. package/cpp/ggml-quants.c +5238 -5238
  46. package/cpp/ggml-threading.h +14 -14
  47. package/cpp/ggml.c +6529 -6514
  48. package/cpp/ggml.h +2198 -2194
  49. package/cpp/gguf.cpp +1329 -1329
  50. package/cpp/gguf.h +202 -202
  51. package/cpp/json-schema-to-grammar.cpp +1024 -1045
  52. package/cpp/json-schema-to-grammar.h +21 -8
  53. package/cpp/json.hpp +24766 -24766
  54. package/cpp/llama-adapter.cpp +347 -347
  55. package/cpp/llama-adapter.h +74 -74
  56. package/cpp/llama-arch.cpp +1513 -1487
  57. package/cpp/llama-arch.h +403 -400
  58. package/cpp/llama-batch.cpp +368 -368
  59. package/cpp/llama-batch.h +88 -88
  60. package/cpp/llama-chat.cpp +588 -578
  61. package/cpp/llama-chat.h +53 -52
  62. package/cpp/llama-context.cpp +1775 -1775
  63. package/cpp/llama-context.h +128 -128
  64. package/cpp/llama-cparams.cpp +1 -1
  65. package/cpp/llama-cparams.h +37 -37
  66. package/cpp/llama-cpp.h +30 -30
  67. package/cpp/llama-grammar.cpp +1219 -1139
  68. package/cpp/llama-grammar.h +173 -143
  69. package/cpp/llama-hparams.cpp +71 -71
  70. package/cpp/llama-hparams.h +139 -139
  71. package/cpp/llama-impl.cpp +167 -167
  72. package/cpp/llama-impl.h +61 -61
  73. package/cpp/llama-kv-cache.cpp +718 -718
  74. package/cpp/llama-kv-cache.h +219 -218
  75. package/cpp/llama-mmap.cpp +600 -590
  76. package/cpp/llama-mmap.h +68 -67
  77. package/cpp/llama-model-loader.cpp +1124 -1124
  78. package/cpp/llama-model-loader.h +167 -167
  79. package/cpp/llama-model.cpp +4087 -3997
  80. package/cpp/llama-model.h +370 -370
  81. package/cpp/llama-sampling.cpp +2558 -2408
  82. package/cpp/llama-sampling.h +32 -32
  83. package/cpp/llama-vocab.cpp +3264 -3247
  84. package/cpp/llama-vocab.h +125 -125
  85. package/cpp/llama.cpp +10284 -10077
  86. package/cpp/llama.h +1354 -1323
  87. package/cpp/log.cpp +393 -401
  88. package/cpp/log.h +132 -121
  89. package/cpp/minja/chat-template.hpp +529 -0
  90. package/cpp/minja/minja.hpp +2915 -0
  91. package/cpp/minja.hpp +2915 -0
  92. package/cpp/rn-llama.cpp +66 -6
  93. package/cpp/rn-llama.h +26 -1
  94. package/cpp/sampling.cpp +570 -505
  95. package/cpp/sampling.h +3 -0
  96. package/cpp/sgemm.cpp +2598 -2597
  97. package/cpp/sgemm.h +14 -14
  98. package/cpp/speculative.cpp +278 -277
  99. package/cpp/speculative.h +28 -28
  100. package/cpp/unicode.cpp +9 -2
  101. package/ios/CMakeLists.txt +6 -0
  102. package/ios/RNLlama.h +0 -8
  103. package/ios/RNLlama.mm +27 -3
  104. package/ios/RNLlamaContext.h +10 -1
  105. package/ios/RNLlamaContext.mm +269 -57
  106. package/jest/mock.js +21 -2
  107. package/lib/commonjs/NativeRNLlama.js.map +1 -1
  108. package/lib/commonjs/grammar.js +3 -0
  109. package/lib/commonjs/grammar.js.map +1 -1
  110. package/lib/commonjs/index.js +87 -13
  111. package/lib/commonjs/index.js.map +1 -1
  112. package/lib/module/NativeRNLlama.js.map +1 -1
  113. package/lib/module/grammar.js +3 -0
  114. package/lib/module/grammar.js.map +1 -1
  115. package/lib/module/index.js +86 -13
  116. package/lib/module/index.js.map +1 -1
  117. package/lib/typescript/NativeRNLlama.d.ts +107 -2
  118. package/lib/typescript/NativeRNLlama.d.ts.map +1 -1
  119. package/lib/typescript/grammar.d.ts.map +1 -1
  120. package/lib/typescript/index.d.ts +32 -7
  121. package/lib/typescript/index.d.ts.map +1 -1
  122. package/llama-rn.podspec +1 -1
  123. package/package.json +3 -2
  124. package/src/NativeRNLlama.ts +115 -3
  125. package/src/grammar.ts +3 -0
  126. package/src/index.ts +138 -21
  127. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CMakeCCompiler.cmake +0 -81
  128. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CMakeSystem.cmake +0 -15
  129. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdC/CMakeCCompilerId.c +0 -904
  130. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdC/CMakeCCompilerId.o +0 -0
  131. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdCXX/CMakeCXXCompilerId.cpp +0 -919
  132. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdCXX/CMakeCXXCompilerId.o +0 -0
  133. package/android/src/main/build-arm64/CMakeFiles/CMakeConfigureLog.yaml +0 -55
  134. package/cpp/rn-llama.hpp +0 -913
@@ -1,386 +1,531 @@
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(__s390x__) && defined(__VEC__)
63
+ #ifndef __VXE__
64
+ #define __VXE__
65
+ #endif
66
+ #ifndef __VXE2__
67
+ #define __VXE2__
68
+ #endif
69
+ #endif
70
+
71
+ #if defined(__ARM_FEATURE_SVE)
72
+ #include <arm_sve.h>
73
+ #include <sys/prctl.h>
74
+ #endif
75
+
76
+ // 16-bit float
77
+ // on Arm, we use __fp16
78
+ // on x86, we use uint16_t
79
+ #if defined(__ARM_NEON)
80
+
81
+ // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
82
+ //
83
+ // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
84
+ //
85
+ #include <arm_neon.h>
86
+
87
+ #ifdef _MSC_VER
88
+
89
+ typedef uint16_t lm_ggml_fp16_internal_t;
90
+
91
+ #define lm_ggml_vld1q_u32(w,x,y,z) { ((w) + ((uint64_t)(x) << 32)), ((y) + ((uint64_t)(z) << 32)) }
92
+
93
+ #else
94
+
95
+ typedef __fp16 lm_ggml_fp16_internal_t;
96
+
97
+ #define lm_ggml_vld1q_u32(w,x,y,z) { (w), (x), (y), (z) }
98
+
99
+ #endif // _MSC_VER
100
+
101
+ #if !defined(__aarch64__)
102
+
103
+ // 32-bit ARM compatibility
104
+
105
+ // vaddlvq_s16
106
+ // vpaddq_s16
107
+ // vpaddq_s32
108
+ // vaddvq_s32
109
+ // vaddvq_f32
110
+ // vmaxvq_f32
111
+ // vcvtnq_s32_f32
112
+ // vzip1_u8
113
+ // vzip2_u8
114
+
115
+ inline static int32_t vaddlvq_s16(int16x8_t v) {
116
+ int32x4_t v0 = vreinterpretq_s32_s64(vpaddlq_s32(vpaddlq_s16(v)));
117
+ return vgetq_lane_s32(v0, 0) + vgetq_lane_s32(v0, 2);
118
+ }
119
+
120
+ inline static int16x8_t vpaddq_s16(int16x8_t a, int16x8_t b) {
121
+ int16x4_t a0 = vpadd_s16(vget_low_s16(a), vget_high_s16(a));
122
+ int16x4_t b0 = vpadd_s16(vget_low_s16(b), vget_high_s16(b));
123
+ return vcombine_s16(a0, b0);
124
+ }
125
+
126
+ inline static int32x4_t vpaddq_s32(int32x4_t a, int32x4_t b) {
127
+ int32x2_t a0 = vpadd_s32(vget_low_s32(a), vget_high_s32(a));
128
+ int32x2_t b0 = vpadd_s32(vget_low_s32(b), vget_high_s32(b));
129
+ return vcombine_s32(a0, b0);
130
+ }
131
+
132
+ inline static int32_t vaddvq_s32(int32x4_t v) {
133
+ return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
134
+ }
135
+
136
+ inline static float vaddvq_f32(float32x4_t v) {
137
+ return vgetq_lane_f32(v, 0) + vgetq_lane_f32(v, 1) + vgetq_lane_f32(v, 2) + vgetq_lane_f32(v, 3);
138
+ }
139
+
140
+ inline static float vmaxvq_f32(float32x4_t v) {
141
+ return
142
+ MAX(MAX(vgetq_lane_f32(v, 0), vgetq_lane_f32(v, 1)),
143
+ MAX(vgetq_lane_f32(v, 2), vgetq_lane_f32(v, 3)));
144
+ }
145
+
146
+ inline static int32x4_t vcvtnq_s32_f32(float32x4_t v) {
147
+ int32x4_t res;
148
+
149
+ res[0] = roundf(vgetq_lane_f32(v, 0));
150
+ res[1] = roundf(vgetq_lane_f32(v, 1));
151
+ res[2] = roundf(vgetq_lane_f32(v, 2));
152
+ res[3] = roundf(vgetq_lane_f32(v, 3));
153
+
154
+ return res;
155
+ }
156
+
157
+ inline static uint8x8_t vzip1_u8(uint8x8_t a, uint8x8_t b) {
158
+ uint8x8_t res;
159
+
160
+ res[0] = a[0]; res[1] = b[0];
161
+ res[2] = a[1]; res[3] = b[1];
162
+ res[4] = a[2]; res[5] = b[2];
163
+ res[6] = a[3]; res[7] = b[3];
164
+
165
+ return res;
166
+ }
167
+
168
+ inline static uint8x8_t vzip2_u8(uint8x8_t a, uint8x8_t b) {
169
+ uint8x8_t res;
170
+
171
+ res[0] = a[4]; res[1] = b[4];
172
+ res[2] = a[5]; res[3] = b[5];
173
+ res[4] = a[6]; res[5] = b[6];
174
+ res[6] = a[7]; res[7] = b[7];
175
+
176
+ return res;
177
+ }
178
+
179
+ // vld1q_s16_x2
180
+ // vld1q_u8_x2
181
+ // vld1q_u8_x4
182
+ // vld1q_s8_x2
183
+ // vld1q_s8_x4
184
+ // TODO: double-check these work correctly
185
+
186
+ typedef struct lm_ggml_int16x8x2_t {
187
+ int16x8_t val[2];
188
+ } lm_ggml_int16x8x2_t;
189
+
190
+ inline static lm_ggml_int16x8x2_t lm_ggml_vld1q_s16_x2(const int16_t * ptr) {
191
+ lm_ggml_int16x8x2_t res;
192
+
193
+ res.val[0] = vld1q_s16(ptr + 0);
194
+ res.val[1] = vld1q_s16(ptr + 8);
195
+
196
+ return res;
197
+ }
198
+
199
+ typedef struct lm_ggml_uint8x16x2_t {
200
+ uint8x16_t val[2];
201
+ } lm_ggml_uint8x16x2_t;
202
+
203
+ inline static lm_ggml_uint8x16x2_t lm_ggml_vld1q_u8_x2(const uint8_t * ptr) {
204
+ lm_ggml_uint8x16x2_t res;
205
+
206
+ res.val[0] = vld1q_u8(ptr + 0);
207
+ res.val[1] = vld1q_u8(ptr + 16);
208
+
209
+ return res;
210
+ }
211
+
212
+ typedef struct lm_ggml_uint8x16x4_t {
213
+ uint8x16_t val[4];
214
+ } lm_ggml_uint8x16x4_t;
215
+
216
+ inline static lm_ggml_uint8x16x4_t lm_ggml_vld1q_u8_x4(const uint8_t * ptr) {
217
+ lm_ggml_uint8x16x4_t res;
218
+
219
+ res.val[0] = vld1q_u8(ptr + 0);
220
+ res.val[1] = vld1q_u8(ptr + 16);
221
+ res.val[2] = vld1q_u8(ptr + 32);
222
+ res.val[3] = vld1q_u8(ptr + 48);
223
+
224
+ return res;
225
+ }
226
+
227
+ typedef struct lm_ggml_int8x16x2_t {
228
+ int8x16_t val[2];
229
+ } lm_ggml_int8x16x2_t;
230
+
231
+ inline static lm_ggml_int8x16x2_t lm_ggml_vld1q_s8_x2(const int8_t * ptr) {
232
+ lm_ggml_int8x16x2_t res;
233
+
234
+ res.val[0] = vld1q_s8(ptr + 0);
235
+ res.val[1] = vld1q_s8(ptr + 16);
236
+
237
+ return res;
238
+ }
239
+
240
+ typedef struct lm_ggml_int8x16x4_t {
241
+ int8x16_t val[4];
242
+ } lm_ggml_int8x16x4_t;
243
+
244
+ inline static lm_ggml_int8x16x4_t lm_ggml_vld1q_s8_x4(const int8_t * ptr) {
245
+ lm_ggml_int8x16x4_t res;
246
+
247
+ res.val[0] = vld1q_s8(ptr + 0);
248
+ res.val[1] = vld1q_s8(ptr + 16);
249
+ res.val[2] = vld1q_s8(ptr + 32);
250
+ res.val[3] = vld1q_s8(ptr + 48);
251
+
252
+ return res;
253
+ }
254
+
255
+ // NOTE: not tested
256
+ inline static int8x16_t lm_ggml_vqtbl1q_s8(int8x16_t a, uint8x16_t b) {
257
+ int8x16_t res;
258
+
259
+ res[ 0] = a[b[ 0]];
260
+ res[ 1] = a[b[ 1]];
261
+ res[ 2] = a[b[ 2]];
262
+ res[ 3] = a[b[ 3]];
263
+ res[ 4] = a[b[ 4]];
264
+ res[ 5] = a[b[ 5]];
265
+ res[ 6] = a[b[ 6]];
266
+ res[ 7] = a[b[ 7]];
267
+ res[ 8] = a[b[ 8]];
268
+ res[ 9] = a[b[ 9]];
269
+ res[10] = a[b[10]];
270
+ res[11] = a[b[11]];
271
+ res[12] = a[b[12]];
272
+ res[13] = a[b[13]];
273
+ res[14] = a[b[14]];
274
+ res[15] = a[b[15]];
275
+
276
+ return res;
277
+ }
278
+
279
+ // NOTE: not tested
280
+ inline static uint8x16_t lm_ggml_vqtbl1q_u8(uint8x16_t a, uint8x16_t b) {
281
+ uint8x16_t res;
282
+
283
+ res[ 0] = a[b[ 0]];
284
+ res[ 1] = a[b[ 1]];
285
+ res[ 2] = a[b[ 2]];
286
+ res[ 3] = a[b[ 3]];
287
+ res[ 4] = a[b[ 4]];
288
+ res[ 5] = a[b[ 5]];
289
+ res[ 6] = a[b[ 6]];
290
+ res[ 7] = a[b[ 7]];
291
+ res[ 8] = a[b[ 8]];
292
+ res[ 9] = a[b[ 9]];
293
+ res[10] = a[b[10]];
294
+ res[11] = a[b[11]];
295
+ res[12] = a[b[12]];
296
+ res[13] = a[b[13]];
297
+ res[14] = a[b[14]];
298
+ res[15] = a[b[15]];
299
+
300
+ return res;
301
+ }
302
+
303
+ #else
304
+
305
+ #define lm_ggml_int16x8x2_t int16x8x2_t
306
+ #define lm_ggml_uint8x16x2_t uint8x16x2_t
307
+ #define lm_ggml_uint8x16x4_t uint8x16x4_t
308
+ #define lm_ggml_int8x16x2_t int8x16x2_t
309
+ #define lm_ggml_int8x16x4_t int8x16x4_t
310
+
311
+ #define lm_ggml_vld1q_s16_x2 vld1q_s16_x2
312
+ #define lm_ggml_vld1q_u8_x2 vld1q_u8_x2
313
+ #define lm_ggml_vld1q_u8_x4 vld1q_u8_x4
314
+ #define lm_ggml_vld1q_s8_x2 vld1q_s8_x2
315
+ #define lm_ggml_vld1q_s8_x4 vld1q_s8_x4
316
+ #define lm_ggml_vqtbl1q_s8 vqtbl1q_s8
317
+ #define lm_ggml_vqtbl1q_u8 vqtbl1q_u8
318
+
319
+ #endif // !defined(__aarch64__)
320
+
321
+ #if !defined(__ARM_FEATURE_DOTPROD)
322
+
323
+ inline static int32x4_t lm_ggml_vdotq_s32(int32x4_t acc, int8x16_t a, int8x16_t b) {
324
+ const int16x8_t p0 = vmull_s8(vget_low_s8 (a), vget_low_s8 (b));
325
+ const int16x8_t p1 = vmull_s8(vget_high_s8(a), vget_high_s8(b));
326
+
327
+ return vaddq_s32(acc, vaddq_s32(vpaddlq_s16(p0), vpaddlq_s16(p1)));
328
+ }
329
+
330
+ #else
331
+
332
+ #define lm_ggml_vdotq_s32(a, b, c) vdotq_s32(a, b, c)
333
+
334
+ #endif // !defined(__ARM_FEATURE_DOTPROD)
335
+
336
+ #endif // defined(__ARM_NEON)
337
+
338
+ #ifdef __wasm_simd128__
339
+ #include <wasm_simd128.h>
340
+ #else
341
+ #ifdef __POWER9_VECTOR__
342
+ #include <altivec.h>
343
+ #undef bool
344
+ #define bool _Bool
345
+ #else
346
+ #if defined(_MSC_VER) || defined(__MINGW32__)
347
+ #include <intrin.h>
348
+ #else
349
+ #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__) || defined(__SSE__)
350
+ #if !defined(__riscv)
351
+ #include <immintrin.h>
352
+ #endif
353
+ #endif
354
+ #endif
355
+ #endif
356
+ #endif
357
+
358
+ #ifdef __riscv_v_intrinsic
359
+ #include <riscv_vector.h>
360
+ #endif
361
+
362
+ #if defined(__loongarch64)
363
+ #if defined(__loongarch_asx)
364
+ #include <lasxintrin.h>
365
+ #endif
366
+ #if defined(__loongarch_sx)
367
+ #include <lsxintrin.h>
368
+ #endif
369
+ #endif
370
+
371
+ #if defined(__VXE__) || defined(__VXE2__)
372
+ #include <vecintrin.h>
373
+
374
+ #define vec_neg(a) (-(a)) // Vector Negate
375
+ #define vec_add(a, b) ((a) + (b)) // Vector Add
376
+ #define vec_sub(a, b) ((a) - (b)) // Vector Subtract
377
+ #define vec_mul(a, b) ((a) * (b)) // Vector Multiply
378
+ #define vec_div(a, b) ((a) / (b)) // Vector Divide
379
+ #define vec_sl(a, b) ((a) << (b)) // Vector Shift Left
380
+ #define vec_sra(a, b) ((a) >> (b)) // Vector Shift Right
381
+ #define vec_sr(a, b) ((a) >> (b)) // Vector Shift Right Algebraic
382
+ #define vec_slo(a, b) vec_slb(a, (b) << 64) // Vector Shift Left by Octet
383
+ #define vec_sro(a, b) vec_srb(a, (b) << 64) // Vector Shift Right by Octet
384
+
385
+ #ifndef vec_and
386
+ #define vec_and(a, b) ((a) & (b)) // Vector AND
387
+ #endif
388
+
389
+ #ifndef vec_or
390
+ #define vec_or(a, b) ((a) | (b)) // Vector OR
391
+ #endif
392
+
393
+ #ifndef vec_xor
394
+ #define vec_xor(a, b) ((a) ^ (b)) // Vector XOR
395
+ #endif
396
+
397
+ typedef signed char char8x16_t __attribute__((vector_size(16)));
398
+ typedef unsigned char uchar8x16_t __attribute__((vector_size(16)));
399
+
400
+ typedef int8_t int8x16_t __attribute__((vector_size(16)));
401
+ typedef int16_t int16x8_t __attribute__((vector_size(16)));
402
+ typedef int32_t int32x4_t __attribute__((vector_size(16)));
403
+
404
+ typedef uint8_t uint8x16_t __attribute__((vector_size(16)));
405
+ typedef uint16_t uint16x8_t __attribute__((vector_size(16)));
406
+ typedef uint32_t uint32x4_t __attribute__((vector_size(16)));
407
+
408
+ typedef float float32x4_t __attribute__((vector_size(16)));
409
+ typedef double double64x2_t __attribute((vector_size(16)));
410
+
411
+ typedef signed long long long64x2_t __attribute((vector_size(16)));
412
+ typedef unsigned long long ulong64x2_t __attribute__((vector_size(16)));
413
+
414
+ typedef struct lm_ggml_uint8x16x2_t {
415
+ uint8x16_t val[2];
416
+ } lm_ggml_uint8x16x2_t;
417
+
418
+ inline static lm_ggml_uint8x16x2_t lm_ggml_vec_xl_u8x2(const uint8_t * ptr) {
419
+ lm_ggml_uint8x16x2_t res;
420
+
421
+ res.val[0] = vec_xl( 0, ptr);
422
+ res.val[1] = vec_xl(16, ptr);
423
+
424
+ return res;
425
+ }
426
+
427
+ typedef struct lm_ggml_uint8x16x4_t {
428
+ uint8x16_t val[4];
429
+ } lm_ggml_uint8x16x4_t;
430
+
431
+ inline static lm_ggml_uint8x16x4_t lm_ggml_vec_xl_u8x4(const uint8_t * ptr) {
432
+ lm_ggml_uint8x16x4_t res;
433
+
434
+ res.val[0] = vec_xl( 0, ptr);
435
+ res.val[1] = vec_xl(16, ptr);
436
+ res.val[2] = vec_xl(32, ptr);
437
+ res.val[3] = vec_xl(48, ptr);
438
+
439
+ return res;
440
+ }
441
+
442
+ typedef struct lm_ggml_int8x16x4_t {
443
+ int8x16_t val[4];
444
+ } lm_ggml_int8x16x4_t;
445
+
446
+ inline static lm_ggml_int8x16x4_t lm_ggml_vec_xl_s8x4(const int8_t * ptr) {
447
+ lm_ggml_int8x16x4_t res;
448
+
449
+ res.val[0] = vec_xl( 0, ptr);
450
+ res.val[1] = vec_xl(16, ptr);
451
+ res.val[2] = vec_xl(32, ptr);
452
+ res.val[3] = vec_xl(48, ptr);
453
+
454
+ return res;
455
+ }
456
+
457
+ typedef struct lm_ggml_int16x8x2_t {
458
+ int16x8_t val[2];
459
+ } lm_ggml_int16x8x2_t;
460
+
461
+ inline static lm_ggml_int16x8x2_t lm_ggml_vec_xl_s16x2(const int16_t * ptr) {
462
+ lm_ggml_int16x8x2_t res;
463
+
464
+ res.val[0] = vec_xl( 0, ptr);
465
+ res.val[1] = vec_xl(16, ptr);
466
+
467
+ return res;
468
+ }
469
+
470
+ /*
471
+ ! WARNING: Very slow. Use vec_perm if possible. Refer to iq4_xs
472
+ ! or iq4_nl for example implementation.
473
+ */
474
+ inline static int8x16_t lm_ggml_vec_tbl(int8x16_t a, uint8x16_t b) {
475
+ int8x16_t res;
476
+
477
+ res[ 0] = a[b[ 0]];
478
+ res[ 1] = a[b[ 1]];
479
+ res[ 2] = a[b[ 2]];
480
+ res[ 3] = a[b[ 3]];
481
+ res[ 4] = a[b[ 4]];
482
+ res[ 5] = a[b[ 5]];
483
+ res[ 6] = a[b[ 6]];
484
+ res[ 7] = a[b[ 7]];
485
+ res[ 8] = a[b[ 8]];
486
+ res[ 9] = a[b[ 9]];
487
+ res[10] = a[b[10]];
488
+ res[11] = a[b[11]];
489
+ res[12] = a[b[12]];
490
+ res[13] = a[b[13]];
491
+ res[14] = a[b[14]];
492
+ res[15] = a[b[15]];
493
+
494
+ return res;
495
+ }
496
+
497
+ inline static int16x8_t vec_padd_s16(int16x8_t a, int16x8_t b) {
498
+ const uchar8x16_t v_maske = { 0, 1, 4, 5, 8, 9, 12, 13,
499
+ 16, 17, 20, 21, 24, 25, 28, 29 };
500
+
501
+ const int16x8_t v_abo = vec_pack((int32x4_t)a, (int32x4_t)b);
502
+ const int16x8_t v_abe = vec_perm(a, b, v_maske);
503
+ return v_abo + v_abe;
504
+ }
505
+
506
+ inline static int32x4_t lm_ggml_vec_dot(int32x4_t acc, int8x16_t a, int8x16_t b) {
507
+ const int16x8_t p = vec_mule(a, b) + vec_mulo(a, b);
508
+ return acc + (vec_unpackh(p) + vec_unpackl(p));
509
+ }
510
+
511
+ #endif
512
+
513
+ #if defined(__loongarch_asx)
514
+ /* float type data load instructions */
515
+ static __m128 __lsx_vreplfr2vr_s(const float val) {
516
+ v4f32 res = {val, val, val, val};
517
+ return (__m128)res;
518
+ }
519
+
520
+ static __m256 __lasx_xvreplfr2vr_s(const float val) {
521
+ v8f32 res = {val, val, val, val, val, val, val, val};
522
+ return (__m256)res;
523
+ }
524
+ #endif
525
+
526
+ // TODO: move to ggml-threading
527
+ void lm_ggml_barrier(struct lm_ggml_threadpool * tp);
528
+
529
+ #ifdef __cplusplus
530
+ }
531
+ #endif