File size: 2,119 Bytes
0070fce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Reference: https://github.com/comfyanonymous/ComfyUI/blob/master/cuda_malloc.py


def get_gpu_names():
    from subprocess import check_output

    gpu_names = set()
    out = check_output(["nvidia-smi", "-L"])
    for line in out.split(b"\n"):
        if len(line) > 0:
            gpu_names.add(line.decode("utf-8").split(" (UUID")[0])
    return gpu_names


def cuda_malloc_supported():
    blacklist = {
        "GeForce GTX TITAN X",
        "GeForce GTX 980",
        "GeForce GTX 970",
        "GeForce GTX 960",
        "GeForce GTX 950",
        "GeForce 945M",
        "GeForce 940M",
        "GeForce 930M",
        "GeForce 920M",
        "GeForce 910M",
        "GeForce GTX 750",
        "GeForce GTX 745",
        "Quadro K620",
        "Quadro K1200",
        "Quadro K2200",
        "Quadro M500",
        "Quadro M520",
        "Quadro M600",
        "Quadro M620",
        "Quadro M1000",
        "Quadro M1200",
        "Quadro M2000",
        "Quadro M2200",
        "Quadro M3000",
        "Quadro M4000",
        "Quadro M5000",
        "Quadro M5500",
        "Quadro M6000",
        "GeForce MX110",
        "GeForce MX130",
        "GeForce 830M",
        "GeForce 840M",
        "GeForce GTX 850M",
        "GeForce GTX 860M",
        "GeForce GTX 1650",
        "GeForce GTX 1630",
        "Tesla M4",
        "Tesla M6",
        "Tesla M10",
        "Tesla M40",
        "Tesla M60",
    }

    try:
        names = get_gpu_names()
    except Exception:
        return False

    for x in names:
        if "NVIDIA" in x:
            for b in blacklist:
                if b in x:
                    return False
            return True

    return False


def try_cuda_malloc():
    import os

    if not cuda_malloc_supported():
        print("Failed to use cudaMallocAsync backend...")
        return

    env_var = os.environ.get("PYTORCH_CUDA_ALLOC_CONF", None)
    if env_var is None:
        env_var = "backend:cudaMallocAsync"
    else:
        env_var += ",backend:cudaMallocAsync"

    os.environ["PYTORCH_CUDA_ALLOC_CONF"] = env_var
    print("Using cudaMallocAsync backend")