File size: 554 Bytes
19b3da3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import gc

import torch


def clear_cuda_and_gc():
    clear_cuda()
    clear_gc()


def clear_cuda():
    torch.cuda.empty_cache()


def clear_gc():
    gc.collect()


def auto_clear_cuda_and_gc(controlnet):
    def auto_clear_cuda_and_gc_wrapper(func):
        def wrapper(*args, **kwargs):
            try:
                return func(*args, **kwargs)
            except Exception as e:
                controlnet.cleanup()
                clear_cuda_and_gc()
                raise e

        return wrapper

    return auto_clear_cuda_and_gc_wrapper