Repository navigation
Expand file tree
/
Copy pathlogging.h
More file actions
383 lines (349 loc) · 15.3 KB
/
Copy pathlogging.h
File metadata and controls
383 lines (349 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Copyright (c) 2024-2026 Qualcomm Technologies, Inc. and/or its subsidiaries.
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
#define FMT_HEADER_ONLY
#ifndef FMT_USE_CONSTEVAL
#define FMT_USE_CONSTEVAL 0
#endif
#include <cstring>
#include "external/fmt/core.h"
#include "geniex.h"
GENIEX_API extern geniex_log_callback geniex_log;
GENIEX_API extern geniex_LogLevel geniex_log_level;
template <typename T>
inline auto lp(T arg) {
if constexpr (std::is_pointer_v<T>) {
if (arg == nullptr) {
return fmt::format("nullptr");
} else if constexpr (std::is_same_v<std::remove_cv_t<std::remove_pointer_t<T>>, char>) {
return fmt::format("{}", arg);
} else if constexpr (std::is_same_v<std::remove_cv_t<T>, void*> ||
std::is_same_v<std::remove_cv_t<T>, const void*>) {
return fmt::format("{}", static_cast<const void*>(arg));
} else {
return fmt::format("{}", *arg);
}
} else {
return fmt::format("{}", arg);
}
}
template <typename... Args>
void geniex_log_internal(geniex_LogLevel level, const char* file, int32_t line, const char* func,
fmt::format_string<Args...> fmt, Args&&... args) {
if (geniex_log == nullptr) return;
auto p = std::strstr(file, PROJECT_SOURCE_DIR);
auto filename = p ? p + std::strlen(PROJECT_SOURCE_DIR) + 1 : file;
geniex_log(level,
fmt::format("[{}:{}:{}] {}", filename, line, func, fmt::format(fmt, lp(std::forward<Args>(args))...)).c_str());
}
#define GENIEX_LEVEL_LOG(level, ...) \
do { \
if ((level) >= geniex_log_level && geniex_log != nullptr) { \
geniex_log_internal((level), __FILE__, __LINE__, __func__, __VA_ARGS__); \
} \
} while (0)
// All log macros forward to the registered callback; the embedder is
// responsible for any further filtering.
#define GENIEX_LOG_TRACE(...) GENIEX_LEVEL_LOG(GENIEX_LOG_LEVEL_TRACE, __VA_ARGS__)
#define GENIEX_LOG_DEBUG(...) GENIEX_LEVEL_LOG(GENIEX_LOG_LEVEL_DEBUG, __VA_ARGS__)
#define GENIEX_LOG_INFO(...) GENIEX_LEVEL_LOG(GENIEX_LOG_LEVEL_INFO, __VA_ARGS__)
#define GENIEX_LOG_WARN(...) GENIEX_LEVEL_LOG(GENIEX_LOG_LEVEL_WARN, __VA_ARGS__)
#define GENIEX_LOG_ERROR(...) GENIEX_LEVEL_LOG(GENIEX_LOG_LEVEL_ERROR, __VA_ARGS__)
template <>
struct fmt::formatter<geniex_LogLevel> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_LogLevel& p, fmt::format_context& ctx) const {
const char* level_str = "";
switch (p) {
case GENIEX_LOG_LEVEL_TRACE:
level_str = "TRACE";
break;
case GENIEX_LOG_LEVEL_DEBUG:
level_str = "DEBUG";
break;
case GENIEX_LOG_LEVEL_INFO:
level_str = "INFO";
break;
case GENIEX_LOG_LEVEL_WARN:
level_str = "WARN";
break;
case GENIEX_LOG_LEVEL_ERROR:
level_str = "ERROR";
break;
default:
level_str = "UNKNOWN";
break;
}
return fmt::format_to(ctx.out(), "LogLevel[{}]({})", lp(static_cast<int32_t>(p)), lp(level_str));
}
};
template <>
struct fmt::formatter<geniex_KvCacheSaveInput> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_KvCacheSaveInput& p, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(), "KvCacheSaveInput(path: {})", lp(p.path));
}
};
template <>
struct fmt::formatter<geniex_KvCacheSaveOutput> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_KvCacheSaveOutput& _, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(), "KvCacheSaveOutput()");
}
};
template <>
struct fmt::formatter<geniex_KvCacheLoadInput> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_KvCacheLoadInput& p, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(), "KvCacheLoadInput(path: {})", lp(p.path));
}
};
template <>
struct fmt::formatter<geniex_KvCacheLoadOutput> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_KvCacheLoadOutput& _, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(), "KvCacheLoadOutput()");
}
};
template <>
struct fmt::formatter<geniex_ErrorCode> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_ErrorCode& p, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(), "ErrorCode[{}]({})", static_cast<int32_t>(p), (geniex_get_error_message(p)));
}
};
template <>
struct fmt::formatter<geniex_GetPluginListOutput> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_GetPluginListOutput& p, fmt::format_context& ctx) const {
return fmt::format_to(
ctx.out(), "GetPluginListOutput(plugin_ids: {}, plugin_count: {})", lp(p.plugin_ids), lp(p.plugin_count));
}
};
template <>
struct fmt::formatter<geniex_GetDeviceListInput> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_GetDeviceListInput& p, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(), "GetDeviceListInput(plugin_id: {})", lp(p.plugin_id));
}
};
template <>
struct fmt::formatter<geniex_GetDeviceListOutput> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_GetDeviceListOutput& p, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(),
"GetDeviceListOutput(device_ids: {}, device_names: {}, device_count: {})",
lp(fmt::ptr(p.device_ids)),
lp(fmt::ptr(p.device_names)),
lp(p.device_count));
}
};
template <>
struct fmt::formatter<geniex_ProfileData> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_ProfileData& p, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(),
"ProfileData(ttft: {} us, prompt_time: {} us, decode_time: {} us, prompt_tokens: {}, "
"generated_tokens: {}, prefill_speed: {} tokens/s, decoding_speed: {} "
"tokens/s, stop_reason: {})",
lp(p.ttft),
lp(p.prompt_time),
lp(p.decode_time),
lp(p.prompt_tokens),
lp(p.generated_tokens),
lp(p.prefill_speed),
lp(p.decoding_speed),
lp(p.stop_reason));
}
};
template <>
struct fmt::formatter<geniex_SamplerConfig> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_SamplerConfig& p, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(),
"SamplerConfig(temperature: {}, top_p: {}, top_k: {}, min_p: {}, repetition_penalty: {}, presence_penalty: "
"{}, "
"frequency_penalty: {}, seed: {}, grammar_path: {}, grammar_string: {})",
lp(p.temperature),
lp(p.top_p),
lp(p.top_k),
lp(p.min_p),
lp(p.repetition_penalty),
lp(p.presence_penalty),
lp(p.frequency_penalty),
lp(p.seed),
lp(p.grammar_path),
lp(p.grammar_string));
}
};
template <>
struct fmt::formatter<geniex_GenerationConfig> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_GenerationConfig& p, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(),
"GenerationConfig(max_tokens: {}, stop_count: {}, sampler_config: {}, image_paths: {}, "
"image_count: {}, audio_paths: {}, audio_count: {})",
lp(p.max_tokens),
lp(p.stop_count),
lp(p.sampler_config),
lp(fmt::ptr(p.image_paths)),
lp(p.image_count),
lp(fmt::ptr(p.audio_paths)),
lp(p.audio_count));
}
};
template <>
struct fmt::formatter<geniex_ModelConfig> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_ModelConfig& p, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(),
"ModelConfig(n_ctx: {}, n_threads: {}, n_threads_batch: {}, n_batch: {}, n_ubatch: {}, n_seq_max: {}, "
"n_gpu_layers: {}, "
"chat_template_path: {}, chat_template_content: {})",
lp(p.n_ctx),
lp(p.n_threads),
lp(p.n_threads_batch),
lp(p.n_batch),
lp(p.n_ubatch),
lp(p.n_seq_max),
lp(p.n_gpu_layers),
lp(p.chat_template_path),
lp(p.chat_template_content));
}
};
// // LLM formatters
template <>
struct fmt::formatter<geniex_LlmCreateInput> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_LlmCreateInput& p, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(),
"LlmCreateInput(model_path: {}, tokenizer_path: {}, config: {}, plugin_id: {}, device_id: {})",
lp(p.model_path),
lp(p.tokenizer_path),
lp(p.config),
lp(p.plugin_id),
lp(p.device_id));
}
};
template <>
struct fmt::formatter<geniex_LlmApplyChatTemplateInput> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_LlmApplyChatTemplateInput& p, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(),
"LlmApplyChatTemplateInput(messages: {}, message_count: {}, tools: {}, enable_thinking: {})",
lp(fmt::ptr(p.messages)),
lp(p.message_count),
lp(p.tools),
lp(p.enable_thinking));
}
};
template <>
struct fmt::formatter<geniex_LlmChatMessage> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_LlmChatMessage& p, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(), "LlmChatMessage(role: {}, content: {})", lp(p.role), lp(p.content));
}
};
template <>
struct fmt::formatter<geniex_LlmApplyChatTemplateOutput> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_LlmApplyChatTemplateOutput& p, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(), "LlmApplyChatTemplateOutput(formatted_text: {})", lp(p.formatted_text));
}
};
template <>
struct fmt::formatter<geniex_LlmGenerateInput> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_LlmGenerateInput& p, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(),
"LlmGenerateInput(prompt_utf8: {}, input_ids: {}, input_ids_count: {}, config: {}, on_token: {}, "
"user_data: {})",
lp(p.prompt_utf8),
lp(fmt::ptr(p.input_ids)),
lp(p.input_ids_count),
lp(p.config),
lp(fmt::ptr(p.on_token)),
lp(fmt::ptr(p.user_data)));
}
};
template <>
struct fmt::formatter<geniex_LlmGenerateOutput> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_LlmGenerateOutput& p, fmt::format_context& ctx) const {
return fmt::format_to(
ctx.out(), "LlmGenerateOutput(full_text: {}, profile_data: {})", lp(p.full_text), p.profile_data);
}
};
// // VLM formatters
template <>
struct fmt::formatter<geniex_VlmContent> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_VlmContent& p, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(), "VlmContent(type: {}, text: {})", lp(p.type), lp(p.text));
}
};
template <>
struct fmt::formatter<geniex_VlmChatMessage> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_VlmChatMessage& p, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(),
"VlmChatMessage(role: {}, content_count: {}, contents: {})",
lp(p.role),
lp(p.content_count),
lp(fmt::ptr(p.contents)));
}
};
template <>
struct fmt::formatter<geniex_VlmCreateInput> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_VlmCreateInput& p, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(),
"VlmCreateInput(model_path: {}, mmproj_path: {}, config: {}, plugin_id: {}, device_id: {}, "
"tokenizer_path: {})",
lp(p.model_path),
lp(p.mmproj_path),
lp(p.config),
lp(p.plugin_id),
lp(p.device_id),
lp(p.tokenizer_path));
}
};
template <>
struct fmt::formatter<geniex_VlmApplyChatTemplateInput> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_VlmApplyChatTemplateInput& p, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(),
"VlmApplyChatTemplateInput(messages: {}, message_count: {}, tools: {}, enable_thinking: {})",
lp(fmt::ptr(p.messages)),
lp(p.message_count),
lp(p.tools),
lp(p.enable_thinking));
}
};
template <>
struct fmt::formatter<geniex_VlmApplyChatTemplateOutput> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_VlmApplyChatTemplateOutput& p, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(), "VlmApplyChatTemplateOutput(formatted_text: {})", lp(p.formatted_text));
}
};
template <>
struct fmt::formatter<geniex_VlmGenerateInput> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_VlmGenerateInput& p, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(),
"VlmGenerateInput(prompt_utf8: {}, config: {}, on_token: {}, user_data: {})",
lp(p.prompt_utf8),
lp(p.config),
lp(fmt::ptr(p.on_token)),
lp(fmt::ptr(p.user_data)));
}
};
template <>
struct fmt::formatter<geniex_VlmGenerateOutput> {
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
auto format(const geniex_VlmGenerateOutput& p, fmt::format_context& ctx) const {
return fmt::format_to(
ctx.out(), "VlmGenerateOutput(full_text: {}, profile_data: {})", lp(p.full_text), p.profile_data);
}
};