@@ -50,6 +50,8 @@ class XPUContext;
50
50
namespace backends {
51
51
namespace xpu {
52
52
53
+ static std::once_flag xpuml_init_flag;
54
+
53
55
/* *************************** Version Management **************************/
54
56
55
57
// ! Get the version of XPU Driver
@@ -221,6 +223,50 @@ void MemcpySyncD2D(void* dst,
221
223
222
224
/* *************************** Others **************************/
223
225
226
+ int GetXPUDeviceUtilizationRate (int dev_id) {
227
+ std::call_once (xpuml_init_flag, xpumlInit);
228
+ if (dev_id == -1 ) {
229
+ dev_id = GetXPUCurrentDeviceId ();
230
+ }
231
+ xpumlDevice_t dev_handle;
232
+ PADDLE_ENFORCE_XPUML_SUCCESS (
233
+ xpumlDeviceGetHandleByIndex (dev_id, &dev_handle));
234
+ xpumlUtilization_t dev_util;
235
+ PADDLE_ENFORCE_XPUML_SUCCESS (
236
+ xpumlDeviceGetUtilizationRates (dev_handle, &dev_util));
237
+ return dev_util.xpu ;
238
+ }
239
+
240
+ int GetXPUDeviceTotalMemory (int dev_id) {
241
+ std::call_once (xpuml_init_flag, xpumlInit);
242
+ if (dev_id == -1 ) {
243
+ dev_id = GetXPUCurrentDeviceId ();
244
+ }
245
+
246
+ xpumlDevice_t dev_handle;
247
+ PADDLE_ENFORCE_XPUML_SUCCESS (
248
+ xpumlDeviceGetHandleByIndex (dev_id, &dev_handle));
249
+ xpumlMemory_t dev_mem_info;
250
+ PADDLE_ENFORCE_XPUML_SUCCESS (
251
+ xpumlDeviceGetMemoryInfo (dev_handle, &dev_mem_info));
252
+ return dev_mem_info.totalGlobalMemory / 1024 / 1024 ; // MB
253
+ }
254
+
255
+ int GetXPUDeviceUsedMemory (int dev_id) {
256
+ std::call_once (xpuml_init_flag, xpumlInit);
257
+ if (dev_id == -1 ) {
258
+ dev_id = GetXPUCurrentDeviceId ();
259
+ }
260
+
261
+ xpumlDevice_t dev_handle;
262
+ PADDLE_ENFORCE_XPUML_SUCCESS (
263
+ xpumlDeviceGetHandleByIndex (dev_id, &dev_handle));
264
+ xpumlMemory_t dev_mem_info;
265
+ PADDLE_ENFORCE_XPUML_SUCCESS (
266
+ xpumlDeviceGetMemoryInfo (dev_handle, &dev_mem_info));
267
+ return dev_mem_info.usedGlobalMemory / 1024 / 1024 ; // MB
268
+ }
269
+
224
270
XPUVersion get_xpu_version (int dev_id) {
225
271
if (dev_id == -1 ) {
226
272
dev_id = GetXPUCurrentDeviceId ();
0 commit comments