39
39
40
40
#include " ggml-common.h"
41
41
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
-
105
42
/* *
106
43
* @brief Handles CANN errors by printing an error message and aborting.
107
44
*
@@ -116,10 +53,10 @@ static void ggml_cann_log(enum ggml_log_level level, const char* format, ...) {
116
53
int32_t id = -1 ;
117
54
aclrtGetDevice (&id);
118
55
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,
121
58
file, line);
122
- GGML_CANN_LOG_ERROR (" %s\n " , stmt);
59
+ GGML_LOG_ERROR (" %s\n " , stmt);
123
60
// abort with GGML_ASSERT to get a stack trace
124
61
GGML_ABORT (" CANN error" );
125
62
}
@@ -165,7 +102,7 @@ static ggml_cann_device_info ggml_cann_init() {
165
102
aclError err = aclrtGetDeviceCount ((uint32_t *)&info.device_count );
166
103
167
104
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 " ,
169
106
__func__, aclGetRecentErrMsg ());
170
107
return info;
171
108
}
@@ -315,7 +252,7 @@ struct ggml_cann_pool_leg : public ggml_cann_pool {
315
252
*actual_size = look_ahead_size;
316
253
pool_size += look_ahead_size;
317
254
#ifdef DEBUG_CANN_MALLOC
318
- GGML_CANN_LOG_INFO (
255
+ GGML_LOG_INFO (
319
256
" %s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, "
320
257
" requested %u MB\n " ,
321
258
__func__, device, nnz, (uint32_t )(max_size / 1024 / 1024 ),
@@ -470,7 +407,7 @@ struct ggml_cann_pool_vmm : public ggml_cann_pool {
470
407
// add to the pool
471
408
pool_size += reserve_size;
472
409
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 (
474
411
// reserved %llu MB)\n",
475
412
// device, (unsigned long long) (pool_size/1024/1024),
476
413
// (unsigned long long) (reserve_size/1024/1024));
@@ -483,7 +420,7 @@ struct ggml_cann_pool_vmm : public ggml_cann_pool {
483
420
pool_used += size;
484
421
485
422
#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,
487
424
(unsigned long long )size, (unsigned long long )ptr);
488
425
#endif
489
426
return ptr;
@@ -497,7 +434,7 @@ struct ggml_cann_pool_vmm : public ggml_cann_pool {
497
434
*/
498
435
void free (void * ptr, size_t size) override {
499
436
#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,
501
438
(unsigned long long )size, (unsigned long long )ptr);
502
439
#endif
503
440
@@ -1095,7 +1032,7 @@ ggml_backend_cann_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft,
1095
1032
void * dev_ptr;
1096
1033
aclError err = aclrtMalloc (&dev_ptr, size, ACL_MEM_MALLOC_HUGE_FIRST);
1097
1034
if (err != ACL_SUCCESS) {
1098
- GGML_CANN_LOG_ERROR (
1035
+ GGML_LOG_ERROR (
1099
1036
" %s: allocating %.2f MiB on device %d: aclrtMalloc failed: %s\n " ,
1100
1037
__func__, size / 1024.0 / 1024.0 , buft_ctx->device ,
1101
1038
aclGetRecentErrMsg ());
@@ -1280,7 +1217,7 @@ static void * ggml_cann_host_malloc(size_t size) {
1280
1217
aclError err = aclrtMallocHost ((void **) &hostPtr, size);
1281
1218
if (err != ACL_SUCCESS) {
1282
1219
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__,
1284
1221
size / 1024.0 / 1024.0 , aclGetRecentErrMsg ());
1285
1222
return nullptr ;
1286
1223
}
@@ -1733,7 +1670,7 @@ static enum ggml_status ggml_backend_cann_graph_compute(
1733
1670
bool ok = ggml_cann_compute_forward (*cann_ctx, node);
1734
1671
1735
1672
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__,
1737
1674
node->name , ggml_op_name (node->op ));
1738
1675
}
1739
1676
GGML_ASSERT (ok);
@@ -2043,13 +1980,13 @@ static ggml_guid_t ggml_backend_cann_guid() {
2043
1980
ggml_backend_t ggml_backend_cann_init (int32_t device) {
2044
1981
aclInit (nullptr );
2045
1982
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);
2047
1984
return nullptr ;
2048
1985
}
2049
1986
2050
1987
ggml_backend_cann_context* ctx = new ggml_backend_cann_context (device);
2051
1988
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__);
2053
1990
return nullptr ;
2054
1991
}
2055
1992
ggml_cann_set_device (ctx->device );
0 commit comments