deforum / deforum/sd-webui-deforum

[Bug]: Extra models are not unloaded if Deforum OOM-crashes

Open
#761 0 comments 0 reactions 1 assignee Claimed by @hithereai View on GitHub
bug
Dominant language
Python
Stars
2.9k
Forks
407
PR merge metrics
No merged PRs in 30d

Description

### Have you read the latest version of the FAQ?

- [X] I have visited the FAQ page right now and my issue is not present there

### Is there an existing issue for this?

- [X] I have searched the existing issues and checked the recent builds/commits of both this extension and the webui

### Are you using the latest version of the Deforum extension?

- [X] I have Deforum updated to the lastest version and I still have the issue.

### What happened?

Deforum OOMs at lower resolution (running fine) after OOMing on higher res, the depth models are not unloaded

### Steps to reproduce the problem

1. Launch Deforum on extra large resolution with 3D models on
2. It OOMs
3. The depth models are still in VRAM (see the task manager)

### What should have happened?

Unload the models with try catch

### WebUI and Deforum extension Commit IDs

webui commit id - b6af0a3809ea869fb180633f9affcae4b199ffcf
deforum exten commit id - f761a2f1

### Torch version

2.0.0+cu118

### On which platform are you launching the webui with the extension?

Local PC setup (Windows)

### Deforum settings

[20230603010826_settings.txt](https://github.com/deforum-art/sd-webui-deforum/files/11639334/20230603010826_settings.txt)

### Webui core settings

[config.json.txt](https://github.com/deforum-art/sd-webui-deforum/files/11639332/config.json.txt)

### Console logs

```Shell
PS C:\Users\kabachuha\stable-diffusion-webui> .\webui-user.bat
venv "C:\Users\kabachuha\stable-diffusion-webui\venv\Scripts\Python.exe"
Python 3.10.11 (tags/v3.10.11:7d4cc5a, Apr 5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)]
Version: v1.3.1
Commit hash: b6af0a3809ea869fb180633f9affcae4b199ffcf
Installing requirements

Launching Web UI with arguments: --medvram --no-half-vae
No module 'xformers'. Proceeding without it.
ControlNet v1.1.212
ControlNet v1.1.212
Loading weights [19151bff86] from C:\Users\kabachuha\stable-diffusion-webui\models\Stable-diffusion\breakdomain_M2000.safetensors
Create LRU cache (max_size=16) for preprocessor results.
Create LRU cache (max_size=16) for preprocessor results.
*Deforum ControlNet support: enabled*
Creating model from config: C:\Users\kabachuha\stable-diffusion-webui\configs\v1-inference.yaml
LatentDiffusion: Running in eps-prediction mode
Running on local URL: http://127.0.0.1:7860

To create a public link, set `share=True` in `launch()`.
Create LRU cache (max_size=16) for preprocessor results.
Startup time: 6.8s (import torch: 1.4s, import gradio: 1.2s, import ldm: 0.9s, other imports: 0.8s, setup codeformer: 0.2s, load scripts: 1.2s, create ui: 0.8s, gradio launch: 0.3s).
DiffusionWrapper has 859.52 M params.
Loading VAE weights specified in settings: C:\Users\kabachuha\stable-diffusion-webui\models\VAE\vae-ft-mse-840000-ema-pruned.safetensors
Applying optimization: Doggettx... done.
Textual inversion embeddings loaded(7): bad-artist-anime, bad-hands-5, bad-image-v2-39000, bad_prompt_version2, EasyNegative, NG_DeepNegative_V1_4T, verybadimagenegative_v1.3
Model loaded in 4.3s (load weights from disk: 1.0s, create model: 0.7s, apply weights to model: 1.6s, apply half(): 0.6s, load VAE: 0.4s).
Loading preprocessor: reference_only
preprocessor resolution = 512
Calling preprocessor reference_only outside of cache.
0%| | 0/20 [00:00> allocated memory try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF
*END OF TRACEBACK*

User friendly error message:
Error: CUDA out of memory. Tried to allocate 56.00 MiB (GPU 0; 4.00 GiB total capacity; 2.16 GiB already allocated; 0 bytes free; 2.35 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. Check your schedules/ init values please. Also make sure you don't have a backwards slash in any of your PATHs - use / instead of \.
Deforum progress: 0%| | 1/26110 [00:04<31:19:44, 4.32s/it]
Deforum extension for auto1111 webui, v2.4b
Git commit: f761a2f1
Video to extract: ink.webm
Extracting video (1 every 1) frames to C:\Users\kabachuha\stable-diffusion-webui\outputs/img2img-images\Deforum_20230601165518\inputframes...
Trying to extract frames from video with input FPS of 23.976023976023978. Please wait patiently.
Extracted 2611 frames from video in 7.98 seconds!
Using 2611 input frames from C:\Users\kabachuha\stable-diffusion-webui\outputs/img2img-images\Deforum_20230601165518\inputframes...
Using init_image from video: C:\Users\kabachuha\stable-diffusion-webui\outputs\img2img-images\Deforum_20230601165518\inputframes\ink000000000.jpg
ControlNet #1 is in LoopBack mode, skipping video/ image extraction stage.
Saving animation frames to:
C:\Users\kabachuha\stable-diffusion-webui\outputs/img2img-images\Deforum_20230601165518
img_size [640, 450]
Using cache found in C:\Users\kabachuha/.cache\torch\hub\intel-isl_MiDaS_master
Params passed to Resize transform:
width: 450
height: 640
resize_target: True
keep_aspect_ratio: True
ensure_multiple_of: 32
resize_method: minimal
Using pretrained resource url::https://github.com/isl-org/ZoeDepth/releases/download/v1.0/ZoeD_M12_NK.pt
Loaded successfully

*START OF TRACEBACK*
Traceback (most recent call last):
File "C:\Users\kabachuha\stable-diffusion-webui\extensions\sd-webui-deforum\scripts\deforum_helpers\run_deforum.py", line 78, in run_deforum
render_animation(args, anim_args, video_args, parseq_args, loop_args, controlnet_args, root)
File "C:\Users\kabachuha\stable-diffusion-webui\extensions\sd-webui-deforum\scripts\deforum_helpers\render.py", line 109, in render_animation
depth_model = DepthModel(root.models_path, device, root.half_precision, keep_in_vram=keep_in_vram, depth_algorithm=anim_args.depth_algorithm, Width=args.W, Height=args.H,
File "C:\Users\kabachuha\stable-diffusion-webui\extensions\sd-webui-deforum\scripts\deforum_helpers\depth.py", line 32, in __new__
cls._instance._initialize(models_path=args[0], device=args[1], half_precision=not cmd_opts.no_half, keep_in_vram=keep_in_vram, depth_algorithm=depth_algorithm, Width=Width, Height=Height, midas_weight=midas_weight)
File "C:\Users\kabachuha\stable-diffusion-webui\extensions\sd-webui-deforum\scripts\deforum_helpers\depth.py", line 48, in _initialize
self._initialize_model()
File "C:\Users\kabachuha\stable-diffusion-webui\extensions\sd-webui-deforum\scripts\deforum_helpers\depth.py", line 53, in _initialize_model
self.zoe_depth = ZoeDepth(self.Width, self.Height)
File "C:\Users\kabachuha\stable-diffusion-webui\extensions\sd-webui-deforum\scripts\deforum_helpers\depth_zoe.py", line 11, in __init__
self.zoe = self.model_zoe.to(self.DEVICE)
File "C:\Users\kabachuha\stable-diffusion-webui\extensions\sd-webui-deforum\scripts\deforum_helpers\src\zoedepth\models\depth_model.py", line 42, in to
return super().to(device)
File "C:\Users\kabachuha\stable-diffusion-webui\venv\lib\site-packages\torch\nn\modules\module.py", line 1145, in to
return self._apply(convert)
File "C:\Users\kabachuha\stable-diffusion-webui\venv\lib\site-packages\torch\nn\modules\module.py", line 797, in _apply
module._apply(fn)
File "C:\Users\kabachuha\stable-diffusion-webui\venv\lib\site-packages\torch\nn\modules\module.py", line 797, in _apply
module._apply(fn)
File "C:\Users\kabachuha\stable-diffusion-webui\venv\lib\site-packages\torch\nn\modules\module.py", line 797, in _apply
module._apply(fn)
[Previous line repeated 5 more times]
File "C:\Users\kabachuha\stable-diffusion-webui\venv\lib\site-packages\torch\nn\modules\module.py", line 820, in _apply
param_applied = fn(param)
File "C:\Users\kabachuha\stable-diffusion-webui\venv\lib\site-packages\torch\nn\modules\module.py", line 1143, in convert
return t.to(device, dtype if t.is_floating_point() or t.is_complex() else None, non_blocking)
torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 20.00 MiB (GPU 0; 4.00 GiB total capacity; 2.30 GiB already allocated; 0 bytes free; 2.36 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
*END OF TRACEBACK*

User friendly error message:
Error: CUDA out of memory. Tried to allocate 20.00 MiB (GPU 0; 4.00 GiB total capacity; 2.30 GiB already allocated; 0 bytes free; 2.36 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. Check your schedules/ init values please. Also make sure you don't have a backwards slash in any of your PATHs - use / instead of \.
Interrupted with signal 2 in
```

### Additional information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.