Commit b05204927 for llama.cpp
commit b052049276778d47eb216739c9e1537d764c5585
Author: Alexey Kopytko <alexey@kopytko.com>
Date: Sun Nov 22 10:11:39 2026 +0900
vulkan : fix memset for unaligned ranges for non-UMA devices (#32725)
See https://github.com/ggml-org/llama.cpp/pull/32574
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan-buffers.cpp b/ggml/src/ggml-vulkan/ggml-vulkan-buffers.cpp
index ed814b59f..778d012bc 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan-buffers.cpp
+++ b/ggml/src/ggml-vulkan/ggml-vulkan-buffers.cpp
@@ -809,9 +809,25 @@ void ggml_vk_buffer_memset(vk_buffer& dst, size_t offset, uint32_t c, size_t siz
return;
}
+ // vkCmdFillBuffer requires a 4-byte aligned offset and size, so write the unaligned ends from the matching bytes of c
+ const size_t end = offset + size;
+ const size_t begin_aligned = std::min(GGML_PAD(offset, 4), end);
+ const size_t end_aligned = std::max(end - end % 4, begin_aligned);
+
+ const uint8_t * c_bytes = (const uint8_t *) &c;
+ if (begin_aligned > offset) {
+ ggml_vk_buffer_write(dst, offset, c_bytes + offset % 4, begin_aligned - offset);
+ }
+ if (end > end_aligned) {
+ ggml_vk_buffer_write(dst, end_aligned, c_bytes, end - end_aligned);
+ }
+ if (end_aligned == begin_aligned) {
+ return;
+ }
+
std::lock_guard<std::recursive_mutex> guard(dst->device->mutex);
vk_context subctx = ggml_vk_begin_sync_ctx(dst->device, dst->device->transfer_queue->cmd_pool);
- subctx->s->buffer->buf.fillBuffer(dst->buffer, offset, size, c);
+ subctx->s->buffer->buf.fillBuffer(dst->buffer, begin_aligned, end_aligned - begin_aligned, c);
ggml_vk_submit_sync(dst->device, subctx);
}
diff --git a/tests/test-backend-tensor.cpp b/tests/test-backend-tensor.cpp
index ed617939d..213275f62 100644
--- a/tests/test-backend-tensor.cpp
+++ b/tests/test-backend-tensor.cpp
@@ -170,6 +170,25 @@ static bool test_tensor_n(ggml_backend_buffer_type_t buft) {
}
}
+ {
+ uint8_t expected[24], out[24] = {};
+ memset(expected, 0x11, sizeof(expected));
+ ggml_backend_tensor_set(t_big, expected, 0, sizeof(expected));
+
+ // Verify memset of unaligned ranges, such as single F16 elements.
+ const size_t ranges[][2] = { { 2, 2 }, { 1, 1 }, { 5, 2 }, { 9, 13 }, { 22, 2 } };
+ for (const auto & range : ranges) {
+ const uint8_t value = (uint8_t) (0xA0 + range[0]);
+ memset(expected + range[0], value, range[1]);
+ ggml_backend_tensor_memset(t_big, value, range[0], range[1]);
+ }
+
+ ggml_backend_tensor_get(t_big, out, 0, sizeof(out));
+ if (memcmp(out, expected, sizeof(out)) != 0) {
+ return false;
+ }
+ }
+
{
std::vector<ggml_tensor *> batch;
ggml_backend_buffer_ptr scatter_buf = alloc_tensors(ctx.get(), buft, { 64 }, batch);