


"""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__				(		lowercase   ,    unittest.TestCase				):
       """simple docstring"""


       a  :				List[Any]   =PriorTransformer
       a  :				Optional[Any]   ="hidden_states"

       @property
       def        lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase     :     Optional[int]								=							4
         lowerCAmelCase     :     str								=							8
         lowerCAmelCase     :     Optional[Any]								=							7

         lowerCAmelCase     :     Tuple								=							floats_tensor((batch_size, embedding_dim)						).to(snake_case__						)

         lowerCAmelCase     :     Tuple								=							floats_tensor((batch_size, embedding_dim)						).to(snake_case__						)
         lowerCAmelCase     :     Tuple								=							floats_tensor((batch_size, num_embeddings, embedding_dim)						).to(snake_case__						)

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

       def        lowercase__							(						self		,     snake_case__=0						):






         """simple docstring"""





         torch.manual_seed(snake_case__						)
         lowerCAmelCase     :     Union[str, Any]								=							4
         lowerCAmelCase     :     List[Any]								=							8
         lowerCAmelCase     :     Tuple								=							7

         lowerCAmelCase     :     Optional[Any]								=							torch.randn((batch_size, embedding_dim)						).to(snake_case__						)

         lowerCAmelCase     :     List[Any]								=							torch.randn((batch_size, embedding_dim)						).to(snake_case__						)
         lowerCAmelCase     :     Tuple								=							torch.randn((batch_size, num_embeddings, embedding_dim)						).to(snake_case__						)

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

       @property
       def        lowercase__							(						self						):






         """simple docstring"""





         return (4, 8)

       @property
       def        lowercase__							(						self						):






         """simple docstring"""





         return (4, 8)

       def        lowercase__							(						self						):






         """simple docstring"""





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

       def        lowercase__							(						self						):






         """simple docstring"""





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

         model.to(snake_case__						)
         lowerCAmelCase     :     Optional[int]								=							model(**self.dummy_input						)[0]

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

       def        lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase						,  lowerCAmelCase     :     Union[str, Any]								=							self.prepare_init_args_and_inputs_for_common()

         lowerCAmelCase     :     int								=							self.model_class(**snake_case__						)
         lowerCAmelCase     :     Union[str, Any]								=							inspect.signature(model.forward						)
         # signature.parameters is an OrderedDict => so arg_names order is deterministic
         lowerCAmelCase     :     List[str]								=							[*signature.parameters.keys()]

         lowerCAmelCase     :     List[str]								=							["hidden_states", "timestep"]
         self.assertListEqual(arg_names[:2]		,     snake_case__						)







       def        lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase     :     Optional[Any]								=							PriorTransformer.from_pretrained("hf-internal-testing/prior-dummy"						)
         lowerCAmelCase     :     int								=							model.to(snake_case__						)

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

         lowerCAmelCase     :     Union[str, Any]								=							self.get_dummy_seed_input()

         with torch.no_grad():
           lowerCAmelCase     :     List[Any]								=							model(**snake_case__						)[0]

         lowerCAmelCase     :     Optional[Any]								=							output[0, :5].flatten().cpu()
         print(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.
         lowerCAmelCase     :     List[str]								=							torch.tensor([-1.3436, -0.2870, 0.7538, 0.4368, -0.0239]						)
         self.assertTrue(torch_all_close(snake_case__		,     snake_case__		,     rtol=1e-2						)						)


@slow
class       SCREAMING_SNAKE_CASE__				(		unittest.TestCase				):
       """simple docstring"""


       def        lowercase__							(						self		,     snake_case__=1		,     snake_case__=768		,     snake_case__=77		,     snake_case__=0						):






         """simple docstring"""





         torch.manual_seed(snake_case__						)
         lowerCAmelCase     :     Union[str, Any]								=							batch_size
         lowerCAmelCase     :     Optional[Any]								=							embedding_dim
         lowerCAmelCase     :     int								=							num_embeddings

         lowerCAmelCase     :     Union[str, Any]								=							torch.randn((batch_size, embedding_dim)						).to(snake_case__						)

         lowerCAmelCase     :     Dict								=							torch.randn((batch_size, embedding_dim)						).to(snake_case__						)
         lowerCAmelCase     :     Optional[Any]								=							torch.randn((batch_size, num_embeddings, embedding_dim)						).to(snake_case__						)

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

       def        lowercase__							(						self						):






         """simple docstring"""





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







       @parameterized.expand(
           [
               # fmt: off
               [13, [-0.5861,  0.1283, -0.0931,  0.0882,  0.4476,  0.1329, -0.0498,  0.0640]],
               [37, [-0.4913,  0.0110, -0.0483,  0.0541,  0.4954, -0.0170,  0.0354,  0.1651]],
               # fmt: on
           ]						)
       def        lowercase__							(						self		,     snake_case__		,     snake_case__						):






         """simple docstring"""





         lowerCAmelCase     :     str								=							PriorTransformer.from_pretrained("kandinsky-community/kandinsky-2-1-prior"		,     subfolder="prior"						)
         model.to(snake_case__						)
         lowerCAmelCase     :     List[Any]								=							self.get_dummy_seed_input(seed=snake_case__						)

         with torch.no_grad():
           lowerCAmelCase     :     Tuple								=							model(**snake_case__						)[0]

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

         lowerCAmelCase     :     Optional[int]								=							sample[0, :8].flatten().cpu()
         print(snake_case__						)
         lowerCAmelCase     :     List[Any]								=							torch.tensor(snake_case__						)

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









"""simple docstring"""







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


lowerCAmelCase__      				=   logging.get_logger(__name__)

lowerCAmelCase__      				=   {}


class       SCREAMING_SNAKE_CASE__				(		lowercase				):
       """simple docstring"""


       a  :				str   ="llama"
       a  :				List[str]   =["past_key_values"]

       def __init__(						self		,     snake_case__=32_000		,     snake_case__=4_096		,     snake_case__=11_008		,     snake_case__=32		,     snake_case__=32		,     snake_case__=None		,     snake_case__="silu"		,     snake_case__=2_048		,     snake_case__=0.02		,     snake_case__=1e-6		,     snake_case__=True		,     snake_case__=0		,     snake_case__=1		,     snake_case__=2		,     snake_case__=1		,     snake_case__=False		,     snake_case__=None		,     **snake_case__		,     ):






         """simple docstring"""





         lowerCAmelCase     :     Optional[Any]								=							vocab_size
         lowerCAmelCase     :     str								=							max_position_embeddings
         lowerCAmelCase     :     str								=							hidden_size
         lowerCAmelCase     :     Optional[int]								=							intermediate_size
         lowerCAmelCase     :     Any								=							num_hidden_layers
         lowerCAmelCase     :     List[str]								=							num_attention_heads

         # for backward compatibility
         if num_key_value_heads is None:
           lowerCAmelCase     :     Tuple								=							num_attention_heads

         lowerCAmelCase     :     Dict								=							num_key_value_heads
         lowerCAmelCase     :     Optional[Any]								=							hidden_act
         lowerCAmelCase     :     Optional[Any]								=							initializer_range
         lowerCAmelCase     :     Any								=							rms_norm_eps
         lowerCAmelCase     :     List[Any]								=							pretraining_tp
         lowerCAmelCase     :     int								=							use_cache
         lowerCAmelCase     :     List[str]								=							rope_scaling
         self._rope_scaling_validation()

         super().__init__(
             pad_token_id=snake_case__		,     bos_token_id=snake_case__		,     eos_token_id=snake_case__		,     tie_word_embeddings=snake_case__		,     **snake_case__		,     )







       def        lowercase__							(						self						):






         """simple docstring"""





         if self.rope_scaling is None:
           return

         if not isinstance(self.rope_scaling		,     snake_case__						) or len(self.rope_scaling						) != 2:
           raise ValueError(
               "`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, "
               f"""got {self.rope_scaling}"""						)
         lowerCAmelCase     :     Optional[Any]								=							self.rope_scaling.get("type"		,     snake_case__						)
         lowerCAmelCase     :     int								=							self.rope_scaling.get("factor"		,     snake_case__						)
         if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
           raise ValueError(
               f"""`rope_scaling`'s name field must be one of ['linear', 'dynamic'], got {rope_scaling_type}"""						)
         if rope_scaling_factor is None or not isinstance(snake_case__		,     snake_case__						) or rope_scaling_factor <= 1.0:
           raise ValueError(f"""`rope_scaling`'s factor field must be an float > 1, got {rope_scaling_factor}"""						)





