text
stringlengths
1
1.02k
class_index
int64
0
1.38k
source
stringclasses
431 values
if query.dtype in (torch.float16, torch.bfloat16): hidden_states = torch_npu.npu_fusion_attention( query, key, value, attn.heads, input_layout="BNSD", pse=None, scale=1.0 / math.sqrt(query.shape[-...
787
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
if encoder_hidden_states is not None: encoder_hidden_states, hidden_states = ( hidden_states[:, : encoder_hidden_states.shape[1]], hidden_states[:, encoder_hidden_states.shape[1] :], ) # linear proj hidden_states = attn.to_out[0](hidden_st...
787
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
class FusedFluxAttnProcessor2_0: """Attention processor used typically in processing the SD3-like self-attention projections.""" def __init__(self): if not hasattr(F, "scaled_dot_product_attention"): raise ImportError( "FusedFluxAttnProcessor2_0 requires PyTorch 2.0, to use ...
788
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
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, -1, attn.heads, head_dim).transpose(1, 2) value = value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2) if...
788
/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...
788
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
# attention query = torch.cat([encoder_hidden_states_query_proj, query], dim=2) key = torch.cat([encoder_hidden_states_key_proj, key], dim=2) value = torch.cat([encoder_hidden_states_value_proj, value], dim=2) if image_rotary_emb is not None: from .embeddings imp...
788
/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) encoder_hidden_states = attn.to_add_out(encoder_hidden_states) return hidden_states, encoder_hidden_states else: return hid...
788
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
class FusedFluxAttnProcessor2_0_NPU: """Attention processor used typically in processing the SD3-like self-attention projections.""" def __init__(self): if not hasattr(F, "scaled_dot_product_attention"): raise ImportError( "FluxAttnProcessor2_0_NPU requires PyTorch 2.0 and t...
789
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
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, -1, attn.heads, head_dim).transpose(1, 2) value = value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2) if...
789
/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...
789
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
# attention query = torch.cat([encoder_hidden_states_query_proj, query], dim=2) key = torch.cat([encoder_hidden_states_key_proj, key], dim=2) value = torch.cat([encoder_hidden_states_value_proj, value], dim=2) if image_rotary_emb is not None: from .embeddings imp...
789
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
if query.dtype in (torch.float16, torch.bfloat16): hidden_states = torch_npu.npu_fusion_attention( query, key, value, attn.heads, input_layout="BNSD", pse=None, scale=1.0 / math.sqrt(query.shape[-...
789
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
if encoder_hidden_states is not None: encoder_hidden_states, hidden_states = ( hidden_states[:, : encoder_hidden_states.shape[1]], hidden_states[:, encoder_hidden_states.shape[1] :], ) # linear proj hidden_states = attn.to_out[0](hidden_st...
789
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
class FluxIPAdapterJointAttnProcessor2_0(torch.nn.Module): """Flux Attention processor for IP-Adapter.""" def __init__( self, hidden_size: int, cross_attention_dim: int, num_tokens=(4,), scale=1.0, device=None, dtype=None ): super().__init__() if not hasattr(F, "scaled_dot_product_...
790
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
self.to_k_ip = nn.ModuleList( [ nn.Linear(cross_attention_dim, hidden_size, bias=True, device=device, dtype=dtype) for _ in range(len(num_tokens)) ] ) self.to_v_ip = nn.ModuleList( [ nn.Linear(cross_attention_dim, hidden...
790
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
# `sample` projections. hidden_states_query_proj = attn.to_q(hidden_states) key = attn.to_k(hidden_states) value = attn.to_v(hidden_states) inner_dim = key.shape[-1] head_dim = inner_dim // attn.heads hidden_states_query_proj = hidden_states_query_proj.view(batch_size, ...
790
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
# the attention in FluxSingleTransformerBlock does not use `encoder_hidden_states` if encoder_hidden_states is not None: # `context` projections. encoder_hidden_states_query_proj = attn.add_q_proj(encoder_hidden_states) encoder_hidden_states_key_proj = attn.add_k_proj(encoder...
790
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
if attn.norm_added_q is not None: encoder_hidden_states_query_proj = attn.norm_added_q(encoder_hidden_states_query_proj) if attn.norm_added_k is not None: encoder_hidden_states_key_proj = attn.norm_added_k(encoder_hidden_states_key_proj) # attention q...
790
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
hidden_states = F.scaled_dot_product_attention(query, key, value, dropout_p=0.0, is_causal=False) hidden_states = hidden_states.transpose(1, 2).reshape(batch_size, -1, attn.heads * head_dim) hidden_states = hidden_states.to(query.dtype) if encoder_hidden_states is not None: encoder_...
790
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
# IP-adapter ip_query = hidden_states_query_proj ip_attn_output = None # for ip-adapter # TODO: support for multiple adapters for current_ip_hidden_states, scale, to_k_ip, to_v_ip in zip( ip_hidden_states, self.scale, self.to_k_ip, self.to_v_ip...
790
/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.scale when we move to Torch 2...
790
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
class CogVideoXAttnProcessor2_0: r""" Processor for implementing scaled dot-product attention for the CogVideoX model. It applies a rotary embedding on query and key vectors, but does not include spatial normalization. """ def __init__(self): if not hasattr(F, "scaled_dot_product_attention"...
791
/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) attention_mask = attention_mask.view(batch_size, attn.heads, -1, attention_mask.shape[-1]) query = attn.to_q(hidden_states) key = attn.to_k(hidden_states) ...
791
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
query[:, :, text_seq_length:] = apply_rotary_emb(query[:, :, text_seq_length:], image_rotary_emb) if not attn.is_cross_attention: key[:, :, text_seq_length:] = apply_rotary_emb(key[:, :, text_seq_length:], image_rotary_emb) hidden_states = F.scaled_dot_product_attention( ...
791
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
class FusedCogVideoXAttnProcessor2_0: r""" Processor for implementing scaled dot-product attention for the CogVideoX model. It applies a rotary embedding on query and key vectors, but does not include spatial normalization. """ def __init__(self): if not hasattr(F, "scaled_dot_product_atten...
792
/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) attention_mask = attention_mask.view(batch_size, attn.heads, -1, attention_mask.shape[-1]) qkv = attn.to_qkv(hidden_states) split_size = qkv.shape[-1] // 3 ...
792
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
query[:, :, text_seq_length:] = apply_rotary_emb(query[:, :, text_seq_length:], image_rotary_emb) if not attn.is_cross_attention: key[:, :, text_seq_length:] = apply_rotary_emb(key[:, :, text_seq_length:], image_rotary_emb) hidden_states = F.scaled_dot_product_attention( ...
792
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
class XFormersAttnAddedKVProcessor: r""" Processor for implementing memory efficient attention using xFormers. Args: attention_op (`Callable`, *optional*, defaults to `None`): The base [operator](https://facebookresearch.github.io/xformers/components/ops.html#xformers.ops.At...
793
/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: residual = hidden_states hidden_states = hidden_states.view(hidden_states....
793
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
encoder_hidden_states_key_proj = attn.add_k_proj(encoder_hidden_states) encoder_hidden_states_value_proj = attn.add_v_proj(encoder_hidden_states) encoder_hidden_states_key_proj = attn.head_to_batch_dim(encoder_hidden_states_key_proj) encoder_hidden_states_value_proj = attn.head_to_batch_dim(enco...
793
/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) # linear proj hidden_sta...
793
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
class XFormersAttnProcessor: r""" Processor for implementing memory efficient attention using xFormers. Args: attention_op (`Callable`, *optional*, defaults to `None`): The base [operator](https://facebookresearch.github.io/xformers/components/ops.html#xformers.ops.Attention...
794
/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, *args, **kwargs, ) -> torch.Tensor: if l...
794
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
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) batch_size, key_tokens, _ = ( hidden_states.shape if encoder_hidden_states is None else encoder_hidden_states....
794
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
attention_mask = attn.prepare_attention_mask(attention_mask, key_tokens, batch_size) if attention_mask is not None: # expand our mask's singleton query_tokens dimension: # [batch*heads, 1, key_tokens] -> # [batch*heads, query_tokens, key_tokens] # s...
794
/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...
794
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
if input_ndim == 4: hidden_states = hidden_states.transpose(-1, -2).reshape(batch_size, channel, height, width) if attn.residual_connection: hidden_states = hidden_states + residual hidden_states = hidden_states / attn.rescale_output_factor return hidden_states
794
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
class AttnProcessorNPU: r""" Processor for implementing flash attention using torch_npu. Torch_npu supports only fp16 and bf16 data types. If fp32 is used, F.scaled_dot_product_attention will be used for computation, but the acceleration effect on NPU is not significant. """ def __init__(self)...
795
/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, *args, **kwargs, ) -> torch.Tensor: if l...
795
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
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) batch_size, sequence_length, _ = ( hidden_states.shape if encoder_hidden_states is None else encoder_hidden_st...
795
/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) inner_dim...
795
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
# the output of sdp = (batch, num_heads, seq_len, head_dim) if query.dtype in (torch.float16, torch.bfloat16): hidden_states = torch_npu.npu_fusion_attention( query, key, value, attn.heads, input_layout="BNSD", ...
795
/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) # linear proj hidden_states = attn.to_out[0](hidden_states) # dropout hidden_states = attn.to_out[1](hidden_states) if input_ndim =...
795
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
class AttnProcessor2_0: r""" Processor for implementing scaled dot-product attention (enabled by default if you're using PyTorch 2.0). """ def __init__(self): if not hasattr(F, "scaled_dot_product_attention"): raise ImportError("AttnProcessor2_0 requires PyTorch 2.0, to use it, plea...
796
/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, *args, **kwargs, ) -> torch.Tensor: if l...
796
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
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) batch_size, sequence_length, _ = ( hidden_states.shape if encoder_hidden_states is None else encoder_hidden_st...
796
/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) inner_dim...
796
/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 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...
796
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
class XLAFlashAttnProcessor2_0: r""" Processor for implementing scaled dot-product attention with pallas flash attention kernel if using `torch_xla`. """ def __init__(self, partition_spec: Optional[Tuple[Optional[str], ...]] = None): if not hasattr(F, "scaled_dot_product_attention"): ...
797
/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, *args, **kwargs, ) -> torch.Tensor: resi...
797
/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) # scaled_dot_product_attention expects attention_mask shape to be # (batch, heads, source_length, target_length) attention_mask = attention_mask.view(b...
797
/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) if attn.norm_q is not None: query = attn.norm_q(query) if a...
797
/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 if all(tensor.shape[2] >= 4096 for tensor in [query, key, value]): if attention_mask is not None: attention_mask = attention_mask.view(batch_size, 1, 1, at...
797
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
# Apply attention mask to key key = key + attention_mask query /= math.sqrt(query.shape[3]) partition_spec = self.partition_spec if is_spmd() else None hidden_states = flash_attention(query, key, value, causal=False, partition_spec=partition_spec) else: ...
797
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
if input_ndim == 4: hidden_states = hidden_states.transpose(-1, -2).reshape(batch_size, channel, height, width) if attn.residual_connection: hidden_states = hidden_states + residual hidden_states = hidden_states / attn.rescale_output_factor return hidden_states
797
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
class XLAFluxFlashAttnProcessor2_0: r""" Processor for implementing scaled dot-product attention with pallas flash attention kernel if using `torch_xla`. """ def __init__(self, partition_spec: Optional[Tuple[Optional[str], ...]] = None): if not hasattr(F, "scaled_dot_product_attention"): ...
798
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
def __call__( self, attn: Attention, hidden_states: torch.FloatTensor, encoder_hidden_states: torch.FloatTensor = None, attention_mask: Optional[torch.FloatTensor] = None, image_rotary_emb: Optional[torch.Tensor] = None, ) -> torch.FloatTensor: batch_size, _, ...
798
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
if attn.norm_q is not None: query = attn.norm_q(query) if attn.norm_k is not None: key = attn.norm_k(key) # the attention in FluxSingleTransformerBlock does not use `encoder_hidden_states` if encoder_hidden_states is not None: # `context` projections. ...
798
/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...
798
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
# attention query = torch.cat([encoder_hidden_states_query_proj, query], dim=2) key = torch.cat([encoder_hidden_states_key_proj, key], dim=2) value = torch.cat([encoder_hidden_states_value_proj, value], dim=2) if image_rotary_emb is not None: from .embeddings imp...
798
/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) encoder_hidden_states = attn.to_add_out(encoder_hidden_states) return hidden_states, encoder_hidden_states else: return hi...
798
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
class MochiVaeAttnProcessor2_0: r""" Attention processor used in Mochi VAE. """ def __init__(self): if not hasattr(F, "scaled_dot_product_attention"): raise ImportError("AttnProcessor2_0 requires PyTorch 2.0, to use it, please upgrade PyTorch to 2.0.") def __call__( sel...
799
/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) # scaled_dot_product_attention expects attention_mask shape to be # (batch, heads, source_length, target_length) attention_mask = attention_mask.view(b...
799
/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) 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, -1, attn.heads, head_dim).transpose(1...
799
/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 attn.residual_connection: hidden_states = hidden_states + residual hidden_states = hidden_states / attn.rescale_output_factor return hidde...
799
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
class StableAudioAttnProcessor2_0: r""" Processor for implementing scaled dot-product attention (enabled by default if you're using PyTorch 2.0). This is used in the Stable Audio model. It applies rotary embedding on query and key vector, and allows MHA, GQA or MQA. """ def __init__(self): ...
800
/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, rotary_emb: Optional[torch.Tensor] = None, ) -> torch.Tensor: from .embeddings import apply_r...
800
/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) # scaled_dot_product_attention expects attention_mask shape to be # (batch, heads, source_length, target_length) attention_mask = attention_mask.view(b...
800
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
key = key.view(batch_size, -1, kv_heads, head_dim).transpose(1, 2) value = value.view(batch_size, -1, kv_heads, head_dim).transpose(1, 2) if kv_heads != attn.heads: # if GQA or MQA, repeat the key/value heads to reach the number of query heads. heads_per_kv_head = attn.heads // ...
800
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
rot_dim = rotary_emb[0].shape[-1] query_to_rotate, query_unrotated = query[..., :rot_dim], query[..., rot_dim:] query_rotated = apply_rotary_emb(query_to_rotate, rotary_emb, use_real=True, use_real_unbind_dim=-2) query = torch.cat((query_rotated, query_unrotated), dim=-1) ...
800
/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) # linear proj hidden_states = attn.to_out[0](hidden_states) # dropout hidden_states = attn.to_out[1](hidden_states) if input_ndim =...
800
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
class HunyuanAttnProcessor2_0: r""" Processor for implementing scaled dot-product attention (enabled by default if you're using PyTorch 2.0). This is used in the HunyuanDiT model. It applies a s normalization layer and rotary embedding on query and key vector. """ def __init__(self): if not...
801
/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) batch_size, sequence_length, _ = ( hidden_states.shape if encoder_hid...
801
/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) inner_dim...
801
/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 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...
801
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
class FusedHunyuanAttnProcessor2_0: r""" Processor for implementing scaled dot-product attention (enabled by default if you're using PyTorch 2.0) with fused projection layers. This is used in the HunyuanDiT model. It applies a s normalization layer and rotary embedding on query and key vector. """ ...
802
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
residual = hidden_states 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(...
802
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
if attn.group_norm is not None: hidden_states = attn.group_norm(hidden_states.transpose(1, 2)).transpose(1, 2) if encoder_hidden_states is None: qkv = attn.to_qkv(hidden_states) split_size = qkv.shape[-1] // 3 query, key, value = torch.split(qkv, split_size, dim=...
802
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
if attn.norm_q is not None: query = attn.norm_q(query) if attn.norm_k is not None: key = attn.norm_k(key) # Apply RoPE if needed if image_rotary_emb is not None: query = apply_rotary_emb(query, image_rotary_emb) if not attn.is_cross_attention: ...
802
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
if input_ndim == 4: hidden_states = hidden_states.transpose(-1, -2).reshape(batch_size, channel, height, width) if attn.residual_connection: hidden_states = hidden_states + residual hidden_states = hidden_states / attn.rescale_output_factor return hidden_states
802
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
class PAGHunyuanAttnProcessor2_0: r""" Processor for implementing scaled dot-product attention (enabled by default if you're using PyTorch 2.0). This is used in the HunyuanDiT model. It applies a normalization layer and rotary embedding on query and key vector. This variant of the processor employs [Per...
803
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
residual = hidden_states 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(...
803
/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) # scaled_dot_product_attention expects attention_mask shape to be # (batch, heads, source_length, target_length) attention_mask = attention_mask.view(b...
803
/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) if attn.norm_q is not None: query = attn.norm_q(query) if a...
803
/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...
803
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
if input_ndim == 4: hidden_states_ptb = hidden_states_ptb.transpose(-1, -2).reshape(batch_size, channel, height, width) # cat hidden_states = torch.cat([hidden_states_org, hidden_states_ptb]) if attn.residual_connection: hidden_states = hidden_states + residual ...
803
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
class PAGCFGHunyuanAttnProcessor2_0: r""" Processor for implementing scaled dot-product attention (enabled by default if you're using PyTorch 2.0). This is used in the HunyuanDiT model. It applies a normalization layer and rotary embedding on query and key vector. This variant of the processor employs [...
804
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
residual = hidden_states 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(...
804
/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) # scaled_dot_product_attention expects attention_mask shape to be # (batch, heads, source_length, target_length) attention_mask = attention_mask.view(b...
804
/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) if attn.norm_q is not None: query = attn.norm_q(query) if a...
804
/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...
804
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
if input_ndim == 4: hidden_states_ptb = hidden_states_ptb.transpose(-1, -2).reshape(batch_size, channel, height, width) # cat hidden_states = torch.cat([hidden_states_org, hidden_states_ptb]) if attn.residual_connection: hidden_states = hidden_states + residual ...
804
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
class LuminaAttnProcessor2_0: r""" Processor for implementing scaled dot-product attention (enabled by default if you're using PyTorch 2.0). This is used in the LuminaNextDiT model. It applies a s normalization layer and rotary embedding on query and key vector. """ def __init__(self): if n...
805
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
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) batch_size, sequence_length, _ = hidden_states.shape # Get Query-Key-Value Pair query = attn.to_q(hidden_...
805
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
# Apply RoPE if needed if query_rotary_emb is not None: query = apply_rotary_emb(query, query_rotary_emb, use_real=False) if key_rotary_emb is not None: key = apply_rotary_emb(key, key_rotary_emb, use_real=False) query, key = query.to(dtype), key.to(dtype) # App...
805
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
# scaled_dot_product_attention expects attention_mask shape to be # (batch, heads, source_length, target_length) attention_mask = attention_mask.bool().view(batch_size, 1, 1, -1) attention_mask = attention_mask.expand(-1, attn.heads, sequence_length, -1) query = query.transpose(1, 2) ...
805
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
class FusedAttnProcessor2_0: r""" Processor for implementing scaled dot-product attention (enabled by default if you're using PyTorch 2.0). It uses fused projection layers. For self-attention modules, all projection matrices (i.e., query, key, value) are fused. For cross-attention modules, key and value...
806
/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, *args, **kwargs, ) -> torch.Tensor: if l...
806
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
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) batch_size, sequence_length, _ = ( hidden_states.shape if encoder_hidden_states is None else encoder_hidden_st...
806
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py
if encoder_hidden_states is None: qkv = attn.to_qkv(hidden_states) split_size = qkv.shape[-1] // 3 query, key, value = torch.split(qkv, split_size, dim=-1) else: if attn.norm_cross: encoder_hidden_states = attn.norm_encoder_hidden_states(encoder_hi...
806
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/models/attention_processor.py