Page-align buffers used by Metal

This commit is contained in:
Kilty McGowan 2023-06-04 23:16:45 -07:00
parent bc04508666
commit a9d0bea047
2 changed files with 21 additions and 0 deletions

5
ggml.c
View file

@ -20,6 +20,7 @@
#include <stdio.h> #include <stdio.h>
#include <float.h> #include <float.h>
#include <limits.h> #include <limits.h>
#include <unistd.h>
// if C99 - static_assert is noop // if C99 - static_assert is noop
// ref: https://stackoverflow.com/a/53923785/4039976 // ref: https://stackoverflow.com/a/53923785/4039976
@ -121,7 +122,11 @@ typedef void* thread_ret_t;
#else #else
inline static void* ggml_aligned_malloc(size_t size) { inline static void* ggml_aligned_malloc(size_t size) {
void* aligned_memory = NULL; void* aligned_memory = NULL;
#ifdef GGML_USE_METAL
int result = posix_memalign(&aligned_memory, getpagesize(), size);
#else
int result = posix_memalign(&aligned_memory, GGML_MEM_ALIGN, size); int result = posix_memalign(&aligned_memory, GGML_MEM_ALIGN, size);
#endif
if (result != 0) { if (result != 0) {
// Handle allocation failure // Handle allocation failure
return NULL; return NULL;

View file

@ -405,13 +405,29 @@ struct llama_buffer {
llama_buffer() = default; llama_buffer() = default;
void resize(size_t len) { void resize(size_t len) {
#ifdef GGML_USE_METAL
free(addr);
int result = posix_memalign((void **) &addr, getpagesize(), len);
if (result == 0) {
memset(addr, 0, len);
}
else {
addr = NULL;
}
#else
delete[] addr; delete[] addr;
addr = new uint8_t[len]; addr = new uint8_t[len];
#endif
size = len; size = len;
} }
~llama_buffer() { ~llama_buffer() {
#ifdef GGML_USE_METAL
free(addr);
#else
delete[] addr; delete[] addr;
#endif
addr = NULL;
} }
// disable copy and move // disable copy and move