# Outofmemory error when running pipieline.to("cuda")

**URL:** <https://discuss.huggingface.co/t/outofmemory-error-when-running-pipieline-to-cuda/33956>\
**Category:** 🧨 Diffusers\
**Created:** [March 16, 2023, 4:08pm UTC](https://discuss.huggingface.co/t/outofmemory-error-when-running-pipieline-to-cuda/33956 "2023-03-16T16:08:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![winecoding](https://avatars.discourse-cdn.com/v4/letter/w/839c29/32.png) [@winecoding](https://discuss.huggingface.co/u/winecoding)\
**Post date:** [March 16, 2023, 4:08pm UTC](https://discuss.huggingface.co/t/outofmemory-error-when-running-pipieline-to-cuda/33956/1 "2023-03-16T16:08:29Z")

</div>

I am trying to reproducing the [quickguide of diffuser](https://github.com/huggingface/diffusers), and got the following error messages when running **pipeline.to(“cuda”)** Which part in pipeline class I should modify to solve this issue?

> OutOfMemoryError Traceback (most recent call last)  
> Cell In[4], line 1  
> ----\> 1 pipeline.to(“cuda”)
> 
> File C:\ProgramData\Miniconda3\envs\hg\_diffuser\lib\site-packages\diffusers\pipelines\pipeline\_utils.py:396, in DiffusionPipeline.to(self, torch\_device, silence\_dtype\_warnings)  
> 383 if (  
> 384 module.dtype == torch.float16  
> 385 and str(torch\_device) in [“cpu”]  
> 386 and not silence\_dtype\_warnings  
> 387 and not is\_offloaded  
> 388 ):  
> 389 logger.warning(  
> 390 “Pipelines loaded with `torch_dtype=torch.float16` cannot run with `cpu` device. It”  
> 391 " is not recommended to move them to `cpu` as running them will fail. Please make"  
> (…)  
> 394 " `torch_dtype=torch.float16` argument, or use another device for inference."  
> 395 )  
> → 396 module.to(torch\_device)  
> 397 return self
> 
> File C:\ProgramData\Miniconda3\envs\hg\_diffuser\lib\site-packages\torch\nn\modules\module.py:1145, in Module.to(self, \*args, \*\*kwargs)  
> 1141 return t.to(device, dtype if t.is\_floating\_point() or t.is\_complex() else None,  
> 1142 non\_blocking, memory\_format=convert\_to\_format)  
> 1143 return t.to(device, dtype if t.is\_floating\_point() or t.is\_complex() else None, non\_blocking)  
> → 1145 return self.\_apply(convert)
> 
> File C:\ProgramData\Miniconda3\envs\hg\_diffuser\lib\site-packages\torch\nn\modules\module.py:797, in Module.\_apply(self, fn)  
> 795 def \_apply(self, fn):  
> 796 for module in self.children():  
> → 797 module.\_apply(fn)  
> 799 def compute\_should\_use\_set\_data(tensor, tensor\_applied):  
> 800 if torch.\_has\_compatible\_shallow\_copy\_type(tensor, tensor\_applied):  
> 801 # If the new tensor has compatible tensor type as the existing tensor,  
> 802 # the current behavior is to change the tensor in-place using `.data =`,  
> (…)  
> 807 # global flag to let the user control whether they want the future  
> 808 # behavior of overwriting the existing tensor or not.
> 
> File C:\ProgramData\Miniconda3\envs\hg\_diffuser\lib\site-packages\torch\nn\modules\module.py:797, in Module.\_apply(self, fn)  
> 795 def \_apply(self, fn):  
> 796 for module in self.children():  
> → 797 module.\_apply(fn)  
> 799 def compute\_should\_use\_set\_data(tensor, tensor\_applied):  
> 800 if torch.\_has\_compatible\_shallow\_copy\_type(tensor, tensor\_applied):  
> 801 # If the new tensor has compatible tensor type as the existing tensor,  
> 802 # the current behavior is to change the tensor in-place using `.data =`,  
> (…)  
> 807 # global flag to let the user control whether they want the future  
> 808 # behavior of overwriting the existing tensor or not.
> 
> ```
> [... skipping similar frames: Module._apply at line 797 (2 times)]
> 
> ```
> 
> File C:\ProgramData\Miniconda3\envs\hg\_diffuser\lib\site-packages\torch\nn\modules\module.py:797, in Module.\_apply(self, fn)  
> 795 def \_apply(self, fn):  
> 796 for module in self.children():  
> → 797 module.\_apply(fn)  
> 799 def compute\_should\_use\_set\_data(tensor, tensor\_applied):  
> 800 if torch.\_has\_compatible\_shallow\_copy\_type(tensor, tensor\_applied):  
> 801 # If the new tensor has compatible tensor type as the existing tensor,  
> 802 # the current behavior is to change the tensor in-place using `.data =`,  
> (…)  
> 807 # global flag to let the user control whether they want the future  
> 808 # behavior of overwriting the existing tensor or not.
> 
> File C:\ProgramData\Miniconda3\envs\hg\_diffuser\lib\site-packages\torch\nn\modules\module.py:820, in Module.\_apply(self, fn)  
> 816 # Tensors stored in modules are graph leaves, and we don’t want to  
> 817 # track autograd history of `param_applied`, so we have to use  
> 818 # `with torch.no_grad():`  
> 819 with torch.no\_grad():  
> → 820 param\_applied = fn(param)  
> 821 should\_use\_set\_data = compute\_should\_use\_set\_data(param, param\_applied)  
> 822 if should\_use\_set\_data:
> 
> File C:\ProgramData\Miniconda3\envs\hg\_diffuser\lib\site-packages\torch\nn\modules\module.py:1143, in Module.to..convert(t)  
> 1140 if convert\_to\_format is not None and t.dim() in (4, 5):  
> 1141 return t.to(device, dtype if t.is\_floating\_point() or t.is\_complex() else None,  
> 1142 non\_blocking, memory\_format=convert\_to\_format)  
> → 1143 return t.to(device, dtype if t.is\_floating\_point() or t.is\_complex() else None, non\_blocking)
> 
> OutOfMemoryError: CUDA out of memory. Tried to allocate 58.00 MiB (GPU 0; 4.00 GiB total capacity; 3.41 GiB already allocated; 0 bytes free; 3.47 GiB reserved in total by PyTorch) If reserved memory is \>\> allocated memory try setting max\_split\_size\_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH\_CUDA\_ALLOC\_CONF

---

<div class="post-metadata">

**Author:** ![williamberman](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/williamberman/32/14112_2.png) [@williamberman](https://discuss.huggingface.co/u/williamberman)\
**Post date:** [March 16, 2023, 11:51pm UTC](https://discuss.huggingface.co/t/outofmemory-error-when-running-pipieline-to-cuda/33956/2 "2023-03-16T23:51:57Z")

</div>

Hey @winecoding it looks like your gpu only has 4GB of memory. We support a few memory optimizations documented here [Memory and speed](https://huggingface.co/docs/diffusers/optimization/fp16) I would recommend looking into accelerate offloading
