From 7e74ec3311fb4fbf6fba600c9ffe9b6e2850a99a Mon Sep 17 00:00:00 2001 From: bong-water-water-bong Date: Sun, 15 Nov 2026 03:13:56 -0400 Subject: [PATCH] hrx: the loader's file mapping is a mapped weight again, not an import (zaya1-8b failed to load since #371, engine #2500) Since #371 (engine #2393) device_buffer_from_host_ptr returns an imported, non-host buffer, and it is the generic entry the loader uses for every mmap-backed weight range. So every model weight became an imported buffer. A weight view an HRX matcher declines then has no backend: ZAYA's grouped CCA conv (a strided F16 ggml_view_3d of blk.N.cca_conv_grp.weight into a batched MUL_MAT) aborted at load with "pre-allocated tensor ... in a buffer () that cannot run the operation (VIEW)". HRX now exposes the mapped path by name (registry proc "ggml_backend_hrx_model_buffer_from_mmap" -> model_buffer_from_mmap), and the loader uses it for HRX devices. Other callers of buffer_from_host_ptr keep the import semantics #2393 needs. The imported buffer type also gets its context (name HRX0_Imported, device); it had none, hence the empty "()" in the abort. Test: hrx-buffer-test loader_mmap_proc (fails without the change). Co-Authored-By: Claude Opus 5.5 --- ggml/src/ggml-hrx/ggml-hrx.cpp | 14 +++++++++ ggml/src/ggml-hrx/model-buffer-from-mmap.cpp | 3 ++ src/llama-model.cpp | 11 ++++++- tests/hrx-buffer-test.cpp | 33 ++++++++++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/ggml/src/ggml-hrx/ggml-hrx.cpp b/ggml/src/ggml-hrx/ggml-hrx.cpp index f68291dffac5..5835d7787872 100644 --- a/ggml/src/ggml-hrx/ggml-hrx.cpp +++ b/ggml/src/ggml-hrx/ggml-hrx.cpp @@ -1804,10 +1804,24 @@ static bool ggml_backend_hrx_buffer_export_dmabuf(ggml_backend_buffer_t buffer, ggml::hrx::export_dmabuf(device_ptr, buffer->size, fd, offset); } +// engine #2500: the loader's file mapping is a MAPPED weight (host memory the runtime packs once, model_buffer_from_mmap), +// not an import. Since #371, device_buffer_from_host_ptr is the import path, and it is the generic entry the loader used, +// so every mmap-backed weight became a non-host imported buffer: a weight view an HRX matcher declines (ZAYA's grouped +// CCA conv) then had no backend at all ("pre-allocated tensor ... in a buffer that cannot run the operation"). +// The loader asks for this by name for HRX devices (src/llama-model.cpp); other callers keep the import semantics. +static ggml_backend_buffer_t ggml_backend_hrx_model_buffer_from_mmap(ggml_backend_dev_t device, void * ptr, size_t size, + size_t max_tensor_size) { + GGML_UNUSED(max_tensor_size); + return ggml::hrx::model_buffer_from_mmap(device_context(device), ptr, size); +} + static void * registry_proc(ggml_backend_reg_t registry, const char * name) { if (std::strcmp(name, "ggml_backend_buffer_export_dmabuf") == 0) { return (void *) ggml_backend_hrx_buffer_export_dmabuf; } + if (std::strcmp(name, "ggml_backend_hrx_model_buffer_from_mmap") == 0) { + return (void *) ggml_backend_hrx_model_buffer_from_mmap; + } GGML_UNUSED(registry); GGML_UNUSED(name); return nullptr; diff --git a/ggml/src/ggml-hrx/model-buffer-from-mmap.cpp b/ggml/src/ggml-hrx/model-buffer-from-mmap.cpp index 6ba4d183dfc9..b0b458f80f21 100644 --- a/ggml/src/ggml-hrx/model-buffer-from-mmap.cpp +++ b/ggml/src/ggml-hrx/model-buffer-from-mmap.cpp @@ -125,6 +125,9 @@ bool buffer_is_file_mapped(ggml_backend_buffer_t buffer) { void model_buffer_from_mmap_init_type(ggml_backend_hrx_device_context * device) { device->mapped_buft_context = { device, device->name + "_Mapped", true }; + // engine #2500: the imported type had no context (name "", device null) since #371, so every imported weight buffer + // was a nameless, device-less type: "pre-allocated tensor (...) in a buffer () that cannot run the operation". + device->imported_buft_context = { device, device->name + "_Imported", false }; device->mapped_buft = { // designated: the merged ggml interface also has alloc_buffer_n and get_alloc_size_n (left null) { .get_name = mapped_type_name, .alloc_buffer = mapped_type_alloc, .get_alignment = mapped_type_alignment, diff --git a/src/llama-model.cpp b/src/llama-model.cpp index 86c159eee3a4..d5a97228b7b6 100644 --- a/src/llama-model.cpp +++ b/src/llama-model.cpp @@ -2007,6 +2007,13 @@ bool llama_model_base::load_tensors(llama_model_loader & ml) { GGML_ASSERT(!ml.no_alloc); const size_t max_size = ggml_get_max_tensor_size(ctx); + // engine #2500: an HRX device wraps the loader's mapping as a mapped (host) weight buffer, not an import + using hrx_mmap_buffer_fn = ggml_backend_buffer_t (*)(ggml_backend_dev_t, void *, size_t, size_t); + hrx_mmap_buffer_fn hrx_mmap_buffer = nullptr; + if (ggml_backend_reg_t reg = ggml_backend_dev_backend_reg(dev); + reg != nullptr && std::strcmp(ggml_backend_reg_name(reg), "HRX") == 0) { + hrx_mmap_buffer = (hrx_mmap_buffer_fn) ggml_backend_reg_get_proc_address(reg, "ggml_backend_hrx_model_buffer_from_mmap"); + } for (uint32_t idx = 0; idx < ml.files.size(); idx++) { // map the mmap regions that contain the tensors of this context // this is important for metal with apple silicon: a backend buffer pins everything it @@ -2017,7 +2024,9 @@ bool llama_model_base::load_tensors(llama_model_loader & ml) { void * addr = ml.mappings.at(idx)->addr(); for (const auto & range : ml.get_mapping_ranges(idx, ctx)) { - ggml_backend_buffer_t buf = ggml_backend_dev_buffer_from_host_ptr(dev, (char *) addr + range.first, range.last - range.first, max_size); + ggml_backend_buffer_t buf = hrx_mmap_buffer != nullptr + ? hrx_mmap_buffer(dev, (char *) addr + range.first, range.last - range.first, max_size) + : ggml_backend_dev_buffer_from_host_ptr(dev, (char *) addr + range.first, range.last - range.first, max_size); if (buf == nullptr) { throw std::runtime_error(format("unable to allocate %s buffer", ggml_backend_buft_name(buft))); } diff --git a/tests/hrx-buffer-test.cpp b/tests/hrx-buffer-test.cpp index 9bf67ee09ab4..8e4a19bff66d 100644 --- a/tests/hrx-buffer-test.cpp +++ b/tests/hrx-buffer-test.cpp @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -774,6 +775,33 @@ static void run_with_hrx_backend(F && f) { ggml_backend_free(backend); } +// engine #2500: the loader wraps its file mapping through the HRX proc "ggml_backend_hrx_model_buffer_from_mmap" +// (src/llama-model.cpp), which must give a MAPPED weight buffer (host, file-mapped), while the generic +// buffer_from_host_ptr stays an import (non-host, #2393). Since #371 the loader got the import, and a weight view an +// HRX matcher declines (ZAYA's grouped CCA conv) then had no backend at all. The imported type also needs a name. +static void run_loader_mmap_proc_checks(ggml_backend_t backend) { + ggml_backend_dev_t device = ggml_backend_get_device(backend); + ggml_backend_reg_t reg = ggml_backend_dev_backend_reg(device); + REQUIRE(reg != nullptr); + using fn_t = ggml_backend_buffer_t (*)(ggml_backend_dev_t, void *, size_t, size_t); + auto fn = (fn_t) ggml_backend_reg_get_proc_address(reg, "ggml_backend_hrx_model_buffer_from_mmap"); + REQUIRE(fn != nullptr); + alignas(TENSOR_ALIGNMENT) std::array source = {}; + ggml_backend_buffer_t mapped = fn(device, source.data(), sizeof(source), sizeof(source)); + REQUIRE(mapped != nullptr); + REQUIRE(ggml_backend_buffer_is_host(mapped)); + REQUIRE(ggml::hrx::buffer_is_file_mapped(mapped)); + REQUIRE(ggml_backend_dev_supports_buft(device, ggml_backend_buffer_get_type(mapped))); + ggml_backend_buffer_t imported = ggml_backend_dev_buffer_from_host_ptr(device, source.data(), sizeof(source), sizeof(source)); + REQUIRE(imported != nullptr); + REQUIRE(!ggml_backend_buffer_is_host(imported)); + const std::string imported_name = ggml_backend_buft_name(ggml_backend_buffer_get_type(imported)); + REQUIRE(!imported_name.empty()); + REQUIRE(imported_name != ggml_backend_buft_name(ggml_backend_buffer_get_type(mapped))); + ggml_backend_buffer_free(imported); + ggml_backend_buffer_free(mapped); +} + static void register_hrx_buffer_cases(test_runner::Suite & suite) { suite.device_case("backend_buffer", [] { run_with_hrx_backend([](ggml_backend_t backend, ggml_backend_hrx_context *) { @@ -795,6 +823,11 @@ static void register_hrx_buffer_cases(test_runner::Suite & suite) { run_mapped_host_ptr_checks(backend); }); }); + suite.device_case("loader_mmap_proc", [] { + run_with_hrx_backend([](ggml_backend_t backend, ggml_backend_hrx_context *) { + run_loader_mmap_proc_checks(backend); + }); + }); suite.device_case("host_transfer", [] { run_with_hrx_backend([](ggml_backend_t, ggml_backend_hrx_context * context) { run_host_transfer_checks(context);