Fonction PyTorch torch.cuda.memory_allocated
Manuel de référence Pytorch torch
torch.cuda.memory_allocatedest une fonction de PyTorch utilisée pour obtenir la mémoire GPU allouée. Elle renvoie la taille de la mémoire allouée sur le périphérique CUDA actuel (en bytes).
Définition de fonction
torch.cuda.memory_allocated(device=None)
Exemple d'utilisation
Exemple
import torch
# Vérifier la mémoire GPU allouée
if torch.cuda.is_available():
print(f"Mémoire initiale : {torch.cuda.memory_allocated()} bytes")
# Créer un tenseur
x = torch.randn(1000, 1000).cuda()
print(f"Après allocation : {torch.cuda.memory_allocated()} bytes")
# Supprimer le tenseur
del x
print(f"Après suppression : {torch.cuda.memory_allocated()} bytes")
else:
print("CUDA non disponible")
# Vérifier la mémoire GPU allouée
if torch.cuda.is_available():
print(f"Mémoire initiale : {torch.cuda.memory_allocated()} bytes")
# Créer un tenseur
x = torch.randn(1000, 1000).cuda()
print(f"Après allocation : {torch.cuda.memory_allocated()} bytes")
# Supprimer le tenseur
del x
print(f"Après suppression : {torch.cuda.memory_allocated()} bytes")
else:
print("CUDA non disponible")
Autres extensions