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.
- package/README.md +93 -114
- package/android/src/main/CMakeLists.txt +5 -0
- package/android/src/main/java/com/rnllama/LlamaContext.java +91 -17
- package/android/src/main/java/com/rnllama/RNLlama.java +37 -4
- package/android/src/main/jni-utils.h +6 -0
- package/android/src/main/jni.cpp +289 -31
- package/android/src/main/jniLibs/arm64-v8a/librnllama.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librnllama_v8.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod_i8mm.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_i8mm.so +0 -0
- package/android/src/main/jniLibs/x86_64/librnllama.so +0 -0
- package/android/src/main/jniLibs/x86_64/librnllama_x86_64.so +0 -0
- package/android/src/newarch/java/com/rnllama/RNLlamaModule.java +7 -2
- package/android/src/oldarch/java/com/rnllama/RNLlamaModule.java +7 -2
- package/cpp/chat-template.hpp +529 -0
- package/cpp/chat.cpp +1779 -0
- package/cpp/chat.h +135 -0
- package/cpp/common.cpp +2064 -1873
- package/cpp/common.h +700 -699
- package/cpp/ggml-alloc.c +1039 -1042
- package/cpp/ggml-alloc.h +1 -1
- package/cpp/ggml-backend-impl.h +255 -255
- package/cpp/ggml-backend-reg.cpp +586 -582
- package/cpp/ggml-backend.cpp +2004 -2002
- package/cpp/ggml-backend.h +354 -354
- package/cpp/ggml-common.h +1851 -1853
- package/cpp/ggml-cpp.h +39 -39
- package/cpp/ggml-cpu-aarch64.cpp +4248 -4247
- package/cpp/ggml-cpu-aarch64.h +8 -8
- package/cpp/ggml-cpu-impl.h +531 -386
- package/cpp/ggml-cpu-quants.c +12527 -10920
- package/cpp/ggml-cpu-traits.cpp +36 -36
- package/cpp/ggml-cpu-traits.h +38 -38
- package/cpp/ggml-cpu.c +15766 -14391
- package/cpp/ggml-cpu.cpp +655 -635
- package/cpp/ggml-cpu.h +138 -135
- package/cpp/ggml-impl.h +567 -567
- package/cpp/ggml-metal-impl.h +235 -0
- package/cpp/ggml-metal.h +1 -1
- package/cpp/ggml-metal.m +5146 -4884
- package/cpp/ggml-opt.cpp +854 -854
- package/cpp/ggml-opt.h +216 -216
- package/cpp/ggml-quants.c +5238 -5238
- package/cpp/ggml-threading.h +14 -14
- package/cpp/ggml.c +6529 -6514
- package/cpp/ggml.h +2198 -2194
- package/cpp/gguf.cpp +1329 -1329
- package/cpp/gguf.h +202 -202
- package/cpp/json-schema-to-grammar.cpp +1024 -1045
- package/cpp/json-schema-to-grammar.h +21 -8
- package/cpp/json.hpp +24766 -24766
- package/cpp/llama-adapter.cpp +347 -347
- package/cpp/llama-adapter.h +74 -74
- package/cpp/llama-arch.cpp +1513 -1487
- package/cpp/llama-arch.h +403 -400
- package/cpp/llama-batch.cpp +368 -368
- package/cpp/llama-batch.h +88 -88
- package/cpp/llama-chat.cpp +588 -578
- package/cpp/llama-chat.h +53 -52
- package/cpp/llama-context.cpp +1775 -1775
- package/cpp/llama-context.h +128 -128
- package/cpp/llama-cparams.cpp +1 -1
- package/cpp/llama-cparams.h +37 -37
- package/cpp/llama-cpp.h +30 -30
- package/cpp/llama-grammar.cpp +1219 -1139
- package/cpp/llama-grammar.h +173 -143
- package/cpp/llama-hparams.cpp +71 -71
- package/cpp/llama-hparams.h +139 -139
- package/cpp/llama-impl.cpp +167 -167
- package/cpp/llama-impl.h +61 -61
- package/cpp/llama-kv-cache.cpp +718 -718
- package/cpp/llama-kv-cache.h +219 -218
- package/cpp/llama-mmap.cpp +600 -590
- package/cpp/llama-mmap.h +68 -67
- package/cpp/llama-model-loader.cpp +1124 -1124
- package/cpp/llama-model-loader.h +167 -167
- package/cpp/llama-model.cpp +4087 -3997
- package/cpp/llama-model.h +370 -370
- package/cpp/llama-sampling.cpp +2558 -2408
- package/cpp/llama-sampling.h +32 -32
- package/cpp/llama-vocab.cpp +3264 -3247
- package/cpp/llama-vocab.h +125 -125
- package/cpp/llama.cpp +10284 -10077
- package/cpp/llama.h +1354 -1323
- package/cpp/log.cpp +393 -401
- package/cpp/log.h +132 -121
- package/cpp/minja/chat-template.hpp +529 -0
- package/cpp/minja/minja.hpp +2915 -0
- package/cpp/minja.hpp +2915 -0
- package/cpp/rn-llama.cpp +66 -6
- package/cpp/rn-llama.h +26 -1
- package/cpp/sampling.cpp +570 -505
- package/cpp/sampling.h +3 -0
- package/cpp/sgemm.cpp +2598 -2597
- package/cpp/sgemm.h +14 -14
- package/cpp/speculative.cpp +278 -277
- package/cpp/speculative.h +28 -28
- package/cpp/unicode.cpp +9 -2
- package/ios/CMakeLists.txt +6 -0
- package/ios/RNLlama.h +0 -8
- package/ios/RNLlama.mm +27 -3
- package/ios/RNLlamaContext.h +10 -1
- package/ios/RNLlamaContext.mm +269 -57
- package/jest/mock.js +21 -2
- package/lib/commonjs/NativeRNLlama.js.map +1 -1
- package/lib/commonjs/grammar.js +3 -0
- package/lib/commonjs/grammar.js.map +1 -1
- package/lib/commonjs/index.js +87 -13
- package/lib/commonjs/index.js.map +1 -1
- package/lib/module/NativeRNLlama.js.map +1 -1
- package/lib/module/grammar.js +3 -0
- package/lib/module/grammar.js.map +1 -1
- package/lib/module/index.js +86 -13
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/NativeRNLlama.d.ts +107 -2
- package/lib/typescript/NativeRNLlama.d.ts.map +1 -1
- package/lib/typescript/grammar.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +32 -7
- package/lib/typescript/index.d.ts.map +1 -1
- package/llama-rn.podspec +1 -1
- package/package.json +3 -2
- package/src/NativeRNLlama.ts +115 -3
- package/src/grammar.ts +3 -0
- package/src/index.ts +138 -21
- package/android/src/main/build-arm64/CMakeFiles/3.31.4/CMakeCCompiler.cmake +0 -81
- package/android/src/main/build-arm64/CMakeFiles/3.31.4/CMakeSystem.cmake +0 -15
- package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdC/CMakeCCompilerId.c +0 -904
- package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdC/CMakeCCompilerId.o +0 -0
- package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdCXX/CMakeCXXCompilerId.cpp +0 -919
- package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdCXX/CMakeCXXCompilerId.o +0 -0
- package/android/src/main/build-arm64/CMakeFiles/CMakeConfigureLog.yaml +0 -55
- package/cpp/rn-llama.hpp +0 -913
package/cpp/ggml-cpu-impl.h
CHANGED
@@ -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(
|
63
|
-
#
|
64
|
-
#
|
65
|
-
#endif
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
//
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
//
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
res[
|
152
|
-
res[
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
res[
|
163
|
-
res[
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
}
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
}
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
}
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
res[
|
251
|
-
|
252
|
-
res
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
res
|
258
|
-
|
259
|
-
res[
|
260
|
-
res[
|
261
|
-
res[
|
262
|
-
res[
|
263
|
-
res[
|
264
|
-
res[
|
265
|
-
res[
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
res[
|
275
|
-
|
276
|
-
res
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
res
|
282
|
-
|
283
|
-
res[
|
284
|
-
res[
|
285
|
-
res[
|
286
|
-
res[
|
287
|
-
res[
|
288
|
-
res[
|
289
|
-
res[
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
#
|
304
|
-
|
305
|
-
#define
|
306
|
-
#define
|
307
|
-
#define
|
308
|
-
#define
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
#
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
#
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
#
|
331
|
-
|
332
|
-
#
|
333
|
-
|
334
|
-
#
|
335
|
-
|
336
|
-
#
|
337
|
-
|
338
|
-
#
|
339
|
-
#
|
340
|
-
#
|
341
|
-
#
|
342
|
-
#include <
|
343
|
-
#
|
344
|
-
#
|
345
|
-
#
|
346
|
-
#
|
347
|
-
#
|
348
|
-
|
349
|
-
#
|
350
|
-
#
|
351
|
-
#
|
352
|
-
|
353
|
-
#
|
354
|
-
#
|
355
|
-
#
|
356
|
-
#endif
|
357
|
-
|
358
|
-
#
|
359
|
-
#
|
360
|
-
#endif
|
361
|
-
|
362
|
-
#if defined(
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
#
|
380
|
-
|
381
|
-
//
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
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(__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
|