PyTorch torch.cuda.memory_allocated 関数
torch.cuda.memory_allocatedPyTorch において、割り当てられたGPUメモリを取得するための関数です。現在の CUDA デバイスに割り当てられたメモリサイズを(バイト単位で)返します。
関数定義
torch.cuda.memory_allocated(device=None)
使用例
例
import torch
# 割り当てられたGPUメモリを確認
if torch.cuda.is_available():
print(f"Initial memory: {torch.cuda.memory_allocated()} bytes")
# テンソルを作成
x = torch.randn(1000, 1000).cuda()
print(f"After allocation: {torch.cuda.memory_allocated()} bytes")
# テンソルを削除
del x
print(f"After deletion: {torch.cuda.memory_allocated()} bytes")
else:
print("CUDA not available")
# 割り当てられたGPUメモリを確認
if torch.cuda.is_available():
print(f"Initial memory: {torch.cuda.memory_allocated()} bytes")
# テンソルを作成
x = torch.randn(1000, 1000).cuda()
print(f"After allocation: {torch.cuda.memory_allocated()} bytes")
# テンソルを削除
del x
print(f"After deletion: {torch.cuda.memory_allocated()} bytes")
else:
print("CUDA not available")
その他の拡張
Pytorch torch リファレンスマニュアル