summaryrefslogtreecommitdiff
path: root/ggml.c
diff options
context:
space:
mode:
authorKunshang Ji <kunshang.ji@intel.com>2023-09-08 09:46:56 +0800
committerGitHub <noreply@github.com>2023-09-08 03:46:56 +0200
commit7f412dab9c8801f5d37904f7dce1faf4c2b43b42 (patch)
treebc1b629e787a261378a7b123b1e7299464ab2c7f /ggml.c
parent6336d834ec7bff3e93e24182c0f609d2f2bdce26 (diff)
enable CPU HBM (#2603)
* add cpu hbm support * add memalign 0 byte check * Update ggml.c * Update llama.cpp * ggml : allow ggml_init with 0 size * retrigger ci * fix code style --------- Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
Diffstat (limited to 'ggml.c')
-rw-r--r--ggml.c20
1 files changed, 19 insertions, 1 deletions
diff --git a/ggml.c b/ggml.c
index 8a677ab2..a4b9781d 100644
--- a/ggml.c
+++ b/ggml.c
@@ -104,6 +104,9 @@ typedef void * thread_ret_t;
#include <unistd.h>
#endif
+#ifdef GGML_USE_CPU_HBM
+#include <hbwmalloc.h>
+#endif
// __FMA__ and __F16C__ are not defined in MSVC, however they are implied with AVX2/AVX512
#if defined(_MSC_VER) && (defined(__AVX2__) || defined(__AVX512F__))
@@ -192,8 +195,14 @@ typedef void * thread_ret_t;
#define GGML_ALIGNED_FREE(ptr) _aligned_free(ptr)
#else
inline static void * ggml_aligned_malloc(size_t size) {
+ if (size == 0) {
+ GGML_PRINT("WARNING: Behavior may be unexpected when allocating 0 bytes for ggml_aligned_malloc!\n");
+ return NULL;
+ }
void * aligned_memory = NULL;
-#ifdef GGML_USE_METAL
+#ifdef GGML_USE_CPU_HBM
+ int result = hbw_posix_memalign(&aligned_memory, 16, size);
+#elif GGML_USE_METAL
int result = posix_memalign(&aligned_memory, sysconf(_SC_PAGESIZE), size);
#else
int result = posix_memalign(&aligned_memory, GGML_MEM_ALIGN, size);
@@ -215,8 +224,12 @@ inline static void * ggml_aligned_malloc(size_t size) {
return aligned_memory;
}
#define GGML_ALIGNED_MALLOC(size) ggml_aligned_malloc(size)
+#ifdef GGML_USE_CPU_HBM
+#define GGML_ALIGNED_FREE(ptr) if(NULL != ptr) hbw_free(ptr)
+#else
#define GGML_ALIGNED_FREE(ptr) free(ptr)
#endif
+#endif
#define UNUSED GGML_UNUSED
#define SWAP(x, y, T) do { T SWAP = x; x = y; y = SWAP; } while (0)
@@ -4566,6 +4579,11 @@ struct ggml_context * ggml_init(struct ggml_init_params params) {
return NULL;
}
+ // allow to call ggml_init with 0 size
+ if (params.mem_size == 0) {
+ params.mem_size = GGML_MEM_ALIGN;
+ }
+
const size_t mem_size = params.mem_buffer ? params.mem_size : GGML_PAD(params.mem_size, GGML_MEM_ALIGN);
*ctx = (struct ggml_context) {