Skip to content

Commit 7fa7374

Browse files
authored
Merge pull request #19 from kpouget/remoting
remotingfrontend: detect initialization issue and improve the logging
2 parents 646db23 + 2afc692 commit 7fa7374

File tree

4 files changed

+63
-30
lines changed

4 files changed

+63
-30
lines changed

ggml/src/ggml-remotingbackend/backend-dispatched-metal.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ void (*ggml_backend_metal_get_device_context_fct)(ggml_backend_dev_t dev,
1414
uint32_t
1515
backend_metal_get_device_context(struct vn_cs_encoder *enc, struct vn_cs_decoder *dec, struct virgl_apir_context *ctx) {
1616
UNUSED(ctx);
17+
UNUSED(dec);
1718

1819
bool has_simdgroup_mm;
1920
bool has_simdgroup_reduction;

ggml/src/ggml-remotingbackend/backend-utils.h

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,55 @@
77
#include <ggml.h>
88

99
#define UNUSED GGML_UNUSED
10+
#define APIR_LLAMA_CPP_LOG_TO_FILE_ENV "APIR_LLAMA_CPP_LOG_TO_FILE"
11+
12+
static FILE *
13+
get_log_dest(void)
14+
{
15+
static FILE *dest = NULL;
16+
if (dest) {
17+
return dest;
18+
}
19+
const char *apir_log_to_file = getenv(APIR_LLAMA_CPP_LOG_TO_FILE_ENV);
20+
if (!apir_log_to_file) {
21+
dest = stderr;
22+
return dest;
23+
}
24+
25+
dest = fopen(apir_log_to_file, "w");
26+
27+
return dest;
28+
}
29+
30+
#define APIR_VA_PRINT(prefix, format) \
31+
do { \
32+
FILE *dest = get_log_dest(); \
33+
fprintf(dest, prefix); \
34+
va_list argptr; \
35+
va_start(argptr, format); \
36+
vfprintf(dest, format, argptr); \
37+
fprintf(dest, "\n"); \
38+
va_end(argptr); \
39+
fflush(dest); \
40+
} while (0)
1041

1142
inline void
1243
INFO(const char *format, ...) {
13-
va_list argptr;
14-
va_start(argptr, format);
15-
vfprintf(stderr, format, argptr);
16-
fprintf(stderr, "\n");
17-
va_end(argptr);
44+
APIR_VA_PRINT("INFO: ", format);
1845
}
1946

2047
inline void
2148
WARNING(const char *format, ...) {
22-
fprintf(stderr, "WARNING: ");
23-
24-
va_list argptr;
25-
va_start(argptr, format);
26-
vfprintf(stderr, format, argptr);
27-
fprintf(stderr, "\n");
28-
va_end(argptr);
49+
APIR_VA_PRINT("WARNING: ", format);
2950
}
3051

3152
inline void
3253
ERROR(const char *format, ...) {
33-
fprintf(stderr, "ERROR: ");
34-
35-
va_list argptr;
36-
va_start(argptr, format);
37-
vfprintf(stderr, format, argptr);
38-
fprintf(stderr, "\n");
39-
va_end(argptr);
54+
APIR_VA_PRINT("ERROR: ", format);
4055
}
4156

42-
inline void
57+
[[noreturn]] inline void
4358
FATAL(const char *format, ...) {
44-
fprintf(stderr, "FATAL: ");
45-
46-
va_list argptr;
47-
va_start(argptr, format);
48-
vfprintf(stderr, format, argptr);
49-
fprintf(stderr, "\n");
50-
va_end(argptr);
51-
if (format)
52-
assert(false);
59+
APIR_VA_PRINT("FORMAT: ", format);
60+
abort();
5361
}

ggml/src/ggml-remotingbackend/shared/apir_backend.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define APIR_BACKEND_INITIALIZE_MISSING_BACKEND_SYMBOLS 3
77
#define APIR_BACKEND_INITIALIZE_MISSING_GGML_SYMBOLS 4
88
#define APIR_BACKEND_INITIALIZE_BACKEND_FAILED 5
9+
// new entries here need to be added to the apir_backend_initialize_error function below
910

1011
#define APIR_BACKEND_FORWARD_INDEX_INVALID 6
1112

@@ -106,3 +107,21 @@ static inline void show_timer(struct timer_data *timer) {
106107
INFO("%14s [%9.0f] ms for %4ld invocations | ITL %2.2f ms | throughput = %4.2f t/s",
107108
timer->name, ms, timer->count, itl, speed);
108109
}
110+
111+
static const char *apir_backend_initialize_error(int code) {
112+
#define APIR_BACKEND_INITIALIZE_ERROR(code_name) \
113+
do { \
114+
if (code == code_name) return #code_name; \
115+
} while (0) \
116+
117+
APIR_BACKEND_INITIALIZE_ERROR(APIR_BACKEND_INITIALIZE_SUCCESSS);
118+
APIR_BACKEND_INITIALIZE_ERROR(APIR_BACKEND_INITIALIZE_CANNOT_OPEN_BACKEND_LIBRARY);
119+
APIR_BACKEND_INITIALIZE_ERROR(APIR_BACKEND_INITIALIZE_CANNOT_OPEN_GGML_LIBRARY);
120+
APIR_BACKEND_INITIALIZE_ERROR(APIR_BACKEND_INITIALIZE_MISSING_BACKEND_SYMBOLS);
121+
APIR_BACKEND_INITIALIZE_ERROR(APIR_BACKEND_INITIALIZE_MISSING_GGML_SYMBOLS);
122+
APIR_BACKEND_INITIALIZE_ERROR(APIR_BACKEND_INITIALIZE_BACKEND_FAILED);
123+
124+
return "Unknown APIR_BACKEND_INITIALIZE error:/";
125+
126+
#undef APIR_BACKEND_INITIALIZE_ERROR
127+
}

ggml/src/ggml-remotingfrontend/virtgpu.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,12 @@ create_virtgpu() {
8282

8383
if (!gpu->reply_shmem) {
8484
FATAL("%s: failed to create the shared reply memory pages :/", __func__);
85+
return NULL;
8586
}
8687

8788
if (!gpu->data_shmem) {
8889
FATAL("%s: failed to create the shared data memory pages :/", __func__);
90+
return NULL;
8991
}
9092

9193
struct vn_cs_encoder *encoder;
@@ -100,11 +102,14 @@ create_virtgpu() {
100102
decoder = remote_call(gpu, encoder, MAX_WAIT_US);
101103
if (!decoder) {
102104
FATAL("%s: failed to initialize the API remoting libraries. :/", __func__);
105+
return NULL;
103106
}
104107

105108
ret = remote_call_finish(encoder, decoder);
106109
if (ret != 0) {
107-
FATAL("%s: failed to load the APIR backend libraries (code=%d):/", __func__, ret);
110+
FATAL("%s: failed to load the APIR backend libraries (code=%d | %s):/",
111+
__func__, ret, apir_backend_initialize_error(ret));
112+
return NULL;
108113
}
109114

110115
return gpu;

0 commit comments

Comments
 (0)