r""" Processor for implementing attention for the Custom Diffusion method using PyTorch 2.0’s memory-efficient scaled dot-product attention. Args: train_kv (`bool`, defaults to `True`): Whether to newly train the key and value matrices corresponding to the text featu
| 1380 | |
| 1381 | |
| 1382 | class CustomDiffusionAttnProcessor2_0(nn.Module): |
| 1383 | r""" |
| 1384 | Processor for implementing attention for the Custom Diffusion method using PyTorch 2.0’s memory-efficient scaled |
| 1385 | dot-product attention. |
| 1386 | |
| 1387 | Args: |
| 1388 | train_kv (`bool`, defaults to `True`): |
| 1389 | Whether to newly train the key and value matrices corresponding to the text features. |
| 1390 | train_q_out (`bool`, defaults to `True`): |
| 1391 | Whether to newly train query matrices corresponding to the latent image features. |
| 1392 | hidden_size (`int`, *optional*, defaults to `None`): |
| 1393 | The hidden size of the attention layer. |
| 1394 | cross_attention_dim (`int`, *optional*, defaults to `None`): |
| 1395 | The number of channels in the `encoder_hidden_states`. |
| 1396 | out_bias (`bool`, defaults to `True`): |
| 1397 | Whether to include the bias parameter in `train_q_out`. |
| 1398 | dropout (`float`, *optional*, defaults to 0.0): |
| 1399 | The dropout probability to use. |
| 1400 | """ |
| 1401 | |
| 1402 | def __init__( |
| 1403 | self, |
| 1404 | train_kv: bool = True, |
| 1405 | train_q_out: bool = True, |
| 1406 | hidden_size: Optional[int] = None, |
| 1407 | cross_attention_dim: Optional[int] = None, |
| 1408 | out_bias: bool = True, |
| 1409 | dropout: float = 0.0, |
| 1410 | ): |
| 1411 | super().__init__() |
| 1412 | self.train_kv = train_kv |
| 1413 | self.train_q_out = train_q_out |
| 1414 | |
| 1415 | self.hidden_size = hidden_size |
| 1416 | self.cross_attention_dim = cross_attention_dim |
| 1417 | |
| 1418 | # `_custom_diffusion` id for easy serialization and loading. |
| 1419 | if self.train_kv: |
| 1420 | self.to_k_custom_diffusion = nn.Linear(cross_attention_dim or hidden_size, hidden_size, bias=False) |
| 1421 | self.to_v_custom_diffusion = nn.Linear(cross_attention_dim or hidden_size, hidden_size, bias=False) |
| 1422 | if self.train_q_out: |
| 1423 | self.to_q_custom_diffusion = nn.Linear(hidden_size, hidden_size, bias=False) |
| 1424 | self.to_out_custom_diffusion = nn.ModuleList([]) |
| 1425 | self.to_out_custom_diffusion.append(nn.Linear(hidden_size, hidden_size, bias=out_bias)) |
| 1426 | self.to_out_custom_diffusion.append(nn.Dropout(dropout)) |
| 1427 | |
| 1428 | def __call__( |
| 1429 | self, |
| 1430 | attn: Attention, |
| 1431 | hidden_states: torch.FloatTensor, |
| 1432 | encoder_hidden_states: Optional[torch.FloatTensor] = None, |
| 1433 | attention_mask: Optional[torch.FloatTensor] = None, |
| 1434 | ) -> torch.FloatTensor: |
| 1435 | batch_size, sequence_length, _ = hidden_states.shape |
| 1436 | attention_mask = attn.prepare_attention_mask(attention_mask, sequence_length, batch_size) |
| 1437 | if self.train_q_out: |
| 1438 | query = self.to_q_custom_diffusion(hidden_states) |
| 1439 | else: |
nothing calls this directly
no outgoing calls
no test coverage detected