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