huggingface / transformers

🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.
https://huggingface.co/transformers
Apache License 2.0
131.33k stars 26.12k forks source link

Memory leak when using CLIPTextModel #31439

Open minsuk00 opened 2 months ago

minsuk00 commented 2 months ago

System Info

Who can help?

Information

Tasks

Reproduction

I can't free GPU memory after I use CLIPTextModel Also, memory is allocated in another device for some reason

problem should be reproduced by using the following code snippet

from transformers import CLIPTextModel
import torch

clip_text_model = CLIPTextModel.from_pretrained("openai/clip-vit-large-patch14").to("cuda:1")
del clip_text_model
torch.cuda.empty_cache()

Expected behavior

I've also tried using garbage collection and explicitly moving model to cpu, but they don't work.

younesbelkada commented 2 months ago

Hi @minsuk00 you can also try the release_memory utility method from accelerate.utils - cc @muellerzr

minsuk00 commented 2 months ago

@younesbelkada -cc @muellerz Thanks for the suggestion, but it doesn't seem to work. clip_text_model = accelerate.utils.release_memory(clip_text_model) does not free any GPU memory.

Additionally, calling clip_text_model.cpu() or torch.cuda.empty_cache() simply results in the behavior described above.

amyeroberts commented 1 month ago

cc @muellerzr regarding the accelerate behaviour.

Regarding torch.cuda.empty_cache() it's recommended that this function is not manually used c.f. a related issue, and this discussion in the pytorch forum