From a8e14e9f841b883232bf464de006b6a0892b98f7 Mon Sep 17 00:00:00 2001 From: bong-water-water-bong Date: Sat, 14 Nov 2026 07:33:47 -0400 Subject: [PATCH] hrx: restore #357's async host-buffer fallback and misaligned-pointer refusal that #371 removed (engine#2393) #371 deleted both halves of #357 while adding the imported buffer type: - backend_set/get_tensor_async again dropped data on a mapped (retagged CPU) buffer with "invalid HRX tensor upload/download"; - model_buffer_from_mmap again aborted in ggml_backend_cpu_buffer_from_ptr's GGML_ASSERT on a null or misaligned pointer, so mapped_host_ptr aborts. model_buffer_from_mmap still returns the retagged CPU buffer after #371, so both are needed. Restored as #357 had them, plus the ggml-impl.h include the refusal uses. Co-Authored-By: Claude Opus 5.5 --- ggml/src/ggml-hrx/ggml-hrx.cpp | 14 ++++++++++++++ ggml/src/ggml-hrx/model-buffer-from-mmap.cpp | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/ggml/src/ggml-hrx/ggml-hrx.cpp b/ggml/src/ggml-hrx/ggml-hrx.cpp index 631329bd2f27..88a1145fb945 100644 --- a/ggml/src/ggml-hrx/ggml-hrx.cpp +++ b/ggml/src/ggml-hrx/ggml-hrx.cpp @@ -789,6 +789,13 @@ static void backend_set_tensor_async(ggml_backend_t backend, auto * backend_context = static_cast(backend->context); ggml_backend_hrx_buffer_context * context = nullptr; size_t tensor_base = 0; + ggml_backend_buffer_t buffer = tensor->view_src != nullptr ? tensor->view_src->buffer : tensor->buffer; + if (buffer != nullptr && buffer->iface.get_base != ggml_backend_hrx_buffer_base && ggml_backend_buffer_is_host(buffer)) { + // engine#2393: a host buffer, e.g. the mapped weight buffer of model_buffer_from_mmap + backend_synchronize(backend); + ggml_backend_tensor_set(tensor, data, offset, size); + return; + } if (!ggml_backend_hrx_tensor_binding(tensor, &context, &tensor_base) || offset > ggml_nbytes(tensor) || size > ggml_nbytes(tensor) - offset) { GGML_LOG_ERROR("%s: invalid HRX tensor upload\n", __func__); @@ -819,6 +826,13 @@ static void backend_get_tensor_async(ggml_backend_t backend, auto * backend_context = static_cast(backend->context); ggml_backend_hrx_buffer_context * context = nullptr; size_t tensor_base = 0; + ggml_backend_buffer_t buffer = tensor->view_src != nullptr ? tensor->view_src->buffer : tensor->buffer; + if (buffer != nullptr && buffer->iface.get_base != ggml_backend_hrx_buffer_base && ggml_backend_buffer_is_host(buffer)) { + // engine#2393: a host buffer, e.g. the mapped weight buffer of model_buffer_from_mmap + backend_synchronize(backend); + ggml_backend_tensor_get(tensor, data, offset, size); + return; + } if (!ggml_backend_hrx_tensor_binding(tensor, &context, &tensor_base) || offset > ggml_nbytes(tensor) || size > ggml_nbytes(tensor) - offset) { GGML_LOG_ERROR("%s: invalid HRX tensor download\n", __func__); diff --git a/ggml/src/ggml-hrx/model-buffer-from-mmap.cpp b/ggml/src/ggml-hrx/model-buffer-from-mmap.cpp index 42341865b08d..80207a309299 100644 --- a/ggml/src/ggml-hrx/model-buffer-from-mmap.cpp +++ b/ggml/src/ggml-hrx/model-buffer-from-mmap.cpp @@ -17,6 +17,7 @@ #include "backend-context.h" #include "ggml-backend-impl.h" +#include "ggml-impl.h" #include "runtime/host-memory.h" #include @@ -147,6 +148,11 @@ ggml_backend_buffer_t model_buffer_from_mmap(ggml_backend_hrx_device_context * d // packs once, and it keeps the retagged CPU from-pointer buffer: making it an imported handle (borrowed buffer, no // pool charge) broke real model loading -- verified by a control run on an unmodified tree that generated tokens // while this tree crashed in cli_server::wait_ready(). The import path is device_buffer_from_host_ptr's own branch. + if (ptr == nullptr || size == 0 || reinterpret_cast(ptr) % TENSOR_ALIGNMENT != 0) { + GGML_LOG_ERROR("%s: refusing host pointer %p (size %zu): it must be non-null and %d-byte aligned\n", __func__, + ptr, size, TENSOR_ALIGNMENT); + return nullptr; + } ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size); if (buffer != nullptr) { buffer->buft = &device->mapped_buft;