cui-llama.rn 1.2.6 → 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 (75) hide show
  1. package/README.md +3 -2
  2. package/android/src/main/CMakeLists.txt +26 -6
  3. package/android/src/main/java/com/rnllama/LlamaContext.java +115 -27
  4. package/android/src/main/java/com/rnllama/RNLlama.java +40 -7
  5. package/android/src/main/jni.cpp +228 -40
  6. package/android/src/newarch/java/com/rnllama/RNLlamaModule.java +9 -4
  7. package/android/src/oldarch/java/com/rnllama/RNLlamaModule.java +9 -4
  8. package/cpp/amx/amx.cpp +196 -0
  9. package/cpp/amx/amx.h +20 -0
  10. package/cpp/amx/common.h +101 -0
  11. package/cpp/amx/mmq.cpp +2524 -0
  12. package/cpp/amx/mmq.h +16 -0
  13. package/cpp/common.cpp +118 -251
  14. package/cpp/common.h +53 -30
  15. package/cpp/ggml-aarch64.c +46 -3395
  16. package/cpp/ggml-aarch64.h +0 -20
  17. package/cpp/ggml-alloc.c +6 -8
  18. package/cpp/ggml-backend-impl.h +33 -11
  19. package/cpp/ggml-backend-reg.cpp +423 -0
  20. package/cpp/ggml-backend.cpp +14 -676
  21. package/cpp/ggml-backend.h +46 -9
  22. package/cpp/ggml-common.h +6 -0
  23. package/cpp/ggml-cpu-aarch64.c +3823 -0
  24. package/cpp/ggml-cpu-aarch64.h +32 -0
  25. package/cpp/ggml-cpu-impl.h +14 -242
  26. package/cpp/ggml-cpu-quants.c +10835 -0
  27. package/cpp/ggml-cpu-quants.h +63 -0
  28. package/cpp/ggml-cpu.c +13971 -13720
  29. package/cpp/ggml-cpu.cpp +715 -0
  30. package/cpp/ggml-cpu.h +65 -63
  31. package/cpp/ggml-impl.h +285 -25
  32. package/cpp/ggml-metal.h +8 -8
  33. package/cpp/ggml-metal.m +1221 -728
  34. package/cpp/ggml-quants.c +189 -10681
  35. package/cpp/ggml-quants.h +78 -125
  36. package/cpp/ggml-threading.cpp +12 -0
  37. package/cpp/ggml-threading.h +12 -0
  38. package/cpp/ggml.c +688 -1460
  39. package/cpp/ggml.h +58 -244
  40. package/cpp/json-schema-to-grammar.cpp +1045 -1045
  41. package/cpp/json.hpp +24766 -24766
  42. package/cpp/llama-sampling.cpp +5 -2
  43. package/cpp/llama.cpp +409 -123
  44. package/cpp/llama.h +8 -4
  45. package/cpp/rn-llama.hpp +89 -25
  46. package/cpp/sampling.cpp +42 -3
  47. package/cpp/sampling.h +22 -1
  48. package/cpp/sgemm.cpp +608 -0
  49. package/cpp/speculative.cpp +270 -0
  50. package/cpp/speculative.h +28 -0
  51. package/cpp/unicode.cpp +11 -0
  52. package/ios/RNLlama.mm +43 -20
  53. package/ios/RNLlamaContext.h +9 -3
  54. package/ios/RNLlamaContext.mm +146 -33
  55. package/jest/mock.js +0 -1
  56. package/lib/commonjs/NativeRNLlama.js.map +1 -1
  57. package/lib/commonjs/grammar.js +4 -2
  58. package/lib/commonjs/grammar.js.map +1 -1
  59. package/lib/commonjs/index.js +52 -15
  60. package/lib/commonjs/index.js.map +1 -1
  61. package/lib/module/NativeRNLlama.js.map +1 -1
  62. package/lib/module/grammar.js +2 -1
  63. package/lib/module/grammar.js.map +1 -1
  64. package/lib/module/index.js +51 -15
  65. package/lib/module/index.js.map +1 -1
  66. package/lib/typescript/NativeRNLlama.d.ts +122 -8
  67. package/lib/typescript/NativeRNLlama.d.ts.map +1 -1
  68. package/lib/typescript/grammar.d.ts +5 -6
  69. package/lib/typescript/grammar.d.ts.map +1 -1
  70. package/lib/typescript/index.d.ts +15 -6
  71. package/lib/typescript/index.d.ts.map +1 -1
  72. package/package.json +2 -1
  73. package/src/NativeRNLlama.ts +135 -13
  74. package/src/grammar.ts +10 -8
  75. package/src/index.ts +104 -28
@@ -0,0 +1,32 @@
1
+ #pragma once
2
+
3
+ #include "ggml.h"
4
+
5
+ // GGML internal header
6
+
7
+ #ifdef __cplusplus
8
+ extern "C" {
9
+ #endif
10
+
11
+ // Quantization
12
+ void quantize_mat_q8_0(const float * LM_GGML_RESTRICT x, void * LM_GGML_RESTRICT y, int64_t nrows, int64_t n_per_row, int64_t blck_size_interleave);
13
+
14
+ // GEMV
15
+ void lm_ggml_gemv_q4_0_4x4_q8_0(int n, float * LM_GGML_RESTRICT s, size_t bs, const void * LM_GGML_RESTRICT vx, const void * LM_GGML_RESTRICT vy, int nr, int nc);
16
+ void lm_ggml_gemv_q4_0_4x8_q8_0(int n, float * LM_GGML_RESTRICT s, size_t bs, const void * LM_GGML_RESTRICT vx, const void * LM_GGML_RESTRICT vy, int nr, int nc);
17
+ void lm_ggml_gemv_q4_0_8x8_q8_0(int n, float * LM_GGML_RESTRICT s, size_t bs, const void * LM_GGML_RESTRICT vx, const void * LM_GGML_RESTRICT vy, int nr, int nc);
18
+ void lm_ggml_gemv_iq4_nl_4x4_q8_0(int n, float * LM_GGML_RESTRICT s, size_t bs, const void * LM_GGML_RESTRICT vx, const void * LM_GGML_RESTRICT vy, int nr, int nc);
19
+
20
+ // GEMM
21
+ void lm_ggml_gemm_q4_0_4x4_q8_0(int n, float * LM_GGML_RESTRICT s, size_t bs, const void * LM_GGML_RESTRICT vx, const void * LM_GGML_RESTRICT vy, int nr, int nc);
22
+ void lm_ggml_gemm_q4_0_4x8_q8_0(int n, float * LM_GGML_RESTRICT s, size_t bs, const void * LM_GGML_RESTRICT vx, const void * LM_GGML_RESTRICT vy, int nr, int nc);
23
+ void lm_ggml_gemm_q4_0_8x8_q8_0(int n, float * LM_GGML_RESTRICT s, size_t bs, const void * LM_GGML_RESTRICT vx, const void * LM_GGML_RESTRICT vy, int nr, int nc);
24
+ void lm_ggml_gemm_iq4_nl_4x4_q8_0(int n, float * LM_GGML_RESTRICT s, size_t bs, const void * LM_GGML_RESTRICT vx, const void * LM_GGML_RESTRICT vy, int nr, int nc);
25
+
26
+ void lm_ggml_aarch64_repack_tensor(struct lm_ggml_tensor * cur, enum lm_ggml_type repack_type, const void * data, size_t data_size);
27
+ enum lm_ggml_type lm_ggml_aarch64_get_optimal_repack_type(const struct lm_ggml_tensor * cur);
28
+
29
+ #ifdef __cplusplus
30
+ }
31
+ #endif
32
+
@@ -15,6 +15,18 @@
15
15
  extern "C" {
16
16
  #endif
17
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
+
18
30
  #if defined(_MSC_VER)
19
31
 
20
32
  #define m512bh(p) p
@@ -27,80 +39,6 @@ extern "C" {
27
39
 
28
40
  #endif
29
41
 
30
- /**
31
- * Converts brain16 to float32.
32
- *
33
- * The bfloat16 floating point format has the following structure:
34
- *
35
- * ┌sign
36
- * │
37
- * │ ┌exponent
38
- * │ │
39
- * │ │ ┌mantissa
40
- * │ │ │
41
- * │┌──┴───┐┌─┴───┐
42
- * 0b0000000000000000 brain16
43
- *
44
- * Since bf16 has the same number of exponent bits as a 32bit float,
45
- * encoding and decoding numbers becomes relatively straightforward.
46
- *
47
- * ┌sign
48
- * │
49
- * │ ┌exponent
50
- * │ │
51
- * │ │ ┌mantissa
52
- * │ │ │
53
- * │┌──┴───┐┌─┴───────────────────┐
54
- * 0b00000000000000000000000000000000 IEEE binary32
55
- *
56
- * For comparison, the standard fp16 format has fewer exponent bits.
57
- *
58
- * ┌sign
59
- * │
60
- * │ ┌exponent
61
- * │ │
62
- * │ │ ┌mantissa
63
- * │ │ │
64
- * │┌─┴─┐┌─┴──────┐
65
- * 0b0000000000000000 IEEE binary16
66
- *
67
- * @see IEEE 754-2008
68
- */
69
- static inline float lm_ggml_compute_bf16_to_fp32(lm_ggml_bf16_t h) {
70
- union {
71
- float f;
72
- uint32_t i;
73
- } u;
74
- u.i = (uint32_t)h.bits << 16;
75
- return u.f;
76
- }
77
-
78
- /**
79
- * Converts float32 to brain16.
80
- *
81
- * This is binary identical with Google Brain float conversion.
82
- * Floats shall round to nearest even, and NANs shall be quiet.
83
- * Subnormals aren't flushed to zero, except perhaps when used.
84
- * This code should vectorize nicely if using modern compilers.
85
- */
86
- static inline lm_ggml_bf16_t lm_ggml_compute_fp32_to_bf16(float s) {
87
- lm_ggml_bf16_t h;
88
- union {
89
- float f;
90
- uint32_t i;
91
- } u;
92
- u.f = s;
93
- if ((u.i & 0x7fffffff) > 0x7f800000) { /* nan */
94
- h.bits = (u.i >> 16) | 64; /* force to quiet */
95
- return h;
96
- }
97
- h.bits = (u.i + (0x7fff + ((u.i >> 16) & 1))) >> 16;
98
- return h;
99
- }
100
-
101
- #define LM_GGML_FP32_TO_BF16(x) lm_ggml_compute_fp32_to_bf16(x)
102
- #define LM_GGML_BF16_TO_FP32(x) lm_ggml_compute_bf16_to_fp32(x)
103
-
104
42
  // __FMA__ and __F16C__ are not defined in MSVC, however they are implied with AVX2/AVX512
105
43
  #if defined(_MSC_VER) && (defined(__AVX2__) || defined(__AVX512F__))
106
44
  #ifndef __FMA__
@@ -388,28 +326,6 @@ inline static int32x4_t lm_ggml_vdotq_s32(int32x4_t acc, int8x16_t a, int8x16_t
388
326
 
389
327
  #endif // defined(__ARM_NEON)
390
328
 
391
- #if defined(__ARM_NEON) && !defined(_MSC_VER)
392
-
393
- #define LM_GGML_COMPUTE_FP16_TO_FP32(x) lm_ggml_compute_fp16_to_fp32(x)
394
- #define LM_GGML_COMPUTE_FP32_TO_FP16(x) lm_ggml_compute_fp32_to_fp16(x)
395
-
396
- #define LM_GGML_FP16_TO_FP32(x) lm_ggml_compute_fp16_to_fp32(x)
397
-
398
- static inline float lm_ggml_compute_fp16_to_fp32(lm_ggml_fp16_t h) {
399
- lm_ggml_fp16_internal_t tmp;
400
- memcpy(&tmp, &h, sizeof(lm_ggml_fp16_t));
401
- return (float)tmp;
402
- }
403
-
404
- static inline lm_ggml_fp16_t lm_ggml_compute_fp32_to_fp16(float f) {
405
- lm_ggml_fp16_t res;
406
- lm_ggml_fp16_internal_t tmp = f;
407
- memcpy(&res, &tmp, sizeof(lm_ggml_fp16_t));
408
- return res;
409
- }
410
-
411
- #else
412
-
413
329
  #ifdef __wasm_simd128__
414
330
  #include <wasm_simd128.h>
415
331
  #else
@@ -462,152 +378,8 @@ static __m256 __lasx_xvreplfr2vr_s(float val) {
462
378
  }
463
379
  #endif
464
380
 
465
- #ifdef __F16C__
466
-
467
- #ifdef _MSC_VER
468
- #define LM_GGML_COMPUTE_FP16_TO_FP32(x) _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128(x)))
469
- #define LM_GGML_COMPUTE_FP32_TO_FP16(x) _mm_extract_epi16(_mm_cvtps_ph(_mm_set_ss(x), 0), 0)
470
- #else
471
- #define LM_GGML_COMPUTE_FP16_TO_FP32(x) _cvtsh_ss(x)
472
- #define LM_GGML_COMPUTE_FP32_TO_FP16(x) _cvtss_sh(x, 0)
473
- #endif
474
-
475
- #elif defined(__POWER9_VECTOR__)
476
-
477
- #define LM_GGML_COMPUTE_FP16_TO_FP32(x) lm_ggml_compute_fp16_to_fp32(x)
478
- #define LM_GGML_COMPUTE_FP32_TO_FP16(x) lm_ggml_compute_fp32_to_fp16(x)
479
- /* the inline asm below is about 12% faster than the lookup method */
480
- #define LM_GGML_FP16_TO_FP32(x) LM_GGML_COMPUTE_FP16_TO_FP32(x)
481
- #define LM_GGML_FP32_TO_FP16(x) LM_GGML_COMPUTE_FP32_TO_FP16(x)
482
-
483
- static inline float lm_ggml_compute_fp16_to_fp32(lm_ggml_fp16_t h) {
484
- register float f;
485
- register double d;
486
- __asm__(
487
- "mtfprd %0,%2\n"
488
- "xscvhpdp %0,%0\n"
489
- "frsp %1,%0\n" :
490
- /* temp */ "=d"(d),
491
- /* out */ "=f"(f):
492
- /* in */ "r"(h));
493
- return f;
494
- }
495
-
496
- static inline lm_ggml_fp16_t lm_ggml_compute_fp32_to_fp16(float f) {
497
- register double d;
498
- register lm_ggml_fp16_t r;
499
- __asm__( /* xscvdphp can work on double or single precision */
500
- "xscvdphp %0,%2\n"
501
- "mffprd %1,%0\n" :
502
- /* temp */ "=d"(d),
503
- /* out */ "=r"(r):
504
- /* in */ "f"(f));
505
- return r;
506
- }
507
-
508
- #else
509
-
510
- // FP16 <-> FP32
511
- // ref: https://github.com/Maratyszcza/FP16
512
-
513
- static inline float fp32_from_bits(uint32_t w) {
514
- union {
515
- uint32_t as_bits;
516
- float as_value;
517
- } fp32;
518
- fp32.as_bits = w;
519
- return fp32.as_value;
520
- }
521
-
522
- static inline uint32_t fp32_to_bits(float f) {
523
- union {
524
- float as_value;
525
- uint32_t as_bits;
526
- } fp32;
527
- fp32.as_value = f;
528
- return fp32.as_bits;
529
- }
530
-
531
- static inline float lm_ggml_compute_fp16_to_fp32(lm_ggml_fp16_t h) {
532
- const uint32_t w = (uint32_t) h << 16;
533
- const uint32_t sign = w & UINT32_C(0x80000000);
534
- const uint32_t two_w = w + w;
535
-
536
- const uint32_t exp_offset = UINT32_C(0xE0) << 23;
537
- #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
538
- const float exp_scale = 0x1.0p-112f;
539
- #else
540
- const float exp_scale = fp32_from_bits(UINT32_C(0x7800000));
541
- #endif
542
- const float normalized_value = fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale;
543
-
544
- const uint32_t magic_mask = UINT32_C(126) << 23;
545
- const float magic_bias = 0.5f;
546
- const float denormalized_value = fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;
547
-
548
- const uint32_t denormalized_cutoff = UINT32_C(1) << 27;
549
- const uint32_t result = sign |
550
- (two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value) : fp32_to_bits(normalized_value));
551
- return fp32_from_bits(result);
552
- }
553
-
554
- static inline lm_ggml_fp16_t lm_ggml_compute_fp32_to_fp16(float f) {
555
- #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
556
- const float scale_to_inf = 0x1.0p+112f;
557
- const float scale_to_zero = 0x1.0p-110f;
558
- #else
559
- const float scale_to_inf = fp32_from_bits(UINT32_C(0x77800000));
560
- const float scale_to_zero = fp32_from_bits(UINT32_C(0x08800000));
561
- #endif
562
- float base = (fabsf(f) * scale_to_inf) * scale_to_zero;
563
-
564
- const uint32_t w = fp32_to_bits(f);
565
- const uint32_t shl1_w = w + w;
566
- const uint32_t sign = w & UINT32_C(0x80000000);
567
- uint32_t bias = shl1_w & UINT32_C(0xFF000000);
568
- if (bias < UINT32_C(0x71000000)) {
569
- bias = UINT32_C(0x71000000);
570
- }
571
-
572
- base = fp32_from_bits((bias >> 1) + UINT32_C(0x07800000)) + base;
573
- const uint32_t bits = fp32_to_bits(base);
574
- const uint32_t exp_bits = (bits >> 13) & UINT32_C(0x00007C00);
575
- const uint32_t mantissa_bits = bits & UINT32_C(0x00000FFF);
576
- const uint32_t nonsign = exp_bits + mantissa_bits;
577
- return (sign >> 16) | (shl1_w > UINT32_C(0xFF000000) ? UINT16_C(0x7E00) : nonsign);
578
- }
579
-
580
- #define LM_GGML_COMPUTE_FP16_TO_FP32(x) lm_ggml_compute_fp16_to_fp32(x)
581
- #define LM_GGML_COMPUTE_FP32_TO_FP16(x) lm_ggml_compute_fp32_to_fp16(x)
582
-
583
- #endif // __F16C__
584
-
585
- #endif // defined(__ARM_NEON) && (!defined(__MSC_VER)
586
-
587
- #ifdef __ARM_FEATURE_SVE
588
- #include <arm_sve.h>
589
- #endif // __ARM_FEATURE_SVE
590
-
591
- // precomputed f32 table for f16 (256 KB)
592
- // defined in ggml.c, initialized in lm_ggml_init()
593
- extern float lm_ggml_table_f32_f16[1 << 16];
594
-
595
- // On ARM NEON, it's quicker to directly convert x -> x instead of calling into lm_ggml_lookup_fp16_to_fp32,
596
- // so we define LM_GGML_FP16_TO_FP32 and LM_GGML_FP32_TO_FP16 elsewhere for NEON.
597
- // This is also true for POWER9.
598
- #if !defined(LM_GGML_FP16_TO_FP32)
599
- inline static float lm_ggml_lookup_fp16_to_fp32(lm_ggml_fp16_t f) {
600
- uint16_t s;
601
- memcpy(&s, &f, sizeof(uint16_t));
602
- return lm_ggml_table_f32_f16[s];
603
- }
604
-
605
- #define LM_GGML_FP16_TO_FP32(x) lm_ggml_lookup_fp16_to_fp32(x)
606
- #endif
607
-
608
- #if !defined(LM_GGML_FP32_TO_FP16)
609
- #define LM_GGML_FP32_TO_FP16(x) LM_GGML_COMPUTE_FP32_TO_FP16(x)
610
- #endif
381
+ // TODO: move to ggml-threading
382
+ void lm_ggml_barrier(struct lm_ggml_threadpool * tp);
611
383
 
612
384
  #ifdef __cplusplus
613
385
  }