Skip to content

Commit d2e2080

Browse files
committed
add cpu hbm support
1 parent 8afe228 commit d2e2080

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,11 @@ endif()
541541

542542
# ggml
543543

544+
if (GGML_USE_CPU_HBM)
545+
add_definitions(-DGGML_USE_CPU_HBM)
546+
find_library(memkind memkind REQUIRED)
547+
endif()
548+
544549
add_library(ggml OBJECT
545550
ggml.c
546551
ggml.h
@@ -556,6 +561,9 @@ add_library(ggml OBJECT
556561
target_include_directories(ggml PUBLIC . ${LLAMA_EXTRA_INCLUDES})
557562
target_compile_features(ggml PUBLIC c_std_11) # don't bump
558563
target_link_libraries(ggml PUBLIC Threads::Threads ${LLAMA_EXTRA_LIBS})
564+
if (GGML_USE_CPU_HBM)
565+
target_link_libraries(ggml PUBLIC memkind)
566+
endif()
559567

560568
add_library(ggml_static STATIC $<TARGET_OBJECTS:ggml>)
561569
if (BUILD_SHARED_LIBS)

ggml.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ typedef void * thread_ret_t;
103103
#include <sys/stat.h>
104104
#include <unistd.h>
105105

106+
#endif
107+
#ifdef GGML_USE_CPU_HBM
108+
#include <hbwmalloc.h>
106109
#endif
107110

108111
// __FMA__ and __F16C__ are not defined in MSVC, however they are implied with AVX2/AVX512
@@ -193,7 +196,13 @@ typedef void * thread_ret_t;
193196
#else
194197
inline static void * ggml_aligned_malloc(size_t size) {
195198
void * aligned_memory = NULL;
196-
#ifdef GGML_USE_METAL
199+
#ifdef GGML_USE_CPU_HBM
200+
if (size == 0) {
201+
GGML_PRINT("WARNING: Behavior may be unexpected when allocate 0 byte for hbw_posix_memalign!");
202+
return NULL;
203+
}
204+
int result = hbw_posix_memalign(&aligned_memory, 16, size);
205+
#elif GGML_USE_METAL
197206
int result = posix_memalign(&aligned_memory, getpagesize(), size);
198207
#else
199208
int result = posix_memalign(&aligned_memory, GGML_MEM_ALIGN, size);
@@ -215,8 +224,12 @@ inline static void * ggml_aligned_malloc(size_t size) {
215224
return aligned_memory;
216225
}
217226
#define GGML_ALIGNED_MALLOC(size) ggml_aligned_malloc(size)
227+
#ifdef GGML_USE_CPU_HBM
228+
#define GGML_ALIGNED_FREE(ptr) if(NULL != ptr) hbw_free(ptr)
229+
#else
218230
#define GGML_ALIGNED_FREE(ptr) free(ptr)
219231
#endif
232+
#endif
220233

221234
#define UNUSED GGML_UNUSED
222235
#define SWAP(x, y, T) do { T SWAP = x; x = y; y = SWAP; } while (0)

llama.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ void replace_all(std::string & s, const std::string & search, const std::string
126126
}
127127
s = std::move(result);
128128
}
129+
#ifdef GGML_USE_CPU_HBM
130+
#include <hbwmalloc.h>
131+
#endif
129132

130133
static void zeros(std::ofstream & file, size_t n) {
131134
char zero = 0;
@@ -412,6 +415,9 @@ static void ggml_graph_compute_helper(std::vector<uint8_t> & buf, ggml_cgraph *
412415
#elif GGML_USE_METAL
413416
# define llama_host_malloc(n) ggml_metal_host_malloc(n)
414417
# define llama_host_free(data) ggml_metal_host_free(data)
418+
#elif GGML_USE_CPU_HBM
419+
# define llama_host_malloc(n) hbw_malloc(n)
420+
# define llama_host_free(data) if(data != NULL) hbw_free(data)
415421
#else
416422
# define llama_host_malloc(n) malloc(n)
417423
# define llama_host_free(data) free(data)
@@ -1446,7 +1452,11 @@ struct llama_model_loader {
14461452
// allocate temp buffer if not using mmap
14471453
if (!use_mmap && cur->data == NULL) {
14481454
GGML_ASSERT(cur->backend != GGML_BACKEND_CPU);
1449-
cur->data = malloc(ggml_nbytes(cur));
1455+
#ifdef GGML_USE_CPU_HBM
1456+
cur->data = (uint8_t*)hbw_malloc(ggml_nbytes(cur));
1457+
#else
1458+
cur->data = (uint8_t*)malloc(ggml_nbytes(cur));
1459+
#endif
14501460
}
14511461

14521462
load_data_for(cur);
@@ -5327,7 +5337,7 @@ void llama_backend_init(bool numa) {
53275337

53285338
// needed to initialize f16 tables
53295339
{
5330-
struct ggml_init_params params = { 0, NULL, false };
5340+
struct ggml_init_params params = { 1, NULL, false };
53315341
struct ggml_context * ctx = ggml_init(params);
53325342
ggml_free(ctx);
53335343
}

0 commit comments

Comments
 (0)