-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathsystem_info.cpp
More file actions
3865 lines (3361 loc) · 138 KB
/
Copy pathsystem_info.cpp
File metadata and controls
3865 lines (3361 loc) · 138 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "lemon/system_info.h"
#include "lemon/runtime_config.h"
#include "lemon/version.h"
#include "lemon/backend_manager.h"
#include "lemon/utils/path_utils.h"
#include "lemon/utils/version_utils.h"
#include "lemon/utils/json_utils.h"
#include "lemon/utils/process_manager.h"
#include "lemon/backends/backend_utils.h"
#include <filesystem>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <regex>
#include <lemon/utils/aixlog.hpp>
#include <algorithm>
#include <cctype>
#include <set>
#include <map>
#include <mutex>
#include <vector>
#include <cmath>
#ifdef _WIN32
#include <windows.h>
#include <comdef.h>
#include <Wbemidl.h>
#include <intrin.h>
#include "utils/wmi_helper.h"
#pragma comment(lib, "wbemuuid.lib")
#endif
#ifdef __APPLE__
#include <sys/sysctl.h>
#include <unistd.h>
#endif
#ifdef __linux__
#include <unistd.h>
#endif
#ifdef __linux__
#include <dlfcn.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include "lemon/amdxdna_accel.h"
#endif
#ifndef _WIN32
#include <sys/wait.h>
#endif
#ifdef HAVE_SYSTEMD
#include <systemd/sd-login.h>
#endif
namespace lemon {
namespace fs = std::filesystem;
using namespace lemon::utils;
using namespace lemon::backends;
// AMD discrete GPU keywords
const std::vector<std::string> AMD_DISCRETE_GPU_KEYWORDS = {
"rx ", "xt", "pro w", "pro v", "radeon pro", "firepro", "fury"
};
// NVIDIA discrete GPU keywords
const std::vector<std::string> NVIDIA_DISCRETE_GPU_KEYWORDS = {
"geforce", "rtx", "gtx", "quadro", "tesla", "titan",
"a100", "a40", "a30", "a10", "a6000", "a5000", "a4000", "a2000"
};
// CUDA Compute Capability targets that the lemonade-sdk/llama.cpp release pipeline
// publishes binaries for. Each entry is a literal `sm_XX` token that appears in the
// release asset filename (e.g. llama-ubuntu-cuda-sm_86-x64.tar.xz).
// Empty string means "no CUDA binary for this compute capability" — skip for
// get_cuda_arch / install filenames.
const std::set<std::string> CUDA_SUPPORTED_ARCHS = {
"sm_75", // Turing (RTX 20, GTX 16, T4, Quadro RTX)
"sm_80", // Ampere DC (A100)
"sm_86", // Ampere (RTX 30, A40, A6000, A4000)
"sm_89", // Ada Lovelace (RTX 40, L40, L4)
"sm_90", // Hopper (H100, H200)
"sm_100", // Blackwell DC (B100, B200)
"sm_120", // Blackwell (RTX 50)
};
// ROCm architecture mapping - maps specific gfx architectures to their family (download target).
// Empty string means "no ROCm binary for this ISA" — skip for get_rocm_arch / install filenames.
const std::map<std::string, std::string> ROCM_ARCH_MAPPING = {
// RDNA2 family (gfx103X)
{"gfx1030", "gfx103X"},
{"gfx1031", "gfx103X"},
{"gfx1032", "gfx103X"},
{"gfx1034", "gfx103X"},
// Note: gfx1033, gfx1035, gfx1036 are NOT included (not confirmed as supported)
// map to "" so get_rocm_arch skips them
{"gfx1033", ""},
{"gfx1035", ""},
{"gfx1036", ""},
// RDNA3 family (gfx110X)
{"gfx1100", "gfx110X"},
{"gfx1101", "gfx110X"},
{"gfx1102", "gfx110X"},
{"gfx1103", "gfx110X"},
// RDNA3.5 iGPUs - explicit binary names (no family mapping)
{"gfx1150", "gfx1150"}, // Maps to exact binary name
{"gfx1151", "gfx1151"}, // Maps to exact binary name
// RDNA4 family (gfx120X)
{"gfx1200", "gfx120X"},
{"gfx1201", "gfx120X"},
};
#ifdef __linux__
namespace {
// Minimal HSA ABI surface for runtime dlopen probing.
// Keep values aligned with ROCm headers so this works even when headers are
// not present at build time (for example inside release Docker builds).
using hsa_status_t = int32_t;
using hsa_agent_info_t = int32_t;
using hsa_amd_memory_pool_info_t = int32_t;
using hsa_amd_memory_pool_location_t = int32_t;
using hsa_device_type_t = int32_t;
using hsa_amd_segment_t = int32_t;
struct hsa_agent_t {
uint64_t handle;
};
struct hsa_amd_memory_pool_t {
uint64_t handle;
};
constexpr hsa_status_t HSA_STATUS_SUCCESS = 0x0;
constexpr hsa_status_t HSA_STATUS_ERROR_INVALID_ARGUMENT = 0x1001;
constexpr hsa_agent_info_t HSA_AGENT_INFO_NAME = 0;
constexpr hsa_agent_info_t HSA_AGENT_INFO_VENDOR_NAME = 1;
constexpr hsa_agent_info_t HSA_AGENT_INFO_DEVICE = 17;
constexpr hsa_agent_info_t HSA_AMD_AGENT_INFO_PRODUCT_NAME = 0xA009;
constexpr hsa_agent_info_t HSA_AMD_AGENT_INFO_MEMORY_PROPERTIES = 0xA114;
constexpr hsa_device_type_t HSA_DEVICE_TYPE_CPU = 0;
constexpr hsa_device_type_t HSA_DEVICE_TYPE_GPU = 1;
constexpr hsa_amd_segment_t HSA_AMD_SEGMENT_GLOBAL = 0;
constexpr hsa_amd_memory_pool_info_t HSA_AMD_MEMORY_POOL_INFO_SEGMENT = 0;
constexpr hsa_amd_memory_pool_info_t HSA_AMD_MEMORY_POOL_INFO_GLOBAL_FLAGS = 1;
constexpr hsa_amd_memory_pool_info_t HSA_AMD_MEMORY_POOL_INFO_SIZE = 2;
constexpr hsa_amd_memory_pool_info_t HSA_AMD_MEMORY_POOL_INFO_LOCATION = 17;
constexpr hsa_amd_memory_pool_location_t HSA_AMD_MEMORY_POOL_LOCATION_CPU = 0;
constexpr hsa_amd_memory_pool_location_t HSA_AMD_MEMORY_POOL_LOCATION_GPU = 1;
constexpr uint32_t HSA_AMD_MEMORY_POOL_GLOBAL_FLAG_KERNARG_INIT = 1;
constexpr uint8_t HSA_AMD_MEMORY_PROPERTY_AGENT_IS_APU = (1u << 0);
using HsaAgentCallback = hsa_status_t (*)(hsa_agent_t, void*);
using HsaMemoryPoolCallback = hsa_status_t (*)(hsa_amd_memory_pool_t, void*);
struct RocmAgentInfo {
std::string display_name;
std::string arch_name;
bool is_integrated = false;
double vram_gb = 0.0;
};
struct HsaRuntimeApi {
void* handle = nullptr;
hsa_status_t (*init)() = nullptr;
hsa_status_t (*shut_down)() = nullptr;
hsa_status_t (*iterate_agents)(HsaAgentCallback, void*) = nullptr;
hsa_status_t (*agent_get_info)(hsa_agent_t, hsa_agent_info_t, void*) = nullptr;
hsa_status_t (*amd_agent_iterate_memory_pools)(hsa_agent_t, HsaMemoryPoolCallback, void*) = nullptr;
hsa_status_t (*amd_memory_pool_get_info)(hsa_amd_memory_pool_t, hsa_amd_memory_pool_info_t, void*) = nullptr;
};
std::string trim_copy(const std::string& value) {
const auto start = value.find_first_not_of(" \t\n\r");
if (start == std::string::npos) {
return "";
}
const auto end = value.find_last_not_of(" \t\n\r");
return value.substr(start, end - start + 1);
}
std::string to_lower_copy(std::string value) {
std::transform(value.begin(), value.end(), value.begin(), ::tolower);
return value;
}
bool is_dxg_rocm_environment() {
return fs::exists("/dev/dxg");
}
double round_gb(double value_gb) {
return std::round(value_gb * 10.0) / 10.0;
}
template <typename T>
bool load_hsa_symbol(void* handle, const char* symbol_name, T& symbol) {
dlerror();
void* raw_symbol = dlsym(handle, symbol_name);
const char* error = dlerror();
if (error != nullptr || raw_symbol == nullptr) {
symbol = nullptr;
return false;
}
symbol = reinterpret_cast<T>(raw_symbol);
return true;
}
bool load_hsa_runtime(HsaRuntimeApi& api) {
static const std::vector<std::string> HSA_RUNTIME_CANDIDATES = {
"libhsa-runtime64.so.1",
"libhsa-runtime64.so",
"/opt/rocm/lib/libhsa-runtime64.so.1",
"/opt/rocm/lib/libhsa-runtime64.so"
};
for (const auto& candidate : HSA_RUNTIME_CANDIDATES) {
api.handle = dlopen(candidate.c_str(), RTLD_LAZY | RTLD_LOCAL);
if (!api.handle) {
continue;
}
if (load_hsa_symbol(api.handle, "hsa_init", api.init) &&
load_hsa_symbol(api.handle, "hsa_shut_down", api.shut_down) &&
load_hsa_symbol(api.handle, "hsa_iterate_agents", api.iterate_agents) &&
load_hsa_symbol(api.handle, "hsa_agent_get_info", api.agent_get_info) &&
load_hsa_symbol(api.handle, "hsa_amd_agent_iterate_memory_pools", api.amd_agent_iterate_memory_pools) &&
load_hsa_symbol(api.handle, "hsa_amd_memory_pool_get_info", api.amd_memory_pool_get_info)) {
return true;
}
dlclose(api.handle);
api = HsaRuntimeApi{};
}
return false;
}
void unload_hsa_runtime(HsaRuntimeApi& api) {
if (api.handle != nullptr) {
dlclose(api.handle);
api = HsaRuntimeApi{};
}
}
struct HsaPoolQueryContext {
HsaRuntimeApi* api = nullptr;
double largest_global_pool_gb = 0.0;
};
hsa_status_t collect_hsa_memory_pool_info(hsa_amd_memory_pool_t pool, void* data) {
auto* context = static_cast<HsaPoolQueryContext*>(data);
if (!context || !context->api) {
return HSA_STATUS_ERROR_INVALID_ARGUMENT;
}
hsa_amd_segment_t segment = HSA_AMD_SEGMENT_GLOBAL;
if (context->api->amd_memory_pool_get_info(pool, HSA_AMD_MEMORY_POOL_INFO_SEGMENT, &segment) != HSA_STATUS_SUCCESS) {
return HSA_STATUS_SUCCESS;
}
if (segment != HSA_AMD_SEGMENT_GLOBAL) {
return HSA_STATUS_SUCCESS;
}
hsa_amd_memory_pool_location_t location = HSA_AMD_MEMORY_POOL_LOCATION_CPU;
if (context->api->amd_memory_pool_get_info(pool, HSA_AMD_MEMORY_POOL_INFO_LOCATION, &location) != HSA_STATUS_SUCCESS) {
return HSA_STATUS_SUCCESS;
}
if (location != HSA_AMD_MEMORY_POOL_LOCATION_GPU) {
return HSA_STATUS_SUCCESS;
}
uint32_t global_flags = 0;
if (context->api->amd_memory_pool_get_info(pool, HSA_AMD_MEMORY_POOL_INFO_GLOBAL_FLAGS, &global_flags) != HSA_STATUS_SUCCESS) {
return HSA_STATUS_SUCCESS;
}
if ((global_flags & HSA_AMD_MEMORY_POOL_GLOBAL_FLAG_KERNARG_INIT) != 0) {
return HSA_STATUS_SUCCESS;
}
size_t pool_size = 0;
if (context->api->amd_memory_pool_get_info(pool, HSA_AMD_MEMORY_POOL_INFO_SIZE, &pool_size) != HSA_STATUS_SUCCESS) {
return HSA_STATUS_SUCCESS;
}
const double pool_gb = round_gb(static_cast<double>(pool_size) / (1024.0 * 1024.0 * 1024.0));
context->largest_global_pool_gb = std::max(context->largest_global_pool_gb, pool_gb);
return HSA_STATUS_SUCCESS;
}
struct HsaAgentQueryContext {
HsaRuntimeApi* api = nullptr;
std::vector<RocmAgentInfo>* agents = nullptr;
std::set<std::string>* seen_agents = nullptr;
};
hsa_status_t collect_hsa_agent_info(hsa_agent_t agent, void* data) {
auto* context = static_cast<HsaAgentQueryContext*>(data);
if (!context || !context->api || !context->agents || !context->seen_agents) {
return HSA_STATUS_ERROR_INVALID_ARGUMENT;
}
hsa_device_type_t device_type = HSA_DEVICE_TYPE_CPU;
if (context->api->agent_get_info(agent, HSA_AGENT_INFO_DEVICE, &device_type) != HSA_STATUS_SUCCESS ||
device_type != HSA_DEVICE_TYPE_GPU) {
return HSA_STATUS_SUCCESS;
}
char arch_name[64] = {0};
char marketing_name[64] = {0};
char vendor_name[64] = {0};
if (context->api->agent_get_info(agent, HSA_AGENT_INFO_NAME, arch_name) != HSA_STATUS_SUCCESS ||
context->api->agent_get_info(agent, HSA_AGENT_INFO_VENDOR_NAME, vendor_name) != HSA_STATUS_SUCCESS) {
return HSA_STATUS_SUCCESS;
}
// Product name availability varies across ROCm/WSL runtime builds.
// Treat it as optional and fall back to the arch name when unavailable.
if (context->api->agent_get_info(agent, static_cast<hsa_agent_info_t>(HSA_AMD_AGENT_INFO_PRODUCT_NAME), marketing_name) != HSA_STATUS_SUCCESS) {
marketing_name[0] = '\0';
}
const std::string vendor = to_lower_copy(trim_copy(vendor_name));
const std::string arch = trim_copy(arch_name);
const std::string marketing = trim_copy(marketing_name);
if (vendor.find("amd") == std::string::npos || to_lower_copy(arch).find("gfx") != 0) {
return HSA_STATUS_SUCCESS;
}
HsaPoolQueryContext pool_context;
pool_context.api = context->api;
context->api->amd_agent_iterate_memory_pools(agent, collect_hsa_memory_pool_info, &pool_context);
RocmAgentInfo rocm_agent;
rocm_agent.arch_name = arch;
if (!marketing.empty() && marketing != arch) {
rocm_agent.display_name = marketing + " (" + arch + ")";
} else if (!marketing.empty()) {
rocm_agent.display_name = marketing;
} else {
rocm_agent.display_name = arch;
}
uint8_t memory_properties[8] = {0};
if (context->api->agent_get_info(
agent,
HSA_AMD_AGENT_INFO_MEMORY_PROPERTIES,
memory_properties) == HSA_STATUS_SUCCESS) {
rocm_agent.is_integrated =
(memory_properties[0] & HSA_AMD_MEMORY_PROPERTY_AGENT_IS_APU) != 0;
}
rocm_agent.vram_gb = pool_context.largest_global_pool_gb;
// Include the HSA agent handle so two identical GPUs are kept as distinct devices.
const std::string dedupe_key = rocm_agent.display_name + "|" + rocm_agent.arch_name + "|" + std::to_string(agent.handle);
if (context->seen_agents->insert(dedupe_key).second) {
context->agents->push_back(rocm_agent);
}
return HSA_STATUS_SUCCESS;
}
std::vector<RocmAgentInfo> query_rocm_agents_via_hsa_runtime() {
std::vector<RocmAgentInfo> agents;
if (!is_dxg_rocm_environment()) {
return agents;
}
HsaRuntimeApi api;
if (!load_hsa_runtime(api)) {
return agents;
}
if (api.init() != HSA_STATUS_SUCCESS) {
unload_hsa_runtime(api);
return agents;
}
std::set<std::string> seen_agents;
HsaAgentQueryContext context;
context.api = &api;
context.agents = &agents;
context.seen_agents = &seen_agents;
api.iterate_agents(collect_hsa_agent_info, &context);
api.shut_down();
unload_hsa_runtime(api);
return agents;
}
std::vector<RocmAgentInfo> query_rocm_agents() {
return query_rocm_agents_via_hsa_runtime();
}
std::vector<GPUInfo> query_dxg_amd_gpus(const std::string& gpu_type) {
std::vector<GPUInfo> gpus;
for (const auto& agent : query_rocm_agents()) {
if ((gpu_type == "integrated" && !agent.is_integrated) ||
(gpu_type == "discrete" && agent.is_integrated)) {
continue;
}
GPUInfo gpu;
gpu.name = agent.display_name;
gpu.available = true;
gpu.vram_gb = agent.vram_gb;
gpus.push_back(gpu);
}
return gpus;
}
} // namespace
#endif
// ============================================================================
// Recipe/Backend definition table - single source of truth for support matrix
// ============================================================================
// Device constraints: device_type -> set of allowed families (empty = all families)
using DeviceConstraints = std::map<std::string, std::set<std::string>>;
struct RecipeBackendDef {
std::string recipe;
std::string backend;
std::set<std::string> supported_os;
DeviceConstraints devices;
};
// Recipe definitions table - single source of truth for all recipe/backend support
// Format: {recipe, backend, {supported_os}, {{device_type, {allowed_families}}}}
//
// IMPORTANT: Backend order matters! For recipes with multiple backends (e.g., llamacpp),
// the order in this table defines the preference order. First listed = most preferred.
// Example: metal is listed before vulkan on macOS, vulkan before cpu elsewhere.
//
// Empty family set {} means "all families of that device type"
static const std::vector<RecipeBackendDef> RECIPE_DEFS = {
// llamacpp with multiple backends (order = preference)
{"llamacpp", "system", {"linux"}, {
{"cpu", {"x86_64"}}, // Placeholder, actual check is PATH-based
}},
{"llamacpp", "metal", {"macos"},
{
{"metal", {}},
}},
{"llamacpp", "cuda", {"windows", "linux"}, {
{"nvidia_gpu", {"sm_75", "sm_80", "sm_86", "sm_89", "sm_90", "sm_100", "sm_120"}},
}},
{"llamacpp", "vulkan", {"windows", "linux"}, {
{"cpu", {"x86_64"}},
{"amd_gpu", {}}, // all AMD GPU families
}},
{"llamacpp", "openvino", {"linux"}, {
{"cpu", {"x86_64"}},
}},
{"llamacpp", "rocm", {"windows", "linux"}, {
{"amd_gpu", {"gfx1150", "gfx1151", "gfx103X", "gfx110X", "gfx120X"}}, // STX iGPUs + RDNA2/3/4 dGPUs
}},
{"llamacpp", "cpu", {"windows", "linux"}, {
{"cpu", {"x86_64"}},
}},
// whisper.cpp - Windows: NPU and CPU; Linux: CPU and Vulkan; macOS: Metal
{"whispercpp", "npu", {"windows"}, {
{"amd_npu", {"XDNA2"}},
}},
{"whispercpp", "vulkan", {"linux"}, {
{"cpu", {"x86_64"}},
}},
{"whispercpp", "cpu", {"windows", "linux"}, {
{"cpu", {"x86_64"}},
}},
{"whispercpp", "metal", {"macos"}, {
{"metal", {}},
}},
// kokoro - Windows/Linux x86_64; macOS arm64 (Metal)
{"kokoro", "cpu", {"windows", "linux"}, {
{"cpu", {"x86_64"}},
}},
{"kokoro", "metal", {"macos"}, {
{"metal", {}},
}},
// stable-diffusion.cpp - ROCm backend for AMD GPUs
{"sd-cpp", "rocm", {"windows", "linux"}, {
{"amd_gpu", {
"gfx1150",
"gfx1151", "gfx103X", "gfx110X", "gfx120X"
}},
}},
// stable-diffusion.cpp - Vulkan backend (Windows/Linux x86_64)
{"sd-cpp", "vulkan", {"windows", "linux"}, {
{"cpu", {"x86_64"}},
{"amd_gpu", {}},
{"nvidia_gpu", {}},
}},
// stable-diffusion.cpp - CPU backend (Windows/Linux x86_64)
{"sd-cpp", "cpu", {"windows", "linux"}, {
{"cpu", {"x86_64"}},
}},
// stable-diffusion.cpp - Metal backend (macOS arm64)
{"sd-cpp", "metal", {"macos"}, {
{"metal", {}},
}},
// FLM - NPU (XDNA2)
{"flm", "npu", {"windows", "linux"}, {
{"amd_npu", {"XDNA2"}},
}},
// RyzenAI LLM - Windows NPU (XDNA2)
{"ryzenai-llm", "npu", {"windows"}, {
{"amd_npu", {"XDNA2"}},
}},
// vLLM - ROCm backend for AMD GPUs (Linux only)
{"vllm", "rocm", {"linux"}, {
{"amd_gpu", {"gfx1150", "gfx1151", "gfx110X", "gfx120X"}},
}},
};
// ============================================================================
// Device family to human-readable name mapping
// ============================================================================
// Maps device family codes to human-readable descriptions
// Format: {family_code, human_readable_name}
static const std::map<std::string, std::string> DEVICE_FAMILY_NAMES = {
// CPU architectures
{"x86_64", "x86-64 processors"},
{"arm64", "ARM64 processors"},
// AMD GPU architectures (ROCm)
{"gfx1150", "Radeon 880M/890M (Strix Point)"},
{"gfx1151", "Radeon 8050S/8060S (Strix Halo)"},
{"gfx103X", "Radeon RX 6000 series (RDNA2)"},
{"gfx110X", "Radeon RX 7000 series (RDNA3)"},
{"gfx120X", "Radeon RX 9000 series (RDNA4)"},
// NVIDIA GPU compute capabilities (CUDA)
{"sm_75", "GeForce RTX 20 / GTX 16 series (Turing)"},
{"sm_80", "NVIDIA A100 (Ampere)"},
{"sm_86", "GeForce RTX 30 / A40 / A6000 (Ampere)"},
{"sm_89", "GeForce RTX 40 / L40 / L4 (Ada Lovelace)"},
{"sm_90", "NVIDIA H100 / H200 (Hopper)"},
{"sm_100", "NVIDIA B100 / B200 (Blackwell)"},
{"sm_120", "GeForce RTX 50 series (Blackwell)"},
// NPU architectures
{"XDNA2", "AMD XDNA 2"},
};
// Maps device types to human-readable names (for error messages)
static const std::map<std::string, std::string> DEVICE_TYPE_NAMES = {
{"cpu", "CPU"},
{"amd_gpu", "AMD GPU"},
{"amd_npu", "AMD NPU"},
{"nvidia_gpu", "NVIDIA GPU"},
{"metal", "MacOS Metal GPU"}
};
// Get human-readable name for a device family (e.g., "gfx1150" -> "Radeon 880M/890M")
static std::string get_family_name(const std::string& family) {
auto it = DEVICE_FAMILY_NAMES.find(family);
return it != DEVICE_FAMILY_NAMES.end() ? it->second : family;
}
// Get human-readable name for a device type (e.g., "amd_gpu" -> "AMD GPU")
static std::string get_device_type_name(const std::string& device_type) {
auto it = DEVICE_TYPE_NAMES.find(device_type);
return it != DEVICE_TYPE_NAMES.end() ? it->second : device_type;
}
// Generate a human-readable error message for unsupported backend
// Uses RECIPE_DEFS and DEVICE_FAMILY_NAMES to build a descriptive message
std::string SystemInfo::get_unsupported_backend_error(const std::string& recipe, const std::string& backend) {
std::string error;
// Find the recipe/backend in RECIPE_DEFS
for (const auto& def : RECIPE_DEFS) {
if (def.recipe == recipe && def.backend == backend) {
// Collect all required family names
std::vector<std::string> family_names;
for (const auto& [device_type, families] : def.devices) {
for (const auto& f : families) {
family_names.push_back(get_family_name(f));
}
}
// Build error message
error = "No compatible device detected for " + recipe;
error += " (" + backend + " backend)";
if (!family_names.empty()) {
error += ". Requires: ";
for (size_t i = 0; i < family_names.size(); i++) {
if (i > 0) error += ", ";
error += family_names[i];
}
}
error += ".";
break;
}
}
if (error.empty()) {
error = "Unsupported recipe/backend combination: " + recipe + "/" + backend;
}
return error;
}
// Detected device with its family
struct DetectedDevice {
std::string type; // "cpu", "amd_gpu", "amd_npu"
std::string name; // Full device name
std::string family; // "x86_64", "gfx1150", "XDNA2", etc.
bool present;
};
// Get current OS identifier
static std::string get_current_os() {
#ifdef _WIN32
return "windows";
#elif defined(__APPLE__)
return "macos";
#else
return "linux";
#endif
}
// Forward declarations for helper functions
std::string identify_rocm_arch_from_name(const std::string& device_name);
std::string identify_cuda_arch_from_name(const std::string& device_name);
std::string identify_npu_arch();
static std::string compute_cap_to_sm(const std::string& compute_cap);
static std::string read_version_file(const fs::path& version_file);
static std::string get_expected_backend_version(const std::string& recipe, const std::string& backend);
// Check if device matches constraints (empty constraint set = all families allowed)
static bool device_matches_constraint(const std::string& device_family,
const std::set<std::string>& allowed_families) {
if (allowed_families.empty()) {
return true; // Empty = all families allowed
}
return allowed_families.count(device_family) > 0;
}
// Generic installation check
static bool is_recipe_installed(const std::string& recipe, const std::string& backend, std::string& error_message) {
bool is_llamacpp_rocm_backend = recipe == "llamacpp" && backend == "rocm";
// Special handling for ROCm backends on gfx1151 (Strix Halo) if kernel CWSR fix is missing
bool is_vllm_rocm_backend = recipe == "vllm" && backend == "rocm";
if ((recipe == "sd-cpp" && backend == "rocm") || is_llamacpp_rocm_backend || is_vllm_rocm_backend) {
if (needs_gfx1151_cwsr_fix()) {
error_message = "Linux kernel missing support";
return false;
}
}
auto* spec = try_get_spec_for_recipe(recipe);
if (spec) {
try {
BackendUtils::get_backend_binary_path(*spec, backend);
// For system llamacpp backend, also verify the HIP plugin is available
// This is required for ROCm GPU acceleration with dynamically loaded backends
if (recipe == "llamacpp" && backend == "system") {
#ifdef __linux__
// Check if AMD GPU driver is loaded (KFD indicates amdgpu driver)
if (fs::exists("/sys/class/kfd")) {
// System has AMD GPU(s), so we need the HIP plugin
if (!is_ggml_hip_plugin_available()) {
error_message = "HIP plugin libggml-hip.so not installed";
return false;
}
}
#endif
}
return true;
} catch (...) {
#ifndef _WIN32
// On Linux, FLM is installed as a system package (in PATH, not install dir)
if (recipe == "flm" && !utils::find_flm_executable().empty()) {
return true;
}
#endif
return false;
}
}
return false;
}
static std::string get_recipe_version(const std::string& recipe, const std::string& backend) {
if (recipe == "llamacpp" && backend == "system") {
return SystemInfo::get_system_llamacpp_version();
}
auto* spec = try_get_spec_for_recipe(recipe);
if (spec) {
std::string version_file = BackendUtils::get_installed_version_file(*spec, backend);
if (version_file.empty()) {
#ifndef _WIN32
// On Linux, FLM is a system package with no version.txt - query directly
if (recipe == "flm") {
return SystemInfo::get_flm_version();
}
#endif
return "unknown";
}
std::string version = read_version_file(version_file);
#ifndef _WIN32
// On Linux, version.txt may not exist on disk for system-installed FLM
if (recipe == "flm" && (version.empty() || version == "unknown")) {
return SystemInfo::get_flm_version();
}
#endif
return version;
}
return "";
}
static std::string get_install_command(const std::string& recipe, const std::string& backend) {
if (auto* cfg = RuntimeConfig::global()) {
if (cfg->no_fetch_executables()) {
return "";
}
}
return "lemonade backends install " + recipe + ":" + backend;
}
// Extract every contiguous run of digits from `s` into a vector of ints.
// Used by version_compare to handle the variety of upstream tag conventions
// across our backends:
// - "b8664" -> [8664] (llama.cpp, kokoro)
// - "v1.8.2" -> [1, 8, 2] (whisper.cpp, flm, ryzenai)
// - "master-569-ab6afe8"-> [569, 6] (sd-cpp)
// Hex hashes contribute their numeric digits; that's noisy but harmless because
// we only compare tags from the same repo, which share a tag format.
static std::vector<int> numeric_runs(const std::string& s) {
std::vector<int> out;
long long cur = 0;
bool in_num = false;
for (char c : s) {
if (std::isdigit(static_cast<unsigned char>(c))) {
cur = cur * 10 + (c - '0');
in_num = true;
} else if (in_num) {
out.push_back(static_cast<int>(cur));
cur = 0;
in_num = false;
}
}
if (in_num) out.push_back(static_cast<int>(cur));
return out;
}
// Returns -1 / 0 / +1 for a < b / a == b / a > b. When either input lacks any
// digit runs, returns 0 — this is fail-safe: ambiguous comparisons suppress the
// upgrade signal rather than nag the user incorrectly.
static int version_compare(const std::string& a, const std::string& b) {
if (a == b) return 0;
auto pa = numeric_runs(a);
auto pb = numeric_runs(b);
if (pa.empty() || pb.empty()) return 0;
size_t n = pa.size() > pb.size() ? pa.size() : pb.size();
for (size_t i = 0; i < n; ++i) {
int ai = i < pa.size() ? pa[i] : 0;
int bi = i < pb.size() ? pb[i] : 0;
if (ai < bi) return -1;
if (ai > bi) return +1;
}
return 0;
}
// True if the user's *_bin config value for this (recipe, backend) is "latest".
static bool is_bin_pinned_to_latest(const std::string& recipe, const std::string& backend) {
return BackendUtils::get_bin_config_value(recipe, backend) == "latest";
}
static std::string get_expected_backend_version(const std::string& recipe, const std::string& backend) {
static json backend_versions = []() -> json {
try {
std::string config_path = utils::get_resource_path("resources/backend_versions.json");
std::ifstream file(config_path);
if (!file.is_open()) {
return json::object();
}
json data = json::parse(file);
file.close();
return data;
} catch (...) {
return json::object();
}
}();
if (!backend_versions.contains(recipe)) {
return "";
}
// sd-cpp and llamacpp expose a single "rocm" backend but store per-channel
// version pins ("rocm-stable", "rocm-nightly") in backend_versions.json.
// Mirror the resolution done by BackendUtils::get_backend_version().
std::string resolved_backend = backend;
if ((recipe == "llamacpp" || recipe == "sd-cpp") && backend == "rocm") {
std::string channel = "stable";
if (auto* cfg = RuntimeConfig::global()) {
channel = cfg->rocm_channel_for_recipe(recipe);
}
resolved_backend = "rocm-" + channel;
}
const auto& recipe_config = backend_versions[recipe];
if (!recipe_config.contains(resolved_backend) || !recipe_config[resolved_backend].is_string()) {
return "";
}
return recipe_config[resolved_backend].get<std::string>();
}
// ============================================================================
// SystemInfo base class implementation
// ============================================================================
json SystemInfo::get_system_info_dict() {
json info;
info["OS Version"] = get_os_version();
return info;
}
json SystemInfo::get_device_dict() {
json devices;
// NOTE: This function collects hardware info only (no inference engines).
// Inference engines are detected separately in get_system_info_with_cache()
// because they should always be fresh (not cached).
// Get CPU info - with fault tolerance
try {
auto cpu = get_cpu_device();
devices["cpu"] = {
{"name", cpu.name},
{"cores", cpu.cores},
{"threads", cpu.threads},
{"available", cpu.available}
};
#if defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64)
devices["cpu"]["family"] = "x86_64";
#elif defined(__aarch64__) || defined(_M_ARM64)
devices["cpu"]["family"] = "arm64";
#else
devices["cpu"]["family"] = "unknown";
#endif
if (!cpu.error.empty()) {
devices["cpu"]["error"] = cpu.error;
}
} catch (const std::exception& e) {
devices["cpu"] = {
{"name", "Unknown"},
{"cores", 0},
{"threads", 0},
{"available", true}, // Assume available - trust the user
{"error", std::string("Detection exception: ") + e.what()}
};
}
// Get AMD GPU info (both integrated and discrete) - with fault tolerance
try {
devices["amd_gpu"] = json::array();
auto amd_igpu = get_amd_igpu_device();
if (amd_igpu.available) {
json gpu_json = {
{"name", amd_igpu.name},
{"available", amd_igpu.available}
};
if (amd_igpu.vram_gb > 0) {
gpu_json["vram_gb"] = amd_igpu.vram_gb;
}
if (amd_igpu.virtual_gb > 0) {
gpu_json["virtual_mem_gb"] = amd_igpu.virtual_gb;
}
gpu_json["family"] = identify_rocm_arch_from_name(amd_igpu.name);
if (!amd_igpu.error.empty()) {
gpu_json["error"] = amd_igpu.error;
}
devices["amd_gpu"].push_back(gpu_json);
}
auto amd_dgpus = get_amd_dgpu_devices();
for (const auto& gpu : amd_dgpus) {
if (gpu.available) {
json gpu_json = {
{"name", gpu.name},
{"available", gpu.available}
};
if (gpu.vram_gb > 0) {
gpu_json["vram_gb"] = gpu.vram_gb;
}
if (gpu.virtual_gb > 0) {
gpu_json["virtual_mem_gb"] = gpu.virtual_gb;
}
if (!gpu.driver_version.empty()) {
gpu_json["driver_version"] = gpu.driver_version;
}
gpu_json["family"] = identify_rocm_arch_from_name(gpu.name);
if (!gpu.error.empty()) {
gpu_json["error"] = gpu.error;
}
devices["amd_gpu"].push_back(gpu_json);
}
}
} catch (const std::exception& e) {
devices["amd_gpu"] = json::array();
devices["amd_gpu_error"] = std::string("Detection exception: ") + e.what();
}
// Get NVIDIA dGPU info - with fault tolerance
try {
auto nvidia_gpus = get_nvidia_gpu_devices();
devices["nvidia_gpu"] = json::array();
for (const auto& gpu : nvidia_gpus) {
json gpu_json = {
{"name", gpu.name},
{"available", gpu.available}
};
if (gpu.index >= 0) {
gpu_json["index"] = gpu.index;
}
if (!gpu.uuid.empty()) {
gpu_json["uuid"] = gpu.uuid;
}
if (gpu.available) {
std::string family;
const bool has_compute_cap = !gpu.compute_capability.empty();
if (has_compute_cap) {
// Primary: derive sm_XX from nvidia-smi compute_cap (e.g. "8.6" -> "sm_86").
// Keep the derived value even when unsupported so availability logic can
// surface a precise "Unsupported GPU: sm_XX" message.
family = compute_cap_to_sm(gpu.compute_capability);
gpu_json["compute_capability"] = gpu.compute_capability;
}
if (family.empty() && !has_compute_cap && !gpu.name.empty()) {
// Fallback only when compute_cap is unavailable.
family = identify_cuda_arch_from_name(gpu.name);
}
gpu_json["family"] = family;
}
if (gpu.vram_gb > 0) {
gpu_json["vram_gb"] = gpu.vram_gb;
}
if (!gpu.driver_version.empty()) {
gpu_json["driver_version"] = gpu.driver_version;
}
if (!gpu.error.empty()) {
gpu_json["error"] = gpu.error;
}
devices["nvidia_gpu"].push_back(gpu_json);
}
} catch (const std::exception& e) {
devices["nvidia_gpu"] = json::array();
devices["nvidia_gpu_error"] = std::string("Detection exception: ") + e.what();
}
// Get NPU info - with fault tolerance
// Use CPU processor name as the NPU device name (e.g., "AMD Ryzen AI 9 HX 375")
try {
auto npu = get_npu_device();
std::string cpu_name = devices.contains("cpu") ? devices["cpu"].value("name", "") : "";
devices["amd_npu"] = {
{"name", cpu_name.empty() ? npu.name : cpu_name},
{"available", npu.available}
};
devices["amd_npu"]["family"] = identify_npu_arch();
if (npu.tops_max > 0) {
devices["amd_npu"]["tops_max_int"] = npu.tops_max;
}
devices["amd_npu"]["utilization"] = npu.utilization;
if (!npu.power_mode.empty()) {