Skip to content

Commit 0e4a036

Browse files
authored
common : add common_print_available_devices() (ggml-org#26170)
Signed-off-by: Adrien Gallouët <angt@huggingface.co>
1 parent b77d646 commit 0e4a036

3 files changed

Lines changed: 30 additions & 30 deletions

File tree

common/arg.cpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,31 @@ static std::vector<ggml_backend_dev_t> parse_device_list(const std::string & val
10611061
return devices;
10621062
}
10631063

1064+
void common_print_available_devices() {
1065+
constexpr size_t MiB = 1024 * 1024;
1066+
std::vector<ggml_backend_dev_t> devices;
1067+
1068+
ggml_backend_load_all();
1069+
1070+
for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
1071+
auto * dev = ggml_backend_dev_get(i);
1072+
if (ggml_backend_dev_type(dev) != GGML_BACKEND_DEVICE_TYPE_CPU) {
1073+
devices.push_back(dev);
1074+
}
1075+
}
1076+
printf("Available devices:\n");
1077+
1078+
if (devices.empty()) {
1079+
printf(" (none)\n");
1080+
return;
1081+
}
1082+
for (auto * dev : devices) {
1083+
size_t free, total;
1084+
ggml_backend_dev_memory(dev, &free, &total);
1085+
printf(" %s: %s (%zu MiB, %zu MiB free)\n", ggml_backend_dev_name(dev), ggml_backend_dev_description(dev), total / MiB, free / MiB);
1086+
}
1087+
}
1088+
10641089
static void add_rpc_devices(const std::string & servers) {
10651090
auto rpc_servers = string_split<std::string>(servers, ',');
10661091
if (rpc_servers.empty()) {
@@ -2588,20 +2613,7 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
25882613
{"--list-devices"},
25892614
"print list of available devices and exit",
25902615
[](common_params &) {
2591-
ggml_backend_load_all();
2592-
std::vector<ggml_backend_dev_t> devices;
2593-
for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
2594-
auto * dev = ggml_backend_dev_get(i);
2595-
if (ggml_backend_dev_type(dev) != GGML_BACKEND_DEVICE_TYPE_CPU) {
2596-
devices.push_back(dev);
2597-
}
2598-
}
2599-
printf("Available devices:\n");
2600-
for (auto * dev : devices) {
2601-
size_t free, total;
2602-
ggml_backend_dev_memory(dev, &free, &total);
2603-
printf(" %s: %s (%zu MiB, %zu MiB free)\n", ggml_backend_dev_name(dev), ggml_backend_dev_description(dev), total / 1024 / 1024, free / 1024 / 1024);
2604-
}
2616+
common_print_available_devices();
26052617
exit(0);
26062618
}
26072619
));

common/arg.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ struct common_params_context {
123123
// if one argument has invalid value, it will automatically display usage of the specific argument (and not the full usage message)
124124
bool common_params_parse(int argc, char ** argv, common_params & params, llama_example ex, void(*print_usage)(int, char **) = nullptr);
125125

126+
// load all backends and print the list of available (non-CPU) devices to stdout
127+
void common_print_available_devices();
128+
126129
// parse input arguments from CLI into a map
127130
bool common_params_to_map(int argc, char ** argv, llama_example ex, std::map<common_arg, std::string> & out_map);
128131

tools/llama-bench/llama-bench.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -670,22 +670,7 @@ static cmd_params parse_cmd_params(int argc, char ** argv) {
670670
break;
671671
}
672672
} else if (arg == "--list-devices") {
673-
std::vector<ggml_backend_dev_t> devices;
674-
for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
675-
auto * dev = ggml_backend_dev_get(i);
676-
if (ggml_backend_dev_type(dev) != GGML_BACKEND_DEVICE_TYPE_CPU) {
677-
devices.push_back(dev);
678-
}
679-
}
680-
printf("Available devices:\n");
681-
if (devices.empty()) {
682-
printf(" (none)\n");
683-
}
684-
for (auto * dev : devices) {
685-
size_t free, total;
686-
ggml_backend_dev_memory(dev, &free, &total);
687-
printf(" %s: %s (%zu MiB, %zu MiB free)\n", ggml_backend_dev_name(dev), ggml_backend_dev_description(dev), total / 1024 / 1024, free / 1024 / 1024);
688-
}
673+
common_print_available_devices();
689674
exit(0);
690675
} else if (arg == "-t" || arg == "--threads") {
691676
if (++i >= argc) {

0 commit comments

Comments
 (0)