MCPcopy Create free account
hub / github.com/huggingface/diffusers / forward

Method forward

src/diffusers/models/resnet.py:319–377  ·  view source on GitHub ↗
(self, input_tensor: torch.Tensor, temb: torch.Tensor, *args, **kwargs)

Source from the content-addressed store, hash-verified

317 )
318
319 def forward(self, input_tensor: torch.Tensor, temb: torch.Tensor, *args, **kwargs) -> torch.Tensor:
320 if len(args) > 0 or kwargs.get("scale", None) is not None:
321 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`."
322 deprecate("scale", "1.0.0", deprecation_message)
323
324 hidden_states = input_tensor
325
326 hidden_states = self.norm1(hidden_states)
327 hidden_states = self.nonlinearity(hidden_states)
328
329 if self.upsample is not None:
330 # upsample_nearest_nhwc fails with large batch sizes. see https://github.com/huggingface/diffusers/issues/984
331 if hidden_states.shape[0] >= 64:
332 input_tensor = input_tensor.contiguous()
333 hidden_states = hidden_states.contiguous()
334 input_tensor = self.upsample(input_tensor)
335 hidden_states = self.upsample(hidden_states)
336 elif self.downsample is not None:
337 input_tensor = self.downsample(input_tensor)
338 hidden_states = self.downsample(hidden_states)
339
340 hidden_states = self.conv1(hidden_states)
341
342 if self.time_emb_proj is not None:
343 if not self.skip_time_act:
344 temb = self.nonlinearity(temb)
345 temb = self.time_emb_proj(temb)[:, :, None, None]
346
347 if self.time_embedding_norm == "default":
348 if temb is not None:
349 hidden_states = hidden_states + temb
350 hidden_states = self.norm2(hidden_states)
351 elif self.time_embedding_norm == "scale_shift":
352 if temb is None:
353 raise ValueError(
354 f" `temb` should not be None when `time_embedding_norm` is {self.time_embedding_norm}"
355 )
356 time_scale, time_shift = torch.chunk(temb, 2, dim=1)
357 hidden_states = self.norm2(hidden_states)
358 hidden_states = hidden_states * (1 + time_scale) + time_shift
359 else:
360 hidden_states = self.norm2(hidden_states)
361
362 hidden_states = self.nonlinearity(hidden_states)
363
364 hidden_states = self.dropout(hidden_states)
365 hidden_states = self.conv2(hidden_states)
366
367 if self.conv_shortcut is not None:
368 # Only use contiguous() during training to avoid DDP gradient stride mismatch warning.
369 # In inference mode (eval or no_grad), skip contiguous() for better performance, especially on CPU.
370 # Issue: https://github.com/huggingface/diffusers/issues/12975
371 if self.training:
372 input_tensor = input_tensor.contiguous()
373 input_tensor = self.conv_shortcut(input_tensor)
374
375 output_tensor = (input_tensor + hidden_states) / self.output_scale_factor
376

Callers

nothing calls this directly

Calls 3

deprecateFunction · 0.85
downsampleMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected