Skip to content

Commit 6db3d1f

Browse files
authored
ggml-hexagon: graceful fallback for older socs where rpcmem_alloc2 and FASTRPC_GET_URI is unsupported (ggml-org#16987)
* support older socs where FASTRPC_GET_URI is unsupported * added graceful fallback when FASTRPC_GET_URI call fails * use weak symbols instead of loading libcdsprpc.so dynamically * Add weak pragma for rpcmem_alloc2 * Remove weak declaration for rpcmem_alloc2 in ggml-hexagon.cpp Removed weak declaration for rpcmem_alloc2. * Enforce ndev to 1 for archs below v75 Force ndev to 1 for SoCs architectures lower than v75.
1 parent 230d116 commit 6db3d1f

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,13 @@ struct ggml_backend_hexagon_buffer_context {
367367
ggml_backend_hexagon_buffer_context(ggml_hexagon_session * sess, size_t size, bool repack) {
368368
size += 4 * 1024; // extra page for padding
369369

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

16811687
// 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);
16841688

16851689
char session_uri[256];
16861690
{
1687-
struct remote_rpc_get_uri u;
1691+
char htp_uri[256];
1692+
snprintf(htp_uri, sizeof(htp_uri), "file:///libggml-htp-v%u.so?htp_iface_skel_handle_invoke&_modver=1.0", opt_arch);
1693+
1694+
struct remote_rpc_get_uri u = {};
16881695
u.session_id = this->session_id;
16891696
u.domain_name = const_cast<char *>(CDSP_DOMAIN_NAME);
16901697
u.domain_name_len = strlen(CDSP_DOMAIN_NAME);
@@ -1695,8 +1702,12 @@ void ggml_hexagon_session::allocate(int dev_id) noexcept(false) {
16951702

16961703
int err = remote_session_control(FASTRPC_GET_URI, (void *) &u, sizeof(u));
16971704
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)");
1705+
// fallback to single session uris
1706+
int htp_URI_domain_len = strlen(htp_uri) + MAX_DOMAIN_NAMELEN;
1707+
1708+
snprintf(session_uri, htp_URI_domain_len, "%s%s", htp_uri, my_domain->uri);
1709+
1710+
GGML_LOG_WARN("ggml-hex: failed to get URI for session %d : error 0x%x. Falling back to single session URI: %s\n", dev_id, err, session_uri);
17001711
}
17011712
}
17021713

@@ -3668,6 +3679,11 @@ ggml_hexagon_registry::ggml_hexagon_registry(ggml_backend_reg_t reg) {
36683679
}
36693680
}
36703681

3682+
if(opt_arch < 75) {
3683+
opt_ndev = 1;
3684+
GGML_LOG_WARN("ggml-hex: forcing ndev to 1 for SoCs archs lower than v75.\n");
3685+
}
3686+
36713687
GGML_LOG_INFO("ggml-hex: Hexagon Arch version v%d\n", opt_arch);
36723688

36733689
// Create devices / sessions

ggml/src/ggml-hexagon/htp-utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ extern "C" {
6464
# pragma weak remote_handle64_control
6565
# pragma weak fastrpc_mmap
6666
# pragma weak fastrpc_munmap
67+
# pragma weak rpcmem_alloc2
6768
#endif
6869

6970
#if !defined(_WINDOWS)

0 commit comments

Comments
 (0)