"...git@developer.sourcefind.cn:chenpangpang/transformers.git" did not exist on "2f3a4210185f5311f6cfab3c91b30616c9a30fc8"
Unverified Commit 8a1faf28 authored by Marc Sun's avatar Marc Sun Committed by GitHub
Browse files

Add compatibility with skip_memory_metrics for mps device (#29264)

* Add compatibility with mps device

* fix

* typo and style
parent 5c341d45
...@@ -526,6 +526,8 @@ class TrainerMemoryTracker: ...@@ -526,6 +526,8 @@ class TrainerMemoryTracker:
elif is_torch_npu_available(): elif is_torch_npu_available():
self.torch.npu.reset_peak_memory_stats() self.torch.npu.reset_peak_memory_stats()
self.torch.npu.empty_cache() self.torch.npu.empty_cache()
elif is_torch_mps_available():
self.torch.mps.empty_cache()
# gpu # gpu
if self.torch is not None: if self.torch is not None:
...@@ -535,6 +537,8 @@ class TrainerMemoryTracker: ...@@ -535,6 +537,8 @@ class TrainerMemoryTracker:
self.gpu_mem_used_at_start = self.torch.xpu.memory_allocated() self.gpu_mem_used_at_start = self.torch.xpu.memory_allocated()
elif is_torch_npu_available(): elif is_torch_npu_available():
self.gpu_mem_used_at_start = self.torch.npu.memory_allocated() self.gpu_mem_used_at_start = self.torch.npu.memory_allocated()
elif is_torch_mps_available():
self.gpu_mem_used_at_start = self.torch.mps.current_allocated_memory()
# cpu # cpu
self.cpu_mem_used_at_start = self.cpu_mem_used() self.cpu_mem_used_at_start = self.cpu_mem_used()
...@@ -564,6 +568,8 @@ class TrainerMemoryTracker: ...@@ -564,6 +568,8 @@ class TrainerMemoryTracker:
self.torch.xpu.empty_cache() self.torch.xpu.empty_cache()
elif is_torch_npu_available(): elif is_torch_npu_available():
self.torch.npu.empty_cache() self.torch.npu.empty_cache()
elif is_torch_mps_available():
self.torch.mps.empty_cache()
# concepts: # concepts:
# - alloc_delta: the difference of allocated memory between the end and the start # - alloc_delta: the difference of allocated memory between the end and the start
...@@ -581,6 +587,11 @@ class TrainerMemoryTracker: ...@@ -581,6 +587,11 @@ class TrainerMemoryTracker:
elif is_torch_npu_available(): elif is_torch_npu_available():
self.gpu_mem_used_now = self.torch.npu.memory_allocated() self.gpu_mem_used_now = self.torch.npu.memory_allocated()
self.gpu_mem_used_peak = self.torch.npu.max_memory_allocated() self.gpu_mem_used_peak = self.torch.npu.max_memory_allocated()
elif is_torch_mps_available():
self.gpu_mem_used_now = self.torch.mps.current_allocated_memory()
# self.torch.mps.max_memory_allocated() does not exist yet
self.gpu_mem_used_peak = None
else: else:
raise ValueError("No available GPU device found!") raise ValueError("No available GPU device found!")
...@@ -588,8 +599,11 @@ class TrainerMemoryTracker: ...@@ -588,8 +599,11 @@ class TrainerMemoryTracker:
"begin": self.gpu_mem_used_at_start, "begin": self.gpu_mem_used_at_start,
"end": self.gpu_mem_used_now, "end": self.gpu_mem_used_now,
"alloc": (self.gpu_mem_used_now - self.gpu_mem_used_at_start), "alloc": (self.gpu_mem_used_now - self.gpu_mem_used_at_start),
"peaked": max(0, self.gpu_mem_used_peak - self.gpu_mem_used_now),
} }
if self.gpu_mem_used_peak is not None:
self.gpu[self.cur_stage]["peaked"] = max(0, self.gpu_mem_used_peak - self.gpu_mem_used_now)
else:
self.gpu[self.cur_stage]["peaked"] = "Not available"
# cpu # cpu
self.cpu_mem_used_now = self.cpu_mem_used() self.cpu_mem_used_now = self.cpu_mem_used()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment