@fugood/llama.node 0.3.0 → 0.3.2
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/CMakeLists.txt +1 -10
- package/bin/darwin/arm64/llama-node.node +0 -0
- package/bin/darwin/x64/llama-node.node +0 -0
- package/bin/linux/arm64/llama-node.node +0 -0
- package/bin/linux/x64/llama-node.node +0 -0
- package/bin/linux-vulkan/arm64/llama-node.node +0 -0
- package/bin/linux-vulkan/x64/llama-node.node +0 -0
- package/bin/win32/arm64/llama-node.node +0 -0
- package/bin/win32/arm64/node.lib +0 -0
- package/bin/win32/x64/llama-node.node +0 -0
- package/bin/win32/x64/node.lib +0 -0
- package/bin/win32-vulkan/arm64/llama-node.node +0 -0
- package/bin/win32-vulkan/arm64/node.lib +0 -0
- package/bin/win32-vulkan/x64/llama-node.node +0 -0
- package/bin/win32-vulkan/x64/node.lib +0 -0
- package/package.json +6 -4
- package/src/LlamaCompletionWorker.cpp +6 -6
- package/src/LlamaContext.cpp +7 -9
- package/src/common.hpp +2 -1
- package/src/llama.cpp/.github/workflows/build.yml +98 -24
- package/src/llama.cpp/.github/workflows/close-issue.yml +5 -0
- package/src/llama.cpp/.github/workflows/docker.yml +43 -34
- package/src/llama.cpp/.github/workflows/nix-ci-aarch64.yml +7 -0
- package/src/llama.cpp/.github/workflows/nix-ci.yml +7 -0
- package/src/llama.cpp/.github/workflows/python-check-requirements.yml +2 -4
- package/src/llama.cpp/.github/workflows/python-type-check.yml +3 -1
- package/src/llama.cpp/.github/workflows/server.yml +7 -0
- package/src/llama.cpp/CMakeLists.txt +20 -8
- package/src/llama.cpp/common/CMakeLists.txt +12 -10
- package/src/llama.cpp/common/arg.cpp +2006 -0
- package/src/llama.cpp/common/arg.h +77 -0
- package/src/llama.cpp/common/common.cpp +496 -1632
- package/src/llama.cpp/common/common.h +161 -63
- package/src/llama.cpp/common/console.cpp +3 -0
- package/src/llama.cpp/common/log.cpp +401 -0
- package/src/llama.cpp/common/log.h +66 -698
- package/src/llama.cpp/common/ngram-cache.cpp +3 -0
- package/src/llama.cpp/common/sampling.cpp +348 -350
- package/src/llama.cpp/common/sampling.h +62 -139
- package/src/llama.cpp/common/stb_image.h +5990 -6398
- package/src/llama.cpp/common/train.cpp +2 -0
- package/src/llama.cpp/docs/build.md +36 -1
- package/src/llama.cpp/examples/CMakeLists.txt +0 -1
- package/src/llama.cpp/examples/baby-llama/baby-llama.cpp +1 -2
- package/src/llama.cpp/examples/batched/batched.cpp +39 -55
- package/src/llama.cpp/examples/batched-bench/batched-bench.cpp +34 -44
- package/src/llama.cpp/examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp +55 -52
- package/src/llama.cpp/examples/cvector-generator/cvector-generator.cpp +15 -15
- package/src/llama.cpp/examples/cvector-generator/pca.hpp +3 -13
- package/src/llama.cpp/examples/embedding/embedding.cpp +143 -87
- package/src/llama.cpp/examples/eval-callback/eval-callback.cpp +33 -33
- package/src/llama.cpp/examples/export-lora/export-lora.cpp +36 -35
- package/src/llama.cpp/examples/gbnf-validator/gbnf-validator.cpp +14 -39
- package/src/llama.cpp/examples/gen-docs/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/gen-docs/gen-docs.cpp +83 -0
- package/src/llama.cpp/examples/gguf-split/gguf-split.cpp +58 -39
- package/src/llama.cpp/examples/gritlm/gritlm.cpp +34 -27
- package/src/llama.cpp/examples/imatrix/imatrix.cpp +59 -62
- package/src/llama.cpp/examples/infill/infill.cpp +117 -132
- package/src/llama.cpp/examples/llama-bench/llama-bench.cpp +265 -58
- package/src/llama.cpp/examples/llama.android/llama/src/main/cpp/llama-android.cpp +29 -22
- package/src/llama.cpp/examples/llava/CMakeLists.txt +7 -0
- package/src/llama.cpp/examples/llava/clip.cpp +685 -150
- package/src/llama.cpp/examples/llava/clip.h +11 -2
- package/src/llama.cpp/examples/llava/llava-cli.cpp +47 -58
- package/src/llama.cpp/examples/llava/llava.cpp +110 -24
- package/src/llama.cpp/examples/llava/llava.h +2 -3
- package/src/llama.cpp/examples/llava/minicpmv-cli.cpp +323 -0
- package/src/llama.cpp/examples/llava/requirements.txt +1 -0
- package/src/llama.cpp/examples/lookahead/lookahead.cpp +42 -43
- package/src/llama.cpp/examples/lookup/lookup-create.cpp +10 -8
- package/src/llama.cpp/examples/lookup/lookup-stats.cpp +23 -22
- package/src/llama.cpp/examples/lookup/lookup.cpp +40 -43
- package/src/llama.cpp/examples/main/main.cpp +210 -262
- package/src/llama.cpp/examples/parallel/parallel.cpp +49 -49
- package/src/llama.cpp/examples/passkey/passkey.cpp +42 -50
- package/src/llama.cpp/examples/perplexity/perplexity.cpp +187 -200
- package/src/llama.cpp/examples/quantize/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/quantize/quantize.cpp +27 -9
- package/src/llama.cpp/examples/quantize-stats/quantize-stats.cpp +2 -3
- package/src/llama.cpp/examples/retrieval/retrieval.cpp +49 -44
- package/src/llama.cpp/examples/rpc/rpc-server.cpp +24 -1
- package/src/llama.cpp/examples/save-load-state/save-load-state.cpp +32 -35
- package/src/llama.cpp/examples/server/CMakeLists.txt +3 -5
- package/src/llama.cpp/examples/server/server.cpp +1027 -1073
- package/src/llama.cpp/examples/server/tests/requirements.txt +2 -1
- package/src/llama.cpp/examples/server/utils.hpp +107 -105
- package/src/llama.cpp/examples/simple/simple.cpp +35 -41
- package/src/llama.cpp/examples/speculative/speculative.cpp +129 -103
- package/src/llama.cpp/examples/sycl/run-llama2.sh +10 -19
- package/src/llama.cpp/examples/sycl/win-run-llama2.bat +1 -1
- package/src/llama.cpp/examples/tokenize/tokenize.cpp +25 -27
- package/src/llama.cpp/ggml/CMakeLists.txt +14 -3
- package/src/llama.cpp/ggml/include/ggml-alloc.h +3 -3
- package/src/llama.cpp/ggml/include/ggml-backend.h +145 -60
- package/src/llama.cpp/ggml/include/ggml-blas.h +3 -3
- package/src/llama.cpp/ggml/include/ggml-cann.h +15 -19
- package/src/llama.cpp/ggml/include/ggml-cuda.h +16 -16
- package/src/llama.cpp/ggml/include/ggml-metal.h +5 -8
- package/src/llama.cpp/ggml/include/ggml-rpc.h +5 -5
- package/src/llama.cpp/ggml/include/ggml-sycl.h +8 -8
- package/src/llama.cpp/ggml/include/ggml-vulkan.h +7 -7
- package/src/llama.cpp/ggml/include/ggml.h +293 -186
- package/src/llama.cpp/ggml/src/CMakeLists.txt +86 -44
- package/src/llama.cpp/ggml/src/ggml-aarch64.c +2135 -1119
- package/src/llama.cpp/ggml/src/ggml-alloc.c +6 -0
- package/src/llama.cpp/ggml/src/ggml-backend-impl.h +152 -70
- package/src/llama.cpp/ggml/src/{ggml-backend.c → ggml-backend.cpp} +606 -286
- package/src/llama.cpp/ggml/src/ggml-blas.cpp +9 -10
- package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.cpp +4 -27
- package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.h +32 -4
- package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.cpp +179 -41
- package/src/llama.cpp/ggml/src/ggml-cann/common.h +1 -0
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/CMakeLists.txt +2 -1
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/ascendc_kernels.h +2 -0
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_float_to_q4_0.cpp +278 -0
- package/src/llama.cpp/ggml/src/ggml-cann.cpp +215 -216
- package/src/llama.cpp/ggml/src/ggml-common.h +20 -0
- package/src/llama.cpp/ggml/src/ggml-cpu-impl.h +614 -0
- package/src/llama.cpp/ggml/src/ggml-cuda/vendors/cuda.h +14 -0
- package/src/llama.cpp/ggml/src/ggml-cuda/vendors/hip.h +178 -0
- package/src/llama.cpp/ggml/src/ggml-cuda/vendors/musa.h +134 -0
- package/src/llama.cpp/ggml/src/ggml-impl.h +49 -603
- package/src/llama.cpp/ggml/src/ggml-kompute.cpp +4 -24
- package/src/llama.cpp/ggml/src/ggml-quants.c +972 -92
- package/src/llama.cpp/ggml/src/ggml-quants.h +15 -0
- package/src/llama.cpp/ggml/src/ggml-rpc.cpp +116 -66
- package/src/llama.cpp/ggml/src/ggml-sycl/backend.hpp +3 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/common.cpp +11 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/common.hpp +52 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/conv.cpp +99 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/conv.hpp +21 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/convert.cpp +57 -57
- package/src/llama.cpp/ggml/src/ggml-sycl/convert.hpp +1 -1
- package/src/llama.cpp/ggml/src/ggml-sycl/dequantize.hpp +106 -106
- package/src/llama.cpp/ggml/src/ggml-sycl/dmmv.cpp +4 -4
- package/src/llama.cpp/ggml/src/ggml-sycl/dpct/helper.hpp +16 -3
- package/src/llama.cpp/ggml/src/ggml-sycl/gemm.hpp +101 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/im2col.cpp +125 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/im2col.hpp +23 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/mmvq.cpp +1 -1
- package/src/llama.cpp/ggml/src/ggml-sycl/norm.cpp +6 -3
- package/src/llama.cpp/ggml/src/ggml-sycl/presets.hpp +2 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/rope.cpp +1 -1
- package/src/llama.cpp/ggml/src/ggml-sycl/tsembd.cpp +71 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/tsembd.hpp +21 -0
- package/src/llama.cpp/ggml/src/ggml-sycl.cpp +97 -169
- package/src/llama.cpp/ggml/src/ggml-vulkan.cpp +1508 -1124
- package/src/llama.cpp/ggml/src/ggml.c +3001 -1647
- package/src/llama.cpp/ggml/src/llamafile/sgemm.cpp +192 -0
- package/src/llama.cpp/ggml/src/vulkan-shaders/CMakeLists.txt +2 -0
- package/src/llama.cpp/ggml/src/vulkan-shaders/vulkan-shaders-gen.cpp +88 -40
- package/src/llama.cpp/include/llama.h +241 -264
- package/src/llama.cpp/models/ggml-vocab-chameleon.gguf.inp +112 -0
- package/src/llama.cpp/models/ggml-vocab-chameleon.gguf.out +46 -0
- package/src/llama.cpp/requirements/requirements-convert_legacy_llama.txt +1 -1
- package/src/llama.cpp/src/llama-grammar.cpp +721 -122
- package/src/llama.cpp/src/llama-grammar.h +120 -15
- package/src/llama.cpp/src/llama-impl.h +156 -1
- package/src/llama.cpp/src/llama-sampling.cpp +1375 -303
- package/src/llama.cpp/src/llama-sampling.h +20 -47
- package/src/llama.cpp/src/llama-vocab.cpp +343 -120
- package/src/llama.cpp/src/llama-vocab.h +33 -17
- package/src/llama.cpp/src/llama.cpp +4247 -1525
- package/src/llama.cpp/src/unicode-data.cpp +6 -4
- package/src/llama.cpp/src/unicode-data.h +4 -4
- package/src/llama.cpp/src/unicode.cpp +15 -7
- package/src/llama.cpp/tests/CMakeLists.txt +3 -0
- package/src/llama.cpp/tests/test-arg-parser.cpp +131 -0
- package/src/llama.cpp/tests/test-backend-ops.cpp +1592 -289
- package/src/llama.cpp/tests/test-barrier.cpp +93 -0
- package/src/llama.cpp/tests/test-grad0.cpp +187 -70
- package/src/llama.cpp/tests/test-grammar-integration.cpp +23 -38
- package/src/llama.cpp/tests/test-grammar-parser.cpp +6 -4
- package/src/llama.cpp/tests/test-json-schema-to-grammar.cpp +6 -4
- package/src/llama.cpp/tests/test-llama-grammar.cpp +9 -8
- package/src/llama.cpp/tests/test-log.cpp +39 -0
- package/src/llama.cpp/tests/test-quantize-fns.cpp +6 -0
- package/src/llama.cpp/tests/test-rope.cpp +1 -1
- package/src/llama.cpp/tests/test-sampling.cpp +157 -98
- package/src/llama.cpp/tests/test-tokenizer-0.cpp +55 -35
- package/patches/llama.patch +0 -22
- package/src/llama.cpp/.github/workflows/bench.yml +0 -310
- package/src/llama.cpp/common/grammar-parser.cpp +0 -536
- package/src/llama.cpp/common/grammar-parser.h +0 -29
- package/src/llama.cpp/examples/benchmark/CMakeLists.txt +0 -6
- package/src/llama.cpp/examples/benchmark/benchmark-matmult.cpp +0 -275
|
@@ -227,6 +227,25 @@ typedef struct {
|
|
|
227
227
|
} block_q8_0x8;
|
|
228
228
|
static_assert(sizeof(block_q8_0x8) == 8 * sizeof(ggml_half) + QK8_0 * 8, "wrong q8_0x8 block size/padding");
|
|
229
229
|
|
|
230
|
+
//
|
|
231
|
+
// Ternary quantization
|
|
232
|
+
//
|
|
233
|
+
|
|
234
|
+
// 1.6875 bpw
|
|
235
|
+
typedef struct {
|
|
236
|
+
uint8_t qs[(QK_K - 4 * QK_K / 64) / 5]; // 5 elements per byte (3^5 = 243 < 256)
|
|
237
|
+
uint8_t qh[QK_K/64]; // 4 elements per byte
|
|
238
|
+
ggml_half d;
|
|
239
|
+
} block_tq1_0;
|
|
240
|
+
static_assert(sizeof(block_tq1_0) == sizeof(ggml_half) + QK_K / 64 + (QK_K - 4 * QK_K / 64) / 5, "wrong tq1_0 block size/padding");
|
|
241
|
+
|
|
242
|
+
// 2.0625 bpw
|
|
243
|
+
typedef struct {
|
|
244
|
+
uint8_t qs[QK_K/4]; // 2 bits per element
|
|
245
|
+
ggml_half d;
|
|
246
|
+
} block_tq2_0;
|
|
247
|
+
static_assert(sizeof(block_tq2_0) == sizeof(ggml_half) + QK_K / 4, "wrong tq2_0 block size/padding");
|
|
248
|
+
|
|
230
249
|
//
|
|
231
250
|
// Super-block quantization structures
|
|
232
251
|
//
|
|
@@ -361,6 +380,7 @@ typedef struct {
|
|
|
361
380
|
} block_iq3_s;
|
|
362
381
|
static_assert(sizeof(block_iq3_s) == sizeof(ggml_half) + 13*(QK_K/32) + IQ3S_N_SCALE, "wrong iq3_s block size/padding");
|
|
363
382
|
|
|
383
|
+
// 1.5625 bpw
|
|
364
384
|
typedef struct {
|
|
365
385
|
ggml_half d;
|
|
366
386
|
uint8_t qs[QK_K/8];
|
|
@@ -0,0 +1,614 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
// GGML CPU internal header
|
|
4
|
+
|
|
5
|
+
#include "ggml.h"
|
|
6
|
+
#include "ggml-impl.h"
|
|
7
|
+
#include <stdlib.h> // load `stdlib.h` before other headers to work around MinGW bug: https://sourceforge.net/p/mingw-w64/bugs/192/
|
|
8
|
+
//#include <stddef.h>
|
|
9
|
+
#include <stdbool.h>
|
|
10
|
+
#include <string.h> // memcpy
|
|
11
|
+
#include <math.h> // fabsf
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
#ifdef __cplusplus
|
|
15
|
+
extern "C" {
|
|
16
|
+
#endif
|
|
17
|
+
|
|
18
|
+
#if defined(_MSC_VER)
|
|
19
|
+
|
|
20
|
+
#define m512bh(p) p
|
|
21
|
+
#define m512i(p) p
|
|
22
|
+
|
|
23
|
+
#else
|
|
24
|
+
|
|
25
|
+
#define m512bh(p) (__m512bh)(p)
|
|
26
|
+
#define m512i(p) (__m512i)(p)
|
|
27
|
+
|
|
28
|
+
#endif
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Converts brain16 to float32.
|
|
32
|
+
*
|
|
33
|
+
* The bfloat16 floating point format has the following structure:
|
|
34
|
+
*
|
|
35
|
+
* ┌sign
|
|
36
|
+
* │
|
|
37
|
+
* │ ┌exponent
|
|
38
|
+
* │ │
|
|
39
|
+
* │ │ ┌mantissa
|
|
40
|
+
* │ │ │
|
|
41
|
+
* │┌──┴───┐┌─┴───┐
|
|
42
|
+
* 0b0000000000000000 brain16
|
|
43
|
+
*
|
|
44
|
+
* Since bf16 has the same number of exponent bits as a 32bit float,
|
|
45
|
+
* encoding and decoding numbers becomes relatively straightforward.
|
|
46
|
+
*
|
|
47
|
+
* ┌sign
|
|
48
|
+
* │
|
|
49
|
+
* │ ┌exponent
|
|
50
|
+
* │ │
|
|
51
|
+
* │ │ ┌mantissa
|
|
52
|
+
* │ │ │
|
|
53
|
+
* │┌──┴───┐┌─┴───────────────────┐
|
|
54
|
+
* 0b00000000000000000000000000000000 IEEE binary32
|
|
55
|
+
*
|
|
56
|
+
* For comparison, the standard fp16 format has fewer exponent bits.
|
|
57
|
+
*
|
|
58
|
+
* ┌sign
|
|
59
|
+
* │
|
|
60
|
+
* │ ┌exponent
|
|
61
|
+
* │ │
|
|
62
|
+
* │ │ ┌mantissa
|
|
63
|
+
* │ │ │
|
|
64
|
+
* │┌─┴─┐┌─┴──────┐
|
|
65
|
+
* 0b0000000000000000 IEEE binary16
|
|
66
|
+
*
|
|
67
|
+
* @see IEEE 754-2008
|
|
68
|
+
*/
|
|
69
|
+
static inline float ggml_compute_bf16_to_fp32(ggml_bf16_t h) {
|
|
70
|
+
union {
|
|
71
|
+
float f;
|
|
72
|
+
uint32_t i;
|
|
73
|
+
} u;
|
|
74
|
+
u.i = (uint32_t)h.bits << 16;
|
|
75
|
+
return u.f;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Converts float32 to brain16.
|
|
80
|
+
*
|
|
81
|
+
* This is binary identical with Google Brain float conversion.
|
|
82
|
+
* Floats shall round to nearest even, and NANs shall be quiet.
|
|
83
|
+
* Subnormals aren't flushed to zero, except perhaps when used.
|
|
84
|
+
* This code should vectorize nicely if using modern compilers.
|
|
85
|
+
*/
|
|
86
|
+
static inline ggml_bf16_t ggml_compute_fp32_to_bf16(float s) {
|
|
87
|
+
ggml_bf16_t h;
|
|
88
|
+
union {
|
|
89
|
+
float f;
|
|
90
|
+
uint32_t i;
|
|
91
|
+
} u;
|
|
92
|
+
u.f = s;
|
|
93
|
+
if ((u.i & 0x7fffffff) > 0x7f800000) { /* nan */
|
|
94
|
+
h.bits = (u.i >> 16) | 64; /* force to quiet */
|
|
95
|
+
return h;
|
|
96
|
+
}
|
|
97
|
+
h.bits = (u.i + (0x7fff + ((u.i >> 16) & 1))) >> 16;
|
|
98
|
+
return h;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
#define GGML_FP32_TO_BF16(x) ggml_compute_fp32_to_bf16(x)
|
|
102
|
+
#define GGML_BF16_TO_FP32(x) ggml_compute_bf16_to_fp32(x)
|
|
103
|
+
|
|
104
|
+
// __FMA__ and __F16C__ are not defined in MSVC, however they are implied with AVX2/AVX512
|
|
105
|
+
#if defined(_MSC_VER) && (defined(__AVX2__) || defined(__AVX512F__))
|
|
106
|
+
#ifndef __FMA__
|
|
107
|
+
#define __FMA__
|
|
108
|
+
#endif
|
|
109
|
+
#ifndef __F16C__
|
|
110
|
+
#define __F16C__
|
|
111
|
+
#endif
|
|
112
|
+
#endif
|
|
113
|
+
|
|
114
|
+
// __SSE3__ and __SSSE3__ are not defined in MSVC, but SSE3/SSSE3 are present when AVX/AVX2/AVX512 are available
|
|
115
|
+
#if defined(_MSC_VER) && (defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__))
|
|
116
|
+
#ifndef __SSE3__
|
|
117
|
+
#define __SSE3__
|
|
118
|
+
#endif
|
|
119
|
+
#ifndef __SSSE3__
|
|
120
|
+
#define __SSSE3__
|
|
121
|
+
#endif
|
|
122
|
+
#endif
|
|
123
|
+
|
|
124
|
+
#if defined(__ARM_FEATURE_SVE)
|
|
125
|
+
#include <arm_sve.h>
|
|
126
|
+
#include <sys/prctl.h>
|
|
127
|
+
#endif
|
|
128
|
+
|
|
129
|
+
// 16-bit float
|
|
130
|
+
// on Arm, we use __fp16
|
|
131
|
+
// on x86, we use uint16_t
|
|
132
|
+
#if defined(__ARM_NEON)
|
|
133
|
+
|
|
134
|
+
// if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
|
|
135
|
+
//
|
|
136
|
+
// $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
|
|
137
|
+
//
|
|
138
|
+
#include <arm_neon.h>
|
|
139
|
+
|
|
140
|
+
#ifdef _MSC_VER
|
|
141
|
+
|
|
142
|
+
typedef uint16_t ggml_fp16_internal_t;
|
|
143
|
+
|
|
144
|
+
#define ggml_vld1q_u32(w,x,y,z) { ((w) + ((uint64_t)(x) << 32)), ((y) + ((uint64_t)(z) << 32)) }
|
|
145
|
+
|
|
146
|
+
#else
|
|
147
|
+
|
|
148
|
+
typedef __fp16 ggml_fp16_internal_t;
|
|
149
|
+
|
|
150
|
+
#define ggml_vld1q_u32(w,x,y,z) { (w), (x), (y), (z) }
|
|
151
|
+
|
|
152
|
+
#endif // _MSC_VER
|
|
153
|
+
|
|
154
|
+
#if !defined(__aarch64__)
|
|
155
|
+
|
|
156
|
+
// 32-bit ARM compatibility
|
|
157
|
+
|
|
158
|
+
// vaddlvq_s16
|
|
159
|
+
// vpaddq_s16
|
|
160
|
+
// vpaddq_s32
|
|
161
|
+
// vaddvq_s32
|
|
162
|
+
// vaddvq_f32
|
|
163
|
+
// vmaxvq_f32
|
|
164
|
+
// vcvtnq_s32_f32
|
|
165
|
+
// vzip1_u8
|
|
166
|
+
// vzip2_u8
|
|
167
|
+
|
|
168
|
+
inline static int32_t vaddlvq_s16(int16x8_t v) {
|
|
169
|
+
int32x4_t v0 = vreinterpretq_s32_s64(vpaddlq_s32(vpaddlq_s16(v)));
|
|
170
|
+
return vgetq_lane_s32(v0, 0) + vgetq_lane_s32(v0, 2);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
inline static int16x8_t vpaddq_s16(int16x8_t a, int16x8_t b) {
|
|
174
|
+
int16x4_t a0 = vpadd_s16(vget_low_s16(a), vget_high_s16(a));
|
|
175
|
+
int16x4_t b0 = vpadd_s16(vget_low_s16(b), vget_high_s16(b));
|
|
176
|
+
return vcombine_s16(a0, b0);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
inline static int32x4_t vpaddq_s32(int32x4_t a, int32x4_t b) {
|
|
180
|
+
int32x2_t a0 = vpadd_s32(vget_low_s32(a), vget_high_s32(a));
|
|
181
|
+
int32x2_t b0 = vpadd_s32(vget_low_s32(b), vget_high_s32(b));
|
|
182
|
+
return vcombine_s32(a0, b0);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
inline static int32_t vaddvq_s32(int32x4_t v) {
|
|
186
|
+
return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
inline static float vaddvq_f32(float32x4_t v) {
|
|
190
|
+
return vgetq_lane_f32(v, 0) + vgetq_lane_f32(v, 1) + vgetq_lane_f32(v, 2) + vgetq_lane_f32(v, 3);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
inline static float vmaxvq_f32(float32x4_t v) {
|
|
194
|
+
return
|
|
195
|
+
MAX(MAX(vgetq_lane_f32(v, 0), vgetq_lane_f32(v, 1)),
|
|
196
|
+
MAX(vgetq_lane_f32(v, 2), vgetq_lane_f32(v, 3)));
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
inline static int32x4_t vcvtnq_s32_f32(float32x4_t v) {
|
|
200
|
+
int32x4_t res;
|
|
201
|
+
|
|
202
|
+
res[0] = roundf(vgetq_lane_f32(v, 0));
|
|
203
|
+
res[1] = roundf(vgetq_lane_f32(v, 1));
|
|
204
|
+
res[2] = roundf(vgetq_lane_f32(v, 2));
|
|
205
|
+
res[3] = roundf(vgetq_lane_f32(v, 3));
|
|
206
|
+
|
|
207
|
+
return res;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
inline static uint8x8_t vzip1_u8(uint8x8_t a, uint8x8_t b) {
|
|
211
|
+
uint8x8_t res;
|
|
212
|
+
|
|
213
|
+
res[0] = a[0]; res[1] = b[0];
|
|
214
|
+
res[2] = a[1]; res[3] = b[1];
|
|
215
|
+
res[4] = a[2]; res[5] = b[2];
|
|
216
|
+
res[6] = a[3]; res[7] = b[3];
|
|
217
|
+
|
|
218
|
+
return res;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
inline static uint8x8_t vzip2_u8(uint8x8_t a, uint8x8_t b) {
|
|
222
|
+
uint8x8_t res;
|
|
223
|
+
|
|
224
|
+
res[0] = a[4]; res[1] = b[4];
|
|
225
|
+
res[2] = a[5]; res[3] = b[5];
|
|
226
|
+
res[4] = a[6]; res[5] = b[6];
|
|
227
|
+
res[6] = a[7]; res[7] = b[7];
|
|
228
|
+
|
|
229
|
+
return res;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// vld1q_s16_x2
|
|
233
|
+
// vld1q_u8_x2
|
|
234
|
+
// vld1q_u8_x4
|
|
235
|
+
// vld1q_s8_x2
|
|
236
|
+
// vld1q_s8_x4
|
|
237
|
+
// TODO: double-check these work correctly
|
|
238
|
+
|
|
239
|
+
typedef struct ggml_int16x8x2_t {
|
|
240
|
+
int16x8_t val[2];
|
|
241
|
+
} ggml_int16x8x2_t;
|
|
242
|
+
|
|
243
|
+
inline static ggml_int16x8x2_t ggml_vld1q_s16_x2(const int16_t * ptr) {
|
|
244
|
+
ggml_int16x8x2_t res;
|
|
245
|
+
|
|
246
|
+
res.val[0] = vld1q_s16(ptr + 0);
|
|
247
|
+
res.val[1] = vld1q_s16(ptr + 8);
|
|
248
|
+
|
|
249
|
+
return res;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
typedef struct ggml_uint8x16x2_t {
|
|
253
|
+
uint8x16_t val[2];
|
|
254
|
+
} ggml_uint8x16x2_t;
|
|
255
|
+
|
|
256
|
+
inline static ggml_uint8x16x2_t ggml_vld1q_u8_x2(const uint8_t * ptr) {
|
|
257
|
+
ggml_uint8x16x2_t res;
|
|
258
|
+
|
|
259
|
+
res.val[0] = vld1q_u8(ptr + 0);
|
|
260
|
+
res.val[1] = vld1q_u8(ptr + 16);
|
|
261
|
+
|
|
262
|
+
return res;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
typedef struct ggml_uint8x16x4_t {
|
|
266
|
+
uint8x16_t val[4];
|
|
267
|
+
} ggml_uint8x16x4_t;
|
|
268
|
+
|
|
269
|
+
inline static ggml_uint8x16x4_t ggml_vld1q_u8_x4(const uint8_t * ptr) {
|
|
270
|
+
ggml_uint8x16x4_t res;
|
|
271
|
+
|
|
272
|
+
res.val[0] = vld1q_u8(ptr + 0);
|
|
273
|
+
res.val[1] = vld1q_u8(ptr + 16);
|
|
274
|
+
res.val[2] = vld1q_u8(ptr + 32);
|
|
275
|
+
res.val[3] = vld1q_u8(ptr + 48);
|
|
276
|
+
|
|
277
|
+
return res;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
typedef struct ggml_int8x16x2_t {
|
|
281
|
+
int8x16_t val[2];
|
|
282
|
+
} ggml_int8x16x2_t;
|
|
283
|
+
|
|
284
|
+
inline static ggml_int8x16x2_t ggml_vld1q_s8_x2(const int8_t * ptr) {
|
|
285
|
+
ggml_int8x16x2_t res;
|
|
286
|
+
|
|
287
|
+
res.val[0] = vld1q_s8(ptr + 0);
|
|
288
|
+
res.val[1] = vld1q_s8(ptr + 16);
|
|
289
|
+
|
|
290
|
+
return res;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
typedef struct ggml_int8x16x4_t {
|
|
294
|
+
int8x16_t val[4];
|
|
295
|
+
} ggml_int8x16x4_t;
|
|
296
|
+
|
|
297
|
+
inline static ggml_int8x16x4_t ggml_vld1q_s8_x4(const int8_t * ptr) {
|
|
298
|
+
ggml_int8x16x4_t res;
|
|
299
|
+
|
|
300
|
+
res.val[0] = vld1q_s8(ptr + 0);
|
|
301
|
+
res.val[1] = vld1q_s8(ptr + 16);
|
|
302
|
+
res.val[2] = vld1q_s8(ptr + 32);
|
|
303
|
+
res.val[3] = vld1q_s8(ptr + 48);
|
|
304
|
+
|
|
305
|
+
return res;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// NOTE: not tested
|
|
309
|
+
inline static int8x16_t ggml_vqtbl1q_s8(int8x16_t a, uint8x16_t b) {
|
|
310
|
+
int8x16_t res;
|
|
311
|
+
|
|
312
|
+
res[ 0] = a[b[ 0]];
|
|
313
|
+
res[ 1] = a[b[ 1]];
|
|
314
|
+
res[ 2] = a[b[ 2]];
|
|
315
|
+
res[ 3] = a[b[ 3]];
|
|
316
|
+
res[ 4] = a[b[ 4]];
|
|
317
|
+
res[ 5] = a[b[ 5]];
|
|
318
|
+
res[ 6] = a[b[ 6]];
|
|
319
|
+
res[ 7] = a[b[ 7]];
|
|
320
|
+
res[ 8] = a[b[ 8]];
|
|
321
|
+
res[ 9] = a[b[ 9]];
|
|
322
|
+
res[10] = a[b[10]];
|
|
323
|
+
res[11] = a[b[11]];
|
|
324
|
+
res[12] = a[b[12]];
|
|
325
|
+
res[13] = a[b[13]];
|
|
326
|
+
res[14] = a[b[14]];
|
|
327
|
+
res[15] = a[b[15]];
|
|
328
|
+
|
|
329
|
+
return res;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// NOTE: not tested
|
|
333
|
+
inline static uint8x16_t ggml_vqtbl1q_u8(uint8x16_t a, uint8x16_t b) {
|
|
334
|
+
uint8x16_t res;
|
|
335
|
+
|
|
336
|
+
res[ 0] = a[b[ 0]];
|
|
337
|
+
res[ 1] = a[b[ 1]];
|
|
338
|
+
res[ 2] = a[b[ 2]];
|
|
339
|
+
res[ 3] = a[b[ 3]];
|
|
340
|
+
res[ 4] = a[b[ 4]];
|
|
341
|
+
res[ 5] = a[b[ 5]];
|
|
342
|
+
res[ 6] = a[b[ 6]];
|
|
343
|
+
res[ 7] = a[b[ 7]];
|
|
344
|
+
res[ 8] = a[b[ 8]];
|
|
345
|
+
res[ 9] = a[b[ 9]];
|
|
346
|
+
res[10] = a[b[10]];
|
|
347
|
+
res[11] = a[b[11]];
|
|
348
|
+
res[12] = a[b[12]];
|
|
349
|
+
res[13] = a[b[13]];
|
|
350
|
+
res[14] = a[b[14]];
|
|
351
|
+
res[15] = a[b[15]];
|
|
352
|
+
|
|
353
|
+
return res;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
#else
|
|
357
|
+
|
|
358
|
+
#define ggml_int16x8x2_t int16x8x2_t
|
|
359
|
+
#define ggml_uint8x16x2_t uint8x16x2_t
|
|
360
|
+
#define ggml_uint8x16x4_t uint8x16x4_t
|
|
361
|
+
#define ggml_int8x16x2_t int8x16x2_t
|
|
362
|
+
#define ggml_int8x16x4_t int8x16x4_t
|
|
363
|
+
|
|
364
|
+
#define ggml_vld1q_s16_x2 vld1q_s16_x2
|
|
365
|
+
#define ggml_vld1q_u8_x2 vld1q_u8_x2
|
|
366
|
+
#define ggml_vld1q_u8_x4 vld1q_u8_x4
|
|
367
|
+
#define ggml_vld1q_s8_x2 vld1q_s8_x2
|
|
368
|
+
#define ggml_vld1q_s8_x4 vld1q_s8_x4
|
|
369
|
+
#define ggml_vqtbl1q_s8 vqtbl1q_s8
|
|
370
|
+
#define ggml_vqtbl1q_u8 vqtbl1q_u8
|
|
371
|
+
|
|
372
|
+
#endif // !defined(__aarch64__)
|
|
373
|
+
|
|
374
|
+
#if !defined(__ARM_FEATURE_DOTPROD)
|
|
375
|
+
|
|
376
|
+
inline static int32x4_t ggml_vdotq_s32(int32x4_t acc, int8x16_t a, int8x16_t b) {
|
|
377
|
+
const int16x8_t p0 = vmull_s8(vget_low_s8 (a), vget_low_s8 (b));
|
|
378
|
+
const int16x8_t p1 = vmull_s8(vget_high_s8(a), vget_high_s8(b));
|
|
379
|
+
|
|
380
|
+
return vaddq_s32(acc, vaddq_s32(vpaddlq_s16(p0), vpaddlq_s16(p1)));
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
#else
|
|
384
|
+
|
|
385
|
+
#define ggml_vdotq_s32(a, b, c) vdotq_s32(a, b, c)
|
|
386
|
+
|
|
387
|
+
#endif // !defined(__ARM_FEATURE_DOTPROD)
|
|
388
|
+
|
|
389
|
+
#endif // defined(__ARM_NEON)
|
|
390
|
+
|
|
391
|
+
#if defined(__ARM_NEON) && !defined(_MSC_VER)
|
|
392
|
+
|
|
393
|
+
#define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
|
|
394
|
+
#define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
|
|
395
|
+
|
|
396
|
+
#define GGML_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
|
|
397
|
+
|
|
398
|
+
static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
|
|
399
|
+
ggml_fp16_internal_t tmp;
|
|
400
|
+
memcpy(&tmp, &h, sizeof(ggml_fp16_t));
|
|
401
|
+
return (float)tmp;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
|
|
405
|
+
ggml_fp16_t res;
|
|
406
|
+
ggml_fp16_internal_t tmp = f;
|
|
407
|
+
memcpy(&res, &tmp, sizeof(ggml_fp16_t));
|
|
408
|
+
return res;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
#else
|
|
412
|
+
|
|
413
|
+
#ifdef __wasm_simd128__
|
|
414
|
+
#include <wasm_simd128.h>
|
|
415
|
+
#else
|
|
416
|
+
#ifdef __POWER9_VECTOR__
|
|
417
|
+
#include <altivec.h>
|
|
418
|
+
#undef bool
|
|
419
|
+
#define bool _Bool
|
|
420
|
+
#else
|
|
421
|
+
#if defined(_MSC_VER) || defined(__MINGW32__)
|
|
422
|
+
#include <intrin.h>
|
|
423
|
+
#else
|
|
424
|
+
#if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__) || defined(__SSE__)
|
|
425
|
+
#if !defined(__riscv)
|
|
426
|
+
#include <immintrin.h>
|
|
427
|
+
#endif
|
|
428
|
+
#endif
|
|
429
|
+
#endif
|
|
430
|
+
#endif
|
|
431
|
+
#endif
|
|
432
|
+
|
|
433
|
+
#ifdef __riscv_v_intrinsic
|
|
434
|
+
#include <riscv_vector.h>
|
|
435
|
+
#endif
|
|
436
|
+
|
|
437
|
+
#if defined(__loongarch64)
|
|
438
|
+
#if defined(__loongarch_asx)
|
|
439
|
+
#include <lasxintrin.h>
|
|
440
|
+
#endif
|
|
441
|
+
#if defined(__loongarch_sx)
|
|
442
|
+
#include <lsxintrin.h>
|
|
443
|
+
#endif
|
|
444
|
+
#endif
|
|
445
|
+
|
|
446
|
+
#if defined(__loongarch_asx)
|
|
447
|
+
|
|
448
|
+
typedef union {
|
|
449
|
+
int32_t i;
|
|
450
|
+
float f;
|
|
451
|
+
} ft_union;
|
|
452
|
+
|
|
453
|
+
/* float type data load instructions */
|
|
454
|
+
static __m128 __lsx_vreplfr2vr_s(float val) {
|
|
455
|
+
ft_union fi_tmpval = {.f = val};
|
|
456
|
+
return (__m128)__lsx_vreplgr2vr_w(fi_tmpval.i);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
static __m256 __lasx_xvreplfr2vr_s(float val) {
|
|
460
|
+
ft_union fi_tmpval = {.f = val};
|
|
461
|
+
return (__m256)__lasx_xvreplgr2vr_w(fi_tmpval.i);
|
|
462
|
+
}
|
|
463
|
+
#endif
|
|
464
|
+
|
|
465
|
+
#ifdef __F16C__
|
|
466
|
+
|
|
467
|
+
#ifdef _MSC_VER
|
|
468
|
+
#define GGML_COMPUTE_FP16_TO_FP32(x) _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128(x)))
|
|
469
|
+
#define GGML_COMPUTE_FP32_TO_FP16(x) _mm_extract_epi16(_mm_cvtps_ph(_mm_set_ss(x), 0), 0)
|
|
470
|
+
#else
|
|
471
|
+
#define GGML_COMPUTE_FP16_TO_FP32(x) _cvtsh_ss(x)
|
|
472
|
+
#define GGML_COMPUTE_FP32_TO_FP16(x) _cvtss_sh(x, 0)
|
|
473
|
+
#endif
|
|
474
|
+
|
|
475
|
+
#elif defined(__POWER9_VECTOR__)
|
|
476
|
+
|
|
477
|
+
#define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
|
|
478
|
+
#define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
|
|
479
|
+
/* the inline asm below is about 12% faster than the lookup method */
|
|
480
|
+
#define GGML_FP16_TO_FP32(x) GGML_COMPUTE_FP16_TO_FP32(x)
|
|
481
|
+
#define GGML_FP32_TO_FP16(x) GGML_COMPUTE_FP32_TO_FP16(x)
|
|
482
|
+
|
|
483
|
+
static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
|
|
484
|
+
register float f;
|
|
485
|
+
register double d;
|
|
486
|
+
__asm__(
|
|
487
|
+
"mtfprd %0,%2\n"
|
|
488
|
+
"xscvhpdp %0,%0\n"
|
|
489
|
+
"frsp %1,%0\n" :
|
|
490
|
+
/* temp */ "=d"(d),
|
|
491
|
+
/* out */ "=f"(f):
|
|
492
|
+
/* in */ "r"(h));
|
|
493
|
+
return f;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
|
|
497
|
+
register double d;
|
|
498
|
+
register ggml_fp16_t r;
|
|
499
|
+
__asm__( /* xscvdphp can work on double or single precision */
|
|
500
|
+
"xscvdphp %0,%2\n"
|
|
501
|
+
"mffprd %1,%0\n" :
|
|
502
|
+
/* temp */ "=d"(d),
|
|
503
|
+
/* out */ "=r"(r):
|
|
504
|
+
/* in */ "f"(f));
|
|
505
|
+
return r;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
#else
|
|
509
|
+
|
|
510
|
+
// FP16 <-> FP32
|
|
511
|
+
// ref: https://github.com/Maratyszcza/FP16
|
|
512
|
+
|
|
513
|
+
static inline float fp32_from_bits(uint32_t w) {
|
|
514
|
+
union {
|
|
515
|
+
uint32_t as_bits;
|
|
516
|
+
float as_value;
|
|
517
|
+
} fp32;
|
|
518
|
+
fp32.as_bits = w;
|
|
519
|
+
return fp32.as_value;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
static inline uint32_t fp32_to_bits(float f) {
|
|
523
|
+
union {
|
|
524
|
+
float as_value;
|
|
525
|
+
uint32_t as_bits;
|
|
526
|
+
} fp32;
|
|
527
|
+
fp32.as_value = f;
|
|
528
|
+
return fp32.as_bits;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
|
|
532
|
+
const uint32_t w = (uint32_t) h << 16;
|
|
533
|
+
const uint32_t sign = w & UINT32_C(0x80000000);
|
|
534
|
+
const uint32_t two_w = w + w;
|
|
535
|
+
|
|
536
|
+
const uint32_t exp_offset = UINT32_C(0xE0) << 23;
|
|
537
|
+
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
|
|
538
|
+
const float exp_scale = 0x1.0p-112f;
|
|
539
|
+
#else
|
|
540
|
+
const float exp_scale = fp32_from_bits(UINT32_C(0x7800000));
|
|
541
|
+
#endif
|
|
542
|
+
const float normalized_value = fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale;
|
|
543
|
+
|
|
544
|
+
const uint32_t magic_mask = UINT32_C(126) << 23;
|
|
545
|
+
const float magic_bias = 0.5f;
|
|
546
|
+
const float denormalized_value = fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;
|
|
547
|
+
|
|
548
|
+
const uint32_t denormalized_cutoff = UINT32_C(1) << 27;
|
|
549
|
+
const uint32_t result = sign |
|
|
550
|
+
(two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value) : fp32_to_bits(normalized_value));
|
|
551
|
+
return fp32_from_bits(result);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
|
|
555
|
+
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
|
|
556
|
+
const float scale_to_inf = 0x1.0p+112f;
|
|
557
|
+
const float scale_to_zero = 0x1.0p-110f;
|
|
558
|
+
#else
|
|
559
|
+
const float scale_to_inf = fp32_from_bits(UINT32_C(0x77800000));
|
|
560
|
+
const float scale_to_zero = fp32_from_bits(UINT32_C(0x08800000));
|
|
561
|
+
#endif
|
|
562
|
+
float base = (fabsf(f) * scale_to_inf) * scale_to_zero;
|
|
563
|
+
|
|
564
|
+
const uint32_t w = fp32_to_bits(f);
|
|
565
|
+
const uint32_t shl1_w = w + w;
|
|
566
|
+
const uint32_t sign = w & UINT32_C(0x80000000);
|
|
567
|
+
uint32_t bias = shl1_w & UINT32_C(0xFF000000);
|
|
568
|
+
if (bias < UINT32_C(0x71000000)) {
|
|
569
|
+
bias = UINT32_C(0x71000000);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
base = fp32_from_bits((bias >> 1) + UINT32_C(0x07800000)) + base;
|
|
573
|
+
const uint32_t bits = fp32_to_bits(base);
|
|
574
|
+
const uint32_t exp_bits = (bits >> 13) & UINT32_C(0x00007C00);
|
|
575
|
+
const uint32_t mantissa_bits = bits & UINT32_C(0x00000FFF);
|
|
576
|
+
const uint32_t nonsign = exp_bits + mantissa_bits;
|
|
577
|
+
return (sign >> 16) | (shl1_w > UINT32_C(0xFF000000) ? UINT16_C(0x7E00) : nonsign);
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
#define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
|
|
581
|
+
#define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
|
|
582
|
+
|
|
583
|
+
#endif // __F16C__
|
|
584
|
+
|
|
585
|
+
#endif // defined(__ARM_NEON) && (!defined(__MSC_VER)
|
|
586
|
+
|
|
587
|
+
#ifdef __ARM_FEATURE_SVE
|
|
588
|
+
#include <arm_sve.h>
|
|
589
|
+
#endif // __ARM_FEATURE_SVE
|
|
590
|
+
|
|
591
|
+
// precomputed f32 table for f16 (256 KB)
|
|
592
|
+
// defined in ggml.c, initialized in ggml_init()
|
|
593
|
+
extern float ggml_table_f32_f16[1 << 16];
|
|
594
|
+
|
|
595
|
+
// On ARM NEON, it's quicker to directly convert x -> x instead of calling into ggml_lookup_fp16_to_fp32,
|
|
596
|
+
// so we define GGML_FP16_TO_FP32 and GGML_FP32_TO_FP16 elsewhere for NEON.
|
|
597
|
+
// This is also true for POWER9.
|
|
598
|
+
#if !defined(GGML_FP16_TO_FP32)
|
|
599
|
+
inline static float ggml_lookup_fp16_to_fp32(ggml_fp16_t f) {
|
|
600
|
+
uint16_t s;
|
|
601
|
+
memcpy(&s, &f, sizeof(uint16_t));
|
|
602
|
+
return ggml_table_f32_f16[s];
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
#define GGML_FP16_TO_FP32(x) ggml_lookup_fp16_to_fp32(x)
|
|
606
|
+
#endif
|
|
607
|
+
|
|
608
|
+
#if !defined(GGML_FP32_TO_FP16)
|
|
609
|
+
#define GGML_FP32_TO_FP16(x) GGML_COMPUTE_FP32_TO_FP16(x)
|
|
610
|
+
#endif
|
|
611
|
+
|
|
612
|
+
#ifdef __cplusplus
|
|
613
|
+
}
|
|
614
|
+
#endif
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <cuda_runtime.h>
|
|
4
|
+
#include <cuda.h>
|
|
5
|
+
#include <cublas_v2.h>
|
|
6
|
+
#include <cuda_fp16.h>
|
|
7
|
+
|
|
8
|
+
#if CUDART_VERSION < 11020
|
|
9
|
+
#define CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED CU_DEVICE_ATTRIBUTE_VIRTUAL_ADDRESS_MANAGEMENT_SUPPORTED
|
|
10
|
+
#define CUBLAS_TF32_TENSOR_OP_MATH CUBLAS_TENSOR_OP_MATH
|
|
11
|
+
#define CUBLAS_COMPUTE_16F CUDA_R_16F
|
|
12
|
+
#define CUBLAS_COMPUTE_32F CUDA_R_32F
|
|
13
|
+
#define cublasComputeType_t cudaDataType_t
|
|
14
|
+
#endif // CUDART_VERSION < 11020
|