ops.py 6.17 KB
Newer Older
comfyanonymous's avatar
comfyanonymous committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
"""
    This file is part of ComfyUI.
    Copyright (C) 2024 Stability AI

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
"""

comfyanonymous's avatar
comfyanonymous committed
19
import torch
20
21
22
23
24
25
26
import comfy.model_management

def cast_bias_weight(s, input):
    bias = None
    non_blocking = comfy.model_management.device_supports_non_blocking(input.device)
    if s.bias is not None:
        bias = s.bias.to(device=input.device, dtype=input.dtype, non_blocking=non_blocking)
27
28
        if s.bias_function is not None:
            bias = s.bias_function(bias)
29
    weight = s.weight.to(device=input.device, dtype=input.dtype, non_blocking=non_blocking)
30
31
    if s.weight_function is not None:
        weight = s.weight_function(weight)
32
33
    return weight, bias

comfyanonymous's avatar
comfyanonymous committed
34

comfyanonymous's avatar
comfyanonymous committed
35
36
class disable_weight_init:
    class Linear(torch.nn.Linear):
37
        comfy_cast_weights = False
38
39
40
        weight_function = None
        bias_function = None

comfyanonymous's avatar
comfyanonymous committed
41
42
        def reset_parameters(self):
            return None
43

44
45
46
47
48
49
50
51
52
53
        def forward_comfy_cast_weights(self, input):
            weight, bias = cast_bias_weight(self, input)
            return torch.nn.functional.linear(input, weight, bias)

        def forward(self, *args, **kwargs):
            if self.comfy_cast_weights:
                return self.forward_comfy_cast_weights(*args, **kwargs)
            else:
                return super().forward(*args, **kwargs)

comfyanonymous's avatar
comfyanonymous committed
54
    class Conv2d(torch.nn.Conv2d):
55
        comfy_cast_weights = False
56
57
58
        weight_function = None
        bias_function = None

comfyanonymous's avatar
comfyanonymous committed
59
60
        def reset_parameters(self):
            return None
61

62
63
64
65
66
67
68
69
70
71
        def forward_comfy_cast_weights(self, input):
            weight, bias = cast_bias_weight(self, input)
            return self._conv_forward(input, weight, bias)

        def forward(self, *args, **kwargs):
            if self.comfy_cast_weights:
                return self.forward_comfy_cast_weights(*args, **kwargs)
            else:
                return super().forward(*args, **kwargs)

comfyanonymous's avatar
comfyanonymous committed
72
    class Conv3d(torch.nn.Conv3d):
73
        comfy_cast_weights = False
74
75
76
        weight_function = None
        bias_function = None

comfyanonymous's avatar
comfyanonymous committed
77
78
        def reset_parameters(self):
            return None
comfyanonymous's avatar
comfyanonymous committed
79

80
81
82
83
84
85
86
87
88
89
        def forward_comfy_cast_weights(self, input):
            weight, bias = cast_bias_weight(self, input)
            return self._conv_forward(input, weight, bias)

        def forward(self, *args, **kwargs):
            if self.comfy_cast_weights:
                return self.forward_comfy_cast_weights(*args, **kwargs)
            else:
                return super().forward(*args, **kwargs)

comfyanonymous's avatar
comfyanonymous committed
90
    class GroupNorm(torch.nn.GroupNorm):
91
        comfy_cast_weights = False
92
93
94
        weight_function = None
        bias_function = None

comfyanonymous's avatar
comfyanonymous committed
95
96
        def reset_parameters(self):
            return None
97

98
99
100
101
102
103
104
105
106
107
108
        def forward_comfy_cast_weights(self, input):
            weight, bias = cast_bias_weight(self, input)
            return torch.nn.functional.group_norm(input, self.num_groups, weight, bias, self.eps)

        def forward(self, *args, **kwargs):
            if self.comfy_cast_weights:
                return self.forward_comfy_cast_weights(*args, **kwargs)
            else:
                return super().forward(*args, **kwargs)


comfyanonymous's avatar
comfyanonymous committed
109
    class LayerNorm(torch.nn.LayerNorm):
110
        comfy_cast_weights = False
111
112
113
        weight_function = None
        bias_function = None

comfyanonymous's avatar
comfyanonymous committed
114
115
        def reset_parameters(self):
            return None
116

117
        def forward_comfy_cast_weights(self, input):
comfyanonymous's avatar
comfyanonymous committed
118
119
120
121
122
            if self.weight is not None:
                weight, bias = cast_bias_weight(self, input)
            else:
                weight = None
                bias = None
123
124
125
126
127
128
129
130
            return torch.nn.functional.layer_norm(input, self.normalized_shape, weight, bias, self.eps)

        def forward(self, *args, **kwargs):
            if self.comfy_cast_weights:
                return self.forward_comfy_cast_weights(*args, **kwargs)
            else:
                return super().forward(*args, **kwargs)

comfyanonymous's avatar
comfyanonymous committed
131
132
    class ConvTranspose2d(torch.nn.ConvTranspose2d):
        comfy_cast_weights = False
133
134
135
        weight_function = None
        bias_function = None

comfyanonymous's avatar
comfyanonymous committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
        def reset_parameters(self):
            return None

        def forward_comfy_cast_weights(self, input, output_size=None):
            num_spatial_dims = 2
            output_padding = self._output_padding(
                input, output_size, self.stride, self.padding, self.kernel_size,
                num_spatial_dims, self.dilation)

            weight, bias = cast_bias_weight(self, input)
            return torch.nn.functional.conv_transpose2d(
                input, weight, bias, self.stride, self.padding,
                output_padding, self.groups, self.dilation)

        def forward(self, *args, **kwargs):
            if self.comfy_cast_weights:
                return self.forward_comfy_cast_weights(*args, **kwargs)
            else:
                return super().forward(*args, **kwargs)

comfyanonymous's avatar
comfyanonymous committed
156
157
158
159
160
161
162
163
    @classmethod
    def conv_nd(s, dims, *args, **kwargs):
        if dims == 2:
            return s.Conv2d(*args, **kwargs)
        elif dims == 3:
            return s.Conv3d(*args, **kwargs)
        else:
            raise ValueError(f"unsupported dimensions: {dims}")
164

165

comfyanonymous's avatar
comfyanonymous committed
166
167
class manual_cast(disable_weight_init):
    class Linear(disable_weight_init.Linear):
168
        comfy_cast_weights = True
169

comfyanonymous's avatar
comfyanonymous committed
170
    class Conv2d(disable_weight_init.Conv2d):
171
        comfy_cast_weights = True
172

comfyanonymous's avatar
comfyanonymous committed
173
    class Conv3d(disable_weight_init.Conv3d):
174
        comfy_cast_weights = True
175

comfyanonymous's avatar
comfyanonymous committed
176
    class GroupNorm(disable_weight_init.GroupNorm):
177
        comfy_cast_weights = True
178

comfyanonymous's avatar
comfyanonymous committed
179
    class LayerNorm(disable_weight_init.LayerNorm):
180
        comfy_cast_weights = True
comfyanonymous's avatar
comfyanonymous committed
181
182
183

    class ConvTranspose2d(disable_weight_init.ConvTranspose2d):
        comfy_cast_weights = True