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

Method forward

src/diffusers/models/autoencoders/vae.py:537–644  ·  view source on GitHub ↗

r"""The forward method of the `MaskConditionDecoder` class.

(
        self,
        z: torch.FloatTensor,
        image: Optional[torch.FloatTensor] = None,
        mask: Optional[torch.FloatTensor] = None,
        latent_embeds: Optional[torch.FloatTensor] = None,
    )

Source from the content-addressed store, hash-verified

535 self.gradient_checkpointing = False
536
537 def forward(
538 self,
539 z: torch.FloatTensor,
540 image: Optional[torch.FloatTensor] = None,
541 mask: Optional[torch.FloatTensor] = None,
542 latent_embeds: Optional[torch.FloatTensor] = None,
543 ) -> torch.FloatTensor:
544 r"""The forward method of the `MaskConditionDecoder` class."""
545 sample = z
546 sample = self.conv_in(sample)
547
548 upscale_dtype = next(iter(self.up_blocks.parameters())).dtype
549 if self.training and self.gradient_checkpointing:
550
551 def create_custom_forward(module):
552 def custom_forward(*inputs):
553 return module(*inputs)
554
555 return custom_forward
556
557 if is_torch_version(">=", "1.11.0"):
558 # middle
559 sample = torch.utils.checkpoint.checkpoint(
560 create_custom_forward(self.mid_block),
561 sample,
562 latent_embeds,
563 use_reentrant=False,
564 )
565 sample = sample.to(upscale_dtype)
566
567 # condition encoder
568 if image is not None and mask is not None:
569 masked_image = (1 - mask) * image
570 im_x = torch.utils.checkpoint.checkpoint(
571 create_custom_forward(self.condition_encoder),
572 masked_image,
573 mask,
574 use_reentrant=False,
575 )
576
577 # up
578 for up_block in self.up_blocks:
579 if image is not None and mask is not None:
580 sample_ = im_x[str(tuple(sample.shape))]
581 mask_ = nn.functional.interpolate(mask, size=sample.shape[-2:], mode="nearest")
582 sample = sample * mask_ + sample_ * (1 - mask_)
583 sample = torch.utils.checkpoint.checkpoint(
584 create_custom_forward(up_block),
585 sample,
586 latent_embeds,
587 use_reentrant=False,
588 )
589 if image is not None and mask is not None:
590 sample = sample * mask + im_x[str(tuple(sample.shape))] * (1 - mask)
591 else:
592 # middle
593 sample = torch.utils.checkpoint.checkpoint(
594 create_custom_forward(self.mid_block), sample, latent_embeds

Callers

nothing calls this directly

Calls 3

is_torch_versionFunction · 0.85
toMethod · 0.45
interpolateMethod · 0.45

Tested by

no test coverage detected