記得標注每種方法是否為侵入式
CPU
pyinstrument
可以生成heat graph和每一行代碼的時間,不用自己二次清洗和可視化數據,算是比較簡單易用的
內存
針對非復雜對象的數據類型,sys.getsizeof()和pympler可用
import sys
prob = [float(i) for i in range(1000000)]
print(sys.getsizeof(prob ))
# 使用前需要pip install pympler安裝pympler
from pympler import asizeof
prob = [float(i) for i in range(1000000)]
print(asizeof.asizeof(prob))
memory_profiler可以逐行監(jiān)控函數的內存開銷,但遇到復雜函數會明顯耗時增加甚至卡死
監(jiān)控的函數可以帶輸入
# 使用前需要pip install memory_profiler安裝memory_profiler
from memory_profiler import profile
@profile(precision=4, stream=open('memory_profiler.log', 'w+'))
def test(prob):
prob_dict = {}
width = int(onp.log2(len(prob3)))
counter = 0
for p in prob:
prob_dict[bin(counter)[2:].zfill(width)] = p
counter += 1
return prob_dict
prob1 = [0.2, 0.2, 0, 0.6]
test(prob1 )
GPU
使用pytorch
import torch
if not torch.cuda.is_available():
print("CUDA is not available.")
else:
num_gpus = torch.cuda.device_count()
for i in range(num_gpus):
with torch.cuda.device(i): # get the i th GPU
memory_stats = torch.cuda.memory_stats()
allocated_memory = memory_stats['allocated_bytes.all.current'] # in bytes
reserved_memory = memory_stats['reserved_bytes.all.current'] # in bytes
max_allocated_memory = memory_stats['allocated_bytes.all.peak'] # in bytes
print(f"GPU {i}:")
print(f" Allocated GPU memory: {allocated_memory / (1024 ** 3):.2f} GB")
print(f" Reserved GPU memory: {reserved_memory / (1024 ** 3):.2f} GB")
print(f" Max allocated GPU memory: {max_allocated_memory / (1024 ** 3):.2f} GB")
使用GPUtil
# 使用前需要pip install GPUtil安裝GPUtil
import GPUtil
gpus = GPUtil.getGPUs()
for gpu in gpus:
print(f"GPU {gpu.id}:")
print(f" GPU load: {gpu.load * 100:.2f}%")
print(f" GPU Memory Used: {gpu.memoryUsed} MB / {gpu.memoryTotal} MB")
print(f" GPU Memory Utilization: {gpu.memoryUtil * 100:.2f}%")
py3nvml?
import py3nvml
def monitor_gpus():
pynvml.nvmlInit() # 初始化 NVML 庫
try:
gpu_count = pynvml.nvmlDeviceGetCount() # 獲取 GPU 數量
for i in range(gpu_count):
handle = pynvml.nvmlDeviceGetHandleByIndex(i) # 獲取 GPU i 的句柄
name = pynvml.nvmlDeviceGetName(handle).decode('utf-8') # 獲取 GPU 名稱
memory_info = pynvml.nvmlDeviceGetMemoryInfo(handle) # 獲取顯存信息
total_memory = memory_info.total / (1024 ** 3) # 總顯存,單位 GB
free_memory = memory_info.free / (1024 ** 3) # 空閑顯存,單位 GB
used_memory = memory_info.used / (1024 ** 3) # 已用顯存,單位 GB
memory_utilization = (used_memory / total_memory) * 100 # 顯存使用率,百分比
print(f"GPU {i}: {name}")
print(f" 總顯存: {total_memory:.2f} GB")
print(f" 已用顯存: {used_memory:.2f} GB")
print(f" 空閑顯存: {free_memory:.2f} GB")
print(f" 顯存使用率: {memory_utilization:.2f}%\n")
finally:
pynvml.nvmlShutdown() # 釋放資源
if __name__ == "__main__":
monitor_gpus()