




from __future__ import annotations

import unittest

from transformers import DistilBertConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
   import tensorflow as tf

   from transformers.models.distilbert.modeling_tf_distilbert import (
       TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
       TFDistilBertForMaskedLM,
       TFDistilBertForMultipleChoice,
       TFDistilBertForQuestionAnswering,
       TFDistilBertForSequenceClassification,
       TFDistilBertForTokenClassification,
       TFDistilBertModel,
   )





class 			lowerCamelCase_				:


 '''simple docstring'''







 def __init__(				self    :						Union[str, Any]							,							_lowerCAmelCase    :						int							,							):
       SCREAMING_SNAKE_CASE_      		=     parent
       SCREAMING_SNAKE_CASE_      		=     13
       SCREAMING_SNAKE_CASE_      		=     7
       SCREAMING_SNAKE_CASE_      		=     True
       SCREAMING_SNAKE_CASE_      		=     True
       SCREAMING_SNAKE_CASE_      		=     False
       SCREAMING_SNAKE_CASE_      		=     True
       SCREAMING_SNAKE_CASE_      		=     99
       SCREAMING_SNAKE_CASE_      		=     32
       SCREAMING_SNAKE_CASE_      		=     2
       SCREAMING_SNAKE_CASE_      		=     4
       SCREAMING_SNAKE_CASE_      		=     37
       SCREAMING_SNAKE_CASE_      		=     'gelu'
       SCREAMING_SNAKE_CASE_      		=     0.1
       SCREAMING_SNAKE_CASE_      		=     0.1
       SCREAMING_SNAKE_CASE_      		=     512
       SCREAMING_SNAKE_CASE_      		=     16
       SCREAMING_SNAKE_CASE_      		=     2
       SCREAMING_SNAKE_CASE_      		=     0.02
       SCREAMING_SNAKE_CASE_      		=     3
       SCREAMING_SNAKE_CASE_      		=     4
       SCREAMING_SNAKE_CASE_      		=     None



 def  lowerCAmelCase_    (				self    :						str		):
       SCREAMING_SNAKE_CASE_      		=     ids_tensor([self.batch_size, self.seq_length]							,							self.vocab_size		)

       SCREAMING_SNAKE_CASE_      		=     None
       if self.use_input_mask:
             SCREAMING_SNAKE_CASE_      		=     random_attention_mask([self.batch_size, self.seq_length]		)

       SCREAMING_SNAKE_CASE_      		=     None
       SCREAMING_SNAKE_CASE_      		=     None
       SCREAMING_SNAKE_CASE_      		=     None
       if self.use_labels:
             SCREAMING_SNAKE_CASE_      		=     ids_tensor([self.batch_size]							,							self.type_sequence_label_size		)
             SCREAMING_SNAKE_CASE_      		=     ids_tensor([self.batch_size, self.seq_length]							,							self.num_labels		)
             SCREAMING_SNAKE_CASE_      		=     ids_tensor([self.batch_size]							,							self.num_choices		)

       SCREAMING_SNAKE_CASE_      		=     DistilBertConfig(
           vocab_size=self.vocab_size							,							dim=self.hidden_size							,							n_layers=self.num_hidden_layers							,							n_heads=self.num_attention_heads							,							hidden_dim=self.intermediate_size							,							hidden_act=self.hidden_act							,							dropout=self.hidden_dropout_prob							,							attention_dropout=self.attention_probs_dropout_prob							,							max_position_embeddings=self.max_position_embeddings							,							initializer_range=self.initializer_range							,							)

       return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels



 def  lowerCAmelCase_    (				self    :						List[Any]							,							_lowerCAmelCase    :						int							,							_lowerCAmelCase    :						Union[str, Any]							,							_lowerCAmelCase    :						int							,							_lowerCAmelCase    :						Dict							,							_lowerCAmelCase    :						Union[str, Any]							,							_lowerCAmelCase    :						int		):
       SCREAMING_SNAKE_CASE_      		=     TFDistilBertModel(config=_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     {'input_ids': input_ids, 'attention_mask': input_mask}

       SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_      		=     [input_ids, input_mask]

       SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase		)

       self.parent.assertEqual(result.last_hidden_state.shape							,							(self.batch_size, self.seq_length, self.hidden_size)		)



 def  lowerCAmelCase_    (				self    :						Dict							,							_lowerCAmelCase    :						Tuple							,							_lowerCAmelCase    :						List[str]							,							_lowerCAmelCase    :						Optional[Any]							,							_lowerCAmelCase    :						int							,							_lowerCAmelCase    :						Optional[Any]							,							_lowerCAmelCase    :						Dict		):
       SCREAMING_SNAKE_CASE_      		=     TFDistilBertForMaskedLM(config=_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     {'input_ids': input_ids, 'attention_mask': input_mask}
       SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase		)
       self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.seq_length, self.vocab_size)		)



 def  lowerCAmelCase_    (				self    :						Dict							,							_lowerCAmelCase    :						Optional[Any]							,							_lowerCAmelCase    :						List[str]							,							_lowerCAmelCase    :						Union[str, Any]							,							_lowerCAmelCase    :						str							,							_lowerCAmelCase    :						List[str]							,							_lowerCAmelCase    :						int		):
       SCREAMING_SNAKE_CASE_      		=     TFDistilBertForQuestionAnswering(config=_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     {
           'input_ids': input_ids,
           'attention_mask': input_mask,
       }
       SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase		)
       self.parent.assertEqual(result.start_logits.shape							,							(self.batch_size, self.seq_length)		)
       self.parent.assertEqual(result.end_logits.shape							,							(self.batch_size, self.seq_length)		)



 def  lowerCAmelCase_    (				self    :						Tuple							,							_lowerCAmelCase    :						List[str]							,							_lowerCAmelCase    :						List[Any]							,							_lowerCAmelCase    :						List[str]							,							_lowerCAmelCase    :						int							,							_lowerCAmelCase    :						Optional[int]							,							_lowerCAmelCase    :						Optional[int]		):
       SCREAMING_SNAKE_CASE_      		=     self.num_labels
       SCREAMING_SNAKE_CASE_      		=     TFDistilBertForSequenceClassification(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     {'input_ids': input_ids, 'attention_mask': input_mask}
       SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase		)
       self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.num_labels)		)



 def  lowerCAmelCase_    (				self    :						List[str]							,							_lowerCAmelCase    :						Tuple							,							_lowerCAmelCase    :						str							,							_lowerCAmelCase    :						Optional[Any]							,							_lowerCAmelCase    :						str							,							_lowerCAmelCase    :						int							,							_lowerCAmelCase    :						int		):
       SCREAMING_SNAKE_CASE_      		=     self.num_choices
       SCREAMING_SNAKE_CASE_      		=     TFDistilBertForMultipleChoice(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     tf.tile(tf.expand_dims(_lowerCAmelCase							,							1		)							,							(1, self.num_choices, 1)		)
       SCREAMING_SNAKE_CASE_      		=     tf.tile(tf.expand_dims(_lowerCAmelCase							,							1		)							,							(1, self.num_choices, 1)		)
       SCREAMING_SNAKE_CASE_      		=     {
           'input_ids': multiple_choice_inputs_ids,
           'attention_mask': multiple_choice_input_mask,
       }
       SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase		)
       self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.num_choices)		)



 def  lowerCAmelCase_    (				self    :						Union[str, Any]							,							_lowerCAmelCase    :						Optional[Any]							,							_lowerCAmelCase    :						Optional[int]							,							_lowerCAmelCase    :						Optional[Any]							,							_lowerCAmelCase    :						Optional[int]							,							_lowerCAmelCase    :						Optional[Any]							,							_lowerCAmelCase    :						str		):
       SCREAMING_SNAKE_CASE_      		=     self.num_labels
       SCREAMING_SNAKE_CASE_      		=     TFDistilBertForTokenClassification(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     {'input_ids': input_ids, 'attention_mask': input_mask}
       SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase		)
       self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.seq_length, self.num_labels)		)




 def  lowerCAmelCase_    (				self    :						int		):
       SCREAMING_SNAKE_CASE_      		=     self.prepare_config_and_inputs()
       ((SCREAMING_SNAKE_CASE_)		,					(SCREAMING_SNAKE_CASE_)		,					(SCREAMING_SNAKE_CASE_)		,					(SCREAMING_SNAKE_CASE_)		,					(SCREAMING_SNAKE_CASE_)		,					(SCREAMING_SNAKE_CASE_))      		=     config_and_inputs
       SCREAMING_SNAKE_CASE_      		=     {'input_ids': input_ids, 'attention_mask': input_mask}
       return config, inputs_dict





@require_tf
class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE			,  _SCREAMING_SNAKE_CASE			,  unittest.TestCase   ):


 '''simple docstring'''







 lowercase_							       =       (
     (
         TFDistilBertModel,
         TFDistilBertForMaskedLM,
         TFDistilBertForQuestionAnswering,
         TFDistilBertForSequenceClassification,
         TFDistilBertForTokenClassification,
         TFDistilBertForMultipleChoice,
     )
     if is_tf_available()
     else None
 )
 lowercase_							       =       (
     {
         "feature-extraction": TFDistilBertModel,
         "fill-mask": TFDistilBertForMaskedLM,
         "question-answering": TFDistilBertForQuestionAnswering,
         "text-classification": TFDistilBertForSequenceClassification,
         "token-classification": TFDistilBertForTokenClassification,
         "zero-shot": TFDistilBertForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 lowercase_							       =       False
 lowercase_							       =       False



 def  lowerCAmelCase_    (				self    :						Optional[Any]		):
       SCREAMING_SNAKE_CASE_      		=     TFDistilBertModelTester(self		)
       SCREAMING_SNAKE_CASE_      		=     ConfigTester(self							,							config_class=_lowerCAmelCase							,							dim=37		)



 def  lowerCAmelCase_    (				self    :						int		):
       self.config_tester.run_common_tests()



 def  lowerCAmelCase_    (				self    :						Union[str, Any]		):
       SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_model(*_lowerCAmelCase		)



 def  lowerCAmelCase_    (				self    :						str		):
       SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_masked_lm(*_lowerCAmelCase		)



 def  lowerCAmelCase_    (				self    :						Union[str, Any]		):
       SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_question_answering(*_lowerCAmelCase		)



 def  lowerCAmelCase_    (				self    :						List[str]		):
       SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_sequence_classification(*_lowerCAmelCase		)



 def  lowerCAmelCase_    (				self    :						Any		):
       SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_multiple_choice(*_lowerCAmelCase		)



 def  lowerCAmelCase_    (				self    :						Any		):
       SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_token_classification(*_lowerCAmelCase		)




 @slow
 def  lowerCAmelCase_    (				self    :						Dict		):
       for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]		):
             SCREAMING_SNAKE_CASE_      		=     TFDistilBertModel.from_pretrained(_lowerCAmelCase		)
             self.assertIsNotNone(_lowerCAmelCase		)





@require_tf
class 			lowerCamelCase_				( unittest.TestCase   ):


 '''simple docstring'''







 @slow
 def  lowerCAmelCase_    (				self    :						Tuple		):
       SCREAMING_SNAKE_CASE_      		=     TFDistilBertModel.from_pretrained('distilbert-base-uncased'		)
       SCREAMING_SNAKE_CASE_      		=     tf.constant([[0, 1, 2, 3, 4, 5]]		)
       SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase		)[0]

       SCREAMING_SNAKE_CASE_      		=     [1, 6, 768]
       self.assertEqual(output.shape							,							_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_      		=     tf.constant(
           [
               [
                   [0.1926_1885, -0.1373_2955, 0.411_9799],
                   [0.2215_0156, -0.0742_2661, 0.3903_7204],
                   [0.2275_6018, -0.089_6414, 0.370_1467],
               ]
           ]		)
       tf.debugging.assert_near(output[:, :3, :3]							,							_lowerCAmelCase							,							atol=1E-4		)






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       __lowerCAmelCase					(       _UpperCamelCase   ,    unittest.TestCase      ):


	'''simple docstring'''





	_A													=				PriorTransformer
	_A													=				"hidden_states"

	@property
	def    snake_case__(						self:						Union[str, Any]			):
			lowercase__					:      List[Any]      =		4
			lowercase__					:      Optional[int]      =		8
			lowercase__					:      List[str]      =		7

			lowercase__					:      Optional[Any]      =		floats_tensor((batch_size, embedding_dim)			).to(lowerCamelCase_			)

			lowercase__					:      Union[str, Any]      =		floats_tensor((batch_size, embedding_dim)			).to(lowerCamelCase_			)
			lowercase__					:      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    snake_case__(						self:						int,					lowerCamelCase_:						Dict=0			):
			torch.manual_seed(lowerCamelCase_			)
			lowercase__					:      Tuple      =		4
			lowercase__					:      List[Any]      =		8
			lowercase__					:      List[str]      =		7

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

			lowercase__					:      Optional[int]      =		torch.randn((batch_size, embedding_dim)			).to(lowerCamelCase_			)
			lowercase__					:      List[str]      =		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    snake_case__(						self:						str			):
			return (4, 8)

	@property
	def    snake_case__(						self:						List[str]			):
			return (4, 8)

	def    snake_case__(						self:						Dict			):
			lowercase__					:      int      =		{
			    'num_attention_heads': 2,
			    'attention_head_dim': 4,
			    'num_layers': 2,
			    'embedding_dim': 8,
			    'num_embeddings': 7,
			    'additional_embeddings': 4,
			}
			lowercase__					:      Optional[Any]      =		self.dummy_input
			return init_dict, inputs_dict

	def    snake_case__(						self:						int			):
			lowercase__   ,				lowercase__					:      Dict      =		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_			)
			lowercase__					:      Tuple      =		model(**self.dummy_input			)[0]

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

	def    snake_case__(						self:						str			):
			lowercase__   ,				lowercase__					:      List[Any]      =		self.prepare_init_args_and_inputs_for_common()

			lowercase__					:      Optional[int]      =		self.model_class(**lowerCamelCase_			)
			lowercase__					:      int      =		inspect.signature(model.forward			)
			# signature.parameters is an OrderedDict => so arg_names order is deterministic
			lowercase__					:      Dict      =		[*signature.parameters.keys()]

			lowercase__					:      List[str]      =		['hidden_states', 'timestep']
			self.assertListEqual(arg_names[:2],					lowerCamelCase_			)





	def    snake_case__(						self:						Union[str, Any]			):
			lowercase__					:      str      =		PriorTransformer.from_pretrained('hf-internal-testing/prior-dummy'			)
			lowercase__					:      Optional[int]      =		model.to(lowerCamelCase_			)

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

			lowercase__					:      List[str]      =		self.get_dummy_seed_input()

			with torch.no_grad():
					lowercase__					:      str      =		model(**lowerCamelCase_			)[0]

			lowercase__					:      int      =		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.
			lowercase__					:      List[str]      =		torch.tensor([-1.3_4_3_6, -0.2_8_7_0, 0.7_5_3_8, 0.4_3_6_8, -0.0_2_3_9]			)
			self.assertTrue(torch_all_close(lowerCamelCase_,					lowerCamelCase_,					rtol=1E-2			)			)


@slow
class       __lowerCAmelCase					(       unittest.TestCase      ):


	'''simple docstring'''





	def    snake_case__(						self:						Tuple,					lowerCamelCase_:						Union[str, Any]=1,					lowerCamelCase_:						Tuple=768,					lowerCamelCase_:						Dict=77,					lowerCamelCase_:						Union[str, Any]=0			):
			torch.manual_seed(lowerCamelCase_			)
			lowercase__					:      Dict      =		batch_size
			lowercase__					:      Dict      =		embedding_dim
			lowercase__					:      Dict      =		num_embeddings

			lowercase__					:      int      =		torch.randn((batch_size, embedding_dim)			).to(lowerCamelCase_			)

			lowercase__					:      str      =		torch.randn((batch_size, embedding_dim)			).to(lowerCamelCase_			)
			lowercase__					:      List[str]      =		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    snake_case__(						self:						str			):
			# clean up the VRAM after each test
			super().tearDown()
			gc.collect()
			torch.cuda.empty_cache()





	@parameterized.expand(
	    [
	        # fmt: off
	        [13, [-0.5_8_6_1,  0.1_2_8_3, -0.0_9_3_1,  0.0_8_8_2,  0.4_4_7_6,  0.1_3_2_9, -0.0_4_9_8,  0.0_6_4_0]],
	        [37, [-0.4_9_1_3,  0.0_1_1_0, -0.0_4_8_3,  0.0_5_4_1,  0.4_9_5_4, -0.0_1_7_0,  0.0_3_5_4,  0.1_6_5_1]],
	        # fmt: on
	    ]			)
	def    snake_case__(						self:						Optional[Any],					lowerCamelCase_:						List[str],					lowerCamelCase_:						str			):
			lowercase__					:      List[Any]      =		PriorTransformer.from_pretrained('kandinsky-community/kandinsky-2-1-prior',					subfolder='prior'			)
			model.to(lowerCamelCase_			)
			lowercase__					:      Optional[int]      =		self.get_dummy_seed_input(seed=lowerCamelCase_			)

			with torch.no_grad():
					lowercase__					:      List[str]      =		model(**lowerCamelCase_			)[0]

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

			lowercase__					:      Union[str, Any]      =		sample[0, :8].flatten().cpu()
			print(lowerCamelCase_			)
			lowercase__					:      Optional[Any]      =		torch.tensor(lowerCamelCase_			)

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


