

'''simple docstring'''



import tempfile
import unittest

import numpy as np
import torch

from diffusers import DiffusionPipeline
from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor







class 		lowerCAmelCase__     (   unittest.TestCase      ):



 """simple docstring"""



 def    UpperCAmelCase__	(	self      : List[Any]      ,							__SCREAMING_SNAKE_CASE      : bool = False    )						->				Optional[int]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE						     =   10

      if only_cross_attention:
           __SCREAMING_SNAKE_CASE						     =   12
      else:
           # when only cross attention is not set, the cross attention dim must be the same as the query dim
           __SCREAMING_SNAKE_CASE						     =   query_dim

      return {
          "query_dim": query_dim,
          "cross_attention_dim": cross_attention_dim,
          "heads": 2,
          "dim_head": 4,
          "added_kv_proj_dim": 6,
          "norm_num_groups": 1,
          "only_cross_attention": only_cross_attention,
          "processor": AttnAddedKVProcessor(),
      }
 def    UpperCAmelCase__	(	self      : Optional[int]      ,							__SCREAMING_SNAKE_CASE      : Optional[int]      ,							__SCREAMING_SNAKE_CASE      : Optional[Any]    )						->				Dict:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE						     =   2

      __SCREAMING_SNAKE_CASE						     =   torch.rand(__SCREAMING_SNAKE_CASE      ,							__SCREAMING_SNAKE_CASE      ,							3      ,							2    )
      __SCREAMING_SNAKE_CASE						     =   torch.rand(__SCREAMING_SNAKE_CASE      ,							4      ,							__SCREAMING_SNAKE_CASE    )
      __SCREAMING_SNAKE_CASE						     =   None

      return {
          "hidden_states": hidden_states,
          "encoder_hidden_states": encoder_hidden_states,
          "attention_mask": attention_mask,
      }


 def    UpperCAmelCase__	(	self      : Any    )						->				Union[str, Any]:


      """simple docstring"""




      torch.manual_seed(0    )

      __SCREAMING_SNAKE_CASE						     =   self.get_constructor_arguments(only_cross_attention=__SCREAMING_SNAKE_CASE    )
      __SCREAMING_SNAKE_CASE						     =   Attention(**__SCREAMING_SNAKE_CASE    )

      self.assertTrue(attn.to_k is not None    )
      self.assertTrue(attn.to_v is not None    )

      __SCREAMING_SNAKE_CASE						     =   self.get_forward_arguments(
          query_dim=constructor_args["""query_dim"""]      ,							added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]    )

      __SCREAMING_SNAKE_CASE						     =   attn(**__SCREAMING_SNAKE_CASE    )

      # only self attention

      torch.manual_seed(0    )

      __SCREAMING_SNAKE_CASE						     =   self.get_constructor_arguments(only_cross_attention=__SCREAMING_SNAKE_CASE    )
      __SCREAMING_SNAKE_CASE						     =   Attention(**__SCREAMING_SNAKE_CASE    )

      self.assertTrue(attn.to_k is None    )
      self.assertTrue(attn.to_v is None    )

      __SCREAMING_SNAKE_CASE						     =   self.get_forward_arguments(
          query_dim=constructor_args["""query_dim"""]      ,							added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]    )

      __SCREAMING_SNAKE_CASE						     =   attn(**__SCREAMING_SNAKE_CASE    )

      self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()    )







class 		lowerCAmelCase__     (   unittest.TestCase      ):



 """simple docstring"""



 def    UpperCAmelCase__	(	self      : Optional[int]    )						->				Union[str, Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE						     =   DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe"""      ,							safety_checker=__SCREAMING_SNAKE_CASE    )

      __SCREAMING_SNAKE_CASE						     =   pipe(
          """foo"""      ,							num_inference_steps=2      ,							generator=torch.Generator("""cpu"""    ).manual_seed(0    )      ,							output_type="""np"""      ,							).images

      # the initial conversion succeeds
      __SCREAMING_SNAKE_CASE						     =   DiffusionPipeline.from_pretrained(
          """hf-internal-testing/tiny-stable-diffusion-pipe"""      ,							device_map="""sequential"""      ,							safety_checker=__SCREAMING_SNAKE_CASE    )

      __SCREAMING_SNAKE_CASE						     =   pipe(
          """foo"""      ,							num_inference_steps=2      ,							generator=torch.Generator("""cpu"""    ).manual_seed(0    )      ,							output_type="""np"""      ,							).images

      with tempfile.TemporaryDirectory() as tmpdir:
           # save the converted model
           pipe.save_pretrained(__SCREAMING_SNAKE_CASE    )

           # can also load the converted weights
           __SCREAMING_SNAKE_CASE						     =   DiffusionPipeline.from_pretrained(__SCREAMING_SNAKE_CASE      ,							device_map="""sequential"""      ,							safety_checker=__SCREAMING_SNAKE_CASE    )

      __SCREAMING_SNAKE_CASE						     =   pipe(
          """foo"""      ,							num_inference_steps=2      ,							generator=torch.Generator("""cpu"""    ).manual_seed(0    )      ,							output_type="""np"""      ,							).images

      self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE      ,							__SCREAMING_SNAKE_CASE      ,							atol=1E-5    )    )
      self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE      ,							__SCREAMING_SNAKE_CASE      ,							atol=1E-5    )    )









'''simple docstring'''



from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxSeqaSeqConfigWithPast
from ...utils import logging


UpperCAmelCase				: Union[str, Any]         =  logging.get_logger(__name__)







class 		lowerCAmelCase__     (   a      ):



 """simple docstring"""



 lowerCAmelCase__						      =   "mt5"
 lowerCAmelCase__						      =   ["past_key_values"]
 def __init__(	self      : str      ,							__SCREAMING_SNAKE_CASE      : Optional[int]=250_112      ,							__SCREAMING_SNAKE_CASE      : Union[str, Any]=512      ,							__SCREAMING_SNAKE_CASE      : Optional[Any]=64      ,							__SCREAMING_SNAKE_CASE      : Optional[int]=1_024      ,							__SCREAMING_SNAKE_CASE      : List[str]=8      ,							__SCREAMING_SNAKE_CASE      : Any=None      ,							__SCREAMING_SNAKE_CASE      : List[str]=6      ,							__SCREAMING_SNAKE_CASE      : Any=32      ,							__SCREAMING_SNAKE_CASE      : Union[str, Any]=128      ,							__SCREAMING_SNAKE_CASE      : List[str]=0.1      ,							__SCREAMING_SNAKE_CASE      : Union[str, Any]=1E-6      ,							__SCREAMING_SNAKE_CASE      : Optional[Any]=1.0      ,							__SCREAMING_SNAKE_CASE      : Tuple="gated-gelu"      ,							__SCREAMING_SNAKE_CASE      : Union[str, Any]=True      ,							__SCREAMING_SNAKE_CASE      : Union[str, Any]=True      ,							__SCREAMING_SNAKE_CASE      : Tuple="T5Tokenizer"      ,							__SCREAMING_SNAKE_CASE      : Optional[int]=False      ,							__SCREAMING_SNAKE_CASE      : Tuple=0      ,							__SCREAMING_SNAKE_CASE      : str=1      ,							__SCREAMING_SNAKE_CASE      : Dict=0      ,							**__SCREAMING_SNAKE_CASE      : str      ,							)						->				List[Any]:


      """simple docstring"""




      super().__init__(
          is_encoder_decoder=__SCREAMING_SNAKE_CASE      ,							tokenizer_class=__SCREAMING_SNAKE_CASE      ,							tie_word_embeddings=__SCREAMING_SNAKE_CASE      ,							pad_token_id=__SCREAMING_SNAKE_CASE      ,							eos_token_id=__SCREAMING_SNAKE_CASE      ,							decoder_start_token_id=__SCREAMING_SNAKE_CASE      ,							**__SCREAMING_SNAKE_CASE      ,							)
      __SCREAMING_SNAKE_CASE						     =   vocab_size
      __SCREAMING_SNAKE_CASE						     =   d_model
      __SCREAMING_SNAKE_CASE						     =   d_kv
      __SCREAMING_SNAKE_CASE						     =   d_ff
      __SCREAMING_SNAKE_CASE						     =   num_layers
      __SCREAMING_SNAKE_CASE						     =   (
          num_decoder_layers if num_decoder_layers is not None else self.num_layers
      )  # default = symmetry
      __SCREAMING_SNAKE_CASE						     =   num_heads
      __SCREAMING_SNAKE_CASE						     =   relative_attention_num_buckets
      __SCREAMING_SNAKE_CASE						     =   relative_attention_max_distance
      __SCREAMING_SNAKE_CASE						     =   dropout_rate
      __SCREAMING_SNAKE_CASE						     =   layer_norm_epsilon
      __SCREAMING_SNAKE_CASE						     =   initializer_factor
      __SCREAMING_SNAKE_CASE						     =   feed_forward_proj
      __SCREAMING_SNAKE_CASE						     =   use_cache

      __SCREAMING_SNAKE_CASE						     =   self.feed_forward_proj.split("""-"""    )
      __SCREAMING_SNAKE_CASE						     =   act_info[-1]
      __SCREAMING_SNAKE_CASE						     =   act_info[0] == """gated"""

      if len(__SCREAMING_SNAKE_CASE    ) > 1 and act_info[0] != "gated" or len(__SCREAMING_SNAKE_CASE    ) > 2:
           raise ValueError(
               f'`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer.'
               """Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. """
               """'gated-gelu' or 'relu'"""    )

      # for backwards compatibility
      if feed_forward_proj == "gated-gelu":
           __SCREAMING_SNAKE_CASE						     =   """gelu_new"""
 @property
 def    UpperCAmelCase__	(	self      : Optional[int]    )						->				str:


      """simple docstring"""




      return self.d_model
 @property
 def    UpperCAmelCase__	(	self      : List[str]    )						->				int:


      """simple docstring"""




      return self.num_heads


 @property
 def    UpperCAmelCase__	(	self      : List[str]    )						->				Union[str, Any]:


      """simple docstring"""




      return self.num_layers







class 		lowerCAmelCase__     (   a      ):



 """simple docstring"""



 @property
 # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.inputs
 def    UpperCAmelCase__	(	self      : Union[str, Any]    )						->				Mapping[str, Mapping[int, str]]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE						     =   {
          """input_ids""": {0: """batch""", 1: """encoder_sequence"""},
          """attention_mask""": {0: """batch""", 1: """encoder_sequence"""},
      }
      if self.use_past:
           __SCREAMING_SNAKE_CASE						     =   """past_encoder_sequence + sequence"""
           __SCREAMING_SNAKE_CASE						     =   {0: """batch"""}
           __SCREAMING_SNAKE_CASE						     =   {0: """batch""", 1: """past_decoder_sequence + sequence"""}
      else:
           __SCREAMING_SNAKE_CASE						     =   {0: """batch""", 1: """decoder_sequence"""}
           __SCREAMING_SNAKE_CASE						     =   {0: """batch""", 1: """decoder_sequence"""}

      if self.use_past:
           self.fill_with_past_key_values_(__SCREAMING_SNAKE_CASE      ,							direction="""inputs"""    )

      return common_inputs
 @property
 # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.default_onnx_opset
 def    UpperCAmelCase__	(	self      : Optional[int]    )						->				int:


      """simple docstring"""




      return 13


 @property
 def    UpperCAmelCase__	(	self      : Tuple    )						->				float:


      """simple docstring"""




      return 5E-4






