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

Method _upsample_2d

src/diffusers/models/upsampling.py:225–318  ·  view source on GitHub ↗

Fused `upsample_2d()` followed by `Conv2d()`. 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 order

(
        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

223 self.out_channels = out_channels
224
225 def _upsample_2d(
226 self,
227 hidden_states: torch.FloatTensor,
228 weight: Optional[torch.FloatTensor] = None,
229 kernel: Optional[torch.FloatTensor] = None,
230 factor: int = 2,
231 gain: float = 1,
232 ) -> torch.FloatTensor:
233 """Fused `upsample_2d()` followed by `Conv2d()`.
234
235 Padding is performed only once at the beginning, not between the operations. The fused op is considerably more
236 efficient than performing the same calculation using standard TensorFlow ops. It supports gradients of
237 arbitrary order.
238
239 Args:
240 hidden_states (`torch.FloatTensor`):
241 Input tensor of the shape `[N, C, H, W]` or `[N, H, W, C]`.
242 weight (`torch.FloatTensor`, *optional*):
243 Weight tensor of the shape `[filterH, filterW, inChannels, outChannels]`. Grouped convolution can be
244 performed by `inChannels = x.shape[0] // numGroups`.
245 kernel (`torch.FloatTensor`, *optional*):
246 FIR filter of the shape `[firH, firW]` or `[firN]` (separable). The default is `[1] * factor`, which
247 corresponds to nearest-neighbor upsampling.
248 factor (`int`, *optional*): Integer upsampling factor (default: 2).
249 gain (`float`, *optional*): Scaling factor for signal magnitude (default: 1.0).
250
251 Returns:
252 output (`torch.FloatTensor`):
253 Tensor of the shape `[N, C, H * factor, W * factor]` or `[N, H * factor, W * factor, C]`, and same
254 datatype as `hidden_states`.
255 """
256
257 assert isinstance(factor, int) and factor >= 1
258
259 # Setup filter kernel.
260 if kernel is None:
261 kernel = [1] * factor
262
263 # setup kernel
264 kernel = torch.tensor(kernel, dtype=torch.float32)
265 if kernel.ndim == 1:
266 kernel = torch.outer(kernel, kernel)
267 kernel /= torch.sum(kernel)
268
269 kernel = kernel * (gain * (factor**2))
270
271 if self.use_conv:
272 convH = weight.shape[2]
273 convW = weight.shape[3]
274 inC = weight.shape[1]
275
276 pad_value = (kernel.shape[0] - factor) - (convW - 1)
277
278 stride = (factor, factor)
279 # Determine data dimensions.
280 output_shape = (
281 (hidden_states.shape[2] - 1) * factor + convH,
282 (hidden_states.shape[3] - 1) * factor + convW,

Callers 1

forwardMethod · 0.95

Calls 1

upfirdn2d_nativeFunction · 0.85

Tested by

no test coverage detected