Commit 3da11de1f for llama.cpp
commit 3da11de1f1dde90c5e387b55683c7137a3ad4916
Author: Aaron Teo <aaron.teo1@ibm.com>
Date: Sat Nov 21 19:05:23 2026 +0800
ggml-zdnn: fix ztensor load race condition (#32279)
Signed-off-by: Aaron Teo <aaron.teo1@ibm.com>
diff --git a/ggml/src/ggml-zdnn/common.hpp b/ggml/src/ggml-zdnn/common.hpp
index e28e7608b..3ab3b4b27 100644
--- a/ggml/src/ggml-zdnn/common.hpp
+++ b/ggml/src/ggml-zdnn/common.hpp
@@ -13,6 +13,7 @@
#include <future>
#include <map>
#include <memory>
+#include <mutex>
#include <utility>
#include <vector>
@@ -50,6 +51,8 @@ struct ggml_backend_zdnn_buffer {
std::vector<float> scales; // per-row scales for quantized weights
std::vector<float> channel_scales; // per-input-channel scales for quantized weights, applied to the inputs
+ std::mutex mutex;
+
char name[GGML_MAX_NAME];
};
diff --git a/ggml/src/ggml-zdnn/mmf.cpp b/ggml/src/ggml-zdnn/mmf.cpp
index acd443e5b..0efb4821e 100644
--- a/ggml/src/ggml-zdnn/mmf.cpp
+++ b/ggml/src/ggml-zdnn/mmf.cpp
@@ -61,9 +61,7 @@ void ggml_zdnn_mul_mat_f(
GGML_ASSERT(weights_extra->pre_tfm_desc.dim2 == weights->ne[1] && "weights_extra->pre_tfm_desc.dim2 must match weights->ne[1]");
// weights read straight into a host buffer skip set_tensor, so transform them on first use
- if (!weights_extra->ztensor.is_transformed) {
- ggml_zdnn_load_tensor(weights_extra->ztensor, weights->data);
- }
+ ggml_zdnn_load_weights(weights_extra, weights);
// the inputs can be in any host buffer or be a view, so transform them from their data on every call
{
diff --git a/ggml/src/ggml-zdnn/mmq.cpp b/ggml/src/ggml-zdnn/mmq.cpp
index 285479b41..50acddb25 100644
--- a/ggml/src/ggml-zdnn/mmq.cpp
+++ b/ggml/src/ggml-zdnn/mmq.cpp
@@ -190,9 +190,7 @@ void ggml_zdnn_mul_mat_q(
ggml_backend_zdnn_buffer * weights_extra = (ggml_backend_zdnn_buffer *)weights->extra;
// weights read straight into a host buffer skip set_tensor, so transform them on first use
- if (!weights_extra->ztensor.is_transformed) {
- ggml_zdnn_load_tensor_q(weights_extra, weights);
- }
+ ggml_zdnn_load_weights(weights_extra, weights);
// starting threads costs more than it saves for small inputs, e.g. during token generation
const int32_t n_threads = ne10*ne11 >= 65536 ? ctx->n_threads : 1;
diff --git a/ggml/src/ggml-zdnn/utils.cpp b/ggml/src/ggml-zdnn/utils.cpp
index 18e9879d0..d59ed4001 100644
--- a/ggml/src/ggml-zdnn/utils.cpp
+++ b/ggml/src/ggml-zdnn/utils.cpp
@@ -144,6 +144,20 @@ void ggml_zdnn_load_tensor_q(ggml_backend_zdnn_buffer * buffer, const ggml_tenso
ZDNN_CHECK(zdnn_transform_quantized_ztensor(&buffer->ztensor, false, -127, 127, q.data()));
}
+void ggml_zdnn_load_weights(ggml_backend_zdnn_buffer * buffer, const ggml_tensor * tensor) {
+ std::lock_guard<std::mutex> lock(buffer->mutex);
+
+ if (buffer->ztensor.is_transformed) {
+ return;
+ }
+
+ if (ggml_is_quantized(tensor->type)) {
+ ggml_zdnn_load_tensor_q(buffer, tensor);
+ } else {
+ ggml_zdnn_load_tensor(buffer->ztensor, tensor->data);
+ }
+}
+
void ggml_zdnn_init_tensor(ggml_backend_zdnn_buffer * buffer, const ggml_tensor * tensor) {
if (ggml_is_quantized(tensor->type)) {
GGML_ASSERT(ggml_is_matrix(tensor));
diff --git a/ggml/src/ggml-zdnn/utils.hpp b/ggml/src/ggml-zdnn/utils.hpp
index 20f5be5b4..f610c216f 100644
--- a/ggml/src/ggml-zdnn/utils.hpp
+++ b/ggml/src/ggml-zdnn/utils.hpp
@@ -18,6 +18,8 @@ int64_t ggml_zdnn_q_group_size(const ggml_tensor * tensor);
void ggml_zdnn_load_tensor_q(ggml_backend_zdnn_buffer * buffer, const ggml_tensor * tensor);
+void ggml_zdnn_load_weights(ggml_backend_zdnn_buffer * buffer, const ggml_tensor * tensor);
+
void ggml_zdnn_init_tensor(ggml_backend_zdnn_buffer * buffer, const ggml_tensor * tensor);
#endif // GGML_ZDNN_UTILITIES_HPP