diff --git a/ggml/src/ggml-vulkan/ggml-vulkan.cpp b/ggml/src/ggml-vulkan/ggml-vulkan.cpp index efb644793..60cebe494 100644 --- a/ggml/src/ggml-vulkan/ggml-vulkan.cpp +++ b/ggml/src/ggml-vulkan/ggml-vulkan.cpp @@ -14279,7 +14279,9 @@ bool ggml_backend_vk_buffer_cpy_tensor_n(ggml_backend_buffer_t buffer, const ggm void ggml_backend_vk_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) { ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context; - ggml_vk_buffer_memset(ctx->dev_buffer, 0, value, buffer->size); + // set all bytes of a u32 to the same 1 byte value + uint32_t val32 = (uint32_t)value * 0x01010101; + ggml_vk_buffer_memset(ctx->dev_buffer, 0, val32, buffer->size); } const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft) { diff --git a/ggml/src/ggml-webgpu/ggml-webgpu.cpp b/ggml/src/ggml-webgpu/ggml-webgpu.cpp index 3fd8a6672..886760849 100644 --- a/ggml/src/ggml-webgpu/ggml-webgpu.cpp +++ b/ggml/src/ggml-webgpu/ggml-webgpu.cpp @@ -4066,7 +4066,7 @@ static void ggml_backend_webgpu_buffer_memset_tensor(ggml_backend_buffer_t buffe size_t total_offset = ggml_webgpu_tensor_offset(tensor) + offset; - // This is a trick to set all bytes of a u32 to the same 1 byte value. + // set all bytes of a u32 to the same 1 byte value uint32_t val32 = (uint32_t) value * 0x01010101; ggml_backend_webgpu_buffer_memset(buf_ctx->global_ctx, buf_ctx->buffer, val32, total_offset, size); WEBGPU_CPU_PROFILE_TOTAL_END(memset_tensor, buf_ctx->global_ctx); @@ -4406,7 +4406,10 @@ static void ggml_backend_webgpu_buffer_clear(ggml_backend_buffer_t buffer, uint8 WEBGPU_LOG_DEBUG("ggml_backend_webgpu_buffer_clear(" << buffer << ", " << (uint32_t) value << ")"); WEBGPU_CPU_PROFILE_TOTAL_START(clear); ggml_backend_webgpu_buffer_context * buf_ctx = (ggml_backend_webgpu_buffer_context *) buffer->context; - ggml_backend_webgpu_buffer_memset(buf_ctx->global_ctx, buf_ctx->buffer, value, 0, buffer->size); + + // set all bytes of a u32 to the same 1 byte value + uint32_t val32 = (uint32_t) value * 0x01010101; + ggml_backend_webgpu_buffer_memset(buf_ctx->global_ctx, buf_ctx->buffer, val32, 0, buffer->size); WEBGPU_CPU_PROFILE_TOTAL_END(clear, buf_ctx->global_ctx); } diff --git a/tests/test-backend-tensor.cpp b/tests/test-backend-tensor.cpp index 213275f62..84c4dc2bd 100644 --- a/tests/test-backend-tensor.cpp +++ b/tests/test-backend-tensor.cpp @@ -223,6 +223,19 @@ static bool test_tensor_n(ggml_backend_buffer_type_t buft) { } } + { + uint8_t expected[24], out[24] = {}; + memset(expected, 0x5A, sizeof(expected)); + + // Verify that clear fills every byte with a nonzero value. + ggml_backend_buffer_clear(buf.get(), 0x5A); + + ggml_backend_tensor_get(t_big, out, 0, sizeof(out)); + if (memcmp(out, expected, sizeof(out)) != 0) { + return false; + } + } + return true; }