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
37 changes: 30 additions & 7 deletions ggml/src/ggml-hrx/ggml-hrx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,10 @@ static void placement_observer(ggml_backend_sched_t sched, ggml_cgraph * graph,
// 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;
// engine#2494 review: the classes must PARTITION the off-device nodes -- by design + host-non-weight + neither.
// Counting only the first two made "the parts sum to the whole" a claim nothing checked, and a counter that is
// written but never summarised is how a class goes missing from a census without anyone noticing.
int64_t off_unexplained = 0;
ggml_tensor * first_off = nullptr;
ggml_tensor * first_unexplained = nullptr;
for (int i = 0; i < graph->n_nodes; i++) {
Expand Down Expand Up @@ -666,21 +670,40 @@ static void placement_observer(ggml_backend_sched_t sched, ggml_cgraph * graph,
off_mapped_weights++;
}
}
if (!facts.src_host_weight) {
const bool by_design = ggml::hrx::placement_node_is_by_design(facts);
if (by_design) {
off_by_design++;
} else {
off_unexplained++; // neither by design nor (below) host-non-weight: a candidate for strict mode
if (first_unexplained == nullptr) {
first_unexplained = node;
}
}
// engine#2494: counted only for a node that is NOT already explained by design, so the reasons in the census
// PARTITION the off-device nodes instead of overlapping. A reader who adds the counters up should get the
// total; two explanations for one node is how a summary stops summing.
if (!by_design && !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++;
off_unexplained--; // explained now; the partition below depends on it
if (first_unexplained == node) {
first_unexplained = nullptr; // strict mode must not abort on an explained node
}
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++;
} else if (first_unexplained == nullptr) {
first_unexplained = node;
}
}

// THE PARTS SUM TO THE WHOLE. Every off-device node is by design, host-non-weight, or neither; if that ever fails
// the census is missing a class, and the summary would under-report without saying so. WARN, not assert: this is
// a diagnostic path and a broken census must not abort a run it was only describing.
if (!ggml::hrx::census_partitions(off_by_design, off_host_nonweight, off_unexplained, off_nodes)) {
GGML_LOG_WARN("%s: census does not partition: %" PRId64 " by design + %" PRId64 " host-non-weight + %" PRId64
" neither != %" PRId64 " off-device node(s) -- a class is missing from this census\n", __func__,
off_by_design, off_host_nonweight, off_unexplained, off_nodes);
}

std::string off_detail;
Expand Down
7 changes: 7 additions & 0 deletions ggml/src/ggml-hrx/host-weight-placement.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,11 @@ bool host_weight_is_host_resident(const ggml_tensor * tensor);
// 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);

// engine#2494 review: the census explains every off-device node by exactly one class -- by design, host-non-weight, or
// neither. That partition is the thing a reader checks by adding the printed counters up, so it is a pure predicate
// rather than an inline comparison: a claim nothing can call with made-up numbers is a claim nothing can test.
inline bool census_partitions(int64_t by_design, int64_t host_nonweight, int64_t unexplained, int64_t off_nodes) {
return by_design + host_nonweight + unexplained == off_nodes;
}

} // namespace ggml::hrx
8 changes: 8 additions & 0 deletions tests/test-hrx-host-resident.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ int main() {
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");

// engine#2494 review: THE CENSUS MUST PARTITION. Pure arithmetic, so it needs no graph and no backend -- the
// property a reader checks by adding the printed counters up, checked here instead.
CHECK(ggml::hrx::census_partitions(3, 2, 0, 5), "3 by design + 2 host-non-weight == 5 off-device");
CHECK(ggml::hrx::census_partitions(0, 0, 4, 4), "all unexplained still partitions");
CHECK(ggml::hrx::census_partitions(0, 0, 0, 0), "no node on the CPU partitions trivially");
CHECK(!ggml::hrx::census_partitions(3, 2, 0, 6), "one node counted twice must NOT partition");
CHECK(!ggml::hrx::census_partitions(3, 2, 0, 4), "one node missed must NOT partition");

ggml_backend_buffer_free(buf);
ggml_free(ctx);
ggml_backend_free(backend);
Expand Down