3939
4040#include " ggml-common.h"
4141
42- /* *
43- * @brief Default logging callback for GGML.
44- *
45- * This function is the default logging callback that logs messages to stderr.
46- *
47- * @param level The log level.
48- * @param msg The log message.
49- * @param user_data User data passed to the callback.
50- */
51- static void ggml_cann_default_log_callback (enum ggml_log_level level,
52- const char * msg, void * user_data) {
53- GGML_UNUSED (level);
54- GGML_UNUSED (user_data);
55- fprintf (stderr, " %s" , msg);
56- }
57-
58- ggml_log_callback ggml_cann_log_callback = ggml_cann_default_log_callback;
59- void * ggml_cann_log_user_data = NULL ;
60-
61- GGML_API void ggml_backend_cann_log_set_callback (ggml_log_callback log_callback,
62- void * user_data) {
63- ggml_cann_log_callback = log_callback;
64- ggml_cann_log_user_data = user_data;
65- }
66-
67- #define GGML_CANN_LOG_INFO (...) ggml_cann_log(GGML_LOG_LEVEL_INFO, __VA_ARGS__)
68- #define GGML_CANN_LOG_WARN (...) ggml_cann_log(GGML_LOG_LEVEL_WARN, __VA_ARGS__)
69- #define GGML_CANN_LOG_ERROR (...) \
70- ggml_cann_log (GGML_LOG_LEVEL_ERROR, __VA_ARGS__)
71-
72- GGML_ATTRIBUTE_FORMAT(2 , 3 )
73-
74- /* *
75- * @brief Log a message using the current logging callback.
76- *
77- * This function formats a log message and passes it to the current logging
78- * callback.
79- *
80- * @param level The log level.
81- * @param format The format string for the log message.
82- * @param ... The arguments for the format string.
83- */
84- static void ggml_cann_log(enum ggml_log_level level, const char * format, ...) {
85- if (ggml_cann_log_callback != NULL ) {
86- va_list args;
87- va_start (args, format);
88- char buffer[128 ];
89- int len = vsnprintf (buffer, 128 , format, args);
90- if (len < 128 ) {
91- ggml_cann_log_callback (level, buffer, ggml_cann_log_user_data);
92- } else {
93- // vsnprintf adds a null terminator
94- std::vector<char > buffer2 (len + 1 );
95- va_end (args);
96- va_start (args, format);
97- vsnprintf (&buffer2[0 ], buffer2.size (), format, args);
98- ggml_cann_log_callback (level, buffer2.data (),
99- ggml_cann_log_user_data);
100- }
101- va_end (args);
102- }
103- }
104-
10542/* *
10643 * @brief Handles CANN errors by printing an error message and aborting.
10744 *
@@ -116,10 +53,10 @@ static void ggml_cann_log(enum ggml_log_level level, const char* format, ...) {
11653 int32_t id = -1 ;
11754 aclrtGetDevice (&id);
11855
119- GGML_CANN_LOG_ERROR (" CANN error: %s\n " , msg);
120- GGML_CANN_LOG_ERROR (" current device: %d, in function %s at %s:%d\n " , id, func,
56+ GGML_LOG_ERROR (" CANN error: %s\n " , msg);
57+ GGML_LOG_ERROR (" current device: %d, in function %s at %s:%d\n " , id, func,
12158 file, line);
122- GGML_CANN_LOG_ERROR (" %s\n " , stmt);
59+ GGML_LOG_ERROR (" %s\n " , stmt);
12360 // abort with GGML_ASSERT to get a stack trace
12461 GGML_ABORT (" CANN error" );
12562}
@@ -165,7 +102,7 @@ static ggml_cann_device_info ggml_cann_init() {
165102 aclError err = aclrtGetDeviceCount ((uint32_t *)&info.device_count );
166103
167104 if (err != ACL_SUCCESS) {
168- GGML_CANN_LOG_ERROR (" %s: failed to initialize CANN: %s\n " ,
105+ GGML_LOG_ERROR (" %s: failed to initialize CANN: %s\n " ,
169106 __func__, aclGetRecentErrMsg ());
170107 return info;
171108 }
@@ -315,7 +252,7 @@ struct ggml_cann_pool_leg : public ggml_cann_pool {
315252 *actual_size = look_ahead_size;
316253 pool_size += look_ahead_size;
317254#ifdef DEBUG_CANN_MALLOC
318- GGML_CANN_LOG_INFO (
255+ GGML_LOG_INFO (
319256 " %s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, "
320257 " requested %u MB\n " ,
321258 __func__, device, nnz, (uint32_t )(max_size / 1024 / 1024 ),
@@ -470,7 +407,7 @@ struct ggml_cann_pool_vmm : public ggml_cann_pool {
470407 // add to the pool
471408 pool_size += reserve_size;
472409
473- // GGML_CANN_LOG_INFO ("cann pool[%d]: size increased to %llu MB (
410+ // GGML_LOG_INFO ("cann pool[%d]: size increased to %llu MB (
474411 // reserved %llu MB)\n",
475412 // device, (unsigned long long) (pool_size/1024/1024),
476413 // (unsigned long long) (reserve_size/1024/1024));
@@ -483,7 +420,7 @@ struct ggml_cann_pool_vmm : public ggml_cann_pool {
483420 pool_used += size;
484421
485422#ifdef DEBUG_CANN_MALLOC
486- GGML_CANN_LOG_INFO (" cann pool[%d]: allocated %llu bytes at %llx\n " , device,
423+ GGML_LOG_INFO (" cann pool[%d]: allocated %llu bytes at %llx\n " , device,
487424 (unsigned long long )size, (unsigned long long )ptr);
488425#endif
489426 return ptr;
@@ -497,7 +434,7 @@ struct ggml_cann_pool_vmm : public ggml_cann_pool {
497434 */
498435 void free (void * ptr, size_t size) override {
499436#ifdef DEBUG_CANN_MALLOC
500- GGML_CANN_LOG_INFO (" cann pool[%d]: freed %llu bytes at %llx\n " , device,
437+ GGML_LOG_INFO (" cann pool[%d]: freed %llu bytes at %llx\n " , device,
501438 (unsigned long long )size, (unsigned long long )ptr);
502439#endif
503440
@@ -1095,7 +1032,7 @@ ggml_backend_cann_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft,
10951032 void * dev_ptr;
10961033 aclError err = aclrtMalloc (&dev_ptr, size, ACL_MEM_MALLOC_HUGE_FIRST);
10971034 if (err != ACL_SUCCESS) {
1098- GGML_CANN_LOG_ERROR (
1035+ GGML_LOG_ERROR (
10991036 " %s: allocating %.2f MiB on device %d: aclrtMalloc failed: %s\n " ,
11001037 __func__, size / 1024.0 / 1024.0 , buft_ctx->device ,
11011038 aclGetRecentErrMsg ());
@@ -1280,7 +1217,7 @@ static void * ggml_cann_host_malloc(size_t size) {
12801217 aclError err = aclrtMallocHost ((void **) &hostPtr, size);
12811218 if (err != ACL_SUCCESS) {
12821219
1283- GGML_CANN_LOG_WARN (" %s: failed to allocate %.2f MiB of pinned memory: %s\n " , __func__,
1220+ GGML_LOG_WARN (" %s: failed to allocate %.2f MiB of pinned memory: %s\n " , __func__,
12841221 size / 1024.0 / 1024.0 , aclGetRecentErrMsg ());
12851222 return nullptr ;
12861223 }
@@ -1733,7 +1670,7 @@ static enum ggml_status ggml_backend_cann_graph_compute(
17331670 bool ok = ggml_cann_compute_forward (*cann_ctx, node);
17341671
17351672 if (!ok) {
1736- GGML_CANN_LOG_ERROR (" %s: error: op not supported %s (%s)\n " , __func__,
1673+ GGML_LOG_ERROR (" %s: error: op not supported %s (%s)\n " , __func__,
17371674 node->name , ggml_op_name (node->op ));
17381675 }
17391676 GGML_ASSERT (ok);
@@ -2043,13 +1980,13 @@ static ggml_guid_t ggml_backend_cann_guid() {
20431980ggml_backend_t ggml_backend_cann_init (int32_t device) {
20441981 aclInit (nullptr );
20451982 if (device < 0 || device >= ggml_backend_cann_get_device_count ()) {
2046- GGML_CANN_LOG_ERROR (" %s: error: invalid device %d\n " , __func__, device);
1983+ GGML_LOG_ERROR (" %s: error: invalid device %d\n " , __func__, device);
20471984 return nullptr ;
20481985 }
20491986
20501987 ggml_backend_cann_context* ctx = new ggml_backend_cann_context (device);
20511988 if (ctx == nullptr ) {
2052- GGML_CANN_LOG_ERROR (" %s: error: failed to allocate context\n " , __func__);
1989+ GGML_LOG_ERROR (" %s: error: failed to allocate context\n " , __func__);
20531990 return nullptr ;
20541991 }
20551992 ggml_cann_set_device (ctx->device );
0 commit comments