Skip to content

Commit 6f23ba5

Browse files
committed
Ensure --mlock works properly with mmap() support
1 parent 78ca983 commit 6f23ba5

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

ggml.c

+25-14
Original file line numberDiff line numberDiff line change
@@ -2884,36 +2884,47 @@ size_t ggml_set_scratch(struct ggml_context * ctx, struct ggml_scratch scratch)
28842884
return result;
28852885
}
28862886

2887+
#ifdef __APPLE__
2888+
#define MLOCK_SUGGESTION \
2889+
"Try increasing the sysctl values 'vm.user_wire_limit' and 'vm.global_user_wire_limit' and/or " \
2890+
"decreasing 'vm.global_no_user_wire_amount'. Also try increasing RLIMIT_MLOCK (ulimit -l).\n"
2891+
#else
2892+
#define MLOCK_SUGGESTION \
2893+
"Try increasing RLIMIT_MLOCK ('ulimit -l' as root).\n"
2894+
#endif
2895+
28872896
bool ggml_mlock_supported(void) {
28882897
return GGML_MLOCK_SUPPORT;
28892898
}
28902899

2900+
bool ggml_mlock(
2901+
struct ggml_context * ctx,
2902+
const void *opt_extra_addr,
2903+
size_t opt_extra_len,
2904+
char **err_p) {
2905+
// TODO: Use SetProcessWorkingSetSize() + VirtualLock() on WIN32
28912906
#if GGML_MLOCK_SUPPORT
2892-
#ifdef __APPLE__
2893-
#define MLOCK_SUGGESTION "Try increasing the sysctl values 'vm.user_wire_limit' and 'vm.global_user_wire_limit' and/or\n" \
2894-
"decreasing 'vm.global_no_user_wire_amount'. Also try increasing RLIMIT_MLOCK (ulimit -l)."
2895-
#else
2896-
#define MLOCK_SUGGESTION "Try increasing RLIMIT_MLOCK (ulimit -l)."
2897-
#endif
2898-
bool ggml_mlock(struct ggml_context * ctx, char ** err_p) {
28992907
if (ctx->mem_buffer_mlocked) {
29002908
return true;
29012909
}
2902-
if (mlock(ctx->mem_buffer, ctx->mem_size)) {
2903-
int ret = asprintf(err_p, "failed to mlock %zu-byte buffer: %s\n" MLOCK_SUGGESTION,
2904-
ctx->mem_size, strerror(errno));
2905-
GGML_ASSERT(ret >= 0);
2910+
if (mlock(ctx->mem_buffer, ctx->mem_size) ||
2911+
(opt_extra_len &&
2912+
mlock(opt_extra_addr, opt_extra_len))) {
2913+
if ((*err_p = malloc(1024))) {
2914+
snprintf(*err_p, 1024,
2915+
"failed to mlock %zu-byte buffer: %s\n" MLOCK_SUGGESTION,
2916+
ctx->mem_size + opt_extra_len,
2917+
strerror(errno));
2918+
}
29062919
return false;
29072920
}
29082921
ctx->mem_buffer_mlocked = true;
29092922
return true;
2910-
}
29112923
#else // GGML_MLOCK_SUPPORT
2912-
bool ggml_mlock(struct ggml_context * ctx, char ** err_p) {
29132924
*err_p = strdup("can't mlock because it's not supported on this system");
29142925
return false;
2915-
}
29162926
#endif // GGML_MLOCK_SUPPORT
2927+
}
29172928

29182929
////////////////////////////////////////////////////////////////////////////////
29192930

ggml.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,11 @@ size_t ggml_used_mem(const struct ggml_context * ctx);
345345
size_t ggml_set_scratch(struct ggml_context * ctx, struct ggml_scratch scratch);
346346

347347
bool ggml_mlock_supported(void);
348-
bool ggml_mlock(struct ggml_context * ctx, char ** err_p);
348+
bool ggml_mlock(
349+
struct ggml_context * ctx,
350+
const void *opt_extra_addr,
351+
size_t opt_extra_len,
352+
char **err_p);
349353

350354
struct ggml_tensor * ggml_new_tensor(
351355
struct ggml_context * ctx,

llama.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,10 @@ struct llama_context * llama_init_from_file(
15951595

15961596
if (params.use_mlock) {
15971597
char *err;
1598-
if (!ggml_mlock(ctx->model.ctx, &err)) {
1598+
if (!ggml_mlock(ctx->model.ctx,
1599+
ctx->model.mm_addr,
1600+
ctx->model.mm_length,
1601+
&err)) {
15991602
fprintf(stderr, "%s\n", err);
16001603
free(err);
16011604
llama_free(ctx);

0 commit comments

Comments
 (0)