Skip to content

Commit c65345b

Browse files
committed
refactor: enhance memory allocation handling and improve error logging in hexagon backend
1 parent 5e90233 commit c65345b

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

ggml/src/ggml-hexagon/ggml-hexagon.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@ struct ggml_backend_hexagon_buffer_type_context {
328328
std::string name;
329329
};
330330

331+
#pragma weak rpcmem_alloc2
332+
331333
struct ggml_backend_hexagon_buffer_context {
332334
bool mmap_to(ggml_hexagon_session * s) {
333335
HEX_VERBOSE("ggml-hex: %s mmaping buffer: base %p domain-id %d session-id %d size %zu fd %d repack %d\n",
@@ -367,7 +369,13 @@ struct ggml_backend_hexagon_buffer_context {
367369
ggml_backend_hexagon_buffer_context(ggml_hexagon_session * sess, size_t size, bool repack) {
368370
size += 4 * 1024; // extra page for padding
369371

370-
this->base = (uint8_t *) rpcmem_alloc2(RPCMEM_HEAP_ID_SYSTEM, RPCMEM_DEFAULT_FLAGS | RPCMEM_HEAP_NOREG, size);
372+
if (rpcmem_alloc2) {
373+
this->base = (uint8_t *) rpcmem_alloc2(RPCMEM_HEAP_ID_SYSTEM, RPCMEM_DEFAULT_FLAGS | RPCMEM_HEAP_NOREG, size);
374+
} else {
375+
GGML_LOG_INFO("ggml-hex: %s rpcmem_alloc2 not found, falling back to rpcmem_alloc\n", sess->name.c_str());
376+
this->base = (uint8_t *) rpcmem_alloc(RPCMEM_HEAP_ID_SYSTEM, RPCMEM_DEFAULT_FLAGS | RPCMEM_HEAP_NOREG, size);
377+
}
378+
371379
if (!this->base) {
372380
GGML_LOG_ERROR("ggml-hex: %s failed to allocate buffer : size %zu\n", sess->name.c_str(), size);
373381
throw std::runtime_error("ggml-hex: rpcmem_alloc failed (see log for details)");
@@ -1679,12 +1687,13 @@ void ggml_hexagon_session::allocate(int dev_id) noexcept(false) {
16791687
}
16801688

16811689
// Get session URI
1682-
char htp_uri[256];
1683-
sprintf(htp_uri, "file:///libggml-htp-v%u.so?htp_iface_skel_handle_invoke&_modver=1.0", opt_arch);
16841690

16851691
char session_uri[256];
16861692
{
1687-
struct remote_rpc_get_uri u;
1693+
char htp_uri[256];
1694+
snprintf(htp_uri, sizeof(htp_uri), "file:///libggml-htp-v%u.so?htp_iface_skel_handle_invoke&_modver=1.0", opt_arch);
1695+
1696+
struct remote_rpc_get_uri u = {};
16881697
u.session_id = this->session_id;
16891698
u.domain_name = const_cast<char *>(CDSP_DOMAIN_NAME);
16901699
u.domain_name_len = strlen(CDSP_DOMAIN_NAME);
@@ -1695,8 +1704,10 @@ void ggml_hexagon_session::allocate(int dev_id) noexcept(false) {
16951704

16961705
int err = remote_session_control(FASTRPC_GET_URI, (void *) &u, sizeof(u));
16971706
if (err != AEE_SUCCESS) {
1698-
GGML_LOG_ERROR("ggml-hex: failed to get URI for session %d : error 0x%x\n", dev_id, err);
1699-
throw std::runtime_error("ggml-hex: remote_session_control(get-uri) failed (see log for details)");
1707+
// Fallback: use default URI
1708+
snprintf(session_uri, sizeof(session_uri), "file:///libggml-htp-v%u.so?htp_iface_skel_handle_invoke&_modver=1.0&_dom=cdsp", opt_arch);
1709+
1710+
GGML_LOG_WARN("ggml-hex: failed to get URI for session %d : error 0x%x, fall back to %s\n", dev_id, err, session_uri);
17001711
}
17011712
}
17021713

0 commit comments

Comments
 (0)