cui-llama.rn 1.2.4 → 1.3.0

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 (70) hide show
  1. package/README.md +3 -4
  2. package/android/src/main/CMakeLists.txt +21 -5
  3. package/android/src/main/java/com/rnllama/LlamaContext.java +115 -30
  4. package/android/src/main/java/com/rnllama/RNLlama.java +40 -7
  5. package/android/src/main/jni.cpp +222 -36
  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/common.cpp +1682 -2122
  9. package/cpp/common.h +600 -594
  10. package/cpp/ggml-aarch64.c +129 -3209
  11. package/cpp/ggml-aarch64.h +19 -39
  12. package/cpp/ggml-alloc.c +1040 -1040
  13. package/cpp/ggml-alloc.h +76 -76
  14. package/cpp/ggml-backend-impl.h +216 -227
  15. package/cpp/ggml-backend-reg.cpp +195 -0
  16. package/cpp/ggml-backend.cpp +1997 -2625
  17. package/cpp/ggml-backend.h +328 -326
  18. package/cpp/ggml-common.h +1853 -1853
  19. package/cpp/ggml-cpp.h +38 -0
  20. package/cpp/ggml-cpu-aarch64.c +3560 -0
  21. package/cpp/ggml-cpu-aarch64.h +30 -0
  22. package/cpp/ggml-cpu-impl.h +371 -614
  23. package/cpp/ggml-cpu-quants.c +10822 -0
  24. package/cpp/ggml-cpu-quants.h +63 -0
  25. package/cpp/ggml-cpu.c +13975 -0
  26. package/cpp/ggml-cpu.cpp +663 -0
  27. package/cpp/ggml-cpu.h +177 -0
  28. package/cpp/ggml-impl.h +550 -209
  29. package/cpp/ggml-metal.h +66 -66
  30. package/cpp/ggml-metal.m +4294 -3819
  31. package/cpp/ggml-quants.c +5247 -15752
  32. package/cpp/ggml-quants.h +100 -147
  33. package/cpp/ggml-threading.cpp +12 -0
  34. package/cpp/ggml-threading.h +12 -0
  35. package/cpp/ggml.c +8180 -23464
  36. package/cpp/ggml.h +2411 -2562
  37. package/cpp/llama-grammar.cpp +1138 -1138
  38. package/cpp/llama-grammar.h +144 -144
  39. package/cpp/llama-impl.h +181 -181
  40. package/cpp/llama-sampling.cpp +2348 -2194
  41. package/cpp/llama-sampling.h +48 -30
  42. package/cpp/llama-vocab.cpp +1984 -1968
  43. package/cpp/llama-vocab.h +170 -165
  44. package/cpp/llama.cpp +22132 -21969
  45. package/cpp/llama.h +1253 -1253
  46. package/cpp/log.cpp +401 -401
  47. package/cpp/log.h +121 -121
  48. package/cpp/rn-llama.hpp +83 -19
  49. package/cpp/sampling.cpp +466 -458
  50. package/cpp/sgemm.cpp +1884 -1219
  51. package/ios/RNLlama.mm +43 -20
  52. package/ios/RNLlamaContext.h +9 -3
  53. package/ios/RNLlamaContext.mm +133 -33
  54. package/jest/mock.js +0 -1
  55. package/lib/commonjs/NativeRNLlama.js.map +1 -1
  56. package/lib/commonjs/index.js +52 -15
  57. package/lib/commonjs/index.js.map +1 -1
  58. package/lib/module/NativeRNLlama.js.map +1 -1
  59. package/lib/module/index.js +51 -15
  60. package/lib/module/index.js.map +1 -1
  61. package/lib/typescript/NativeRNLlama.d.ts +29 -6
  62. package/lib/typescript/NativeRNLlama.d.ts.map +1 -1
  63. package/lib/typescript/index.d.ts +12 -5
  64. package/lib/typescript/index.d.ts.map +1 -1
  65. package/package.json +1 -1
  66. package/src/NativeRNLlama.ts +41 -7
  67. package/src/index.ts +82 -27
  68. package/cpp/json-schema-to-grammar.cpp +0 -1045
  69. package/cpp/json-schema-to-grammar.h +0 -8
  70. package/cpp/json.hpp +0 -24766
package/cpp/ggml-impl.h CHANGED
@@ -1,209 +1,550 @@
1
- #pragma once
2
-
3
- // GGML internal header
4
-
5
- #include "ggml.h"
6
-
7
- #include <assert.h>
8
- #include <stdlib.h> // load `stdlib.h` before other headers to work around MinGW bug: https://sourceforge.net/p/mingw-w64/bugs/192/
9
- #include <stdbool.h>
10
- #include <stdint.h>
11
-
12
- #ifdef __cplusplus
13
- extern "C" {
14
- #endif
15
-
16
- #undef MIN
17
- #undef MAX
18
-
19
- #define MIN(a, b) ((a) < (b) ? (a) : (b))
20
- #define MAX(a, b) ((a) > (b) ? (a) : (b))
21
-
22
- // required for mmap as gguf only guarantees 32-byte alignment
23
- #define TENSOR_ALIGNMENT 32
24
-
25
- // static_assert should be a #define, but if it's not,
26
- // fall back to the _Static_assert C11 keyword.
27
- // if C99 - static_assert is noop
28
- // ref: https://stackoverflow.com/a/53923785/4039976
29
- #ifndef __cplusplus
30
- #ifndef static_assert
31
- #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201100L)
32
- #define static_assert(cond, msg) _Static_assert(cond, msg)
33
- #else
34
- #define static_assert(cond, msg) struct global_scope_noop_trick
35
- #endif
36
- #endif
37
- #endif
38
-
39
- //
40
- // logging
41
- //
42
-
43
- LM_GGML_ATTRIBUTE_FORMAT(2, 3)
44
- void lm_ggml_log_internal (enum lm_ggml_log_level level, const char * format, ...);
45
- void lm_ggml_log_callback_default(enum lm_ggml_log_level level, const char * text, void * user_data);
46
-
47
- #define LM_GGML_LOG(...) lm_ggml_log_internal(LM_GGML_LOG_LEVEL_NONE , __VA_ARGS__)
48
- #define LM_GGML_LOG_INFO(...) lm_ggml_log_internal(LM_GGML_LOG_LEVEL_INFO , __VA_ARGS__)
49
- #define LM_GGML_LOG_WARN(...) lm_ggml_log_internal(LM_GGML_LOG_LEVEL_WARN , __VA_ARGS__)
50
- #define LM_GGML_LOG_ERROR(...) lm_ggml_log_internal(LM_GGML_LOG_LEVEL_ERROR, __VA_ARGS__)
51
- #define LM_GGML_LOG_DEBUG(...) lm_ggml_log_internal(LM_GGML_LOG_LEVEL_DEBUG, __VA_ARGS__)
52
- #define LM_GGML_LOG_CONT(...) lm_ggml_log_internal(LM_GGML_LOG_LEVEL_CONT , __VA_ARGS__)
53
-
54
- // bitset
55
-
56
- typedef uint32_t lm_ggml_bitset_t;
57
-
58
- static_assert(sizeof(lm_ggml_bitset_t) == 4, "bitset_t constants must be updated");
59
- #define BITSET_SHR 5 // log2(sizeof(lm_ggml_bitset_t)*8)
60
- #define BITSET_MASK (sizeof(lm_ggml_bitset_t)*8 - 1)
61
-
62
- static size_t lm_ggml_bitset_size(size_t n) {
63
- return (n + BITSET_MASK) >> BITSET_SHR;
64
- }
65
-
66
- static inline bool lm_ggml_bitset_get(const lm_ggml_bitset_t * bitset, size_t i) {
67
- return !!(bitset[i >> BITSET_SHR] & (1u << (i & BITSET_MASK)));
68
- }
69
-
70
- static inline void lm_ggml_bitset_set(lm_ggml_bitset_t * bitset, size_t i) {
71
- bitset[i >> BITSET_SHR] |= (1u << (i & BITSET_MASK));
72
- }
73
-
74
- static inline void lm_ggml_bitset_clear(lm_ggml_bitset_t * bitset, size_t i) {
75
- bitset[i >> BITSET_SHR] &= ~(1u << (i & BITSET_MASK));
76
- }
77
-
78
- // hash set
79
-
80
- #define LM_GGML_HASHSET_FULL ((size_t)-1)
81
- #define LM_GGML_HASHSET_ALREADY_EXISTS ((size_t)-2)
82
-
83
- struct lm_ggml_hash_set {
84
- size_t size;
85
- lm_ggml_bitset_t * used; // whether or not the keys are in use i.e. set
86
- struct lm_ggml_tensor ** keys; // actual tensors in the set, keys[i] is only defined if lm_ggml_bitset_get(used, i)
87
- };
88
-
89
- struct lm_ggml_hash_set lm_ggml_hash_set_new(size_t size);
90
- void lm_ggml_hash_set_free(struct lm_ggml_hash_set * hash_set);
91
-
92
- // returns the minimum size for a hash set that can hold min_sz elements
93
- size_t lm_ggml_hash_size(size_t min_sz);
94
-
95
- // remove all elements from the hash set
96
- void lm_ggml_hash_set_reset(struct lm_ggml_hash_set * hash_set);
97
-
98
- // returns true if key is in the hash set
99
- static bool lm_ggml_hash_contains(const struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key);
100
-
101
- // returns LM_GGML_HASHSET_FULL if table is full, otherwise the current index of the key or where it should be inserted
102
- static size_t lm_ggml_hash_find(const struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key);
103
-
104
- // returns LM_GGML_HASHSET_ALREADY_EXISTS if key already exists, index otherwise, asserts if table is full
105
- static size_t lm_ggml_hash_insert(struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key);
106
-
107
- // return index, asserts if table is full
108
- static size_t lm_ggml_hash_find_or_insert(struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key);
109
-
110
- // hash function for lm_ggml_tensor
111
- static inline size_t lm_ggml_hash(const struct lm_ggml_tensor * p) {
112
- // the last 4 bits are always zero due to alignment
113
- return (size_t)(uintptr_t)p >> 4;
114
- }
115
-
116
- static size_t lm_ggml_hash_find(const struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key) {
117
- size_t h = lm_ggml_hash(key) % hash_set->size;
118
-
119
- // linear probing
120
- size_t i = h;
121
- while (lm_ggml_bitset_get(hash_set->used, i) && hash_set->keys[i] != key) {
122
- i = (i + 1) % hash_set->size;
123
- if (i == h) {
124
- // visited all hash table entries -> not found
125
- return LM_GGML_HASHSET_FULL;
126
- }
127
- }
128
- return i;
129
- }
130
-
131
- static bool lm_ggml_hash_contains(const struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key) {
132
- size_t i = lm_ggml_hash_find(hash_set, key);
133
- return i != LM_GGML_HASHSET_FULL && lm_ggml_bitset_get(hash_set->used, i);
134
- }
135
-
136
- static size_t lm_ggml_hash_insert(struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key) {
137
- size_t h = lm_ggml_hash(key) % hash_set->size;
138
-
139
- // linear probing
140
- size_t i = h;
141
- do {
142
- if (!lm_ggml_bitset_get(hash_set->used, i)) {
143
- lm_ggml_bitset_set(hash_set->used, i);
144
- hash_set->keys[i] = key;
145
- return i;
146
- }
147
- if (hash_set->keys[i] == key) {
148
- return LM_GGML_HASHSET_ALREADY_EXISTS;
149
- }
150
- i = (i + 1) % hash_set->size;
151
- } while (i != h);
152
-
153
- // visited all hash table entries -> not found
154
- LM_GGML_ABORT("fatal error");
155
- }
156
-
157
- static size_t lm_ggml_hash_find_or_insert(struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key) {
158
- size_t h = lm_ggml_hash(key) % hash_set->size;
159
-
160
- // linear probing
161
- size_t i = h;
162
- do {
163
- if (!lm_ggml_bitset_get(hash_set->used, i)) {
164
- lm_ggml_bitset_set(hash_set->used, i);
165
- hash_set->keys[i] = key;
166
- return i;
167
- }
168
- if (hash_set->keys[i] == key) {
169
- return i;
170
- }
171
- i = (i + 1) % hash_set->size;
172
- } while (i != h);
173
-
174
- // visited all hash table entries -> not found
175
- LM_GGML_ABORT("fatal error");
176
- }
177
-
178
- // computation graph
179
-
180
- enum lm_ggml_cgraph_eval_order {
181
- LM_GGML_CGRAPH_EVAL_ORDER_LEFT_TO_RIGHT = 0,
182
- LM_GGML_CGRAPH_EVAL_ORDER_RIGHT_TO_LEFT,
183
- LM_GGML_CGRAPH_EVAL_ORDER_COUNT
184
- };
185
-
186
- struct lm_ggml_cgraph {
187
- int size;
188
- int n_nodes;
189
- int n_leafs;
190
-
191
- struct lm_ggml_tensor ** nodes;
192
- struct lm_ggml_tensor ** grads;
193
- struct lm_ggml_tensor ** leafs;
194
-
195
- struct lm_ggml_hash_set visited_hash_set;
196
-
197
- enum lm_ggml_cgraph_eval_order order;
198
- };
199
-
200
- struct lm_ggml_cgraph lm_ggml_graph_view(struct lm_ggml_cgraph * cgraph, int i0, int i1);
201
-
202
- // Memory allocation
203
-
204
- void * lm_ggml_aligned_malloc(size_t size);
205
- void lm_ggml_aligned_free(void * ptr, size_t size);
206
-
207
- #ifdef __cplusplus
208
- }
209
- #endif
1
+ #pragma once
2
+
3
+ // GGML internal header
4
+
5
+ #include "ggml.h"
6
+ #include <assert.h>
7
+ #include <math.h>
8
+ #include <stdlib.h> // load `stdlib.h` before other headers to work around MinGW bug: https://sourceforge.net/p/mingw-w64/bugs/192/
9
+ #include <stdbool.h>
10
+ #include <stdint.h>
11
+ #include <string.h>
12
+
13
+ #ifdef __ARM_FEATURE_SVE
14
+ #include <arm_sve.h>
15
+ #endif // __ARM_FEATURE_SVE
16
+
17
+ #if defined(__ARM_NEON)
18
+ // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
19
+ //
20
+ // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
21
+ //
22
+ #include <arm_neon.h>
23
+ #endif
24
+
25
+ #if defined(__F16C__)
26
+ #include <immintrin.h>
27
+ #endif
28
+
29
+ #ifdef __cplusplus
30
+ extern "C" {
31
+ #endif
32
+
33
+ #undef MIN
34
+ #undef MAX
35
+
36
+ #define MIN(a, b) ((a) < (b) ? (a) : (b))
37
+ #define MAX(a, b) ((a) > (b) ? (a) : (b))
38
+
39
+ // required for mmap as gguf only guarantees 32-byte alignment
40
+ #define TENSOR_ALIGNMENT 32
41
+
42
+ // static_assert should be a #define, but if it's not,
43
+ // fall back to the _Static_assert C11 keyword.
44
+ // if C99 - static_assert is noop
45
+ // ref: https://stackoverflow.com/a/53923785/4039976
46
+ #ifndef __cplusplus
47
+ #ifndef static_assert
48
+ #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201100L)
49
+ #define static_assert(cond, msg) _Static_assert(cond, msg)
50
+ #else
51
+ #define static_assert(cond, msg) struct global_scope_noop_trick
52
+ #endif
53
+ #endif
54
+ #endif
55
+
56
+ static inline int lm_ggml_up32(int n) {
57
+ return (n + 31) & ~31;
58
+ }
59
+
60
+ //static inline int lm_ggml_up64(int n) {
61
+ // return (n + 63) & ~63;
62
+ //}
63
+
64
+ static inline int lm_ggml_up(int n, int m) {
65
+ // assert m is a power of 2
66
+ LM_GGML_ASSERT((m & (m - 1)) == 0);
67
+ return (n + m - 1) & ~(m - 1);
68
+ }
69
+
70
+ //
71
+ // logging
72
+ //
73
+
74
+ LM_GGML_ATTRIBUTE_FORMAT(2, 3)
75
+ void lm_ggml_log_internal (enum lm_ggml_log_level level, const char * format, ...);
76
+ void lm_ggml_log_callback_default(enum lm_ggml_log_level level, const char * text, void * user_data);
77
+
78
+ #define LM_GGML_LOG(...) lm_ggml_log_internal(LM_GGML_LOG_LEVEL_NONE , __VA_ARGS__)
79
+ #define LM_GGML_LOG_INFO(...) lm_ggml_log_internal(LM_GGML_LOG_LEVEL_INFO , __VA_ARGS__)
80
+ #define LM_GGML_LOG_WARN(...) lm_ggml_log_internal(LM_GGML_LOG_LEVEL_WARN , __VA_ARGS__)
81
+ #define LM_GGML_LOG_ERROR(...) lm_ggml_log_internal(LM_GGML_LOG_LEVEL_ERROR, __VA_ARGS__)
82
+ #define LM_GGML_LOG_DEBUG(...) lm_ggml_log_internal(LM_GGML_LOG_LEVEL_DEBUG, __VA_ARGS__)
83
+ #define LM_GGML_LOG_CONT(...) lm_ggml_log_internal(LM_GGML_LOG_LEVEL_CONT , __VA_ARGS__)
84
+
85
+ #define LM_GGML_DEBUG 0
86
+
87
+ #if (LM_GGML_DEBUG >= 1)
88
+ #define LM_GGML_PRINT_DEBUG(...) LM_GGML_LOG_DEBUG(__VA_ARGS__)
89
+ #else
90
+ #define LM_GGML_PRINT_DEBUG(...)
91
+ #endif
92
+
93
+ #if (LM_GGML_DEBUG >= 5)
94
+ #define LM_GGML_PRINT_DEBUG_5(...) LM_GGML_LOG_DEBUG(__VA_ARGS__)
95
+ #else
96
+ #define LM_GGML_PRINT_DEBUG_5(...)
97
+ #endif
98
+
99
+ #if (LM_GGML_DEBUG >= 10)
100
+ #define LM_GGML_PRINT_DEBUG_10(...) LM_GGML_LOG_DEBUG(__VA_ARGS__)
101
+ #else
102
+ #define LM_GGML_PRINT_DEBUG_10(...)
103
+ #endif
104
+
105
+ // tensor params
106
+
107
+ static void lm_ggml_set_op_params(struct lm_ggml_tensor * tensor, const void * params, size_t params_size) {
108
+ LM_GGML_ASSERT(tensor != NULL); // silence -Warray-bounds warnings
109
+ assert(params_size <= LM_GGML_MAX_OP_PARAMS);
110
+ memcpy(tensor->op_params, params, params_size);
111
+ }
112
+
113
+ static int32_t lm_ggml_get_op_params_i32(const struct lm_ggml_tensor * tensor, uint32_t i) {
114
+ assert(i < LM_GGML_MAX_OP_PARAMS / sizeof(int32_t));
115
+ return ((const int32_t *)(tensor->op_params))[i];
116
+ }
117
+
118
+ static float lm_ggml_get_op_params_f32(const struct lm_ggml_tensor * tensor, uint32_t i) {
119
+ assert(i < LM_GGML_MAX_OP_PARAMS / sizeof(float));
120
+ return ((const float *)(tensor->op_params))[i];
121
+ }
122
+
123
+ static void lm_ggml_set_op_params_i32(struct lm_ggml_tensor * tensor, uint32_t i, int32_t value) {
124
+ assert(i < LM_GGML_MAX_OP_PARAMS / sizeof(int32_t));
125
+ ((int32_t *)(tensor->op_params))[i] = value;
126
+ }
127
+
128
+ static void lm_ggml_set_op_params_f32(struct lm_ggml_tensor * tensor, uint32_t i, float value) {
129
+ assert(i < LM_GGML_MAX_OP_PARAMS / sizeof(float));
130
+ ((float *)(tensor->op_params))[i] = value;
131
+ }
132
+
133
+ struct lm_ggml_map_custom1_op_params {
134
+ lm_ggml_custom1_op_t fun;
135
+ int n_tasks;
136
+ void * userdata;
137
+ };
138
+
139
+ struct lm_ggml_map_custom2_op_params {
140
+ lm_ggml_custom2_op_t fun;
141
+ int n_tasks;
142
+ void * userdata;
143
+ };
144
+
145
+ struct lm_ggml_map_custom3_op_params {
146
+ lm_ggml_custom3_op_t fun;
147
+ int n_tasks;
148
+ void * userdata;
149
+ };
150
+
151
+ // bitset
152
+
153
+ typedef uint32_t lm_ggml_bitset_t;
154
+
155
+ static_assert(sizeof(lm_ggml_bitset_t) == 4, "bitset_t constants must be updated");
156
+ #define BITSET_SHR 5 // log2(sizeof(lm_ggml_bitset_t)*8)
157
+ #define BITSET_MASK (sizeof(lm_ggml_bitset_t)*8 - 1)
158
+
159
+ static size_t lm_ggml_bitset_size(size_t n) {
160
+ return (n + BITSET_MASK) >> BITSET_SHR;
161
+ }
162
+
163
+ static inline bool lm_ggml_bitset_get(const lm_ggml_bitset_t * bitset, size_t i) {
164
+ return !!(bitset[i >> BITSET_SHR] & (1u << (i & BITSET_MASK)));
165
+ }
166
+
167
+ static inline void lm_ggml_bitset_set(lm_ggml_bitset_t * bitset, size_t i) {
168
+ bitset[i >> BITSET_SHR] |= (1u << (i & BITSET_MASK));
169
+ }
170
+
171
+ static inline void lm_ggml_bitset_clear(lm_ggml_bitset_t * bitset, size_t i) {
172
+ bitset[i >> BITSET_SHR] &= ~(1u << (i & BITSET_MASK));
173
+ }
174
+
175
+ // hash set
176
+
177
+ #define LM_GGML_HASHSET_FULL ((size_t)-1)
178
+ #define LM_GGML_HASHSET_ALREADY_EXISTS ((size_t)-2)
179
+
180
+ struct lm_ggml_hash_set {
181
+ size_t size;
182
+ lm_ggml_bitset_t * used; // whether or not the keys are in use i.e. set
183
+ struct lm_ggml_tensor ** keys; // actual tensors in the set, keys[i] is only defined if lm_ggml_bitset_get(used, i)
184
+ };
185
+
186
+ struct lm_ggml_hash_set lm_ggml_hash_set_new(size_t size);
187
+ void lm_ggml_hash_set_free(struct lm_ggml_hash_set * hash_set);
188
+
189
+ // returns the minimum size for a hash set that can hold min_sz elements
190
+ size_t lm_ggml_hash_size(size_t min_sz);
191
+
192
+ // remove all elements from the hash set
193
+ void lm_ggml_hash_set_reset(struct lm_ggml_hash_set * hash_set);
194
+
195
+ // returns true if key is in the hash set
196
+ static bool lm_ggml_hash_contains(const struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key);
197
+
198
+ // returns LM_GGML_HASHSET_FULL if table is full, otherwise the current index of the key or where it should be inserted
199
+ static size_t lm_ggml_hash_find(const struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key);
200
+
201
+ // returns LM_GGML_HASHSET_ALREADY_EXISTS if key already exists, index otherwise, asserts if table is full
202
+ static size_t lm_ggml_hash_insert(struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key);
203
+
204
+ // return index, asserts if table is full
205
+ static size_t lm_ggml_hash_find_or_insert(struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key);
206
+
207
+ // hash function for lm_ggml_tensor
208
+ static inline size_t lm_ggml_hash(const struct lm_ggml_tensor * p) {
209
+ // the last 4 bits are always zero due to alignment
210
+ return (size_t)(uintptr_t)p >> 4;
211
+ }
212
+
213
+ static size_t lm_ggml_hash_find(const struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key) {
214
+ size_t h = lm_ggml_hash(key) % hash_set->size;
215
+
216
+ // linear probing
217
+ size_t i = h;
218
+ while (lm_ggml_bitset_get(hash_set->used, i) && hash_set->keys[i] != key) {
219
+ i = (i + 1) % hash_set->size;
220
+ if (i == h) {
221
+ // visited all hash table entries -> not found
222
+ return LM_GGML_HASHSET_FULL;
223
+ }
224
+ }
225
+ return i;
226
+ }
227
+
228
+ static bool lm_ggml_hash_contains(const struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key) {
229
+ size_t i = lm_ggml_hash_find(hash_set, key);
230
+ return i != LM_GGML_HASHSET_FULL && lm_ggml_bitset_get(hash_set->used, i);
231
+ }
232
+
233
+ static size_t lm_ggml_hash_insert(struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key) {
234
+ size_t h = lm_ggml_hash(key) % hash_set->size;
235
+
236
+ // linear probing
237
+ size_t i = h;
238
+ do {
239
+ if (!lm_ggml_bitset_get(hash_set->used, i)) {
240
+ lm_ggml_bitset_set(hash_set->used, i);
241
+ hash_set->keys[i] = key;
242
+ return i;
243
+ }
244
+ if (hash_set->keys[i] == key) {
245
+ return LM_GGML_HASHSET_ALREADY_EXISTS;
246
+ }
247
+ i = (i + 1) % hash_set->size;
248
+ } while (i != h);
249
+
250
+ // visited all hash table entries -> not found
251
+ LM_GGML_ABORT("fatal error");
252
+ }
253
+
254
+ static size_t lm_ggml_hash_find_or_insert(struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key) {
255
+ size_t h = lm_ggml_hash(key) % hash_set->size;
256
+
257
+ // linear probing
258
+ size_t i = h;
259
+ do {
260
+ if (!lm_ggml_bitset_get(hash_set->used, i)) {
261
+ lm_ggml_bitset_set(hash_set->used, i);
262
+ hash_set->keys[i] = key;
263
+ return i;
264
+ }
265
+ if (hash_set->keys[i] == key) {
266
+ return i;
267
+ }
268
+ i = (i + 1) % hash_set->size;
269
+ } while (i != h);
270
+
271
+ // visited all hash table entries -> not found
272
+ LM_GGML_ABORT("fatal error");
273
+ }
274
+
275
+ // computation graph
276
+
277
+ enum lm_ggml_cgraph_eval_order {
278
+ LM_GGML_CGRAPH_EVAL_ORDER_LEFT_TO_RIGHT = 0,
279
+ LM_GGML_CGRAPH_EVAL_ORDER_RIGHT_TO_LEFT,
280
+ LM_GGML_CGRAPH_EVAL_ORDER_COUNT
281
+ };
282
+
283
+ struct lm_ggml_cgraph {
284
+ int size;
285
+ int n_nodes;
286
+ int n_leafs;
287
+
288
+ struct lm_ggml_tensor ** nodes;
289
+ struct lm_ggml_tensor ** grads;
290
+ struct lm_ggml_tensor ** leafs;
291
+
292
+ struct lm_ggml_hash_set visited_hash_set;
293
+
294
+ enum lm_ggml_cgraph_eval_order order;
295
+ };
296
+
297
+ struct lm_ggml_cgraph lm_ggml_graph_view(struct lm_ggml_cgraph * cgraph, int i0, int i1);
298
+
299
+ // Memory allocation
300
+
301
+ void * lm_ggml_aligned_malloc(size_t size);
302
+ void lm_ggml_aligned_free(void * ptr, size_t size);
303
+
304
+ // FP16 to FP32 conversion
305
+
306
+ #if defined(__ARM_NEON)
307
+ #ifdef _MSC_VER
308
+ typedef uint16_t lm_ggml_fp16_internal_t;
309
+ #else
310
+ typedef __fp16 lm_ggml_fp16_internal_t;
311
+ #endif
312
+ #endif
313
+
314
+ #if defined(__ARM_NEON) && !defined(_MSC_VER)
315
+ #define LM_GGML_COMPUTE_FP16_TO_FP32(x) lm_ggml_compute_fp16_to_fp32(x)
316
+ #define LM_GGML_COMPUTE_FP32_TO_FP16(x) lm_ggml_compute_fp32_to_fp16(x)
317
+
318
+ #define LM_GGML_FP16_TO_FP32(x) lm_ggml_compute_fp16_to_fp32(x)
319
+
320
+ static inline float lm_ggml_compute_fp16_to_fp32(lm_ggml_fp16_t h) {
321
+ lm_ggml_fp16_internal_t tmp;
322
+ memcpy(&tmp, &h, sizeof(lm_ggml_fp16_t));
323
+ return (float)tmp;
324
+ }
325
+
326
+ static inline lm_ggml_fp16_t lm_ggml_compute_fp32_to_fp16(float f) {
327
+ lm_ggml_fp16_t res;
328
+ lm_ggml_fp16_internal_t tmp = f;
329
+ memcpy(&res, &tmp, sizeof(lm_ggml_fp16_t));
330
+ return res;
331
+ }
332
+
333
+ #elif defined(__F16C__)
334
+
335
+ #ifdef _MSC_VER
336
+ #define LM_GGML_COMPUTE_FP16_TO_FP32(x) _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128(x)))
337
+ #define LM_GGML_COMPUTE_FP32_TO_FP16(x) _mm_extract_epi16(_mm_cvtps_ph(_mm_set_ss(x), 0), 0)
338
+ #else
339
+ #define LM_GGML_COMPUTE_FP16_TO_FP32(x) _cvtsh_ss(x)
340
+ #define LM_GGML_COMPUTE_FP32_TO_FP16(x) _cvtss_sh(x, 0)
341
+ #endif
342
+
343
+ #elif defined(__POWER9_VECTOR__)
344
+
345
+ #define LM_GGML_COMPUTE_FP16_TO_FP32(x) lm_ggml_compute_fp16_to_fp32(x)
346
+ #define LM_GGML_COMPUTE_FP32_TO_FP16(x) lm_ggml_compute_fp32_to_fp16(x)
347
+ /* the inline asm below is about 12% faster than the lookup method */
348
+ #define LM_GGML_FP16_TO_FP32(x) LM_GGML_COMPUTE_FP16_TO_FP32(x)
349
+ #define LM_GGML_FP32_TO_FP16(x) LM_GGML_COMPUTE_FP32_TO_FP16(x)
350
+
351
+ static inline float lm_ggml_compute_fp16_to_fp32(lm_ggml_fp16_t h) {
352
+ register float f;
353
+ register double d;
354
+ __asm__(
355
+ "mtfprd %0,%2\n"
356
+ "xscvhpdp %0,%0\n"
357
+ "frsp %1,%0\n" :
358
+ /* temp */ "=d"(d),
359
+ /* out */ "=f"(f):
360
+ /* in */ "r"(h));
361
+ return f;
362
+ }
363
+
364
+ static inline lm_ggml_fp16_t lm_ggml_compute_fp32_to_fp16(float f) {
365
+ register double d;
366
+ register lm_ggml_fp16_t r;
367
+ __asm__( /* xscvdphp can work on double or single precision */
368
+ "xscvdphp %0,%2\n"
369
+ "mffprd %1,%0\n" :
370
+ /* temp */ "=d"(d),
371
+ /* out */ "=r"(r):
372
+ /* in */ "f"(f));
373
+ return r;
374
+ }
375
+
376
+ #else
377
+
378
+ // FP16 <-> FP32
379
+ // ref: https://github.com/Maratyszcza/FP16
380
+
381
+ static inline float fp32_from_bits(uint32_t w) {
382
+ union {
383
+ uint32_t as_bits;
384
+ float as_value;
385
+ } fp32;
386
+ fp32.as_bits = w;
387
+ return fp32.as_value;
388
+ }
389
+
390
+ static inline uint32_t fp32_to_bits(float f) {
391
+ union {
392
+ float as_value;
393
+ uint32_t as_bits;
394
+ } fp32;
395
+ fp32.as_value = f;
396
+ return fp32.as_bits;
397
+ }
398
+
399
+ static inline float lm_ggml_compute_fp16_to_fp32(lm_ggml_fp16_t h) {
400
+ const uint32_t w = (uint32_t) h << 16;
401
+ const uint32_t sign = w & UINT32_C(0x80000000);
402
+ const uint32_t two_w = w + w;
403
+
404
+ const uint32_t exp_offset = UINT32_C(0xE0) << 23;
405
+ #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)) && (!defined(__cplusplus) || __cplusplus >= 201703L)
406
+ const float exp_scale = 0x1.0p-112f;
407
+ #else
408
+ const float exp_scale = fp32_from_bits(UINT32_C(0x7800000));
409
+ #endif
410
+ const float normalized_value = fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale;
411
+
412
+ const uint32_t magic_mask = UINT32_C(126) << 23;
413
+ const float magic_bias = 0.5f;
414
+ const float denormalized_value = fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;
415
+
416
+ const uint32_t denormalized_cutoff = UINT32_C(1) << 27;
417
+ const uint32_t result = sign |
418
+ (two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value) : fp32_to_bits(normalized_value));
419
+ return fp32_from_bits(result);
420
+ }
421
+
422
+ static inline lm_ggml_fp16_t lm_ggml_compute_fp32_to_fp16(float f) {
423
+ #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)) && (!defined(__cplusplus) || __cplusplus >= 201703L)
424
+ const float scale_to_inf = 0x1.0p+112f;
425
+ const float scale_to_zero = 0x1.0p-110f;
426
+ #else
427
+ const float scale_to_inf = fp32_from_bits(UINT32_C(0x77800000));
428
+ const float scale_to_zero = fp32_from_bits(UINT32_C(0x08800000));
429
+ #endif
430
+ float base = (fabsf(f) * scale_to_inf) * scale_to_zero;
431
+
432
+ const uint32_t w = fp32_to_bits(f);
433
+ const uint32_t shl1_w = w + w;
434
+ const uint32_t sign = w & UINT32_C(0x80000000);
435
+ uint32_t bias = shl1_w & UINT32_C(0xFF000000);
436
+ if (bias < UINT32_C(0x71000000)) {
437
+ bias = UINT32_C(0x71000000);
438
+ }
439
+
440
+ base = fp32_from_bits((bias >> 1) + UINT32_C(0x07800000)) + base;
441
+ const uint32_t bits = fp32_to_bits(base);
442
+ const uint32_t exp_bits = (bits >> 13) & UINT32_C(0x00007C00);
443
+ const uint32_t mantissa_bits = bits & UINT32_C(0x00000FFF);
444
+ const uint32_t nonsign = exp_bits + mantissa_bits;
445
+ return (sign >> 16) | (shl1_w > UINT32_C(0xFF000000) ? UINT16_C(0x7E00) : nonsign);
446
+ }
447
+
448
+ #define LM_GGML_COMPUTE_FP16_TO_FP32(x) lm_ggml_compute_fp16_to_fp32(x)
449
+ #define LM_GGML_COMPUTE_FP32_TO_FP16(x) lm_ggml_compute_fp32_to_fp16(x)
450
+
451
+ #endif // defined(__ARM_NEON) && (!defined(__MSC_VER)
452
+
453
+ // precomputed f32 table for f16 (256 KB)
454
+ // defined in ggml.c, initialized in lm_ggml_init()
455
+ LM_GGML_API float lm_ggml_table_f32_f16[1 << 16];
456
+
457
+ // On ARM NEON, it's quicker to directly convert x -> x instead of calling into lm_ggml_lookup_fp16_to_fp32,
458
+ // so we define LM_GGML_FP16_TO_FP32 and LM_GGML_FP32_TO_FP16 elsewhere for NEON.
459
+ // This is also true for POWER9.
460
+ #if !defined(LM_GGML_FP16_TO_FP32)
461
+ inline static float lm_ggml_lookup_fp16_to_fp32(lm_ggml_fp16_t f) {
462
+ uint16_t s;
463
+ memcpy(&s, &f, sizeof(uint16_t));
464
+ return lm_ggml_table_f32_f16[s];
465
+ }
466
+
467
+ #define LM_GGML_FP16_TO_FP32(x) lm_ggml_lookup_fp16_to_fp32(x)
468
+ #endif
469
+
470
+ #if !defined(LM_GGML_FP32_TO_FP16)
471
+ #define LM_GGML_FP32_TO_FP16(x) LM_GGML_COMPUTE_FP32_TO_FP16(x)
472
+ #endif
473
+
474
+ /**
475
+ * Converts brain16 to float32.
476
+ *
477
+ * The bfloat16 floating point format has the following structure:
478
+ *
479
+ * ┌sign
480
+ * │
481
+ * │ ┌exponent
482
+ * │ │
483
+ * │ │ ┌mantissa
484
+ * │ │ │
485
+ * │┌──┴───┐┌─┴───┐
486
+ * 0b0000000000000000 brain16
487
+ *
488
+ * Since bf16 has the same number of exponent bits as a 32bit float,
489
+ * encoding and decoding numbers becomes relatively straightforward.
490
+ *
491
+ * ┌sign
492
+ * │
493
+ * │ ┌exponent
494
+ * │ │
495
+ * │ │ ┌mantissa
496
+ * │ │ │
497
+ * │┌──┴───┐┌─┴───────────────────┐
498
+ * 0b00000000000000000000000000000000 IEEE binary32
499
+ *
500
+ * For comparison, the standard fp16 format has fewer exponent bits.
501
+ *
502
+ * ┌sign
503
+ * │
504
+ * │ ┌exponent
505
+ * │ │
506
+ * │ │ ┌mantissa
507
+ * │ │ │
508
+ * │┌─┴─┐┌─┴──────┐
509
+ * 0b0000000000000000 IEEE binary16
510
+ *
511
+ * @see IEEE 754-2008
512
+ */
513
+ static inline float lm_ggml_compute_bf16_to_fp32(lm_ggml_bf16_t h) {
514
+ union {
515
+ float f;
516
+ uint32_t i;
517
+ } u;
518
+ u.i = (uint32_t)h.bits << 16;
519
+ return u.f;
520
+ }
521
+
522
+ /**
523
+ * Converts float32 to brain16.
524
+ *
525
+ * This is binary identical with Google Brain float conversion.
526
+ * Floats shall round to nearest even, and NANs shall be quiet.
527
+ * Subnormals aren't flushed to zero, except perhaps when used.
528
+ * This code should vectorize nicely if using modern compilers.
529
+ */
530
+ static inline lm_ggml_bf16_t lm_ggml_compute_fp32_to_bf16(float s) {
531
+ lm_ggml_bf16_t h;
532
+ union {
533
+ float f;
534
+ uint32_t i;
535
+ } u;
536
+ u.f = s;
537
+ if ((u.i & 0x7fffffff) > 0x7f800000) { /* nan */
538
+ h.bits = (u.i >> 16) | 64; /* force to quiet */
539
+ return h;
540
+ }
541
+ h.bits = (u.i + (0x7fff + ((u.i >> 16) & 1))) >> 16;
542
+ return h;
543
+ }
544
+
545
+ #define LM_GGML_FP32_TO_BF16(x) lm_ggml_compute_fp32_to_bf16(x)
546
+ #define LM_GGML_BF16_TO_FP32(x) lm_ggml_compute_bf16_to_fp32(x)
547
+
548
+ #ifdef __cplusplus
549
+ }
550
+ #endif