MCPcopy Create free account
hub / github.com/TencentARC/BrushNet / forward

Method forward

src/diffusers/models/upsampling.py:143–193  ·  view source on GitHub ↗
(
        self,
        hidden_states: torch.FloatTensor,
        output_size: Optional[int] = None,
        scale: float = 1.0,
    )

Source from the content-addressed store, hash-verified

141 self.Conv2d_0 = conv
142
143 def forward(
144 self,
145 hidden_states: torch.FloatTensor,
146 output_size: Optional[int] = None,
147 scale: float = 1.0,
148 ) -> torch.FloatTensor:
149 assert hidden_states.shape[1] == self.channels
150
151 if self.norm is not None:
152 hidden_states = self.norm(hidden_states.permute(0, 2, 3, 1)).permute(0, 3, 1, 2)
153
154 if self.use_conv_transpose:
155 return self.conv(hidden_states)
156
157 # Cast to float32 to as 'upsample_nearest2d_out_frame' op does not support bfloat16
158 # TODO(Suraj): Remove this cast once the issue is fixed in PyTorch
159 # https://github.com/pytorch/pytorch/issues/86679
160 dtype = hidden_states.dtype
161 if dtype == torch.bfloat16:
162 hidden_states = hidden_states.to(torch.float32)
163
164 # upsample_nearest_nhwc fails with large batch sizes. see https://github.com/huggingface/diffusers/issues/984
165 if hidden_states.shape[0] >= 64:
166 hidden_states = hidden_states.contiguous()
167
168 # if `output_size` is passed we force the interpolation output
169 # size and do not make use of `scale_factor=2`
170 if self.interpolate:
171 if output_size is None:
172 hidden_states = F.interpolate(hidden_states, scale_factor=2.0, mode="nearest")
173 else:
174 hidden_states = F.interpolate(hidden_states, size=output_size, mode="nearest")
175
176 # If the input is bfloat16, we cast back to bfloat16
177 if dtype == torch.bfloat16:
178 hidden_states = hidden_states.to(dtype)
179
180 # TODO(Suraj, Patrick) - clean up after weight dicts are correctly renamed
181 if self.use_conv:
182 if self.name == "conv":
183 if isinstance(self.conv, LoRACompatibleConv) and not USE_PEFT_BACKEND:
184 hidden_states = self.conv(hidden_states, scale)
185 else:
186 hidden_states = self.conv(hidden_states)
187 else:
188 if isinstance(self.Conv2d_0, LoRACompatibleConv) and not USE_PEFT_BACKEND:
189 hidden_states = self.Conv2d_0(hidden_states, scale)
190 else:
191 hidden_states = self.Conv2d_0(hidden_states)
192
193 return hidden_states
194
195
196class FirUpsample2D(nn.Module):

Callers

nothing calls this directly

Calls 2

toMethod · 0.45
interpolateMethod · 0.45

Tested by

no test coverage detected