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

Method _downsample_2d

src/diffusers/models/downsampling.py:186–250  ·  view source on GitHub ↗

Fused `Conv2d()` followed by `downsample_2d()`. Padding is performed only once at the beginning, not between the operations. The fused op is considerably more efficient than performing the same calculation using standard TensorFlow ops. It supports gradients of arbitrary orde

(
        self,
        hidden_states: torch.FloatTensor,
        weight: Optional[torch.FloatTensor] = None,
        kernel: Optional[torch.FloatTensor] = None,
        factor: int = 2,
        gain: float = 1,
    )

Source from the content-addressed store, hash-verified

184 self.out_channels = out_channels
185
186 def _downsample_2d(
187 self,
188 hidden_states: torch.FloatTensor,
189 weight: Optional[torch.FloatTensor] = None,
190 kernel: Optional[torch.FloatTensor] = None,
191 factor: int = 2,
192 gain: float = 1,
193 ) -> torch.FloatTensor:
194 """Fused `Conv2d()` followed by `downsample_2d()`.
195 Padding is performed only once at the beginning, not between the operations. The fused op is considerably more
196 efficient than performing the same calculation using standard TensorFlow ops. It supports gradients of
197 arbitrary order.
198
199 Args:
200 hidden_states (`torch.FloatTensor`):
201 Input tensor of the shape `[N, C, H, W]` or `[N, H, W, C]`.
202 weight (`torch.FloatTensor`, *optional*):
203 Weight tensor of the shape `[filterH, filterW, inChannels, outChannels]`. Grouped convolution can be
204 performed by `inChannels = x.shape[0] // numGroups`.
205 kernel (`torch.FloatTensor`, *optional*):
206 FIR filter of the shape `[firH, firW]` or `[firN]` (separable). The default is `[1] * factor`, which
207 corresponds to average pooling.
208 factor (`int`, *optional*, default to `2`):
209 Integer downsampling factor.
210 gain (`float`, *optional*, default to `1.0`):
211 Scaling factor for signal magnitude.
212
213 Returns:
214 output (`torch.FloatTensor`):
215 Tensor of the shape `[N, C, H // factor, W // factor]` or `[N, H // factor, W // factor, C]`, and same
216 datatype as `x`.
217 """
218
219 assert isinstance(factor, int) and factor >= 1
220 if kernel is None:
221 kernel = [1] * factor
222
223 # setup kernel
224 kernel = torch.tensor(kernel, dtype=torch.float32)
225 if kernel.ndim == 1:
226 kernel = torch.outer(kernel, kernel)
227 kernel /= torch.sum(kernel)
228
229 kernel = kernel * gain
230
231 if self.use_conv:
232 _, _, convH, convW = weight.shape
233 pad_value = (kernel.shape[0] - factor) + (convW - 1)
234 stride_value = [factor, factor]
235 upfirdn_input = upfirdn2d_native(
236 hidden_states,
237 torch.tensor(kernel, device=hidden_states.device),
238 pad=((pad_value + 1) // 2, pad_value // 2),
239 )
240 output = F.conv2d(upfirdn_input, weight, stride=stride_value, padding=0)
241 else:
242 pad_value = kernel.shape[0] - factor
243 output = upfirdn2d_native(

Callers 1

forwardMethod · 0.95

Calls 1

upfirdn2d_nativeFunction · 0.85

Tested by

no test coverage detected