-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathml.cpp
More file actions
235 lines (199 loc) · 7.45 KB
/
Copy pathml.cpp
File metadata and controls
235 lines (199 loc) · 7.45 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
// Copyright (c) 2024-2026 Qualcomm Technologies, Inc. and/or its subsidiaries.
// SPDX-License-Identifier: BSD-3-Clause
#include <stdlib.h>
#include "geniex.h"
#if defined(_WIN32)
#define portable_strdup _strdup
#else
#define portable_strdup strdup
#endif
#include <cstdlib>
#include <iostream>
#include "build_config.h"
#include "logging.h"
#include "registry.h"
#include "utils.h"
#ifdef _WIN32
#include <windows.h>
#endif
// Baseline armv8.0 boards (e.g. unoq) lack the armv8.2 features this build bakes
// in, so bail cleanly instead of SIGILL. Keep in sync with -march. See #1217.
#if defined(__linux__) && defined(__aarch64__)
#include <asm/hwcap.h>
#include <sys/auxv.h>
static bool cpu_features_supported() {
unsigned long need = HWCAP_ATOMICS | HWCAP_ASIMDRDM | HWCAP_ASIMDDP | HWCAP_FPHP | HWCAP_ASIMDHP | HWCAP_CRC32;
return (getauxval(AT_HWCAP) & need) == need;
}
#else
static bool cpu_features_supported() { return true; }
#endif
using namespace geniex;
// Default log handler — always compiled. Emits to stderr with optional ANSI
// coloring (disabled when NO_COLOR is set). Filtering is the embedder's job.
static void default_log_handler(geniex_LogLevel level, const char* msg) {
const char* prefix;
const char* colorCode;
switch (level) {
case GENIEX_LOG_LEVEL_TRACE:
prefix = "[TRACE] ";
colorCode = "\033[90m";
break;
case GENIEX_LOG_LEVEL_DEBUG:
prefix = "[DEBUG] ";
colorCode = "\033[34m";
break;
case GENIEX_LOG_LEVEL_INFO:
prefix = "[ INFO] ";
colorCode = "\033[32m";
break;
case GENIEX_LOG_LEVEL_WARN:
prefix = "[ WARN] ";
colorCode = "\033[33m";
break;
case GENIEX_LOG_LEVEL_ERROR:
prefix = "[ERROR] ";
colorCode = "\033[31m";
break;
default:
return;
}
std::cerr << colorCode << prefix << msg << "\033[0m" << std::endl;
}
int32_t geniex_init(void) {
#ifdef _WIN32
// set console output to UTF-8 code page for Windows
SetConsoleOutputCP(CP_UTF8);
#endif
GENIEX_LOG_DEBUG("initializing ml");
// Bail before scan_plugins loads a backend built with armv8.2 instructions
// this CPU can't run, which would otherwise SIGILL deep in inference.
if (!cpu_features_supported()) {
GENIEX_LOG_ERROR(
"this device's CPU lacks features required by geniex; "
"running would crash with an illegal instruction");
return GENIEX_ERROR_COMMON_NOT_SUPPORTED;
}
try {
Registry::instance().scan_plugins();
return GENIEX_SUCCESS;
} catch (const std::exception& e) {
GENIEX_LOG_ERROR("failed to initialize ml: {}", e.what());
return GENIEX_ERROR_COMMON_UNKNOWN;
}
}
int32_t geniex_register_plugin(geniex_plugin_id_func plugin_id_func, geniex_create_plugin_func create_func) {
GENIEX_LOG_DEBUG("register plugin");
try {
void* plugin_id = (void*)plugin_id_func;
void* create_plugin = (void*)create_func;
Registry::instance().register_plugin(plugin_id, create_plugin);
return GENIEX_SUCCESS;
} catch (const std::exception& e) {
GENIEX_LOG_ERROR("failed to register plugin: {}", e.what());
return GENIEX_ERROR_COMMON_UNKNOWN;
}
}
int32_t geniex_deinit(void) {
GENIEX_LOG_DEBUG("deinitializing ml");
try {
// Clean up the registry to ensure proper plugin destruction
geniex::Registry::instance().clear();
} catch (const std::exception& e) {
GENIEX_LOG_ERROR("geniex_deinit() - Error during registry cleanup: {}", e.what());
}
return GENIEX_SUCCESS;
}
// Logging
geniex_log_callback geniex_log = default_log_handler;
// Every level is forwarded to the callback; the embedder filters.
geniex_LogLevel geniex_log_level = GENIEX_LOG_LEVEL_TRACE;
int32_t geniex_set_log(geniex_log_callback callback) {
geniex_log = callback;
return GENIEX_SUCCESS;
}
void geniex_free(void* ptr) {
if (ptr) free(ptr);
}
// Version
const char* version = build_config::kBridgeVersion;
const char* geniex_version() { return version; }
const char* geniex_get_plugin_version(geniex_PluginId plugin_id) {
if (!plugin_id) {
GENIEX_LOG_ERROR("plugin_id is nullptr");
return nullptr;
}
try {
auto plugin = Registry::instance().get<Plugin>(plugin_id);
return plugin ? plugin->version() : nullptr;
} catch (const std::exception& e) {
GENIEX_LOG_ERROR("failed to get plugin version for {}: {}", plugin_id, e.what());
return nullptr;
}
}
// Get Plugin List
int32_t geniex_get_plugin_list(geniex_GetPluginListOutput* output) {
GENIEX_LOG_TRACE("getting plugin list: {}", output);
if (!output) {
GENIEX_LOG_ERROR("output is nullptr");
return GENIEX_ERROR_COMMON_INVALID_INPUT;
}
try {
auto plugin_list = Registry::instance().get_plugin_list();
if (plugin_list.empty()) {
output->plugin_ids = nullptr;
output->plugin_count = 0;
return GENIEX_SUCCESS;
}
output->plugin_ids = static_cast<geniex_PluginId*>(malloc(plugin_list.size() * sizeof(geniex_PluginId)));
if (!output->plugin_ids) {
GENIEX_LOG_ERROR("failed to allocate memory for plugin IDs");
return GENIEX_ERROR_COMMON_MEMORY_ALLOCATION;
}
output->plugin_count = static_cast<int32_t>(plugin_list.size());
for (int32_t i = 0; i < output->plugin_count; i++) {
output->plugin_ids[i] = portable_strdup(plugin_list[i].c_str());
if (!output->plugin_ids[i]) {
GENIEX_LOG_ERROR("failed to duplicate plugin ID at index {}", i);
for (int32_t j = 0; j < i; j++) {
std::free(const_cast<char*>(output->plugin_ids[j]));
}
std::free(output->plugin_ids);
output->plugin_ids = nullptr;
output->plugin_count = 0;
return GENIEX_ERROR_COMMON_MEMORY_ALLOCATION;
}
}
return GENIEX_SUCCESS;
} catch (const std::exception& e) {
GENIEX_LOG_ERROR("failed to get plugin list: {}", e.what());
return GENIEX_ERROR_COMMON_UNKNOWN;
}
}
// Get Device List
int32_t geniex_get_device_list(const geniex_GetDeviceListInput* input, geniex_GetDeviceListOutput* output) {
GENIEX_LOG_TRACE("getting device list: {}", input);
if (!input || !input->plugin_id || !output) {
GENIEX_LOG_ERROR("input or input->plugin_id or output is nullptr");
return GENIEX_ERROR_COMMON_INVALID_INPUT;
}
try {
auto plugin = Registry::instance().get<Plugin>(input->plugin_id);
if (plugin) {
return plugin->get_device_list(input, output);
} else {
GENIEX_LOG_ERROR("failed to get device list for plugin: {}", input->plugin_id);
return GENIEX_ERROR_COMMON_UNKNOWN;
}
return GENIEX_SUCCESS;
} catch (const PluginNotFoundException&) {
GENIEX_LOG_ERROR("plugin not found: {}", input->plugin_id);
return GENIEX_ERROR_COMMON_PLUGIN_INVALID;
} catch (const PluginLoadException&) {
GENIEX_LOG_ERROR("plugin load error: {}", input->plugin_id);
return GENIEX_ERROR_COMMON_PLUGIN_LOAD;
} catch (const std::exception& e) {
GENIEX_LOG_ERROR("failed to get device list: {}", e.what());
return GENIEX_ERROR_COMMON_UNKNOWN;
}
}