11// Copyright © 2026 Apple Inc.
22
3+ #include < sys/sysctl.h>
4+
35#include " mlx/backend/gpu/device_info.h"
6+ #include " mlx/backend/metal/device.h"
47#include " mlx/backend/metal/metal.h"
58
69namespace mlx ::core::gpu {
@@ -14,8 +17,42 @@ int device_count() {
1417}
1518
1619const std::unordered_map<std::string, std::variant<std::string, size_t >>&
17- device_info (int /* device_index */ ) {
18- return metal::device_info ();
20+ device_info (int device_index) {
21+ auto init_device_info = []()
22+ -> std::unordered_map<std::string, std::variant<std::string, size_t >> {
23+ auto pool = metal::new_scoped_memory_pool ();
24+ auto raw_device = metal::device (mlx::core::Device::gpu).mtl_device ();
25+ auto name = std::string (raw_device->name ()->utf8String ());
26+ auto arch = std::string (raw_device->architecture ()->name ()->utf8String ());
27+
28+ size_t memsize = 0 ;
29+ size_t length = sizeof (memsize);
30+ sysctlbyname (" hw.memsize" , &memsize, &length, NULL , 0 );
31+
32+ size_t rsrc_limit = 0 ;
33+ sysctlbyname (" iogpu.rsrc_limit" , &rsrc_limit, &length, NULL , 0 );
34+ if (rsrc_limit == 0 ) {
35+ rsrc_limit = 499000 ;
36+ }
37+
38+ return {
39+ {" device_name" , name},
40+ {" architecture" , arch},
41+ {" max_buffer_length" , raw_device->maxBufferLength ()},
42+ {" max_recommended_working_set_size" ,
43+ raw_device->recommendedMaxWorkingSetSize ()},
44+ {" memory_size" , memsize},
45+ {" resource_limit" , rsrc_limit}};
46+ };
47+ static auto device_info_ = init_device_info ();
48+ static std::unordered_map<std::string, std::variant<std::string, size_t >>
49+ empty;
50+
51+ if (device_index == 0 ) {
52+ return device_info_;
53+ } else {
54+ return empty;
55+ }
1956}
2057
2158} // namespace mlx::core::gpu
0 commit comments