native: pf-serve on a GPU reads the model once, not twice (#3590)
On a GPU (Metal, Vulkan), privacy-filter.cpp read the whole 1.6 GB GGUF into a CPU ggml context, copied every tensor into the device's buffer, and kept the CPU copy "as source" for as long as pf-serve ran. #3590's Mac held 4.7 GB of physical footprint idle. patches/weights-once.patch opens the GGUF metadata-only when the backend isn't the CPU, and reads each tensor from the file straight into the device's buffer through one 16 MB chunk. The CPU backend still reads it whole and uses that buffer in place, as before. Run: build.sh for every target on CI (privacy-filter run 40188759504, green on all five), then a probe (start pf-serve, classify 7 texts in en/zh/code/cards plus a 57K and a 20K text, 20 hot requests, 3 long ones, read memory after 5 s idle) on the released pf-v1 and on this commit's CI build, with the pinned model (sha256 80efc180…): Mac M4 Pro, Metal, GGML_METAL_NO_RESIDENCY=1 as magpie starts it: idle phys_footprint 4777 MB -> 1683 MB, RSS 4798 -> 1706 MB hot request median 9.0/13.8 ms -> 10.8/9.9 ms (two runs each, load average ~180, so noisy); long 1.5-1.9 s both Mac, CPU: 1852 MB both, as before Windows, AMD 8060S, Vulkan: private bytes 6629 MB -> 1968 MB, working set 3258 -> 174 MB hot median 4.2-4.7 ms both, long 0.79-0.85 s both, load 1.2 -> 0.8 s Windows, CPU: 1810 MB working set both The spans are byte-identical before and after on each backend (Metal, Mac CPU, Vulkan, Windows CPU; each backend's own as before). Not tried: Linux's pf-serve-vulkan on a real GPU (the same code path as Windows' Vulkan); linux-arm64 and darwin-amd64 run on the CPU only, which this doesn't change, and were only built.
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
diff --git a/src/model.cpp b/src/model.cpp
|
||||
index 80afe56..0ff1edb 100644
|
||||
--- a/src/model.cpp
|
||||
+++ b/src/model.cpp
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
#include <ggml.h>
|
||||
|
||||
+#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
+#include <fstream>
|
||||
|
||||
namespace pf {
|
||||
|
||||
@@ -57,15 +59,19 @@ void yarn_freq_factors(const hparams & hp, std::vector<float> & ff, float & attn
|
||||
bool model::load(const std::string & gguf_path, const std::string & device, int n_threads) {
|
||||
release();
|
||||
|
||||
- if (!file.open(gguf_path, /*with_data =*/ true)) {
|
||||
- error = file.error;
|
||||
- return false;
|
||||
- }
|
||||
if (!be.init(device, n_threads)) {
|
||||
error = be.error;
|
||||
return false;
|
||||
}
|
||||
- if (!map_tensors() || !realize_weights()) {
|
||||
+ // The CPU uses the loader's buffer in place, so it is read whole. A
|
||||
+ // device gets the metadata only, and realize_weights reads each tensor
|
||||
+ // from the file into the device's buffer: the weights are never held
|
||||
+ // twice, once in the device's buffer and once here.
|
||||
+ if (!file.open(gguf_path, /*with_data =*/ be.is_cpu())) {
|
||||
+ error = file.error;
|
||||
+ return false;
|
||||
+ }
|
||||
+ if (!map_tensors() || !realize_weights(gguf_path)) {
|
||||
return false;
|
||||
}
|
||||
yarn_freq_factors(file.hp, freq_factors, attn_factor);
|
||||
@@ -116,7 +122,7 @@ bool model::map_tensors() {
|
||||
return true;
|
||||
}
|
||||
|
||||
-bool model::realize_weights() {
|
||||
+bool model::realize_weights(const std::string & gguf_path) {
|
||||
if (be.is_cpu()) {
|
||||
// Zero-copy: the GGUF loaded with no_alloc=false, so all tensor data
|
||||
// lives in one contiguous ctx buffer. Wrap it as a CPU backend buffer
|
||||
@@ -155,11 +161,36 @@ bool model::realize_weights() {
|
||||
error = "device weight alloc failed (out of device memory?)";
|
||||
return false;
|
||||
}
|
||||
+ // Each tensor's bytes go from the file to the device through one
|
||||
+ // 16 MB buffer.
|
||||
+ std::ifstream in(gguf_path, std::ios::binary);
|
||||
+ if (!in) {
|
||||
+ error = "failed to reopen GGUF: " + gguf_path;
|
||||
+ return false;
|
||||
+ }
|
||||
+ const size_t data_off = gguf_get_data_offset(file.guf);
|
||||
+ std::vector<char> chunk(16u << 20);
|
||||
for (ggml_tensor * s = ggml_get_first_tensor(file.ctx); s; s = ggml_get_next_tensor(file.ctx, s)) {
|
||||
- ggml_tensor * d = ggml_get_tensor(device_ctx, ggml_get_name(s));
|
||||
- ggml_backend_tensor_set(d, s->data, 0, ggml_nbytes(s));
|
||||
+ ggml_tensor * d = ggml_get_tensor(device_ctx, ggml_get_name(s));
|
||||
+ const int64_t id = gguf_find_tensor(file.guf, ggml_get_name(s));
|
||||
+ if (id < 0) {
|
||||
+ error = std::string("tensor not in GGUF: ") + ggml_get_name(s);
|
||||
+ return false;
|
||||
+ }
|
||||
+ in.seekg((std::streamoff) (data_off + gguf_get_tensor_offset(file.guf, id)));
|
||||
+ const size_t n = ggml_nbytes(s);
|
||||
+ for (size_t off = 0; off < n;) {
|
||||
+ const size_t k = std::min(chunk.size(), n - off);
|
||||
+ if (!in.read(chunk.data(), (std::streamsize) k)) {
|
||||
+ error = std::string("short read of tensor ") + ggml_get_name(s);
|
||||
+ return false;
|
||||
+ }
|
||||
+ ggml_backend_tensor_set(d, chunk.data(), off, k);
|
||||
+ off += k;
|
||||
+ }
|
||||
}
|
||||
- // Repoint the weight map at the device tensors (file.ctx stays as source).
|
||||
+ // Repoint the weight map at the device tensors (file.ctx holds their
|
||||
+ // metadata alone).
|
||||
auto remap = [&](ggml_tensor *& t) { if (t) t = ggml_get_tensor(device_ctx, ggml_get_name(t)); };
|
||||
remap(tok_embd); remap(output_norm); remap(cls_w); remap(cls_b);
|
||||
for (auto & l : layers) {
|
||||
diff --git a/src/model.h b/src/model.h
|
||||
index 9fd8f06..6dc37d6 100644
|
||||
--- a/src/model.h
|
||||
+++ b/src/model.h
|
||||
@@ -69,7 +69,7 @@ struct model {
|
||||
tap_map * taps = nullptr);
|
||||
|
||||
private:
|
||||
- bool realize_weights();
|
||||
+ bool realize_weights(const std::string & gguf_path);
|
||||
bool map_tensors();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user