diff --git a/.github/workflows/android_hexagon_candidate.yml b/.github/workflows/android_hexagon_candidate.yml index a80b5bb2..50f8b3cf 100644 --- a/.github/workflows/android_hexagon_candidate.yml +++ b/.github/workflows/android_hexagon_candidate.yml @@ -4,10 +4,15 @@ on: pull_request: paths: - '.github/workflows/android_hexagon_candidate.yml' + - 'CMakeLists.txt' + - 'cmake/llama_cpp_patches.cmake' + - 'patches/llama.cpp/**' + - 'third_party/llama.cpp' - 'tests/test_android_hexagon.py' - 'tools/android_hexagon.py' - 'tools/build.py' - 'tools/build_android_hexagon_candidate.sh' + - 'tools/llama_cpp_patches.py' workflow_call: inputs: native_commit: diff --git a/.github/workflows/validate_wrapper.yml b/.github/workflows/validate_wrapper.yml index aa57eb4c..635c71bd 100644 --- a/.github/workflows/validate_wrapper.yml +++ b/.github/workflows/validate_wrapper.yml @@ -521,6 +521,7 @@ jobs: run: >- cmake --build build/wrapper-contract --target llamadart_owner_api_test llamadart_owner_reentrancy_test + llamadart_hexagon_startup_test llamadart_file_sha256_test llamadart_model_fd_test llamadart_threadpool_test llamadart_grammar_top_k_test llamadart_f16_dot_accumulation_test diff --git a/CMakeLists.txt b/CMakeLists.txt index dc2df5b9..b5f951b4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -399,6 +399,40 @@ target_include_directories(llamadart_lib PRIVATE if (LLAMADART_BUILD_TESTS) enable_testing() target_compile_definitions(llamadart_lib PRIVATE LLAMADART_TESTING=1) + if (NOT CMAKE_CROSSCOMPILING AND (CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Darwin")) + set(hexagon_dir ggml/src/ggml-hexagon) + set(hexagon_host_source ${hexagon_dir}/ggml-hexagon.cpp) + set(hexagon_upstream ${CMAKE_CURRENT_SOURCE_DIR}/third_party/llama.cpp/${hexagon_dir}) + if (hexagon_host_source IN_LIST LLAMADART_LLAMA_CPP_PATCHED_SOURCES) + set(hexagon_host_source "${CMAKE_CURRENT_BINARY_DIR}/llama.cpp-patched/${hexagon_host_source}") + else() + set(hexagon_host_source "${hexagon_upstream}/ggml-hexagon.cpp") + endif() + add_executable(llamadart_hexagon_startup_test + tests/hexagon_startup_test.cpp + "${hexagon_upstream}/htp-fusion.cpp" + "${hexagon_upstream}/htp-kparams.cpp" + "${hexagon_upstream}/htp-lower.cpp" + "${hexagon_upstream}/htp-opnode.cpp" + "${hexagon_upstream}/htp-repack.cpp") + target_compile_features(llamadart_hexagon_startup_test PRIVATE cxx_std_17) + if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm)") + # GCC defines __fp16 only on Arm; the host Hexagon sources use it for sizes and storage. + target_compile_definitions(llamadart_hexagon_startup_test PRIVATE __fp16=_Float16) + endif() + target_compile_definitions(llamadart_hexagon_startup_test PRIVATE + "LLAMADART_HEXAGON_HOST_SOURCE=\"${hexagon_host_source}\"") + target_include_directories(llamadart_hexagon_startup_test PRIVATE + tests/fixtures/hexagon_fastrpc src third_party/llama.cpp/include + third_party/llama.cpp/ggml/src "${hexagon_upstream}") + target_link_libraries(llamadart_hexagon_startup_test PRIVATE llamadart_lib) + foreach(scenario IN ITEMS cleanup-ok cleanup-fail group-rollback constructed-abort) + add_test(NAME llamadart_hexagon_startup_${scenario} + COMMAND llamadart_hexagon_startup_test ${scenario}) + set_tests_properties(llamadart_hexagon_startup_${scenario} PROPERTIES TIMEOUT 30) + endforeach() + endif() + add_executable(llamadart_f16_dot_accumulation_test tests/f16_dot_accumulation_test.cpp) target_compile_features(llamadart_f16_dot_accumulation_test PRIVATE cxx_std_17) target_compile_definitions(llamadart_f16_dot_accumulation_test PRIVATE diff --git a/cmake/llama_cpp_patches.cmake b/cmake/llama_cpp_patches.cmake index 012b1a7f..846f7c98 100644 --- a/cmake/llama_cpp_patches.cmake +++ b/cmake/llama_cpp_patches.cmake @@ -42,6 +42,7 @@ function(llamadart_apply_llama_cpp_patches) if (error) message(WARNING "patches/llama.cpp does not fit this llama.cpp:\n${error}") endif() + set(LLAMADART_LLAMA_CPP_PATCHED_SOURCES "${patched_sources}" PARENT_SCOPE) if (NOT patched_sources) return() endif() diff --git a/docs/android_hexagon.md b/docs/android_hexagon.md index 063837ac..545581a1 100644 --- a/docs/android_hexagon.md +++ b/docs/android_hexagon.md @@ -58,3 +58,37 @@ actual FastRPC session and DSP cycles together with completed decode. Check that missing and stale assets refuse HTP. The presence of a skeleton in the bundle does not qualify its DSP version. The Dart hook and runtime contract are tracked in [llamadart#2553](https://github.com/leehack/llamadart/issues/2553). + +## Session startup rollback + +Pinned llama.cpp aborts the process when `dspqueue_close` or `htp_iface_stop` +fails in `ggml_hexagon_session::release()`. During session construction that +rollback ran after a failed `dspqueue_export` (or after a failed sibling of a +multi-session group), so a FastRPC cleanup error hid the startup error that +`llama_dart_ggml_backend_dev_init` should report +([#264](https://github.com/leehack/llamadart-native/issues/264)). + +Patch `0010-hexagon-startup-rollback.patch` marks a session as started only +after its constructor succeeds. Before that, a failed `dspqueue_close` or +`htp_iface_stop` is logged as `startup rollback: ... failed` and the rollback +continues; a group rollback also demotes the sessions that had already started. +A fully constructed session keeps the upstream abort on its destructor path. +The maintainer approved the patch-eligibility exception for 0010 +(`patches/llama.cpp/README.md`, "Exception for native #264"). + +`llamadart_hexagon_startup_test` links the pinned (patched) Hexagon host source +against stubbed FastRPC calls on Linux and macOS hosts. Its `cleanup-ok`, +`cleanup-fail` and `group-rollback` scenarios must return no backend, the +original `dspqueue export failed` error and a close of every queue and handle. +`constructed-abort` runs a successfully constructed session in a child process +with a failing `dspqueue_close` and requires the upstream `GGML_ABORT`. Without +the patch the `cleanup-fail` and `group-rollback` scenarios abort. The Android +candidate fails validation when `libggml-hexagon.so` lacks the patch marker. +Remove the patch when upstream stops aborting during startup rollback. + +Device qualification of the candidate must cover the rollback path the host +test cannot: after a failed `htp_iface_stop` during startup rollback (a demoted +sibling or the group parent), teardown now unmaps and frees the session buffers +and closes the handle where upstream aborted. Confirm on a device that this +leaves no stuck DSP state or leaked mapping, and that the next session start +succeeds. diff --git a/patches/llama.cpp/0010-hexagon-startup-rollback.patch b/patches/llama.cpp/0010-hexagon-startup-rollback.patch new file mode 100644 index 00000000..42b8b521 --- /dev/null +++ b/patches/llama.cpp/0010-hexagon-startup-rollback.patch @@ -0,0 +1,47 @@ +diff --git a/ggml/src/ggml-hexagon/ggml-hexagon.cpp b/ggml/src/ggml-hexagon/ggml-hexagon.cpp +index 966219e52..5c886e243 100644 +--- a/ggml/src/ggml-hexagon/ggml-hexagon.cpp ++++ b/ggml/src/ggml-hexagon/ggml-hexagon.cpp +@@ -317,4 +317,5 @@ struct ggml_hexagon_session { + bool valid_queue; + bool valid_iface; ++ bool startup_complete = false; + + ggml_hexagon_opbatch* op_batch; +@@ -2500,4 +2501,10 @@ void ggml_hexagon_session::release() noexcept(true) { + GGML_LOG_INFO("ggml-hex: releasing session: %s\n", this->name.c_str()); + ++ if (!this->startup_complete) { ++ // A sibling may have started before the group constructor failed. ++ for (auto & session : this->mdev.sessions) { ++ session->startup_complete = false; ++ } ++ } + this->mdev.sessions.clear(); + +@@ -2508,5 +2515,8 @@ void ggml_hexagon_session::release() noexcept(true) { + err = htp_iface_stop(this->handle); + if (err != 0) { +- GGML_ABORT("ggml-hex: htp_iface_stop failed: 0x%08x\n", (unsigned) err); ++ if (this->startup_complete) { ++ GGML_ABORT("ggml-hex: htp_iface_stop failed: 0x%08x\n", (unsigned) err); ++ } ++ GGML_LOG_ERROR("ggml-hex: startup rollback: htp_iface_stop failed: 0x%08x\n", (unsigned) err); + } + } +@@ -2552,5 +2562,8 @@ void ggml_hexagon_session::release() noexcept(true) { + err = dspqueue_close(queue); + if (err != 0) { +- GGML_ABORT("ggml-hex: dspqueue_close failed: 0x%08x\n", (unsigned) err); ++ if (this->startup_complete) { ++ GGML_ABORT("ggml-hex: dspqueue_close failed: 0x%08x\n", (unsigned) err); ++ } ++ GGML_LOG_ERROR("ggml-hex: startup rollback: dspqueue_close failed: 0x%08x\n", (unsigned) err); + } + } +@@ -2579,4 +2592,5 @@ ggml_hexagon_session::ggml_hexagon_session(const ggml_hexagon_device_config & co + } + } ++ this->startup_complete = true; + } catch (const std::exception & exc) { + release(); diff --git a/patches/llama.cpp/README.md b/patches/llama.cpp/README.md index 8c3ae2fd..7f897779 100644 --- a/patches/llama.cpp/README.md +++ b/patches/llama.cpp/README.md @@ -61,6 +61,24 @@ Remove both when upstream ships a `src1`-precision-gated F16 x F16 path for the CPU backend, or ARM F16 x F32 kernels, that keep F16 prefill and decode at 0.97 or more of `v0.9.0` on the FP16 modules. +## Exception for native #264 + +The maintainer approved (2026-12-01) a third narrow exception to the first +condition above for patch 0010 only. The opt-in Android Hexagon candidate is +not a released artifact; the crash is a P1 from +[#264](https://github.com/leehack/llamadart-native/issues/264), found before +that candidate ships. The patch is about 16 lines and upstream has no fix. + +When a FastRPC cleanup call fails while a Hexagon session is still being +constructed, pinned llama.cpp aborts the process and the startup error is +lost. Patch 0010 logs that failure instead and keeps rolling back; a fully +constructed session keeps upstream's abort. Evidence, the host test and the +device-qualification note: +[android_hexagon.md](../../docs/android_hexagon.md#session-startup-rollback). +This exception does not extend eligibility to other patches. + +Remove 0010 when upstream stops aborting during startup rollback. + ## Files - `series.json` lists the patches in the order they apply. Each entry has the diff --git a/patches/llama.cpp/series.json b/patches/llama.cpp/series.json index 31994b38..0857db1f 100644 --- a/patches/llama.cpp/series.json +++ b/patches/llama.cpp/series.json @@ -59,6 +59,14 @@ "apple_markers": [ {"library": "libllamadart.dylib", "text": "llamadart F16 K.Q route"} ] + }, + { + "file": "0010-hexagon-startup-rollback.patch", + "tracking": "https://github.com/leehack/llamadart-native/issues/264", + "upstream": "https://github.com/ggml-org/llama.cpp/blob/171527522610996f5f0acf12334e185156ca8f44/ggml/src/ggml-hexagon/ggml-hexagon.cpp#L2499-L2588", + "android_markers": [ + {"library": "libggml-hexagon.so", "text": "startup rollback: dspqueue_close failed"} + ] } ] } diff --git a/tests/fixtures/hexagon_fastrpc/AEEStdErr.h b/tests/fixtures/hexagon_fastrpc/AEEStdErr.h new file mode 100644 index 00000000..18276531 --- /dev/null +++ b/tests/fixtures/hexagon_fastrpc/AEEStdErr.h @@ -0,0 +1,3 @@ +#pragma once + +enum { AEE_SUCCESS, AEE_EFAILED, AEE_EEXPIRED, AEE_EWOULDBLOCK }; diff --git a/tests/fixtures/hexagon_fastrpc/dspqueue.h b/tests/fixtures/hexagon_fastrpc/dspqueue.h new file mode 100644 index 00000000..9bf38047 --- /dev/null +++ b/tests/fixtures/hexagon_fastrpc/dspqueue.h @@ -0,0 +1,26 @@ +#pragma once + +#include + +using dspqueue_t = uint64_t; +using dspqueue_callback = void (*)(dspqueue_t, void *); +enum { DSPQUEUE_BUFFER_FLAG_FLUSH_SENDER = 1, DSPQUEUE_BUFFER_FLAG_INVALIDATE_RECIPIENT = 2 }; + +struct dspqueue_buffer { + void *ptr; + int fd; + uint32_t flags; + uint32_t offset; + uint32_t size; +}; + +extern "C" { +int dspqueue_create(int, uint32_t, uint32_t, uint32_t, dspqueue_callback, dspqueue_callback, + void *, dspqueue_t *); +int dspqueue_export(dspqueue_t, uint64_t *); +int dspqueue_close(dspqueue_t); +int dspqueue_read(dspqueue_t, uint32_t *, uint32_t, uint32_t *, dspqueue_buffer *, + uint32_t, uint32_t *, uint8_t *, uint32_t); +int dspqueue_write(dspqueue_t, uint32_t, uint32_t, const dspqueue_buffer *, + uint32_t, const uint8_t *, uint32_t); +} diff --git a/tests/fixtures/hexagon_fastrpc/htp_iface.h b/tests/fixtures/hexagon_fastrpc/htp_iface.h new file mode 100644 index 00000000..91709a3f --- /dev/null +++ b/tests/fixtures/hexagon_fastrpc/htp_iface.h @@ -0,0 +1,15 @@ +#pragma once + +#include "remote.h" + +struct htp_iface_pmu_conf { uint32_t events[8]; }; + +int htp_iface_open(const char *, remote_handle64 *); +int htp_iface_close(remote_handle64); +int htp_iface_start(remote_handle64, uint32_t, uint64_t, uint32_t, uint32_t, + uint64_t, uint64_t, uint32_t); +int htp_iface_stop(remote_handle64); +int htp_iface_munmap(remote_handle64, uint32_t); +int htp_iface_profiler(remote_handle64, uint32_t, const htp_iface_pmu_conf *); +int htp_iface_etm(remote_handle64, uint32_t); +int htp_iface_hwinfo(remote_handle64, unsigned *, unsigned *, unsigned *, unsigned long long *); diff --git a/tests/fixtures/hexagon_fastrpc/remote.h b/tests/fixtures/hexagon_fastrpc/remote.h new file mode 100644 index 00000000..eea474c7 --- /dev/null +++ b/tests/fixtures/hexagon_fastrpc/remote.h @@ -0,0 +1,83 @@ +#pragma once + +#include +#include + +using remote_handle64 = uint64_t; +struct domain; +constexpr int CDSP_DOMAIN_ID = 3; +constexpr const char *CDSP_DOMAIN_NAME = "cdsp"; + +enum { + FASTRPC_RESERVE_NEW_SESSION, + FASTRPC_GET_EFFECTIVE_DOMAIN_ID, + DSPRPC_CONTROL_UNSIGNED_MODULE, + FASTRPC_GET_URI, + DSPRPC_CONTROL_LATENCY, + FASTRPC_GET_DOMAINS, + FASTRPC_NSP, +}; + +constexpr int DOMAINS_LIST_FLAGS_SET_TYPE(int flags, int type) { return flags | type; } + +struct remote_rpc_reserve_new_session { + char *domain_name; + size_t domain_name_len; + char *session_name; + size_t session_name_len; + uint32_t session_id; + uint32_t effective_domain_id; +}; + +struct remote_rpc_effective_domain_id { + char *domain_name; + size_t domain_name_len; + uint32_t session_id; + uint32_t effective_domain_id; +}; + +struct remote_rpc_control_unsigned_module { uint32_t domain; int enable; }; +struct remote_rpc_control_latency { int enable; }; + +struct remote_rpc_get_uri { + uint32_t session_id; + char *domain_name; + size_t domain_name_len; + char *module_uri; + size_t module_uri_len; + char *uri; + size_t uri_len; +}; + +struct fastrpc_domain { + int id; + int instance_id; + int status; + int type; + char name[16]; +}; + +struct system_req_payload { + int id; + struct { + fastrpc_domain *domains; + int max_domains; + int num_domains; + int flags; + } sys; +}; + +enum fastrpc_map_flags { + FASTRPC_MAP_FD, + FASTRPC_MAP_FD_DELAYED, + FASTRPC_MAP_FD_DELAYED_EXTENDED, + FASTRPC_MAP_FD_EXTENDED, +}; + +extern "C" { +int remote_session_control(int, void *, uint32_t); +int remote_handle_control(int, void *, uint32_t); +int remote_handle64_control(remote_handle64, int, void *, uint32_t); +int fastrpc_mmap(int, int, void *, int, size_t, fastrpc_map_flags); +int fastrpc_munmap(int, int, void *, size_t); +} diff --git a/tests/fixtures/hexagon_fastrpc/rpcmem.h b/tests/fixtures/hexagon_fastrpc/rpcmem.h new file mode 100644 index 00000000..a2e6d58d --- /dev/null +++ b/tests/fixtures/hexagon_fastrpc/rpcmem.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +enum { RPCMEM_HEAP_ID_SYSTEM, RPCMEM_DEFAULT_FLAGS }; + +extern "C" { +void *rpcmem_alloc2(int, unsigned, size_t); +void rpcmem_free(void *); +int rpcmem_to_fd(void *); +} diff --git a/tests/hexagon_startup_test.cpp b/tests/hexagon_startup_test.cpp new file mode 100644 index 00000000..88b862e4 --- /dev/null +++ b/tests/hexagon_startup_test.cpp @@ -0,0 +1,199 @@ +#include LLAMADART_HEXAGON_HOST_SOURCE +#include "htp-drv.h" +#include "htp_iface.h" +#include "llama_dart_wrapper.h" + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +namespace { + +constexpr int export_error = 0x101; +constexpr int cleanup_error = 0x1c8; +int queue_count = 0; +int queue_closes = 0; +int handle_opens = 0; +int handle_closes = 0; +int stops = 0; +bool fail_close = false; +bool group_rollback = false; +bool constructed_abort = false; + +} // namespace + +int htpdrv_init() { return AEE_SUCCESS; } +int htpdrv_get_arch(int, int *arch) { *arch = 75; return AEE_SUCCESS; } +bool htpdrv_has_read_noblock() { return false; } +std::string htp_lib_dir() { return {}; } +int htpdrv_set_dsp_search_path(int, const std::string &, const char *) { return AEE_EFAILED; } +void htpdrv_set_dsp_env_path(const std::string &, const char *) {} +int remote_system_request(system_req_payload *request) { + const fastrpc_domain domains[] = { + {3, 0, 1, FASTRPC_NSP, "cdsp"}, + {4, 1, 1, FASTRPC_NSP, "cdsp1"}, + {5, 2, 1, FASTRPC_NSP, "cdsp2"}, + }; + request->sys.num_domains = 3; + std::copy_n(domains, std::min(request->sys.max_domains, request->sys.num_domains), + request->sys.domains); + return AEE_SUCCESS; +} + +int remote_session_control(int request, void *, uint32_t) { + if (request == DSPRPC_CONTROL_UNSIGNED_MODULE) { + return AEE_SUCCESS; + } + return AEE_EFAILED; +} + +int remote_handle64_control(remote_handle64, int, void *, uint32_t) { return AEE_SUCCESS; } +int fastrpc_mmap(int, int, void *, int, size_t, fastrpc_map_flags) { return AEE_SUCCESS; } +int fastrpc_munmap(int, int, void *, size_t) { return AEE_SUCCESS; } +void *rpcmem_alloc2(int, unsigned, size_t size) { + void *ptr = nullptr; + return posix_memalign(&ptr, 4096, size) == 0 ? ptr : nullptr; +} +void rpcmem_free(void *ptr) { std::free(ptr); } +int rpcmem_to_fd(void *) { return 42; } + +int htp_iface_open(const char *, remote_handle64 *handle) { + *handle = ++handle_opens; + return AEE_SUCCESS; +} + +int htp_iface_close(remote_handle64) { + ++handle_closes; + return AEE_SUCCESS; +} + +int htp_iface_hwinfo(remote_handle64, unsigned *, unsigned *, unsigned *, unsigned long long *) { + return AEE_EFAILED; // Use the backend's defaults. +} +int htp_iface_start(remote_handle64, uint32_t, uint64_t, uint32_t, uint32_t, uint64_t, uint64_t, uint32_t) { + return AEE_SUCCESS; +} +int htp_iface_stop(remote_handle64 handle) { + ++stops; + return group_rollback && handle == 2 ? cleanup_error : AEE_SUCCESS; +} +int htp_iface_munmap(remote_handle64, uint32_t) { return AEE_SUCCESS; } +int htp_iface_profiler(remote_handle64, uint32_t, const htp_iface_pmu_conf *) { return AEE_SUCCESS; } +int htp_iface_etm(remote_handle64, uint32_t) { return AEE_SUCCESS; } + +int dspqueue_create(int, uint32_t, uint32_t, uint32_t, dspqueue_callback, dspqueue_callback, + void *, dspqueue_t *queue) { + *queue = ++queue_count; + return AEE_SUCCESS; +} + +int dspqueue_export(dspqueue_t queue, uint64_t *id) { + if (!constructed_abort && queue == (group_rollback ? 3 : 1)) { + return export_error; + } + *id = queue; + return AEE_SUCCESS; +} + +int dspqueue_close(dspqueue_t queue) { + ++queue_closes; + return (fail_close || (group_rollback && queue == 2)) ? cleanup_error : AEE_SUCCESS; +} + +int dspqueue_read(dspqueue_t, uint32_t *, uint32_t, uint32_t *, dspqueue_buffer *, + uint32_t, uint32_t *, uint8_t *, uint32_t) { + std::abort(); +} + +int dspqueue_write(dspqueue_t, uint32_t, uint32_t, const dspqueue_buffer *, + uint32_t, const uint8_t *, uint32_t) { + std::abort(); +} + +int main(int argc, char **argv) { + const std::string_view scenario = argc == 2 ? argv[1] : ""; + constructed_abort = scenario == "constructed-abort"; + fail_close = scenario == "cleanup-fail" || constructed_abort; + group_rollback = scenario == "group-rollback"; + if (scenario != "cleanup-ok" && !fail_close && !group_rollback) { + return 2; + } + setenv("GGML_HEXAGON_DEVICES", group_rollback ? "HTP[0-2]" : "1", 1); + setenv("GGML_HEXAGON_ETM", "0", 1); + setenv("GGML_HEXAGON_PROFILE", "0", 1); + setenv("GGML_HEXAGON_VMEM", "1073741824", 1); + + int pipe_fds[2] = {-1, -1}; + pid_t child = 0; + if (constructed_abort) { + if (pipe(pipe_fds) != 0 || (child = fork()) < 0) { + return 1; + } + if (child > 0) { + close(pipe_fds[1]); + std::string output; + char chunk[4096]; + for (ssize_t n; (n = read(pipe_fds[0], chunk, sizeof(chunk))) > 0;) { + output.append(chunk, static_cast(n)); + } + close(pipe_fds[0]); + int status = 0; + waitpid(child, &status, 0); + const bool aborted = WIFSIGNALED(status) && WTERMSIG(status) == SIGABRT && + output.find("ggml-hex: dspqueue_close failed: 0x000001c8") != std::string::npos; + if (!aborted) { + std::fputs(output.c_str(), stderr); + std::fprintf(stderr, "Expected a constructed session to abort on a failed dspqueue_close. status=%d\n", status); + return 1; + } + std::puts("constructed-abort: aborted on the failed dspqueue_close"); + return 0; + } + close(pipe_fds[0]); + dup2(pipe_fds[1], STDERR_FILENO); + close(pipe_fds[1]); + } + + auto *reg = ggml_backend_hexagon_reg(); + auto *dev = reg ? ggml_backend_reg_dev_get(reg, 0) : nullptr; + if (!dev) { + std::fputs("Could not register the stubbed Hexagon device\n", stderr); + return 1; + } + auto *backend = llama_dart_ggml_backend_dev_init(dev, nullptr); + const char *last_error = llama_dart_last_error(); + if (constructed_abort) { + if (!backend) { + std::fprintf(stderr, "Expected a constructed session, got error: %s\n", + last_error ? last_error : "(null)"); + _exit(1); + } + // The upstream registry is never freed at exit, so destroy it here. + delete static_cast(reg->context); + _exit(0); + } + const int expected_sessions = group_rollback ? 3 : 1; + const int expected_stops = group_rollback ? 2 : 0; + if (backend || !last_error || + std::strcmp(last_error, "ggml-hex: dspqueue export failed (see log for details)") != 0 || + queue_count != expected_sessions || queue_closes != expected_sessions || + handle_opens != expected_sessions || handle_closes != expected_sessions || + stops != expected_stops) { + std::fprintf(stderr, + "Expected a null backend, the original export error and cleanup of %d sessions. " + "Got backend=%p last_error=%s queues=%d/%d handles=%d/%d stops=%d\n", + expected_sessions, + static_cast(backend), last_error ? last_error : "(null)", + queue_count, queue_closes, handle_opens, handle_closes, stops); + return 1; + } + std::printf("%s: backend=null last_error=%s queue_close=%d handle_close=%d stop=%d\n", + argv[1], last_error, queue_closes, handle_closes, stops); +}