Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fa404a9d97 |
+21
-9
@@ -27,9 +27,11 @@
|
||||
#include <algorithm>
|
||||
#include <cinttypes>
|
||||
#include <climits>
|
||||
#include <cmath>
|
||||
#include <cstdarg>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <limits>
|
||||
#include <list>
|
||||
#include <regex>
|
||||
#include <set>
|
||||
@@ -56,6 +58,19 @@
|
||||
using json = nlohmann::ordered_json;
|
||||
using namespace common_arg_utils;
|
||||
|
||||
static uint64_t parse_longhaul_cache_gib(const std::string & value) {
|
||||
size_t end = 0;
|
||||
const double gib = std::stod(value, &end);
|
||||
constexpr long double bytes_per_gib = 1024.0L * 1024.0L * 1024.0L;
|
||||
const long double bytes = (long double) gib * bytes_per_gib;
|
||||
|
||||
if (end != value.size() || !std::isfinite(gib) || gib <= 0.0 ||
|
||||
bytes < 1.0L || bytes > (long double) std::numeric_limits<uint64_t>::max()) {
|
||||
throw std::invalid_argument("longhaul cache size must be a positive GiB value");
|
||||
}
|
||||
return (uint64_t) bytes;
|
||||
}
|
||||
|
||||
static std::initializer_list<enum llama_example> mmproj_examples = {
|
||||
LLAMA_EXAMPLE_MTMD,
|
||||
LLAMA_EXAMPLE_SERVER,
|
||||
@@ -815,7 +830,7 @@ static bool common_params_parse_ex(int argc, char ** argv, common_params_context
|
||||
parse_cli_args();
|
||||
|
||||
if (params.load_mode == LLAMA_LOAD_MODE_LONGHAUL && params.longhaul_cache_bytes == 0) {
|
||||
throw std::invalid_argument("error: --longhaul requires --longhaul-cache N\n");
|
||||
throw std::invalid_argument("error: --longhaul requires --longhaul-cache GiB\n");
|
||||
}
|
||||
|
||||
postprocess_cpu_params(params.cpuparams, nullptr);
|
||||
@@ -2631,19 +2646,16 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
|
||||
).set_env("LLAMA_ARG_LOAD_MODE"));
|
||||
add_opt(common_arg(
|
||||
{"--longhaul"},
|
||||
"stream routed Qwen3.5 MoE experts through a bounded Metal cache",
|
||||
"stream routed Qwen3.x MoE experts through a bounded GPU cache",
|
||||
[](common_params & params) {
|
||||
params.load_mode = LLAMA_LOAD_MODE_LONGHAUL;
|
||||
}
|
||||
).set_env("LLAMA_ARG_LONGHAUL"));
|
||||
add_opt(common_arg(
|
||||
{"--longhaul-cache"}, "N",
|
||||
"longhaul expert cache size in GiB",
|
||||
[](common_params & params, int value) {
|
||||
if (value <= 0) {
|
||||
throw std::invalid_argument("longhaul cache size must be positive");
|
||||
}
|
||||
params.longhaul_cache_bytes = uint64_t(value) * 1024 * 1024 * 1024;
|
||||
{"--longhaul-cache"}, "GiB",
|
||||
"longhaul expert cache size in GiB; decimals are supported",
|
||||
[](common_params & params, const std::string & value) {
|
||||
params.longhaul_cache_bytes = parse_longhaul_cache_gib(value);
|
||||
}
|
||||
).set_env("LLAMA_ARG_LONGHAUL_CACHE"));
|
||||
add_opt(common_arg(
|
||||
|
||||
+71
-46
@@ -10,46 +10,13 @@ This mode is intended for running a model whose full expert weights do not fit i
|
||||
llama-cli \
|
||||
--model /path/to/model.gguf \
|
||||
--longhaul \
|
||||
--longhaul-cache 2 \
|
||||
--longhaul-cache 0.5 \
|
||||
--n-gpu-layers 99
|
||||
```
|
||||
|
||||
`--longhaul-cache` is the expert cache budget in GiB. It is required when `--longhaul` is used. The same options are accepted by `llama-server`.
|
||||
|
||||
## RPC mode
|
||||
|
||||
RPC longhaul keeps the paging loop on the accelerator host so expert payloads do
|
||||
not cross the network during generation. Build both hosts with `-DGGML_RPC=ON`.
|
||||
Place the same GGUF shard files on both hosts, preserving their basenames, then
|
||||
start the Metal host with the directory containing those shards:
|
||||
|
||||
```sh
|
||||
ggml-rpc-server \
|
||||
--device MTL0 \
|
||||
--longhaul-root /path/to/model-directory \
|
||||
--host 192.168.1.20
|
||||
```
|
||||
|
||||
Start the client normally:
|
||||
|
||||
```sh
|
||||
llama-server \
|
||||
--model /local/path/model.gguf \
|
||||
--rpc 192.168.1.20:50052 \
|
||||
--longhaul \
|
||||
--longhaul-cache 2 \
|
||||
--n-gpu-layers 99
|
||||
```
|
||||
|
||||
RPC longhaul currently requires all repeating layers on one remote Metal device.
|
||||
The server validates each shard basename, size, GGUF metadata layout digest, and
|
||||
every registered expert range before decoding. An older server, a server without
|
||||
`--longhaul-root`, or a shard mismatch is a fatal model-load error; the client
|
||||
does not fall back to sending expert slices over RPC.
|
||||
|
||||
`--longhaul-root` grants connected RPC clients read access to registered byte
|
||||
ranges in files in that directory. The RPC server remains experimental and
|
||||
insecure and must not be exposed to an untrusted network.
|
||||
`--longhaul-cache` is the expert cache budget in GiB and accepts decimal values.
|
||||
It is required when `--longhaul` is used. The same options are accepted by
|
||||
`llama-server`.
|
||||
|
||||
Longhaul may reduce `--ubatch-size` so that every expert selected by one graph segment can be present in the cache at the same time. The effective value is logged during context creation. If one token selects more experts than the cache has slots, the routed MoE computation is split into multiple stages and the partial results are summed. This permits smaller caches at the cost of additional graph work.
|
||||
|
||||
@@ -59,23 +26,81 @@ The normal startup warmup is skipped automatically in longhaul mode. Routed expe
|
||||
|
||||
Longhaul currently requires:
|
||||
|
||||
- the Metal backend, either local on macOS or on one RPC host
|
||||
- Qwen3.5 MoE or Laguna architecture
|
||||
- all repeating model layers assigned to local Metal or one RPC Metal device
|
||||
- macOS with Metal, or Linux with Vulkan
|
||||
- Qwen3.x MoE or Laguna architecture
|
||||
- all repeating model layers assigned to one supported GPU
|
||||
- text generation without embeddings or LoRA adapters
|
||||
|
||||
Longhaul does not restrict the GGUF quantization type. Individual tensor types must still be supported by Metal.
|
||||
Longhaul does not restrict the GGUF quantization type. Individual tensor types
|
||||
must still be supported by the selected GPU backend.
|
||||
|
||||
MTP/speculative decoding, tensor validation during loading, vocabulary-only loading, and CPU or mixed CPU/Metal layer placement are not supported. Both single-file and split GGUF models are supported; routed expert tensors are read from the shard that owns each tensor.
|
||||
MTP/speculative decoding, tensor validation during loading, vocabulary-only
|
||||
loading, multi-GPU placement, and CPU or mixed CPU/GPU layer placement are not
|
||||
supported. Both single-file and split GGUF models are supported; routed expert
|
||||
tensors are read from the shard that owns each tensor.
|
||||
|
||||
The cache budget covers the compact routed-expert tensors. It does not include dense weights, attention weights, the KV cache, graph allocations, or the temporary buffer used for one expert read. Disk reads bypass the macOS unified file cache where supported, avoiding a second long-lived copy of streamed weights in system RAM.
|
||||
The cache budget covers the compact routed-expert tensors. It does not include
|
||||
dense weights, attention weights, the KV cache, graph allocations, or temporary
|
||||
staging buffers. Disk reads bypass the macOS unified file cache where supported.
|
||||
On Linux, completed streamed reads are released from the kernel page cache. Both
|
||||
avoid a second long-lived copy of streamed weights in system RAM.
|
||||
|
||||
This implementation synchronizes at each routed MoE layer to discover the selected experts, populate missing cache slots, and continue execution with cache-local expert IDs. Storage speed and expert reuse therefore have a large effect on generation speed.
|
||||
|
||||
Expert IDs are planned as a batch at each synchronization point. Experts already
|
||||
needed by that batch are protected from eviction, duplicate IDs are loaded only
|
||||
once, and independent expert slices are read concurrently on shared-memory Metal
|
||||
devices. Private Metal buffers use a staged fallback.
|
||||
once, and independent expert slices are read concurrently. Shared host or Metal
|
||||
buffers can be populated directly. Private Metal and Vulkan device buffers use
|
||||
staged uploads that are synchronized before routed computation continues.
|
||||
|
||||
## Arch Linux and a 4 GiB GTX 1050 Ti
|
||||
|
||||
Install the Vulkan build dependencies and confirm that the NVIDIA GPU is visible:
|
||||
|
||||
```sh
|
||||
sudo pacman -S --needed base-devel cmake ninja shaderc spirv-headers \
|
||||
vulkan-headers vulkan-icd-loader vulkan-tools
|
||||
vulkaninfo --summary
|
||||
```
|
||||
|
||||
A GTX 10-series card needs a Vulkan driver that still supports Pascal. Driver
|
||||
installation is system-specific; do not continue until `vulkaninfo` lists the
|
||||
GTX 1050 Ti.
|
||||
|
||||
Build Longhaul with Vulkan and list the runtime device names:
|
||||
|
||||
```sh
|
||||
cmake -S . -B build-vulkan -G Ninja \
|
||||
-DCMAKE_BUILD_TYPE=Release \
|
||||
-DGGML_VULKAN=ON
|
||||
cmake --build build-vulkan --target llama-cli llama-bench test-longhaul
|
||||
./build-vulkan/bin/llama-cli --list-devices
|
||||
```
|
||||
|
||||
Use the `VulkanN` name corresponding to the discrete NVIDIA card. This matters
|
||||
on systems that also expose an integrated Radeon GPU.
|
||||
|
||||
For `Qwen3.6-35B-A3B-UD-IQ4_XS.gguf`, the non-expert weights use about 2.38 GiB
|
||||
and each cache slot uses about 56.5 MiB. A 0.5 GiB cache provides nine slots for
|
||||
the model's eight selected experts:
|
||||
|
||||
```sh
|
||||
./build-vulkan/bin/llama-cli \
|
||||
--model /path/to/Qwen3.6-35B-A3B-UD-IQ4_XS.gguf \
|
||||
--device VulkanN \
|
||||
--n-gpu-layers 99 \
|
||||
--longhaul \
|
||||
--longhaul-cache 0.5 \
|
||||
--ctx-size 512 \
|
||||
--no-kv-offload \
|
||||
--prompt "Write a short hello message." \
|
||||
--n-predict 32
|
||||
```
|
||||
|
||||
Keep the model on the fastest available drive. A hard drive works correctly,
|
||||
but random expert reads can make generation substantially slower. If Vulkan
|
||||
runs out of memory while the desktop is active, close GPU-heavy applications
|
||||
before reducing the cache below eight slots.
|
||||
|
||||
## Benchmarking prompt processing
|
||||
|
||||
@@ -85,7 +110,7 @@ devices. Private Metal buffers use a staged fallback.
|
||||
llama-bench \
|
||||
--model /path/to/model.gguf \
|
||||
--load-mode longhaul \
|
||||
--longhaul-cache 2 \
|
||||
--longhaul-cache 0.5 \
|
||||
--n-gpu-layers 99 \
|
||||
--n-prompt 2048 \
|
||||
--n-gen 0
|
||||
|
||||
+1
-44
@@ -8,7 +8,7 @@ extern "C" {
|
||||
|
||||
#define RPC_PROTO_MAJOR_VERSION 4
|
||||
#define RPC_PROTO_MINOR_VERSION 0
|
||||
#define RPC_PROTO_PATCH_VERSION 4
|
||||
#define RPC_PROTO_PATCH_VERSION 3
|
||||
|
||||
#ifdef __cplusplus
|
||||
static_assert(GGML_OP_COUNT == 101, "GGML_OP_COUNT has changed - update RPC_PROTO_PATCH_VERSION");
|
||||
@@ -27,49 +27,6 @@ GGML_BACKEND_API void ggml_backend_rpc_get_device_memory(const char * endpoint,
|
||||
GGML_BACKEND_API void ggml_backend_rpc_start_server(const char * endpoint, const char * cache_dir,
|
||||
size_t n_threads, size_t n_devices, ggml_backend_dev_t * devices);
|
||||
|
||||
struct ggml_backend_rpc_longhaul_shard {
|
||||
const char * name;
|
||||
uint64_t size;
|
||||
uint64_t metadata_size;
|
||||
uint8_t metadata_sha256[32];
|
||||
};
|
||||
|
||||
struct ggml_backend_rpc_longhaul_source {
|
||||
struct ggml_tensor * tensor;
|
||||
uint32_t shard;
|
||||
uint64_t offset;
|
||||
uint64_t expert_size;
|
||||
int32_t layer;
|
||||
};
|
||||
|
||||
struct ggml_backend_rpc_longhaul_params {
|
||||
uint32_t n_layers;
|
||||
uint32_t n_experts;
|
||||
uint32_t n_slots;
|
||||
size_t n_shards;
|
||||
const struct ggml_backend_rpc_longhaul_shard * shards;
|
||||
size_t n_sources;
|
||||
const struct ggml_backend_rpc_longhaul_source * sources;
|
||||
};
|
||||
|
||||
GGML_BACKEND_API bool ggml_backend_rpc_longhaul_register(
|
||||
const struct ggml_backend_rpc_longhaul_params * params,
|
||||
uint64_t * registration_id,
|
||||
char * error,
|
||||
size_t error_size);
|
||||
|
||||
GGML_BACKEND_API void ggml_backend_rpc_longhaul_unregister(
|
||||
struct ggml_tensor * tensor,
|
||||
uint64_t registration_id);
|
||||
|
||||
GGML_BACKEND_API void ggml_backend_rpc_start_server_with_options(
|
||||
const char * endpoint,
|
||||
const char * cache_dir,
|
||||
const char * longhaul_root,
|
||||
size_t n_threads,
|
||||
size_t n_devices,
|
||||
ggml_backend_dev_t * devices);
|
||||
|
||||
GGML_BACKEND_API ggml_backend_reg_t ggml_backend_rpc_reg(void);
|
||||
GGML_BACKEND_API ggml_backend_reg_t ggml_backend_rpc_add_server(const char * endpoint);
|
||||
|
||||
|
||||
+10
-994
File diff suppressed because it is too large
Load Diff
@@ -1359,12 +1359,10 @@ llm_graph_result * llama_context::process_ubatch(const llama_ubatch & ubatch, ll
|
||||
res->reset();
|
||||
|
||||
ggml_backend_sched_reset(sched.get());
|
||||
auto * longhaul = model.longhaul_cache();
|
||||
const bool local_longhaul = longhaul && !longhaul->is_remote();
|
||||
ggml_backend_sched_set_eval_callback(
|
||||
sched.get(),
|
||||
local_longhaul ? longhaul_eval_callback : cparams.cb_eval,
|
||||
local_longhaul ? this : cparams.cb_eval_user_data);
|
||||
model.longhaul_cache() ? longhaul_eval_callback : cparams.cb_eval,
|
||||
model.longhaul_cache() ? this : cparams.cb_eval_user_data);
|
||||
|
||||
//const auto t_start_us = ggml_time_us();
|
||||
|
||||
@@ -2463,7 +2461,7 @@ llm_graph_params llama_context::graph_params(
|
||||
bool llama_context::longhaul_eval_callback(ggml_tensor * tensor, bool ask, void * user_data) {
|
||||
auto * ctx = static_cast<llama_context *>(user_data);
|
||||
auto * cache = ctx->model.longhaul_cache();
|
||||
GGML_ASSERT(cache != nullptr && !cache->is_remote());
|
||||
GGML_ASSERT(cache != nullptr);
|
||||
|
||||
int layer = -1;
|
||||
const bool remap = sscanf(tensor->name, "longhaul.remap.%d", &layer) == 1;
|
||||
|
||||
+67
-141
@@ -1,108 +1,23 @@
|
||||
#include "llama-longhaul.h"
|
||||
|
||||
#include "llama-impl.h"
|
||||
#include "llama-token-cache.h"
|
||||
|
||||
#include "ggml-rpc.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstring>
|
||||
#include <inttypes.h>
|
||||
#include <stdexcept>
|
||||
|
||||
llama_longhaul_cache::llama_longhaul_cache(
|
||||
llama_files files,
|
||||
std::vector<llama_model_loader::longhaul_source> sources,
|
||||
std::vector<llama_model_loader::longhaul_shard> shards,
|
||||
size_t n_slots,
|
||||
uint32_t n_experts,
|
||||
uint32_t n_layers,
|
||||
bool remote) :
|
||||
uint32_t n_layers) :
|
||||
files(std::move(files)),
|
||||
sources(std::move(sources)),
|
||||
shards(std::move(shards)),
|
||||
sources_by_layer(n_layers),
|
||||
layers(n_layers),
|
||||
n_slots(n_slots),
|
||||
n_experts(n_experts),
|
||||
remote(remote) {
|
||||
if (remote) {
|
||||
if (this->shards.size() != this->files.size() || this->sources.empty()) {
|
||||
throw std::runtime_error("RPC longhaul requires named GGUF shards");
|
||||
}
|
||||
std::vector<std::array<uint8_t, 32>> digests(this->shards.size());
|
||||
std::vector<ggml_backend_rpc_longhaul_shard> rpc_shards(this->shards.size());
|
||||
for (size_t i = 0; i < this->shards.size(); ++i) {
|
||||
const auto & shard = this->shards[i];
|
||||
if (shard.metadata_size > shard.size) {
|
||||
throw std::runtime_error("invalid RPC longhaul shard metadata range");
|
||||
}
|
||||
std::vector<uint8_t> metadata(shard.metadata_size);
|
||||
if (!metadata.empty()) {
|
||||
this->files[i]->read_at(metadata.data(), metadata.size(), 0);
|
||||
}
|
||||
const std::string hex = llama_token_cache_hash(metadata.data(), metadata.size());
|
||||
if (hex.size() != 64) {
|
||||
throw std::runtime_error("failed to fingerprint RPC longhaul shard");
|
||||
}
|
||||
for (size_t j = 0; j < digests[i].size(); ++j) {
|
||||
auto nibble = [](char c) -> uint8_t {
|
||||
if (c >= '0' && c <= '9') return c - '0';
|
||||
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
|
||||
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
|
||||
return 0xff;
|
||||
};
|
||||
const uint8_t hi = nibble(hex[2*j]);
|
||||
const uint8_t lo = nibble(hex[2*j + 1]);
|
||||
if (hi > 0x0f || lo > 0x0f) {
|
||||
throw std::runtime_error("invalid RPC longhaul shard fingerprint");
|
||||
}
|
||||
digests[i][j] = (hi << 4) | lo;
|
||||
}
|
||||
rpc_shards[i] = {
|
||||
shard.name.c_str(),
|
||||
shard.size,
|
||||
shard.metadata_size,
|
||||
{},
|
||||
};
|
||||
memcpy(rpc_shards[i].metadata_sha256, digests[i].data(), digests[i].size());
|
||||
}
|
||||
|
||||
std::vector<ggml_backend_rpc_longhaul_source> rpc_sources;
|
||||
rpc_sources.reserve(this->sources.size());
|
||||
for (const auto & source : this->sources) {
|
||||
rpc_sources.push_back({
|
||||
source.tensor,
|
||||
source.file_idx,
|
||||
source.offset,
|
||||
source.expert_size,
|
||||
source.layer,
|
||||
});
|
||||
}
|
||||
ggml_backend_rpc_longhaul_params params = {
|
||||
n_layers,
|
||||
n_experts,
|
||||
(uint32_t) n_slots,
|
||||
rpc_shards.size(),
|
||||
rpc_shards.data(),
|
||||
rpc_sources.size(),
|
||||
rpc_sources.data(),
|
||||
};
|
||||
ggml_backend_reg_t reg = ggml_backend_reg_by_name("RPC");
|
||||
auto register_fn = reg ? (decltype(ggml_backend_rpc_longhaul_register) *)
|
||||
ggml_backend_reg_get_proc_address(reg, "ggml_backend_rpc_longhaul_register") : nullptr;
|
||||
if (!register_fn) {
|
||||
throw std::runtime_error("RPC backend does not expose longhaul registration");
|
||||
}
|
||||
char error[256] = {};
|
||||
if (!register_fn(¶ms, &remote_registration_id, error, sizeof(error))) {
|
||||
throw std::runtime_error(error[0] ? error : "RPC longhaul registration failed");
|
||||
}
|
||||
this->files.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
n_experts(n_experts) {
|
||||
for (auto & file : this->files) {
|
||||
file->set_no_cache();
|
||||
}
|
||||
@@ -130,17 +45,6 @@ llama_longhaul_cache::llama_longhaul_cache(
|
||||
}
|
||||
|
||||
llama_longhaul_cache::~llama_longhaul_cache() {
|
||||
if (remote) {
|
||||
if (!sources.empty() && remote_registration_id != 0) {
|
||||
ggml_backend_reg_t reg = ggml_backend_reg_by_name("RPC");
|
||||
auto unregister_fn = reg ? (decltype(ggml_backend_rpc_longhaul_unregister) *)
|
||||
ggml_backend_reg_get_proc_address(reg, "ggml_backend_rpc_longhaul_unregister") : nullptr;
|
||||
if (unregister_fn) {
|
||||
unregister_fn(sources.front().tensor, remote_registration_id);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(io_mutex);
|
||||
io_stopping = true;
|
||||
@@ -185,8 +89,14 @@ void llama_longhaul_cache::io_worker() {
|
||||
|
||||
try {
|
||||
const auto & source = *job->source;
|
||||
void * destination =
|
||||
void * destination = nullptr;
|
||||
if (job->direct) {
|
||||
destination =
|
||||
static_cast<uint8_t *>(source.tensor->data) + size_t(job->slot) * source.tensor->nb[2];
|
||||
} else {
|
||||
job->staging.resize(source.expert_size);
|
||||
destination = job->staging.data();
|
||||
}
|
||||
files.at(source.file_idx)->read_at(
|
||||
destination, source.expert_size,
|
||||
source.offset + size_t(job->expert_id) * source.expert_size);
|
||||
@@ -203,20 +113,45 @@ void llama_longhaul_cache::io_worker() {
|
||||
}
|
||||
}
|
||||
|
||||
ggml_backend_t llama_longhaul_cache::upload_backend(
|
||||
const llama_model_loader::longhaul_source & source) {
|
||||
ggml_backend_buffer_type_t buft = ggml_backend_buffer_get_type(source.tensor->buffer);
|
||||
ggml_backend_dev_t dev = ggml_backend_buft_get_device(buft);
|
||||
if (dev == nullptr || buft != ggml_backend_dev_buffer_type(dev)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto it = upload_backends.find(dev);
|
||||
if (it != upload_backends.end()) {
|
||||
return it->second.get();
|
||||
}
|
||||
|
||||
ggml_backend_ptr backend(ggml_backend_dev_init(dev, nullptr));
|
||||
if (!backend) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ggml_backend_t result = backend.get();
|
||||
upload_backends.emplace(dev, std::move(backend));
|
||||
return result;
|
||||
}
|
||||
|
||||
bool llama_longhaul_cache::load_plan_sources() {
|
||||
const size_t n_direct_sources = std::count_if(
|
||||
sources_by_layer.at(locked_layer).begin(),
|
||||
sources_by_layer.at(locked_layer).end(),
|
||||
[&](const auto * source) { return source_is_direct(*source); });
|
||||
const size_t n_jobs = load_plan.size() * n_direct_sources;
|
||||
const size_t n_jobs = load_plan.size() * sources_by_layer.at(locked_layer).size();
|
||||
io_jobs.clear();
|
||||
io_jobs.reserve(n_jobs);
|
||||
|
||||
for (const auto & item : load_plan) {
|
||||
for (const auto * source : sources_by_layer.at(locked_layer)) {
|
||||
if (source_is_direct(*source)) {
|
||||
io_jobs.push_back({ source, item.first, item.second, false, {} });
|
||||
}
|
||||
io_jobs.push_back({
|
||||
source,
|
||||
item.first,
|
||||
item.second,
|
||||
source_is_direct(*source),
|
||||
{},
|
||||
false,
|
||||
{},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,35 +164,6 @@ bool llama_longhaul_cache::load_plan_sources() {
|
||||
io_ready.notify_all();
|
||||
}
|
||||
|
||||
bool staged_ok = true;
|
||||
for (const auto & item : load_plan) {
|
||||
const int32_t expert_id = item.first;
|
||||
const int slot = item.second;
|
||||
for (const auto * source : sources_by_layer.at(locked_layer)) {
|
||||
if (source_is_direct(*source)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
read_buffer.resize(source->expert_size);
|
||||
try {
|
||||
files.at(source->file_idx)->read_at(
|
||||
read_buffer.data(), read_buffer.size(),
|
||||
source->offset + size_t(expert_id) * source->expert_size);
|
||||
ggml_backend_tensor_set(
|
||||
source->tensor, read_buffer.data(),
|
||||
size_t(slot) * source->tensor->nb[2], read_buffer.size());
|
||||
bytes_read += read_buffer.size();
|
||||
} catch (const std::exception & e) {
|
||||
last_error = e.what();
|
||||
staged_ok = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!staged_ok) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!io_jobs.empty()) {
|
||||
std::unique_lock<std::mutex> lock(io_mutex);
|
||||
io_done.wait(lock, [&] { return io_pending == 0; });
|
||||
@@ -270,7 +176,31 @@ bool llama_longhaul_cache::load_plan_sources() {
|
||||
}
|
||||
bytes_read += job.source->expert_size;
|
||||
}
|
||||
return staged_ok;
|
||||
|
||||
std::vector<ggml_backend_t> pending_backends;
|
||||
for (const auto & job : io_jobs) {
|
||||
if (job.direct) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const size_t offset = size_t(job.slot) * job.source->tensor->nb[2];
|
||||
ggml_backend_t backend = upload_backend(*job.source);
|
||||
if (backend != nullptr) {
|
||||
ggml_backend_tensor_set_async(
|
||||
backend, job.source->tensor, job.staging.data(), offset, job.staging.size());
|
||||
if (std::find(pending_backends.begin(), pending_backends.end(), backend) == pending_backends.end()) {
|
||||
pending_backends.push_back(backend);
|
||||
}
|
||||
} else {
|
||||
ggml_backend_tensor_set(
|
||||
job.source->tensor, job.staging.data(), offset, job.staging.size());
|
||||
}
|
||||
}
|
||||
|
||||
for (ggml_backend_t backend : pending_backends) {
|
||||
ggml_backend_synchronize(backend);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void llama_longhaul_cache::invalidate_plan(int layer) {
|
||||
@@ -417,7 +347,3 @@ uint64_t llama_longhaul_cache::misses() const {
|
||||
uint64_t llama_longhaul_cache::bytes_read_count() const {
|
||||
return bytes_read;
|
||||
}
|
||||
|
||||
bool llama_longhaul_cache::is_remote() const {
|
||||
return remote;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <condition_variable>
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
@@ -17,11 +18,9 @@ struct llama_longhaul_cache {
|
||||
llama_longhaul_cache(
|
||||
llama_files files,
|
||||
std::vector<llama_model_loader::longhaul_source> sources,
|
||||
std::vector<llama_model_loader::longhaul_shard> shards,
|
||||
size_t n_slots,
|
||||
uint32_t n_experts,
|
||||
uint32_t n_layers,
|
||||
bool remote);
|
||||
uint32_t n_layers);
|
||||
~llama_longhaul_cache();
|
||||
|
||||
bool remap(int layer, ggml_tensor * ids);
|
||||
@@ -33,7 +32,6 @@ struct llama_longhaul_cache {
|
||||
bool failed() const;
|
||||
uint64_t misses() const;
|
||||
uint64_t bytes_read_count() const;
|
||||
bool is_remote() const;
|
||||
|
||||
private:
|
||||
struct layer_state {
|
||||
@@ -46,13 +44,14 @@ private:
|
||||
const llama_model_loader::longhaul_source * source;
|
||||
int32_t expert_id;
|
||||
int slot;
|
||||
bool direct;
|
||||
std::vector<uint8_t> staging;
|
||||
bool ok = false;
|
||||
std::string error;
|
||||
};
|
||||
|
||||
llama_files files;
|
||||
std::vector<llama_model_loader::longhaul_source> sources;
|
||||
std::vector<llama_model_loader::longhaul_shard> shards;
|
||||
std::vector<std::vector<const llama_model_loader::longhaul_source *>> sources_by_layer;
|
||||
std::vector<layer_state> layers;
|
||||
size_t n_slots;
|
||||
@@ -78,7 +77,6 @@ private:
|
||||
std::vector<int32_t> available_slots;
|
||||
std::vector<std::pair<int32_t, int32_t>> load_plan;
|
||||
std::vector<int32_t> id_buffer;
|
||||
std::vector<uint8_t> read_buffer;
|
||||
|
||||
std::vector<std::thread> io_workers;
|
||||
std::vector<io_job> io_jobs;
|
||||
@@ -88,10 +86,11 @@ private:
|
||||
std::condition_variable io_done;
|
||||
size_t io_pending = 0;
|
||||
bool io_stopping = false;
|
||||
bool remote = false;
|
||||
uint64_t remote_registration_id = 0;
|
||||
|
||||
std::map<ggml_backend_dev_t, ggml_backend_ptr> upload_backends;
|
||||
|
||||
bool source_is_direct(const llama_model_loader::longhaul_source & source) const;
|
||||
ggml_backend_t upload_backend(const llama_model_loader::longhaul_source & source);
|
||||
bool load_plan_sources();
|
||||
void invalidate_plan(int layer);
|
||||
void io_worker();
|
||||
|
||||
@@ -66,6 +66,8 @@ static std::string llama_format_win_err(DWORD err) {
|
||||
// llama_file
|
||||
|
||||
struct llama_file::impl {
|
||||
bool no_cache = false;
|
||||
|
||||
#if defined(_WIN32)
|
||||
HANDLE fp_win32;
|
||||
std::string GetErrorMessageWin32(DWORD error_code) const {
|
||||
@@ -448,6 +450,14 @@ void llama_file::read_at(void * ptr, size_t len, size_t offset) {
|
||||
}
|
||||
n_read += result;
|
||||
}
|
||||
#if defined(__linux__) && defined(POSIX_FADV_DONTNEED)
|
||||
if (pimpl->no_cache) {
|
||||
// Longhaul retains useful expert data in its own bounded cache. Drop
|
||||
// completed file reads so the kernel page cache does not become a
|
||||
// second, unbounded copy of the streamed weights.
|
||||
(void) posix_fadvise(file_id(), (off_t) offset, (off_t) len, POSIX_FADV_DONTNEED);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -456,6 +466,12 @@ void llama_file::set_no_cache() const {
|
||||
if (fcntl(file_id(), F_NOCACHE, 1) != 0) {
|
||||
throw std::runtime_error(format("failed to disable file cache: %s", strerror(errno)));
|
||||
}
|
||||
#elif defined(__linux__) && defined(POSIX_FADV_RANDOM)
|
||||
pimpl->no_cache = true;
|
||||
const int err = posix_fadvise(file_id(), 0, 0, POSIX_FADV_RANDOM);
|
||||
if (err != 0) {
|
||||
LLAMA_LOG_WARN("%s: failed to set random-access file advice: %s\n", __func__, strerror(err));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -594,11 +594,6 @@ llama_model_loader::llama_model_loader(
|
||||
|
||||
files.emplace_back(new llama_file(fname.c_str(), "rb", use_direct_io));
|
||||
contexts.emplace_back(ctx);
|
||||
longhaul_shards.push_back({
|
||||
std::filesystem::path(fname).filename().string(),
|
||||
files.back()->size(),
|
||||
gguf_get_data_offset(metadata),
|
||||
});
|
||||
|
||||
// Save tensors data offset of the main file.
|
||||
// For subsidiary files, `meta` tensor data offset must not be used,
|
||||
@@ -667,11 +662,6 @@ llama_model_loader::llama_model_loader(
|
||||
|
||||
files.emplace_back(new llama_file(fname_split, "rb", use_direct_io));
|
||||
contexts.emplace_back(ctx);
|
||||
longhaul_shards.push_back({
|
||||
std::filesystem::path(fname_split).filename().string(),
|
||||
files.back()->size(),
|
||||
gguf_get_data_offset(ctx_gguf.get()),
|
||||
});
|
||||
|
||||
// Save tensors data offset info of the shard.
|
||||
for (ggml_tensor * cur = ggml_get_first_tensor(ctx); cur; cur = ggml_get_next_tensor(ctx, cur)) {
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
|
||||
#include "ggml-cpp.h"
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
@@ -78,12 +77,6 @@ struct llama_model_loader {
|
||||
int layer;
|
||||
};
|
||||
|
||||
struct longhaul_shard {
|
||||
std::string name;
|
||||
uint64_t size;
|
||||
uint64_t metadata_size;
|
||||
};
|
||||
|
||||
int n_kv = 0;
|
||||
int n_tensors = 0;
|
||||
int n_created = 0;
|
||||
@@ -122,7 +115,6 @@ struct llama_model_loader {
|
||||
std::vector<std::pair<size_t, size_t>> mmaps_used;
|
||||
size_t longhaul_slots = 0;
|
||||
std::vector<longhaul_source> longhaul_sources;
|
||||
std::vector<longhaul_shard> longhaul_shards;
|
||||
|
||||
// define a comparator for the buft -> ctx map to ensure that the order is well-defined:
|
||||
struct ggml_backend_buft_comparator {
|
||||
|
||||
+10
-26
@@ -1039,7 +1039,6 @@ struct llama_model::impl {
|
||||
|
||||
std::vector<float> tensor_split_owned;
|
||||
std::unique_ptr<llama_longhaul_cache> longhaul;
|
||||
bool longhaul_remote = false;
|
||||
};
|
||||
|
||||
llama_model::llama_model(const llama_model_params & params) : params(params), pimpl(std::make_unique<impl>()) {
|
||||
@@ -1366,35 +1365,21 @@ bool llama_model_base::load_tensors(llama_model_loader & ml) {
|
||||
if (params.check_tensors || params.no_alloc || params.vocab_only) {
|
||||
throw std::runtime_error("longhaul does not support check-tensors, no-alloc, or vocab-only loading");
|
||||
}
|
||||
bool all_metal = true;
|
||||
bool all_rpc = true;
|
||||
ggml_backend_dev_t longhaul_dev = nullptr;
|
||||
for (int il = 0; il < n_layer_all; ++il) {
|
||||
ggml_backend_dev_t dev = pimpl->dev_layer[il].dev;
|
||||
const char * reg_name = ggml_backend_reg_name(ggml_backend_dev_backend_reg(dev));
|
||||
all_metal = all_metal &&
|
||||
ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_GPU &&
|
||||
strcmp(reg_name, "MTL") == 0;
|
||||
all_rpc = all_rpc &&
|
||||
ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_GPU &&
|
||||
strcmp(reg_name, "RPC") == 0;
|
||||
}
|
||||
if (!all_metal && !all_rpc) {
|
||||
const char * backend = ggml_backend_reg_name(ggml_backend_dev_backend_reg(dev));
|
||||
const bool supported_backend =
|
||||
strcmp(backend, "MTL") == 0 || strcmp(backend, "Vulkan") == 0;
|
||||
if (ggml_backend_dev_type(dev) != GGML_BACKEND_DEVICE_TYPE_GPU || !supported_backend) {
|
||||
throw std::runtime_error(
|
||||
"longhaul requires all model layers on local Metal or one RPC device");
|
||||
"longhaul requires every model layer on a Metal or Vulkan GPU");
|
||||
}
|
||||
if (all_metal) {
|
||||
#if !defined(__APPLE__)
|
||||
throw std::runtime_error("local longhaul is only supported on macOS");
|
||||
#endif
|
||||
} else {
|
||||
ggml_backend_dev_t first = pimpl->dev_layer.front().dev;
|
||||
for (int il = 1; il < n_layer_all; ++il) {
|
||||
if (pimpl->dev_layer[il].dev != first) {
|
||||
if (longhaul_dev != nullptr && dev != longhaul_dev) {
|
||||
throw std::runtime_error(
|
||||
"RPC longhaul v1 requires every model layer on one RPC device");
|
||||
"longhaul requires every model layer on one GPU; select it with --device");
|
||||
}
|
||||
}
|
||||
pimpl->longhaul_remote = true;
|
||||
longhaul_dev = dev;
|
||||
}
|
||||
ml.configure_longhaul(params.longhaul_cache_bytes, n_expert, n_layer_all);
|
||||
if (ml.longhaul_slots < (size_t) n_expert_used) {
|
||||
@@ -1715,8 +1700,7 @@ bool llama_model_base::load_tensors(llama_model_loader & ml) {
|
||||
|
||||
if (params.load_mode == LLAMA_LOAD_MODE_LONGHAUL) {
|
||||
pimpl->longhaul = std::make_unique<llama_longhaul_cache>(
|
||||
std::move(ml.files), std::move(ml.longhaul_sources), std::move(ml.longhaul_shards),
|
||||
ml.longhaul_slots, hparams.n_expert, hparams.n_layer_all, pimpl->longhaul_remote);
|
||||
std::move(ml.files), std::move(ml.longhaul_sources), ml.longhaul_slots, hparams.n_expert, hparams.n_layer_all);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -263,10 +263,6 @@ static bool llama_prepare_model_devices(const llama_model_params & params, llama
|
||||
}
|
||||
}
|
||||
|
||||
if (params.load_mode == LLAMA_LOAD_MODE_LONGHAUL && !rpc_servers.empty()) {
|
||||
// RPC longhaul v1 keeps the entire paging domain on one remote device.
|
||||
model->devices.push_back(rpc_servers.front());
|
||||
} else {
|
||||
// add RPC servers at the front of the list to minimize network transfers
|
||||
model->devices.insert(model->devices.begin(), rpc_servers.begin(), rpc_servers.end());
|
||||
|
||||
@@ -279,7 +275,6 @@ static bool llama_prepare_model_devices(const llama_model_params & params, llama
|
||||
model->devices.insert(model->devices.end(), igpus.begin(), igpus.end());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if using single GPU mode, remove all except the main GPU
|
||||
if (params.split_mode == LLAMA_SPLIT_MODE_NONE && !model->devices.empty()) {
|
||||
|
||||
@@ -160,6 +160,19 @@ static void test(void) {
|
||||
assert(params.load_mode == LLAMA_LOAD_MODE_LONGHAUL);
|
||||
assert(params.longhaul_cache_bytes == 2ULL * 1024 * 1024 * 1024);
|
||||
|
||||
argv = {"binary_name", "--longhaul", "--longhaul-cache", "0.5"};
|
||||
assert(true == common_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
|
||||
assert(params.longhaul_cache_bytes == 512ULL * 1024 * 1024);
|
||||
|
||||
argv = {"binary_name", "--longhaul", "--longhaul-cache", "0"};
|
||||
assert(false == common_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
|
||||
|
||||
argv = {"binary_name", "--longhaul", "--longhaul-cache", "nan"};
|
||||
assert(false == common_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
|
||||
|
||||
argv = {"binary_name", "--longhaul", "--longhaul-cache", "0.5GiB"};
|
||||
assert(false == common_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
|
||||
|
||||
argv = {"binary_name", "--token-cache-size", "256", "--token-cache-dir", "/tmp/llama-token-cache-test.sqlite3"};
|
||||
assert(true == common_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
|
||||
assert(params.token_cache_size_mib == 256);
|
||||
|
||||
+31
-4
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "ggml-backend.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
@@ -24,12 +25,15 @@ struct longhaul_fixture {
|
||||
size_t n_slots,
|
||||
uint32_t n_experts,
|
||||
bool two_sources = false,
|
||||
uint32_t written_experts = 0) {
|
||||
uint32_t written_experts = 0,
|
||||
ggml_backend_dev_t dev = nullptr) {
|
||||
file = tmpfile();
|
||||
GGML_ASSERT(file != nullptr);
|
||||
write_experts(written_experts == 0 ? n_experts : written_experts, two_sources);
|
||||
|
||||
backend.reset(ggml_backend_init_by_type(GGML_BACKEND_DEVICE_TYPE_CPU, nullptr));
|
||||
backend.reset(dev
|
||||
? ggml_backend_dev_init(dev, nullptr)
|
||||
: ggml_backend_init_by_type(GGML_BACKEND_DEVICE_TYPE_CPU, nullptr));
|
||||
GGML_ASSERT(backend);
|
||||
|
||||
ggml_init_params params = {
|
||||
@@ -58,8 +62,7 @@ struct longhaul_fixture {
|
||||
sources.push_back({ weights_b, 0, n_experts * expert_size, expert_size, 0 });
|
||||
}
|
||||
cache = std::make_unique<llama_longhaul_cache>(
|
||||
std::move(files), std::move(sources), std::vector<llama_model_loader::longhaul_shard>{},
|
||||
n_slots, n_experts, 1, false);
|
||||
std::move(files), std::move(sources), n_slots, n_experts, 1);
|
||||
}
|
||||
|
||||
~longhaul_fixture() {
|
||||
@@ -153,6 +156,18 @@ static void test_read_failure_recovery(testing & t) {
|
||||
t.assert_equal(uint8_t(1), fixture.slot_value(fixture.weights_a, remapped[0]));
|
||||
}
|
||||
|
||||
static void test_staged_device_upload(testing & t, ggml_backend_dev_t dev) {
|
||||
longhaul_fixture fixture(2, 4, true, 0, dev);
|
||||
|
||||
const auto remapped = fixture.remap({3, 1});
|
||||
t.assert_equal(2u, remapped.size());
|
||||
t.assert_equal(uint64_t(4 * longhaul_fixture::expert_size), fixture.cache->bytes_read_count());
|
||||
t.assert_equal(uint8_t(4), fixture.slot_value(fixture.weights_a, remapped[0]));
|
||||
t.assert_equal(uint8_t(36), fixture.slot_value(fixture.weights_b, remapped[0]));
|
||||
t.assert_equal(uint8_t(2), fixture.slot_value(fixture.weights_a, remapped[1]));
|
||||
t.assert_equal(uint8_t(34), fixture.slot_value(fixture.weights_b, remapped[1]));
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
testing t;
|
||||
|
||||
@@ -163,6 +178,7 @@ int main(int argc, char ** argv) {
|
||||
if (!t.verbose) {
|
||||
llama_log_set([](ggml_log_level, const char *, void *) {}, nullptr);
|
||||
}
|
||||
ggml_backend_load_all();
|
||||
|
||||
if (argc > 1) {
|
||||
t.set_filter(argv[1]);
|
||||
@@ -173,5 +189,16 @@ int main(int argc, char ** argv) {
|
||||
t.test("invalid_ids", test_invalid_ids);
|
||||
t.test("read_failure", test_read_failure_recovery);
|
||||
|
||||
for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
|
||||
ggml_backend_dev_t dev = ggml_backend_dev_get(i);
|
||||
const char * backend = ggml_backend_reg_name(ggml_backend_dev_backend_reg(dev));
|
||||
if (strcmp(backend, "Vulkan") == 0) {
|
||||
t.test("vulkan_staged_upload", [dev](testing & current) {
|
||||
test_staged_device_upload(current, dev);
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return t.summary();
|
||||
}
|
||||
|
||||
+2
-2
@@ -61,8 +61,8 @@
|
||||
| `--mmap, --no-mmap` | DEPRECATED in favor of `--load-mode`: whether to memory-map model. (if mmap disabled, slower load but may reduce pageouts if not using mlock)<br/>(env: LLAMA_ARG_MMAP) |
|
||||
| `-dio, --direct-io, -ndio, --no-direct-io` | DEPRECATED in favor of `--load-mode`: use DirectIO if available<br/>(env: LLAMA_ARG_DIO) |
|
||||
| `-lm, --load-mode MODE` | model loading mode (default: mmap)<br/>- none: no special loading mode<br/>- mmap: memory-map model (if mmap disabled, slower load but may reduce pageouts if not using mlock)<br/>- mlock: force system to keep model in RAM rather than swapping or compressing<br/>- mmap+mlock: mmap + force system to keep model in RAM rather than swapping or compressing<br/>- dio: use DirectIO if available<br/>- longhaul: stream routed MoE experts through a bounded cache<br/><br/>(env: LLAMA_ARG_LOAD_MODE) |
|
||||
| `--longhaul` | stream routed Qwen3.5 MoE experts through a bounded Metal cache<br/>(env: LLAMA_ARG_LONGHAUL) |
|
||||
| `--longhaul-cache N` | longhaul expert cache size in GiB<br/>(env: LLAMA_ARG_LONGHAUL_CACHE) |
|
||||
| `--longhaul` | stream routed Qwen3.x MoE experts through a bounded GPU cache<br/>(env: LLAMA_ARG_LONGHAUL) |
|
||||
| `--longhaul-cache GiB` | longhaul expert cache size in GiB; decimals are supported<br/>(env: LLAMA_ARG_LONGHAUL_CACHE) |
|
||||
| `--numa TYPE` | attempt optimizations that help on some NUMA systems<br/>- distribute: spread execution evenly over all nodes<br/>- isolate: only spawn threads on CPUs on the node that execution started on<br/>- numactl: use the CPU map provided by numactl<br/>if run without this previously, it is recommended to drop the system page cache before using this<br/>see https://github.com/ggml-org/llama.cpp/issues/1437<br/>(env: LLAMA_ARG_NUMA) |
|
||||
| `-dev, --device <dev1,dev2,..>` | comma-separated list of devices to use for offloading (none = don't offload)<br/>use --list-devices to see a list of available devices<br/>(env: LLAMA_ARG_DEVICE) |
|
||||
| `--list-devices` | print list of available devices and exit |
|
||||
|
||||
@@ -144,8 +144,8 @@ llama-completion.exe -m models\gemma-1.1-7b-it.Q4_K_M.gguf --ignore-eos -n -1
|
||||
| `--mmap, --no-mmap` | DEPRECATED in favor of `--load-mode`: whether to memory-map model. (if mmap disabled, slower load but may reduce pageouts if not using mlock)<br/>(env: LLAMA_ARG_MMAP) |
|
||||
| `-dio, --direct-io, -ndio, --no-direct-io` | DEPRECATED in favor of `--load-mode`: use DirectIO if available<br/>(env: LLAMA_ARG_DIO) |
|
||||
| `-lm, --load-mode MODE` | model loading mode (default: mmap)<br/>- none: no special loading mode<br/>- mmap: memory-map model (if mmap disabled, slower load but may reduce pageouts if not using mlock)<br/>- mlock: force system to keep model in RAM rather than swapping or compressing<br/>- mmap+mlock: mmap + force system to keep model in RAM rather than swapping or compressing<br/>- dio: use DirectIO if available<br/>- longhaul: stream routed MoE experts through a bounded cache<br/><br/>(env: LLAMA_ARG_LOAD_MODE) |
|
||||
| `--longhaul` | stream routed Qwen3.5 MoE experts through a bounded Metal cache<br/>(env: LLAMA_ARG_LONGHAUL) |
|
||||
| `--longhaul-cache N` | longhaul expert cache size in GiB<br/>(env: LLAMA_ARG_LONGHAUL_CACHE) |
|
||||
| `--longhaul` | stream routed Qwen3.x MoE experts through a bounded GPU cache<br/>(env: LLAMA_ARG_LONGHAUL) |
|
||||
| `--longhaul-cache GiB` | longhaul expert cache size in GiB; decimals are supported<br/>(env: LLAMA_ARG_LONGHAUL_CACHE) |
|
||||
| `--numa TYPE` | attempt optimizations that help on some NUMA systems<br/>- distribute: spread execution evenly over all nodes<br/>- isolate: only spawn threads on CPUs on the node that execution started on<br/>- numactl: use the CPU map provided by numactl<br/>if run without this previously, it is recommended to drop the system page cache before using this<br/>see https://github.com/ggml-org/llama.cpp/issues/1437<br/>(env: LLAMA_ARG_NUMA) |
|
||||
| `-dev, --device <dev1,dev2,..>` | comma-separated list of devices to use for offloading (none = don't offload)<br/>use --list-devices to see a list of available devices<br/>(env: LLAMA_ARG_DEVICE) |
|
||||
| `--list-devices` | print list of available devices and exit |
|
||||
|
||||
@@ -56,7 +56,7 @@ test parameters:
|
||||
-ub, --ubatch-size <n> (default: 512)
|
||||
-ctk, --cache-type-k <t> (default: f16)
|
||||
-ctv, --cache-type-v <t> (default: f16)
|
||||
--longhaul-cache <GiB> expert cache size for longhaul mode
|
||||
--longhaul-cache <GiB> expert cache size for longhaul mode (decimals supported)
|
||||
-t, --threads <n> (default: system dependent)
|
||||
-C, --cpu-mask <hex,hex> (default: 0x0)
|
||||
--cpu-strict <0|1> (default: 0)
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <cstring>
|
||||
#include <ctime>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <map>
|
||||
#include <numeric>
|
||||
#include <regex>
|
||||
@@ -37,6 +38,20 @@
|
||||
#endif
|
||||
|
||||
// utils
|
||||
static uint64_t parse_longhaul_cache_gib(const char * value) {
|
||||
size_t end = 0;
|
||||
const std::string text(value);
|
||||
const double gib = std::stod(text, &end);
|
||||
constexpr long double bytes_per_gib = 1024.0L * 1024.0L * 1024.0L;
|
||||
const long double bytes = (long double) gib * bytes_per_gib;
|
||||
|
||||
if (end != text.size() || !std::isfinite(gib) || gib <= 0.0 ||
|
||||
bytes < 1.0L || bytes > (long double) std::numeric_limits<uint64_t>::max()) {
|
||||
throw std::invalid_argument("invalid longhaul cache size");
|
||||
}
|
||||
return (uint64_t) bytes;
|
||||
}
|
||||
|
||||
static uint64_t get_time_ns() {
|
||||
using clock = std::chrono::high_resolution_clock;
|
||||
return std::chrono::nanoseconds(clock::now().time_since_epoch()).count();
|
||||
@@ -462,7 +477,7 @@ static void print_usage(int /* argc */, char ** argv) {
|
||||
printf(" -fa, --flash-attn <on|off|auto> (default: %s)\n", join(transform_to_str(cmd_params_defaults.flash_attn, llama_flash_attn_type_name), ",").c_str());
|
||||
printf(" -dev, --device <dev0/dev1/...> (default: auto)\n");
|
||||
printf(" -lm, --load-mode <none|mmap|mlock|mmap+mlock|dio|longhaul> (default: %s)\n", join(transform_to_str(cmd_params_defaults.load_mode, llama_load_mode_name), ",").c_str());
|
||||
printf(" --longhaul-cache <GiB> expert cache size for longhaul mode\n");
|
||||
printf(" --longhaul-cache <GiB> expert cache size for longhaul mode (decimals supported)\n");
|
||||
printf(" -mmp, --mmap <0|1> (DEPRECATED IN FAVOUR OF --load-mode)\n");
|
||||
printf(" -dio, --direct-io <0|1> (DEPRECATED IN FAVOUR OF --load-mode)\n");
|
||||
printf(" -embd, --embeddings <0|1> (default: %s)\n", join(cmd_params_defaults.embeddings, ",").c_str());
|
||||
@@ -795,12 +810,12 @@ static cmd_params parse_cmd_params(int argc, char ** argv) {
|
||||
invalid_param = true;
|
||||
break;
|
||||
}
|
||||
const uint64_t gib = std::stoull(argv[i]);
|
||||
if (gib == 0 || gib > UINT64_MAX / 1024 / 1024 / 1024) {
|
||||
try {
|
||||
params.longhaul_cache_bytes = parse_longhaul_cache_gib(argv[i]);
|
||||
} catch (const std::exception &) {
|
||||
invalid_param = true;
|
||||
break;
|
||||
}
|
||||
params.longhaul_cache_bytes = gib * 1024 * 1024 * 1024;
|
||||
} else if (arg == "-mg" || arg == "--main-gpu") {
|
||||
if (++i >= argc) {
|
||||
invalid_param = true;
|
||||
@@ -1154,7 +1169,7 @@ static cmd_params parse_cmd_params(int argc, char ** argv) {
|
||||
}
|
||||
if (std::find(params.load_mode.begin(), params.load_mode.end(), LLAMA_LOAD_MODE_LONGHAUL) != params.load_mode.end() &&
|
||||
params.longhaul_cache_bytes == 0) {
|
||||
fprintf(stderr, "error: longhaul load mode requires --longhaul-cache N\n");
|
||||
fprintf(stderr, "error: longhaul load mode requires --longhaul-cache GiB\n");
|
||||
exit(1);
|
||||
}
|
||||
if (params.main_gpu.empty()) {
|
||||
|
||||
+1
-11
@@ -36,17 +36,6 @@ flowchart TD
|
||||
By default, `ggml-rpc-server` exposes all available accelerator devices on the host.
|
||||
If there are no accelerators, it exposes a single `CPU` device.
|
||||
|
||||
For server-resident longhaul MoE paging, place the same GGUF shards on the RPC
|
||||
host and point the server at their directory:
|
||||
|
||||
```sh
|
||||
$ bin/ggml-rpc-server --device MTL0 --longhaul-root /path/to/model-directory
|
||||
```
|
||||
|
||||
The client can then use `--rpc`, `--longhaul`, and `--longhaul-cache` together.
|
||||
RPC longhaul v1 requires one remote Metal device. Expert cache misses are read
|
||||
from the RPC host's local files rather than uploaded by the client.
|
||||
|
||||
## Usage
|
||||
|
||||
### Remote hosts
|
||||
@@ -118,3 +107,4 @@ Use the `GGML_RPC_DEBUG` environment variable to enable debug messages from `ggm
|
||||
```bash
|
||||
$ GGML_RPC_DEBUG=1 bin/ggml-rpc-server
|
||||
```
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
#include <algorithm>
|
||||
#include <clocale>
|
||||
#include <codecvt>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <regex>
|
||||
#include <stdio.h>
|
||||
@@ -174,7 +173,6 @@ struct rpc_server_params {
|
||||
std::string host = "127.0.0.1";
|
||||
int port = 50052;
|
||||
bool use_cache = false;
|
||||
std::string longhaul_root;
|
||||
int n_threads = std::max(1U, std::thread::hardware_concurrency()/2);
|
||||
std::vector<std::string> devices;
|
||||
};
|
||||
@@ -188,7 +186,6 @@ static void print_usage(int /*argc*/, char ** argv, rpc_server_params params) {
|
||||
fprintf(stderr, " -H, --host HOST host to bind to (default: %s)\n", params.host.c_str());
|
||||
fprintf(stderr, " -p, --port PORT port to bind to (default: %d)\n", params.port);
|
||||
fprintf(stderr, " -c, --cache enable local file cache\n");
|
||||
fprintf(stderr, " --longhaul-root PATH serve longhaul expert data from this model directory\n");
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
|
||||
@@ -236,11 +233,6 @@ static bool rpc_server_params_parse(int argc, char ** argv, rpc_server_params &
|
||||
}
|
||||
} else if (arg == "-c" || arg == "--cache") {
|
||||
params.use_cache = true;
|
||||
} else if (arg == "--longhaul-root") {
|
||||
if (++i >= argc) {
|
||||
return false;
|
||||
}
|
||||
params.longhaul_root = argv[i];
|
||||
} else if (arg == "-h" || arg == "--help") {
|
||||
print_usage(argc, argv, params);
|
||||
exit(0);
|
||||
@@ -321,23 +313,6 @@ int main(int argc, char * argv[]) {
|
||||
fprintf(stderr, "No devices found\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!params.longhaul_root.empty()) {
|
||||
std::error_code ec;
|
||||
const auto root = std::filesystem::canonical(params.longhaul_root, ec);
|
||||
if (ec || !std::filesystem::is_directory(root, ec)) {
|
||||
fprintf(stderr, "Invalid longhaul root: %s\n", params.longhaul_root.c_str());
|
||||
return 1;
|
||||
}
|
||||
params.longhaul_root = root.string();
|
||||
for (auto * dev : devices) {
|
||||
auto * dev_reg = ggml_backend_dev_backend_reg(dev);
|
||||
if (!dev_reg || strcmp(ggml_backend_reg_name(dev_reg), "MTL") != 0) {
|
||||
fprintf(stderr, "--longhaul-root currently requires Metal devices\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::string endpoint = params.host + ":" + std::to_string(params.port);
|
||||
const char * cache_dir = nullptr;
|
||||
std::string cache_dir_str;
|
||||
@@ -356,19 +331,12 @@ int main(int argc, char * argv[]) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto start_server_fn = (decltype(ggml_backend_rpc_start_server_with_options)*)
|
||||
ggml_backend_reg_get_proc_address(reg, "ggml_backend_rpc_start_server_with_options");
|
||||
auto start_server_fn = (decltype(ggml_backend_rpc_start_server)*) ggml_backend_reg_get_proc_address(reg, "ggml_backend_rpc_start_server");
|
||||
if (!start_server_fn) {
|
||||
fprintf(stderr, "Failed to obtain RPC backend start server function with options\n");
|
||||
fprintf(stderr, "Failed to obtain RPC backend start server function\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
start_server_fn(
|
||||
endpoint.c_str(),
|
||||
cache_dir,
|
||||
params.longhaul_root.empty() ? nullptr : params.longhaul_root.c_str(),
|
||||
params.n_threads,
|
||||
devices.size(),
|
||||
devices.data());
|
||||
start_server_fn(endpoint.c_str(), cache_dir, params.n_threads, devices.size(), devices.data());
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -78,8 +78,8 @@ For the full list of features, please refer to [server's changelog](https://gith
|
||||
| `--mmap, --no-mmap` | DEPRECATED in favor of `--load-mode`: whether to memory-map model. (if mmap disabled, slower load but may reduce pageouts if not using mlock)<br/>(env: LLAMA_ARG_MMAP) |
|
||||
| `-dio, --direct-io, -ndio, --no-direct-io` | DEPRECATED in favor of `--load-mode`: use DirectIO if available<br/>(env: LLAMA_ARG_DIO) |
|
||||
| `-lm, --load-mode MODE` | model loading mode (default: mmap)<br/>- none: no special loading mode<br/>- mmap: memory-map model (if mmap disabled, slower load but may reduce pageouts if not using mlock)<br/>- mlock: force system to keep model in RAM rather than swapping or compressing<br/>- mmap+mlock: mmap + force system to keep model in RAM rather than swapping or compressing<br/>- dio: use DirectIO if available<br/>- longhaul: stream routed MoE experts through a bounded cache<br/><br/>(env: LLAMA_ARG_LOAD_MODE) |
|
||||
| `--longhaul` | stream routed Qwen3.5 MoE experts through a bounded Metal cache<br/>(env: LLAMA_ARG_LONGHAUL) |
|
||||
| `--longhaul-cache N` | longhaul expert cache size in GiB<br/>(env: LLAMA_ARG_LONGHAUL_CACHE) |
|
||||
| `--longhaul` | stream routed Qwen3.x MoE experts through a bounded GPU cache<br/>(env: LLAMA_ARG_LONGHAUL) |
|
||||
| `--longhaul-cache GiB` | longhaul expert cache size in GiB; decimals are supported<br/>(env: LLAMA_ARG_LONGHAUL_CACHE) |
|
||||
| `--numa TYPE` | attempt optimizations that help on some NUMA systems<br/>- distribute: spread execution evenly over all nodes<br/>- isolate: only spawn threads on CPUs on the node that execution started on<br/>- numactl: use the CPU map provided by numactl<br/>if run without this previously, it is recommended to drop the system page cache before using this<br/>see https://github.com/ggml-org/llama.cpp/issues/1437<br/>(env: LLAMA_ARG_NUMA) |
|
||||
| `-dev, --device <dev1,dev2,..>` | comma-separated list of devices to use for offloading (none = don't offload)<br/>use --list-devices to see a list of available devices<br/>(env: LLAMA_ARG_DEVICE) |
|
||||
| `--list-devices` | print list of available devices and exit |
|
||||
|
||||
Reference in New Issue
Block a user