cui-llama.rn 1.2.6 → 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 -2
  2. package/android/src/main/CMakeLists.txt +20 -5
  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 +222 -34
  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 -2114
  9. package/cpp/common.h +600 -613
  10. package/cpp/ggml-aarch64.c +129 -3478
  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 -216
  15. package/cpp/ggml-backend-reg.cpp +195 -0
  16. package/cpp/ggml-backend.cpp +1997 -2661
  17. package/cpp/ggml-backend.h +328 -314
  18. package/cpp/ggml-common.h +1853 -1853
  19. package/cpp/ggml-cpp.h +38 -38
  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 -13720
  26. package/cpp/ggml-cpu.cpp +663 -0
  27. package/cpp/ggml-cpu.h +177 -150
  28. package/cpp/ggml-impl.h +550 -296
  29. package/cpp/ggml-metal.h +66 -66
  30. package/cpp/ggml-metal.m +4294 -3933
  31. package/cpp/ggml-quants.c +5247 -15739
  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 -8390
  36. package/cpp/ggml.h +2411 -2441
  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 -2345
  41. package/cpp/llama-sampling.h +48 -48
  42. package/cpp/llama-vocab.cpp +1984 -1984
  43. package/cpp/llama-vocab.h +170 -170
  44. package/cpp/llama.cpp +22132 -22046
  45. package/cpp/llama.h +1253 -1255
  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 -466
  50. package/cpp/sgemm.cpp +1884 -1276
  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 -5
  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 -6
  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,296 +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
- #include <string.h>
12
-
13
- #ifdef __cplusplus
14
- extern "C" {
15
- #endif
16
-
17
- #undef MIN
18
- #undef MAX
19
-
20
- #define MIN(a, b) ((a) < (b) ? (a) : (b))
21
- #define MAX(a, b) ((a) > (b) ? (a) : (b))
22
-
23
- // required for mmap as gguf only guarantees 32-byte alignment
24
- #define TENSOR_ALIGNMENT 32
25
-
26
- // static_assert should be a #define, but if it's not,
27
- // fall back to the _Static_assert C11 keyword.
28
- // if C99 - static_assert is noop
29
- // ref: https://stackoverflow.com/a/53923785/4039976
30
- #ifndef __cplusplus
31
- #ifndef static_assert
32
- #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201100L)
33
- #define static_assert(cond, msg) _Static_assert(cond, msg)
34
- #else
35
- #define static_assert(cond, msg) struct global_scope_noop_trick
36
- #endif
37
- #endif
38
- #endif
39
-
40
- static inline int lm_ggml_up32(int n) {
41
- return (n + 31) & ~31;
42
- }
43
-
44
- //static inline int lm_ggml_up64(int n) {
45
- // return (n + 63) & ~63;
46
- //}
47
-
48
- static inline int lm_ggml_up(int n, int m) {
49
- // assert m is a power of 2
50
- LM_GGML_ASSERT((m & (m - 1)) == 0);
51
- return (n + m - 1) & ~(m - 1);
52
- }
53
-
54
- //
55
- // logging
56
- //
57
-
58
- LM_GGML_ATTRIBUTE_FORMAT(2, 3)
59
- void lm_ggml_log_internal (enum lm_ggml_log_level level, const char * format, ...);
60
- void lm_ggml_log_callback_default(enum lm_ggml_log_level level, const char * text, void * user_data);
61
-
62
- #define LM_GGML_LOG(...) lm_ggml_log_internal(LM_GGML_LOG_LEVEL_NONE , __VA_ARGS__)
63
- #define LM_GGML_LOG_INFO(...) lm_ggml_log_internal(LM_GGML_LOG_LEVEL_INFO , __VA_ARGS__)
64
- #define LM_GGML_LOG_WARN(...) lm_ggml_log_internal(LM_GGML_LOG_LEVEL_WARN , __VA_ARGS__)
65
- #define LM_GGML_LOG_ERROR(...) lm_ggml_log_internal(LM_GGML_LOG_LEVEL_ERROR, __VA_ARGS__)
66
- #define LM_GGML_LOG_DEBUG(...) lm_ggml_log_internal(LM_GGML_LOG_LEVEL_DEBUG, __VA_ARGS__)
67
- #define LM_GGML_LOG_CONT(...) lm_ggml_log_internal(LM_GGML_LOG_LEVEL_CONT , __VA_ARGS__)
68
-
69
- #define LM_GGML_DEBUG 0
70
-
71
- #if (LM_GGML_DEBUG >= 1)
72
- #define LM_GGML_PRINT_DEBUG(...) LM_GGML_LOG_DEBUG(__VA_ARGS__)
73
- #else
74
- #define LM_GGML_PRINT_DEBUG(...)
75
- #endif
76
-
77
- #if (LM_GGML_DEBUG >= 5)
78
- #define LM_GGML_PRINT_DEBUG_5(...) LM_GGML_LOG_DEBUG(__VA_ARGS__)
79
- #else
80
- #define LM_GGML_PRINT_DEBUG_5(...)
81
- #endif
82
-
83
- #if (LM_GGML_DEBUG >= 10)
84
- #define LM_GGML_PRINT_DEBUG_10(...) LM_GGML_LOG_DEBUG(__VA_ARGS__)
85
- #else
86
- #define LM_GGML_PRINT_DEBUG_10(...)
87
- #endif
88
-
89
- // tensor params
90
-
91
- static void lm_ggml_set_op_params(struct lm_ggml_tensor * tensor, const void * params, size_t params_size) {
92
- LM_GGML_ASSERT(tensor != NULL); // silence -Warray-bounds warnings
93
- assert(params_size <= LM_GGML_MAX_OP_PARAMS);
94
- memcpy(tensor->op_params, params, params_size);
95
- }
96
-
97
- static int32_t lm_ggml_get_op_params_i32(const struct lm_ggml_tensor * tensor, uint32_t i) {
98
- assert(i < LM_GGML_MAX_OP_PARAMS / sizeof(int32_t));
99
- return ((const int32_t *)(tensor->op_params))[i];
100
- }
101
-
102
- static float lm_ggml_get_op_params_f32(const struct lm_ggml_tensor * tensor, uint32_t i) {
103
- assert(i < LM_GGML_MAX_OP_PARAMS / sizeof(float));
104
- return ((const float *)(tensor->op_params))[i];
105
- }
106
-
107
- static void lm_ggml_set_op_params_i32(struct lm_ggml_tensor * tensor, uint32_t i, int32_t value) {
108
- assert(i < LM_GGML_MAX_OP_PARAMS / sizeof(int32_t));
109
- ((int32_t *)(tensor->op_params))[i] = value;
110
- }
111
-
112
- static void lm_ggml_set_op_params_f32(struct lm_ggml_tensor * tensor, uint32_t i, float value) {
113
- assert(i < LM_GGML_MAX_OP_PARAMS / sizeof(float));
114
- ((float *)(tensor->op_params))[i] = value;
115
- }
116
-
117
- struct lm_ggml_map_custom1_op_params {
118
- lm_ggml_custom1_op_t fun;
119
- int n_tasks;
120
- void * userdata;
121
- };
122
-
123
-
124
- struct lm_ggml_map_custom2_op_params {
125
- lm_ggml_custom2_op_t fun;
126
- int n_tasks;
127
- void * userdata;
128
- };
129
-
130
-
131
- struct lm_ggml_map_custom3_op_params {
132
- lm_ggml_custom3_op_t fun;
133
- int n_tasks;
134
- void * userdata;
135
- };
136
-
137
- // bitset
138
-
139
- typedef uint32_t lm_ggml_bitset_t;
140
-
141
- static_assert(sizeof(lm_ggml_bitset_t) == 4, "bitset_t constants must be updated");
142
- #define BITSET_SHR 5 // log2(sizeof(lm_ggml_bitset_t)*8)
143
- #define BITSET_MASK (sizeof(lm_ggml_bitset_t)*8 - 1)
144
-
145
- static size_t lm_ggml_bitset_size(size_t n) {
146
- return (n + BITSET_MASK) >> BITSET_SHR;
147
- }
148
-
149
- static inline bool lm_ggml_bitset_get(const lm_ggml_bitset_t * bitset, size_t i) {
150
- return !!(bitset[i >> BITSET_SHR] & (1u << (i & BITSET_MASK)));
151
- }
152
-
153
- static inline void lm_ggml_bitset_set(lm_ggml_bitset_t * bitset, size_t i) {
154
- bitset[i >> BITSET_SHR] |= (1u << (i & BITSET_MASK));
155
- }
156
-
157
- static inline void lm_ggml_bitset_clear(lm_ggml_bitset_t * bitset, size_t i) {
158
- bitset[i >> BITSET_SHR] &= ~(1u << (i & BITSET_MASK));
159
- }
160
-
161
- // hash set
162
-
163
- #define LM_GGML_HASHSET_FULL ((size_t)-1)
164
- #define LM_GGML_HASHSET_ALREADY_EXISTS ((size_t)-2)
165
-
166
- struct lm_ggml_hash_set {
167
- size_t size;
168
- lm_ggml_bitset_t * used; // whether or not the keys are in use i.e. set
169
- struct lm_ggml_tensor ** keys; // actual tensors in the set, keys[i] is only defined if lm_ggml_bitset_get(used, i)
170
- };
171
-
172
- struct lm_ggml_hash_set lm_ggml_hash_set_new(size_t size);
173
- void lm_ggml_hash_set_free(struct lm_ggml_hash_set * hash_set);
174
-
175
- // returns the minimum size for a hash set that can hold min_sz elements
176
- size_t lm_ggml_hash_size(size_t min_sz);
177
-
178
- // remove all elements from the hash set
179
- void lm_ggml_hash_set_reset(struct lm_ggml_hash_set * hash_set);
180
-
181
- // returns true if key is in the hash set
182
- static bool lm_ggml_hash_contains(const struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key);
183
-
184
- // returns LM_GGML_HASHSET_FULL if table is full, otherwise the current index of the key or where it should be inserted
185
- static size_t lm_ggml_hash_find(const struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key);
186
-
187
- // returns LM_GGML_HASHSET_ALREADY_EXISTS if key already exists, index otherwise, asserts if table is full
188
- static size_t lm_ggml_hash_insert(struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key);
189
-
190
- // return index, asserts if table is full
191
- static size_t lm_ggml_hash_find_or_insert(struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key);
192
-
193
- // hash function for lm_ggml_tensor
194
- static inline size_t lm_ggml_hash(const struct lm_ggml_tensor * p) {
195
- // the last 4 bits are always zero due to alignment
196
- return (size_t)(uintptr_t)p >> 4;
197
- }
198
-
199
- static size_t lm_ggml_hash_find(const struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key) {
200
- size_t h = lm_ggml_hash(key) % hash_set->size;
201
-
202
- // linear probing
203
- size_t i = h;
204
- while (lm_ggml_bitset_get(hash_set->used, i) && hash_set->keys[i] != key) {
205
- i = (i + 1) % hash_set->size;
206
- if (i == h) {
207
- // visited all hash table entries -> not found
208
- return LM_GGML_HASHSET_FULL;
209
- }
210
- }
211
- return i;
212
- }
213
-
214
- static bool lm_ggml_hash_contains(const struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key) {
215
- size_t i = lm_ggml_hash_find(hash_set, key);
216
- return i != LM_GGML_HASHSET_FULL && lm_ggml_bitset_get(hash_set->used, i);
217
- }
218
-
219
- static size_t lm_ggml_hash_insert(struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key) {
220
- size_t h = lm_ggml_hash(key) % hash_set->size;
221
-
222
- // linear probing
223
- size_t i = h;
224
- do {
225
- if (!lm_ggml_bitset_get(hash_set->used, i)) {
226
- lm_ggml_bitset_set(hash_set->used, i);
227
- hash_set->keys[i] = key;
228
- return i;
229
- }
230
- if (hash_set->keys[i] == key) {
231
- return LM_GGML_HASHSET_ALREADY_EXISTS;
232
- }
233
- i = (i + 1) % hash_set->size;
234
- } while (i != h);
235
-
236
- // visited all hash table entries -> not found
237
- LM_GGML_ABORT("fatal error");
238
- }
239
-
240
- static size_t lm_ggml_hash_find_or_insert(struct lm_ggml_hash_set * hash_set, struct lm_ggml_tensor * key) {
241
- size_t h = lm_ggml_hash(key) % hash_set->size;
242
-
243
- // linear probing
244
- size_t i = h;
245
- do {
246
- if (!lm_ggml_bitset_get(hash_set->used, i)) {
247
- lm_ggml_bitset_set(hash_set->used, i);
248
- hash_set->keys[i] = key;
249
- return i;
250
- }
251
- if (hash_set->keys[i] == key) {
252
- return i;
253
- }
254
- i = (i + 1) % hash_set->size;
255
- } while (i != h);
256
-
257
- // visited all hash table entries -> not found
258
- LM_GGML_ABORT("fatal error");
259
- }
260
-
261
- // computation graph
262
-
263
- enum lm_ggml_cgraph_eval_order {
264
- LM_GGML_CGRAPH_EVAL_ORDER_LEFT_TO_RIGHT = 0,
265
- LM_GGML_CGRAPH_EVAL_ORDER_RIGHT_TO_LEFT,
266
- LM_GGML_CGRAPH_EVAL_ORDER_COUNT
267
- };
268
-
269
- struct lm_ggml_cgraph {
270
- int size;
271
- int n_nodes;
272
- int n_leafs;
273
-
274
- struct lm_ggml_tensor ** nodes;
275
- struct lm_ggml_tensor ** grads;
276
- struct lm_ggml_tensor ** leafs;
277
-
278
- struct lm_ggml_hash_set visited_hash_set;
279
-
280
- enum lm_ggml_cgraph_eval_order order;
281
- };
282
-
283
- struct lm_ggml_cgraph lm_ggml_graph_view(struct lm_ggml_cgraph * cgraph, int i0, int i1);
284
-
285
- // Memory allocation
286
-
287
- void * lm_ggml_aligned_malloc(size_t size);
288
- void lm_ggml_aligned_free(void * ptr, size_t size);
289
-
290
- // TODO: move to threading file
291
- void lm_ggml_critical_section_start(void);
292
- void lm_ggml_critical_section_end(void);
293
-
294
- #ifdef __cplusplus
295
- }
296
- #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