text stringlengths 1 1.02k | class_index int64 0 1.38k | source stringclasses 431
values |
|---|---|---|
# the output of sdp = (batch, num_heads, seq_len, head_dim)
# TODO: add support for attn.scale when we move to Torch 2.1
hidden_states = F.scaled_dot_product_attention(
query, key, value, attn_mask=attention_mask, dropout_p=0.0, is_causal=False
)
hidden_states = hidden_state... | 806 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
class CustomDiffusionXFormersAttnProcessor(nn.Module):
r"""
Processor for implementing memory efficient attention using xFormers for the Custom Diffusion method. | 807 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
Args:
train_kv (`bool`, defaults to `True`):
Whether to newly train the key and value matrices corresponding to the text features.
train_q_out (`bool`, defaults to `True`):
Whether to newly train query matrices corresponding to the latent image features.
hidden_size (`int`, *optional*, defau... | 807 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
as the attention operator. It is recommended to set to `None`, and allow xFormers to choose the best operator.
""" | 807 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
def __init__(
self,
train_kv: bool = True,
train_q_out: bool = False,
hidden_size: Optional[int] = None,
cross_attention_dim: Optional[int] = None,
out_bias: bool = True,
dropout: float = 0.0,
attention_op: Optional[Callable] = None,
):
super()... | 807 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
# `_custom_diffusion` id for easy serialization and loading.
if self.train_kv:
self.to_k_custom_diffusion = nn.Linear(cross_attention_dim or hidden_size, hidden_size, bias=False)
self.to_v_custom_diffusion = nn.Linear(cross_attention_dim or hidden_size, hidden_size, bias=False)
i... | 807 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
def __call__(
self,
attn: Attention,
hidden_states: torch.Tensor,
encoder_hidden_states: Optional[torch.Tensor] = None,
attention_mask: Optional[torch.Tensor] = None,
) -> torch.Tensor:
batch_size, sequence_length, _ = (
hidden_states.shape if encoder_hidd... | 807 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
if self.train_kv:
key = self.to_k_custom_diffusion(encoder_hidden_states.to(self.to_k_custom_diffusion.weight.dtype))
value = self.to_v_custom_diffusion(encoder_hidden_states.to(self.to_v_custom_diffusion.weight.dtype))
key = key.to(attn.to_q.weight.dtype)
value = value.t... | 807 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
hidden_states = xformers.ops.memory_efficient_attention(
query, key, value, attn_bias=attention_mask, op=self.attention_op, scale=attn.scale
)
hidden_states = hidden_states.to(query.dtype)
hidden_states = attn.batch_to_head_dim(hidden_states)
if self.train_q_out:
... | 807 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
class CustomDiffusionAttnProcessor2_0(nn.Module):
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 matric... | 808 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
def __init__(
self,
train_kv: bool = True,
train_q_out: bool = True,
hidden_size: Optional[int] = None,
cross_attention_dim: Optional[int] = None,
out_bias: bool = True,
dropout: float = 0.0,
):
super().__init__()
self.train_kv = train_kv
... | 808 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
# `_custom_diffusion` id for easy serialization and loading.
if self.train_kv:
self.to_k_custom_diffusion = nn.Linear(cross_attention_dim or hidden_size, hidden_size, bias=False)
self.to_v_custom_diffusion = nn.Linear(cross_attention_dim or hidden_size, hidden_size, bias=False)
i... | 808 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
def __call__(
self,
attn: Attention,
hidden_states: torch.Tensor,
encoder_hidden_states: Optional[torch.Tensor] = None,
attention_mask: Optional[torch.Tensor] = None,
) -> torch.Tensor:
batch_size, sequence_length, _ = hidden_states.shape
attention_mask = attn... | 808 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
if self.train_kv:
key = self.to_k_custom_diffusion(encoder_hidden_states.to(self.to_k_custom_diffusion.weight.dtype))
value = self.to_v_custom_diffusion(encoder_hidden_states.to(self.to_v_custom_diffusion.weight.dtype))
key = key.to(attn.to_q.weight.dtype)
value = value.t... | 808 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
head_dim = inner_dim // attn.heads
query = query.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
key = key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
value = value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
# the output of sdp = (batch, num_hea... | 808 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
if self.train_q_out:
# linear proj
hidden_states = self.to_out_custom_diffusion[0](hidden_states)
# dropout
hidden_states = self.to_out_custom_diffusion[1](hidden_states)
else:
# linear proj
hidden_states = attn.to_out[0](hidden_states)
... | 808 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
class SlicedAttnProcessor:
r"""
Processor for implementing sliced attention.
Args:
slice_size (`int`, *optional*):
The number of steps to compute attention. Uses as many slices as `attention_head_dim // slice_size`, and
`attention_head_dim` must be a multiple of the `slice_s... | 809 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
batch_size, sequence_length, _ = (
hidden_states.shape if encoder_hidden_states is None else encoder_hidden_states.shape
)
attention_mask = attn.prepare_attention_mask(attention_mask, sequence_length, batch_size)
if attn.group_norm is not None:
hidden_states = attn.group... | 809 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
batch_size_attention, query_tokens, _ = query.shape
hidden_states = torch.zeros(
(batch_size_attention, query_tokens, dim // attn.heads), device=query.device, dtype=query.dtype
)
for i in range((batch_size_attention - 1) // self.slice_size + 1):
start_idx = i * self.slic... | 809 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
# linear proj
hidden_states = attn.to_out[0](hidden_states)
# dropout
hidden_states = attn.to_out[1](hidden_states)
if input_ndim == 4:
hidden_states = hidden_states.transpose(-1, -2).reshape(batch_size, channel, height, width)
if attn.residual_connection:
... | 809 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
class SlicedAttnAddedKVProcessor:
r"""
Processor for implementing sliced attention with extra learnable key and value matrices for the text encoder.
Args:
slice_size (`int`, *optional*):
The number of steps to compute attention. Uses as many slices as `attention_head_dim // slice_size`,... | 810 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
batch_size, sequence_length, _ = hidden_states.shape
attention_mask = attn.prepare_attention_mask(attention_mask, sequence_length, batch_size)
if encoder_hidden_states is None:
encoder_hidden_states = hidden_states
elif attn.norm_cross:
encoder_hidden_states = attn.norm... | 810 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
if not attn.only_cross_attention:
key = attn.to_k(hidden_states)
value = attn.to_v(hidden_states)
key = attn.head_to_batch_dim(key)
value = attn.head_to_batch_dim(value)
key = torch.cat([encoder_hidden_states_key_proj, key], dim=1)
value = torch.ca... | 810 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
query_slice = query[start_idx:end_idx]
key_slice = key[start_idx:end_idx]
attn_mask_slice = attention_mask[start_idx:end_idx] if attention_mask is not None else None
attn_slice = attn.get_attention_scores(query_slice, key_slice, attn_mask_slice)
attn_slice = torch.bmm(a... | 810 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
class SpatialNorm(nn.Module):
"""
Spatially conditioned normalization as defined in https://arxiv.org/abs/2209.09002.
Args:
f_channels (`int`):
The number of channels for input to group normalization layer, and output of the spatial norm layer.
zq_channels (`int`):
T... | 811 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
def forward(self, f: torch.Tensor, zq: torch.Tensor) -> torch.Tensor:
f_size = f.shape[-2:]
zq = F.interpolate(zq, size=f_size, mode="nearest")
norm_f = self.norm_layer(f)
new_f = norm_f * self.conv_y(zq) + self.conv_b(zq)
return new_f | 811 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
class IPAdapterAttnProcessor(nn.Module):
r"""
Attention processor for Multiple IP-Adapters.
Args:
hidden_size (`int`):
The hidden size of the attention layer.
cross_attention_dim (`int`):
The number of channels in the `encoder_hidden_states`.
num_tokens (`int... | 812 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
if not isinstance(scale, list):
scale = [scale] * len(num_tokens)
if len(scale) != len(num_tokens):
raise ValueError("`scale` should be a list of integers with the same length as `num_tokens`.")
self.scale = scale
self.to_k_ip = nn.ModuleList(
[nn.Linear(cros... | 812 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
# separate ip_hidden_states from encoder_hidden_states
if encoder_hidden_states is not None:
if isinstance(encoder_hidden_states, tuple):
encoder_hidden_states, ip_hidden_states = encoder_hidden_states
else:
deprecation_message = (
"You... | 812 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
if attn.spatial_norm is not None:
hidden_states = attn.spatial_norm(hidden_states, temb)
input_ndim = hidden_states.ndim
if input_ndim == 4:
batch_size, channel, height, width = hidden_states.shape
hidden_states = hidden_states.view(batch_size, channel, height * wid... | 812 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
key = attn.to_k(encoder_hidden_states)
value = attn.to_v(encoder_hidden_states)
query = attn.head_to_batch_dim(query)
key = attn.head_to_batch_dim(key)
value = attn.head_to_batch_dim(value)
attention_probs = attn.get_attention_scores(query, key, attention_mask)
hidden_s... | 812 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
if ip_adapter_masks is not None:
if not isinstance(ip_adapter_masks, List):
# for backward compatibility, we accept `ip_adapter_mask` as a tensor of shape [num_ip_adapter, 1, height, width]
ip_adapter_masks = list(ip_adapter_masks.unsqueeze(1))
if not (len(ip_adap... | 812 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
"Each element of the ip_adapter_masks array should be a tensor with shape "
"[1, num_images_for_ip_adapter, height, width]."
" Please use `IPAdapterMaskProcessor` to preprocess your mask"
)
if mask.shape[1] != ip_state.s... | 812 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
# for ip-adapter
for current_ip_hidden_states, scale, to_k_ip, to_v_ip, mask in zip(
ip_hidden_states, self.scale, self.to_k_ip, self.to_v_ip, ip_adapter_masks
):
skip = False
if isinstance(scale, list):
if all(s == 0 for s in scale):
... | 812 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
ip_attention_probs = attn.get_attention_scores(query, ip_key, None)
_current_ip_hidden_states = torch.bmm(ip_attention_probs, ip_value)
_current_ip_hidden_states = attn.batch_to_head_dim(_current_ip_hidden_states)
mask_downsample = IPAdapterMaskPr... | 812 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
ip_key = attn.head_to_batch_dim(ip_key)
ip_value = attn.head_to_batch_dim(ip_value)
ip_attention_probs = attn.get_attention_scores(query, ip_key, None)
current_ip_hidden_states = torch.bmm(ip_attention_probs, ip_value)
current_ip_hidden_st... | 812 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
class IPAdapterAttnProcessor2_0(torch.nn.Module):
r"""
Attention processor for IP-Adapter for PyTorch 2.0.
Args:
hidden_size (`int`):
The hidden size of the attention layer.
cross_attention_dim (`int`):
The number of channels in the `encoder_hidden_states`.
n... | 813 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
if not isinstance(num_tokens, (tuple, list)):
num_tokens = [num_tokens]
self.num_tokens = num_tokens
if not isinstance(scale, list):
scale = [scale] * len(num_tokens)
if len(scale) != len(num_tokens):
raise ValueError("`scale` should be a list of integers wit... | 813 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
def __call__(
self,
attn: Attention,
hidden_states: torch.Tensor,
encoder_hidden_states: Optional[torch.Tensor] = None,
attention_mask: Optional[torch.Tensor] = None,
temb: Optional[torch.Tensor] = None,
scale: float = 1.0,
ip_adapter_masks: Optional[torch... | 813 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
# separate ip_hidden_states from encoder_hidden_states
if encoder_hidden_states is not None:
if isinstance(encoder_hidden_states, tuple):
encoder_hidden_states, ip_hidden_states = encoder_hidden_states
else:
deprecation_message = (
"You... | 813 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
if attn.spatial_norm is not None:
hidden_states = attn.spatial_norm(hidden_states, temb)
input_ndim = hidden_states.ndim
if input_ndim == 4:
batch_size, channel, height, width = hidden_states.shape
hidden_states = hidden_states.view(batch_size, channel, height * wid... | 813 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
query = attn.to_q(hidden_states)
if encoder_hidden_states is None:
encoder_hidden_states = hidden_states
elif attn.norm_cross:
encoder_hidden_states = attn.norm_encoder_hidden_states(encoder_hidden_states)
key = attn.to_k(encoder_hidden_states)
value = attn.to_v... | 813 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
hidden_states = hidden_states.transpose(1, 2).reshape(batch_size, -1, attn.heads * head_dim)
hidden_states = hidden_states.to(query.dtype) | 813 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
if ip_adapter_masks is not None:
if not isinstance(ip_adapter_masks, List):
# for backward compatibility, we accept `ip_adapter_mask` as a tensor of shape [num_ip_adapter, 1, height, width]
ip_adapter_masks = list(ip_adapter_masks.unsqueeze(1))
if not (len(ip_adap... | 813 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
"Each element of the ip_adapter_masks array should be a tensor with shape "
"[1, num_images_for_ip_adapter, height, width]."
" Please use `IPAdapterMaskProcessor` to preprocess your mask"
)
if mask.shape[1] != ip_state.s... | 813 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
# for ip-adapter
for current_ip_hidden_states, scale, to_k_ip, to_v_ip, mask in zip(
ip_hidden_states, self.scale, self.to_k_ip, self.to_v_ip, ip_adapter_masks
):
skip = False
if isinstance(scale, list):
if all(s == 0 for s in scale):
... | 813 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
ip_key = ip_key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
ip_value = ip_value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
# the output of sdp = (batch, num_heads, seq_len, head_dim)
# TODO: add support for attn.scal... | 813 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
mask_downsample = IPAdapterMaskProcessor.downsample(
mask[:, i, :, :],
batch_size,
_current_ip_hidden_states.shape[1],
_current_ip_hidden_states.shape[2],
)
ma... | 813 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
# the output of sdp = (batch, num_heads, seq_len, head_dim)
# TODO: add support for attn.scale when we move to Torch 2.1
current_ip_hidden_states = F.scaled_dot_product_attention(
query, ip_key, ip_value, attn_mask=None, dropout_p=0.0, is_causal=False
... | 813 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
if attn.residual_connection:
hidden_states = hidden_states + residual
hidden_states = hidden_states / attn.rescale_output_factor
return hidden_states | 813 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
class IPAdapterXFormersAttnProcessor(torch.nn.Module):
r"""
Attention processor for IP-Adapter using xFormers.
Args:
hidden_size (`int`):
The hidden size of the attention layer.
cross_attention_dim (`int`):
The number of channels in the `encoder_hidden_states`.
... | 814 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
def __init__(
self,
hidden_size,
cross_attention_dim=None,
num_tokens=(4,),
scale=1.0,
attention_op: Optional[Callable] = None,
):
super().__init__()
self.hidden_size = hidden_size
self.cross_attention_dim = cross_attention_dim
self.at... | 814 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
self.to_k_ip = nn.ModuleList(
[nn.Linear(cross_attention_dim or hidden_size, hidden_size, bias=False) for _ in range(len(num_tokens))]
)
self.to_v_ip = nn.ModuleList(
[nn.Linear(cross_attention_dim or hidden_size, hidden_size, bias=False) for _ in range(len(num_tokens))]
... | 814 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
# separate ip_hidden_states from encoder_hidden_states
if encoder_hidden_states is not None:
if isinstance(encoder_hidden_states, tuple):
encoder_hidden_states, ip_hidden_states = encoder_hidden_states
else:
deprecation_message = (
"You... | 814 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
if attn.spatial_norm is not None:
hidden_states = attn.spatial_norm(hidden_states, temb)
input_ndim = hidden_states.ndim
if input_ndim == 4:
batch_size, channel, height, width = hidden_states.shape
hidden_states = hidden_states.view(batch_size, channel, height * wid... | 814 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
if attention_mask is not None:
attention_mask = attn.prepare_attention_mask(attention_mask, sequence_length, batch_size)
# expand our mask's singleton query_tokens dimension:
# [batch*heads, 1, key_tokens] ->
# [batch*heads, query_tokens, key_tokens]
... | 814 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
if encoder_hidden_states is None:
encoder_hidden_states = hidden_states
elif attn.norm_cross:
encoder_hidden_states = attn.norm_encoder_hidden_states(encoder_hidden_states)
key = attn.to_k(encoder_hidden_states)
value = attn.to_v(encoder_hidden_states)
query = a... | 814 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
if ip_hidden_states:
if ip_adapter_masks is not None:
if not isinstance(ip_adapter_masks, List):
# for backward compatibility, we accept `ip_adapter_mask` as a tensor of shape [num_ip_adapter, 1, height, width]
ip_adapter_masks = list(ip_adapter_masks.... | 814 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
if not isinstance(mask, torch.Tensor) or mask.ndim != 4:
raise ValueError(
"Each element of the ip_adapter_masks array should be a tensor with shape "
"[1, num_images_for_ip_adapter, height, width]."
... | 814 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
f"number of scales ({len(scale)}) at index {index}"
)
else:
ip_adapter_masks = [None] * len(self.scale) | 814 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
# for ip-adapter
for current_ip_hidden_states, scale, to_k_ip, to_v_ip, mask in zip(
ip_hidden_states, self.scale, self.to_k_ip, self.to_v_ip, ip_adapter_masks
):
skip = False
if isinstance(scale, list):
if all(s == 0 for s in s... | 814 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
ip_key = attn.head_to_batch_dim(ip_key).contiguous()
ip_value = attn.head_to_batch_dim(ip_value).contiguous()
_current_ip_hidden_states = xformers.ops.memory_efficient_attention(
query, ip_key, ip_value, op=self.attention_op
... | 814 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
mask_downsample = mask_downsample.to(dtype=query.dtype, device=query.device)
hidden_states = hidden_states + scale[i] * (_current_ip_hidden_states * mask_downsample)
else:
ip_key = to_k_ip(current_ip_hidden_states)
ip_value ... | 814 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
# linear proj
hidden_states = attn.to_out[0](hidden_states)
# dropout
hidden_states = attn.to_out[1](hidden_states)
if input_ndim == 4:
hidden_states = hidden_states.transpose(-1, -2).reshape(batch_size, channel, height, width)
if attn.residual_connection:
... | 814 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
class SD3IPAdapterJointAttnProcessor2_0(torch.nn.Module):
"""
Attention processor for IP-Adapter used typically in processing the SD3-like self-attention projections, with
additional image-based information and timestep embeddings.
Args:
hidden_size (`int`):
The number of hidden cha... | 815 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
self.norm_ip = AdaLayerNorm(timesteps_emb_dim, output_dim=ip_hidden_states_dim * 2, norm_eps=1e-6, chunk_dim=1)
self.to_k_ip = nn.Linear(ip_hidden_states_dim, hidden_size, bias=False)
self.to_v_ip = nn.Linear(ip_hidden_states_dim, hidden_size, bias=False)
self.norm_q = RMSNorm(head_dim, 1e-6)
... | 815 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
Args:
attn (`Attention`):
Attention instance.
hidden_states (`torch.FloatTensor`):
Input `hidden_states`.
encoder_hidden_states (`torch.FloatTensor`, *optional*):
The encoder hidden states.
attention_mask (`torch.FloatTensor... | 815 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
query = query.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
key = key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
value = value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
img_query = query
img_key = key
img_value = value
if att... | 815 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
encoder_hidden_states_query_proj = encoder_hidden_states_query_proj.view(
batch_size, -1, attn.heads, head_dim
).transpose(1, 2)
encoder_hidden_states_key_proj = encoder_hidden_states_key_proj.view(
batch_size, -1, attn.heads, head_dim
).transpose(1, 2... | 815 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
query = torch.cat([query, encoder_hidden_states_query_proj], dim=2)
key = torch.cat([key, encoder_hidden_states_key_proj], dim=2)
value = torch.cat([value, encoder_hidden_states_value_proj], dim=2)
hidden_states = F.scaled_dot_product_attention(query, key, value, dropout_p=0.0, is_causa... | 815 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
# IP Adapter
if self.scale != 0 and ip_hidden_states is not None:
# Norm image features
norm_ip_hidden_states = self.norm_ip(ip_hidden_states, temb=temb)
# To k and v
ip_key = self.to_k_ip(norm_ip_hidden_states)
ip_value = self.to_v_ip(norm_ip_hidden_... | 815 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
ip_hidden_states = F.scaled_dot_product_attention(query, key, value, dropout_p=0.0, is_causal=False)
ip_hidden_states = ip_hidden_states.transpose(1, 2).view(batch_size, -1, attn.heads * head_dim)
ip_hidden_states = ip_hidden_states.to(query.dtype)
hidden_states = hidden_states + ip... | 815 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
class PAGIdentitySelfAttnProcessor2_0:
r"""
Processor for implementing PAG using scaled dot-product attention (enabled by default if you're using PyTorch 2.0).
PAG reference: https://arxiv.org/abs/2403.17377
"""
def __init__(self):
if not hasattr(F, "scaled_dot_product_attention"):
... | 816 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
input_ndim = hidden_states.ndim
if input_ndim == 4:
batch_size, channel, height, width = hidden_states.shape
hidden_states = hidden_states.view(batch_size, channel, height * width).transpose(1, 2)
# chunk
hidden_states_org, hidden_states_ptb = hidden_states.chunk(2)
... | 816 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
query = attn.to_q(hidden_states_org)
key = attn.to_k(hidden_states_org)
value = attn.to_v(hidden_states_org)
inner_dim = key.shape[-1]
head_dim = inner_dim // attn.heads
query = query.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
key = key.view(batch_size, ... | 816 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
# linear proj
hidden_states_org = attn.to_out[0](hidden_states_org)
# dropout
hidden_states_org = attn.to_out[1](hidden_states_org)
if input_ndim == 4:
hidden_states_org = hidden_states_org.transpose(-1, -2).reshape(batch_size, channel, height, width)
# perturbed pa... | 816 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
# cat
hidden_states = torch.cat([hidden_states_org, hidden_states_ptb])
if attn.residual_connection:
hidden_states = hidden_states + residual
hidden_states = hidden_states / attn.rescale_output_factor
return hidden_states | 816 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
class PAGCFGIdentitySelfAttnProcessor2_0:
r"""
Processor for implementing PAG using scaled dot-product attention (enabled by default if you're using PyTorch 2.0).
PAG reference: https://arxiv.org/abs/2403.17377
"""
def __init__(self):
if not hasattr(F, "scaled_dot_product_attention"):
... | 817 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
input_ndim = hidden_states.ndim
if input_ndim == 4:
batch_size, channel, height, width = hidden_states.shape
hidden_states = hidden_states.view(batch_size, channel, height * width).transpose(1, 2)
# chunk
hidden_states_uncond, hidden_states_org, hidden_states_ptb = hidde... | 817 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
if attn.group_norm is not None:
hidden_states_org = attn.group_norm(hidden_states_org.transpose(1, 2)).transpose(1, 2)
query = attn.to_q(hidden_states_org)
key = attn.to_k(hidden_states_org)
value = attn.to_v(hidden_states_org)
inner_dim = key.shape[-1]
head_dim = i... | 817 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
hidden_states_org = hidden_states_org.transpose(1, 2).reshape(batch_size, -1, attn.heads * head_dim)
hidden_states_org = hidden_states_org.to(query.dtype)
# linear proj
hidden_states_org = attn.to_out[0](hidden_states_org)
# dropout
hidden_states_org = attn.to_out[1](hidden_stat... | 817 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
# linear proj
hidden_states_ptb = attn.to_out[0](hidden_states_ptb)
# dropout
hidden_states_ptb = attn.to_out[1](hidden_states_ptb)
if input_ndim == 4:
hidden_states_ptb = hidden_states_ptb.transpose(-1, -2).reshape(batch_size, channel, height, width)
# cat
... | 817 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
class SanaMultiscaleAttnProcessor2_0:
r"""
Processor for implementing multiscale quadratic attention.
"""
def __call__(self, attn: SanaMultiscaleLinearAttention, hidden_states: torch.Tensor) -> torch.Tensor:
height, width = hidden_states.shape[-2:]
if height * width > attn.attention_hea... | 818 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
hidden_states = torch.cat(multi_scale_qkv, dim=1)
if use_linear_attention:
# for linear attention upcast hidden_states to float32
hidden_states = hidden_states.to(dtype=torch.float32)
hidden_states = hidden_states.reshape(batch_size, -1, 3 * attn.attention_head_dim, height * wi... | 818 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
if attn.norm_type == "rms_norm":
hidden_states = attn.norm_out(hidden_states.movedim(1, -1)).movedim(-1, 1)
else:
hidden_states = attn.norm_out(hidden_states)
if attn.residual_connection:
hidden_states = hidden_states + residual
return hidden_states | 818 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
class LoRAAttnProcessor:
r"""
Processor for implementing attention with LoRA.
"""
def __init__(self):
pass | 819 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
class LoRAAttnProcessor2_0:
r"""
Processor for implementing attention with LoRA (enabled by default if you're using PyTorch 2.0).
"""
def __init__(self):
pass | 820 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
class LoRAXFormersAttnProcessor:
r"""
Processor for implementing attention with LoRA using xFormers.
"""
def __init__(self):
pass | 821 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
class LoRAAttnAddedKVProcessor:
r"""
Processor for implementing attention with LoRA with extra learnable key and value matrices for the text encoder.
"""
def __init__(self):
pass | 822 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
class FluxSingleAttnProcessor2_0(FluxAttnProcessor2_0):
r"""
Processor for implementing scaled dot-product attention (enabled by default if you're using PyTorch 2.0).
"""
def __init__(self):
deprecation_message = "`FluxSingleAttnProcessor2_0` is deprecated and will be removed in a future versio... | 823 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
class SanaLinearAttnProcessor2_0:
r"""
Processor for implementing scaled dot-product linear attention.
"""
def __call__(
self,
attn: Attention,
hidden_states: torch.Tensor,
encoder_hidden_states: Optional[torch.Tensor] = None,
attention_mask: Optional[torch.Tenso... | 824 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
value = F.pad(value, (0, 0, 0, 1), mode="constant", value=1.0)
scores = torch.matmul(value, key)
hidden_states = torch.matmul(scores, query)
hidden_states = hidden_states[:, :, :-1] / (hidden_states[:, :, -1:] + 1e-15)
hidden_states = hidden_states.flatten(1, 2).transpose(1, 2)
... | 824 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
class PAGCFGSanaLinearAttnProcessor2_0:
r"""
Processor for implementing scaled dot-product linear attention.
"""
def __call__(
self,
attn: Attention,
hidden_states: torch.Tensor,
encoder_hidden_states: Optional[torch.Tensor] = None,
attention_mask: Optional[torch... | 825 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
query, key, value = query.float(), key.float(), value.float()
value = F.pad(value, (0, 0, 0, 1), mode="constant", value=1.0)
scores = torch.matmul(value, key)
hidden_states_org = torch.matmul(scores, query)
hidden_states_org = hidden_states_org[:, :, :-1] / (hidden_states_org[:, :, -1:... | 825 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
if original_dtype == torch.float16:
hidden_states = hidden_states.clip(-65504, 65504)
return hidden_states | 825 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
class PAGIdentitySanaLinearAttnProcessor2_0:
r"""
Processor for implementing scaled dot-product linear attention.
"""
def __call__(
self,
attn: Attention,
hidden_states: torch.Tensor,
encoder_hidden_states: Optional[torch.Tensor] = None,
attention_mask: Optional[... | 826 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
value = F.pad(value, (0, 0, 0, 1), mode="constant", value=1.0)
scores = torch.matmul(value, key)
hidden_states_org = torch.matmul(scores, query)
if hidden_states_org.dtype in [torch.float16, torch.bfloat16]:
hidden_states_org = hidden_states_org.float()
hidden_states_org = ... | 826 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
if original_dtype == torch.float16:
hidden_states = hidden_states.clip(-65504, 65504)
return hidden_states | 826 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py |
class ControlNetOutput(ControlNetOutput):
def __init__(self, *args, **kwargs):
deprecation_message = "Importing `ControlNetOutput` from `diffusers.models.controlnet` is deprecated and this will be removed in a future version. Please use `from diffusers.models.controlnets.controlnet import ControlNetOutput`,... | 827 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/controlnet.py |
class ControlNetModel(ControlNetModel):
def __init__(
self,
in_channels: int = 4,
conditioning_channels: int = 3,
flip_sin_to_cos: bool = True,
freq_shift: int = 0,
down_block_types: Tuple[str, ...] = (
"CrossAttnDownBlock2D",
"CrossAttnDownBlo... | 828 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/controlnet.py |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.