Skip to content

Commit 2f5f09b

Browse files
committed
add GGML_NUMA_MIRROR macro and modify tensor def
1 parent 6773189 commit 2f5f09b

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

ggml/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ option(GGML_OPENCL_USE_ADRENO_KERNELS "ggml: use optimized kernels for Adr
198198
set (GGML_OPENCL_TARGET_VERSION "300" CACHE STRING
199199
"gmml: OpenCL API version to target")
200200

201+
option(GGML_NUMA_MIRROR "ggml: support numa aware tensor data" OFF)
202+
201203
# toolchain for vulkan-shaders-gen
202204
set (GGML_VULKAN_SHADERS_GEN_TOOLCHAIN "" CACHE FILEPATH "ggml: toolchain file for vulkan-shaders-gen")
203205

@@ -317,6 +319,18 @@ set(variable_set_statements
317319

318320
set(GGML_SHARED_LIB ${BUILD_SHARED_LIBS})
319321

322+
if (GGML_NUMA_MIRROR)
323+
message(STATUS
324+
"-----------------\n"
325+
"Enabling GGML_NUMA_MIRROR"
326+
message(STATUS
327+
"-----------------")
328+
329+
foreach(lib "ggml" "ggml-base")
330+
target_compile_definitions(${lib} PUBLIC GGML_NUMA_MIRROR)
331+
endforeach()
332+
endif()
333+
320334
get_cmake_property(all_variables VARIABLES)
321335
foreach(variable_name IN LISTS all_variables)
322336
if(variable_name MATCHES "^GGML_")

ggml/include/ggml.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,21 +598,46 @@ extern "C" {
598598
struct ggml_tensor * view_src;
599599
size_t view_offs;
600600

601+
#ifdef GGML_NUMA_MIRROR
602+
union {
603+
#ifdef __NVCC__
604+
void * data;
605+
#endif
606+
void * __data[2];
607+
};
608+
#else
601609
void * data;
610+
#endif
602611

603612
char name[GGML_MAX_NAME];
604613

605614
void * extra; // extra things e.g. for ggml-cuda.cu
606615

616+
#ifndef GGML_NUMA_MIRROR
607617
char padding[8];
618+
#endif
608619
};
609620

621+
#ifdef GGML_NUMA_MIRROR
622+
extern __thread int ggml_current_numa_node;
623+
#endif
624+
610625
static inline void * tensor_data(const struct ggml_tensor * tensor) {
626+
#ifdef GGML_NUMA_MIRROR
627+
int n = ggml_current_numa_node;
628+
return tensor->__data[n];
629+
#else
611630
return tensor->data;
631+
#endif
612632
}
613633

614634
static inline void tensor_set_data(struct ggml_tensor * tensor, void * data) {
635+
#ifdef GGML_NUMA_MIRROR
636+
tensor->__data[0] = data;
637+
tensor->__data[1] = data;
638+
#else
615639
tensor->data = data;
640+
#endif
616641
}
617642

618643
static const size_t GGML_TENSOR_SIZE = sizeof(struct ggml_tensor);

ggml/src/ggml.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@
6060
#define m512i(p) (__m512i)(p)
6161
#endif
6262

63+
#ifdef GGML_NUMA_MIRROR
64+
__thread int ggml_current_numa_node = 0;
65+
#endif
66+
6367
// precomputed f32 table for f16 (256 KB) (ggml-impl.h)
6468
float ggml_table_f32_f16[1 << 16];
6569

@@ -1609,10 +1613,16 @@ static struct ggml_tensor * ggml_new_tensor_impl(
16091613
/*.src =*/ { NULL },
16101614
/*.view_src =*/ view_src,
16111615
/*.view_offs =*/ view_offs,
1616+
#ifdef GGML_NUMA_MIRROR
1617+
/*.data =*/ { .__data = { NULL, NULL } },
1618+
#else
16121619
/*.data =*/ NULL,
1620+
#endif
16131621
/*.name =*/ { 0 },
16141622
/*.extra =*/ NULL,
1623+
#ifndef GGML_NUMA_MIRROR
16151624
/*.padding =*/ { 0 },
1625+
#endif
16161626
};
16171627
tensor_set_data(result, obj_alloc_size > 0 ? (void *)(result + 1) : data);
16181628

0 commit comments

Comments
 (0)