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
5 changes: 5 additions & 0 deletions .github/workflows/android_hexagon_candidate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/validate_wrapper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cmake/llama_cpp_patches.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
34 changes: 34 additions & 0 deletions docs/android_hexagon.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
47 changes: 47 additions & 0 deletions patches/llama.cpp/0010-hexagon-startup-rollback.patch
Original file line number Diff line number Diff line change
@@ -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();
18 changes: 18 additions & 0 deletions patches/llama.cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions patches/llama.cpp/series.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
]
}
]
}
3 changes: 3 additions & 0 deletions tests/fixtures/hexagon_fastrpc/AEEStdErr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

enum { AEE_SUCCESS, AEE_EFAILED, AEE_EEXPIRED, AEE_EWOULDBLOCK };
26 changes: 26 additions & 0 deletions tests/fixtures/hexagon_fastrpc/dspqueue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <stdint.h>

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);
}
15 changes: 15 additions & 0 deletions tests/fixtures/hexagon_fastrpc/htp_iface.h
Original file line number Diff line number Diff line change
@@ -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 *);
83 changes: 83 additions & 0 deletions tests/fixtures/hexagon_fastrpc/remote.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#pragma once

#include <stddef.h>
#include <stdint.h>

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);
}
11 changes: 11 additions & 0 deletions tests/fixtures/hexagon_fastrpc/rpcmem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <stddef.h>

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 *);
}
Loading
Loading