23
23
#include " ggml-cann.h"
24
24
25
25
#include < acl/acl.h>
26
+ #include < stdarg.h>
26
27
27
28
#include < cmath>
28
29
#include < cstdio>
37
38
38
39
#include " ggml-common.h"
39
40
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
+
40
104
/* *
41
105
* @brief Handles CANN errors by printing an error message and aborting.
42
106
*
51
115
int32_t id = -1 ;
52
116
aclrtGetDevice (&id);
53
117
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,
56
120
file, line);
57
- fprintf (stderr, " %s\n " , stmt);
121
+ GGML_CANN_LOG_ERROR ( " %s\n " , stmt);
58
122
// abort with GGML_ASSERT to get a stack trace
59
123
GGML_ASSERT (!" CANN error" );
60
124
}
@@ -100,7 +164,7 @@ static ggml_cann_device_info ggml_cann_init() {
100
164
aclError err = aclrtGetDeviceCount ((uint32_t *)&info.device_count );
101
165
102
166
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 " ,
104
168
__func__, aclGetRecentErrMsg ());
105
169
return info;
106
170
}
@@ -138,7 +202,7 @@ const ggml_cann_device_info& ggml_cann_info() {
138
202
return info;
139
203
}
140
204
141
- // #define DEBUG_CANN_MALLOC
205
+ // #define DEBUG_CANN_MALLOC
142
206
/* *
143
207
* @brief A pool of CANN buffers(legacy).
144
208
*
@@ -250,7 +314,7 @@ struct ggml_cann_pool_leg : public ggml_cann_pool {
250
314
*actual_size = look_ahead_size;
251
315
pool_size += look_ahead_size;
252
316
#ifdef DEBUG_CANN_MALLOC
253
- printf (
317
+ GGML_CANN_LOG_INFO (
254
318
" %s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, "
255
319
" requested %u MB\n " ,
256
320
__func__, device, nnz, (uint32_t )(max_size / 1024 / 1024 ),
@@ -405,8 +469,8 @@ struct ggml_cann_pool_vmm : public ggml_cann_pool {
405
469
// add to the pool
406
470
pool_size += reserve_size;
407
471
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",
410
474
// device, (unsigned long long) (pool_size/1024/1024),
411
475
// (unsigned long long) (reserve_size/1024/1024));
412
476
}
@@ -418,7 +482,7 @@ struct ggml_cann_pool_vmm : public ggml_cann_pool {
418
482
pool_used += size;
419
483
420
484
#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,
422
486
(unsigned long long )size, (unsigned long long )ptr);
423
487
#endif
424
488
return ptr;
@@ -432,7 +496,7 @@ struct ggml_cann_pool_vmm : public ggml_cann_pool {
432
496
*/
433
497
void free (void * ptr, size_t size) override {
434
498
#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,
436
500
(unsigned long long )size, (unsigned long long )ptr);
437
501
#endif
438
502
@@ -1034,8 +1098,7 @@ ggml_backend_cann_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft,
1034
1098
void * dev_ptr;
1035
1099
aclError err = aclrtMalloc (&dev_ptr, size, ACL_MEM_MALLOC_HUGE_FIRST);
1036
1100
if (err != ACL_SUCCESS) {
1037
- fprintf (
1038
- stderr,
1101
+ GGML_CANN_LOG_ERROR (
1039
1102
" %s: allocating %.2f MiB on device %d: aclrtMalloc failed: %s\n " ,
1040
1103
__func__, size / 1024.0 / 1024.0 , buft_ctx->device ,
1041
1104
aclGetRecentErrMsg ());
@@ -1569,7 +1632,7 @@ GGML_CALL static enum ggml_status ggml_backend_cann_graph_compute(
1569
1632
bool ok = ggml_cann_compute_forward (*cann_ctx, node);
1570
1633
1571
1634
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__,
1573
1636
node->name , ggml_op_name (node->op ));
1574
1637
}
1575
1638
GGML_ASSERT (ok);
@@ -1873,13 +1936,13 @@ static ggml_guid_t ggml_backend_cann_guid() {
1873
1936
GGML_CALL ggml_backend_t ggml_backend_cann_init (int32_t device) {
1874
1937
aclInit (nullptr );
1875
1938
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);
1877
1940
return nullptr ;
1878
1941
}
1879
1942
1880
1943
ggml_backend_cann_context* ctx = new ggml_backend_cann_context (device);
1881
1944
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__);
1883
1946
return nullptr ;
1884
1947
}
1885
1948
0 commit comments