cui-llama.rn 1.1.6 → 1.2.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.
package/cpp/ggml-impl.h CHANGED
@@ -1,15 +1,17 @@
1
1
  #pragma once
2
2
 
3
- #include "ggml.h"
4
-
5
3
  // GGML internal header
6
4
 
5
+ #include "ggml.h"
6
+
7
7
  #include <assert.h>
8
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 <stddef.h>
10
9
  #include <stdbool.h>
11
- #include <string.h> // memcpy
12
- #include <math.h> // fabsf
10
+ #include <stdint.h>
11
+
12
+ #ifdef __cplusplus
13
+ extern "C" {
14
+ #endif
13
15
 
14
16
  #undef MIN
15
17
  #undef MAX
@@ -17,96 +19,6 @@
17
19
  #define MIN(a, b) ((a) < (b) ? (a) : (b))
18
20
  #define MAX(a, b) ((a) > (b) ? (a) : (b))
19
21
 
20
- #if defined(_MSC_VER)
21
-
22
- #define m512bh(p) p
23
- #define m512i(p) p
24
-
25
- #else
26
-
27
- #define m512bh(p) (__m512bh)(p)
28
- #define m512i(p) (__m512i)(p)
29
-
30
- #endif
31
-
32
- /**
33
- * Converts brain16 to float32.
34
- *
35
- * The bfloat16 floating point format has the following structure:
36
- *
37
- * ┌sign
38
- * │
39
- * │ ┌exponent
40
- * │ │
41
- * │ │ ┌mantissa
42
- * │ │ │
43
- * │┌──┴───┐┌─┴───┐
44
- * 0b0000000000000000 brain16
45
- *
46
- * Since bf16 has the same number of exponent bits as a 32bit float,
47
- * encoding and decoding numbers becomes relatively straightforward.
48
- *
49
- * ┌sign
50
- * │
51
- * │ ┌exponent
52
- * │ │
53
- * │ │ ┌mantissa
54
- * │ │ │
55
- * │┌──┴───┐┌─┴───────────────────┐
56
- * 0b00000000000000000000000000000000 IEEE binary32
57
- *
58
- * For comparison, the standard fp16 format has fewer exponent bits.
59
- *
60
- * ┌sign
61
- * │
62
- * │ ┌exponent
63
- * │ │
64
- * │ │ ┌mantissa
65
- * │ │ │
66
- * │┌─┴─┐┌─┴──────┐
67
- * 0b0000000000000000 IEEE binary16
68
- *
69
- * @see IEEE 754-2008
70
- */
71
- static inline float lm_ggml_compute_bf16_to_fp32(lm_ggml_bf16_t h) {
72
- union {
73
- float f;
74
- uint32_t i;
75
- } u;
76
- u.i = (uint32_t)h.bits << 16;
77
- return u.f;
78
- }
79
-
80
- /**
81
- * Converts float32 to brain16.
82
- *
83
- * This is binary identical with Google Brain float conversion.
84
- * Floats shall round to nearest even, and NANs shall be quiet.
85
- * Subnormals aren't flushed to zero, except perhaps when used.
86
- * This code should vectorize nicely if using modern compilers.
87
- */
88
- static inline lm_ggml_bf16_t lm_ggml_compute_fp32_to_bf16(float s) {
89
- lm_ggml_bf16_t h;
90
- union {
91
- float f;
92
- uint32_t i;
93
- } u;
94
- u.f = s;
95
- if ((u.i & 0x7fffffff) > 0x7f800000) { /* nan */
96
- h.bits = (u.i >> 16) | 64; /* force to quiet */
97
- return h;
98
- }
99
- h.bits = (u.i + (0x7fff + ((u.i >> 16) & 1))) >> 16;
100
- return h;
101
- }
102
-
103
- #define LM_GGML_FP32_TO_BF16(x) lm_ggml_compute_fp32_to_bf16(x)
104
- #define LM_GGML_BF16_TO_FP32(x) lm_ggml_compute_bf16_to_fp32(x)
105
-
106
- #ifdef __cplusplus
107
- extern "C" {
108
- #endif
109
-
110
22
  // static_assert should be a #define, but if it's not,
111
23
  // fall back to the _Static_assert C11 keyword.
112
24
  // if C99 - static_assert is noop
@@ -121,520 +33,6 @@ extern "C" {
121
33
  #endif
122
34
  #endif
123
35
 
124
- // __FMA__ and __F16C__ are not defined in MSVC, however they are implied with AVX2/AVX512
125
- #if defined(_MSC_VER) && (defined(__AVX2__) || defined(__AVX512F__))
126
- #ifndef __FMA__
127
- #define __FMA__
128
- #endif
129
- #ifndef __F16C__
130
- #define __F16C__
131
- #endif
132
- #endif
133
-
134
- // __SSE3__ and __SSSE3__ are not defined in MSVC, but SSE3/SSSE3 are present when AVX/AVX2/AVX512 are available
135
- #if defined(_MSC_VER) && (defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__))
136
- #ifndef __SSE3__
137
- #define __SSE3__
138
- #endif
139
- #ifndef __SSSE3__
140
- #define __SSSE3__
141
- #endif
142
- #endif
143
-
144
- #if defined(__ARM_FEATURE_SVE)
145
- #include <arm_sve.h>
146
- #include <sys/prctl.h>
147
- #endif
148
-
149
- // 16-bit float
150
- // on Arm, we use __fp16
151
- // on x86, we use uint16_t
152
- #if defined(__ARM_NEON)
153
-
154
- // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
155
- //
156
- // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
157
- //
158
- #include <arm_neon.h>
159
-
160
- #ifdef _MSC_VER
161
-
162
- typedef uint16_t lm_ggml_fp16_internal_t;
163
-
164
- #define lm_ggml_vld1q_u32(w,x,y,z) { ((w) + ((uint64_t)(x) << 32)), ((y) + ((uint64_t)(z) << 32)) }
165
-
166
- #else
167
-
168
- typedef __fp16 lm_ggml_fp16_internal_t;
169
-
170
- #define lm_ggml_vld1q_u32(w,x,y,z) { (w), (x), (y), (z) }
171
-
172
- #endif // _MSC_VER
173
-
174
- #if !defined(__aarch64__)
175
-
176
- // 32-bit ARM compatibility
177
-
178
- // vaddlvq_s16
179
- // vpaddq_s16
180
- // vpaddq_s32
181
- // vaddvq_s32
182
- // vaddvq_f32
183
- // vmaxvq_f32
184
- // vcvtnq_s32_f32
185
- // vzip1_u8
186
- // vzip2_u8
187
-
188
- inline static int32_t vaddlvq_s16(int16x8_t v) {
189
- int32x4_t v0 = vreinterpretq_s32_s64(vpaddlq_s32(vpaddlq_s16(v)));
190
- return vgetq_lane_s32(v0, 0) + vgetq_lane_s32(v0, 2);
191
- }
192
-
193
- inline static int16x8_t vpaddq_s16(int16x8_t a, int16x8_t b) {
194
- int16x4_t a0 = vpadd_s16(vget_low_s16(a), vget_high_s16(a));
195
- int16x4_t b0 = vpadd_s16(vget_low_s16(b), vget_high_s16(b));
196
- return vcombine_s16(a0, b0);
197
- }
198
-
199
- inline static int32x4_t vpaddq_s32(int32x4_t a, int32x4_t b) {
200
- int32x2_t a0 = vpadd_s32(vget_low_s32(a), vget_high_s32(a));
201
- int32x2_t b0 = vpadd_s32(vget_low_s32(b), vget_high_s32(b));
202
- return vcombine_s32(a0, b0);
203
- }
204
-
205
- inline static int32_t vaddvq_s32(int32x4_t v) {
206
- return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
207
- }
208
-
209
- inline static float vaddvq_f32(float32x4_t v) {
210
- return vgetq_lane_f32(v, 0) + vgetq_lane_f32(v, 1) + vgetq_lane_f32(v, 2) + vgetq_lane_f32(v, 3);
211
- }
212
-
213
- inline static float vmaxvq_f32(float32x4_t v) {
214
- return
215
- MAX(MAX(vgetq_lane_f32(v, 0), vgetq_lane_f32(v, 1)),
216
- MAX(vgetq_lane_f32(v, 2), vgetq_lane_f32(v, 3)));
217
- }
218
-
219
- inline static int32x4_t vcvtnq_s32_f32(float32x4_t v) {
220
- int32x4_t res;
221
-
222
- res[0] = roundf(vgetq_lane_f32(v, 0));
223
- res[1] = roundf(vgetq_lane_f32(v, 1));
224
- res[2] = roundf(vgetq_lane_f32(v, 2));
225
- res[3] = roundf(vgetq_lane_f32(v, 3));
226
-
227
- return res;
228
- }
229
-
230
- inline static uint8x8_t vzip1_u8(uint8x8_t a, uint8x8_t b) {
231
- uint8x8_t res;
232
-
233
- res[0] = a[0]; res[1] = b[0];
234
- res[2] = a[1]; res[3] = b[1];
235
- res[4] = a[2]; res[5] = b[2];
236
- res[6] = a[3]; res[7] = b[3];
237
-
238
- return res;
239
- }
240
-
241
- inline static uint8x8_t vzip2_u8(uint8x8_t a, uint8x8_t b) {
242
- uint8x8_t res;
243
-
244
- res[0] = a[4]; res[1] = b[4];
245
- res[2] = a[5]; res[3] = b[5];
246
- res[4] = a[6]; res[5] = b[6];
247
- res[6] = a[7]; res[7] = b[7];
248
-
249
- return res;
250
- }
251
-
252
- // vld1q_s16_x2
253
- // vld1q_u8_x2
254
- // vld1q_u8_x4
255
- // vld1q_s8_x2
256
- // vld1q_s8_x4
257
- // TODO: double-check these work correctly
258
-
259
- typedef struct lm_ggml_int16x8x2_t {
260
- int16x8_t val[2];
261
- } lm_ggml_int16x8x2_t;
262
-
263
- inline static lm_ggml_int16x8x2_t lm_ggml_vld1q_s16_x2(const int16_t * ptr) {
264
- lm_ggml_int16x8x2_t res;
265
-
266
- res.val[0] = vld1q_s16(ptr + 0);
267
- res.val[1] = vld1q_s16(ptr + 8);
268
-
269
- return res;
270
- }
271
-
272
- typedef struct lm_ggml_uint8x16x2_t {
273
- uint8x16_t val[2];
274
- } lm_ggml_uint8x16x2_t;
275
-
276
- inline static lm_ggml_uint8x16x2_t lm_ggml_vld1q_u8_x2(const uint8_t * ptr) {
277
- lm_ggml_uint8x16x2_t res;
278
-
279
- res.val[0] = vld1q_u8(ptr + 0);
280
- res.val[1] = vld1q_u8(ptr + 16);
281
-
282
- return res;
283
- }
284
-
285
- typedef struct lm_ggml_uint8x16x4_t {
286
- uint8x16_t val[4];
287
- } lm_ggml_uint8x16x4_t;
288
-
289
- inline static lm_ggml_uint8x16x4_t lm_ggml_vld1q_u8_x4(const uint8_t * ptr) {
290
- lm_ggml_uint8x16x4_t res;
291
-
292
- res.val[0] = vld1q_u8(ptr + 0);
293
- res.val[1] = vld1q_u8(ptr + 16);
294
- res.val[2] = vld1q_u8(ptr + 32);
295
- res.val[3] = vld1q_u8(ptr + 48);
296
-
297
- return res;
298
- }
299
-
300
- typedef struct lm_ggml_int8x16x2_t {
301
- int8x16_t val[2];
302
- } lm_ggml_int8x16x2_t;
303
-
304
- inline static lm_ggml_int8x16x2_t lm_ggml_vld1q_s8_x2(const int8_t * ptr) {
305
- lm_ggml_int8x16x2_t res;
306
-
307
- res.val[0] = vld1q_s8(ptr + 0);
308
- res.val[1] = vld1q_s8(ptr + 16);
309
-
310
- return res;
311
- }
312
-
313
- typedef struct lm_ggml_int8x16x4_t {
314
- int8x16_t val[4];
315
- } lm_ggml_int8x16x4_t;
316
-
317
- inline static lm_ggml_int8x16x4_t lm_ggml_vld1q_s8_x4(const int8_t * ptr) {
318
- lm_ggml_int8x16x4_t res;
319
-
320
- res.val[0] = vld1q_s8(ptr + 0);
321
- res.val[1] = vld1q_s8(ptr + 16);
322
- res.val[2] = vld1q_s8(ptr + 32);
323
- res.val[3] = vld1q_s8(ptr + 48);
324
-
325
- return res;
326
- }
327
-
328
- // NOTE: not tested
329
- inline static int8x16_t lm_ggml_vqtbl1q_s8(int8x16_t a, uint8x16_t b) {
330
- int8x16_t res;
331
-
332
- res[ 0] = a[b[ 0]];
333
- res[ 1] = a[b[ 1]];
334
- res[ 2] = a[b[ 2]];
335
- res[ 3] = a[b[ 3]];
336
- res[ 4] = a[b[ 4]];
337
- res[ 5] = a[b[ 5]];
338
- res[ 6] = a[b[ 6]];
339
- res[ 7] = a[b[ 7]];
340
- res[ 8] = a[b[ 8]];
341
- res[ 9] = a[b[ 9]];
342
- res[10] = a[b[10]];
343
- res[11] = a[b[11]];
344
- res[12] = a[b[12]];
345
- res[13] = a[b[13]];
346
- res[14] = a[b[14]];
347
- res[15] = a[b[15]];
348
-
349
- return res;
350
- }
351
-
352
- // NOTE: not tested
353
- inline static uint8x16_t lm_ggml_vqtbl1q_u8(uint8x16_t a, uint8x16_t b) {
354
- uint8x16_t res;
355
-
356
- res[ 0] = a[b[ 0]];
357
- res[ 1] = a[b[ 1]];
358
- res[ 2] = a[b[ 2]];
359
- res[ 3] = a[b[ 3]];
360
- res[ 4] = a[b[ 4]];
361
- res[ 5] = a[b[ 5]];
362
- res[ 6] = a[b[ 6]];
363
- res[ 7] = a[b[ 7]];
364
- res[ 8] = a[b[ 8]];
365
- res[ 9] = a[b[ 9]];
366
- res[10] = a[b[10]];
367
- res[11] = a[b[11]];
368
- res[12] = a[b[12]];
369
- res[13] = a[b[13]];
370
- res[14] = a[b[14]];
371
- res[15] = a[b[15]];
372
-
373
- return res;
374
- }
375
-
376
- #else
377
-
378
- #define lm_ggml_int16x8x2_t int16x8x2_t
379
- #define lm_ggml_uint8x16x2_t uint8x16x2_t
380
- #define lm_ggml_uint8x16x4_t uint8x16x4_t
381
- #define lm_ggml_int8x16x2_t int8x16x2_t
382
- #define lm_ggml_int8x16x4_t int8x16x4_t
383
-
384
- #define lm_ggml_vld1q_s16_x2 vld1q_s16_x2
385
- #define lm_ggml_vld1q_u8_x2 vld1q_u8_x2
386
- #define lm_ggml_vld1q_u8_x4 vld1q_u8_x4
387
- #define lm_ggml_vld1q_s8_x2 vld1q_s8_x2
388
- #define lm_ggml_vld1q_s8_x4 vld1q_s8_x4
389
- #define lm_ggml_vqtbl1q_s8 vqtbl1q_s8
390
- #define lm_ggml_vqtbl1q_u8 vqtbl1q_u8
391
-
392
- #endif // !defined(__aarch64__)
393
-
394
- #if !defined(__ARM_FEATURE_DOTPROD)
395
-
396
- inline static int32x4_t lm_ggml_vdotq_s32(int32x4_t acc, int8x16_t a, int8x16_t b) {
397
- const int16x8_t p0 = vmull_s8(vget_low_s8 (a), vget_low_s8 (b));
398
- const int16x8_t p1 = vmull_s8(vget_high_s8(a), vget_high_s8(b));
399
-
400
- return vaddq_s32(acc, vaddq_s32(vpaddlq_s16(p0), vpaddlq_s16(p1)));
401
- }
402
-
403
- #else
404
-
405
- #define lm_ggml_vdotq_s32(a, b, c) vdotq_s32(a, b, c)
406
-
407
- #endif // !defined(__ARM_FEATURE_DOTPROD)
408
-
409
- #endif // defined(__ARM_NEON)
410
-
411
- #if defined(__ARM_NEON) && !defined(_MSC_VER)
412
-
413
- #define LM_GGML_COMPUTE_FP16_TO_FP32(x) lm_ggml_compute_fp16_to_fp32(x)
414
- #define LM_GGML_COMPUTE_FP32_TO_FP16(x) lm_ggml_compute_fp32_to_fp16(x)
415
-
416
- #define LM_GGML_FP16_TO_FP32(x) lm_ggml_compute_fp16_to_fp32(x)
417
-
418
- static inline float lm_ggml_compute_fp16_to_fp32(lm_ggml_fp16_t h) {
419
- lm_ggml_fp16_internal_t tmp;
420
- memcpy(&tmp, &h, sizeof(lm_ggml_fp16_t));
421
- return (float)tmp;
422
- }
423
-
424
- static inline lm_ggml_fp16_t lm_ggml_compute_fp32_to_fp16(float f) {
425
- lm_ggml_fp16_t res;
426
- lm_ggml_fp16_internal_t tmp = f;
427
- memcpy(&res, &tmp, sizeof(lm_ggml_fp16_t));
428
- return res;
429
- }
430
-
431
- #else
432
-
433
- #ifdef __wasm_simd128__
434
- #include <wasm_simd128.h>
435
- #else
436
- #ifdef __POWER9_VECTOR__
437
- #include <altivec.h>
438
- #undef bool
439
- #define bool _Bool
440
- #else
441
- #if defined(_MSC_VER) || defined(__MINGW32__)
442
- #include <intrin.h>
443
- #else
444
- #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__) || defined(__SSE__)
445
- #if !defined(__riscv)
446
- #include <immintrin.h>
447
- #endif
448
- #endif
449
- #endif
450
- #endif
451
- #endif
452
-
453
- #ifdef __riscv_v_intrinsic
454
- #include <riscv_vector.h>
455
- #endif
456
-
457
- #if defined(__loongarch64)
458
- #if defined(__loongarch_asx)
459
- #include <lasxintrin.h>
460
- #endif
461
- #if defined(__loongarch_sx)
462
- #include <lsxintrin.h>
463
- #endif
464
- #endif
465
-
466
- #if defined(__loongarch_asx)
467
-
468
- typedef union {
469
- int32_t i;
470
- float f;
471
- } ft_union;
472
-
473
- /* float type data load instructions */
474
- static __m128 __lsx_vreplfr2vr_s(float val) {
475
- ft_union fi_tmpval = {.f = val};
476
- return (__m128)__lsx_vreplgr2vr_w(fi_tmpval.i);
477
- }
478
-
479
- static __m256 __lasx_xvreplfr2vr_s(float val) {
480
- ft_union fi_tmpval = {.f = val};
481
- return (__m256)__lasx_xvreplgr2vr_w(fi_tmpval.i);
482
- }
483
- #endif
484
-
485
- #ifdef __F16C__
486
-
487
- #ifdef _MSC_VER
488
- #define LM_GGML_COMPUTE_FP16_TO_FP32(x) _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128(x)))
489
- #define LM_GGML_COMPUTE_FP32_TO_FP16(x) _mm_extract_epi16(_mm_cvtps_ph(_mm_set_ss(x), 0), 0)
490
- #else
491
- #define LM_GGML_COMPUTE_FP16_TO_FP32(x) _cvtsh_ss(x)
492
- #define LM_GGML_COMPUTE_FP32_TO_FP16(x) _cvtss_sh(x, 0)
493
- #endif
494
-
495
- #elif defined(__POWER9_VECTOR__)
496
-
497
- #define LM_GGML_COMPUTE_FP16_TO_FP32(x) lm_ggml_compute_fp16_to_fp32(x)
498
- #define LM_GGML_COMPUTE_FP32_TO_FP16(x) lm_ggml_compute_fp32_to_fp16(x)
499
- /* the inline asm below is about 12% faster than the lookup method */
500
- #define LM_GGML_FP16_TO_FP32(x) LM_GGML_COMPUTE_FP16_TO_FP32(x)
501
- #define LM_GGML_FP32_TO_FP16(x) LM_GGML_COMPUTE_FP32_TO_FP16(x)
502
-
503
- static inline float lm_ggml_compute_fp16_to_fp32(lm_ggml_fp16_t h) {
504
- register float f;
505
- register double d;
506
- __asm__(
507
- "mtfprd %0,%2\n"
508
- "xscvhpdp %0,%0\n"
509
- "frsp %1,%0\n" :
510
- /* temp */ "=d"(d),
511
- /* out */ "=f"(f):
512
- /* in */ "r"(h));
513
- return f;
514
- }
515
-
516
- static inline lm_ggml_fp16_t lm_ggml_compute_fp32_to_fp16(float f) {
517
- register double d;
518
- register lm_ggml_fp16_t r;
519
- __asm__( /* xscvdphp can work on double or single precision */
520
- "xscvdphp %0,%2\n"
521
- "mffprd %1,%0\n" :
522
- /* temp */ "=d"(d),
523
- /* out */ "=r"(r):
524
- /* in */ "f"(f));
525
- return r;
526
- }
527
-
528
- #else
529
-
530
- // FP16 <-> FP32
531
- // ref: https://github.com/Maratyszcza/FP16
532
-
533
- static inline float fp32_from_bits(uint32_t w) {
534
- union {
535
- uint32_t as_bits;
536
- float as_value;
537
- } fp32;
538
- fp32.as_bits = w;
539
- return fp32.as_value;
540
- }
541
-
542
- static inline uint32_t fp32_to_bits(float f) {
543
- union {
544
- float as_value;
545
- uint32_t as_bits;
546
- } fp32;
547
- fp32.as_value = f;
548
- return fp32.as_bits;
549
- }
550
-
551
- static inline float lm_ggml_compute_fp16_to_fp32(lm_ggml_fp16_t h) {
552
- const uint32_t w = (uint32_t) h << 16;
553
- const uint32_t sign = w & UINT32_C(0x80000000);
554
- const uint32_t two_w = w + w;
555
-
556
- const uint32_t exp_offset = UINT32_C(0xE0) << 23;
557
- #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
558
- const float exp_scale = 0x1.0p-112f;
559
- #else
560
- const float exp_scale = fp32_from_bits(UINT32_C(0x7800000));
561
- #endif
562
- const float normalized_value = fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale;
563
-
564
- const uint32_t magic_mask = UINT32_C(126) << 23;
565
- const float magic_bias = 0.5f;
566
- const float denormalized_value = fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;
567
-
568
- const uint32_t denormalized_cutoff = UINT32_C(1) << 27;
569
- const uint32_t result = sign |
570
- (two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value) : fp32_to_bits(normalized_value));
571
- return fp32_from_bits(result);
572
- }
573
-
574
- static inline lm_ggml_fp16_t lm_ggml_compute_fp32_to_fp16(float f) {
575
- #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
576
- const float scale_to_inf = 0x1.0p+112f;
577
- const float scale_to_zero = 0x1.0p-110f;
578
- #else
579
- const float scale_to_inf = fp32_from_bits(UINT32_C(0x77800000));
580
- const float scale_to_zero = fp32_from_bits(UINT32_C(0x08800000));
581
- #endif
582
- float base = (fabsf(f) * scale_to_inf) * scale_to_zero;
583
-
584
- const uint32_t w = fp32_to_bits(f);
585
- const uint32_t shl1_w = w + w;
586
- const uint32_t sign = w & UINT32_C(0x80000000);
587
- uint32_t bias = shl1_w & UINT32_C(0xFF000000);
588
- if (bias < UINT32_C(0x71000000)) {
589
- bias = UINT32_C(0x71000000);
590
- }
591
-
592
- base = fp32_from_bits((bias >> 1) + UINT32_C(0x07800000)) + base;
593
- const uint32_t bits = fp32_to_bits(base);
594
- const uint32_t exp_bits = (bits >> 13) & UINT32_C(0x00007C00);
595
- const uint32_t mantissa_bits = bits & UINT32_C(0x00000FFF);
596
- const uint32_t nonsign = exp_bits + mantissa_bits;
597
- return (sign >> 16) | (shl1_w > UINT32_C(0xFF000000) ? UINT16_C(0x7E00) : nonsign);
598
- }
599
-
600
- #define LM_GGML_COMPUTE_FP16_TO_FP32(x) lm_ggml_compute_fp16_to_fp32(x)
601
- #define LM_GGML_COMPUTE_FP32_TO_FP16(x) lm_ggml_compute_fp32_to_fp16(x)
602
-
603
- #endif // __F16C__
604
-
605
- #endif // defined(__ARM_NEON) && (!defined(__MSC_VER)
606
-
607
- #ifdef __ARM_FEATURE_SVE
608
- #include <arm_sve.h>
609
- #endif // __ARM_FEATURE_SVE
610
-
611
- // precomputed f32 table for f16 (256 KB)
612
- // defined in ggml.c, initialized in lm_ggml_init()
613
- extern float lm_ggml_table_f32_f16[1 << 16];
614
-
615
- // On ARM NEON, it's quicker to directly convert x -> x instead of calling into lm_ggml_lookup_fp16_to_fp32,
616
- // so we define LM_GGML_FP16_TO_FP32 and LM_GGML_FP32_TO_FP16 elsewhere for NEON.
617
- // This is also true for POWER9.
618
- #if !defined(LM_GGML_FP16_TO_FP32)
619
- inline static float lm_ggml_lookup_fp16_to_fp32(lm_ggml_fp16_t f) {
620
- uint16_t s;
621
- memcpy(&s, &f, sizeof(uint16_t));
622
- return lm_ggml_table_f32_f16[s];
623
- }
624
-
625
- #define LM_GGML_FP16_TO_FP32(x) lm_ggml_lookup_fp16_to_fp32(x)
626
- #endif
627
-
628
- #if !defined(LM_GGML_FP32_TO_FP16)
629
- #define LM_GGML_FP32_TO_FP16(x) LM_GGML_COMPUTE_FP32_TO_FP16(x)
630
- #endif
631
-
632
- enum lm_ggml_cgraph_eval_order {
633
- LM_GGML_CGRAPH_EVAL_ORDER_LEFT_TO_RIGHT = 0,
634
- LM_GGML_CGRAPH_EVAL_ORDER_RIGHT_TO_LEFT,
635
- LM_GGML_CGRAPH_EVAL_ORDER_COUNT
636
- };
637
-
638
36
  // bitset
639
37
 
640
38
  typedef uint32_t lm_ggml_bitset_t;
@@ -761,6 +159,12 @@ static size_t lm_ggml_hash_find_or_insert(struct lm_ggml_hash_set * hash_set, st
761
159
 
762
160
  // computation graph
763
161
 
162
+ enum lm_ggml_cgraph_eval_order {
163
+ LM_GGML_CGRAPH_EVAL_ORDER_LEFT_TO_RIGHT = 0,
164
+ LM_GGML_CGRAPH_EVAL_ORDER_RIGHT_TO_LEFT,
165
+ LM_GGML_CGRAPH_EVAL_ORDER_COUNT
166
+ };
167
+
764
168
  struct lm_ggml_cgraph {
765
169
  int size;
766
170
  int n_nodes;
package/cpp/ggml-metal.m CHANGED
@@ -3167,6 +3167,7 @@ static struct lm_ggml_backend_buffer_i lm_ggml_backend_metal_buffer_i = {
3167
3167
  /* .free_buffer = */ lm_ggml_backend_metal_buffer_free_buffer,
3168
3168
  /* .get_base = */ lm_ggml_backend_metal_buffer_get_base,
3169
3169
  /* .init_tensor = */ NULL,
3170
+ /* .memset_tensor = */ NULL,
3170
3171
  /* .set_tensor = */ lm_ggml_backend_metal_buffer_set_tensor,
3171
3172
  /* .get_tensor = */ lm_ggml_backend_metal_buffer_get_tensor,
3172
3173
  /* .cpy_tensor = */ lm_ggml_backend_metal_buffer_cpy_tensor,
package/cpp/ggml-quants.c CHANGED
@@ -3,6 +3,7 @@
3
3
 
4
4
  #include "ggml-quants.h"
5
5
  #include "ggml-impl.h"
6
+ #include "ggml-cpu-impl.h"
6
7
 
7
8
 
8
9
  #include <math.h>