Skip to content

Commit 7f3c896

Browse files
authored
Metal: mlock model weights in memory (#170)
1 parent 95d7716 commit 7f3c896

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

gpt_oss/metal/source/include/internal/model.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <stdatomic.h>
4+
#include <stdbool.h>
45
#include <stddef.h>
56
#include <stdint.h>
67

@@ -54,6 +55,8 @@ struct gptoss_model {
5455
// Once the batch size is reached, we process it to fill the KV cache.
5556
size_t max_batch_tokens;
5657

58+
bool lock_memory;
59+
5760
size_t weights_size;
5861
size_t allocation_size;
5962

gpt_oss/metal/source/model.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,12 @@ enum gptoss_status GPTOSS_ABI gptoss_model_create_from_file(
290290

291291
prefetch_fd(fd, model_mapping_start, model_mapping_size, path);
292292

293+
if (mlock(model_mapping_ptr, model_mapping_size) != 0) {
294+
GPTOSS_LOG_WARNING("mlock(%s, size=%zu) failed with error %d", path, model_mapping_size, errno);
295+
} else {
296+
model->lock_memory = true;
297+
}
298+
293299
// Initialize Metal
294300
status = gptoss_metal_device_create_system_default(&model->device);
295301
if (status != gptoss_status_success) {
@@ -497,6 +503,12 @@ enum gptoss_status GPTOSS_ABI gptoss_model_release(
497503
// Weight buffers
498504

499505
if (model->mapping_ptr != NULL && model->mapping_size != 0) {
506+
if (model->lock_memory) {
507+
if (munlock(model->mapping_ptr, model->mapping_size) != 0) {
508+
GPTOSS_LOG_WARNING("munlock for model weight mapping failed with error %d", errno);
509+
}
510+
}
511+
500512
if (munmap(model->mapping_ptr, model->mapping_size) != 0) {
501513
GPTOSS_LOG_WARNING("munmap for model weight mapping failed with error %d", errno);
502514
}

0 commit comments

Comments
 (0)