


import tempfile
import unittest

import numpy as np
import torch

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


class 	a__						(    unittest.TestCase     ):

       def     UpperCAmelCase( self   :  List[str]   ,				lowerCamelCase_   :  bool = False     ):
            a_     :  List[Any]				      =     1_0

            if only_cross_attention:
                 a_     :  Any				      =     1_2
            else:
                 # when only cross attention is not set, the cross attention dim must be the same as the query dim
                 a_     :  Union[str, Any]				      =     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   :  Union[str, Any]   ,				lowerCamelCase_   :  str   ,				lowerCamelCase_   :  str     ):
            a_     :  str				      =     2

            a_     :  int				      =     torch.rand(lowerCamelCase_   ,				lowerCamelCase_   ,				3   ,				2     )
            a_     :  List[str]				      =     torch.rand(lowerCamelCase_   ,				4   ,				lowerCamelCase_     )
            a_     :  Optional[int]				      =     None

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

       def     UpperCAmelCase( self   :  List[Any]     ):
            # self and cross attention

            torch.manual_seed(0     )

            a_     :  List[str]				      =     self.get_constructor_arguments(only_cross_attention=lowerCamelCase_     )
            a_     :  List[Any]				      =     Attention(**lowerCamelCase_     )

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

            a_     :  Tuple				      =     self.get_forward_arguments(
                query_dim=constructor_args["""query_dim"""]   ,				added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]     )

            a_     :  Dict				      =     attn(**lowerCamelCase_     )

            # only self attention

            torch.manual_seed(0     )

            a_     :  Tuple				      =     self.get_constructor_arguments(only_cross_attention=lowerCamelCase_     )
            a_     :  int				      =     Attention(**lowerCamelCase_     )

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

            a_     :  List[str]				      =     self.get_forward_arguments(
                query_dim=constructor_args["""query_dim"""]   ,				added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]     )

            a_     :  Union[str, Any]				      =     attn(**lowerCamelCase_     )

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


class 	a__						(    unittest.TestCase     ):

       def     UpperCAmelCase( self   :  str     ):
            a_     :  List[str]				      =     DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe"""   ,				safety_checker=lowerCamelCase_     )

            a_     :  Optional[Any]				      =     pipe(
                """foo"""   ,				num_inference_steps=2   ,				generator=torch.Generator("""cpu"""     ).manual_seed(0     )   ,				output_type="""np"""   ,				).images

            # the initial conversion succeeds
            a_     :  Optional[int]				      =     DiffusionPipeline.from_pretrained(
                """hf-internal-testing/tiny-stable-diffusion-pipe"""   ,				device_map="""sequential"""   ,				safety_checker=lowerCamelCase_     )

            a_     :  List[str]				      =     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(lowerCamelCase_     )

                 # can also load the converted weights
                 a_     :  List[str]				      =     DiffusionPipeline.from_pretrained(lowerCamelCase_   ,				device_map="""sequential"""   ,				safety_checker=lowerCamelCase_     )

            a_     :  List[Any]				      =     pipe(
                """foo"""   ,				num_inference_steps=2   ,				generator=torch.Generator("""cpu"""     ).manual_seed(0     )   ,				output_type="""np"""   ,				).images

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






from collections import OrderedDict
from typing import Mapping

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


__A	:   List[Any]	=					logging.get_logger(__name__)

__A	:   Tuple	=					{
    'google/electra-small-generator': 'https://huggingface.co/google/electra-small-generator/resolve/main/config.json',
    'google/electra-base-generator': 'https://huggingface.co/google/electra-base-generator/resolve/main/config.json',
    'google/electra-large-generator': 'https://huggingface.co/google/electra-large-generator/resolve/main/config.json',
    'google/electra-small-discriminator': (
        'https://huggingface.co/google/electra-small-discriminator/resolve/main/config.json'
    ),
    'google/electra-base-discriminator': (
        'https://huggingface.co/google/electra-base-discriminator/resolve/main/config.json'
    ),
    'google/electra-large-discriminator': (
        'https://huggingface.co/google/electra-large-discriminator/resolve/main/config.json'
    ),
}


class 			_SCREAMING_SNAKE_CASE      (  lowerCAmelCase__):
						_UpperCamelCase:Tuple	=		"electra"







						def __init__(			self     ,						_SCREAMING_SNAKE_CASE=3_0522     ,						_SCREAMING_SNAKE_CASE=128     ,						_SCREAMING_SNAKE_CASE=256     ,						_SCREAMING_SNAKE_CASE=12     ,						_SCREAMING_SNAKE_CASE=4     ,						_SCREAMING_SNAKE_CASE=1024     ,						_SCREAMING_SNAKE_CASE="gelu"     ,						_SCREAMING_SNAKE_CASE=0.1     ,						_SCREAMING_SNAKE_CASE=0.1     ,						_SCREAMING_SNAKE_CASE=512     ,						_SCREAMING_SNAKE_CASE=2     ,						_SCREAMING_SNAKE_CASE=0.0_2     ,						_SCREAMING_SNAKE_CASE=1E-12     ,						_SCREAMING_SNAKE_CASE="first"     ,						_SCREAMING_SNAKE_CASE=True     ,						_SCREAMING_SNAKE_CASE="gelu"     ,						_SCREAMING_SNAKE_CASE=0.1     ,						_SCREAMING_SNAKE_CASE=0     ,						_SCREAMING_SNAKE_CASE="absolute"     ,						_SCREAMING_SNAKE_CASE=True     ,						_SCREAMING_SNAKE_CASE=None     ,						**_SCREAMING_SNAKE_CASE     ,						)->							Optional[int]:
													super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE     ,						**_SCREAMING_SNAKE_CASE							)

													lowerCamelCase_								=vocab_size
													lowerCamelCase_								=embedding_size
													lowerCamelCase_								=hidden_size
													lowerCamelCase_								=num_hidden_layers
													lowerCamelCase_								=num_attention_heads
													lowerCamelCase_								=intermediate_size
													lowerCamelCase_								=hidden_act
													lowerCamelCase_								=hidden_dropout_prob
													lowerCamelCase_								=attention_probs_dropout_prob
													lowerCamelCase_								=max_position_embeddings
													lowerCamelCase_								=type_vocab_size
													lowerCamelCase_								=initializer_range
													lowerCamelCase_								=layer_norm_eps

													lowerCamelCase_								=summary_type
													lowerCamelCase_								=summary_use_proj
													lowerCamelCase_								=summary_activation
													lowerCamelCase_								=summary_last_dropout
													lowerCamelCase_								=position_embedding_type
													lowerCamelCase_								=use_cache
													lowerCamelCase_								=classifier_dropout


class 			_SCREAMING_SNAKE_CASE      (  lowerCAmelCase__):







						@property
						def        _snake_case				(			self							)->							Mapping[str, Mapping[int, str]]:
													if self.task == "multiple-choice":
																				lowerCamelCase_								={0: """batch""", 1: """choice""", 2: """sequence"""}
													else:
																				lowerCamelCase_								={0: """batch""", 1: """sequence"""}
													return OrderedDict(
													    [
													        ("""input_ids""", dynamic_axis),
													        ("""attention_mask""", dynamic_axis),
													        ("""token_type_ids""", dynamic_axis),
													    ]							)
