### Untrusted environments or networks
If you can't run your models in a secure and isolated environment or if it must be exposed to an untrusted network, make sure to take the following security precautions:
-* Do not use the RPC backend, [rpc-server](https://github.com/ggml-org/llama.cpp/tree/master/tools/rpc) and [llama-server](https://github.com/ggml-org/llama.cpp/tree/master/tools/server) functionality (see https://github.com/ggml-org/llama.cpp/pull/13061).
+* Do not use the RPC backend, [ggml-rpc-server](https://github.com/ggml-org/llama.cpp/tree/master/tools/rpc) and [llama-server](https://github.com/ggml-org/llama.cpp/tree/master/tools/server) functionality (see https://github.com/ggml-org/llama.cpp/pull/13061).
* Confirm the hash of any downloaded artifact (e.g. pre-trained model weights) matches a known-good value.
* Encrypt your data if sending it over the network.
llama_build_and_test(test-alloc.cpp)
target_include_directories(test-alloc PRIVATE ${PROJECT_SOURCE_DIR}/ggml/src)
-llama_build(export-graph-ops.cpp)
-target_include_directories(export-graph-ops PRIVATE ${PROJECT_SOURCE_DIR}/ggml/src)
+llama_build(test-export-graph-ops.cpp)
+target_include_directories(test-export-graph-ops PRIVATE ${PROJECT_SOURCE_DIR}/ggml/src)
if (TARGET gguf-model-data)
- target_link_libraries(export-graph-ops PRIVATE gguf-model-data)
- target_compile_definitions(export-graph-ops PRIVATE LLAMA_HF_FETCH)
+ target_link_libraries(test-export-graph-ops PRIVATE gguf-model-data)
+ target_compile_definitions(test-export-graph-ops PRIVATE LLAMA_HF_FETCH)
endif()
+++ /dev/null
-#include "arg.h"
-#include "common.h"
-#include "log.h"
-#include "llama-cpp.h"
-#include "../src/llama-ext.h"
-#include "ggml.h"
-#include "gguf-model-data.h"
-#include "gguf.h"
-#include "ggml-backend.h"
-#include "download.h"
-
-#include <array>
-#include <vector>
-#include <set>
-#include <fstream>
-#include <iostream>
-#include <random>
-
-// Noop because weights are not needed
-static void set_tensor_data(struct ggml_tensor * tensor, void * userdata) {
- GGML_UNUSED(tensor);
- GGML_UNUSED(userdata);
-}
-
-struct input_tensor {
- ggml_type type;
- std::array<int64_t, 4> ne;
- std::array<size_t, 4> nb;
-
- input_tensor(ggml_type type, int64_t * ne, size_t * nb): type(type) {
- memcpy(this->ne.data(), ne, 4 * sizeof(int64_t));
- memcpy(this->nb.data(), nb, 4 * sizeof(size_t));
- }
-
- bool operator<(const input_tensor &b) const {
- return std::tie(type, ne, nb) <
- std::tie(b.type, b.ne, b.nb);
- }
-
- void serialize(std::ostream& out) const {
- out << type << ' ';
- for (size_t i = 0; i < 4; i++) {
- out << ne[i] << ' ';
- }
- for (size_t i = 0; i < 4; i++) {
- out << nb[i] << ' ';
- }
- }
-};
-
-struct test_object {
- ggml_op op;
- ggml_type type;
- std::array<int64_t, 4> ne;
- std::vector<int32_t> op_params;
- std::vector<input_tensor> sources;
- std::string name;
-
- void serialize(std::ostream& out) const {
- out << op << ' ' << type << ' ';
- for (size_t i = 0; i < 4; i++) {
- out << ne[i] << ' ';
- }
-
- out << op_params.size() << ' ';
- for (size_t i = 0; i < op_params.size(); i++) {
- out << op_params[i] << ' ';
- }
-
- out << sources.size() << ' ';
- for (size_t s = 0; s < sources.size(); s++) {
- sources[s].serialize(out);
- }
-
- if (!name.empty()) {
- out << name;
- } else {
- out << '-';
- }
-
- out << '\n';
- }
-
- bool operator<(const test_object &b) const {
- return std::tie(op, type, ne, op_params, sources) <
- std::tie(b.op, b.type, b.ne, b.op_params, b.sources);
- }
-};
-
-static void extract_graph_ops(ggml_cgraph * cgraph, const char * label, std::set<test_object> & tests) {
- int n_nodes = ggml_graph_n_nodes(cgraph);
- int n_skipped = 0;
- int n_before = (int) tests.size();
- for (int i = 0; i < n_nodes; i++) {
- ggml_tensor * node = ggml_graph_node(cgraph, i);
-
- if (node->op == GGML_OP_NONE || node->op == GGML_OP_VIEW || node->op == GGML_OP_RESHAPE || node->op == GGML_OP_PERMUTE || node->op == GGML_OP_TRANSPOSE) {
- n_skipped++;
- continue;
- }
-
- test_object test;
-
- test.op = node->op;
- test.type = node->type;
- memcpy(&test.ne, node->ne, 4 * sizeof(int64_t));
-
- test.op_params.resize(GGML_MAX_OP_PARAMS / sizeof(int32_t));
- memcpy(test.op_params.data(), node->op_params, GGML_MAX_OP_PARAMS);
-
- for (size_t s = 0; s < GGML_MAX_SRC; s++) {
- if (node->src[s] == nullptr) {
- break;
- }
-
- test.sources.emplace_back(node->src[s]->type, node->src[s]->ne, node->src[s]->nb);
- }
-
- test.name = node->name;
- tests.insert(test);
- }
-
- int n_new = (int) tests.size() - n_before;
- LOG_INF("%s: %d unique ops, %d total nodes, %d skipped (view ops)\n",
- label, n_new, n_nodes, n_skipped);
-}
-
-int main(int argc, char ** argv) {
- common_params params;
- params.out_file = "tests.txt";
-
- common_init();
-
- if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_EXPORT_GRAPH_OPS)) {
- return 1;
- }
-
- // Load CPU-only
- ggml_backend_dev_t cpu_device = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
- params.devices = { cpu_device, nullptr };
- params.fit_params = false;
- params.n_gpu_layers = 0;
-
- params.warmup = false;
-
- llama_context * ctx;
- common_init_result_ptr init_result;
- llama_context_ptr ctx2;
- llama_model_ptr model;
-
- if (params.model.hf_repo.empty()) {
- init_result = common_init_from_params(params);
-
- ctx = init_result->context();
- } else {
-#ifdef LLAMA_HF_FETCH
- auto [hf_repo, hf_quant] = common_download_split_repo_tag(params.model.hf_repo);
- if (hf_quant.empty() || hf_quant == "latest") {
- hf_quant = "Q4_K_M";
- }
-
- gguf_context_ptr gguf_ctx = gguf_fetch_gguf_ctx(hf_repo, hf_quant);
- if (!gguf_ctx) {
- LOG_ERR("failed to fetch GGUF metadata from %s\n", hf_repo.c_str());
- return 1;
- }
-
- llama_model_params model_params = llama_model_default_params();
- model_params.devices = params.devices.data();
- model_params.no_alloc = true;
-
- model.reset(llama_model_init_from_user(gguf_ctx.get(), set_tensor_data, nullptr, model_params));
-
- if (!model) {
- LOG_ERR("failed to create llama_model from %s\n", hf_repo.c_str());
- return 1;
- }
-
- llama_context_params ctx_params = llama_context_default_params();
- ctx2.reset(llama_init_from_model(model.get(), ctx_params));
- ctx = ctx2.get();
-
- if (!ctx) {
- LOG_ERR("failed to create llama_context\n");
- return 1;
- }
-#else
- LOG_ERR("export-graph-ops compiled without HF fetch support\n");
- return 1;
-#endif
- }
-
- const uint32_t n_seqs = llama_n_seq_max(ctx);
- const uint32_t n_tokens = std::min(llama_n_ctx(ctx), llama_n_ubatch(ctx));
-
- std::set<test_object> tests;
-
- auto * gf_pp = llama_graph_reserve(ctx, n_tokens, n_seqs, n_tokens);
- if (!gf_pp) {
- LOG_ERR("failed to reserve prompt processing graph\n");
- return 1;
- }
- extract_graph_ops(gf_pp, "pp", tests);
-
- auto * gf_tg = llama_graph_reserve(ctx, n_seqs, n_seqs, n_seqs);
- if (!gf_tg) {
- LOG_ERR("failed to reserve token generation graph\n");
- return 1;
- }
- extract_graph_ops(gf_tg, "tg", tests);
-
- LOG_INF("%d unique ops total\n", (int) tests.size());
-
- std::ofstream f(params.out_file);
-
- if (!f.is_open()) {
- LOG_ERR("unable to open output file: %s\n", params.out_file.c_str());
- return 1;
- }
-
- for (const auto& test : tests) {
- test.serialize(f);
- }
-
- return 0;
-}
printf(" --output specifies output format (default: console, options: console, sql, csv)\n");
printf(" --list-ops lists all available GGML operations\n");
printf(" --show-coverage shows test coverage\n");
- printf(" --test-file reads test operators from a test file generated by llama-export-graph-ops\n");
+ printf(" --test-file reads test operators from a test file generated by test-export-graph-ops\n");
printf(" -j <n> runs tests using <n> parallel worker threads (default: 1, test mode only)\n");
}
--- /dev/null
+#include "arg.h"
+#include "common.h"
+#include "log.h"
+#include "llama-cpp.h"
+#include "../src/llama-ext.h"
+#include "ggml.h"
+#include "gguf-model-data.h"
+#include "gguf.h"
+#include "ggml-backend.h"
+#include "download.h"
+
+#include <array>
+#include <vector>
+#include <set>
+#include <fstream>
+#include <iostream>
+#include <random>
+
+// Noop because weights are not needed
+static void set_tensor_data(struct ggml_tensor * tensor, void * userdata) {
+ GGML_UNUSED(tensor);
+ GGML_UNUSED(userdata);
+}
+
+struct input_tensor {
+ ggml_type type;
+ std::array<int64_t, 4> ne;
+ std::array<size_t, 4> nb;
+
+ input_tensor(ggml_type type, int64_t * ne, size_t * nb): type(type) {
+ memcpy(this->ne.data(), ne, 4 * sizeof(int64_t));
+ memcpy(this->nb.data(), nb, 4 * sizeof(size_t));
+ }
+
+ bool operator<(const input_tensor &b) const {
+ return std::tie(type, ne, nb) <
+ std::tie(b.type, b.ne, b.nb);
+ }
+
+ void serialize(std::ostream& out) const {
+ out << type << ' ';
+ for (size_t i = 0; i < 4; i++) {
+ out << ne[i] << ' ';
+ }
+ for (size_t i = 0; i < 4; i++) {
+ out << nb[i] << ' ';
+ }
+ }
+};
+
+struct test_object {
+ ggml_op op;
+ ggml_type type;
+ std::array<int64_t, 4> ne;
+ std::vector<int32_t> op_params;
+ std::vector<input_tensor> sources;
+ std::string name;
+
+ void serialize(std::ostream& out) const {
+ out << op << ' ' << type << ' ';
+ for (size_t i = 0; i < 4; i++) {
+ out << ne[i] << ' ';
+ }
+
+ out << op_params.size() << ' ';
+ for (size_t i = 0; i < op_params.size(); i++) {
+ out << op_params[i] << ' ';
+ }
+
+ out << sources.size() << ' ';
+ for (size_t s = 0; s < sources.size(); s++) {
+ sources[s].serialize(out);
+ }
+
+ if (!name.empty()) {
+ out << name;
+ } else {
+ out << '-';
+ }
+
+ out << '\n';
+ }
+
+ bool operator<(const test_object &b) const {
+ return std::tie(op, type, ne, op_params, sources) <
+ std::tie(b.op, b.type, b.ne, b.op_params, b.sources);
+ }
+};
+
+static void extract_graph_ops(ggml_cgraph * cgraph, const char * label, std::set<test_object> & tests) {
+ int n_nodes = ggml_graph_n_nodes(cgraph);
+ int n_skipped = 0;
+ int n_before = (int) tests.size();
+ for (int i = 0; i < n_nodes; i++) {
+ ggml_tensor * node = ggml_graph_node(cgraph, i);
+
+ if (node->op == GGML_OP_NONE || node->op == GGML_OP_VIEW || node->op == GGML_OP_RESHAPE || node->op == GGML_OP_PERMUTE || node->op == GGML_OP_TRANSPOSE) {
+ n_skipped++;
+ continue;
+ }
+
+ test_object test;
+
+ test.op = node->op;
+ test.type = node->type;
+ memcpy(&test.ne, node->ne, 4 * sizeof(int64_t));
+
+ test.op_params.resize(GGML_MAX_OP_PARAMS / sizeof(int32_t));
+ memcpy(test.op_params.data(), node->op_params, GGML_MAX_OP_PARAMS);
+
+ for (size_t s = 0; s < GGML_MAX_SRC; s++) {
+ if (node->src[s] == nullptr) {
+ break;
+ }
+
+ test.sources.emplace_back(node->src[s]->type, node->src[s]->ne, node->src[s]->nb);
+ }
+
+ test.name = node->name;
+ tests.insert(test);
+ }
+
+ int n_new = (int) tests.size() - n_before;
+ LOG_INF("%s: %d unique ops, %d total nodes, %d skipped (view ops)\n",
+ label, n_new, n_nodes, n_skipped);
+}
+
+int main(int argc, char ** argv) {
+ common_params params;
+ params.out_file = "tests.txt";
+
+ common_init();
+
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_EXPORT_GRAPH_OPS)) {
+ return 1;
+ }
+
+ // Load CPU-only
+ ggml_backend_dev_t cpu_device = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
+ params.devices = { cpu_device, nullptr };
+ params.fit_params = false;
+ params.n_gpu_layers = 0;
+
+ params.warmup = false;
+
+ llama_context * ctx;
+ common_init_result_ptr init_result;
+ llama_context_ptr ctx2;
+ llama_model_ptr model;
+
+ if (params.model.hf_repo.empty()) {
+ init_result = common_init_from_params(params);
+
+ ctx = init_result->context();
+ } else {
+#ifdef LLAMA_HF_FETCH
+ auto [hf_repo, hf_quant] = common_download_split_repo_tag(params.model.hf_repo);
+ if (hf_quant.empty() || hf_quant == "latest") {
+ hf_quant = "Q4_K_M";
+ }
+
+ gguf_context_ptr gguf_ctx = gguf_fetch_gguf_ctx(hf_repo, hf_quant);
+ if (!gguf_ctx) {
+ LOG_ERR("failed to fetch GGUF metadata from %s\n", hf_repo.c_str());
+ return 1;
+ }
+
+ llama_model_params model_params = llama_model_default_params();
+ model_params.devices = params.devices.data();
+ model_params.no_alloc = true;
+
+ model.reset(llama_model_init_from_user(gguf_ctx.get(), set_tensor_data, nullptr, model_params));
+
+ if (!model) {
+ LOG_ERR("failed to create llama_model from %s\n", hf_repo.c_str());
+ return 1;
+ }
+
+ llama_context_params ctx_params = llama_context_default_params();
+ ctx2.reset(llama_init_from_model(model.get(), ctx_params));
+ ctx = ctx2.get();
+
+ if (!ctx) {
+ LOG_ERR("failed to create llama_context\n");
+ return 1;
+ }
+#else
+ LOG_ERR("test-export-graph-ops compiled without HF fetch support\n");
+ return 1;
+#endif
+ }
+
+ const uint32_t n_seqs = llama_n_seq_max(ctx);
+ const uint32_t n_tokens = std::min(llama_n_ctx(ctx), llama_n_ubatch(ctx));
+
+ std::set<test_object> tests;
+
+ auto * gf_pp = llama_graph_reserve(ctx, n_tokens, n_seqs, n_tokens);
+ if (!gf_pp) {
+ LOG_ERR("failed to reserve prompt processing graph\n");
+ return 1;
+ }
+ extract_graph_ops(gf_pp, "pp", tests);
+
+ auto * gf_tg = llama_graph_reserve(ctx, n_seqs, n_seqs, n_seqs);
+ if (!gf_tg) {
+ LOG_ERR("failed to reserve token generation graph\n");
+ return 1;
+ }
+ extract_graph_ops(gf_tg, "tg", tests);
+
+ LOG_INF("%d unique ops total\n", (int) tests.size());
+
+ std::ofstream f(params.out_file);
+
+ if (!f.is_open()) {
+ LOG_ERR("unable to open output file: %s\n", params.out_file.c_str());
+ return 1;
+ }
+
+ for (const auto& test : tests) {
+ test.serialize(f);
+ }
+
+ return 0;
+}
-set(TARGET rpc-server)
+set(TARGET ggml-rpc-server)
add_executable(${TARGET} rpc-server.cpp)
target_link_libraries(${TARGET} PRIVATE ggml)
target_compile_features(${TARGET} PRIVATE cxx_std_17)
> This example and the RPC backend are currently in a proof-of-concept development stage. As such, the functionality is fragile and
> insecure. **Never run the RPC server on an open network or in a sensitive environment!**
-The `rpc-server` allows exposing `ggml` devices on a remote host.
-The RPC backend communicates with one or several instances of `rpc-server` and offloads computations to them.
+The `ggml-rpc-server` allows exposing `ggml` devices on a remote host.
+The RPC backend communicates with one or several instances of `ggml-rpc-server` and offloads computations to them.
This can be used for distributed LLM inference with `llama.cpp` in the following way:
```mermaid
rpcb<-->|TCP|srvb
rpcb<-.->|TCP|srvn
subgraph hostn[Host N]
- srvn[rpc-server]<-.->dev4["CUDA0"]
- srvn[rpc-server]<-.->dev5["CPU"]
+ srvn[ggml-rpc-server]<-.->dev4["CUDA0"]
+ srvn[ggml-rpc-server]<-.->dev5["CPU"]
end
subgraph hostb[Host B]
- srvb[rpc-server]<-->dev3["Metal"]
+ srvb[ggml-rpc-server]<-->dev3["Metal"]
end
subgraph hosta[Host A]
- srva[rpc-server]<-->dev["CUDA0"]
- srva[rpc-server]<-->dev2["CUDA1"]
+ srva[ggml-rpc-server]<-->dev["CUDA0"]
+ srva[ggml-rpc-server]<-->dev2["CUDA1"]
end
subgraph host[Main Host]
local["Local devices"]<-->ggml[llama-cli]
class local,dev,dev2,dev3,dev4,dev5 devcls
```
-By default, `rpc-server` exposes all available accelerator devices on the host.
+By default, `ggml-rpc-server` exposes all available accelerator devices on the host.
If there are no accelerators, it exposes a single `CPU` device.
## Usage
### Remote hosts
On each remote host, build the backends for each accelerator by adding `-DGGML_RPC=ON` to the build options.
-For example, to build the `rpc-server` with support for CUDA accelerators:
+For example, to build the `ggml-rpc-server` with support for CUDA accelerators:
```bash
mkdir build-rpc-cuda
cmake --build . --config Release
```
-When started, the `rpc-server` will detect and expose all available `CUDA` devices:
+When started, the `ggml-rpc-server` will detect and expose all available `CUDA` devices:
```bash
-$ bin/rpc-server
+$ bin/ggml-rpc-server
ggml_cuda_init: GGML_CUDA_FORCE_MMQ: no
ggml_cuda_init: GGML_CUDA_FORCE_CUBLAS: no
ggml_cuda_init: found 1 CUDA devices:
You can control the set of exposed CUDA devices with the `CUDA_VISIBLE_DEVICES` environment variable or the `--device` command line option. The following two commands have the same effect:
```bash
-$ CUDA_VISIBLE_DEVICES=0 bin/rpc-server -p 50052
-$ bin/rpc-server --device CUDA0 -p 50052
+$ CUDA_VISIBLE_DEVICES=0 bin/ggml-rpc-server -p 50052
+$ bin/ggml-rpc-server --device CUDA0 -p 50052
```
### Main host
On the main host build `llama.cpp` with the backends for the local devices and add `-DGGML_RPC=ON` to the build options.
-Finally, when running `llama-cli` or `llama-server`, use the `--rpc` option to specify the host and port of each `rpc-server`:
+Finally, when running `llama-cli` or `llama-server`, use the `--rpc` option to specify the host and port of each `ggml-rpc-server`:
```bash
$ llama-cli -hf ggml-org/gemma-3-1b-it-GGUF -ngl 99 --rpc 192.168.88.10:50052,192.168.88.11:50052
To enable the cache, use the `-c` option:
```bash
-$ bin/rpc-server -c
+$ bin/ggml-rpc-server -c
```
By default, the cache is stored in the `$HOME/.cache/llama.cpp/rpc` directory and can be controlled via the `LLAMA_CACHE` environment variable.
### Troubleshooting
-Use the `GGML_RPC_DEBUG` environment variable to enable debug messages from `rpc-server`:
+Use the `GGML_RPC_DEBUG` environment variable to enable debug messages from `ggml-rpc-server`:
```bash
-$ GGML_RPC_DEBUG=1 bin/rpc-server
+$ GGML_RPC_DEBUG=1 bin/ggml-rpc-server
```