diff --git a/ggml/src/ggml-hrx/ggml-hrx.cpp b/ggml/src/ggml-hrx/ggml-hrx.cpp index 8494593f7b0..ebef5fb7379 100644 --- a/ggml/src/ggml-hrx/ggml-hrx.cpp +++ b/ggml/src/ggml-hrx/ggml-hrx.cpp @@ -626,6 +626,10 @@ static void placement_observer(ggml_backend_sched_t sched, ggml_cgraph * graph, int64_t unaligned_input_size = 0; int64_t off_by_design = 0; int64_t off_mapped_weights = 0; + // engine#2494: a host-resident source that is NOT a weight. `host_resident_weight()` requires the WEIGHTS usage, so + // an index/cache/bias/activation kept in host memory was invisible here -- the one state that a GPU cannot read + // without a mapping, and the one this census could not name. + int64_t off_host_nonweight = 0; ggml_tensor * first_off = nullptr; ggml_tensor * first_unexplained = nullptr; for (int i = 0; i < graph->n_nodes; i++) { @@ -662,6 +666,15 @@ static void placement_observer(ggml_backend_sched_t sched, ggml_cgraph * graph, off_mapped_weights++; } } + if (!facts.src_host_weight) { + for (int s = 0; s < GGML_MAX_SRC; ++s) { + const ggml_tensor * src = node->src[s]; + if (src != nullptr && ggml::hrx::tensor_is_host_resident(src->view_src != nullptr ? src->view_src : src)) { + off_host_nonweight++; + break; // per node, like the counters above + } + } + } const bool by_design = ggml::hrx::placement_node_is_by_design(facts); if (by_design) { off_by_design++; @@ -763,6 +776,11 @@ static void placement_observer(ggml_backend_sched_t sched, ggml_cgraph * graph, } // engine#2450: name the other by-design class, so a reader of the census sees why the node is on // the CPU instead of concluding the kernel is missing. + if (off_host_nonweight > 0) { + reason += " - " + std::to_string(off_host_nonweight) + + " node(s) read a source that is host-resident and not a weight (index, cache or bias kept on " + "the host), which the GPU cannot dereference without a mapping"; + } if (off_mapped_weights > 0) { reason += " - " + std::to_string(off_mapped_weights) + " node(s) read a weight that lives in the loader's file mapping, which ggml must see as host memory"; diff --git a/ggml/src/ggml-hrx/host-weight-placement.cpp b/ggml/src/ggml-hrx/host-weight-placement.cpp index 8837e659540..a92cc7ad919 100644 --- a/ggml/src/ggml-hrx/host-weight-placement.cpp +++ b/ggml/src/ggml-hrx/host-weight-placement.cpp @@ -36,7 +36,11 @@ namespace { // True for a model weight that is NOT in an HRX-allocated buffer, i.e. a tensor the executor would have to copy // into device memory before it could bind it. -bool host_resident_weight(const ggml_tensor * tensor) { +// engine#2494: residence and usage are two different questions and only the pair answers "is this a host weight". +// A tensor can be host-resident and NOT a weight -- an index, a cache, a bias, an activation kept on the host -- and +// `host_resident_weight()` returns false for all of those, so the placement census never mentions them even though the +// GPU cannot read them. Asking residence on its own makes that state countable. +bool tensor_is_host_resident(const ggml_tensor * tensor) { if (tensor == nullptr) { return false; } @@ -47,9 +51,14 @@ bool host_resident_weight(const ggml_tensor * tensor) { if (buffer->iface.get_base == ggml_backend_hrx_buffer_base) { return false; } - if (!ggml_backend_buffer_is_host(buffer)) { + return ggml_backend_buffer_is_host(buffer); +} + +bool host_resident_weight(const ggml_tensor * tensor) { + if (!tensor_is_host_resident(tensor)) { return false; } + ggml_backend_buffer_t buffer = tensor->view_src != nullptr ? tensor->view_src->buffer : tensor->buffer; return ggml_backend_buffer_get_usage(buffer) == GGML_BACKEND_BUFFER_USAGE_WEIGHTS; } diff --git a/ggml/src/ggml-hrx/host-weight-placement.h b/ggml/src/ggml-hrx/host-weight-placement.h index a082c2bbc15..bdefe792491 100644 --- a/ggml/src/ggml-hrx/host-weight-placement.h +++ b/ggml/src/ggml-hrx/host-weight-placement.h @@ -48,4 +48,10 @@ bool host_weight_exceeds_residency(const ggml_tensor * op); // Exported so the placement observer classifies a by-design CPU node the same way the residency decline does. bool host_weight_is_host_resident(const ggml_tensor * tensor); +// engine#2494: the two questions behind the one above, separately. `tensor_is_host_resident` is true for ANY tensor +// whose storage is host memory that the GPU therefore cannot dereference without a mapping, whatever the buffer's +// usage; `host_resident_weight` adds the WEIGHTS test. The census wants the first: a host-resident tensor that is +// not a weight is exactly the state nothing could see (see the non-weight counter in ggml-hrx.cpp). +bool tensor_is_host_resident(const ggml_tensor * tensor); + } // namespace ggml::hrx diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8b785ca3310..669b9bb5060 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -471,6 +471,13 @@ if (TARGET ggml-hrx) # (pure helper, no device and no ggml link -- engine#2450) add_executable(test-hrx-placement-classification test-hrx-placement-classification.cpp ../ggml/src/ggml-hrx/placement-classification.cpp) + # engine#2494: residence is not usage. Links ggml because it needs a real backend buffer to mark USAGE_WEIGHTS, + # and the CPU device is the only one guaranteed present -- no GPU, no HRX runtime, no model. + add_executable(test-hrx-host-resident test-hrx-host-resident.cpp + ../ggml/src/ggml-hrx/host-weight-placement.cpp) + target_include_directories(test-hrx-host-resident PRIVATE ../ggml/src/ggml-hrx) + target_link_libraries(test-hrx-host-resident PRIVATE ggml) + add_test(NAME test-hrx-host-resident COMMAND test-hrx-host-resident) target_include_directories(test-hrx-placement-classification PRIVATE ../ggml/src/ggml-hrx) add_test(NAME test-hrx-placement-classification COMMAND test-hrx-placement-classification) add_executable(test-hrx-arena-refusal test-hrx-arena-refusal.cpp) diff --git a/tests/test-hrx-host-resident.cpp b/tests/test-hrx-host-resident.cpp new file mode 100644 index 00000000000..2c833750608 --- /dev/null +++ b/tests/test-hrx-host-resident.cpp @@ -0,0 +1,99 @@ +// Copyright 2026 bong-water-water-bong +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Host test for host-weight-placement.h: residence is not usage (engine#2494). +// +// The census named two by-design reasons a node sits on the CPU, and both asked host_resident_weight(), which ends +// with `usage == GGML_BACKEND_BUFFER_USAGE_WEIGHTS`. A tensor that is genuinely host-resident but whose buffer is +// USAGE_ANY -- an index, a cache, a bias, an activation kept on the host -- therefore returned false, and the one +// state a GPU cannot dereference without a mapping was the one the census could not print. This pins the split: +// tensor_is_host_resident() answers residence, host_resident_weight() answers residence AND usage. +// +// Uses the CPU backend because it is the only one guaranteed present; no device, no HRX runtime, no model. + +#include "host-weight-placement.h" + +#include + +#include "ggml-backend.h" +#include "ggml-alloc.h" +#include "ggml.h" + +static int fails = 0; + +#define CHECK(c, m) do { if (!(c)) { std::printf("FAIL: %s\n", m); ++fails; } } while (0) + +int main() { + // Null is not resident: both predicates must survive being asked about nothing. + CHECK(!ggml::hrx::tensor_is_host_resident(nullptr), "null is not host-resident"); + CHECK(!ggml::hrx::host_weight_is_host_resident(nullptr), "null is not a host weight"); + + ggml_backend_dev_t cpu = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU); + if (cpu == nullptr) { + std::printf("test-hrx-host-resident: no CPU backend, nothing to test\n"); + return 0; + } + ggml_backend_t backend = ggml_backend_dev_init(cpu, nullptr); + if (backend == nullptr) { + std::printf("FAIL: could not init the CPU backend\n"); + return 1; + } + + ggml_init_params params = { /*.mem_size = */ ggml_tensor_overhead() * 8 + ggml_graph_overhead(), + /*.mem_buffer = */ nullptr, + /*.no_alloc = */ true }; + ggml_context * ctx = ggml_init(params); + ggml_tensor * t = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 16, 4); + ggml_tensor * v = ggml_view_2d(ctx, t, 16, 2, t->nb[1], 0); + + ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors(ctx, backend); + if (buf == nullptr) { + std::printf("FAIL: could not allocate context tensors\n"); + ggml_free(ctx); + ggml_backend_free(backend); + return 1; + } + + // 1. A host buffer with the default usage. This is the case that used to be invisible: resident, and NOT a + // weight, so host_resident_weight() said false and the census stayed silent about it. + CHECK(ggml_backend_buffer_get_usage(buf) == GGML_BACKEND_BUFFER_USAGE_ANY, "a fresh buffer is USAGE_ANY"); + CHECK(ggml::hrx::tensor_is_host_resident(t), "a CPU-backend tensor is host-resident"); + CHECK(!ggml::hrx::host_weight_is_host_resident(t), "USAGE_ANY is not a weight"); + + + // 2. A view answers like its base: residence is a property of the storage, not of the tensor object. + CHECK(ggml::hrx::tensor_is_host_resident(v), "a view of a host tensor is host-resident"); + CHECK(!ggml::hrx::host_weight_is_host_resident(v), "a view of a USAGE_ANY buffer is not a weight"); + + // 3. Mark the same buffer as weights: now BOTH predicates are true, which is the pair the census names. + ggml_backend_buffer_set_usage(buf, GGML_BACKEND_BUFFER_USAGE_WEIGHTS); + CHECK(ggml::hrx::tensor_is_host_resident(t), "still host-resident"); + CHECK(ggml::hrx::host_weight_is_host_resident(t), "a WEIGHTS buffer is a host weight"); + + + // 4. The export only exposes the composition; residence on its own is the new export. Both must be true here, + // and the split is what makes the USAGE_ANY case above distinguishable from a non-host tensor. + CHECK(ggml::hrx::tensor_is_host_resident(t) && ggml::hrx::host_weight_is_host_resident(t), + "a WEIGHTS buffer is host-resident AND a weight"); + + ggml_backend_buffer_free(buf); + ggml_free(ctx); + ggml_backend_free(backend); + + if (fails == 0) { + std::printf("test-hrx-host-resident: ok (residence != usage)\n"); + } + return fails == 0 ? 0 : 1; +}