webgpu : avoid crash when offset is not multiple of 4 in WebGPU ggml_backend_tensor_get() implementation (#28045)

* webgpu : avoid crash when offset is not multiple of 4 in WebGPU ggml_backend_tensor_get() implementation

* chore : improve code readability

Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@huggingface.co>

---------

Co-authored-by: Stanisław Szymczyk <sszymczy@gmail.com>
Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@huggingface.co>
This commit is contained in:
fairydreaming
2026-08-31 16:04:38 +02:00
committed by GitHub
parent f8dbcd6189
commit 8e53fcefd2
+11 -4
View File
@@ -3713,11 +3713,18 @@ static void ggml_backend_webgpu_buffer_get_tensor(ggml_backend_buffer_t buffer,
size_t total_offset = ggml_webgpu_tensor_offset(tensor) + offset;
size_t final_size = size;
if (size % 4 != 0) {
size_t local_offset = total_offset % 4;
if (local_offset != 0) {
// If offset is not a multiple of 4, we need to round it down to the previous
// multiple of 4
total_offset -= local_offset;
}
size_t final_size = size + local_offset;
if (final_size % 4 != 0) {
// If size is not a multiple of 4, we need to round it up to the next
// multiple of 4
final_size = size + (4 - (size % 4));
final_size += 4 - (final_size % 4);
}
std::lock_guard<std::recursive_mutex> lock(buf_ctx->global_ctx->mutex);
@@ -3748,7 +3755,7 @@ static void ggml_backend_webgpu_buffer_get_tensor(ggml_backend_buffer_t buffer,
const void * mapped_range = buf_ctx->global_ctx->get_tensor_staging_buf.GetConstMappedRange(0, final_size);
// Copy the data from the mapped range to the output buffer
std::memcpy(data, mapped_range, size);
std::memcpy(data, (const void *) ((const char *) mapped_range + local_offset), size);
buf_ctx->global_ctx->get_tensor_staging_buf.Unmap();
WEBGPU_CPU_PROFILE_TOTAL_END(get_tensor, buf_ctx->global_ctx);
}