Skip to content

Distribute and run LLMs with a single file.

Restore MSVC compatibility

Justine Tunney0d10a3392298
diff --git a/llama.cpp/ggml-backend-impl.h b/llama.cpp/ggml-backend-impl.h
index 8859be9a..6cfe2ca8 100644
--- a/llama.cpp/ggml-backend-impl.h
+++ b/llama.cpp/ggml-backend-impl.h
@@ -121,32 +121,32 @@ extern "C" {
//
struct ggml_backend_api {
- void (*free)(void *);
- void *(*malloc)(size_t);
- typeof(ggml_backend_buffer_init) *GGML_ABI ggml_backend_buffer_init;
- typeof(ggml_backend_cpu_buffer_from_ptr) *GGML_ABI ggml_backend_cpu_buffer_from_ptr;
- typeof(ggml_backend_cpu_buffer_type) *GGML_ABI ggml_backend_cpu_buffer_type;
- typeof(ggml_backend_buft_get_alloc_size) *GGML_ABI ggml_backend_buft_get_alloc_size;
- typeof(ggml_backend_buft_alloc_buffer) *GGML_ABI ggml_backend_buft_alloc_buffer;
- typeof(ggml_backend_is_cpu) *GGML_ABI ggml_backend_is_cpu;
- typeof(ggml_backend_tensor_get) *GGML_ABI ggml_backend_tensor_get;
- typeof(ggml_backend_tensor_set) *GGML_ABI ggml_backend_tensor_set;
- typeof(ggml_is_quantized) *GGML_ABI ggml_is_quantized;
- typeof(ggml_type_size) *GGML_ABI ggml_type_size;
- typeof(ggml_blck_size) *GGML_ABI ggml_blck_size;
- typeof(ggml_is_transposed) *GGML_ABI ggml_is_transposed;
- typeof(ggml_nbytes) *GGML_ABI ggml_nbytes;
- typeof(ggml_get_unary_op) *GGML_ABI ggml_get_unary_op;
- typeof(ggml_nelements) *GGML_ABI ggml_nelements;
- typeof(ggml_nrows) *GGML_ABI ggml_nrows;
- typeof(ggml_is_permuted) *GGML_ABI ggml_is_permuted;
- typeof(ggml_is_contiguous) *GGML_ABI ggml_is_contiguous;
- typeof(ggml_op_name) *GGML_ABI ggml_op_name;
- typeof(ggml_type_name) *GGML_ABI ggml_type_name;
- typeof(ggml_element_size) *GGML_ABI ggml_element_size;
- typeof(ggml_row_size) *GGML_ABI ggml_row_size;
- typeof(ggml_rope_yarn_corr_dims) *GGML_ABI ggml_rope_yarn_corr_dims;
- typeof(ggml_op_desc) *GGML_ABI ggml_op_desc;
+ void (*GGML_ABI free)(void *);
+ void *(*GGML_ABI malloc)(size_t);
+ ggml_backend_buffer_t (*GGML_ABI ggml_backend_buffer_init)(ggml_backend_buffer_type_t, struct ggml_backend_buffer_i, ggml_backend_buffer_context_t, size_t);
+ ggml_backend_buffer_t (*GGML_ABI ggml_backend_cpu_buffer_from_ptr)(void *, size_t);
+ ggml_backend_buffer_type_t (*GGML_ABI ggml_backend_cpu_buffer_type)(void);
+ size_t (*GGML_ABI ggml_backend_buft_get_alloc_size)(ggml_backend_buffer_type_t, struct ggml_tensor *);
+ ggml_backend_buffer_t (*GGML_ABI ggml_backend_buft_alloc_buffer)(ggml_backend_buffer_type_t, size_t);
+ bool (*GGML_ABI ggml_backend_is_cpu)(ggml_backend_t);
+ void (*GGML_ABI ggml_backend_tensor_get)(const struct ggml_tensor *, void *, size_t, size_t);
+ void (*GGML_ABI ggml_backend_tensor_set)(struct ggml_tensor *, const void *, size_t, size_t);
+ bool (*GGML_ABI ggml_is_quantized)(enum ggml_type);
+ size_t (*GGML_ABI ggml_type_size)(enum ggml_type);
+ int (*GGML_ABI ggml_blck_size)(enum ggml_type);
+ bool (*GGML_ABI ggml_is_transposed)(const struct ggml_tensor *);
+ size_t (*GGML_ABI ggml_nbytes)(const struct ggml_tensor *);
+ enum ggml_unary_op (*GGML_ABI ggml_get_unary_op)(const struct ggml_tensor *);
+ int64_t (*GGML_ABI ggml_nelements)(const struct ggml_tensor *);
+ int64_t (*GGML_ABI ggml_nrows)(const struct ggml_tensor *);
+ bool (*GGML_ABI ggml_is_permuted)(const struct ggml_tensor *);
+ bool (*GGML_ABI ggml_is_contiguous)(const struct ggml_tensor *);
+ const char *(*GGML_ABI ggml_op_name)(enum ggml_op);
+ const char *(*GGML_ABI ggml_type_name)(enum ggml_type);
+ size_t (*GGML_ABI ggml_element_size)(const struct ggml_tensor *);
+ size_t (*GGML_ABI ggml_row_size)(enum ggml_type, int64_t);
+ void (*GGML_ABI ggml_rope_yarn_corr_dims)(int, int, float, float, float, float[2]);
+ const char *(*GGML_ABI ggml_op_desc)(const struct ggml_tensor *);
};
#ifdef __cplusplus
diff --git a/llama.cpp/ggml-backend.c b/llama.cpp/ggml-backend.c
index d4b37e93..7c0b2c28 100644
--- a/llama.cpp/ggml-backend.c
+++ b/llama.cpp/ggml-backend.c
@@ -1432,9 +1432,17 @@ void ggml_backend_compare_graph_backend(ggml_backend_t backend1, ggml_backend_t
ggml_backend_graph_copy_free(copy);
}
+GGML_ABI void system_free(void *p) {
+ free(p);
+}
+
+GGML_ABI void *system_malloc(size_t n) {
+ return malloc(n);
+}
+
static const struct ggml_backend_api kGgmlBackendApi = {
- free,
- malloc,
+ system_free,
+ system_malloc,
ggml_backend_buffer_init,
ggml_backend_cpu_buffer_from_ptr,
ggml_backend_cpu_buffer_type,
diff --git a/llama.cpp/ggml-cuda.cu b/llama.cpp/ggml-cuda.cu
index 583c8849..59c5438e 100644
--- a/llama.cpp/ggml-cuda.cu
+++ b/llama.cpp/ggml-cuda.cu
@@ -224,8 +224,21 @@
#include <cuda.h>
#include <cublas_v2.h>
#include <cuda_fp16.h>
+
+#if CUDART_VERSION < 11020
+#define CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED CU_DEVICE_ATTRIBUTE_VIRTUAL_ADDRESS_MANAGEMENT_SUPPORTED
+#define CUBLAS_TF32_TENSOR_OP_MATH CUBLAS_TENSOR_OP_MATH
+#define CUBLAS_COMPUTE_16F CUDA_R_16F
+#define CUBLAS_COMPUTE_32F CUDA_R_32F
+#define cublasComputeType_t cudaDataType_t
+#endif // CUDART_VERSION < 11020
+
#endif // defined(GGML_USE_HIPBLAS)
+#include "ggml-cuda.h"
+#include "ggml.h"
+#include "ggml-backend-impl.h"
+
#ifdef GGML_USE_TINYBLAS
#define CUBLAS_ENTRY() cudaStream_t _ggml_stream = nullptr
#define CUBLAS_SET_STREAM(_, stream) do _ggml_stream = (stream); while (0)
@@ -236,19 +249,6 @@
#define CUBLAS_HANDLE(id) g_cublas_handles[id]
#endif
-#include "ggml-cuda.h"
-#include "ggml.h"
-#include "ggml-backend-impl.h"
-
-#undef MIN
-#undef MAX
-#define MIN(a, b) ((a) < (b) ? (a) : (b))
-#define MAX(a, b) ((a) > (b) ? (a) : (b))
-
-#ifndef M_PI
-#define M_PI 3.14159265358979323846
-#endif
-
static const struct ggml_backend_api *g_backend;
#define ggml_is_quantized g_backend->ggml_is_quantized
#define ggml_type_size g_backend->ggml_type_size
@@ -10264,6 +10264,8 @@ static ggml_backend_i cuda_backend_i = {
};
ggml_backend_t ggml_backend_cuda_init(int device) {
+ ggml_init_cublas(); // TODO: remove from ggml.c
+
if (device < 0 || device >= ggml_cuda_get_device_count()) {
fprintf(stderr, "%s: error: invalid device %d\n", __func__, device);
return nullptr;