Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions ggml/src/ggml-hrx/ggml-hrx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uintptr_t>(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<uint8_t *>(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
Expand Down
6 changes: 4 additions & 2 deletions ggml/src/ggml-hrx/model-buffer-from-mmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions ggml/src/ggml-hrx/model-buffer-from-mmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down