r""" The forward method of the `Attention` class. Args: hidden_states (`torch.Tensor`): The hidden states of the query. encoder_hidden_states (`torch.Tensor`, *optional*): The hidden states of the encoder. attention
(
self,
hidden_states: torch.Tensor,
encoder_hidden_states: Optional[torch.Tensor] = None,
attention_mask: Optional[torch.Tensor] = None,
**cross_attention_kwargs,
)
| 450 | return self.processor |
| 451 | |
| 452 | def forward( |
| 453 | self, |
| 454 | hidden_states: torch.Tensor, |
| 455 | encoder_hidden_states: Optional[torch.Tensor] = None, |
| 456 | attention_mask: Optional[torch.Tensor] = None, |
| 457 | **cross_attention_kwargs, |
| 458 | ) -> torch.Tensor: |
| 459 | r""" |
| 460 | The forward method of the `Attention` class. |
| 461 | |
| 462 | Args: |
| 463 | hidden_states (`torch.Tensor`): |
| 464 | The hidden states of the query. |
| 465 | encoder_hidden_states (`torch.Tensor`, *optional*): |
| 466 | The hidden states of the encoder. |
| 467 | attention_mask (`torch.Tensor`, *optional*): |
| 468 | The attention mask to use. If `None`, no mask is applied. |
| 469 | **cross_attention_kwargs: |
| 470 | Additional keyword arguments to pass along to the cross attention. |
| 471 | |
| 472 | Returns: |
| 473 | `torch.Tensor`: The output of the attention layer. |
| 474 | """ |
| 475 | # The `Attention` class can call different attention processors / attention functions |
| 476 | # here we simply pass along all tensors to the selected processor class |
| 477 | # For standard processors that are defined here, `**cross_attention_kwargs` is empty |
| 478 | |
| 479 | attn_parameters = set(inspect.signature(self.processor.__call__).parameters.keys()) |
| 480 | quiet_attn_parameters = {"ip_adapter_masks"} |
| 481 | unused_kwargs = [ |
| 482 | k for k, _ in cross_attention_kwargs.items() if k not in attn_parameters and k not in quiet_attn_parameters |
| 483 | ] |
| 484 | if len(unused_kwargs) > 0: |
| 485 | logger.warning( |
| 486 | f"cross_attention_kwargs {unused_kwargs} are not expected by {self.processor.__class__.__name__} and will be ignored." |
| 487 | ) |
| 488 | cross_attention_kwargs = {k: w for k, w in cross_attention_kwargs.items() if k in attn_parameters} |
| 489 | |
| 490 | return self.processor( |
| 491 | self, |
| 492 | hidden_states, |
| 493 | encoder_hidden_states=encoder_hidden_states, |
| 494 | attention_mask=attention_mask, |
| 495 | **cross_attention_kwargs, |
| 496 | ) |
| 497 | |
| 498 | def batch_to_head_dim(self, tensor: torch.Tensor) -> torch.Tensor: |
| 499 | r""" |
nothing calls this directly
no outgoing calls
no test coverage detected