Skip to content

Commit 5b7c575

Browse files
committed
Add logging for CANN backend
1 parent 57197b7 commit 5b7c575

File tree

4 files changed

+94
-16
lines changed

4 files changed

+94
-16
lines changed

examples/llava/clip.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ struct clip_ctx * clip_model_load(const char * fname, const int verbosity = 1) {
10071007

10081008
#ifdef GGML_USE_CANN
10091009
new_clip->backend = ggml_backend_cann_init(0);
1010-
printf("%s: CLIP using CANN backend\n", __func__);
1010+
LOG_TEE("%s: CLIP using CANN backend\n", __func__);
10111011
#endif
10121012

10131013

ggml/include/ggml-cann.h

+11
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ GGML_API GGML_CALL void ggml_backend_cann_get_device_memory(int32_t device,
109109
size_t* free,
110110
size_t* total);
111111

112+
/**
113+
* @brief Set the logging callback for GGML.
114+
*
115+
* This function sets the logging callback and user data for logging.
116+
*
117+
* @param log_callback The logging callback to set.
118+
* @param user_data User data to pass to the logging callback.
119+
*/
120+
GGML_API void ggml_backend_cann_log_set_callback(ggml_log_callback log_callback,
121+
void* user_data);
122+
112123
#ifdef __cplusplus
113124
}
114125
#endif

ggml/src/ggml-cann.cpp

+78-15
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "ggml-cann.h"
2424

2525
#include <acl/acl.h>
26+
#include <stdarg.h>
2627

2728
#include <cmath>
2829
#include <cstdio>
@@ -37,6 +38,69 @@
3738

3839
#include "ggml-common.h"
3940

41+
/**
42+
* @brief Default logging callback for GGML.
43+
*
44+
* This function is the default logging callback that logs messages to stderr.
45+
*
46+
* @param level The log level.
47+
* @param msg The log message.
48+
* @param user_data User data passed to the callback.
49+
*/
50+
static void ggml_cann_default_log_callback(enum ggml_log_level level,
51+
const char* msg, void* user_data) {
52+
GGML_UNUSED(level);
53+
GGML_UNUSED(user_data);
54+
fprintf(stderr, "%s", msg);
55+
}
56+
57+
ggml_log_callback ggml_cann_log_callback = ggml_cann_default_log_callback;
58+
void* ggml_cann_log_user_data = NULL;
59+
60+
GGML_API void ggml_backend_cann_log_set_callback(ggml_log_callback log_callback,
61+
void* user_data) {
62+
ggml_cann_log_callback = log_callback;
63+
ggml_cann_log_user_data = user_data;
64+
}
65+
66+
#define GGML_CANN_LOG_INFO(...) ggml_cann_log(GGML_LOG_LEVEL_INFO, __VA_ARGS__)
67+
#define GGML_CANN_LOG_WARN(...) ggml_cann_log(GGML_LOG_LEVEL_WARN, __VA_ARGS__)
68+
#define GGML_CANN_LOG_ERROR(...) \
69+
ggml_cann_log(GGML_LOG_LEVEL_ERROR, __VA_ARGS__)
70+
71+
GGML_ATTRIBUTE_FORMAT(2, 3)
72+
73+
/**
74+
* @brief Log a message using the current logging callback.
75+
*
76+
* This function formats a log message and passes it to the current logging
77+
* callback.
78+
*
79+
* @param level The log level.
80+
* @param format The format string for the log message.
81+
* @param ... The arguments for the format string.
82+
*/
83+
static void ggml_cann_log(enum ggml_log_level level, const char* format, ...) {
84+
if (ggml_cann_log_callback != NULL) {
85+
va_list args;
86+
va_start(args, format);
87+
char buffer[128];
88+
int len = vsnprintf(buffer, 128, format, args);
89+
if (len < 128) {
90+
ggml_cann_log_callback(level, buffer, ggml_cann_log_user_data);
91+
} else {
92+
// vsnprintf adds a null terminator
93+
std::vector<char> buffer2(len + 1);
94+
va_end(args);
95+
va_start(args, format);
96+
vsnprintf(&buffer2[0], buffer2.size(), format, args);
97+
ggml_cann_log_callback(level, buffer2.data(),
98+
ggml_cann_log_user_data);
99+
}
100+
va_end(args);
101+
}
102+
}
103+
40104
/**
41105
* @brief Handles CANN errors by printing an error message and aborting.
42106
*
@@ -51,10 +115,10 @@
51115
int32_t id = -1;
52116
aclrtGetDevice(&id);
53117

54-
fprintf(stderr, "CANN error: %s\n", msg);
55-
fprintf(stderr, " current device: %d, in function %s at %s:%d\n", id, func,
118+
GGML_CANN_LOG_ERROR("CANN error: %s\n", msg);
119+
GGML_CANN_LOG_ERROR(" current device: %d, in function %s at %s:%d\n", id, func,
56120
file, line);
57-
fprintf(stderr, " %s\n", stmt);
121+
GGML_CANN_LOG_ERROR(" %s\n", stmt);
58122
// abort with GGML_ASSERT to get a stack trace
59123
GGML_ASSERT(!"CANN error");
60124
}
@@ -100,7 +164,7 @@ static ggml_cann_device_info ggml_cann_init() {
100164
aclError err = aclrtGetDeviceCount((uint32_t*)&info.device_count);
101165

102166
if (err != ACL_SUCCESS) {
103-
fprintf(stderr, "%s: failed to initialize CANN: %s\n",
167+
GGML_CANN_LOG_ERROR("%s: failed to initialize CANN: %s\n",
104168
__func__, aclGetRecentErrMsg());
105169
return info;
106170
}
@@ -138,7 +202,7 @@ const ggml_cann_device_info& ggml_cann_info() {
138202
return info;
139203
}
140204

141-
// #define DEBUG_CANN_MALLOC
205+
//#define DEBUG_CANN_MALLOC
142206
/**
143207
* @brief A pool of CANN buffers(legacy).
144208
*
@@ -250,7 +314,7 @@ struct ggml_cann_pool_leg : public ggml_cann_pool {
250314
*actual_size = look_ahead_size;
251315
pool_size += look_ahead_size;
252316
#ifdef DEBUG_CANN_MALLOC
253-
printf(
317+
GGML_CANN_LOG_INFO(
254318
"%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, "
255319
"requested %u MB\n",
256320
__func__, device, nnz, (uint32_t)(max_size / 1024 / 1024),
@@ -405,8 +469,8 @@ struct ggml_cann_pool_vmm : public ggml_cann_pool {
405469
// add to the pool
406470
pool_size += reserve_size;
407471

408-
// printf("cann pool[%d]: size increased to %llu MB (reserved %llu
409-
// MB)\n",
472+
// GGML_CANN_LOG_INFO("cann pool[%d]: size increased to %llu MB (
473+
// reserved %llu MB)\n",
410474
// device, (unsigned long long) (pool_size/1024/1024),
411475
// (unsigned long long) (reserve_size/1024/1024));
412476
}
@@ -418,7 +482,7 @@ struct ggml_cann_pool_vmm : public ggml_cann_pool {
418482
pool_used += size;
419483

420484
#ifdef DEBUG_CANN_MALLOC
421-
printf("cann pool[%d]: allocated %llu bytes at %llx\n", device,
485+
GGML_CANN_LOG_INFO("cann pool[%d]: allocated %llu bytes at %llx\n", device,
422486
(unsigned long long)size, (unsigned long long)ptr);
423487
#endif
424488
return ptr;
@@ -432,7 +496,7 @@ struct ggml_cann_pool_vmm : public ggml_cann_pool {
432496
*/
433497
void free(void* ptr, size_t size) override {
434498
#ifdef DEBUG_CANN_MALLOC
435-
printf("cann pool[%d]: freed %llu bytes at %llx\n", device,
499+
GGML_CANN_LOG_INFO("cann pool[%d]: freed %llu bytes at %llx\n", device,
436500
(unsigned long long)size, (unsigned long long)ptr);
437501
#endif
438502

@@ -1034,8 +1098,7 @@ ggml_backend_cann_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft,
10341098
void* dev_ptr;
10351099
aclError err = aclrtMalloc(&dev_ptr, size, ACL_MEM_MALLOC_HUGE_FIRST);
10361100
if (err != ACL_SUCCESS) {
1037-
fprintf(
1038-
stderr,
1101+
GGML_CANN_LOG_ERROR(
10391102
"%s: allocating %.2f MiB on device %d: aclrtMalloc failed: %s\n",
10401103
__func__, size / 1024.0 / 1024.0, buft_ctx->device,
10411104
aclGetRecentErrMsg());
@@ -1569,7 +1632,7 @@ GGML_CALL static enum ggml_status ggml_backend_cann_graph_compute(
15691632
bool ok = ggml_cann_compute_forward(*cann_ctx, node);
15701633

15711634
if (!ok) {
1572-
fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__,
1635+
GGML_CANN_LOG_ERROR("%s: error: op not supported %s (%s)\n", __func__,
15731636
node->name, ggml_op_name(node->op));
15741637
}
15751638
GGML_ASSERT(ok);
@@ -1873,13 +1936,13 @@ static ggml_guid_t ggml_backend_cann_guid() {
18731936
GGML_CALL ggml_backend_t ggml_backend_cann_init(int32_t device) {
18741937
aclInit(nullptr);
18751938
if (device < 0 || device >= ggml_backend_cann_get_device_count()) {
1876-
fprintf(stderr, "%s: error: invalid device %d\n", __func__, device);
1939+
GGML_CANN_LOG_ERROR("%s: error: invalid device %d\n", __func__, device);
18771940
return nullptr;
18781941
}
18791942

18801943
ggml_backend_cann_context* ctx = new ggml_backend_cann_context(device);
18811944
if (ctx == nullptr) {
1882-
fprintf(stderr, "%s: error: failed to allocate context\n", __func__);
1945+
GGML_CANN_LOG_ERROR("%s: error: failed to allocate context\n", __func__);
18831946
return nullptr;
18841947
}
18851948

src/llama.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -2081,6 +2081,8 @@ struct llama_state {
20812081
ggml_backend_metal_log_set_callback(log_callback, log_callback_user_data);
20822082
#elif defined(GGML_USE_CUDA)
20832083
ggml_backend_cuda_log_set_callback(log_callback, log_callback_user_data);
2084+
#elif defined(GGML_USE_CANN)
2085+
ggml_backend_cann_log_set_callback(log_callback, log_callback_user_data);
20842086
#endif
20852087
}
20862088

@@ -21826,6 +21828,8 @@ void llama_log_set(ggml_log_callback log_callback, void * user_data) {
2182621828
ggml_backend_metal_log_set_callback(g_state.log_callback, g_state.log_callback_user_data);
2182721829
#elif defined(GGML_USE_CUDA)
2182821830
ggml_backend_cuda_log_set_callback(g_state.log_callback, g_state.log_callback_user_data);
21831+
#elif defined(GGML_USE_CANN)
21832+
ggml_backend_cann_log_set_callback(g_state.log_callback, g_state.log_callback_user_data);
2182921833
#endif
2183021834
}
2183121835

0 commit comments

Comments
 (0)