MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / _upsample_2d

Method _upsample_2d

diffusers/src/diffusers/models/upsampling.py:216–309  ·  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.Tensor,
        weight: Optional[torch.Tensor] = None,
        kernel: Optional[torch.Tensor] = None,
        factor: int = 2,
        gain: float = 1,
    )

Source from the content-addressed store, hash-verified

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

Callers 1

forwardMethod · 0.95

Calls 1

upfirdn2d_nativeFunction · 0.85

Tested by

no test coverage detected