MCPcopy Create free account
hub / github.com/AlayaLab/Hive / forward

Method forward

models/flowsep/diffusers/models/controlnet.py:449–582  ·  view source on GitHub ↗
(
        self,
        sample: torch.FloatTensor,
        timestep: Union[torch.Tensor, float, int],
        encoder_hidden_states: torch.Tensor,
        controlnet_cond: torch.FloatTensor,
        conditioning_scale: float = 1.0,
        class_labels: Optional[torch.Tensor] = None,
        timestep_cond: Optional[torch.Tensor] = None,
        attention_mask: Optional[torch.Tensor] = None,
        cross_attention_kwargs: Optional[Dict[str, Any]] = None,
        guess_mode: bool = False,
        return_dict: bool = True,
    )

Source from the content-addressed store, hash-verified

447 module.gradient_checkpointing = value
448
449 def forward(
450 self,
451 sample: torch.FloatTensor,
452 timestep: Union[torch.Tensor, float, int],
453 encoder_hidden_states: torch.Tensor,
454 controlnet_cond: torch.FloatTensor,
455 conditioning_scale: float = 1.0,
456 class_labels: Optional[torch.Tensor] = None,
457 timestep_cond: Optional[torch.Tensor] = None,
458 attention_mask: Optional[torch.Tensor] = None,
459 cross_attention_kwargs: Optional[Dict[str, Any]] = None,
460 guess_mode: bool = False,
461 return_dict: bool = True,
462 ) -> Union[ControlNetOutput, Tuple]:
463 # check channel order
464 channel_order = self.config.controlnet_conditioning_channel_order
465
466 if channel_order == "rgb":
467 # in rgb order by default
468 ...
469 elif channel_order == "bgr":
470 controlnet_cond = torch.flip(controlnet_cond, dims=[1])
471 else:
472 raise ValueError(f"unknown `controlnet_conditioning_channel_order`: {channel_order}")
473
474 # prepare attention_mask
475 if attention_mask is not None:
476 attention_mask = (1 - attention_mask.to(sample.dtype)) * -10000.0
477 attention_mask = attention_mask.unsqueeze(1)
478
479 # 1. time
480 timesteps = timestep
481 if not torch.is_tensor(timesteps):
482 # TODO: this requires sync between CPU and GPU. So try to pass timesteps as tensors if you can
483 # This would be a good case for the `match` statement (Python 3.10+)
484 is_mps = sample.device.type == "mps"
485 if isinstance(timestep, float):
486 dtype = torch.float32 if is_mps else torch.float64
487 else:
488 dtype = torch.int32 if is_mps else torch.int64
489 timesteps = torch.tensor([timesteps], dtype=dtype, device=sample.device)
490 elif len(timesteps.shape) == 0:
491 timesteps = timesteps[None].to(sample.device)
492
493 # broadcast to batch dimension in a way that's compatible with ONNX/Core ML
494 timesteps = timesteps.expand(sample.shape[0])
495
496 t_emb = self.time_proj(timesteps)
497
498 # timesteps does not contain any weights and will always return f32 tensors
499 # but time_embedding might actually be running in fp16. so we need to cast here.
500 # there might be better ways to encapsulate this.
501 t_emb = t_emb.to(dtype=sample.dtype)
502
503 emb = self.time_embedding(t_emb, timestep_cond)
504
505 if self.class_embedding is not None:
506 if class_labels is None:

Callers

nothing calls this directly

Calls 2

ControlNetOutputClass · 0.85
toMethod · 0.45

Tested by

no test coverage detected