llama.cpp patch: measure where host copies leave the compute stream

The patch put host-to-device copies up to a fixed 1 MiB in the compute
stream. The crossover is where a copy's own transfer-queue submission
and the compute queue's semaphore wait cost as much as the copy they
let overlap, so ggml-vulkan now measures it once per device on first
use: that overhead with the host clock, times the host-to-device
bandwidth from GPU timestamps around a staged copy. It aborts with the
reason when a queue family has no timestamps, and
GGML_VK_COMPUTE_STREAM_COPY_MAX sets the size instead. On the R9700
here: 130 to 170 us and 1.2 GB/s, so about 150 to 200 KB.

test-backend-sched-inputs also copies a tensor computed on the CPU to
the device at 16 KiB and 4 MiB, on both sides of a 64 KiB limit (or
the measured one with --measured). One comment in ggml-vulkan-types.h
is dropped.
This commit is contained in:
geramyloveless committed 2026-10-04 08:14:11 -07:00
1 parent 3dc1b9af99
commit 37aa287df3
3 files changed
+245 -69

No files matched your search

+237 -64
View File
@@ -16,11 +16,20 @@ freed once the backend has synchronized.
ggml_vk_synchronize no longer adds an empty queue submission to signal its
fence: the fence (and the wait for transfer-queue work) goes with the last
compute submission, and when only transfer-queue work is outstanding the host
waits for its timeline semaphore. Small host-to-device copies go into the
compute stream instead of a transfer-queue submission of their own.
waits for its timeline semaphore.
A host-to-device copy goes on the transfer queue only when that pays off: its
own submission and the compute queue's semaphore wait must cost less than the
copy it lets overlap. The crossover, that overhead times the host-to-device
bandwidth, is measured once per device on first use (the overhead with the
host clock, the bandwidth with GPU timestamps around a staged copy) and
logged; GGML_VK_COMPUTE_STREAM_COPY_MAX sets it instead. Smaller copies go in
the compute stream.
test-backend-sched-inputs checks that inputs are taken when the graph is
queued and ordered against the work already queued on the device.
queued and ordered against the work already queued on the device, and that
tensors computed on the CPU reach the device on both sides of the copy size
limit (--measured uses the measured limit).
diff --git a/ggml/src/ggml-backend-impl.h b/ggml/src/ggml-backend-impl.h
index dc7367d..d2db106 100644
@@ -222,22 +231,30 @@ index 996c57e..00f5c4a 100644
static ggml_guid_t ggml_backend_remoting_guid() {
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan-types.h b/ggml/src/ggml-vulkan/ggml-vulkan-types.h
index 4158ff7..f26e858 100644
index 4158ff7..af21a87 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan-types.h
+++ b/ggml/src/ggml-vulkan/ggml-vulkan-types.h
@@ -1250,6 +1250,10 @@ struct ggml_backend_vk_context {
@@ -712,6 +712,9 @@ struct vk_device_struct {
bool single_queue;
bool support_async;
bool async_use_transfer_queue;
+ // largest host-to-device copy that goes into the compute stream, measured on first use
+ size_t compute_stream_copy_max;
+ bool compute_stream_copy_max_known {};
bool has_internally_synchronized_queues = false;
uint32_t subgroup_size;
uint32_t subgroup_size_log2;
@@ -1250,6 +1253,8 @@ struct ggml_backend_vk_context {
ggml_vk_garbage_collector gc;
size_t prealloc_size_x, prealloc_size_y, prealloc_size_split_k, prealloc_size_add_rms_partials, prealloc_size_add_rms_partials_offset;
vk_buffer prealloc_x, prealloc_y, prealloc_split_k, prealloc_add_rms_partials, sync_staging;
+ // Host memory that writes queued with set_tensor_async_staged copy their data into, in use
+ // until the backend's work completes (ggml_vk_synchronize)
+ std::vector<vk_buffer> input_staging;
+ size_t input_staging_used {};
vk::Fence fence, almost_ready_fence;
bool submit_pending {};
bool almost_ready_fence_pending {};
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan.cpp b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
index bfc65be..fed3d8a 100644
index bfc65be..aa17db1 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan.cpp
+++ b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
@@ -12849,6 +12849,11 @@ void ggml_vk_cleanup(ggml_backend_vk_context * ctx) {
@@ -332,14 +349,140 @@ index bfc65be..fed3d8a 100644
static void ggml_backend_vk_get_tensor_2d_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset,
size_t size, size_t n_copies, size_t stride_tensor, size_t stride_data) {
VK_LOG_DEBUG("ggml_backend_vk_get_tensor_2d_async(" << size << ", " << n_copies << ")");
@@ -13333,16 +13404,26 @@ static bool ggml_backend_vk_cpy_tensor_async(ggml_backend_t backend_src, ggml_ba
@@ -13277,6 +13348,127 @@ static void ggml_backend_vk_get_tensor_async(ggml_backend_t backend, const ggml_
ggml_backend_vk_get_tensor_2d_async(backend, tensor, data, offset, size, 1, size, size);
}
+// A host-to-device copy on the transfer queue overlaps with compute, but costs a submission of its own
+// and a semaphore wait on the compute queue. It pays off when the copy takes longer than that overhead:
+// the crossover is the overhead times the host-to-device bandwidth, both measured once per device,
+// the overhead with the host clock and the bandwidth with GPU timestamps around a staged copy.
+// GGML_VK_COMPUTE_STREAM_COPY_MAX (bytes) sets it instead.
+static size_t ggml_vk_compute_stream_copy_max(vk_device & device) {
+ std::lock_guard<std::recursive_mutex> guard(device->mutex);
+ if (device->compute_stream_copy_max_known) {
+ return device->compute_stream_copy_max;
+ }
+ if (const char * env = getenv("GGML_VK_COMPUTE_STREAM_COPY_MAX")) {
+ device->compute_stream_copy_max = std::strtoull(env, nullptr, 0);
+ device->compute_stream_copy_max_known = true;
+ return device->compute_stream_copy_max;
+ }
+
+ const std::vector<vk::QueueFamilyProperties> families = device->physical_device.getQueueFamilyProperties();
+ for (vk_queue * q : { device->compute_queue.get(), device->transfer_queue.get() }) {
+ if (families[q->queue_family_index].timestampValidBits == 0) {
+ GGML_ABORT("ggml_vulkan: %s: queue family %u has no timestamps, so the host-to-device copy bandwidth "
+ "cannot be measured; set GGML_VK_COMPUTE_STREAM_COPY_MAX", device->name.c_str(), q->queue_family_index);
+ }
+ }
+
+ constexpr size_t probe_size = 16 * 1024 * 1024;
+ constexpr int iterations = 5;
+ vk_buffer dev_buf, host_buf;
+ vk::Semaphore sem;
+ vk::QueryPool pool;
+ try {
+ dev_buf = ggml_vk_create_buffer_device(device, probe_size);
+ host_buf = ggml_vk_create_buffer_check(device, probe_size,
+ vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
+ vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
+ vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eTimeline, 0 };
+ vk::SemaphoreCreateInfo sci{};
+ sci.setPNext(&tci);
+ sem = device->device.createSemaphore(sci);
+ vk_semaphore tl{ sem, 0 };
+ vk::QueryPoolCreateInfo qci{};
+ qci.queryType = vk::QueryType::eTimestamp;
+ qci.queryCount = 2;
+ pool = device->device.createQueryPool(qci);
+
+ // start from idle queues, so earlier work does not count
+ for (vk_queue * q : { device->compute_queue.get(), device->transfer_queue.get() }) {
+ q->handle->lock();
+ q->handle->queue.waitIdle();
+ q->handle->unlock();
+ }
+
+ // Host time of one compute submission, after a transfer submission it waits for when
+ // with_transfer; with copy, GPU time of a staged copy of probe_size bytes in it.
+ auto run = [&](bool with_transfer, bool copy) {
+ vk_context c = ggml_vk_create_temporary_context(device->compute_queue->cmd_pool);
+ ggml_vk_ctx_begin(device, c);
+ if (copy) {
+ c->s->buffer->buf.resetQueryPool(pool, 0, 2);
+ c->s->buffer->buf.writeTimestamp(vk::PipelineStageFlagBits::eTopOfPipe, pool, 0);
+ c->s->buffer->buf.copyBuffer(host_buf->buffer, dev_buf->buffer, { vk::BufferCopy{ 0, 0, probe_size } });
+ c->s->buffer->buf.writeTimestamp(vk::PipelineStageFlagBits::eBottomOfPipe, pool, 1);
+ }
+ ggml_vk_ctx_end(c);
+ vk_context t;
+ if (with_transfer) {
+ t = ggml_vk_create_temporary_context(device->transfer_queue->cmd_pool);
+ ggml_vk_ctx_begin(device, t);
+ t->s->buffer->buf.copyBuffer(host_buf->buffer, dev_buf->buffer, { vk::BufferCopy{ 0, 0, 256 } });
+ ggml_vk_ctx_end(t);
+ tl.value++;
+ t->seqs.back().back().signal_semaphores.push_back(tl);
+ c->seqs.back().back().wait_semaphores.push_back(tl);
+ }
+ const auto start = std::chrono::steady_clock::now();
+ if (t) {
+ ggml_vk_submit(t, {});
+ }
+ ggml_vk_submit(c, device->fence);
+ VK_CHECK(device->device.waitForFences({ device->fence }, true, UINT64_MAX), "compute stream copy probe", device);
+ const auto end = std::chrono::steady_clock::now();
+ device->device.resetFences({ device->fence });
+ if (copy) {
+ uint64_t ts[2];
+ VK_CHECK(device->device.getQueryPoolResults(pool, 0, 2, sizeof(ts), ts, sizeof(uint64_t),
+ vk::QueryResultFlagBits::e64 | vk::QueryResultFlagBits::eWait),
+ "compute stream copy timestamps", device);
+ return (double) (ts[1] - ts[0]) * device->properties.limits.timestampPeriod;
+ }
+ return (double) std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
+ };
+ auto fastest = [&](bool with_transfer, bool copy) {
+ double best = run(with_transfer, copy);
+ for (int i = 1; i < iterations; i++) {
+ best = std::min(best, run(with_transfer, copy));
+ }
+ return best;
+ };
+
+ const double transfer_ns = fastest(true, false) - fastest(false, false);
+ const double copy_ns = fastest(false, true);
+ ggml_vk_queue_command_pools_cleanup(device);
+ if (copy_ns <= 0) {
+ GGML_ABORT("ggml_vulkan: %s: the timestamps around a %zu byte host-to-device copy measured no time",
+ device->name.c_str(), probe_size);
+ }
+ const double bytes_per_ns = probe_size / copy_ns;
+ device->compute_stream_copy_max = transfer_ns > 0 ? (size_t) (transfer_ns * bytes_per_ns) : 0;
+ device->compute_stream_copy_max_known = true;
+ GGML_LOG_INFO("ggml_vulkan: %s: a transfer queue submission costs %.1f us, host-to-device copies run at %.1f GB/s: "
+ "copies up to %zu bytes go in the compute stream\n", device->name.c_str(), transfer_ns / 1e3,
+ bytes_per_ns, device->compute_stream_copy_max);
+ } catch (const vk::SystemError & e) {
+ GGML_ABORT("ggml_vulkan: %s: measuring host-to-device copy costs failed: %s", device->name.c_str(), e.what());
+ }
+ device->device.destroyQueryPool(pool);
+ device->device.destroySemaphore(sem);
+ ggml_vk_destroy_buffer(host_buf);
+ ggml_vk_destroy_buffer(dev_buf);
+ return device->compute_stream_copy_max;
+}
+
static bool ggml_backend_vk_cpy_tensor_async(ggml_backend_t backend_src, ggml_backend_t backend_dst, const ggml_tensor * src, ggml_tensor * dst) {
VK_LOG_DEBUG("ggml_backend_vk_cpy_tensor_async(" << src << " -> " << dst << ", size=" << ggml_nbytes(src) << ")");
ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend_dst->context;
@@ -13333,16 +13525,24 @@ static bool ggml_backend_vk_cpy_tensor_async(ggml_backend_t backend_src, ggml_ba
return true;
}
+ // A small copy goes into the compute stream: a transfer queue submission of its own, and the
+ // semaphore wait the compute queue then needs, cost more than the copy could overlap.
+ static constexpr size_t max_compute_stream_copy_size = 1024 * 1024;
+ const bool use_transfer_queue = ctx->device->async_use_transfer_queue && ggml_nbytes(src) > max_compute_stream_copy_size;
+ const bool use_transfer_queue = ctx->device->async_use_transfer_queue &&
+ ggml_nbytes(src) > ggml_vk_compute_stream_copy_max(ctx->device);
+
vk_context cpy_ctx;
- if (ctx->device->async_use_transfer_queue) {
@@ -363,7 +506,7 @@ index bfc65be..fed3d8a 100644
}
return false;
@@ -13352,10 +13433,15 @@ void ggml_vk_synchronize(ggml_backend_vk_context * ctx) {
@@ -13352,10 +13552,15 @@ void ggml_vk_synchronize(ggml_backend_vk_context * ctx) {
VK_LOG_DEBUG("ggml_vk_synchronize()");
bool do_transfer = !ctx->compute_ctx.expired();
@@ -379,7 +522,7 @@ index bfc65be..fed3d8a 100644
vk_context compute_ctx;
vk_command_buffer* cmd_buf = nullptr;
@@ -13375,16 +13461,32 @@ void ggml_vk_synchronize(ggml_backend_vk_context * ctx) {
@@ -13375,16 +13580,32 @@ void ggml_vk_synchronize(ggml_backend_vk_context * ctx) {
ggml_vk_submit(compute_ctx, ctx->fence);
VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "synchronize waitForFences", ctx->device);
ctx->device->device.resetFences({ ctx->fence });
@@ -416,7 +559,7 @@ index bfc65be..fed3d8a 100644
vk::TimelineSemaphoreSubmitInfo tl_info{
1, &ctx->transfer_semaphore.value,
0, nullptr,
@@ -13401,7 +13503,7 @@ void ggml_vk_synchronize(ggml_backend_vk_context * ctx) {
@@ -13401,7 +13622,7 @@ void ggml_vk_synchronize(ggml_backend_vk_context * ctx) {
} else {
ctx->device->compute_queue->handle->submit({}, ctx->fence);
}
@@ -425,7 +568,7 @@ index bfc65be..fed3d8a 100644
ggml_vk_wait_for_fence(ctx);
}
ctx->submit_pending = false;
@@ -13417,6 +13519,10 @@ void ggml_vk_synchronize(ggml_backend_vk_context * ctx) {
@@ -13417,6 +13638,10 @@ void ggml_vk_synchronize(ggml_backend_vk_context * ctx) {
}
ctx->compute_ctx.reset();
}
@@ -436,7 +579,7 @@ index bfc65be..fed3d8a 100644
}
static void ggml_backend_vk_synchronize(ggml_backend_t backend) {
@@ -15133,6 +15239,7 @@ static ggml_backend_i ggml_backend_vk_interface = {
@@ -15133,6 +15358,7 @@ static ggml_backend_i ggml_backend_vk_interface = {
/* .event_record = */ ggml_backend_vk_event_record,
/* .event_wait = */ ggml_backend_vk_event_wait,
/* .graph_optimize = */ ggml_vk_graph_optimize,
@@ -494,19 +637,23 @@ index 96d910a..0449272 100644
llama_build_and_test(test-model-load-cancel.cpp LABEL "model")
diff --git a/tests/test-backend-sched-inputs.cpp b/tests/test-backend-sched-inputs.cpp
new file mode 100644
index 0000000..c04d99a
index 0000000..109ff3f
--- /dev/null
+++ b/tests/test-backend-sched-inputs.cpp
@@ -0,0 +1,186 @@
+// Graph inputs through the backend scheduler: the scheduler copies user inputs to the device
+// of the split that reads them. Checks that
+// - the copy is taken when the graph is queued: the caller overwrites its input data right
@@ -0,0 +1,216 @@
+// Tensors the backend scheduler copies from the CPU to the device of the split that reads them.
+// Checks that
+// - a user input is taken when the graph is queued: the caller overwrites its input data right
+// after ggml_backend_sched_graph_compute_async, and the result must not change,
+// - the copy is ordered against the device's queued work: a second evaluation queued before
+// the first one completed overwrites the same device copy of the inputs, and both results,
+// the first read back asynchronously before the second is queued, must be right.
+// Run for each GPU device, with the CPU inputs in plain host memory and in the device's host
+// buffer type when it has one.
+// the first read back asynchronously before the second is queued, must be right,
+// - a tensor computed on the CPU reaches the device intact, for copies on both sides of the
+// backend's size limit for copies in its compute stream (the Vulkan backend's
+// GGML_VK_COMPUTE_STREAM_COPY_MAX, set here to 64 KiB unless the environment sets it;
+// with --measured, the backend measures its own).
+// Run for each GPU device, with the CPU tensors in plain host memory and in the device's host
+// buffer type when it has one, with tensors of 16 KiB and 4 MiB.
+
+#include <ggml.h>
+#include <ggml-alloc.h>
@@ -515,27 +662,30 @@ index 0000000..c04d99a
+#include <cmath>
+#include <cstdio>
+#include <cstdlib>
+#include <string>
+#include <vector>
+
+static constexpr int64_t n = 1 << 20; // elements per input: enough work to still be running
+static constexpr int n_layers = 8; // weight multiplications per evaluation
+static constexpr int n_layers = 8; // device multiplications per evaluation
+
+struct model {
+ int64_t n = 0;
+ ggml_context * ctx_w = nullptr;
+ ggml_backend_buffer_t buf_w = nullptr;
+ ggml_tensor * w = nullptr;
+ std::vector<float> w_data;
+ ggml_backend_buffer_t buf_w = nullptr, buf_c = nullptr;
+ ggml_tensor * w = nullptr; // on the device
+ ggml_tensor * c = nullptr; // on the CPU: the first multiplication runs there
+ std::vector<float> w_data, c_data;
+};
+
+// out = (... ((x * w + y) * w + y) ...) with the weights on the device, so the scheduler runs it there
+// out = (... (((x * c) * w + y) * w + y) ...): x * c on the CPU, the rest on the device
+static ggml_cgraph * build_graph(ggml_context * ctx, const model & m, ggml_tensor ** x, ggml_tensor ** y, ggml_tensor ** out) {
+ *x = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, n);
+ *y = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, n);
+ *x = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, m.n);
+ *y = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, m.n);
+ ggml_set_name(*x, "x");
+ ggml_set_name(*y, "y");
+ ggml_set_input(*x);
+ ggml_set_input(*y);
+ ggml_tensor * cur = *x;
+ ggml_tensor * cur = ggml_mul(ctx, *x, m.c);
+ ggml_set_name(cur, "x_c");
+ for (int l = 0; l < n_layers; l++) {
+ cur = ggml_add(ctx, ggml_mul(ctx, cur, m.w), *y);
+ }
@@ -548,9 +698,9 @@ index 0000000..c04d99a
+}
+
+static std::vector<float> reference(const model & m, const std::vector<float> & x, const std::vector<float> & y) {
+ std::vector<float> r(n);
+ for (int64_t i = 0; i < n; i++) {
+ float cur = x[i];
+ std::vector<float> r(m.n);
+ for (int64_t i = 0; i < m.n; i++) {
+ float cur = x[i] * m.c_data[i];
+ for (int l = 0; l < n_layers; l++) {
+ cur = cur * m.w_data[i] + y[i];
+ }
@@ -560,16 +710,16 @@ index 0000000..c04d99a
+}
+
+static bool check(const char * what, const std::vector<float> & got, const std::vector<float> & want) {
+ for (int64_t i = 0; i < n; i++) {
+ for (size_t i = 0; i < want.size(); i++) {
+ if (std::fabs(got[i] - want[i]) > 1e-4f * (1.0f + std::fabs(want[i]))) {
+ fprintf(stderr, " FAIL %s: element %lld is %f, expected %f\n", what, (long long) i, got[i], want[i]);
+ fprintf(stderr, " FAIL %s: element %zu is %f, expected %f\n", what, i, got[i], want[i]);
+ return false;
+ }
+ }
+ return true;
+}
+
+static std::vector<float> values(int seed) {
+static std::vector<float> values(int64_t n, int seed) {
+ std::vector<float> v(n);
+ for (int64_t i = 0; i < n; i++) {
+ v[i] = 0.25f * (float) ((i * 7 + seed * 13) % 9) - 1.0f;
@@ -577,7 +727,7 @@ index 0000000..c04d99a
+ return v;
+}
+
+static bool run(ggml_backend_dev_t dev, bool host_buffer) {
+static bool run(ggml_backend_dev_t dev, bool host_buffer, int64_t n) {
+ ggml_backend_t gpu = ggml_backend_dev_init(dev, nullptr);
+ ggml_backend_t cpu = ggml_backend_init_by_type(GGML_BACKEND_DEVICE_TYPE_CPU, nullptr);
+ if (!gpu || !cpu) {
@@ -591,23 +741,31 @@ index 0000000..c04d99a
+ ggml_backend_free(cpu);
+ return true;
+ }
+ ggml_backend_buffer_type_t cpu_buft = host_buffer ? host_buft : ggml_backend_get_default_buffer_type(cpu);
+
+ model m;
+ ggml_init_params wp = { ggml_tensor_overhead() * 2, nullptr, true };
+ m.n = n;
+ ggml_init_params wp = { ggml_tensor_overhead() * 4, nullptr, true };
+ m.ctx_w = ggml_init(wp);
+ m.w = ggml_new_tensor_1d(m.ctx_w, GGML_TYPE_F32, n);
+ m.buf_w = ggml_backend_alloc_ctx_tensors(m.ctx_w, gpu);
+ ggml_context * ctx_c = ggml_init(wp);
+ m.c = ggml_new_tensor_1d(ctx_c, GGML_TYPE_F32, n);
+ m.buf_c = ggml_backend_alloc_ctx_tensors_from_buft(ctx_c, cpu_buft);
+ ggml_backend_buffer_set_usage(m.buf_w, GGML_BACKEND_BUFFER_USAGE_WEIGHTS);
+ ggml_backend_buffer_set_usage(m.buf_c, GGML_BACKEND_BUFFER_USAGE_WEIGHTS);
+ m.w_data.resize(n);
+ m.c_data.resize(n);
+ for (int64_t i = 0; i < n; i++) {
+ m.w_data[i] = 0.5f + 0.125f * (float) (i % 5);
+ m.c_data[i] = 1.0f + 0.25f * (float) (i % 3);
+ }
+ ggml_backend_tensor_set(m.w, m.w_data.data(), 0, n * sizeof(float));
+ ggml_backend_tensor_set(m.c, m.c_data.data(), 0, n * sizeof(float));
+
+ ggml_backend_t backends[2] = { gpu, cpu };
+ ggml_backend_buffer_type_t bufts[2] = { ggml_backend_get_default_buffer_type(gpu),
+ host_buffer ? host_buft : ggml_backend_get_default_buffer_type(cpu) };
+ ggml_backend_sched_t sched = ggml_backend_sched_new(backends, bufts, 2, GGML_DEFAULT_GRAPH_SIZE, false, true);
+ ggml_backend_buffer_type_t bufts[2] = { ggml_backend_get_default_buffer_type(gpu), cpu_buft };
+ ggml_backend_sched_t sched = ggml_backend_sched_new(backends, bufts, 2, GGML_DEFAULT_GRAPH_SIZE, false, false);
+
+ ggml_init_params gp = { ggml_tensor_overhead() * GGML_DEFAULT_GRAPH_SIZE + ggml_graph_overhead(), nullptr, true };
+ ggml_context * ctx = ggml_init(gp);
@@ -618,51 +776,64 @@ index 0000000..c04d99a
+ if (!ok) {
+ fprintf(stderr, " cannot allocate the graph\n");
+ }
+ if (ok && ggml_backend_sched_get_tensor_backend(sched, out) != gpu) {
+ fprintf(stderr, " the graph does not run on %s\n", ggml_backend_dev_name(dev));
+ if (ok && (ggml_backend_sched_get_tensor_backend(sched, out) != gpu ||
+ ggml_backend_sched_get_tensor_backend(sched, ggml_graph_get_tensor(gf, "x_c")) != cpu)) {
+ fprintf(stderr, " the graph is not split between the CPU and %s\n", ggml_backend_dev_name(dev));
+ ok = false;
+ }
+
+ const size_t bytes = n * sizeof(float);
+ const std::vector<float> garbage(n, NAN);
+ std::vector<float> got_a(n), got_b(n);
+ for (int iter = 0; ok && iter < 4; iter++) {
+ const std::vector<float> xa = values(4 * iter), ya = values(4 * iter + 1);
+ const std::vector<float> xb = values(4 * iter + 2), yb = values(4 * iter + 3);
+ const std::vector<float> xa = values(n, 4 * iter), ya = values(n, 4 * iter + 1);
+ const std::vector<float> xb = values(n, 4 * iter + 2), yb = values(n, 4 * iter + 3);
+
+ // A: queue it, then overwrite the inputs at once
+ ggml_backend_tensor_set(x, xa.data(), 0, n * sizeof(float));
+ ggml_backend_tensor_set(y, ya.data(), 0, n * sizeof(float));
+ ggml_backend_tensor_set(x, xa.data(), 0, bytes);
+ ggml_backend_tensor_set(y, ya.data(), 0, bytes);
+ ok = ggml_backend_sched_graph_compute_async(sched, gf) == GGML_STATUS_SUCCESS;
+ ggml_backend_tensor_set(x, garbage.data(), 0, n * sizeof(float));
+ ggml_backend_tensor_set(y, garbage.data(), 0, n * sizeof(float));
+ ggml_backend_tensor_get_async(gpu, out, got_a.data(), 0, n * sizeof(float));
+ ggml_backend_tensor_set(x, garbage.data(), 0, bytes);
+ ggml_backend_tensor_set(y, garbage.data(), 0, bytes);
+ ggml_backend_tensor_get_async(gpu, out, got_a.data(), 0, bytes);
+
+ // B: queued behind A, into the same device copies of the inputs
+ ggml_backend_tensor_set(x, xb.data(), 0, n * sizeof(float));
+ ggml_backend_tensor_set(y, yb.data(), 0, n * sizeof(float));
+ ggml_backend_tensor_set(x, xb.data(), 0, bytes);
+ ggml_backend_tensor_set(y, yb.data(), 0, bytes);
+ ok = ok && ggml_backend_sched_graph_compute_async(sched, gf) == GGML_STATUS_SUCCESS;
+ ggml_backend_tensor_set(x, garbage.data(), 0, n * sizeof(float));
+ ggml_backend_tensor_set(y, garbage.data(), 0, n * sizeof(float));
+ ggml_backend_tensor_set(x, garbage.data(), 0, bytes);
+ ggml_backend_tensor_set(y, garbage.data(), 0, bytes);
+ ggml_backend_sched_synchronize(sched);
+ ggml_backend_tensor_get(out, got_b.data(), 0, n * sizeof(float));
+ ggml_backend_tensor_get(out, got_b.data(), 0, bytes);
+
+ ok = ok && check("first evaluation", got_a, reference(m, xa, ya));
+ ok = ok && check("second evaluation", got_b, reference(m, xb, yb));
+ }
+
+ printf(" %s, inputs in %s: %s\n", ggml_backend_dev_name(dev),
+ printf(" %s, %lld KiB tensors, CPU tensors in %s: %s\n", ggml_backend_dev_name(dev), (long long) (bytes / 1024),
+ host_buffer ? ggml_backend_buft_name(host_buft) : "host memory", ok ? "OK" : "FAIL");
+
+ ggml_free(ctx);
+ ggml_backend_sched_free(sched);
+ ggml_backend_buffer_free(m.buf_w);
+ ggml_backend_buffer_free(m.buf_c);
+ ggml_free(m.ctx_w);
+ ggml_free(ctx_c);
+ ggml_backend_free(gpu);
+ ggml_backend_free(cpu);
+ return ok;
+}
+
+int main() {
+int main(int argc, char ** argv) {
+ const bool measured = argc > 1 && std::string(argv[1]) == "--measured";
+ // below and above this, CPU-to-device copies take the compute stream and the transfer queue
+ if (!measured && !getenv("GGML_VK_COMPUTE_STREAM_COPY_MAX")) {
+#ifdef _WIN32
+ _putenv_s("GGML_VK_COMPUTE_STREAM_COPY_MAX", "65536");
+#else
+ setenv("GGML_VK_COMPUTE_STREAM_COPY_MAX", "65536", 0);
+#endif
+ }
+ ggml_backend_load_all();
+
+ int tested = 0;
@@ -672,8 +843,10 @@ index 0000000..c04d99a
+ if (ggml_backend_dev_type(dev) != GGML_BACKEND_DEVICE_TYPE_GPU) {
+ continue;
+ }
+ for (bool host_buffer : { false, true }) {
+ ok = run(dev, host_buffer) && ok;
+ for (int64_t n : { (int64_t) 4096, (int64_t) 1 << 20 }) {
+ for (bool host_buffer : { false, true }) {
+ ok = run(dev, host_buffer, n) && ok;
+ }
+ }
+ tested++;
+ }
+2 -2
View File
@@ -162,7 +162,7 @@
"patches": [
{
"patch": "patches/llama.cpp/sched-staged-inputs.patch",
"sha256": "29aefc57926d097399d77b1a2624ebc49890703ec7ac930dff2d4fc9b4542262",
"sha256": "e2c8aca86ce3395c905f2457441c1798259a4c5b51b982e8e886b285b7342307",
"files": [
"ggml/src/ggml-backend-impl.h",
"ggml/src/ggml-backend-meta.cpp",
@@ -187,7 +187,7 @@
"tests/CMakeLists.txt",
"tests/test-backend-sched-inputs.cpp"
],
"reason": "Every request to the GPU is a round trip to the driver process (about 100 us for a trivial request on the R9700), so Vulkan's per-token synchronous work dominates decode. The scheduler wrote each graph input with a blocking set_tensor (a queue submission, a fence wait and a reset on a device without host-visible VRAM, as on Apple silicon), and ggml_vk_synchronize signalled its fence with an empty submission, which RADV turns into eight syncobj requests when it also waits for the transfer queue. Inputs are now staged in host memory and copied in the compute stream (a new optional backend function the scheduler uses for user inputs), the fence goes with the last compute submission, transfer-only work is waited for on the host, and small host copies stay in the compute stream. test-backend-sched-inputs covers input capture and ordering."
"reason": "Every request to the GPU is a round trip to the driver process (about 100 us for a trivial request on the R9700), so Vulkan's per-token synchronous work dominates decode. The scheduler wrote each graph input with a blocking set_tensor (a queue submission, a fence wait and a reset on a device without host-visible VRAM, as on Apple silicon), and ggml_vk_synchronize signalled its fence with an empty submission, which RADV turns into eight syncobj requests when it also waits for the transfer queue. Inputs are now staged in host memory and copied in the compute stream (a new optional backend function the scheduler uses for user inputs), the fence goes with the last compute submission, transfer-only work is waited for on the host, and a host copy uses the transfer queue only above a per-device size measured on first use (submission overhead times host-to-device bandwidth). test-backend-sched-inputs covers input capture and ordering."
}
]
}
+6 -3
View File
@@ -110,9 +110,12 @@ once Mesa is checked out. `lib-dext` verifies only the Linux tree
RADV turns into eight syncobj requests when it also waits for the transfer
queue. Inputs are now staged in host memory and copied in the compute
stream through a new optional backend function, the fence goes with the
last compute submission, and small host copies stay in the compute stream.
`test-backend-sched-inputs` checks that inputs are taken when the graph is
queued and ordered against the device's queued work.
last compute submission, and a host copy goes on the transfer queue only
above a size measured per device on first use: the extra submission's cost
times the host-to-device bandwidth (on the R9700 here about 150 us and
1.2 GB/s, so about 180 KB). `test-backend-sched-inputs` checks that inputs
are taken when the graph is queued, ordered against the device's queued
work, and that CPU results reach the device on both sides of that size.
## VRAM and the CPU on Apple silicon