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.
| 3114 | |
| 3115 | |
| 3116 | class AttnProcessorNPU: |
| 3117 | r""" |
| 3118 | Processor for implementing flash attention using torch_npu. Torch_npu supports only fp16 and bf16 data types. If |
| 3119 | fp32 is used, F.scaled_dot_product_attention will be used for computation, but the acceleration effect on NPU is |
| 3120 | not significant. |
| 3121 | |
| 3122 | """ |
| 3123 | |
| 3124 | def __init__(self): |
| 3125 | if not is_torch_npu_available(): |
| 3126 | raise ImportError("AttnProcessorNPU requires torch_npu extensions and is supported only on npu devices.") |
| 3127 | |
| 3128 | def __call__( |
| 3129 | self, |
| 3130 | attn: Attention, |
| 3131 | hidden_states: torch.Tensor, |
| 3132 | encoder_hidden_states: Optional[torch.Tensor] = None, |
| 3133 | attention_mask: Optional[torch.Tensor] = None, |
| 3134 | temb: Optional[torch.Tensor] = None, |
| 3135 | *args, |
| 3136 | **kwargs, |
| 3137 | ) -> torch.Tensor: |
| 3138 | if len(args) > 0 or kwargs.get("scale", None) is not None: |
| 3139 | deprecation_message = "The `scale` argument is deprecated and will be ignored. Please remove it, as passing it will raise an error in the future. `scale` should directly be passed while calling the underlying pipeline component i.e., via `cross_attention_kwargs`." |
| 3140 | deprecate("scale", "1.0.0", deprecation_message) |
| 3141 | |
| 3142 | residual = hidden_states |
| 3143 | if attn.spatial_norm is not None: |
| 3144 | hidden_states = attn.spatial_norm(hidden_states, temb) |
| 3145 | |
| 3146 | input_ndim = hidden_states.ndim |
| 3147 | |
| 3148 | if input_ndim == 4: |
| 3149 | batch_size, channel, height, width = hidden_states.shape |
| 3150 | hidden_states = hidden_states.view(batch_size, channel, height * width).transpose(1, 2) |
| 3151 | |
| 3152 | batch_size, sequence_length, _ = ( |
| 3153 | hidden_states.shape if encoder_hidden_states is None else encoder_hidden_states.shape |
| 3154 | ) |
| 3155 | |
| 3156 | if attention_mask is not None: |
| 3157 | attention_mask = attn.prepare_attention_mask(attention_mask, sequence_length, batch_size) |
| 3158 | # scaled_dot_product_attention expects attention_mask shape to be |
| 3159 | # (batch, heads, source_length, target_length) |
| 3160 | attention_mask = attention_mask.view(batch_size, attn.heads, -1, attention_mask.shape[-1]) |
| 3161 | |
| 3162 | if attn.group_norm is not None: |
| 3163 | hidden_states = attn.group_norm(hidden_states.transpose(1, 2)).transpose(1, 2) |
| 3164 | |
| 3165 | query = attn.to_q(hidden_states) |
| 3166 | |
| 3167 | if encoder_hidden_states is None: |
| 3168 | encoder_hidden_states = hidden_states |
| 3169 | elif attn.norm_cross: |
| 3170 | encoder_hidden_states = attn.norm_encoder_hidden_states(encoder_hidden_states) |
| 3171 | |
| 3172 | key = attn.to_k(encoder_hidden_states) |
| 3173 | value = attn.to_v(encoder_hidden_states) |
no outgoing calls
no test coverage detected