'''simple docstring'''


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 					__SCREAMING_SNAKE_CASE						(	lowerCamelCase_						,     unittest.TestCase	):







  '''simple docstring'''






  lowerCamelCase_		:int						   =		PriorTransformer
  lowerCamelCase_		:Optional[int]						   =		"""hidden_states"""


  @property
  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       str       =				4
       UpperCAmelCase_							:       List[Any]       =				8
       UpperCAmelCase_							:       int       =				7

       UpperCAmelCase_							:       List[Any]       =				floats_tensor((batch_size, embedding_dim)						).to(lowerCAmelCase__						)

       UpperCAmelCase_							:       List[Any]       =				floats_tensor((batch_size, embedding_dim)						).to(lowerCAmelCase__						)
       UpperCAmelCase_							:       Optional[Any]       =				floats_tensor((batch_size, num_embeddings, embedding_dim)						).to(lowerCAmelCase__						)

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


  def _UpperCamelCase					(			self				,		snake_case_=0						):




       '''simple docstring'''

       torch.manual_seed(lowerCAmelCase__						)
       UpperCAmelCase_							:       Dict       =				4
       UpperCAmelCase_							:       Tuple       =				8
       UpperCAmelCase_							:       Any       =				7

       UpperCAmelCase_							:       Dict       =				torch.randn((batch_size, embedding_dim)						).to(lowerCAmelCase__						)

       UpperCAmelCase_							:       Optional[Any]       =				torch.randn((batch_size, embedding_dim)						).to(lowerCAmelCase__						)
       UpperCAmelCase_							:       Optional[int]       =				torch.randn((batch_size, num_embeddings, embedding_dim)						).to(lowerCAmelCase__						)

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


  @property
  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       return (4, 8)


  @property
  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       return (4, 8)


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       str       =				{
           'num_attention_heads': 2,
           'attention_head_dim': 4,
           'num_layers': 2,
           'embedding_dim': 8,
           'num_embeddings': 7,
           'additional_embeddings': 4,
       }
       UpperCAmelCase_							:       Optional[Any]       =				self.dummy_input
       return init_dict, inputs_dict


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_       ,      UpperCAmelCase_							:       str       =				PriorTransformer.from_pretrained(
           'hf-internal-testing/prior-dummy'				,		output_loading_info=lowerCAmelCase__						)
       self.assertIsNotNone(lowerCAmelCase__						)
       self.assertEqual(len(loading_info['missing_keys']						)				,		0						)

       model.to(lowerCAmelCase__						)
       UpperCAmelCase_							:       Union[str, Any]       =				model(**self.dummy_input						)[0]

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


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_       ,      UpperCAmelCase_							:       Any       =				self.prepare_init_args_and_inputs_for_common()

       UpperCAmelCase_							:       str       =				self.model_class(**lowerCAmelCase__						)
       UpperCAmelCase_							:       List[str]       =				inspect.signature(model.forward						)
       # signature.parameters is an OrderedDict => so arg_names order is deterministic
       UpperCAmelCase_							:       Tuple       =				[*signature.parameters.keys()]

       UpperCAmelCase_							:       Optional[Any]       =				['hidden_states', 'timestep']
       self.assertListEqual(arg_names[:2]				,		lowerCAmelCase__						)






  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       List[str]       =				PriorTransformer.from_pretrained('hf-internal-testing/prior-dummy'						)
       UpperCAmelCase_							:       Any       =				model.to(lowerCAmelCase__						)

       if hasattr(lowerCAmelCase__				,		'set_default_attn_processor'						):
            model.set_default_attn_processor()

       UpperCAmelCase_							:       Optional[Any]       =				self.get_dummy_seed_input()

       with torch.no_grad():
            UpperCAmelCase_							:       int       =				model(**lowerCAmelCase__						)[0]

       UpperCAmelCase_							:       List[Any]       =				output[0, :5].flatten().cpu()
       print(lowerCAmelCase__						)

       # 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.
       UpperCAmelCase_							:       Union[str, Any]       =				torch.tensor([-1.34_36, -0.28_70, 0.75_38, 0.43_68, -0.02_39]						)
       self.assertTrue(torch_all_close(lowerCAmelCase__				,		lowerCAmelCase__				,		rtol=1E-2						)						)







@slow
class 					__SCREAMING_SNAKE_CASE						(	unittest.TestCase	):







  '''simple docstring'''






  def _UpperCamelCase					(			self				,		snake_case_=1				,		snake_case_=7_6_8				,		snake_case_=7_7				,		snake_case_=0						):




       '''simple docstring'''

       torch.manual_seed(lowerCAmelCase__						)
       UpperCAmelCase_							:       Any       =				batch_size
       UpperCAmelCase_							:       str       =				embedding_dim
       UpperCAmelCase_							:       Dict       =				num_embeddings

       UpperCAmelCase_							:       List[str]       =				torch.randn((batch_size, embedding_dim)						).to(lowerCAmelCase__						)

       UpperCAmelCase_							:       List[Any]       =				torch.randn((batch_size, embedding_dim)						).to(lowerCAmelCase__						)
       UpperCAmelCase_							:       Optional[int]       =				torch.randn((batch_size, num_embeddings, embedding_dim)						).to(lowerCAmelCase__						)

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


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

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






  @parameterized.expand(
      [
          # fmt: off
          [1_3, [-0.58_61,  0.12_83, -0.09_31,  0.08_82,  0.44_76,  0.13_29, -0.04_98,  0.06_40]],
          [3_7, [-0.49_13,  0.01_10, -0.04_83,  0.05_41,  0.49_54, -0.01_70,  0.03_54,  0.16_51]],
          # fmt: on
      ]						)
  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       Tuple       =				PriorTransformer.from_pretrained('kandinsky-community/kandinsky-2-1-prior'				,		subfolder='prior'						)
       model.to(lowerCAmelCase__						)
       UpperCAmelCase_							:       int       =				self.get_dummy_seed_input(seed=lowerCAmelCase__						)

       with torch.no_grad():
            UpperCAmelCase_							:       int       =				model(**lowerCAmelCase__						)[0]

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

       UpperCAmelCase_							:       Optional[Any]       =				sample[0, :8].flatten().cpu()
       print(lowerCAmelCase__						)
       UpperCAmelCase_							:       int       =				torch.tensor(lowerCAmelCase__						)

       assert torch_all_close(lowerCAmelCase__				,		lowerCAmelCase__				,		atol=1E-3						)







'''simple docstring'''


from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig


snake_case__					:      Dict				      =  {
    '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/config.json''',
    '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/config.json''',
    '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/config.json''',
    '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/config.json''',
    '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/config.json''',
    '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/config.json''',
    '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/config.json''',
    '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/config.json''',
}






class 					__SCREAMING_SNAKE_CASE						(	lowerCamelCase_	):







  '''simple docstring'''






  lowerCamelCase_		:str						   =		'''albert'''


  def __init__(			self				,		snake_case_=3_0_0_0_0				,		snake_case_=1_2_8				,		snake_case_=4_0_9_6				,		snake_case_=1_2				,		snake_case_=1				,		snake_case_=6_4				,		snake_case_=1_6_3_8_4				,		snake_case_=1				,		snake_case_="gelu_new"				,		snake_case_=0				,		snake_case_=0				,		snake_case_=5_1_2				,		snake_case_=2				,		snake_case_=0.02				,		snake_case_=1E-12				,		snake_case_=0.1				,		snake_case_="absolute"				,		snake_case_=0				,		snake_case_=2				,		snake_case_=3				,		**snake_case_				,		):




       '''simple docstring'''

       super().__init__(pad_token_id=snake_case_				,		bos_token_id=snake_case_				,		eos_token_id=snake_case_				,		**snake_case_						)

       UpperCAmelCase_							:       List[Any]       =				vocab_size
       UpperCAmelCase_							:       Dict       =				embedding_size
       UpperCAmelCase_							:       str       =				hidden_size
       UpperCAmelCase_							:       Any       =				num_hidden_layers
       UpperCAmelCase_							:       Union[str, Any]       =				num_hidden_groups
       UpperCAmelCase_							:       List[str]       =				num_attention_heads
       UpperCAmelCase_							:       Any       =				inner_group_num
       UpperCAmelCase_							:       Optional[int]       =				hidden_act
       UpperCAmelCase_							:       Tuple       =				intermediate_size
       UpperCAmelCase_							:       Any       =				hidden_dropout_prob
       UpperCAmelCase_							:       List[str]       =				attention_probs_dropout_prob
       UpperCAmelCase_							:       Union[str, Any]       =				max_position_embeddings
       UpperCAmelCase_							:       Dict       =				type_vocab_size
       UpperCAmelCase_							:       Union[str, Any]       =				initializer_range
       UpperCAmelCase_							:       Optional[Any]       =				layer_norm_eps
       UpperCAmelCase_							:       Dict       =				classifier_dropout_prob
       UpperCAmelCase_							:       Tuple       =				position_embedding_type







class 					__SCREAMING_SNAKE_CASE						(	lowerCamelCase_	):







  '''simple docstring'''






  @property
  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       if self.task == "multiple-choice":
            UpperCAmelCase_							:       Optional[Any]       =				{0: 'batch', 1: 'choice', 2: 'sequence'}
       else:
            UpperCAmelCase_							:       List[Any]       =				{0: 'batch', 1: 'sequence'}
       return OrderedDict(
           [
               ('input_ids', dynamic_axis),
               ('attention_mask', dynamic_axis),
               ('token_type_ids', dynamic_axis),
           ]						)






