# Cuda out of memory - knowledge distillation

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fcuda-out-of-memory-knowledge-distillation%2F73949%3E%5C
**Category:** 🤗Accelerate\
**Created:** [February 20, 2024, 9:59am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fcuda-out-of-memory-knowledge-distillation%2F73949 "2024-02-20T09:59:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ninagroot](https://avatars.discourse-cdn.com/v4/letter/n/8baadc/32.png) [@ninagroot](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fninagroot)%5C
**Post date:** [February 20, 2024, 9:59am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fcuda-out-of-memory-knowledge-distillation%2F73949%2F1 "2024-02-20T09:59:40Z")

</div>

My code is available in the google folder below:  
[https://drive.google.com/drive/folders/18zpr\_RuDY59Bu94M31z4492GYUlwoS48?usp=share\_link](https://drive.google.com/drive/folders/18zpr_RuDY59Bu94M31z4492GYUlwoS48?usp=share_link)

I run the new\_ddp.py file using the jobscript\_new\_ddp file. With the command: sbatch jobscript\_new\_ddp

Here is my system info:

- `Accelerate` version: 0.26.1
- Platform: Linux-4.18.0-372.57.1.el8\_6.x86\_64-x86\_64-with-glibc2.28
- Python version: 3.9.18
- Numpy version: 1.26.1
- PyTorch version (GPU?): 2.1.2+cu121 (False)
- PyTorch XPU available: False
- PyTorch NPU available: False
- System RAM: 251.38 GB
- `Accelerate` default config:
  - compute\_environment: LOCAL\_MACHINE
  - distributed\_type: MULTI\_GPU
  - mixed\_precision: fp16
  - use\_cpu: False
  - debug: False
  - num\_processes: 2
  - machine\_rank: 0
  - num\_machines: 1
  - gpu\_ids: all
  - rdzv\_backend: static
  - same\_network: True
  - main\_training\_function: main
  - downcast\_bf16: no
  - tpu\_use\_cluster: False
  - tpu\_use\_sudo: False
  - tpu\_env: 

I keep getting the following CUDA error, even though I run the code using a very small dataset and two GPUs. I should have more than enough memory.  
File “/gpfs/home2/ngroot/new\_ddp.py”, line 174, in  
trainer.train()  
File “/home/ngroot/anaconda3/envs/llmke/lib/python3.9/site-packages/transformers/trainer.py”, line 1539, in train  
return inner\_training\_loop(  
File “/home/ngroot/anaconda3/envs/llmke/lib/python3.9/site-packages/transformers/trainer.py”, line 1944, in \_inner\_training\_loop  
self.\_maybe\_log\_save\_evaluate(tr\_loss, model, trial, epoch, ignore\_keys\_for\_eval)  
File “/home/ngroot/anaconda3/envs/llmke/lib/python3.9/site-packages/transformers/trainer.py”, line 2291, in \_maybe\_log\_save\_evaluate  
metrics = self.evaluate(ignore\_keys=ignore\_keys\_for\_eval)  
File “/home/ngroot/anaconda3/envs/llmke/lib/python3.9/site-packages/transformers/trainer.py”, line 3095, in evaluate  
output = eval\_loop(  
File “/home/ngroot/anaconda3/envs/llmke/lib/python3.9/site-packages/transformers/trainer.py”, line 3310, in evaluation\_loop  
preds\_host = logits if preds\_host is None else nested\_concat(preds\_host, logits, padding\_index=-100)  
File “/home/ngroot/anaconda3/envs/llmke/lib/python3.9/site-packages/transformers/trainer\_pt\_utils.py”, line 123, in nested\_concat  
return torch\_pad\_and\_concatenate(tensors, new\_tensors, padding\_index=padding\_index)  
File “/home/ngroot/anaconda3/envs/llmke/lib/python3.9/site-packages/transformers/trainer\_pt\_utils.py”, line 82, in torch\_pad\_and\_concatenate  
return torch.cat((tensor1, tensor2), dim=0)  
torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 5.57 GiB. GPU 0 has a total capacty of 39.39 GiB of which 5.26 GiB is free. Including non-PyTorch memory, this process has 34.12 GiB memory in use. Of the allocated memory 31.64 GiB is allocated by PyTorch, and 1.73 GiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting max\_split\_size\_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH\_CUDA\_ALLOC\_CONF

Does anyone have suggestions on what I am doing wrong and how to fix it?  
Thanks.

---

<div class="post-metadata">

**Author:** ![muellerzr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/muellerzr/32/21583_2.png) [@muellerzr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmuellerzr)%5C
**Post date:** [February 29, 2024, 5:07pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fcuda-out-of-memory-knowledge-distillation%2F73949%2F2 "2024-02-29T17:07:46Z")

</div>

First thing I can think of, let’s clean up this code.

Don’t call `accelerator.prepare()`. The `Trainer` uses accelerate under the hood. Does this still occur if you skip this step?

(You also don’t need to move any models to devices)
