This commit is contained in:
lexasub 2025-02-09 17:33:10 -05:00 committed by GitHub
commit 95a2982558
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,13 +1,13 @@
#include "llama-mmap.h" #include "llama-mmap.h"
#include "llama-impl.h" #include <cerrno>
#include <climits>
#include <cstring>
#include <stdexcept>
#include <thread>
#include "ggml.h" #include "ggml.h"
#include "llama-impl.h"
#include <cstring>
#include <climits>
#include <stdexcept>
#include <cerrno>
#ifdef __has_include #ifdef __has_include
#if __has_include(<unistd.h>) #if __has_include(<unistd.h>)
@ -274,11 +274,20 @@ struct llama_mmap::impl {
int flags = MAP_SHARED; int flags = MAP_SHARED;
if (numa) { prefetch = 0; } if (numa) { prefetch = 0; }
#ifdef __linux__ #ifdef __linux__
if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL)) { if (file->size() > 1024 * 1024 * 1024) { // 1GB+
LLAMA_LOG_WARN("warning: posix_fadvise(.., POSIX_FADV_SEQUENTIAL) failed: %s\n", if (posix_fadvise(fd, 0, 0, POSIX_FADV_WILLNEED | POSIX_FADV_SEQUENTIAL)) {
strerror(errno)); LLAMA_LOG_WARN("warning: posix_fadvise(.., POSIX_FADV_WILLNEED, POSIX_FADV_SEQUENTIAL) failed: %s\n",
strerror(errno));
}
} else {
if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL)) {
LLAMA_LOG_WARN("warning: posix_fadvise(.., POSIX_FADV_SEQUENTIAL) failed: %s\n",
strerror(errno));
}
}
if (prefetch && file->size() <= 1024 * 1024 * 512) { // 512 MB threshold
flags |= MAP_POPULATE;
} }
if (prefetch) { flags |= MAP_POPULATE; }
#endif #endif
addr = mmap(NULL, file->size(), PROT_READ, flags, fd, 0); addr = mmap(NULL, file->size(), PROT_READ, flags, fd, 0);
if (addr == MAP_FAILED) { if (addr == MAP_FAILED) {
@ -286,10 +295,16 @@ struct llama_mmap::impl {
} }
if (prefetch > 0) { if (prefetch > 0) {
if (posix_madvise(addr, std::min(file->size(), prefetch), POSIX_MADV_WILLNEED)) { #ifdef __linux__
LLAMA_LOG_WARN("warning: posix_madvise(.., POSIX_MADV_WILLNEED) failed: %s\n", std::thread([addr=addr, size = file->size(), prefetch] {
strerror(errno)); posix_madvise(addr, std::min(size, prefetch), POSIX_MADV_WILLNEED);
} }).detach();
#elif defined(_WIN32)
std::thread([](void* addr, SIZE_T size) {
WIN32_MEMORY_RANGE_ENTRY range = { addr, size };
PrefetchVirtualMemory(GetCurrentProcess(), 1, &range, 0);
}, addr, std::min(file->size(), prefetch)).detach();
#endif
} }
if (numa) { if (numa) {
if (posix_madvise(addr, file->size(), POSIX_MADV_RANDOM)) { if (posix_madvise(addr, file->size(), POSIX_MADV_RANDOM)) {