

'''simple docstring'''

from ...configuration_utils import PretrainedConfig
from ...utils import logging


SCREAMING_SNAKE_CASE							:				Optional[int]     	=			logging.get_logger(__name__)

SCREAMING_SNAKE_CASE							:				Tuple     	=			{'openai-gpt': 'https://huggingface.co/openai-gpt/resolve/main/config.json'}






class    snake_case     (      _a		):






				"""simple docstring"""
				_a    	=     """openai-gpt"""
				_a    	=     {
				    """max_position_embeddings""": """n_positions""",
				    """hidden_size""": """n_embd""",
				    """num_attention_heads""": """n_head""",
				    """num_hidden_layers""": """n_layer""",
				}



				def __init__(    self,     _lowercase=40478,     _lowercase=512,     _lowercase=768,     _lowercase=12,     _lowercase=12,     _lowercase="gelu",     _lowercase=0.1,     _lowercase=0.1,     _lowercase=0.1,     _lowercase=1E-5,     _lowercase=0.02,     _lowercase="cls_index",     _lowercase=True,     _lowercase=None,     _lowercase=True,     _lowercase=0.1,     **_lowercase,     )     ->							str:
											SCREAMING_SNAKE_CASE_				=				vocab_size
											SCREAMING_SNAKE_CASE_				=				n_positions
											SCREAMING_SNAKE_CASE_				=				n_embd
											SCREAMING_SNAKE_CASE_				=				n_layer
											SCREAMING_SNAKE_CASE_				=				n_head
											SCREAMING_SNAKE_CASE_				=				afn
											SCREAMING_SNAKE_CASE_				=				resid_pdrop
											SCREAMING_SNAKE_CASE_				=				embd_pdrop
											SCREAMING_SNAKE_CASE_				=				attn_pdrop
											SCREAMING_SNAKE_CASE_				=				layer_norm_epsilon
											SCREAMING_SNAKE_CASE_				=				initializer_range
											SCREAMING_SNAKE_CASE_				=				summary_type
											SCREAMING_SNAKE_CASE_				=				summary_use_proj
											SCREAMING_SNAKE_CASE_				=				summary_activation
											SCREAMING_SNAKE_CASE_				=				summary_first_dropout
											SCREAMING_SNAKE_CASE_				=				summary_proj_to_labels
											super().__init__(**_lowercase       )









import gc
import inspect
import unittest

import torch
from parameterized import parameterized

from diffusers import PriorTransformer
from diffusers.utils import floats_tensor, slow, torch_all_close, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin


enable_full_determinism()






class      UpperCamelCase(	_a  ,				unittest.TestCase ):
					snake_case_		:					Any    					=						PriorTransformer
					snake_case_		:					List[str]    					=						"""hidden_states"""


					@property
					def 				SCREAMING_SNAKE_CASE_	(    self						:		str    )      ->						str:




											'''simple docstring'''

											__snake_case				=		4
											__snake_case				=		8
											__snake_case				=		7

											__snake_case				=		floats_tensor((batch_size, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )

											__snake_case				=		floats_tensor((batch_size, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )
											__snake_case				=		floats_tensor((batch_size, num_embeddings, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )

											return {
											    "hidden_states": hidden_states,
											    "timestep": 2,
											    "proj_embedding": proj_embedding,
											    "encoder_hidden_states": encoder_hidden_states,
											}


					def 				SCREAMING_SNAKE_CASE_	(    self						:		str  ,     SCREAMING_SNAKE_CASE						:		Optional[Any]=0    )      ->						Any:




											'''simple docstring'''

											torch.manual_seed(SCREAMING_SNAKE_CASE    )
											__snake_case				=		4
											__snake_case				=		8
											__snake_case				=		7

											__snake_case				=		torch.randn((batch_size, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )

											__snake_case				=		torch.randn((batch_size, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )
											__snake_case				=		torch.randn((batch_size, num_embeddings, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )

											return {
											    "hidden_states": hidden_states,
											    "timestep": 2,
											    "proj_embedding": proj_embedding,
											    "encoder_hidden_states": encoder_hidden_states,
											}


					@property
					def 				SCREAMING_SNAKE_CASE_	(    self						:		str    )      ->						str:




											'''simple docstring'''

											return (4, 8)


					@property
					def 				SCREAMING_SNAKE_CASE_	(    self						:		List[str]    )      ->						Any:




											'''simple docstring'''

											return (4, 8)


					def 				SCREAMING_SNAKE_CASE_	(    self						:		str    )      ->						Dict:




											'''simple docstring'''

											__snake_case				=		{
											    "num_attention_heads": 2,
											    "attention_head_dim": 4,
											    "num_layers": 2,
											    "embedding_dim": 8,
											    "num_embeddings": 7,
											    "additional_embeddings": 4,
											}
											__snake_case				=		self.dummy_input
											return init_dict, inputs_dict


					def 				SCREAMING_SNAKE_CASE_	(    self						:		Any    )      ->						Dict:




											'''simple docstring'''

											__snake_case		,       __snake_case				=		PriorTransformer.from_pretrained(
											    "hf-internal-testing/prior-dummy"  ,     output_loading_info=SCREAMING_SNAKE_CASE    )
											self.assertIsNotNone(SCREAMING_SNAKE_CASE    )
											self.assertEqual(len(loading_info["missing_keys"]    )  ,     0    )

											model.to(SCREAMING_SNAKE_CASE    )
											__snake_case				=		model(**self.dummy_input    )[0]

											assert hidden_states is not None, "Make sure output is not None"


					def 				SCREAMING_SNAKE_CASE_	(    self						:		Dict    )      ->						Optional[Any]:




											'''simple docstring'''

											__snake_case		,       __snake_case				=		self.prepare_init_args_and_inputs_for_common()

											__snake_case				=		self.model_class(**SCREAMING_SNAKE_CASE    )
											__snake_case				=		inspect.signature(model.forward    )
											# signature.parameters is an OrderedDict => so arg_names order is deterministic
											__snake_case				=		[*signature.parameters.keys()]

											__snake_case				=		["hidden_states", "timestep"]
											self.assertListEqual(arg_names[:2]  ,     SCREAMING_SNAKE_CASE    )






					def 				SCREAMING_SNAKE_CASE_	(    self						:		Optional[int]    )      ->						List[str]:




											'''simple docstring'''

											__snake_case				=		PriorTransformer.from_pretrained("hf-internal-testing/prior-dummy"    )
											__snake_case				=		model.to(SCREAMING_SNAKE_CASE    )

											if hasattr(SCREAMING_SNAKE_CASE  ,     "set_default_attn_processor"    ):
																	model.set_default_attn_processor()

											__snake_case				=		self.get_dummy_seed_input()

											with torch.no_grad():
																	__snake_case				=		model(**SCREAMING_SNAKE_CASE    )[0]

											__snake_case				=		output[0, :5].flatten().cpu()
											print(SCREAMING_SNAKE_CASE    )

											# Since the VAE Gaussian prior's generator is seeded on the appropriate device,
											# the expected output slices are not the same for CPU and GPU.
											__snake_case				=		torch.tensor([-1.3436, -0.2870, 0.7538, 0.4368, -0.0239]    )
											self.assertTrue(torch_all_close(SCREAMING_SNAKE_CASE  ,     SCREAMING_SNAKE_CASE  ,     rtol=1e-2    )    )






@slow
class      UpperCamelCase(	unittest.TestCase ):


					def 				SCREAMING_SNAKE_CASE_	(    self						:		List[Any]  ,     SCREAMING_SNAKE_CASE						:		int=1  ,     SCREAMING_SNAKE_CASE						:		str=7_6_8  ,     SCREAMING_SNAKE_CASE						:		Dict=7_7  ,     SCREAMING_SNAKE_CASE						:		int=0    )      ->						List[str]:




											'''simple docstring'''

											torch.manual_seed(SCREAMING_SNAKE_CASE    )
											__snake_case				=		batch_size
											__snake_case				=		embedding_dim
											__snake_case				=		num_embeddings

											__snake_case				=		torch.randn((batch_size, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )

											__snake_case				=		torch.randn((batch_size, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )
											__snake_case				=		torch.randn((batch_size, num_embeddings, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )

											return {
											    "hidden_states": hidden_states,
											    "timestep": 2,
											    "proj_embedding": proj_embedding,
											    "encoder_hidden_states": encoder_hidden_states,
											}


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




											'''simple docstring'''

											super().tearDown()
											gc.collect()
											torch.cuda.empty_cache()






					@parameterized.expand(
					    [
					        # fmt: off
					        [1_3, [-0.5861,  0.1283, -0.0931,  0.0882,  0.4476,  0.1329, -0.0498,  0.0640]],
					        [3_7, [-0.4913,  0.0110, -0.0483,  0.0541,  0.4954, -0.0170,  0.0354,  0.1651]],
					        # fmt: on
					    ]    )
					def 				SCREAMING_SNAKE_CASE_	(    self						:		Union[str, Any]  ,     SCREAMING_SNAKE_CASE						:		int  ,     SCREAMING_SNAKE_CASE						:		Tuple    )      ->						List[str]:




											'''simple docstring'''

											__snake_case				=		PriorTransformer.from_pretrained("kandinsky-community/kandinsky-2-1-prior"  ,     subfolder="prior"    )
											model.to(SCREAMING_SNAKE_CASE    )
											__snake_case				=		self.get_dummy_seed_input(seed=SCREAMING_SNAKE_CASE    )

											with torch.no_grad():
																	__snake_case				=		model(**SCREAMING_SNAKE_CASE    )[0]

											assert list(sample.shape    ) == [1, 7_6_8]

											__snake_case				=		sample[0, :8].flatten().cpu()
											print(SCREAMING_SNAKE_CASE    )
											__snake_case				=		torch.tensor(SCREAMING_SNAKE_CASE    )

											assert torch_all_close(SCREAMING_SNAKE_CASE  ,     SCREAMING_SNAKE_CASE  ,     atol=1e-3    )





