@fugood/llama.node 0.3.2 → 0.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.
- package/CMakeLists.txt +2 -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/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 +1 -1
- package/src/DetokenizeWorker.cpp +1 -1
- package/src/EmbeddingWorker.cpp +2 -2
- package/src/LlamaCompletionWorker.cpp +8 -8
- package/src/LlamaCompletionWorker.h +2 -2
- package/src/LlamaContext.cpp +8 -9
- package/src/TokenizeWorker.cpp +1 -1
- package/src/common.hpp +4 -4
- package/src/llama.cpp/.github/workflows/build.yml +43 -9
- package/src/llama.cpp/.github/workflows/docker.yml +3 -0
- package/src/llama.cpp/CMakeLists.txt +7 -4
- package/src/llama.cpp/cmake/arm64-apple-clang.cmake +16 -0
- package/src/llama.cpp/common/CMakeLists.txt +0 -2
- package/src/llama.cpp/common/arg.cpp +642 -607
- package/src/llama.cpp/common/arg.h +22 -22
- package/src/llama.cpp/common/common.cpp +79 -281
- package/src/llama.cpp/common/common.h +130 -100
- package/src/llama.cpp/common/json-schema-to-grammar.cpp +1 -1
- package/src/llama.cpp/common/log.cpp +50 -50
- package/src/llama.cpp/common/log.h +18 -18
- package/src/llama.cpp/common/ngram-cache.cpp +36 -36
- package/src/llama.cpp/common/ngram-cache.h +19 -19
- package/src/llama.cpp/common/sampling.cpp +116 -108
- package/src/llama.cpp/common/sampling.h +20 -20
- package/src/llama.cpp/docs/build.md +37 -17
- package/src/llama.cpp/examples/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/batched/batched.cpp +14 -14
- package/src/llama.cpp/examples/batched-bench/batched-bench.cpp +10 -11
- package/src/llama.cpp/examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp +1 -1
- package/src/llama.cpp/examples/cvector-generator/cvector-generator.cpp +9 -9
- package/src/llama.cpp/examples/embedding/embedding.cpp +12 -12
- package/src/llama.cpp/examples/eval-callback/eval-callback.cpp +8 -8
- package/src/llama.cpp/examples/export-lora/export-lora.cpp +5 -5
- package/src/llama.cpp/examples/gen-docs/gen-docs.cpp +7 -7
- package/src/llama.cpp/examples/gritlm/gritlm.cpp +18 -18
- package/src/llama.cpp/examples/imatrix/imatrix.cpp +20 -11
- package/src/llama.cpp/examples/infill/infill.cpp +40 -86
- package/src/llama.cpp/examples/llama-bench/llama-bench.cpp +42 -151
- package/src/llama.cpp/examples/llama.android/llama/build.gradle.kts +1 -0
- package/src/llama.cpp/examples/llama.android/llama/src/main/cpp/llama-android.cpp +11 -14
- package/src/llama.cpp/examples/llava/clip.cpp +1 -0
- package/src/llama.cpp/examples/llava/llava-cli.cpp +23 -23
- package/src/llama.cpp/examples/llava/llava.cpp +37 -3
- package/src/llama.cpp/examples/llava/minicpmv-cli.cpp +21 -21
- package/src/llama.cpp/examples/lookahead/lookahead.cpp +26 -26
- package/src/llama.cpp/examples/lookup/lookup-create.cpp +7 -7
- package/src/llama.cpp/examples/lookup/lookup-merge.cpp +4 -4
- package/src/llama.cpp/examples/lookup/lookup-stats.cpp +14 -14
- package/src/llama.cpp/examples/lookup/lookup.cpp +29 -29
- package/src/llama.cpp/examples/main/main.cpp +64 -109
- package/src/llama.cpp/examples/parallel/parallel.cpp +18 -19
- package/src/llama.cpp/examples/passkey/passkey.cpp +14 -14
- package/src/llama.cpp/examples/perplexity/perplexity.cpp +99 -120
- package/src/llama.cpp/examples/quantize-stats/quantize-stats.cpp +10 -9
- package/src/llama.cpp/examples/retrieval/retrieval.cpp +13 -13
- package/src/llama.cpp/examples/rpc/rpc-server.cpp +3 -1
- package/src/llama.cpp/examples/save-load-state/save-load-state.cpp +34 -17
- package/src/llama.cpp/examples/server/CMakeLists.txt +4 -13
- package/src/llama.cpp/examples/server/server.cpp +553 -691
- package/src/llama.cpp/examples/server/utils.hpp +312 -25
- package/src/llama.cpp/examples/simple/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/simple/simple.cpp +128 -96
- package/src/llama.cpp/examples/simple-chat/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/simple-chat/simple-chat.cpp +197 -0
- package/src/llama.cpp/examples/speculative/speculative.cpp +54 -51
- package/src/llama.cpp/examples/tokenize/tokenize.cpp +2 -2
- package/src/llama.cpp/ggml/CMakeLists.txt +15 -9
- package/src/llama.cpp/ggml/include/ggml-amx.h +25 -0
- package/src/llama.cpp/ggml/include/ggml-backend.h +46 -33
- package/src/llama.cpp/ggml/include/ggml-blas.h +5 -3
- package/src/llama.cpp/ggml/include/ggml-cann.h +9 -7
- package/src/llama.cpp/ggml/include/ggml-cpp.h +38 -0
- package/src/llama.cpp/ggml/include/ggml-cpu.h +177 -0
- package/src/llama.cpp/ggml/include/ggml-cuda.h +12 -12
- package/src/llama.cpp/ggml/include/ggml-kompute.h +7 -3
- package/src/llama.cpp/ggml/include/ggml-metal.h +11 -7
- package/src/llama.cpp/ggml/include/ggml-opt.h +216 -0
- package/src/llama.cpp/ggml/include/ggml-rpc.h +9 -5
- package/src/llama.cpp/ggml/include/ggml-sycl.h +18 -11
- package/src/llama.cpp/ggml/include/ggml-vulkan.h +10 -8
- package/src/llama.cpp/ggml/include/ggml.h +53 -393
- package/src/llama.cpp/ggml/src/CMakeLists.txt +66 -1149
- package/src/llama.cpp/ggml/src/ggml-aarch64.c +46 -3126
- package/src/llama.cpp/ggml/src/ggml-aarch64.h +0 -20
- package/src/llama.cpp/ggml/src/ggml-alloc.c +23 -27
- package/src/llama.cpp/ggml/src/ggml-amx/CMakeLists.txt +107 -0
- package/src/llama.cpp/ggml/src/ggml-amx/common.h +94 -0
- package/src/llama.cpp/ggml/src/ggml-amx/ggml-amx.cpp +446 -0
- package/src/llama.cpp/ggml/src/ggml-amx/mmq.cpp +2510 -0
- package/src/llama.cpp/ggml/src/ggml-amx/mmq.h +17 -0
- package/src/llama.cpp/ggml/src/ggml-backend-impl.h +6 -25
- package/src/llama.cpp/ggml/src/ggml-backend-reg.cpp +195 -0
- package/src/llama.cpp/ggml/src/ggml-backend.cpp +303 -864
- package/src/llama.cpp/ggml/src/ggml-blas/CMakeLists.txt +91 -0
- package/src/llama.cpp/ggml/src/{ggml-blas.cpp → ggml-blas/ggml-blas.cpp} +213 -65
- package/src/llama.cpp/ggml/src/ggml-cann/CMakeLists.txt +46 -0
- package/src/llama.cpp/ggml/src/{ggml-cann.cpp → ggml-cann/ggml-cann.cpp} +255 -149
- package/src/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +261 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.c +3560 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.h +30 -0
- package/src/llama.cpp/ggml/src/{ggml-cpu-impl.h → ggml-cpu/ggml-cpu-impl.h} +0 -243
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.c +10822 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.h +63 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +13970 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.cpp +663 -0
- package/src/llama.cpp/ggml/src/{llamafile → ggml-cpu/llamafile}/sgemm.cpp +667 -1
- package/src/llama.cpp/ggml/src/ggml-cuda/CMakeLists.txt +155 -0
- package/src/llama.cpp/ggml/src/ggml-hip/CMakeLists.txt +106 -0
- package/src/llama.cpp/ggml/src/ggml-impl.h +366 -16
- package/src/llama.cpp/ggml/src/ggml-kompute/CMakeLists.txt +162 -0
- package/src/llama.cpp/ggml/src/{ggml-kompute.cpp → ggml-kompute/ggml-kompute.cpp} +238 -72
- package/src/llama.cpp/ggml/src/ggml-metal/CMakeLists.txt +108 -0
- package/src/llama.cpp/ggml/src/ggml-metal/ggml-metal-impl.h +249 -0
- package/src/llama.cpp/ggml/src/ggml-musa/CMakeLists.txt +100 -0
- package/src/llama.cpp/ggml/src/ggml-opt.cpp +867 -0
- package/src/llama.cpp/ggml/src/ggml-quants.c +187 -10692
- package/src/llama.cpp/ggml/src/ggml-quants.h +78 -125
- package/src/llama.cpp/ggml/src/ggml-rpc/CMakeLists.txt +11 -0
- package/src/llama.cpp/ggml/src/{ggml-rpc.cpp → ggml-rpc/ggml-rpc.cpp} +475 -300
- package/src/llama.cpp/ggml/src/ggml-sycl/CMakeLists.txt +81 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/backend.hpp +3 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/common.cpp +40 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/common.hpp +258 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/concat.cpp +1 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/dpct/helper.hpp +2 -22
- package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.cpp +1011 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.hpp +76 -0
- package/src/llama.cpp/ggml/src/{ggml-sycl.cpp → ggml-sycl/ggml-sycl.cpp} +3584 -4142
- package/src/llama.cpp/ggml/src/ggml-sycl/mmvq.cpp +69 -67
- package/src/llama.cpp/ggml/src/ggml-sycl/norm.cpp +3 -3
- package/src/llama.cpp/ggml/src/ggml-sycl/outprod.cpp +56 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/outprod.hpp +11 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/presets.hpp +6 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/vecdotq.hpp +4 -4
- package/src/llama.cpp/ggml/src/ggml-sycl/wkv6.cpp +138 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/wkv6.hpp +10 -0
- package/src/llama.cpp/ggml/src/ggml-threading.cpp +12 -0
- package/src/llama.cpp/ggml/src/ggml-threading.h +12 -0
- package/src/llama.cpp/ggml/src/ggml-vulkan/CMakeLists.txt +78 -0
- package/src/llama.cpp/ggml/src/{ggml-vulkan.cpp → ggml-vulkan/ggml-vulkan.cpp} +555 -623
- package/src/llama.cpp/ggml/src/{vulkan-shaders → ggml-vulkan/vulkan-shaders}/vulkan-shaders-gen.cpp +125 -206
- package/src/llama.cpp/ggml/src/ggml.c +4032 -19890
- package/src/llama.cpp/include/llama.h +67 -33
- package/src/llama.cpp/pocs/vdot/q8dot.cpp +4 -3
- package/src/llama.cpp/pocs/vdot/vdot.cpp +8 -7
- package/src/llama.cpp/src/CMakeLists.txt +2 -1
- package/src/llama.cpp/src/llama-sampling.cpp +745 -105
- package/src/llama.cpp/src/llama-sampling.h +21 -2
- package/src/llama.cpp/src/llama-vocab.cpp +49 -9
- package/src/llama.cpp/src/llama-vocab.h +35 -11
- package/src/llama.cpp/src/llama.cpp +2636 -2406
- package/src/llama.cpp/src/unicode-data.cpp +2 -2
- package/src/llama.cpp/tests/CMakeLists.txt +1 -2
- package/src/llama.cpp/tests/test-arg-parser.cpp +14 -14
- package/src/llama.cpp/tests/test-backend-ops.cpp +185 -60
- package/src/llama.cpp/tests/test-barrier.cpp +1 -0
- package/src/llama.cpp/tests/test-chat-template.cpp +9 -5
- package/src/llama.cpp/tests/test-json-schema-to-grammar.cpp +17 -4
- package/src/llama.cpp/tests/test-log.cpp +2 -2
- package/src/llama.cpp/tests/test-opt.cpp +853 -142
- package/src/llama.cpp/tests/test-quantize-fns.cpp +22 -19
- package/src/llama.cpp/tests/test-quantize-perf.cpp +16 -14
- package/src/llama.cpp/tests/test-rope.cpp +1 -0
- package/src/llama.cpp/tests/test-sampling.cpp +162 -137
- package/src/llama.cpp/tests/test-tokenizer-0.cpp +7 -7
- package/src/llama.cpp/tests/test-tokenizer-1-bpe.cpp +5 -5
- package/src/llama.cpp/tests/test-tokenizer-1-spm.cpp +5 -5
- package/src/llama.cpp/common/train.cpp +0 -1515
- package/src/llama.cpp/common/train.h +0 -233
- package/src/llama.cpp/examples/baby-llama/CMakeLists.txt +0 -5
- package/src/llama.cpp/examples/baby-llama/baby-llama.cpp +0 -1639
- package/src/llama.cpp/tests/test-grad0.cpp +0 -1683
- /package/src/llama.cpp/ggml/{cmake → src/ggml-cpu/cmake}/FindSIMD.cmake +0 -0
- /package/src/llama.cpp/ggml/src/{llamafile → ggml-cpu/llamafile}/sgemm.h +0 -0
- /package/src/llama.cpp/ggml/src/{vulkan-shaders → ggml-vulkan/vulkan-shaders}/CMakeLists.txt +0 -0
|
@@ -34,6 +34,8 @@ extern "C" {
|
|
|
34
34
|
*/
|
|
35
35
|
#define GGML_CANN_MAX_DEVICES 16
|
|
36
36
|
|
|
37
|
+
GGML_BACKEND_API ggml_backend_reg_t ggml_backend_cann_reg(void);
|
|
38
|
+
|
|
37
39
|
/**
|
|
38
40
|
* @brief Initializes the CANN backend for a specified device.
|
|
39
41
|
*
|
|
@@ -44,7 +46,7 @@ extern "C" {
|
|
|
44
46
|
* @param device The index of the device to initialize.
|
|
45
47
|
* @return A pointer to the initialized backend instance, or nullptr on failure.
|
|
46
48
|
*/
|
|
47
|
-
|
|
49
|
+
GGML_BACKEND_API ggml_backend_t ggml_backend_cann_init(int32_t device);
|
|
48
50
|
|
|
49
51
|
/**
|
|
50
52
|
* @brief Checks if a given backend is a CANN backend.
|
|
@@ -55,7 +57,7 @@ GGML_API ggml_backend_t ggml_backend_cann_init(int32_t device);
|
|
|
55
57
|
* @param backend The backend instance to check.
|
|
56
58
|
* @return True if the backend is a CANN backend, false otherwise.
|
|
57
59
|
*/
|
|
58
|
-
|
|
60
|
+
GGML_BACKEND_API bool ggml_backend_is_cann(ggml_backend_t backend);
|
|
59
61
|
|
|
60
62
|
/**
|
|
61
63
|
* @brief Retrieves the CANN buffer type for a specified device.
|
|
@@ -67,7 +69,7 @@ GGML_API bool ggml_backend_is_cann(ggml_backend_t backend);
|
|
|
67
69
|
* @return A pointer to the buffer type interface for the specified device, or
|
|
68
70
|
* nullptr if the device index is out of range.
|
|
69
71
|
*/
|
|
70
|
-
|
|
72
|
+
GGML_BACKEND_API ggml_backend_buffer_type_t
|
|
71
73
|
ggml_backend_cann_buffer_type(int32_t device);
|
|
72
74
|
|
|
73
75
|
/**
|
|
@@ -78,14 +80,14 @@ ggml_backend_cann_buffer_type(int32_t device);
|
|
|
78
80
|
*
|
|
79
81
|
* @return The number of CANN devices available.
|
|
80
82
|
*/
|
|
81
|
-
|
|
83
|
+
GGML_BACKEND_API int32_t ggml_backend_cann_get_device_count(void);
|
|
82
84
|
|
|
83
85
|
/**
|
|
84
86
|
* @brief pinned host buffer for use with the CPU backend for faster copies between CPU and NPU.
|
|
85
87
|
*
|
|
86
88
|
* @return A pointer to the host buffer type interface.
|
|
87
89
|
*/
|
|
88
|
-
|
|
90
|
+
GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_cann_host_buffer_type(void);
|
|
89
91
|
|
|
90
92
|
/**
|
|
91
93
|
* @brief Retrieves the description of a specific CANN device.
|
|
@@ -97,7 +99,7 @@ GGML_API ggml_backend_buffer_type_t ggml_backend_cann_host_buffer_type(void);
|
|
|
97
99
|
* @param description Pointer to a buffer where the description will be written.
|
|
98
100
|
* @param description_size Size of the description buffer.
|
|
99
101
|
*/
|
|
100
|
-
|
|
102
|
+
GGML_BACKEND_API void ggml_backend_cann_get_device_description(
|
|
101
103
|
int32_t device, char* description, size_t description_size);
|
|
102
104
|
|
|
103
105
|
/**
|
|
@@ -112,7 +114,7 @@ GGML_API void ggml_backend_cann_get_device_description(
|
|
|
112
114
|
* @param total Pointer to a variable where the total memory size will be
|
|
113
115
|
* stored.
|
|
114
116
|
*/
|
|
115
|
-
|
|
117
|
+
GGML_BACKEND_API void ggml_backend_cann_get_device_memory(int32_t device,
|
|
116
118
|
size_t* free,
|
|
117
119
|
size_t* total);
|
|
118
120
|
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#ifndef __cplusplus
|
|
4
|
+
#error "This header is for C++ only"
|
|
5
|
+
#endif
|
|
6
|
+
|
|
7
|
+
#include "ggml.h"
|
|
8
|
+
#include "ggml-alloc.h"
|
|
9
|
+
#include "ggml-backend.h"
|
|
10
|
+
#include <memory>
|
|
11
|
+
|
|
12
|
+
// Smart pointers for ggml types
|
|
13
|
+
|
|
14
|
+
// ggml
|
|
15
|
+
|
|
16
|
+
struct ggml_context_deleter { void operator()(ggml_context * ctx) { ggml_free(ctx); } };
|
|
17
|
+
struct gguf_context_deleter { void operator()(gguf_context * ctx) { gguf_free(ctx); } };
|
|
18
|
+
|
|
19
|
+
typedef std::unique_ptr<ggml_context, ggml_context_deleter> ggml_context_ptr;
|
|
20
|
+
typedef std::unique_ptr<gguf_context, gguf_context_deleter> gguf_context_ptr;
|
|
21
|
+
|
|
22
|
+
// ggml-alloc
|
|
23
|
+
|
|
24
|
+
struct ggml_gallocr_deleter { void operator()(ggml_gallocr_t galloc) { ggml_gallocr_free(galloc); } };
|
|
25
|
+
|
|
26
|
+
typedef std::unique_ptr<ggml_gallocr_t, ggml_gallocr_deleter> ggml_gallocr_ptr;
|
|
27
|
+
|
|
28
|
+
// ggml-backend
|
|
29
|
+
|
|
30
|
+
struct ggml_backend_deleter { void operator()(ggml_backend_t backend) { ggml_backend_free(backend); } };
|
|
31
|
+
struct ggml_backend_buffer_deleter { void operator()(ggml_backend_buffer_t buffer) { ggml_backend_buffer_free(buffer); } };
|
|
32
|
+
struct ggml_backend_event_deleter { void operator()(ggml_backend_event_t event) { ggml_backend_event_free(event); } };
|
|
33
|
+
struct ggml_backend_sched_deleter { void operator()(ggml_backend_sched_t sched) { ggml_backend_sched_free(sched); } };
|
|
34
|
+
|
|
35
|
+
typedef std::unique_ptr<ggml_backend, ggml_backend_deleter> ggml_backend_ptr;
|
|
36
|
+
typedef std::unique_ptr<ggml_backend_buffer, ggml_backend_buffer_deleter> ggml_backend_buffer_ptr;
|
|
37
|
+
typedef std::unique_ptr<ggml_backend_event, ggml_backend_event_deleter> ggml_backend_event_ptr;
|
|
38
|
+
typedef std::unique_ptr<ggml_backend_sched, ggml_backend_sched_deleter> ggml_backend_sched_ptr;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "ggml.h"
|
|
4
|
+
#include "ggml-backend.h"
|
|
5
|
+
|
|
6
|
+
#ifdef __cplusplus
|
|
7
|
+
extern "C" {
|
|
8
|
+
#endif
|
|
9
|
+
|
|
10
|
+
// Scheduling priorities
|
|
11
|
+
enum ggml_sched_priority {
|
|
12
|
+
GGML_SCHED_PRIO_NORMAL,
|
|
13
|
+
GGML_SCHED_PRIO_MEDIUM,
|
|
14
|
+
GGML_SCHED_PRIO_HIGH,
|
|
15
|
+
GGML_SCHED_PRIO_REALTIME
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// Threadpool params
|
|
19
|
+
// Use ggml_threadpool_params_default() or ggml_threadpool_params_init() to populate the defaults
|
|
20
|
+
struct ggml_threadpool_params {
|
|
21
|
+
bool cpumask[GGML_MAX_N_THREADS]; // mask of cpu cores (all-zeros means use default affinity settings)
|
|
22
|
+
int n_threads; // number of threads
|
|
23
|
+
enum ggml_sched_priority prio; // thread priority
|
|
24
|
+
uint32_t poll; // polling level (0 - no polling, 100 - aggressive polling)
|
|
25
|
+
bool strict_cpu; // strict cpu placement
|
|
26
|
+
bool paused; // start in paused state
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
struct ggml_threadpool; // forward declaration, see ggml.c
|
|
30
|
+
|
|
31
|
+
typedef struct ggml_threadpool * ggml_threadpool_t;
|
|
32
|
+
|
|
33
|
+
// the compute plan that needs to be prepared for ggml_graph_compute()
|
|
34
|
+
// since https://github.com/ggerganov/ggml/issues/287
|
|
35
|
+
struct ggml_cplan {
|
|
36
|
+
size_t work_size; // size of work buffer, calculated by `ggml_graph_plan()`
|
|
37
|
+
uint8_t * work_data; // work buffer, to be allocated by caller before calling to `ggml_graph_compute()`
|
|
38
|
+
|
|
39
|
+
int n_threads;
|
|
40
|
+
struct ggml_threadpool * threadpool;
|
|
41
|
+
|
|
42
|
+
// abort ggml_graph_compute when true
|
|
43
|
+
ggml_abort_callback abort_callback;
|
|
44
|
+
void * abort_callback_data;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// numa strategies
|
|
48
|
+
enum ggml_numa_strategy {
|
|
49
|
+
GGML_NUMA_STRATEGY_DISABLED = 0,
|
|
50
|
+
GGML_NUMA_STRATEGY_DISTRIBUTE = 1,
|
|
51
|
+
GGML_NUMA_STRATEGY_ISOLATE = 2,
|
|
52
|
+
GGML_NUMA_STRATEGY_NUMACTL = 3,
|
|
53
|
+
GGML_NUMA_STRATEGY_MIRROR = 4,
|
|
54
|
+
GGML_NUMA_STRATEGY_COUNT
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
GGML_BACKEND_API void ggml_numa_init(enum ggml_numa_strategy numa); // call once for better performance on NUMA systems
|
|
58
|
+
GGML_BACKEND_API bool ggml_is_numa(void); // true if init detected that system has >1 NUMA node
|
|
59
|
+
|
|
60
|
+
GGML_BACKEND_API struct ggml_tensor * ggml_new_i32(struct ggml_context * ctx, int32_t value);
|
|
61
|
+
GGML_BACKEND_API struct ggml_tensor * ggml_new_f32(struct ggml_context * ctx, float value);
|
|
62
|
+
|
|
63
|
+
GGML_BACKEND_API struct ggml_tensor * ggml_set_i32 (struct ggml_tensor * tensor, int32_t value);
|
|
64
|
+
GGML_BACKEND_API struct ggml_tensor * ggml_set_f32 (struct ggml_tensor * tensor, float value);
|
|
65
|
+
|
|
66
|
+
GGML_BACKEND_API int32_t ggml_get_i32_1d(const struct ggml_tensor * tensor, int i);
|
|
67
|
+
GGML_BACKEND_API void ggml_set_i32_1d(const struct ggml_tensor * tensor, int i, int32_t value);
|
|
68
|
+
|
|
69
|
+
GGML_BACKEND_API int32_t ggml_get_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3);
|
|
70
|
+
GGML_BACKEND_API void ggml_set_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, int32_t value);
|
|
71
|
+
|
|
72
|
+
GGML_BACKEND_API float ggml_get_f32_1d(const struct ggml_tensor * tensor, int i);
|
|
73
|
+
GGML_BACKEND_API void ggml_set_f32_1d(const struct ggml_tensor * tensor, int i, float value);
|
|
74
|
+
|
|
75
|
+
GGML_BACKEND_API float ggml_get_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3);
|
|
76
|
+
GGML_BACKEND_API void ggml_set_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, float value);
|
|
77
|
+
|
|
78
|
+
GGML_BACKEND_API struct ggml_threadpool_params ggml_threadpool_params_default(int n_threads);
|
|
79
|
+
GGML_BACKEND_API void ggml_threadpool_params_init (struct ggml_threadpool_params * p, int n_threads);
|
|
80
|
+
GGML_BACKEND_API bool ggml_threadpool_params_match (const struct ggml_threadpool_params * p0, const struct ggml_threadpool_params * p1);
|
|
81
|
+
GGML_BACKEND_API struct ggml_threadpool * ggml_threadpool_new (struct ggml_threadpool_params * params);
|
|
82
|
+
GGML_BACKEND_API void ggml_threadpool_free (struct ggml_threadpool * threadpool);
|
|
83
|
+
GGML_BACKEND_API int ggml_threadpool_get_n_threads(struct ggml_threadpool * threadpool);
|
|
84
|
+
GGML_BACKEND_API void ggml_threadpool_pause (struct ggml_threadpool * threadpool);
|
|
85
|
+
GGML_BACKEND_API void ggml_threadpool_resume (struct ggml_threadpool * threadpool);
|
|
86
|
+
|
|
87
|
+
// ggml_graph_plan() has to be called before ggml_graph_compute()
|
|
88
|
+
// when plan.work_size > 0, caller must allocate memory for plan.work_data
|
|
89
|
+
GGML_BACKEND_API struct ggml_cplan ggml_graph_plan(
|
|
90
|
+
const struct ggml_cgraph * cgraph,
|
|
91
|
+
int n_threads, /* = GGML_DEFAULT_N_THREADS */
|
|
92
|
+
struct ggml_threadpool * threadpool /* = NULL */ );
|
|
93
|
+
GGML_BACKEND_API enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cplan * cplan);
|
|
94
|
+
|
|
95
|
+
// same as ggml_graph_compute() but the work data is allocated as a part of the context
|
|
96
|
+
// note: the drawback of this API is that you must have ensured that the context has enough memory for the work data
|
|
97
|
+
GGML_BACKEND_API enum ggml_status ggml_graph_compute_with_ctx(struct ggml_context * ctx, struct ggml_cgraph * cgraph, int n_threads);
|
|
98
|
+
|
|
99
|
+
//
|
|
100
|
+
// system info
|
|
101
|
+
//
|
|
102
|
+
|
|
103
|
+
// x86
|
|
104
|
+
GGML_BACKEND_API int ggml_cpu_has_sse3 (void);
|
|
105
|
+
GGML_BACKEND_API int ggml_cpu_has_ssse3 (void);
|
|
106
|
+
GGML_BACKEND_API int ggml_cpu_has_avx (void);
|
|
107
|
+
GGML_BACKEND_API int ggml_cpu_has_avx2 (void);
|
|
108
|
+
GGML_BACKEND_API int ggml_cpu_has_f16c (void);
|
|
109
|
+
GGML_BACKEND_API int ggml_cpu_has_fma (void);
|
|
110
|
+
GGML_BACKEND_API int ggml_cpu_has_avx_vnni (void);
|
|
111
|
+
GGML_BACKEND_API int ggml_cpu_has_avx512 (void);
|
|
112
|
+
GGML_BACKEND_API int ggml_cpu_has_avx512_vbmi(void);
|
|
113
|
+
GGML_BACKEND_API int ggml_cpu_has_avx512_vnni(void);
|
|
114
|
+
GGML_BACKEND_API int ggml_cpu_has_avx512_bf16(void);
|
|
115
|
+
GGML_BACKEND_API int ggml_cpu_has_amx_int8 (void);
|
|
116
|
+
// ARM
|
|
117
|
+
GGML_BACKEND_API int ggml_cpu_has_neon (void);
|
|
118
|
+
GGML_BACKEND_API int ggml_cpu_has_arm_fma (void);
|
|
119
|
+
GGML_BACKEND_API int ggml_cpu_has_fp16_va (void);
|
|
120
|
+
GGML_BACKEND_API int ggml_cpu_has_matmul_int8(void);
|
|
121
|
+
GGML_BACKEND_API int ggml_cpu_has_sve (void);
|
|
122
|
+
GGML_BACKEND_API int ggml_cpu_get_sve_cnt (void); // sve vector length in bytes
|
|
123
|
+
// other
|
|
124
|
+
GGML_BACKEND_API int ggml_cpu_has_riscv_v (void);
|
|
125
|
+
GGML_BACKEND_API int ggml_cpu_has_vsx (void);
|
|
126
|
+
GGML_BACKEND_API int ggml_cpu_has_wasm_simd (void);
|
|
127
|
+
GGML_BACKEND_API int ggml_cpu_has_llamafile (void);
|
|
128
|
+
|
|
129
|
+
// Internal types and functions exposed for tests and benchmarks
|
|
130
|
+
|
|
131
|
+
typedef void (*ggml_from_float_to_mat_t)
|
|
132
|
+
(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t nr, int64_t k, int64_t bs);
|
|
133
|
+
typedef void (*ggml_vec_dot_t) (int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT x, size_t bx,
|
|
134
|
+
const void * GGML_RESTRICT y, size_t by, int nrc);
|
|
135
|
+
typedef void (*ggml_gemv_t) (int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT x,
|
|
136
|
+
const void * GGML_RESTRICT y, int nr, int nc);
|
|
137
|
+
typedef void (*ggml_gemm_t) (int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT x,
|
|
138
|
+
const void * GGML_RESTRICT y, int nr, int nc);
|
|
139
|
+
|
|
140
|
+
struct ggml_type_traits_cpu {
|
|
141
|
+
ggml_from_float_t from_float;
|
|
142
|
+
ggml_from_float_to_mat_t from_float_to_mat;
|
|
143
|
+
ggml_vec_dot_t vec_dot;
|
|
144
|
+
enum ggml_type vec_dot_type;
|
|
145
|
+
int64_t nrows; // number of rows to process simultaneously
|
|
146
|
+
int64_t ncols; // number of columns to process simultaneously
|
|
147
|
+
ggml_gemv_t gemv;
|
|
148
|
+
ggml_gemm_t gemm;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
GGML_BACKEND_API const struct ggml_type_traits_cpu * ggml_get_type_traits_cpu(enum ggml_type type);
|
|
152
|
+
|
|
153
|
+
GGML_BACKEND_API void ggml_cpu_init(void);
|
|
154
|
+
|
|
155
|
+
//
|
|
156
|
+
// CPU backend
|
|
157
|
+
//
|
|
158
|
+
|
|
159
|
+
GGML_BACKEND_API ggml_backend_t ggml_backend_cpu_init(void);
|
|
160
|
+
|
|
161
|
+
GGML_BACKEND_API bool ggml_backend_is_cpu (ggml_backend_t backend);
|
|
162
|
+
GGML_BACKEND_API void ggml_backend_cpu_set_n_threads (ggml_backend_t backend_cpu, int n_threads);
|
|
163
|
+
GGML_BACKEND_API void ggml_backend_cpu_set_threadpool (ggml_backend_t backend_cpu, ggml_threadpool_t threadpool);
|
|
164
|
+
GGML_BACKEND_API void ggml_backend_cpu_set_abort_callback(ggml_backend_t backend_cpu, ggml_abort_callback abort_callback, void * abort_callback_data);
|
|
165
|
+
|
|
166
|
+
GGML_BACKEND_API ggml_backend_reg_t ggml_backend_cpu_reg(void);
|
|
167
|
+
|
|
168
|
+
#ifdef GGML_USE_CPU_HBM
|
|
169
|
+
GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_cpu_hbm_buffer_type(void);
|
|
170
|
+
#endif
|
|
171
|
+
|
|
172
|
+
GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_cpu_aarch64_buffer_type(void);
|
|
173
|
+
GGML_BACKEND_API bool ggml_backend_cpu_buft_is_aarch64(ggml_backend_buffer_type_t buft);
|
|
174
|
+
|
|
175
|
+
#ifdef __cplusplus
|
|
176
|
+
}
|
|
177
|
+
#endif
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
extern "C" {
|
|
8
8
|
#endif
|
|
9
9
|
|
|
10
|
-
#ifdef
|
|
10
|
+
#ifdef GGML_USE_HIP
|
|
11
11
|
#define GGML_CUDA_NAME "ROCm"
|
|
12
12
|
#define GGML_CUBLAS_NAME "hipBLAS"
|
|
13
13
|
#elif defined(GGML_USE_MUSA)
|
|
@@ -20,27 +20,27 @@ extern "C" {
|
|
|
20
20
|
#define GGML_CUDA_MAX_DEVICES 16
|
|
21
21
|
|
|
22
22
|
// backend API
|
|
23
|
-
|
|
23
|
+
GGML_BACKEND_API ggml_backend_t ggml_backend_cuda_init(int device);
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
GGML_BACKEND_API bool ggml_backend_is_cuda(ggml_backend_t backend);
|
|
26
26
|
|
|
27
27
|
// device buffer
|
|
28
|
-
|
|
28
|
+
GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device);
|
|
29
29
|
|
|
30
30
|
// split tensor buffer that splits matrices by rows across multiple devices
|
|
31
|
-
|
|
31
|
+
GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_cuda_split_buffer_type(int main_device, const float * tensor_split);
|
|
32
32
|
|
|
33
33
|
// pinned host buffer for use with the CPU backend for faster copies between CPU and GPU
|
|
34
|
-
|
|
34
|
+
GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type(void);
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
GGML_BACKEND_API int ggml_backend_cuda_get_device_count(void);
|
|
37
|
+
GGML_BACKEND_API void ggml_backend_cuda_get_device_description(int device, char * description, size_t description_size);
|
|
38
|
+
GGML_BACKEND_API void ggml_backend_cuda_get_device_memory(int device, size_t * free, size_t * total);
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
GGML_BACKEND_API bool ggml_backend_cuda_register_host_buffer(void * buffer, size_t size);
|
|
41
|
+
GGML_BACKEND_API void ggml_backend_cuda_unregister_host_buffer(void * buffer);
|
|
42
42
|
|
|
43
|
-
|
|
43
|
+
GGML_BACKEND_API ggml_backend_reg_t ggml_backend_cuda_reg(void);
|
|
44
44
|
|
|
45
45
|
#ifdef __cplusplus
|
|
46
46
|
}
|
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
extern "C" {
|
|
12
12
|
#endif
|
|
13
13
|
|
|
14
|
+
#define GGML_KOMPUTE_MAX_DEVICES 16
|
|
15
|
+
|
|
14
16
|
struct ggml_vk_device {
|
|
15
17
|
int index;
|
|
16
18
|
int type; // same as VkPhysicalDeviceType
|
|
@@ -35,11 +37,13 @@ struct ggml_vk_device ggml_vk_current_device(void);
|
|
|
35
37
|
// forward declaration
|
|
36
38
|
typedef struct ggml_backend * ggml_backend_t;
|
|
37
39
|
|
|
38
|
-
|
|
40
|
+
GGML_BACKEND_API ggml_backend_t ggml_backend_kompute_init(int device);
|
|
41
|
+
|
|
42
|
+
GGML_BACKEND_API bool ggml_backend_is_kompute(ggml_backend_t backend);
|
|
39
43
|
|
|
40
|
-
|
|
44
|
+
GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_kompute_buffer_type(int device);
|
|
41
45
|
|
|
42
|
-
|
|
46
|
+
GGML_BACKEND_API ggml_backend_reg_t ggml_backend_kompute_reg(void);
|
|
43
47
|
|
|
44
48
|
#ifdef __cplusplus
|
|
45
49
|
}
|
|
@@ -39,23 +39,27 @@ extern "C" {
|
|
|
39
39
|
// user-code should use only these functions
|
|
40
40
|
//
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
GGML_BACKEND_API ggml_backend_t ggml_backend_metal_init(void);
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
GGML_BACKEND_API bool ggml_backend_is_metal(ggml_backend_t backend);
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
GGML_DEPRECATED(
|
|
47
|
+
GGML_BACKEND_API ggml_backend_buffer_t ggml_backend_metal_buffer_from_ptr(void * data, size_t size, size_t max_size),
|
|
48
|
+
"obsoleted by the new device interface - https://github.com/ggerganov/llama.cpp/pull/9713");
|
|
47
49
|
|
|
48
|
-
|
|
50
|
+
GGML_BACKEND_API void ggml_backend_metal_set_abort_callback(ggml_backend_t backend, ggml_abort_callback abort_callback, void * user_data);
|
|
49
51
|
|
|
50
|
-
|
|
52
|
+
GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_metal_buffer_type(void);
|
|
51
53
|
|
|
52
54
|
// helper to check if the device supports a specific family
|
|
53
55
|
// ideally, the user code should be doing these checks
|
|
54
56
|
// ref: https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
|
|
55
|
-
|
|
57
|
+
GGML_BACKEND_API bool ggml_backend_metal_supports_family(ggml_backend_t backend, int family);
|
|
56
58
|
|
|
57
59
|
// capture all command buffers committed the next time `ggml_backend_graph_compute` is called
|
|
58
|
-
|
|
60
|
+
GGML_BACKEND_API void ggml_backend_metal_capture_next_compute(ggml_backend_t backend);
|
|
61
|
+
|
|
62
|
+
GGML_BACKEND_API ggml_backend_reg_t ggml_backend_metal_reg(void);
|
|
59
63
|
|
|
60
64
|
#ifdef __cplusplus
|
|
61
65
|
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// This file contains functionality for training models using GGML.
|
|
2
|
+
// It is not strictly needed vs. just vanilla GGML but it provides a more high-level interface for common needs such as datasets.
|
|
3
|
+
// At the bottom of this file especially there are relatively high-level functions that are suitable use or adaptation in user code.
|
|
4
|
+
//
|
|
5
|
+
// Module maintainer: Johannes Gäßler (@JohannesGaessler, johannesg@5d6.de)
|
|
6
|
+
|
|
7
|
+
#pragma once
|
|
8
|
+
|
|
9
|
+
#include "ggml.h"
|
|
10
|
+
#include "ggml-backend.h"
|
|
11
|
+
|
|
12
|
+
#include <stdint.h>
|
|
13
|
+
|
|
14
|
+
#ifdef __cplusplus
|
|
15
|
+
extern "C" {
|
|
16
|
+
#endif
|
|
17
|
+
|
|
18
|
+
struct ggml_opt_dataset;
|
|
19
|
+
struct ggml_opt_context;
|
|
20
|
+
struct ggml_opt_result;
|
|
21
|
+
|
|
22
|
+
typedef struct ggml_opt_dataset * ggml_opt_dataset_t;
|
|
23
|
+
typedef struct ggml_opt_context * ggml_opt_context_t;
|
|
24
|
+
typedef struct ggml_opt_result * ggml_opt_result_t;
|
|
25
|
+
|
|
26
|
+
// ====== Loss ======
|
|
27
|
+
|
|
28
|
+
// built-in loss types, i.e. the built-in quantities minimized by the optimizer
|
|
29
|
+
// custom loss types can be defined via mean or sum which simply reduce the outputs for all datapoints to a single value
|
|
30
|
+
enum ggml_opt_loss_type {
|
|
31
|
+
GGML_OPT_LOSS_TYPE_MEAN,
|
|
32
|
+
GGML_OPT_LOSS_TYPE_SUM,
|
|
33
|
+
GGML_OPT_LOSS_TYPE_CROSS_ENTROPY,
|
|
34
|
+
GGML_OPT_LOSS_TYPE_MEAN_SQUARED_ERROR,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// ====== Dataset ======
|
|
38
|
+
|
|
39
|
+
GGML_API ggml_opt_dataset_t ggml_opt_dataset_init(
|
|
40
|
+
int64_t ne_datapoint, // number of elements per datapoint
|
|
41
|
+
int64_t ne_label, // number of elements per label
|
|
42
|
+
int64_t ndata, // total number of datapoints/labels
|
|
43
|
+
int64_t ndata_shard); // number of datapoints/labels per shard (unit at which the dataset is shuffled/copied)
|
|
44
|
+
GGML_API void ggml_opt_dataset_free(ggml_opt_dataset_t dataset);
|
|
45
|
+
|
|
46
|
+
// get underlying tensors that store the data
|
|
47
|
+
GGML_API struct ggml_tensor * ggml_opt_dataset_data (ggml_opt_dataset_t dataset); // shape = [ne_datapoint, ndata]
|
|
48
|
+
GGML_API struct ggml_tensor * ggml_opt_dataset_labels(ggml_opt_dataset_t dataset); // shape = [nd_label, ndata]
|
|
49
|
+
|
|
50
|
+
// shuffle idata first datapoints from dataset with RNG from opt_ctx, shuffle all datapoints if idata is negative
|
|
51
|
+
GGML_API void ggml_opt_dataset_shuffle(ggml_opt_context_t opt_ctx, ggml_opt_dataset_t dataset, int64_t idata);
|
|
52
|
+
|
|
53
|
+
// get batch at position ibatch from dataset and copy the data to data_batch and labels_batch
|
|
54
|
+
GGML_API void ggml_opt_dataset_get_batch(
|
|
55
|
+
ggml_opt_dataset_t dataset,
|
|
56
|
+
struct ggml_tensor * data_batch, // shape = [ne_datapoint, ndata_batch]
|
|
57
|
+
struct ggml_tensor * labels_batch, // shape = [ne_label, ndata_batch]
|
|
58
|
+
int64_t ibatch);
|
|
59
|
+
|
|
60
|
+
// ====== Model / Context ======
|
|
61
|
+
|
|
62
|
+
enum ggml_opt_build_type {
|
|
63
|
+
GGML_OPT_BUILD_TYPE_FORWARD,
|
|
64
|
+
GGML_OPT_BUILD_TYPE_GRAD,
|
|
65
|
+
GGML_OPT_BUILD_TYPE_OPT,
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// parameters that control which optimizer is used and how said optimizer tries to find the minimal loss
|
|
69
|
+
struct ggml_opt_optimizer_params {
|
|
70
|
+
// AdamW optimizer parameters
|
|
71
|
+
struct {
|
|
72
|
+
float alpha; // learning rate
|
|
73
|
+
float beta1;
|
|
74
|
+
float beta2;
|
|
75
|
+
float eps; // epsilon for numerical stability
|
|
76
|
+
float wd; // weight decay for AdamW, use 0.0f to disable
|
|
77
|
+
} adamw;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// callback to calculate optimizer parameters prior to a backward pass
|
|
81
|
+
// userdata can be used to pass arbitrary data
|
|
82
|
+
typedef struct ggml_opt_optimizer_params (*ggml_opt_get_optimizer_params)(void * userdata);
|
|
83
|
+
|
|
84
|
+
// returns the default optimizer params (constant)
|
|
85
|
+
// userdata is not used
|
|
86
|
+
GGML_API struct ggml_opt_optimizer_params ggml_opt_get_default_optimizer_params(void * userdata);
|
|
87
|
+
|
|
88
|
+
// parameters for initializing a new optimization context
|
|
89
|
+
struct ggml_opt_params {
|
|
90
|
+
ggml_backend_sched_t backend_sched; // defines which backends are used to construct the compute graphs
|
|
91
|
+
|
|
92
|
+
struct ggml_context * ctx_compute; // created in user code, holds non-static tensors
|
|
93
|
+
|
|
94
|
+
// the forward graph is defined by inputs and outputs
|
|
95
|
+
// those tensors and all tensors inbetween are not intended to be reusable between multiple optimization contexts
|
|
96
|
+
struct ggml_tensor * inputs;
|
|
97
|
+
struct ggml_tensor * outputs;
|
|
98
|
+
|
|
99
|
+
enum ggml_opt_loss_type loss_type;
|
|
100
|
+
enum ggml_opt_build_type build_type;
|
|
101
|
+
|
|
102
|
+
int32_t opt_period; // after how many gradient accumulation steps an optimizer step should be done
|
|
103
|
+
|
|
104
|
+
ggml_opt_get_optimizer_params get_opt_pars; // callback for calculating optimizer parameters
|
|
105
|
+
void * get_opt_pars_ud; // userdata for calculating optimizer parameters
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
// get parameters for an optimization context with defaults set where possible
|
|
109
|
+
// parameters for which no sensible defaults exist are supplied as arguments to this function
|
|
110
|
+
GGML_API ggml_opt_params ggml_opt_default_params(
|
|
111
|
+
ggml_backend_sched_t backend_sched,
|
|
112
|
+
struct ggml_context * ctx_compute,
|
|
113
|
+
struct ggml_tensor * inputs,
|
|
114
|
+
struct ggml_tensor * outputs,
|
|
115
|
+
enum ggml_opt_loss_type loss_type);
|
|
116
|
+
|
|
117
|
+
GGML_API ggml_opt_context_t ggml_opt_init(struct ggml_opt_params params);
|
|
118
|
+
GGML_API void ggml_opt_free(ggml_opt_context_t opt_ctx);
|
|
119
|
+
|
|
120
|
+
// set gradients to zero, initilize loss, and optionally reset the optimizer
|
|
121
|
+
GGML_API void ggml_opt_reset(ggml_opt_context_t opt_ctx, bool optimizer);
|
|
122
|
+
|
|
123
|
+
// get underlying tensors that store data
|
|
124
|
+
GGML_API struct ggml_tensor * ggml_opt_inputs( ggml_opt_context_t opt_ctx); // forward graph input tensor
|
|
125
|
+
GGML_API struct ggml_tensor * ggml_opt_outputs( ggml_opt_context_t opt_ctx); // forward graph output tensor
|
|
126
|
+
GGML_API struct ggml_tensor * ggml_opt_labels( ggml_opt_context_t opt_ctx); // labels to compare outputs against
|
|
127
|
+
GGML_API struct ggml_tensor * ggml_opt_loss( ggml_opt_context_t opt_ctx); // scalar tensor that contains the loss
|
|
128
|
+
GGML_API struct ggml_tensor * ggml_opt_pred( ggml_opt_context_t opt_ctx); // predictions made by outputs
|
|
129
|
+
GGML_API struct ggml_tensor * ggml_opt_ncorrect(ggml_opt_context_t opt_ctx); // number of matching predictions between outputs and labels
|
|
130
|
+
|
|
131
|
+
GGML_API struct ggml_tensor * ggml_opt_grad_acc(ggml_opt_context_t opt_ctx, struct ggml_tensor * node);
|
|
132
|
+
|
|
133
|
+
// ====== Optimization Result ======
|
|
134
|
+
|
|
135
|
+
GGML_API ggml_opt_result_t ggml_opt_result_init();
|
|
136
|
+
GGML_API void ggml_opt_result_free(ggml_opt_result_t result);
|
|
137
|
+
GGML_API void ggml_opt_result_reset(ggml_opt_result_t result);
|
|
138
|
+
|
|
139
|
+
// get data from result, uncertainties are optional and can be ignored by passing NULL
|
|
140
|
+
GGML_API void ggml_opt_result_ndata( ggml_opt_result_t result, int64_t * ndata); // writes 1 value, number of datapoints
|
|
141
|
+
GGML_API void ggml_opt_result_loss( ggml_opt_result_t result, double * loss, double * unc); // writes 1 value
|
|
142
|
+
GGML_API void ggml_opt_result_pred( ggml_opt_result_t result, int32_t * pred); // writes ndata values
|
|
143
|
+
GGML_API void ggml_opt_result_accuracy(ggml_opt_result_t result, double * accuracy, double * unc); // writes 1 value
|
|
144
|
+
|
|
145
|
+
// ====== Computation ======
|
|
146
|
+
|
|
147
|
+
// do forward pass, increment result if not NULL
|
|
148
|
+
GGML_API void ggml_opt_forward(ggml_opt_context_t opt_ctx, ggml_opt_result_t result);
|
|
149
|
+
|
|
150
|
+
// do forward pass, increment result if not NULL, do backward pass
|
|
151
|
+
GGML_API void ggml_opt_forward_backward(ggml_opt_context_t opt_ctx, ggml_opt_result_t result);
|
|
152
|
+
|
|
153
|
+
// ############################################################################
|
|
154
|
+
// ## The high-level functions start here. They do not depend on any private ##
|
|
155
|
+
// ## functions or structs and can be copied to and adapted for user code. ##
|
|
156
|
+
// ############################################################################
|
|
157
|
+
|
|
158
|
+
// ====== Intended Usage ======
|
|
159
|
+
//
|
|
160
|
+
// 1. Select the appropriate loss for your problem.
|
|
161
|
+
// 2. Create a dataset and set the data for the "data" tensor. Also set the "labels" tensor if your loss needs them.
|
|
162
|
+
// Setting the shard size to 1 will be fine, it's the granularity with which data is shuffled/loaded (bigger values are faster).
|
|
163
|
+
// 3. Create a GGML graph for your model with no_alloc == true. Use two separate contexts for the tensors.
|
|
164
|
+
// The first context should contain the model parameters and inputs and be allocated statically in user code.
|
|
165
|
+
// The second context should contain all other tensors and will be (re)allocated automatically.
|
|
166
|
+
// Due to this automated allocation the data of the second context is not defined when accessed in user code.
|
|
167
|
+
// Note that the second dimension of the inputs/outputs are interpreted as the number of datapoints in those tensors.
|
|
168
|
+
// 4. Call ggml_opt_fit. If you need more control you can use ggml_opt_epoch instead.
|
|
169
|
+
|
|
170
|
+
// signature for a callback while evaluating opt_ctx on dataset, called after an evaluation
|
|
171
|
+
typedef void (*ggml_opt_epoch_callback)(
|
|
172
|
+
bool train, // true after training evaluation, false after validation evaluation
|
|
173
|
+
ggml_opt_context_t opt_ctx,
|
|
174
|
+
ggml_opt_dataset_t dataset,
|
|
175
|
+
ggml_opt_result_t result, // result associated with the dataset subsection
|
|
176
|
+
int64_t ibatch, // number of batches that have been evaluated so far
|
|
177
|
+
int64_t ibatch_max, // total number of batches in this dataset subsection
|
|
178
|
+
int64_t t_start_us); // time at which the evaluation on the dataset subsection was started
|
|
179
|
+
|
|
180
|
+
// do training on front of dataset, do evaluation only on back of dataset
|
|
181
|
+
GGML_API void ggml_opt_epoch(
|
|
182
|
+
ggml_opt_context_t opt_ctx,
|
|
183
|
+
ggml_opt_dataset_t dataset,
|
|
184
|
+
ggml_opt_result_t result_train, // result to increment during training, ignored if NULL
|
|
185
|
+
ggml_opt_result_t result_eval, // result to increment during evaluation, ignored if NULL
|
|
186
|
+
int64_t idata_split, // data index at which to split training and evaluation
|
|
187
|
+
ggml_opt_epoch_callback callback_train,
|
|
188
|
+
ggml_opt_epoch_callback callback_eval);
|
|
189
|
+
|
|
190
|
+
// callback that prints a progress bar on stderr
|
|
191
|
+
GGML_API void ggml_opt_epoch_callback_progress_bar(
|
|
192
|
+
bool train,
|
|
193
|
+
ggml_opt_context_t opt_ctx,
|
|
194
|
+
ggml_opt_dataset_t dataset,
|
|
195
|
+
ggml_opt_result_t result,
|
|
196
|
+
int64_t ibatch,
|
|
197
|
+
int64_t ibatch_max,
|
|
198
|
+
int64_t t_start_us);
|
|
199
|
+
|
|
200
|
+
// fit model defined by inputs and outputs to dataset
|
|
201
|
+
GGML_API void ggml_opt_fit(
|
|
202
|
+
ggml_backend_sched_t backend_sched, // backend scheduler for constructing the compute graphs
|
|
203
|
+
ggml_context * ctx_compute, // context with temporarily allocated tensors to calculate the outputs
|
|
204
|
+
ggml_tensor * inputs, // input tensor with shape [ne_datapoint, ndata_batch]
|
|
205
|
+
ggml_tensor * outputs, // output tensor, must have shape [ne_label, ndata_batch] if labels are used
|
|
206
|
+
ggml_opt_dataset_t dataset, // dataset with data and optionally also labels
|
|
207
|
+
enum ggml_opt_loss_type loss_type, // loss to minimize
|
|
208
|
+
ggml_opt_get_optimizer_params get_opt_pars, // callback to get optimizer params, userdata is pointer to epoch (of type int64_t)
|
|
209
|
+
int64_t nepoch, // how many times the dataset should be iterated over
|
|
210
|
+
int64_t nbatch_logical, // datapoints optimizer step, must be a multiple of ndata_batch in inputs/outputs
|
|
211
|
+
float val_split, // fraction of the dataset to use for validation, must be in [0.0f, 1.0f)
|
|
212
|
+
bool silent); // whether or not info prints to stderr should be suppressed
|
|
213
|
+
|
|
214
|
+
#ifdef __cplusplus
|
|
215
|
+
}
|
|
216
|
+
#endif
|
|
@@ -10,14 +10,18 @@ extern "C" {
|
|
|
10
10
|
#define GGML_RPC_MAX_SERVERS 16
|
|
11
11
|
|
|
12
12
|
// backend API
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
GGML_BACKEND_API ggml_backend_t ggml_backend_rpc_init(const char * endpoint);
|
|
14
|
+
GGML_BACKEND_API bool ggml_backend_is_rpc(ggml_backend_t backend);
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_rpc_buffer_type(const char * endpoint);
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
GGML_BACKEND_API void ggml_backend_rpc_get_device_memory(const char * endpoint, size_t * free, size_t * total);
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
GGML_BACKEND_API void ggml_backend_rpc_start_server(ggml_backend_t backend, const char * endpoint, size_t free_mem, size_t total_mem);
|
|
21
|
+
|
|
22
|
+
GGML_BACKEND_API ggml_backend_reg_t ggml_backend_rpc_reg(void);
|
|
23
|
+
|
|
24
|
+
GGML_BACKEND_API ggml_backend_dev_t ggml_backend_rpc_add_device(const char * endpoint);
|
|
21
25
|
|
|
22
26
|
#ifdef __cplusplus
|
|
23
27
|
}
|