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

Method forward

diffusers/src/diffusers/models/upsampling.py:142–184  ·  view source on GitHub ↗
(self, hidden_states: torch.Tensor, output_size: Optional[int] = None, *args, **kwargs)

Source from the content-addressed store, hash-verified

140 self.Conv2d_0 = conv
141
142 def forward(self, hidden_states: torch.Tensor, output_size: Optional[int] = None, *args, **kwargs) -> torch.Tensor:
143 if len(args) > 0 or kwargs.get("scale", None) is not None:
144 deprecation_message = "The `scale` argument is deprecated and will be ignored. Please remove it, as passing it will raise an error in the future. `scale` should directly be passed while calling the underlying pipeline component i.e., via `cross_attention_kwargs`."
145 deprecate("scale", "1.0.0", deprecation_message)
146
147 assert hidden_states.shape[1] == self.channels
148
149 if self.norm is not None:
150 hidden_states = self.norm(hidden_states.permute(0, 2, 3, 1)).permute(0, 3, 1, 2)
151
152 if self.use_conv_transpose:
153 return self.conv(hidden_states)
154
155 # Cast to float32 to as 'upsample_nearest2d_out_frame' op does not support bfloat16 until PyTorch 2.1
156 # https://github.com/pytorch/pytorch/issues/86679#issuecomment-1783978767
157 dtype = hidden_states.dtype
158 if dtype == torch.bfloat16 and is_torch_version("<", "2.1"):
159 hidden_states = hidden_states.to(torch.float32)
160
161 # upsample_nearest_nhwc fails with large batch sizes. see https://github.com/huggingface/diffusers/issues/984
162 if hidden_states.shape[0] >= 64:
163 hidden_states = hidden_states.contiguous()
164
165 # if `output_size` is passed we force the interpolation output
166 # size and do not make use of `scale_factor=2`
167 if self.interpolate:
168 if output_size is None:
169 hidden_states = F.interpolate(hidden_states, scale_factor=2.0, mode="nearest")
170 else:
171 hidden_states = F.interpolate(hidden_states, size=output_size, mode="nearest")
172
173 # Cast back to original dtype
174 if dtype == torch.bfloat16 and is_torch_version("<", "2.1"):
175 hidden_states = hidden_states.to(dtype)
176
177 # TODO(Suraj, Patrick) - clean up after weight dicts are correctly renamed
178 if self.use_conv:
179 if self.name == "conv":
180 hidden_states = self.conv(hidden_states)
181 else:
182 hidden_states = self.Conv2d_0(hidden_states)
183
184 return hidden_states
185
186
187class FirUpsample2D(nn.Module):

Callers

nothing calls this directly

Calls 4

deprecateFunction · 0.85
is_torch_versionFunction · 0.85
toMethod · 0.45
interpolateMethod · 0.45

Tested by

no test coverage detected