Skip to content

Commit ff06047

Browse files
committed
feat: NVMe->VRAM demand weight paging via SAM (Phases 1-4)
- llama-weight-pager: page table, VRAM slot pool, LRU eviction - page_in via pread into hipExtMallocWithFlags fine-grained VRAM (SAM path) - ggml eval callback hook: redirects tensor data pointers before each op - async io_uring prefetch: pipelines next layer NVMe read with GPU compute - CLI: --weight-paging, --weight-paging-slots, --weight-paging-prefetch - metrics: weight pager stats exposed at /metrics endpoint Fixes: hipMalloc pool alloc, strcmp tensor name comparison, slot eviction page invalidation, llama-model.h include for server-context.
1 parent d22c215 commit ff06047

8 files changed

Lines changed: 578 additions & 8 deletions

File tree

common/arg.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,6 +2430,27 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
24302430
}
24312431
}
24322432
).set_env("LLAMA_ARG_N_GPU_LAYERS"));
2433+
add_opt(common_arg(
2434+
{"--weight-paging"}, "",
2435+
"enable NVMe→VRAM demand paging for model weights (allows models larger than VRAM)",
2436+
[](common_params & params, const std::string & value) {
2437+
params.weight_paging_enabled = true;
2438+
}
2439+
).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_WEIGHT_PAGING"));
2440+
add_opt(common_arg(
2441+
{"--weight-paging-slots"}, "N",
2442+
"number of VRAM slots for weight paging (-1 = auto, default: -1)",
2443+
[](common_params & params, const std::string & value) {
2444+
params.weight_paging_slots = std::stoi(value);
2445+
}
2446+
).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_WEIGHT_PAGING_SLOTS"));
2447+
add_opt(common_arg(
2448+
{"--weight-paging-prefetch"}, "",
2449+
"enable async prefetch of next layer (default: enabled)",
2450+
[](common_params & params, const std::string & value) {
2451+
params.weight_paging_prefetch = true;
2452+
}
2453+
).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_WEIGHT_PAGING_PREFETCH"));
24332454
add_opt(common_arg(
24342455
{"-sm", "--split-mode"}, "{none,layer,row,tensor}",
24352456
"how to split the model across multiple GPUs, one of:\n"

common/common.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,11 @@ struct common_params {
533533
bool no_extra_bufts = false; // disable extra buffer types (used for weight repacking)
534534
bool no_host = false; // bypass host buffer allowing extra buffers to be used
535535

536+
// weight paging parameters
537+
bool weight_paging_enabled = false; // enable NVMe→VRAM demand paging for model weights
538+
int32_t weight_paging_slots = -1; // number of VRAM slots for weight paging (-1 = auto)
539+
bool weight_paging_prefetch = true; // enable async prefetch of next layer
540+
536541
bool single_turn = false; // single turn chat conversation
537542

538543
ggml_type cache_type_k = GGML_TYPE_F16; // KV cache data type for the K

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ add_library(llama
3434
llama-io-uring.cpp
3535
llama-model-saver.cpp
3636
llama-model.cpp
37+
llama-weight-pager.cpp
3738
llama-quant.cpp
3839
llama-sampler.cpp
3940
llama-vocab.cpp

src/llama-model-loader.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "llama-arch.h"
77
#include "llama-hparams.h"
88
#include "llama-mmap.h"
9+
#include "llama-weight-pager.h"
910

1011
#include "ggml-cpp.h"
1112

@@ -28,6 +29,13 @@ enum llama_fver {
2829

2930
const char * llama_file_version_name(llama_fver version);
3031

32+
// Information about a weight tensor for the weight pager
33+
struct llama_weight_page_info {
34+
std::string name; // tensor name
35+
size_t offset; // file offset in GGUF
36+
size_t size; // tensor size in bytes
37+
};
38+
3139
struct llama_model_loader {
3240
// Holds information on a model weight
3341
struct llama_tensor_weight {
@@ -90,6 +98,9 @@ struct llama_model_loader {
9098
std::unordered_map<std::string, llama_model_kv_override> kv_overrides;
9199
const llama_model_tensor_buft_override * tensor_buft_overrides;
92100

101+
// weight pager info (Phase 2)
102+
std::vector<llama_weight_page_info> weight_page_infos;
103+
93104
gguf_context_ptr metadata_ptr;
94105
struct gguf_context * metadata; // either metadata_ptr.get() or externally set
95106
llama_model_set_tensor_data_t set_tensor_data;

src/llama-model.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "llama-hparams.h"
77
#include "llama-memory.h"
88
#include "llama-vocab.h"
9+
#include "llama-weight-pager.h"
910

1011
#include <map>
1112
#include <memory>
@@ -571,6 +572,9 @@ struct llama_model {
571572
// for keeping track of associated LoRA adapters
572573
std::unordered_set<llama_adapter_lora *> loras;
573574

575+
// weight paging for NVMe→VRAM demand paging
576+
std::unique_ptr<llama_weight_pager> weight_pager;
577+
574578
// statically allocated context for assigning
575579
struct llama_meta_device_get_split_state_userdata get_split_state_ud;
576580

0 commit comments

Comments
 (0)