@fugood/llama.node 0.0.1-alpha.1
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 +85 -0
- package/README.md +56 -0
- 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/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/lib/binding.js +13 -0
- package/lib/binding.ts +57 -0
- package/lib/index.js +24 -0
- package/lib/index.ts +13 -0
- package/package.json +65 -0
- package/src/addons.cpp +506 -0
- package/src/llama.cpp/CMakeLists.txt +1320 -0
- package/src/llama.cpp/build.zig +172 -0
- package/src/llama.cpp/cmake/FindSIMD.cmake +100 -0
- package/src/llama.cpp/common/CMakeLists.txt +87 -0
- package/src/llama.cpp/common/base64.hpp +392 -0
- package/src/llama.cpp/common/common.cpp +2949 -0
- package/src/llama.cpp/common/common.h +324 -0
- package/src/llama.cpp/common/console.cpp +501 -0
- package/src/llama.cpp/common/console.h +19 -0
- package/src/llama.cpp/common/grammar-parser.cpp +440 -0
- package/src/llama.cpp/common/grammar-parser.h +29 -0
- package/src/llama.cpp/common/json-schema-to-grammar.cpp +764 -0
- package/src/llama.cpp/common/json-schema-to-grammar.h +4 -0
- package/src/llama.cpp/common/json.hpp +24766 -0
- package/src/llama.cpp/common/log.h +724 -0
- package/src/llama.cpp/common/ngram-cache.cpp +282 -0
- package/src/llama.cpp/common/ngram-cache.h +94 -0
- package/src/llama.cpp/common/sampling.cpp +353 -0
- package/src/llama.cpp/common/sampling.h +147 -0
- package/src/llama.cpp/common/stb_image.h +8396 -0
- package/src/llama.cpp/common/train.cpp +1513 -0
- package/src/llama.cpp/common/train.h +233 -0
- package/src/llama.cpp/examples/CMakeLists.txt +52 -0
- package/src/llama.cpp/examples/baby-llama/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/baby-llama/baby-llama.cpp +1640 -0
- package/src/llama.cpp/examples/batched/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/batched/batched.cpp +262 -0
- package/src/llama.cpp/examples/batched-bench/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/batched-bench/batched-bench.cpp +261 -0
- package/src/llama.cpp/examples/beam-search/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/beam-search/beam-search.cpp +188 -0
- package/src/llama.cpp/examples/benchmark/CMakeLists.txt +6 -0
- package/src/llama.cpp/examples/benchmark/benchmark-matmult.cpp +275 -0
- package/src/llama.cpp/examples/convert-llama2c-to-ggml/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp +936 -0
- package/src/llama.cpp/examples/embedding/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/embedding/embedding.cpp +211 -0
- package/src/llama.cpp/examples/eval-callback/CMakeLists.txt +9 -0
- package/src/llama.cpp/examples/eval-callback/eval-callback.cpp +195 -0
- package/src/llama.cpp/examples/export-lora/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/export-lora/export-lora.cpp +462 -0
- package/src/llama.cpp/examples/finetune/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/finetune/finetune.cpp +1861 -0
- package/src/llama.cpp/examples/gbnf-validator/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/gbnf-validator/gbnf-validator.cpp +132 -0
- package/src/llama.cpp/examples/gguf/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/gguf/gguf.cpp +256 -0
- package/src/llama.cpp/examples/gguf-split/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/gguf-split/gguf-split.cpp +553 -0
- package/src/llama.cpp/examples/gritlm/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/gritlm/gritlm.cpp +215 -0
- package/src/llama.cpp/examples/imatrix/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/imatrix/imatrix.cpp +655 -0
- package/src/llama.cpp/examples/infill/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/infill/infill.cpp +767 -0
- package/src/llama.cpp/examples/jeopardy/questions.txt +100 -0
- package/src/llama.cpp/examples/llama-bench/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/llama-bench/llama-bench.cpp +1286 -0
- package/src/llama.cpp/examples/llama.android/app/src/main/cpp/CMakeLists.txt +50 -0
- package/src/llama.cpp/examples/llama.android/app/src/main/cpp/llama-android.cpp +443 -0
- package/src/llama.cpp/examples/llava/CMakeLists.txt +37 -0
- package/src/llama.cpp/examples/llava/clip.cpp +2027 -0
- package/src/llama.cpp/examples/llava/clip.h +85 -0
- package/src/llama.cpp/examples/llava/llava-cli.cpp +309 -0
- package/src/llama.cpp/examples/llava/llava.cpp +426 -0
- package/src/llama.cpp/examples/llava/llava.h +50 -0
- package/src/llama.cpp/examples/llava/requirements.txt +3 -0
- package/src/llama.cpp/examples/lookahead/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/lookahead/lookahead.cpp +485 -0
- package/src/llama.cpp/examples/lookup/CMakeLists.txt +23 -0
- package/src/llama.cpp/examples/lookup/lookup-create.cpp +41 -0
- package/src/llama.cpp/examples/lookup/lookup-merge.cpp +47 -0
- package/src/llama.cpp/examples/lookup/lookup-stats.cpp +160 -0
- package/src/llama.cpp/examples/lookup/lookup.cpp +258 -0
- package/src/llama.cpp/examples/main/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/main/main.cpp +957 -0
- package/src/llama.cpp/examples/main-cmake-pkg/CMakeLists.txt +33 -0
- package/src/llama.cpp/examples/parallel/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/parallel/parallel.cpp +427 -0
- package/src/llama.cpp/examples/passkey/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/passkey/passkey.cpp +302 -0
- package/src/llama.cpp/examples/perplexity/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/perplexity/perplexity.cpp +1943 -0
- package/src/llama.cpp/examples/quantize/CMakeLists.txt +6 -0
- package/src/llama.cpp/examples/quantize/quantize.cpp +423 -0
- package/src/llama.cpp/examples/quantize-stats/CMakeLists.txt +6 -0
- package/src/llama.cpp/examples/quantize-stats/quantize-stats.cpp +424 -0
- package/src/llama.cpp/examples/retrieval/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/retrieval/retrieval.cpp +350 -0
- package/src/llama.cpp/examples/save-load-state/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/save-load-state/save-load-state.cpp +246 -0
- package/src/llama.cpp/examples/server/CMakeLists.txt +40 -0
- package/src/llama.cpp/examples/server/bench/requirements.txt +2 -0
- package/src/llama.cpp/examples/server/httplib.h +9465 -0
- package/src/llama.cpp/examples/server/server.cpp +3826 -0
- package/src/llama.cpp/examples/server/tests/requirements.txt +6 -0
- package/src/llama.cpp/examples/server/utils.hpp +653 -0
- package/src/llama.cpp/examples/simple/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/simple/simple.cpp +183 -0
- package/src/llama.cpp/examples/speculative/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/speculative/speculative.cpp +614 -0
- package/src/llama.cpp/examples/sycl/CMakeLists.txt +9 -0
- package/src/llama.cpp/examples/sycl/ls-sycl-device.cpp +13 -0
- package/src/llama.cpp/examples/tokenize/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/tokenize/tokenize.cpp +42 -0
- package/src/llama.cpp/examples/train-text-from-scratch/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/train-text-from-scratch/train-text-from-scratch.cpp +1252 -0
- package/src/llama.cpp/ggml-alloc.c +985 -0
- package/src/llama.cpp/ggml-alloc.h +76 -0
- package/src/llama.cpp/ggml-backend-impl.h +141 -0
- package/src/llama.cpp/ggml-backend.c +2099 -0
- package/src/llama.cpp/ggml-backend.h +233 -0
- package/src/llama.cpp/ggml-common.h +1853 -0
- package/src/llama.cpp/ggml-cuda.h +43 -0
- package/src/llama.cpp/ggml-impl.h +265 -0
- package/src/llama.cpp/ggml-kompute.cpp +2006 -0
- package/src/llama.cpp/ggml-kompute.h +46 -0
- package/src/llama.cpp/ggml-metal.h +66 -0
- package/src/llama.cpp/ggml-mpi.c +216 -0
- package/src/llama.cpp/ggml-mpi.h +39 -0
- package/src/llama.cpp/ggml-opencl.cpp +2301 -0
- package/src/llama.cpp/ggml-opencl.h +36 -0
- package/src/llama.cpp/ggml-quants.c +12678 -0
- package/src/llama.cpp/ggml-quants.h +133 -0
- package/src/llama.cpp/ggml-sycl.cpp +17882 -0
- package/src/llama.cpp/ggml-sycl.h +49 -0
- package/src/llama.cpp/ggml-vulkan-shaders.hpp +69849 -0
- package/src/llama.cpp/ggml-vulkan.cpp +6442 -0
- package/src/llama.cpp/ggml-vulkan.h +29 -0
- package/src/llama.cpp/ggml.c +21819 -0
- package/src/llama.cpp/ggml.h +2403 -0
- package/src/llama.cpp/llama.cpp +17468 -0
- package/src/llama.cpp/llama.h +1117 -0
- package/src/llama.cpp/pocs/CMakeLists.txt +12 -0
- package/src/llama.cpp/pocs/vdot/CMakeLists.txt +9 -0
- package/src/llama.cpp/pocs/vdot/q8dot.cpp +172 -0
- package/src/llama.cpp/pocs/vdot/vdot.cpp +310 -0
- package/src/llama.cpp/prompts/LLM-questions.txt +49 -0
- package/src/llama.cpp/prompts/alpaca.txt +1 -0
- package/src/llama.cpp/prompts/assistant.txt +31 -0
- package/src/llama.cpp/prompts/chat-with-baichuan.txt +4 -0
- package/src/llama.cpp/prompts/chat-with-bob.txt +7 -0
- package/src/llama.cpp/prompts/chat-with-qwen.txt +1 -0
- package/src/llama.cpp/prompts/chat-with-vicuna-v0.txt +7 -0
- package/src/llama.cpp/prompts/chat-with-vicuna-v1.txt +7 -0
- package/src/llama.cpp/prompts/chat.txt +28 -0
- package/src/llama.cpp/prompts/dan-modified.txt +1 -0
- package/src/llama.cpp/prompts/dan.txt +1 -0
- package/src/llama.cpp/prompts/mnemonics.txt +93 -0
- package/src/llama.cpp/prompts/parallel-questions.txt +43 -0
- package/src/llama.cpp/prompts/reason-act.txt +18 -0
- package/src/llama.cpp/requirements/requirements-convert-hf-to-gguf.txt +3 -0
- package/src/llama.cpp/requirements/requirements-convert-llama-ggml-to-gguf.txt +1 -0
- package/src/llama.cpp/requirements/requirements-convert-lora-to-ggml.txt +2 -0
- package/src/llama.cpp/requirements/requirements-convert-persimmon-to-gguf.txt +2 -0
- package/src/llama.cpp/requirements/requirements-convert.txt +5 -0
- package/src/llama.cpp/requirements.txt +12 -0
- package/src/llama.cpp/scripts/gen-build-info-cpp.cmake +24 -0
- package/src/llama.cpp/scripts/xxd.cmake +16 -0
- package/src/llama.cpp/sgemm.cpp +999 -0
- package/src/llama.cpp/sgemm.h +12 -0
- package/src/llama.cpp/tests/CMakeLists.txt +78 -0
- package/src/llama.cpp/tests/get-model.cpp +21 -0
- package/src/llama.cpp/tests/get-model.h +2 -0
- package/src/llama.cpp/tests/test-autorelease.cpp +24 -0
- package/src/llama.cpp/tests/test-backend-ops.cpp +2266 -0
- package/src/llama.cpp/tests/test-c.c +7 -0
- package/src/llama.cpp/tests/test-chat-template.cpp +107 -0
- package/src/llama.cpp/tests/test-double-float.cpp +57 -0
- package/src/llama.cpp/tests/test-grad0.cpp +1606 -0
- package/src/llama.cpp/tests/test-grammar-integration.cpp +243 -0
- package/src/llama.cpp/tests/test-grammar-parser.cpp +250 -0
- package/src/llama.cpp/tests/test-json-schema-to-grammar.cpp +899 -0
- package/src/llama.cpp/tests/test-llama-grammar.cpp +402 -0
- package/src/llama.cpp/tests/test-model-load-cancel.cpp +27 -0
- package/src/llama.cpp/tests/test-opt.cpp +181 -0
- package/src/llama.cpp/tests/test-quantize-fns.cpp +185 -0
- package/src/llama.cpp/tests/test-quantize-perf.cpp +363 -0
- package/src/llama.cpp/tests/test-rope.cpp +221 -0
- package/src/llama.cpp/tests/test-sampling.cpp +301 -0
- package/src/llama.cpp/tests/test-tokenizer-0-falcon.cpp +187 -0
- package/src/llama.cpp/tests/test-tokenizer-0-llama.cpp +190 -0
- package/src/llama.cpp/tests/test-tokenizer-1-bpe.cpp +123 -0
- package/src/llama.cpp/tests/test-tokenizer-1-llama.cpp +111 -0
- package/src/llama.cpp/unicode-data.cpp +1651 -0
- package/src/llama.cpp/unicode-data.h +16 -0
- package/src/llama.cpp/unicode.cpp +277 -0
- package/src/llama.cpp/unicode.h +28 -0
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
// Unit tests for quantization specific functions - quantize, dequantize and dot product
|
|
2
|
+
|
|
3
|
+
#include "ggml.h"
|
|
4
|
+
|
|
5
|
+
#undef NDEBUG
|
|
6
|
+
#include <assert.h>
|
|
7
|
+
#include <math.h>
|
|
8
|
+
#include <stdio.h>
|
|
9
|
+
#include <string>
|
|
10
|
+
#include <vector>
|
|
11
|
+
|
|
12
|
+
#if defined(_MSC_VER)
|
|
13
|
+
#pragma warning(disable: 4244 4267) // possible loss of data
|
|
14
|
+
#endif
|
|
15
|
+
|
|
16
|
+
constexpr float MAX_QUANTIZATION_REFERENCE_ERROR = 0.0001f;
|
|
17
|
+
constexpr float MAX_QUANTIZATION_TOTAL_ERROR = 0.002f;
|
|
18
|
+
constexpr float MAX_QUANTIZATION_TOTAL_ERROR_2BITS = 0.0075f;
|
|
19
|
+
constexpr float MAX_QUANTIZATION_TOTAL_ERROR_3BITS = 0.0040f;
|
|
20
|
+
constexpr float MAX_QUANTIZATION_TOTAL_ERROR_3BITS_XXS = 0.0050f;
|
|
21
|
+
constexpr float MAX_DOT_PRODUCT_ERROR = 0.02f;
|
|
22
|
+
constexpr float MAX_DOT_PRODUCT_ERROR_LOWBIT = 0.04f;
|
|
23
|
+
|
|
24
|
+
static const char* RESULT_STR[] = {"ok", "FAILED"};
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
// Generate synthetic data
|
|
28
|
+
static void generate_data(float offset, size_t n, float * dst) {
|
|
29
|
+
for (size_t i = 0; i < n; i++) {
|
|
30
|
+
dst[i] = 0.1 + 2*cosf(i + offset);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Calculate RMSE between two float arrays
|
|
35
|
+
static float array_rmse(const float * a1, const float * a2, size_t n) {
|
|
36
|
+
double sum = 0;
|
|
37
|
+
for (size_t i = 0; i < n; i++) {
|
|
38
|
+
double diff = a1[i] - a2[i];
|
|
39
|
+
sum += diff * diff;
|
|
40
|
+
}
|
|
41
|
+
return sqrtf(sum) / n;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Total quantization error on test data
|
|
45
|
+
static float total_quantization_error(ggml_type_traits_t & qfns, size_t test_size, const float * test_data) {
|
|
46
|
+
std::vector<uint8_t> tmp_q(2*test_size);
|
|
47
|
+
std::vector<float> tmp_out(test_size);
|
|
48
|
+
|
|
49
|
+
qfns.from_float(test_data, tmp_q.data(), test_size);
|
|
50
|
+
qfns.to_float(tmp_q.data(), tmp_out.data(), test_size);
|
|
51
|
+
return array_rmse(test_data, tmp_out.data(), test_size);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Total quantization error on test data
|
|
55
|
+
static float reference_quantization_error(ggml_type_traits_t & qfns, size_t test_size, const float * test_data) {
|
|
56
|
+
std::vector<uint8_t> tmp_q(2*test_size);
|
|
57
|
+
std::vector<float> tmp_out(test_size);
|
|
58
|
+
std::vector<float> tmp_out_ref(test_size);
|
|
59
|
+
|
|
60
|
+
qfns.from_float(test_data, tmp_q.data(), test_size);
|
|
61
|
+
qfns.to_float(tmp_q.data(), tmp_out.data(), test_size);
|
|
62
|
+
|
|
63
|
+
qfns.from_float_reference(test_data, tmp_q.data(), test_size);
|
|
64
|
+
qfns.to_float(tmp_q.data(), tmp_out_ref.data(), test_size);
|
|
65
|
+
|
|
66
|
+
return array_rmse(tmp_out.data(), tmp_out_ref.data(), test_size);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
static float dot_product(const float * a1, const float * a2, size_t test_size) {
|
|
70
|
+
double sum = 0;
|
|
71
|
+
for (size_t i = 0; i < test_size; i++) {
|
|
72
|
+
sum += a1[i] * a2[i];
|
|
73
|
+
}
|
|
74
|
+
return sum;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Total dot product error
|
|
78
|
+
static float dot_product_error(
|
|
79
|
+
ggml_type_traits_t & qfns, size_t test_size, const float * test_data1, const float *test_data2
|
|
80
|
+
) {
|
|
81
|
+
std::vector<uint8_t> tmp_q1(2*test_size);
|
|
82
|
+
std::vector<uint8_t> tmp_q2(2*test_size);
|
|
83
|
+
|
|
84
|
+
auto vdot = ggml_internal_get_type_traits(qfns.vec_dot_type);
|
|
85
|
+
|
|
86
|
+
qfns.from_float(test_data1, tmp_q1.data(), test_size);
|
|
87
|
+
vdot.from_float(test_data2, tmp_q2.data(), test_size);
|
|
88
|
+
|
|
89
|
+
float result = INFINITY;
|
|
90
|
+
qfns.vec_dot(test_size, &result, 0, tmp_q1.data(), 0, tmp_q2.data(), 0, 1);
|
|
91
|
+
|
|
92
|
+
const float dot_ref = dot_product(test_data1, test_data2, test_size);
|
|
93
|
+
|
|
94
|
+
return fabsf(result - dot_ref) / test_size;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
int main(int argc, char * argv[]) {
|
|
98
|
+
bool verbose = false;
|
|
99
|
+
const size_t test_size = 32 * 128;
|
|
100
|
+
|
|
101
|
+
std::string arg;
|
|
102
|
+
for (int i = 1; i < argc; i++) {
|
|
103
|
+
arg = argv[i];
|
|
104
|
+
|
|
105
|
+
if (arg == "-v") {
|
|
106
|
+
verbose = true;
|
|
107
|
+
} else {
|
|
108
|
+
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
|
|
109
|
+
return 1;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
std::vector<float> test_data(test_size);
|
|
114
|
+
std::vector<float> test_data2(test_size);
|
|
115
|
+
|
|
116
|
+
generate_data(0.0, test_data.size(), test_data.data());
|
|
117
|
+
generate_data(1.0, test_data2.size(), test_data2.data());
|
|
118
|
+
|
|
119
|
+
// Initialize GGML, ensures float conversion tables are initialized
|
|
120
|
+
struct ggml_init_params ggml_params = {
|
|
121
|
+
/* .mem_size = */ 1*1024,
|
|
122
|
+
/* .mem_buffer = */ NULL,
|
|
123
|
+
/* .no_alloc = */ true,
|
|
124
|
+
};
|
|
125
|
+
struct ggml_context * ctx = ggml_init(ggml_params);
|
|
126
|
+
|
|
127
|
+
int num_failed = 0;
|
|
128
|
+
bool failed = false;
|
|
129
|
+
|
|
130
|
+
for (int i = 0; i < GGML_TYPE_COUNT; i++) {
|
|
131
|
+
ggml_type type = (ggml_type) i;
|
|
132
|
+
ggml_type_traits_t qfns = ggml_internal_get_type_traits(type);
|
|
133
|
+
|
|
134
|
+
// deprecated - skip
|
|
135
|
+
if (qfns.blck_size == 0) {
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const ggml_type ei = (ggml_type)i;
|
|
140
|
+
|
|
141
|
+
printf("Testing %s\n", ggml_type_name((ggml_type) i));
|
|
142
|
+
ggml_quantize_init(ei);
|
|
143
|
+
|
|
144
|
+
if (qfns.from_float && qfns.to_float) {
|
|
145
|
+
const float total_error = total_quantization_error(qfns, test_size, test_data.data());
|
|
146
|
+
const float max_quantization_error =
|
|
147
|
+
type == GGML_TYPE_Q2_K ? MAX_QUANTIZATION_TOTAL_ERROR_2BITS :
|
|
148
|
+
type == GGML_TYPE_IQ2_S ? MAX_QUANTIZATION_TOTAL_ERROR_2BITS :
|
|
149
|
+
type == GGML_TYPE_Q3_K ? MAX_QUANTIZATION_TOTAL_ERROR_3BITS :
|
|
150
|
+
type == GGML_TYPE_IQ3_S ? MAX_QUANTIZATION_TOTAL_ERROR_3BITS :
|
|
151
|
+
type == GGML_TYPE_IQ3_XXS ? MAX_QUANTIZATION_TOTAL_ERROR_3BITS_XXS : MAX_QUANTIZATION_TOTAL_ERROR;
|
|
152
|
+
failed = !(total_error < max_quantization_error);
|
|
153
|
+
num_failed += failed;
|
|
154
|
+
if (failed || verbose) {
|
|
155
|
+
printf("%5s absolute quantization error: %s (%f)\n", ggml_type_name(type), RESULT_STR[failed], total_error);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const float reference_error = reference_quantization_error(qfns, test_size, test_data.data());
|
|
159
|
+
failed = !(reference_error < MAX_QUANTIZATION_REFERENCE_ERROR);
|
|
160
|
+
num_failed += failed;
|
|
161
|
+
if (failed || verbose) {
|
|
162
|
+
printf("%5s reference implementation error: %s (%f)\n", ggml_type_name(type), RESULT_STR[failed], reference_error);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const float vec_dot_error = dot_product_error(qfns, test_size, test_data.data(), test_data2.data());
|
|
166
|
+
const float max_allowed_error = type == GGML_TYPE_Q2_K || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ2_XXS ||
|
|
167
|
+
type == GGML_TYPE_IQ3_XXS || type == GGML_TYPE_IQ3_S || type == GGML_TYPE_IQ2_S
|
|
168
|
+
? MAX_DOT_PRODUCT_ERROR_LOWBIT
|
|
169
|
+
: MAX_DOT_PRODUCT_ERROR;
|
|
170
|
+
failed = !(vec_dot_error < max_allowed_error);
|
|
171
|
+
num_failed += failed;
|
|
172
|
+
if (failed || verbose) {
|
|
173
|
+
printf("%5s dot product error: %s (%f)\n", ggml_type_name(type), RESULT_STR[failed], vec_dot_error);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (num_failed || verbose) {
|
|
179
|
+
printf("%d tests failed\n", num_failed);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
ggml_free(ctx);
|
|
183
|
+
|
|
184
|
+
return num_failed > 0;
|
|
185
|
+
}
|
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
// Benchmark quantization specific functions on synthetic data
|
|
2
|
+
|
|
3
|
+
#include "ggml.h"
|
|
4
|
+
|
|
5
|
+
#undef NDEBUG
|
|
6
|
+
#include <algorithm>
|
|
7
|
+
#include <assert.h>
|
|
8
|
+
#include <functional>
|
|
9
|
+
#include <inttypes.h>
|
|
10
|
+
#include <math.h>
|
|
11
|
+
#include <memory>
|
|
12
|
+
#include <stdio.h>
|
|
13
|
+
#include <string>
|
|
14
|
+
#include <vector>
|
|
15
|
+
|
|
16
|
+
#if defined(_MSC_VER)
|
|
17
|
+
#pragma warning(disable: 4244 4267) // possible loss of data
|
|
18
|
+
#endif
|
|
19
|
+
|
|
20
|
+
#define MAX_ALIGNMENT 64
|
|
21
|
+
#define QK 32
|
|
22
|
+
#define WARMUP 5
|
|
23
|
+
#define ITERATIONS 10
|
|
24
|
+
#define MAX_ITERATIONS 100000000
|
|
25
|
+
|
|
26
|
+
#define L1_SIZE 32*128
|
|
27
|
+
#define L2_SIZE 32*2048
|
|
28
|
+
#define L3_SIZE 32*20480
|
|
29
|
+
#define MEM_SIZE 32*2048000
|
|
30
|
+
|
|
31
|
+
struct quantize_perf_params {
|
|
32
|
+
std::vector<std::string> include_types;
|
|
33
|
+
std::vector<size_t> test_sizes;
|
|
34
|
+
size_t alignment_offset = 0;
|
|
35
|
+
bool op_quantize_row_q_reference = false;
|
|
36
|
+
bool op_quantize_row_q = false;
|
|
37
|
+
bool op_dequantize_row_q = false;
|
|
38
|
+
bool op_quantize_row_q_dot = false;
|
|
39
|
+
bool op_vec_dot_q = false;
|
|
40
|
+
int64_t iterations = ITERATIONS;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
#if defined(__x86_64__) || defined(__i386__)
|
|
44
|
+
|
|
45
|
+
#include <x86intrin.h>
|
|
46
|
+
inline int64_t cpu_cycles() {
|
|
47
|
+
// Rough way to detect new-ish CPUs
|
|
48
|
+
#ifdef __POPCNT__
|
|
49
|
+
unsigned int dummy;
|
|
50
|
+
return __rdtscp(&dummy);
|
|
51
|
+
#else
|
|
52
|
+
return __rdtsc();
|
|
53
|
+
#endif
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
#else
|
|
57
|
+
|
|
58
|
+
#define cpu_cycles() 0
|
|
59
|
+
|
|
60
|
+
#endif
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
// Generate synthetic data
|
|
64
|
+
static void generate_data(float offset, size_t n, float * dst) {
|
|
65
|
+
for (size_t i = 0; i < n; i++) {
|
|
66
|
+
dst[i] = 0.1 + 2*cosf(i + offset);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
static float gigabytes_per_second(size_t bytes, int64_t usecs) {
|
|
71
|
+
return bytes / (float) usecs * 1000000 / (1024*1024*1024);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
static void * align_with_offset(void * ptr, int offset) {
|
|
75
|
+
size_t dummy_size = MAX_ALIGNMENT * 4;
|
|
76
|
+
return (char *) std::align(MAX_ALIGNMENT, MAX_ALIGNMENT, ptr, dummy_size) + offset;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static void benchmark_function(size_t size, size_t q_size, int64_t iterations, const std::function<float(void)> & func) {
|
|
80
|
+
int64_t min_time_us = INT64_MAX;
|
|
81
|
+
int64_t total_time_us = 0;
|
|
82
|
+
int64_t min_time_cycles = INT64_MAX;
|
|
83
|
+
int64_t total_time_cycles = 0;
|
|
84
|
+
|
|
85
|
+
for (int i = 0; i < WARMUP; i++) {
|
|
86
|
+
func();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
for (int i = 0; i < iterations; i++) {
|
|
90
|
+
const int64_t start_time = ggml_time_us();
|
|
91
|
+
const int64_t start_cycles = cpu_cycles();
|
|
92
|
+
|
|
93
|
+
func();
|
|
94
|
+
|
|
95
|
+
const int64_t end_cycles = cpu_cycles();
|
|
96
|
+
const int64_t end_time = ggml_time_us();
|
|
97
|
+
|
|
98
|
+
total_time_cycles += end_cycles - start_cycles;
|
|
99
|
+
min_time_cycles = std::min(min_time_cycles, end_cycles - start_cycles);
|
|
100
|
+
total_time_us += end_time - start_time;
|
|
101
|
+
min_time_us = std::min(min_time_us, end_time - start_time);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
printf(" min cycles/%d vals : %9.2f\n", QK, QK * min_time_cycles / (float) size);
|
|
105
|
+
printf(" avg cycles/%d vals : %9.2f\n", QK, QK * total_time_cycles / (float) (size * iterations));
|
|
106
|
+
printf(" float32 throughput : %9.2f GB/s\n", gigabytes_per_second(4 * size * iterations, total_time_us));
|
|
107
|
+
printf(" quantized throughput : %9.2f GB/s\n", gigabytes_per_second(q_size * iterations, total_time_us));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
static void usage(char * argv[]) {
|
|
111
|
+
printf("Benchmark quantization specific functions on synthetic data\n");
|
|
112
|
+
printf("\n");
|
|
113
|
+
printf("usage: %s [options]\n", argv[0]);
|
|
114
|
+
printf("\n");
|
|
115
|
+
printf("options: (default)\n");
|
|
116
|
+
printf(" -h, --help show this help message and exit\n");
|
|
117
|
+
printf(" --size SIZE set test size, divisible by 32 (L1_SIZE:%d)\n", L1_SIZE);
|
|
118
|
+
printf(" -3 use size as L1, L2, L3 sizes (L1:%d L2:%d L3:%d)\n", L1_SIZE, L2_SIZE, L3_SIZE);
|
|
119
|
+
printf(" -4 use size as L1, L2, L3, MEM sizes (L1:%d L2:%d L3:%d MEM:%d)\n", L1_SIZE, L2_SIZE, L3_SIZE, MEM_SIZE);
|
|
120
|
+
printf(" --op OP set test operation as quantize_row_q_reference, quantize_row_q, dequantize_row_q,\n");
|
|
121
|
+
printf(" quantize_row_q_dot, vec_dot_q (all)\n");
|
|
122
|
+
printf(" --type TYPE set test type as");
|
|
123
|
+
for (int i = 0; i < GGML_TYPE_COUNT; i++) {
|
|
124
|
+
ggml_type type = (ggml_type) i;
|
|
125
|
+
ggml_type_traits_t qfns = ggml_internal_get_type_traits(type);
|
|
126
|
+
if (ggml_type_name(type) != NULL) {
|
|
127
|
+
if (qfns.from_float && qfns.to_float) {
|
|
128
|
+
printf(" %s", ggml_type_name(type));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
printf(" (all)\n");
|
|
133
|
+
printf(" --alignment-offset OFFSET\n");
|
|
134
|
+
printf(" set alignment offset as OFFSET (0)\n");
|
|
135
|
+
printf(" -i NUM, --iterations NUM\n");
|
|
136
|
+
printf(" set test iteration number (%d)\n", ITERATIONS);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
int main(int argc, char * argv[]) {
|
|
140
|
+
quantize_perf_params params {};
|
|
141
|
+
|
|
142
|
+
// read command line
|
|
143
|
+
|
|
144
|
+
bool invalid_param = false;
|
|
145
|
+
std::string arg;
|
|
146
|
+
for (int i = 1; i < argc; i++) {
|
|
147
|
+
arg = argv[i];
|
|
148
|
+
|
|
149
|
+
if (arg == "--size") {
|
|
150
|
+
if (++i >= argc) {
|
|
151
|
+
invalid_param = true;
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
size_t size = std::stoi(argv[i]);
|
|
155
|
+
if (size % 32 != 0) {
|
|
156
|
+
fprintf(stderr, "error: size %zu not divisible by 32\n", size);
|
|
157
|
+
invalid_param = true;
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
params.test_sizes.push_back(size);
|
|
161
|
+
} else if (arg == "-3") {
|
|
162
|
+
// quick select sizes that probably fit in CPU caches
|
|
163
|
+
params.test_sizes.push_back(L1_SIZE);
|
|
164
|
+
params.test_sizes.push_back(L2_SIZE);
|
|
165
|
+
params.test_sizes.push_back(L3_SIZE);
|
|
166
|
+
} else if (arg == "-4") {
|
|
167
|
+
// quick select cache sizes + memory
|
|
168
|
+
params.test_sizes.push_back(L1_SIZE);
|
|
169
|
+
params.test_sizes.push_back(L2_SIZE);
|
|
170
|
+
params.test_sizes.push_back(L3_SIZE);
|
|
171
|
+
params.test_sizes.push_back(MEM_SIZE);
|
|
172
|
+
} else if (arg == "--op") {
|
|
173
|
+
if (++i >= argc) {
|
|
174
|
+
invalid_param = true;
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
std::string op {argv[i]};
|
|
178
|
+
if (op == "quantize_row_q_reference") {
|
|
179
|
+
params.op_quantize_row_q_reference = true;
|
|
180
|
+
} else if (op == "quantize_row_q") {
|
|
181
|
+
params.op_quantize_row_q = true;
|
|
182
|
+
} else if (op == "dequantize_row_q") {
|
|
183
|
+
params.op_dequantize_row_q = true;
|
|
184
|
+
} else if (op == "quantize_row_q_dot") {
|
|
185
|
+
params.op_quantize_row_q_dot = true;
|
|
186
|
+
} else if (op == "vec_dot_q") {
|
|
187
|
+
params.op_vec_dot_q = true;
|
|
188
|
+
} else {
|
|
189
|
+
invalid_param = true;
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
} else if (arg == "--type") {
|
|
193
|
+
if (++i >= argc) {
|
|
194
|
+
invalid_param = true;
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
params.include_types.push_back(argv[i]);
|
|
198
|
+
} else if (arg == "--alignment-offset") {
|
|
199
|
+
if (++i >= argc) {
|
|
200
|
+
invalid_param = true;
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
int alignment = std::stoi(argv[i]);
|
|
204
|
+
if (alignment < 0 || alignment > MAX_ALIGNMENT) {
|
|
205
|
+
fprintf(stderr, "error: alignment-offset must be less than %d\n", MAX_ALIGNMENT);
|
|
206
|
+
invalid_param = true;
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
params.alignment_offset = alignment;
|
|
210
|
+
} else if ((arg == "-i") || (arg == "--iterations")) {
|
|
211
|
+
if (++i >= argc) {
|
|
212
|
+
invalid_param = true;
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
int number = std::stoi(argv[i]);
|
|
216
|
+
if (number < 0 || number > MAX_ITERATIONS) {
|
|
217
|
+
fprintf(stderr, "error: iterations must be less than %d\n", MAX_ITERATIONS);
|
|
218
|
+
invalid_param = true;
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
params.iterations = number;
|
|
222
|
+
} else if ((arg == "-h") || (arg == "--help")) {
|
|
223
|
+
usage(argv);
|
|
224
|
+
return 1;
|
|
225
|
+
} else {
|
|
226
|
+
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
|
|
227
|
+
return 1;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
if (invalid_param) {
|
|
231
|
+
fprintf(stderr, "error: invalid parameter for argument: %s\n", arg.c_str());
|
|
232
|
+
return 1;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (params.test_sizes.empty()) {
|
|
236
|
+
params.test_sizes.push_back(L1_SIZE);
|
|
237
|
+
}
|
|
238
|
+
if (!(params.op_quantize_row_q_reference || params.op_quantize_row_q || params.op_dequantize_row_q || params.op_quantize_row_q_dot || params.op_vec_dot_q)) {
|
|
239
|
+
params.op_quantize_row_q_reference = params.op_quantize_row_q = params.op_dequantize_row_q = params.op_quantize_row_q_dot = params.op_vec_dot_q = true;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
std::sort(params.test_sizes.begin(), params.test_sizes.end());
|
|
243
|
+
size_t largest = params.test_sizes.back();
|
|
244
|
+
|
|
245
|
+
std::vector<uint8_t> test_data1_v(largest*4 + MAX_ALIGNMENT*2);
|
|
246
|
+
std::vector<uint8_t> test_data2_v(largest*4 + MAX_ALIGNMENT*2);
|
|
247
|
+
std::vector<uint8_t> test_q1_v (largest*4 + MAX_ALIGNMENT*2);
|
|
248
|
+
std::vector<uint8_t> test_q2_v (largest*4 + MAX_ALIGNMENT*2);
|
|
249
|
+
std::vector<uint8_t> test_out_v (largest*4 + MAX_ALIGNMENT*2);
|
|
250
|
+
|
|
251
|
+
float * test_data1 = (float *) align_with_offset(test_data1_v.data(), params.alignment_offset);
|
|
252
|
+
float * test_data2 = (float *) align_with_offset(test_data2_v.data(), params.alignment_offset);
|
|
253
|
+
float * test_q1 = (float *) align_with_offset(test_q1_v.data(), params.alignment_offset);
|
|
254
|
+
float * test_q2 = (float *) align_with_offset(test_q2_v.data(), params.alignment_offset);
|
|
255
|
+
float * test_out = (float *) align_with_offset(test_out_v.data(), params.alignment_offset);
|
|
256
|
+
|
|
257
|
+
generate_data(0, largest, test_data1);
|
|
258
|
+
generate_data(1, largest, test_data2);
|
|
259
|
+
|
|
260
|
+
int64_t iterations = params.iterations;
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
// Initialize GGML, ensures float conversion tables are initialized
|
|
264
|
+
struct ggml_init_params ggml_params = {
|
|
265
|
+
/* .mem_size = */ 1*1024,
|
|
266
|
+
/* .mem_buffer = */ NULL,
|
|
267
|
+
/* .no_alloc = */ true,
|
|
268
|
+
};
|
|
269
|
+
struct ggml_context * ctx = ggml_init(ggml_params);
|
|
270
|
+
|
|
271
|
+
for (int i = 0; i < GGML_TYPE_COUNT; i++) {
|
|
272
|
+
ggml_type type = (ggml_type) i;
|
|
273
|
+
ggml_type_traits_t qfns = ggml_internal_get_type_traits(type);
|
|
274
|
+
if (!params.include_types.empty() && ggml_type_name(type) && std::find(params.include_types.begin(), params.include_types.end(), ggml_type_name(type)) == params.include_types.end()) {
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (qfns.from_float && qfns.to_float) {
|
|
279
|
+
printf("%s\n", ggml_type_name(type));
|
|
280
|
+
|
|
281
|
+
ggml_quantize_init(type);
|
|
282
|
+
|
|
283
|
+
if (params.op_quantize_row_q_reference) {
|
|
284
|
+
printf(" quantize_row_q_reference\n");
|
|
285
|
+
for (size_t size : params.test_sizes) {
|
|
286
|
+
printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
|
|
287
|
+
auto quantize_fn = [&](void) -> float {
|
|
288
|
+
qfns.from_float_reference(test_data1, test_q1, size);
|
|
289
|
+
return test_q1[0];
|
|
290
|
+
};
|
|
291
|
+
size_t quantized_size = ggml_row_size(type, size);
|
|
292
|
+
benchmark_function(size, quantized_size, iterations, quantize_fn);
|
|
293
|
+
}
|
|
294
|
+
printf("\n");
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (params.op_quantize_row_q) {
|
|
298
|
+
printf(" quantize_row_q\n");
|
|
299
|
+
for (size_t size : params.test_sizes) {
|
|
300
|
+
printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
|
|
301
|
+
auto quantize_fn = [&](void) -> float {
|
|
302
|
+
qfns.from_float(test_data1, test_q1, size);
|
|
303
|
+
return test_q1[0];
|
|
304
|
+
};
|
|
305
|
+
size_t quantized_size = ggml_row_size(type, size);
|
|
306
|
+
benchmark_function(size, quantized_size, iterations, quantize_fn);
|
|
307
|
+
}
|
|
308
|
+
printf("\n");
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
if (params.op_dequantize_row_q) {
|
|
312
|
+
printf(" dequantize_row_q\n");
|
|
313
|
+
qfns.from_float(test_data1, test_q1, largest);
|
|
314
|
+
for (size_t size : params.test_sizes) {
|
|
315
|
+
printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
|
|
316
|
+
auto quantize_fn = [&](void) -> float {
|
|
317
|
+
qfns.to_float(test_q1, test_out, size);
|
|
318
|
+
return test_out[0];
|
|
319
|
+
};
|
|
320
|
+
size_t quantized_size = ggml_row_size(type, size);
|
|
321
|
+
benchmark_function(size, quantized_size, iterations, quantize_fn);
|
|
322
|
+
}
|
|
323
|
+
printf("\n");
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (params.op_quantize_row_q_dot) {
|
|
327
|
+
printf(" quantize_row_q_dot\n");
|
|
328
|
+
for (size_t size : params.test_sizes) {
|
|
329
|
+
printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
|
|
330
|
+
auto quantize_fn = [&](void) -> float {
|
|
331
|
+
auto vdot = ggml_internal_get_type_traits(qfns.vec_dot_type);
|
|
332
|
+
vdot.from_float(test_data1, test_q1, size);
|
|
333
|
+
return test_q1[0];
|
|
334
|
+
};
|
|
335
|
+
size_t quantized_size = ggml_row_size(type, size);
|
|
336
|
+
benchmark_function(size, quantized_size, iterations, quantize_fn);
|
|
337
|
+
}
|
|
338
|
+
printf("\n");
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (params.op_vec_dot_q) {
|
|
342
|
+
printf(" vec_dot_q\n");
|
|
343
|
+
qfns.from_float(test_data1, test_q1, largest);
|
|
344
|
+
qfns.from_float(test_data2, test_q2, largest);
|
|
345
|
+
for (size_t size : params.test_sizes) {
|
|
346
|
+
printf(" %zu values (%.2f MB)\n", size, 4*size/(float)(1024*1024));
|
|
347
|
+
auto quantize_fn = [&](void) -> float {
|
|
348
|
+
float result;
|
|
349
|
+
qfns.vec_dot(size, &result, 0, test_q1, 0, test_q2, 0, 1);
|
|
350
|
+
return result;
|
|
351
|
+
};
|
|
352
|
+
size_t quantized_size = ggml_row_size(type, size);
|
|
353
|
+
benchmark_function(size, quantized_size, iterations, quantize_fn);
|
|
354
|
+
}
|
|
355
|
+
printf("\n");
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
ggml_free(ctx);
|
|
361
|
+
|
|
362
|
+
return 0;
|
|
363
|
+
}
|