interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-vulkan / ggml-vulkan-comm.cpp

591 lines · 23043 bytes Raw Blame History
1#include "ggml-vulkan-common.h"
2
3#if !defined(_WIN32)
4#include <fcntl.h>
5#include <unistd.h>
6#if defined(__linux__)
7#include <pthread.h>
8#endif
9
10static constexpr size_t ggml_vk_comm_f16_min_size = 1024 * 1024;
11
12// follows ops ggml-backend-meta may delay a reduction across
13// TODO: the required precision should come from the meta backend instead.
14static bool ggml_vk_comm_needs_f32(const ggml_tensor * t, int depth = 0) {
15 if (t == nullptr || depth > 64) {
16 return false;
17 }
18 switch (t->op) {
19 case GGML_OP_MUL_MAT:
20 case GGML_OP_MUL_MAT_ID:
21 return ggml_get_op_params_i32(t, 0) == GGML_PREC_F32;
22 case GGML_OP_ADD:
23 return ggml_vk_comm_needs_f32(t->src[0], depth + 1) || ggml_vk_comm_needs_f32(t->src[1], depth + 1);
24 case GGML_OP_MUL:
25 case GGML_OP_ADD_ID:
26 return ggml_vk_comm_needs_f32(t->src[0], depth + 1);
27 case GGML_OP_VIEW:
28 return ggml_vk_comm_needs_f32(t->src[0], depth + 1);
29 default:
30 return false;
31 }
32}
33
34// host_bridge: device i signals done[i], a host thread signals go[i] once all peers of i are done
35// sync_fd: a host thread exchanges SYNC_FD semaphores between peers, the wait resolves in the kernel
36enum class vk_comm_sync {
37 host_bridge,
38 sync_fd,
39};
40
41struct vk_comm_device {
42 ggml_backend_t backend = nullptr;
43 ggml_backend_vk_context * ctx = nullptr;
44 void * host_ptr = nullptr;
45 std::vector<ggml_backend_buffer_t> host_buf; // host_ptr imported into each peer
46
47 struct {
48 PFN_vkGetSemaphoreFdKHR get_fd = nullptr;
49 PFN_vkImportSemaphoreFdKHR import_fd = nullptr;
50 vk::Semaphore sem;
51 std::vector<vk::Semaphore> imp; // [peer], pooled, reused only after ggml_vk_graph_cleanup
52 } sfd;
53
54 struct {
55 vk::Semaphore done;
56 vk::Semaphore go;
57 } bridge;
58};
59
60struct ggml_backend_vk_comm_context {
61 std::vector<vk_comm_device> devs;
62 vk_comm_sync mode = vk_comm_sync::host_bridge;
63 size_t align = 4096;
64 size_t cap = 0;
65 uint64_t round = 0;
66 ggml_context * tctx = nullptr;
67 ggml_tensor * slot = nullptr;
68 bool disabled = false;
69
70 std::thread thread;
71 std::chrono::nanoseconds spin{ 200'000 };
72 std::atomic<uint64_t> submitted{ 0 };
73 std::atomic<uint64_t> imported{ 0 };
74 std::atomic<bool> sleeping{ false };
75 std::atomic<bool> stop{ false };
76 std::mutex mutex;
77 std::condition_variable cv;
78};
79
80static void ggml_vk_comm_free_buffers(ggml_backend_vk_comm_context * comm) {
81 for (vk_comm_device & d : comm->devs) {
82 for (ggml_backend_buffer_t & buf : d.host_buf) {
83 ggml_backend_buffer_free(buf);
84 buf = nullptr;
85 }
86 std::free(d.host_ptr);
87 d.host_ptr = nullptr;
88 }
89 comm->cap = 0;
90}
91
92static bool ggml_backend_vk_comm_ensure(ggml_backend_vk_comm_context * comm, size_t nbytes) {
93 if (comm->disabled) {
94 return false;
95 }
96 if (nbytes <= comm->cap) {
97 return true;
98 }
99 auto fail = [comm]() {
100 GGML_LOG_WARN("ggml_vulkan: shared host buffer allocation failed, using generic all-reduce\n");
101 comm->disabled = true;
102 return false;
103 };
104 for (vk_comm_device & d : comm->devs) {
105 ggml_backend_synchronize(d.backend);
106 }
107 ggml_vk_comm_free_buffers(comm);
108
109 const size_t n = comm->devs.size();
110 const size_t cap = GGML_PAD(nbytes, comm->align);
111 for (size_t i = 0; i < n; i++) {
112 vk_comm_device & d = comm->devs[i];
113 if (posix_memalign(&d.host_ptr, comm->align, 2 * cap) != 0) {
114 d.host_ptr = nullptr;
115 return fail();
116 }
117 for (size_t j = 0; j < n; j++) {
118 d.host_buf[j] = ggml_backend_vk_device_buffer_from_host_ptr(
119 ggml_backend_get_device(comm->devs[j].backend), d.host_ptr, 2 * cap, 2 * cap);
120 if (!d.host_buf[j]) {
121 return fail();
122 }
123 }
124 }
125 comm->cap = cap;
126 return true;
127}
128
129static void ggml_vk_comm_set_slot(ggml_tensor * slot, const ggml_tensor * src, ggml_type type, ggml_backend_buffer_t buf, size_t offs) {
130 slot->type = type;
131 slot->nb[0] = ggml_type_size(type);
132 for (int d = 0; d < GGML_MAX_DIMS; d++) {
133 slot->ne[d] = src->ne[d];
134 if (d > 0) {
135 slot->nb[d] = slot->nb[d - 1] * slot->ne[d - 1];
136 }
137 }
138 slot->buffer = buf;
139 slot->data = (char *) ggml_backend_buffer_get_base(buf) + offs;
140}
141
142static void ggml_vk_comm_submit(ggml_backend_vk_context * ctx, vk_context & c, vk::Semaphore signal, uint64_t value) {
143 ggml_vk_ctx_end(c);
144 for (auto & cpy : c->in_memcpys) {
145 memcpy(cpy.dst, cpy.src, cpy.n);
146 }
147 for (auto & mset : c->memsets) {
148 memset(mset.dst, mset.val, mset.n);
149 }
150 c->in_memcpys.clear();
151 c->memsets.clear();
152 if (signal) {
153 c->seqs.back().back().signal_semaphores.push_back({ signal, value });
154 }
155 ggml_vk_submit(c, {});
156 ctx->submit_pending = true;
157 ctx->compute_ctx.reset();
158}
159
160static void ggml_vk_comm_notify_submitted(ggml_backend_vk_comm_context * comm, uint64_t round) {
161 comm->submitted.store(round);
162 if (comm->sleeping.load()) {
163 std::lock_guard<std::mutex> lock(comm->mutex);
164 comm->cv.notify_one();
165 }
166}
167
168// false on stop
169static bool ggml_vk_comm_wait_submitted(ggml_backend_vk_comm_context * comm, uint64_t r) {
170 using clock = std::chrono::steady_clock;
171 for (const auto t0 = clock::now(); comm->submitted.load() < r;) {
172 if (comm->stop.load()) {
173 return false;
174 }
175 if (clock::now() - t0 < comm->spin) {
176 YIELD();
177 continue;
178 }
179 comm->sleeping.store(true);
180 if (comm->submitted.load() < r) {
181 std::unique_lock<std::mutex> lock(comm->mutex);
182 comm->cv.wait(lock, [&] { return comm->submitted.load() >= r || comm->stop.load(); });
183 }
184 comm->sleeping.store(false);
185 }
186 return true;
187}
188
189// signals go[i] once all peers of i are done; false on stop
190static bool ggml_vk_comm_bridge_release(ggml_backend_vk_comm_context * comm, uint64_t r, std::vector<uint64_t> & seen, std::vector<bool> & sent) {
191 using clock = std::chrono::steady_clock;
192 const size_t n = comm->devs.size();
193 std::fill(sent.begin(), sent.end(), false);
194 size_t left = n;
195 for (const auto t0 = clock::now();;) {
196 size_t pending = n;
197 for (size_t i = 0; i < n; i++) {
198 if (sent[i]) {
199 continue;
200 }
201 bool ready = true;
202 for (size_t j = 0; j < n && ready; j++) {
203 if (j != i && seen[j] < r) {
204 const vk_comm_device & p = comm->devs[j];
205 seen[j] = p.ctx->device->device.getSemaphoreCounterValue(p.bridge.done);
206 ready = seen[j] >= r;
207 pending = ready ? pending : j;
208 }
209 }
210 if (ready) {
211 const vk_comm_device & d = comm->devs[i];
212 d.ctx->device->device.signalSemaphore({ d.bridge.go, r });
213 sent[i] = true;
214 left--;
215 }
216 }
217 if (left == 0) {
218 return true;
219 }
220 if (clock::now() - t0 < comm->spin) {
221 YIELD();
222 continue;
223 }
224 const vk_comm_device & p = comm->devs[pending];
225 const vk::SemaphoreWaitInfo wi{ {}, 1, &p.bridge.done, &r };
226 while (p.ctx->device->device.waitSemaphores(wi, 100'000'000) != vk::Result::eSuccess) {
227 if (comm->stop.load()) {
228 return false;
229 }
230 }
231 }
232}
233
234static void ggml_vk_comm_bridge_thread(ggml_backend_vk_comm_context * comm) {
235#if defined(__linux__)
236 pthread_setname_np(pthread_self(), "ggml-vk-comm");
237#endif
238 std::vector<uint64_t> seen(comm->devs.size(), 0);
239 std::vector<bool> sent(comm->devs.size());
240 try {
241 for (uint64_t r = 1;; r++) {
242 if (!ggml_vk_comm_wait_submitted(comm, r) || !ggml_vk_comm_bridge_release(comm, r, seen, sent)) {
243 return;
244 }
245 }
246 } catch (const vk::SystemError & e) {
247 GGML_ABORT("ggml_vulkan: all-reduce bridge failed: %s", e.what());
248 }
249}
250
251static void ggml_vk_comm_bridge_init(ggml_backend_vk_comm_context * comm) {
252 vk::SemaphoreTypeCreateInfo stci{ vk::SemaphoreType::eTimeline, 0 };
253 vk::SemaphoreCreateInfo sci{};
254 sci.pNext = &stci;
255 for (vk_comm_device & d : comm->devs) {
256 d.bridge.done = d.ctx->device->device.createSemaphore(sci);
257 d.bridge.go = d.ctx->device->device.createSemaphore(sci);
258 }
259 comm->thread = std::thread(ggml_vk_comm_bridge_thread, comm);
260}
261
262static void ggml_vk_comm_bridge_free(vk_comm_device & d) {
263 if (d.bridge.done) {
264 d.ctx->device->device.destroySemaphore(d.bridge.done);
265 }
266 if (d.bridge.go) {
267 d.ctx->device->device.destroySemaphore(d.bridge.go);
268 }
269}
270
271static void ggml_vk_comm_barrier_to_host(vk_context & c) {
272 const vk::MemoryBarrier to_host{ vk::AccessFlagBits::eTransferWrite | vk::AccessFlagBits::eShaderWrite, vk::AccessFlagBits::eHostRead };
273 c->s->buffer->buf.pipelineBarrier(c->p->q->stage_flags, vk::PipelineStageFlagBits::eHost, {}, { to_host }, {}, {});
274}
275
276// the peers' slots were made available to the host before the wait resolved, the submit predates that
277static void ggml_vk_comm_barrier_from_host(vk_context & c) {
278 const vk::MemoryBarrier from_host{ vk::AccessFlagBits::eHostWrite, vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eTransferRead };
279 c->s->buffer->buf.pipelineBarrier(vk::PipelineStageFlagBits::eHost, c->p->q->stage_flags, {}, { from_host }, {}, {});
280}
281
282static void ggml_vk_comm_bridge_signal(ggml_backend_vk_comm_context * comm, size_t i, vk_context & c, uint64_t round) {
283 const vk_comm_device & d = comm->devs[i];
284 ggml_vk_comm_barrier_to_host(c);
285 ggml_vk_comm_submit(d.ctx, c, d.bridge.done, round);
286}
287
288static void ggml_vk_comm_bridge_wait(ggml_backend_vk_comm_context * comm, size_t i, vk_context & c, uint64_t round) {
289 c->s->wait_semaphores.push_back({ comm->devs[i].bridge.go, round });
290 ggml_vk_comm_barrier_from_host(c);
291}
292
293static bool ggml_vk_comm_sfd_supported(vk_comm_device & d) {
294 const vk_device & dev = d.ctx->device;
295 const bool mesa = dev->driver_id == vk::DriverId::eMesaRadv ||
296 dev->driver_id == vk::DriverId::eIntelOpenSourceMESA ||
297 dev->driver_id == vk::DriverId::eMesaNvk;
298 if (!mesa || !dev->external_semaphore) {
299 return false;
300 }
301 d.sfd.get_fd = (PFN_vkGetSemaphoreFdKHR) dev->device.getProcAddr("vkGetSemaphoreFdKHR");
302 d.sfd.import_fd = (PFN_vkImportSemaphoreFdKHR) dev->device.getProcAddr("vkImportSemaphoreFdKHR");
303 if (!d.sfd.get_fd || !d.sfd.import_fd) {
304 return false;
305 }
306 vk::PhysicalDeviceExternalSemaphoreInfo esi{ vk::ExternalSemaphoreHandleTypeFlagBits::eSyncFd };
307 vk::ExternalSemaphoreProperties esp = dev->physical_device.getExternalSemaphoreProperties(esi);
308 const vk::ExternalSemaphoreFeatureFlags need =
309 vk::ExternalSemaphoreFeatureFlagBits::eExportable | vk::ExternalSemaphoreFeatureFlagBits::eImportable;
310 return (esp.externalSemaphoreFeatures & need) == need &&
311 (esp.compatibleHandleTypes & vk::ExternalSemaphoreHandleTypeFlagBits::eSyncFd);
312}
313
314// Mesa moves a queue to its submit thread on the first wait-before-signal, taking the CS ioctls off this thread
315static void ggml_vk_comm_sfd_enable_submit_thread(const vk_device & dev) {
316 vk::SemaphoreTypeCreateInfo stci{ vk::SemaphoreType::eTimeline, 0 };
317 vk::SemaphoreCreateInfo sci{};
318 sci.pNext = &stci;
319 const vk::Semaphore sem = dev->device.createSemaphore(sci);
320 const vk::Fence fence = dev->device.createFence({});
321 const uint64_t one = 1;
322 vk::TimelineSemaphoreSubmitInfo tsi{ 1, &one, 0, nullptr };
323 const vk::PipelineStageFlags stage = vk::PipelineStageFlagBits::eAllCommands;
324 vk::SubmitInfo si{ 1, &sem, &stage, 0, nullptr, 0, nullptr };
325 si.setPNext(&tsi);
326 dev->compute_queue->handle->submit(si, fence);
327 dev->device.signalSemaphore({ sem, 1 });
328 VK_CHECK(dev->device.waitForFences({ fence }, true, UINT64_MAX), "ggml_vk_comm_sfd_enable_submit_thread", dev);
329 dev->device.destroyFence(fence);
330 dev->device.destroySemaphore(sem);
331}
332
333// Requires a submitted signal on sem; the export resets it.
334static int ggml_vk_comm_sfd_export(vk_comm_device & d) {
335 int fd = -1;
336 VkSemaphoreGetFdInfoKHR gi{};
337 gi.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
338 gi.semaphore = d.sfd.sem;
339 gi.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
340 const VkResult result = d.sfd.get_fd((VkDevice) d.ctx->device->device, &gi, &fd);
341 if (result != VK_SUCCESS) {
342 GGML_ABORT("ggml_vulkan: vkGetSemaphoreFdKHR failed: %d", (int) result);
343 }
344 return fd;
345}
346
347// fd -1 imports an already signalled payload
348static void ggml_vk_comm_sfd_import(vk_comm_device & d, vk::Semaphore sem, int fd) {
349 VkImportSemaphoreFdInfoKHR isi{};
350 isi.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
351 isi.semaphore = sem;
352 isi.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
353 isi.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
354 isi.fd = fd;
355 const VkResult result = d.sfd.import_fd((VkDevice) d.ctx->device->device, &isi);
356 if (result != VK_SUCCESS) {
357 GGML_ABORT("ggml_vulkan: vkImportSemaphoreFdKHR failed: %d", (int) result);
358 }
359}
360
361static void ggml_vk_comm_sfd_exchange(ggml_backend_vk_comm_context * comm, uint64_t r) {
362 const size_t n = comm->devs.size();
363 for (size_t j = 0; j < n; j++) {
364 const int fd = ggml_vk_comm_sfd_export(comm->devs[j]);
365 const size_t last = j == n - 1 ? n - 2 : n - 1;
366 for (size_t i = 0; i < n; i++) {
367 if (i == j) {
368 continue;
369 }
370 const int imp_fd = fd < 0 || i == last ? fd : fcntl(fd, F_DUPFD_CLOEXEC, 0);
371 GGML_ASSERT(imp_fd >= 0 || fd < 0);
372 ggml_vk_comm_sfd_import(comm->devs[i], comm->devs[i].sfd.imp[j], imp_fd);
373 }
374 }
375 comm->imported.store(r, std::memory_order_release);
376}
377
378static void ggml_vk_comm_sfd_thread(ggml_backend_vk_comm_context * comm) {
379#if defined(__linux__)
380 pthread_setname_np(pthread_self(), "ggml-vk-comm");
381#endif
382 for (uint64_t r = 1; ggml_vk_comm_wait_submitted(comm, r); r++) {
383 ggml_vk_comm_sfd_exchange(comm, r);
384 }
385}
386
387static void ggml_vk_comm_sfd_init(ggml_backend_vk_comm_context * comm) {
388 vk::ExportSemaphoreCreateInfo esci{ vk::ExternalSemaphoreHandleTypeFlagBits::eSyncFd };
389 vk::SemaphoreCreateInfo sci{};
390 sci.pNext = &esci;
391 for (vk_comm_device & d : comm->devs) {
392 d.sfd.sem = d.ctx->device->device.createSemaphore(sci);
393 d.sfd.imp.assign(comm->devs.size(), {});
394 ggml_vk_comm_sfd_enable_submit_thread(d.ctx->device);
395 }
396 comm->thread = std::thread(ggml_vk_comm_sfd_thread, comm);
397}
398
399static void ggml_vk_comm_sfd_free(vk_comm_device & d) {
400 if (d.sfd.sem) {
401 d.ctx->device->device.destroySemaphore(d.sfd.sem);
402 }
403}
404
405static void ggml_vk_comm_sfd_signal(ggml_backend_vk_comm_context * comm, size_t i, vk_context & c) {
406 vk_comm_device & d = comm->devs[i];
407 ggml_vk_comm_barrier_to_host(c);
408 ggml_vk_comm_submit(d.ctx, c, d.sfd.sem, 0);
409 for (size_t j = 0; j < comm->devs.size(); j++) {
410 d.sfd.imp[j] = j == i ? vk::Semaphore{} : ggml_vk_create_binary_semaphore(d.ctx);
411 }
412}
413
414static void ggml_vk_comm_sfd_wait(ggml_backend_vk_comm_context * comm, size_t i, vk_context & c, uint64_t round) {
415 for (size_t j = 0; j < comm->devs.size(); j++) {
416 if (j != i) {
417 c->s->wait_semaphores.push_back({ comm->devs[i].sfd.imp[j], 0 });
418 }
419 }
420 c->s->ready = &comm->imported;
421 c->s->ready_value = round;
422 ggml_vk_comm_barrier_from_host(c);
423}
424
425void ggml_backend_vk_comm_free(void * comm_ctx) {
426 ggml_backend_vk_comm_context * comm = static_cast<ggml_backend_vk_comm_context *>(comm_ctx);
427 // pending reductions need the thread
428 for (vk_comm_device & d : comm->devs) {
429 ggml_backend_synchronize(d.backend);
430 }
431 if (comm->thread.joinable()) {
432 {
433 std::lock_guard<std::mutex> lock(comm->mutex);
434 comm->stop.store(true);
435 }
436 comm->cv.notify_all();
437 comm->thread.join();
438 }
439 for (vk_comm_device & d : comm->devs) {
440 ggml_vk_comm_sfd_free(d);
441 ggml_vk_comm_bridge_free(d);
442 }
443 ggml_vk_comm_free_buffers(comm);
444 if (comm->tctx) {
445 ggml_free(comm->tctx);
446 }
447 delete comm;
448}
449
450void * ggml_backend_vk_comm_init(ggml_backend_t * backends, size_t n_backends) {
451 if (vk_instance.allreduce_disabled || n_backends < 2) {
452 return nullptr;
453 }
454 for (size_t i = 0; i < n_backends; i++) {
455 if (!ggml_backend_is_vk(backends[i])) {
456 return nullptr;
457 }
458 }
459 ggml_backend_vk_comm_context * comm = new ggml_backend_vk_comm_context;
460 comm->devs.resize(n_backends);
461 for (size_t i = 0; i < n_backends; i++) {
462 vk_comm_device & d = comm->devs[i];
463 d.backend = backends[i];
464 d.ctx = (ggml_backend_vk_context *) backends[i]->context;
465 d.host_buf.assign(n_backends, nullptr);
466 }
467
468 for (size_t i = 0; i < n_backends; i++) {
469 const vk_device & dev = comm->devs[i].ctx->device;
470 comm->align = std::max(comm->align, (size_t) dev->min_imported_host_pointer_alignment);
471 if (!dev->external_memory_host) {
472 GGML_LOG_WARN("ggml_vulkan: device %zu: VK_EXT_external_memory_host missing, using generic all-reduce\n", i);
473 ggml_backend_vk_comm_free(comm);
474 return nullptr;
475 }
476 if (dev->serialize_submissions) {
477 GGML_LOG_WARN("ggml_vulkan: device %zu: GGML_VK_SERIALIZE_SUBMISSIONS set, using generic all-reduce\n", i);
478 ggml_backend_vk_comm_free(comm);
479 return nullptr;
480 }
481 }
482
483 if (std::all_of(comm->devs.begin(), comm->devs.end(), ggml_vk_comm_sfd_supported)) {
484 comm->mode = vk_comm_sync::sync_fd;
485 }
486
487 if (const char * spin = getenv("GGML_VK_ALLREDUCE_SPIN_US")) {
488 comm->spin = std::chrono::microseconds(std::max(0, atoi(spin)));
489 }
490 const ggml_init_params ip = { ggml_tensor_overhead(), nullptr, true };
491 comm->tctx = ggml_init(ip);
492 comm->slot = ggml_new_tensor_1d(comm->tctx, GGML_TYPE_F32, 1);
493
494 try {
495 if (comm->mode == vk_comm_sync::sync_fd) {
496 ggml_vk_comm_sfd_init(comm);
497 } else {
498 ggml_vk_comm_bridge_init(comm);
499 }
500 } catch (const vk::SystemError & e) {
501 GGML_LOG_WARN("ggml_vulkan: semaphore initialization failed: %s, using generic all-reduce\n", e.what());
502 ggml_backend_vk_comm_free(comm);
503 return nullptr;
504 }
505 return comm;
506}
507
508// A: partial to own host slot (round & 1), signal; B: wait on peers' A, add their slots; all A precede any B
509// slot reuse: A(k) follows own B(k-1), which waited on peers' A(k-1), submitted after their B(k-2), the last reader
510bool ggml_backend_vk_comm_allreduce_tensor(void * comm_ctx, ggml_tensor ** tensors) {
511 ggml_backend_vk_comm_context * comm = static_cast<ggml_backend_vk_comm_context *>(comm_ctx);
512 const size_t n = comm->devs.size();
513
514 const int64_t ne = ggml_nelements(tensors[0]);
515 for (size_t i = 0; i < n; i++) {
516 const ggml_tensor * t = tensors[i];
517 if (t->type != GGML_TYPE_F32 || ggml_nelements(t) != ne || !ggml_is_contiguous(t) || !ggml_backend_buffer_is_vk(t->buffer)) {
518 return false;
519 }
520 const ggml_backend_vk_context * ctx = comm->devs[i].ctx;
521 if (!ctx->compute_ctx.expired() && !ctx->compute_ctx.lock()->out_memcpys.empty()) {
522 return false;
523 }
524 }
525 if (ne == 0) {
526 return true;
527 }
528 const size_t nbytes = ggml_nbytes(tensors[0]);
529 if (!ggml_backend_vk_comm_ensure(comm, nbytes)) {
530 return false;
531 }
532
533 const bool f32_wire = nbytes < ggml_vk_comm_f16_min_size || ggml_vk_comm_needs_f32(tensors[0]);
534 const ggml_type wire = f32_wire ? GGML_TYPE_F32 : GGML_TYPE_F16;
535 const uint64_t round = ++comm->round;
536 const size_t slot_offs = (round & 1) * comm->cap;
537 const size_t slot_size = ne * ggml_type_size(wire);
538
539 for (size_t i = 0; i < n; i++) {
540 const vk_comm_device & d = comm->devs[i];
541 ggml_backend_vk_context * ctx = d.ctx;
542 ggml_tensor * t = tensors[i];
543 vk_buffer & tbuf = ((ggml_backend_vk_buffer_context *) t->buffer->context)->dev_buffer;
544 vk_buffer & hbuf = ((ggml_backend_vk_buffer_context *) d.host_buf[i]->context)->dev_buffer;
545 const size_t toffs = vk_tensor_offset(t) + t->view_offs;
546
547 vk_context c = ggml_vk_get_compute_ctx(ctx);
548 ggml_vk_sync_buffers(ctx, c);
549 if (!(t->flags & GGML_TENSOR_FLAG_COMPUTE)) {
550 c->s->buffer->buf.fillBuffer(hbuf->buffer, slot_offs, GGML_PAD(slot_size, 4), 0);
551 c->s->buffer->buf.fillBuffer(tbuf->buffer, toffs, nbytes, 0);
552 } else if (wire == GGML_TYPE_F32) {
553 ggml_vk_buffer_copy_async(c, hbuf, slot_offs, tbuf, toffs, nbytes);
554 } else {
555 ggml_vk_comm_set_slot(comm->slot, t, wire, d.host_buf[i], slot_offs);
556 ggml_vk_cpy(ctx, c, t, comm->slot);
557 }
558 if (comm->mode == vk_comm_sync::sync_fd) {
559 ggml_vk_comm_sfd_signal(comm, i, c);
560 } else {
561 ggml_vk_comm_bridge_signal(comm, i, c, round);
562 }
563 }
564 ggml_vk_comm_notify_submitted(comm, round);
565
566 for (size_t i = 0; i < n; i++) {
567 ggml_backend_vk_context * ctx = comm->devs[i].ctx;
568 vk_context c = ggml_vk_get_compute_ctx(ctx);
569 ctx->do_add_rms_partials = false;
570 if (comm->mode == vk_comm_sync::sync_fd) {
571 ggml_vk_comm_sfd_wait(comm, i, c, round);
572 } else {
573 ggml_vk_comm_bridge_wait(comm, i, c, round);
574 }
575 ggml_vk_sync_buffers(ctx, c);
576 for (size_t j = 0; j < n; j++) {
577 if (j == i) {
578 continue;
579 }
580 ggml_vk_comm_set_slot(comm->slot, tensors[i], wire, comm->devs[j].host_buf[i], slot_offs);
581 ggml_vk_add(ctx, c, tensors[i], comm->slot, tensors[i]);
582 ggml_vk_sync_buffers(ctx, c);
583 }
584 // small reductions go out with the next graph's first submit
585 if (wire != GGML_TYPE_F32 || vk_perf_logger_enabled) {
586 ggml_vk_comm_submit(ctx, c, {}, 0);
587 }
588 }
589 return true;
590}
591#endif