diff --git a/ggml/src/ggml-hrx/ggml-hrx.cpp b/ggml/src/ggml-hrx/ggml-hrx.cpp index 88a1145fb945..862dee90ebfc 100644 --- a/ggml/src/ggml-hrx/ggml-hrx.cpp +++ b/ggml/src/ggml-hrx/ggml-hrx.cpp @@ -1928,6 +1928,27 @@ ggml_backend_buffer_type_t ggml_backend_hrx_buffer_type(size_t device) { nullptr; } +ggml_backend_buffer_t hrx_mapped_buffer_from_ptr(ggml_backend_hrx_device_context * device, void * ptr, size_t size) { + // engine #2393: a mapped weight is host memory the loader owns, presented as a window -- the same shape as an + // import with two fields different: buft is mapped_buft (host-visible on purpose, the scheduler's placement for a + // mapped weight depends on it) and there is no parent allocation to inherit from. imported = true is the honest + // ownership flag: nothing here was allocated by the backend, so buffer_free must neither release a handle (there + // is none) nor credit the pool. + 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; + } + const uint64_t generation = g_allocation_generation.fetch_add(1); + auto * context = new (std::nothrow) ggml_backend_hrx_buffer_context{ + device, nullptr, static_cast(ptr), generation, generation, false, true, 0, 0, nullptr, + }; + if (context == nullptr) { + return nullptr; + } + return ggml_backend_buffer_init(&device->mapped_buft, buffer_i, context, size); +} + ggml_backend_buffer_t hrx_imported_buffer_from_ptr(ggml_backend_hrx_device_context * device, void * ptr, size_t size) { // engine #2393: a zero-copy import is an HRX buffer whose storage is the caller's, not a retagged CPU buffer -- // retagging made ggml_backend_hrx_buffer_context_from_buffer() read a CPU context as an HRX one, so diff --git a/ggml/src/ggml-hrx/model-buffer-from-mmap.cpp b/ggml/src/ggml-hrx/model-buffer-from-mmap.cpp index 80207a309299..6ba4d183dfc9 100644 --- a/ggml/src/ggml-hrx/model-buffer-from-mmap.cpp +++ b/ggml/src/ggml-hrx/model-buffer-from-mmap.cpp @@ -153,9 +153,11 @@ ggml_backend_buffer_t model_buffer_from_mmap(ggml_backend_hrx_device_context * d ptr, size, TENSOR_ALIGNMENT); return nullptr; } - ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size); + // engine #2393: a real HRX window, not a retagged CPU buffer -- retagging left the context a CPU context, so + // every ggml_backend_hrx_buffer_context reader (and backend_set_tensor_async's choice of write path) read + // unrelated memory. + ggml_backend_buffer_t buffer = ::hrx_mapped_buffer_from_ptr(device, ptr, size); if (buffer != nullptr) { - buffer->buft = &device->mapped_buft; // engine #2137: entries from a mapping that was here before describe other bytes forget_stored_device_copies(ptr, size); } diff --git a/ggml/src/ggml-hrx/model-buffer-from-mmap.h b/ggml/src/ggml-hrx/model-buffer-from-mmap.h index 8987ad50f92a..d2121a3ee293 100644 --- a/ggml/src/ggml-hrx/model-buffer-from-mmap.h +++ b/ggml/src/ggml-hrx/model-buffer-from-mmap.h @@ -25,6 +25,8 @@ struct ggml_backend_hrx_device_context; // Used by model_buffer_from_mmap instead of retagging a CPU buffer, which made the HRX buffer-context accessor // read a CPU context. Global scope on purpose: the definition sits outside the anonymous ggml::hrx namespace. ggml_backend_buffer_t hrx_imported_buffer_from_ptr(ggml_backend_hrx_device_context * device, void * ptr, size_t size); +// engine #2393: the same window shape for a mapped weight -- mapped_buft, no parent to inherit. +ggml_backend_buffer_t hrx_mapped_buffer_from_ptr(ggml_backend_hrx_device_context * device, void * ptr, size_t size); namespace ggml::hrx {