Fonction torch.cuda.empty_cache de PyTorch


Pytorch torch 参考手册Manuel de référence PyTorch torch

torch.cuda.empty_cacheC'est une fonction de PyTorch utilisée pour vider le cache CUDA. Elle libère la mémoire en cache inutilisée afin qu'elle puisse être réallouée.

Définition de fonction

torch.cuda.empty_cache()

Exemple d'utilisation

Exemple

import torch

# Vider le cache CUDA
if torch.cuda.is_available():
    # Créer quelques tenseurs
    x = torch.randn(1000, 1000).cuda()
    y = torch.randn(1000, 1000).cuda()

    # Vider le cache
    torch.cuda.empty_cache()
    print("Cache CUDA vidé")

    # Vérifier la mémoire
    print(f"Mémoire allouée : {torch.cuda.memory_allocated() / 1024**2:.2f} MB")
else:
    print("CUDA non disponible")

Pytorch torch 参考手册Manuel de référence PyTorch torch

Autres extensions