Revert "Unmap the file in llama_free"

This reverts commit 276e5b7811.
This commit is contained in:
anzz1 2023-04-02 15:39:53 +03:00
parent 9569171712
commit 7ae9e74031

View file

@ -149,10 +149,6 @@ struct llama_model {
// the model memory buffer // the model memory buffer
std::vector<uint8_t> buf; std::vector<uint8_t> buf;
// model memory mapped file
void * mm_addr;
size_t mm_length;
// tensors // tensors
int n_loaded; int n_loaded;
std::unordered_map<std::string, struct ggml_tensor *> tensors; std::unordered_map<std::string, struct ggml_tensor *> tensors;
@ -300,32 +296,22 @@ struct llama_context_params llama_context_default_params() {
// model loading // model loading
// //
static void mmap_file(const char* fname, void * &mm_addr, size_t &mm_length) { static void * mmap_file(const char* fname) {
#if defined(MAP_FAILED) #if defined(MAP_FAILED)
// POSIX // POSIX mmap
int fd = open(fname, O_RDONLY); int fd = open(fname, O_RDONLY);
mm_length = lseek(fd, 0, SEEK_END); size_t len = lseek(fd, 0, SEEK_END);
mm_addr = mmap(NULL, mm_length, PROT_READ, MAP_SHARED, fd, 0); void * mm_addr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
close(fd);
if (mm_addr == MAP_FAILED) { if (mm_addr == MAP_FAILED) {
perror("mmap failed"); perror("mmap failed");
mm_addr = NULL; mm_addr = NULL;
mm_length = 0;
} }
close(fd);
return mm_addr;
#else #else
// TODO: windows support // TODO: windows support
(void)(fname); // suppress warnings (void)(fname); // suppress warnings
#endif return NULL;
}
static void munmap_file(void * addr, size_t length) {
#if defined(MAP_FAILED)
// POSIX
munmap(addr, length);
#else
// TODO: windows support
(void)(addr); // suppress warnings
(void)(length);
#endif #endif
} }
@ -494,15 +480,12 @@ static bool llama_model_load(
bool use_mmap = (n_parts == 1); bool use_mmap = (n_parts == 1);
// try to memory map the model file // try to memory map the model file
void * mm_addr = NULL; void* mm_addr = NULL;
if (use_mmap) { if (use_mmap) {
mmap_file(fname.c_str(), model.mm_addr, model.mm_length); mm_addr = mmap_file(fname.c_str());
if (model.mm_addr == NULL) { if (mm_addr == NULL) {
use_mmap = false; use_mmap = false;
} }
else {
mm_addr = model.mm_addr;
}
} }
auto & ctx = model.ctx; auto & ctx = model.ctx;
@ -1767,10 +1750,6 @@ void llama_free(struct llama_context * ctx) {
ggml_free(ctx->model.ctx); ggml_free(ctx->model.ctx);
} }
if (ctx->model.mm_addr) {
munmap_file(ctx->model.mm_addr, ctx->model.mm_length);
}
delete ctx; delete ctx;
} }